blob: 851f32a93b31359534fcba525bd704008ffbd9aa [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Frédéric Lécaille7da71292019-05-20 09:47:07 +020028#include <types/dict.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010030#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020031#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010032#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020033
William Lallemand222baf22016-11-19 02:00:33 +010034#include <proto/applet.h>
35#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090036#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020037#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020038#include <proto/port_range.h>
39#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020040#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010041#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020042#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010044#include <proto/stream_interface.h>
45#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020047#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010048#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049
Willy Tarreau3ff577e2018-08-02 11:48:52 +020050static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020051static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010052static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010053static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
William Dauchy3b614332020-05-02 21:52:36 +020054static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau21faa912012-10-10 08:27:36 +020056/* List head of all known server keywords */
57static struct srv_kw_list srv_keywords = {
58 .list = LIST_HEAD_INIT(srv_keywords.list)
59};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020060
Olivier Houchard9ea5d362019-02-14 18:29:09 +010061__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
62struct eb_root idle_conn_srv = EB_ROOT;
63struct task *idle_conn_task = NULL;
64struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
65struct list toremove_connections[MAX_THREADS];
Olivier Houchardba936e52019-07-11 15:49:00 +020066__decl_hathreads(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010067
Frédéric Lécaille7da71292019-05-20 09:47:07 +020068/* The server names dictionary */
69struct dict server_name_dict = {
70 .name = "server names",
71 .values = EB_ROOT_UNIQUE,
72};
73
Simon Hormana3608442013-11-01 16:46:15 +090074int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020075{
Emeric Brun52a91d32017-08-31 14:41:55 +020076 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020077 return s->down_time;
78
79 return now.tv_sec - s->last_change + s->down_time;
80}
Willy Tarreaubaaee002006-06-26 02:48:02 +020081
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050082int srv_lastsession(const struct server *s)
83{
84 if (s->counters.last_sess)
85 return now.tv_sec - s->counters.last_sess;
86
87 return -1;
88}
89
Simon Horman4a741432013-02-23 15:35:38 +090090int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020091{
Simon Horman4a741432013-02-23 15:35:38 +090092 const struct server *s = check->server;
93
Willy Tarreauff5ae352013-12-11 20:36:34 +010094 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090095 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010096
Emeric Brun52a91d32017-08-31 14:41:55 +020097 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090098 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010099
Simon Horman4a741432013-02-23 15:35:38 +0900100 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100101}
102
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100103/*
104 * Check that we did not get a hash collision.
105 * Unlikely, but it can happen.
106 */
107static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100108{
109 struct proxy *p = s->proxy;
110 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100111
112 for (tmpserv = p->srv; tmpserv != NULL;
113 tmpserv = tmpserv->next) {
114 if (tmpserv == s)
115 continue;
116 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
117 continue;
118 if (tmpserv->cookie &&
119 strcmp(tmpserv->cookie, s->cookie) == 0) {
120 ha_warning("We generated two equal cookies for two different servers.\n"
121 "Please change the secret key for '%s'.\n",
122 s->proxy->id);
123 }
124 }
125
126}
127
Willy Tarreau46b7f532018-08-21 11:54:26 +0200128/*
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200129 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200130 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100131void srv_set_dyncookie(struct server *s)
132{
133 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100134 char *tmpbuf;
135 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100136 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100137 size_t buffer_len;
138 int addr_len;
139 int port;
140
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200141 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
142
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 if ((s->flags & SRV_F_COOKIESET) ||
144 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
145 s->proxy->dyncookie_key == NULL)
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200146 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100147 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100148
149 if (s->addr.ss_family != AF_INET &&
150 s->addr.ss_family != AF_INET6)
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200151 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100152 /*
153 * Buffer to calculate the cookie value.
154 * The buffer contains the secret key + the server IP address
155 * + the TCP port.
156 */
157 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
158 /*
159 * The TCP port should use only 2 bytes, but is stored in
160 * an unsigned int in struct server, so let's use 4, to be
161 * on the safe side.
162 */
163 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200164 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100165 memcpy(tmpbuf, p->dyncookie_key, key_len);
166 memcpy(&(tmpbuf[key_len]),
167 s->addr.ss_family == AF_INET ?
168 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
169 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
170 addr_len);
171 /*
172 * Make sure it's the same across all the load balancers,
173 * no matter their endianness.
174 */
175 port = htonl(s->svc_port);
176 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
177 hash_value = XXH64(tmpbuf, buffer_len, 0);
178 memprintf(&s->cookie, "%016llx", hash_value);
179 if (!s->cookie)
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200180 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100181 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100182
183 /* Don't bother checking if the dyncookie is duplicated if
184 * the server is marked as "disabled", maybe it doesn't have
185 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100186 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100187 if (!(s->next_admin & SRV_ADMF_FMAINT))
188 srv_check_for_dup_dyncookie(s);
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200189 out:
190 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100191}
192
Willy Tarreau21faa912012-10-10 08:27:36 +0200193/*
194 * Registers the server keyword list <kwl> as a list of valid keywords for next
195 * parsing sessions.
196 */
197void srv_register_keywords(struct srv_kw_list *kwl)
198{
199 LIST_ADDQ(&srv_keywords.list, &kwl->list);
200}
201
202/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
203 * keyword is found with a NULL ->parse() function, then an attempt is made to
204 * find one with a valid ->parse() function. This way it is possible to declare
205 * platform-dependant, known keywords as NULL, then only declare them as valid
206 * if some options are met. Note that if the requested keyword contains an
207 * opening parenthesis, everything from this point is ignored.
208 */
209struct srv_kw *srv_find_kw(const char *kw)
210{
211 int index;
212 const char *kwend;
213 struct srv_kw_list *kwl;
214 struct srv_kw *ret = NULL;
215
216 kwend = strchr(kw, '(');
217 if (!kwend)
218 kwend = kw + strlen(kw);
219
220 list_for_each_entry(kwl, &srv_keywords.list, list) {
221 for (index = 0; kwl->kw[index].kw != NULL; index++) {
222 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
223 kwl->kw[index].kw[kwend-kw] == 0) {
224 if (kwl->kw[index].parse)
225 return &kwl->kw[index]; /* found it !*/
226 else
227 ret = &kwl->kw[index]; /* may be OK */
228 }
229 }
230 }
231 return ret;
232}
233
234/* Dumps all registered "server" keywords to the <out> string pointer. The
235 * unsupported keywords are only dumped if their supported form was not
236 * found.
237 */
238void srv_dump_kws(char **out)
239{
240 struct srv_kw_list *kwl;
241 int index;
242
243 *out = NULL;
244 list_for_each_entry(kwl, &srv_keywords.list, list) {
245 for (index = 0; kwl->kw[index].kw != NULL; index++) {
246 if (kwl->kw[index].parse ||
247 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
248 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
249 kwl->scope,
250 kwl->kw[index].kw,
251 kwl->kw[index].skip ? " <arg>" : "",
252 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
253 kwl->kw[index].parse ? "" : " (not supported)");
254 }
255 }
256 }
257}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100258
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100259/* Parse the "addr" server keyword */
260static int srv_parse_addr(char **args, int *cur_arg,
261 struct proxy *curproxy, struct server *newsrv, char **err)
262{
263 char *errmsg, *arg;
264 struct sockaddr_storage *sk;
265 int port1, port2;
266 struct protocol *proto;
267
268 errmsg = NULL;
269 arg = args[*cur_arg + 1];
270
271 if (!*arg) {
272 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
273 goto err;
274 }
275
276 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
277 if (!sk) {
278 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
279 goto err;
280 }
281
282 proto = protocol_by_family(sk->ss_family);
283 if (!proto || !proto->connect) {
284 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
285 args[*cur_arg], arg);
286 goto err;
287 }
288
289 if (port1 != port2) {
290 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
291 args[*cur_arg], arg);
292 goto err;
293 }
294
295 newsrv->check.addr = newsrv->agent.addr = *sk;
296 newsrv->flags |= SRV_F_CHECKADDR;
297 newsrv->flags |= SRV_F_AGENTADDR;
298
299 return 0;
300
301 err:
302 free(errmsg);
303 return ERR_ALERT | ERR_FATAL;
304}
305
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100306/* Parse the "agent-check" server keyword */
307static int srv_parse_agent_check(char **args, int *cur_arg,
308 struct proxy *curproxy, struct server *newsrv, char **err)
309{
310 newsrv->do_agent = 1;
311 return 0;
312}
313
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100314/* Parse the "backup" server keyword */
315static int srv_parse_backup(char **args, int *cur_arg,
316 struct proxy *curproxy, struct server *newsrv, char **err)
317{
318 newsrv->flags |= SRV_F_BACKUP;
319 return 0;
320}
321
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100322/* Parse the "check" server keyword */
323static int srv_parse_check(char **args, int *cur_arg,
324 struct proxy *curproxy, struct server *newsrv, char **err)
325{
326 newsrv->do_check = 1;
327 return 0;
328}
329
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100330/* Parse the "check-send-proxy" server keyword */
331static int srv_parse_check_send_proxy(char **args, int *cur_arg,
332 struct proxy *curproxy, struct server *newsrv, char **err)
333{
334 newsrv->check.send_proxy = 1;
335 return 0;
336}
337
Alexander Liu2a54bb72019-05-22 19:44:48 +0800338/* Parse the "check-via-socks4" server keyword */
339static int srv_parse_check_via_socks4(char **args, int *cur_arg,
340 struct proxy *curproxy, struct server *newsrv, char **err)
341{
342 newsrv->check.via_socks4 = 1;
343 return 0;
344}
345
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100346/* Parse the "cookie" server keyword */
347static int srv_parse_cookie(char **args, int *cur_arg,
348 struct proxy *curproxy, struct server *newsrv, char **err)
349{
350 char *arg;
351
352 arg = args[*cur_arg + 1];
353 if (!*arg) {
354 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
355 return ERR_ALERT | ERR_FATAL;
356 }
357
358 free(newsrv->cookie);
359 newsrv->cookie = strdup(arg);
360 newsrv->cklen = strlen(arg);
361 newsrv->flags |= SRV_F_COOKIESET;
362 return 0;
363}
364
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100365/* Parse the "disabled" server keyword */
366static int srv_parse_disabled(char **args, int *cur_arg,
367 struct proxy *curproxy, struct server *newsrv, char **err)
368{
Emeric Brun52a91d32017-08-31 14:41:55 +0200369 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
370 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100371 newsrv->check.state |= CHK_ST_PAUSED;
372 newsrv->check.health = 0;
373 return 0;
374}
375
376/* Parse the "enabled" server keyword */
377static int srv_parse_enabled(char **args, int *cur_arg,
378 struct proxy *curproxy, struct server *newsrv, char **err)
379{
Emeric Brun52a91d32017-08-31 14:41:55 +0200380 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
381 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100382 newsrv->check.state &= ~CHK_ST_PAUSED;
383 newsrv->check.health = newsrv->check.rise;
384 return 0;
385}
386
Willy Tarreau9c538e02019-01-23 10:21:49 +0100387static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
388{
389 char *arg;
390
391 arg = args[*cur_arg + 1];
392 if (!*arg) {
393 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
394 return ERR_ALERT | ERR_FATAL;
395 }
396 newsrv->max_reuse = atoi(arg);
397
398 return 0;
399}
400
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100401static 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 +0100402{
403 const char *res;
404 char *arg;
405 unsigned int time;
406
407 arg = args[*cur_arg + 1];
408 if (!*arg) {
409 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
410 return ERR_ALERT | ERR_FATAL;
411 }
412 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200413 if (res == PARSE_TIME_OVER) {
414 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
415 args[*cur_arg+1], args[*cur_arg]);
416 return ERR_ALERT | ERR_FATAL;
417 }
418 else if (res == PARSE_TIME_UNDER) {
419 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
420 args[*cur_arg+1], args[*cur_arg]);
421 return ERR_ALERT | ERR_FATAL;
422 }
423 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100424 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
425 *res, args[*cur_arg]);
426 return ERR_ALERT | ERR_FATAL;
427 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100428 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100429
430 return 0;
431}
432
Olivier Houchard006e3102018-12-10 18:30:32 +0100433static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
434{
435 char *arg;
436
437 arg = args[*cur_arg + 1];
438 if (!*arg) {
439 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
440 return ERR_ALERT | ERR_FATAL;
441 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100442
Olivier Houchard006e3102018-12-10 18:30:32 +0100443 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100444 if ((int)newsrv->max_idle_conns < -1) {
445 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
446 return ERR_ALERT | ERR_FATAL;
447 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100448
449 return 0;
450}
451
Willy Tarreaudff55432012-10-10 17:51:05 +0200452/* parse the "id" server keyword */
453static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
454{
455 struct eb32_node *node;
456
457 if (!*args[*cur_arg + 1]) {
458 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
459 return ERR_ALERT | ERR_FATAL;
460 }
461
462 newsrv->puid = atol(args[*cur_arg + 1]);
463 newsrv->conf.id.key = newsrv->puid;
464
465 if (newsrv->puid <= 0) {
466 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
467 return ERR_ALERT | ERR_FATAL;
468 }
469
470 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
471 if (node) {
472 struct server *target = container_of(node, struct server, conf.id);
473 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
474 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
475 target->id);
476 return ERR_ALERT | ERR_FATAL;
477 }
478
479 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200480 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200481 return 0;
482}
483
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100484/* Parse the "namespace" server keyword */
485static int srv_parse_namespace(char **args, int *cur_arg,
486 struct proxy *curproxy, struct server *newsrv, char **err)
487{
Willy Tarreaue5733232019-05-22 19:24:06 +0200488#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100489 char *arg;
490
491 arg = args[*cur_arg + 1];
492 if (!*arg) {
493 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
494 return ERR_ALERT | ERR_FATAL;
495 }
496
497 if (!strcmp(arg, "*")) {
498 /* Use the namespace associated with the connection (if present). */
499 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
500 return 0;
501 }
502
503 /*
504 * As this parser may be called several times for the same 'default-server'
505 * object, or for a new 'server' instance deriving from a 'default-server'
506 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
507 */
508 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
509
510 newsrv->netns = netns_store_lookup(arg, strlen(arg));
511 if (!newsrv->netns)
512 newsrv->netns = netns_store_insert(arg);
513
514 if (!newsrv->netns) {
515 memprintf(err, "Cannot open namespace '%s'", arg);
516 return ERR_ALERT | ERR_FATAL;
517 }
518
519 return 0;
520#else
521 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
522 return ERR_ALERT | ERR_FATAL;
523#endif
524}
525
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100526/* Parse the "no-agent-check" server keyword */
527static int srv_parse_no_agent_check(char **args, int *cur_arg,
528 struct proxy *curproxy, struct server *newsrv, char **err)
529{
530 free_check(&newsrv->agent);
531 newsrv->agent.inter = 0;
532 newsrv->agent.port = 0;
533 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
534 newsrv->do_agent = 0;
535 return 0;
536}
537
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100538/* Parse the "no-backup" server keyword */
539static int srv_parse_no_backup(char **args, int *cur_arg,
540 struct proxy *curproxy, struct server *newsrv, char **err)
541{
542 newsrv->flags &= ~SRV_F_BACKUP;
543 return 0;
544}
545
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100546/* Parse the "no-check" server keyword */
547static int srv_parse_no_check(char **args, int *cur_arg,
548 struct proxy *curproxy, struct server *newsrv, char **err)
549{
550 free_check(&newsrv->check);
551 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
552 newsrv->do_check = 0;
553 return 0;
554}
555
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100556/* Parse the "no-check-send-proxy" server keyword */
557static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
558 struct proxy *curproxy, struct server *newsrv, char **err)
559{
560 newsrv->check.send_proxy = 0;
561 return 0;
562}
563
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100564/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200565static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100566{
567 srv->pp_opts &= ~flags;
568 return 0;
569}
570
571/* Parse the "no-send-proxy" server keyword */
572static int srv_parse_no_send_proxy(char **args, int *cur_arg,
573 struct proxy *curproxy, struct server *newsrv, char **err)
574{
575 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
576}
577
578/* Parse the "no-send-proxy-v2" server keyword */
579static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
580 struct proxy *curproxy, struct server *newsrv, char **err)
581{
582 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
583}
584
Frédéric Lécailleaeeb1c92019-07-04 14:19:06 +0200585/* Parse the "no-tfo" server keyword */
586static int srv_parse_no_tfo(char **args, int *cur_arg,
587 struct proxy *curproxy, struct server *newsrv, char **err)
588{
589 newsrv->flags &= ~SRV_F_FASTOPEN;
590 return 0;
591}
592
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100593/* Parse the "non-stick" server keyword */
594static int srv_parse_non_stick(char **args, int *cur_arg,
595 struct proxy *curproxy, struct server *newsrv, char **err)
596{
597 newsrv->flags |= SRV_F_NON_STICK;
598 return 0;
599}
600
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100601/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200602static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100603{
604 srv->pp_opts |= flags;
605 return 0;
606}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200607/* parse the "proto" server keyword */
608static int srv_parse_proto(char **args, int *cur_arg,
609 struct proxy *px, struct server *newsrv, char **err)
610{
611 struct ist proto;
612
613 if (!*args[*cur_arg + 1]) {
614 memprintf(err, "'%s' : missing value", args[*cur_arg]);
615 return ERR_ALERT | ERR_FATAL;
616 }
617 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
618 newsrv->mux_proto = get_mux_proto(proto);
619 if (!newsrv->mux_proto) {
620 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
621 return ERR_ALERT | ERR_FATAL;
622 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200623 return 0;
624}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100625
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100626/* parse the "proxy-v2-options" */
627static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
628 struct proxy *px, struct server *newsrv, char **err)
629{
630 char *p, *n;
631 for (p = args[*cur_arg+1]; p; p = n) {
632 n = strchr(p, ',');
633 if (n)
634 *n++ = '\0';
635 if (!strcmp(p, "ssl")) {
636 newsrv->pp_opts |= SRV_PP_V2_SSL;
637 } else if (!strcmp(p, "cert-cn")) {
638 newsrv->pp_opts |= SRV_PP_V2_SSL;
639 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100640 } else if (!strcmp(p, "cert-key")) {
641 newsrv->pp_opts |= SRV_PP_V2_SSL;
642 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
643 } else if (!strcmp(p, "cert-sig")) {
644 newsrv->pp_opts |= SRV_PP_V2_SSL;
645 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
646 } else if (!strcmp(p, "ssl-cipher")) {
647 newsrv->pp_opts |= SRV_PP_V2_SSL;
648 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100649 } else if (!strcmp(p, "authority")) {
650 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100651 } else if (!strcmp(p, "crc32c")) {
652 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100653 } else
654 goto fail;
655 }
656 return 0;
657 fail:
658 if (err)
659 memprintf(err, "'%s' : proxy v2 option not implemented", p);
660 return ERR_ALERT | ERR_FATAL;
661}
662
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100663/* Parse the "observe" server keyword */
664static int srv_parse_observe(char **args, int *cur_arg,
665 struct proxy *curproxy, struct server *newsrv, char **err)
666{
667 char *arg;
668
669 arg = args[*cur_arg + 1];
670 if (!*arg) {
671 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
672 return ERR_ALERT | ERR_FATAL;
673 }
674
675 if (!strcmp(arg, "none")) {
676 newsrv->observe = HANA_OBS_NONE;
677 }
678 else if (!strcmp(arg, "layer4")) {
679 newsrv->observe = HANA_OBS_LAYER4;
680 }
681 else if (!strcmp(arg, "layer7")) {
682 if (curproxy->mode != PR_MODE_HTTP) {
683 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
684 return ERR_ALERT;
685 }
686 newsrv->observe = HANA_OBS_LAYER7;
687 }
688 else {
689 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
690 "but got '%s'\n", args[*cur_arg], arg);
691 return ERR_ALERT | ERR_FATAL;
692 }
693
694 return 0;
695}
696
Frédéric Lécaille16186232017-03-14 16:42:49 +0100697/* Parse the "redir" server keyword */
698static int srv_parse_redir(char **args, int *cur_arg,
699 struct proxy *curproxy, struct server *newsrv, char **err)
700{
701 char *arg;
702
703 arg = args[*cur_arg + 1];
704 if (!*arg) {
705 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
706 return ERR_ALERT | ERR_FATAL;
707 }
708
709 free(newsrv->rdr_pfx);
710 newsrv->rdr_pfx = strdup(arg);
711 newsrv->rdr_len = strlen(arg);
712
713 return 0;
714}
715
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100716/* Parse the "send-proxy" server keyword */
717static int srv_parse_send_proxy(char **args, int *cur_arg,
718 struct proxy *curproxy, struct server *newsrv, char **err)
719{
720 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
721}
722
723/* Parse the "send-proxy-v2" server keyword */
724static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
725 struct proxy *curproxy, struct server *newsrv, char **err)
726{
727 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
728}
729
Frédéric Lécailledba97072017-03-17 15:33:50 +0100730
731/* Parse the "source" server keyword */
732static int srv_parse_source(char **args, int *cur_arg,
733 struct proxy *curproxy, struct server *newsrv, char **err)
734{
735 char *errmsg;
736 int port_low, port_high;
737 struct sockaddr_storage *sk;
738 struct protocol *proto;
739
740 errmsg = NULL;
741
742 if (!*args[*cur_arg + 1]) {
743 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
744 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
745 goto err;
746 }
747
748 /* 'sk' is statically allocated (no need to be freed). */
749 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
750 if (!sk) {
751 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
752 goto err;
753 }
754
755 proto = protocol_by_family(sk->ss_family);
756 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100757 ha_alert("'%s %s' : connect() not supported for this address family.\n",
758 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100759 goto err;
760 }
761
762 newsrv->conn_src.opts |= CO_SRC_BIND;
763 newsrv->conn_src.source_addr = *sk;
764
765 if (port_low != port_high) {
766 int i;
767
768 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100769 ha_alert("'%s' does not support port offsets (found '%s').\n",
770 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100771 goto err;
772 }
773
774 if (port_low <= 0 || port_low > 65535 ||
775 port_high <= 0 || port_high > 65535 ||
776 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100777 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 +0100778 goto err;
779 }
780 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
781 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
782 newsrv->conn_src.sport_range->ports[i] = port_low + i;
783 }
784
785 *cur_arg += 2;
786 while (*(args[*cur_arg])) {
787 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
788#if defined(CONFIG_HAP_TRANSPARENT)
789 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100790 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
791 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100792 goto err;
793 }
794 if (!strcmp(args[*cur_arg + 1], "client")) {
795 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
796 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
797 }
798 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
799 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
800 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
801 }
802 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
803 char *name, *end;
804
805 name = args[*cur_arg + 1] + 7;
806 while (isspace(*name))
807 name++;
808
809 end = name;
810 while (*end && !isspace(*end) && *end != ',' && *end != ')')
811 end++;
812
813 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
814 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
815 free(newsrv->conn_src.bind_hdr_name);
816 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
817 newsrv->conn_src.bind_hdr_len = end - name;
818 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
819 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
820 newsrv->conn_src.bind_hdr_occ = -1;
821
822 /* now look for an occurrence number */
823 while (isspace(*end))
824 end++;
825 if (*end == ',') {
826 end++;
827 name = end;
828 if (*end == '-')
829 end++;
830 while (isdigit((int)*end))
831 end++;
832 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
833 }
834
835 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100836 ha_alert("usesrc hdr_ip(name,num) does not support negative"
837 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100838 goto err;
839 }
840 }
841 else {
842 struct sockaddr_storage *sk;
843 int port1, port2;
844
845 /* 'sk' is statically allocated (no need to be freed). */
846 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
847 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100848 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100849 goto err;
850 }
851
852 proto = protocol_by_family(sk->ss_family);
853 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100854 ha_alert("'%s %s' : connect() not supported for this address family.\n",
855 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100856 goto err;
857 }
858
859 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100860 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
861 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100862 goto err;
863 }
864 newsrv->conn_src.tproxy_addr = *sk;
865 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
866 }
867 global.last_checks |= LSTCHK_NETADM;
868 *cur_arg += 2;
869 continue;
870#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100871 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 +0100872 goto err;
873#endif /* defined(CONFIG_HAP_TRANSPARENT) */
874 } /* "usesrc" */
875
876 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
877#ifdef SO_BINDTODEVICE
878 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100879 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100880 goto err;
881 }
882 free(newsrv->conn_src.iface_name);
883 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
884 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
885 global.last_checks |= LSTCHK_NETADM;
886#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100887 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100888 goto err;
889#endif
890 *cur_arg += 2;
891 continue;
892 }
893 /* this keyword in not an option of "source" */
894 break;
895 } /* while */
896
897 return 0;
898
899 err:
900 free(errmsg);
901 return ERR_ALERT | ERR_FATAL;
902}
903
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100904/* Parse the "stick" server keyword */
905static int srv_parse_stick(char **args, int *cur_arg,
906 struct proxy *curproxy, struct server *newsrv, char **err)
907{
908 newsrv->flags &= ~SRV_F_NON_STICK;
909 return 0;
910}
911
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100912/* Parse the "track" server keyword */
913static int srv_parse_track(char **args, int *cur_arg,
914 struct proxy *curproxy, struct server *newsrv, char **err)
915{
916 char *arg;
917
918 arg = args[*cur_arg + 1];
919 if (!*arg) {
920 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
921 return ERR_ALERT | ERR_FATAL;
922 }
923
924 free(newsrv->trackit);
925 newsrv->trackit = strdup(arg);
926
927 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800928}
929
930/* Parse the "socks4" server keyword */
931static int srv_parse_socks4(char **args, int *cur_arg,
932 struct proxy *curproxy, struct server *newsrv, char **err)
933{
934 char *errmsg;
935 int port_low, port_high;
936 struct sockaddr_storage *sk;
937 struct protocol *proto;
938
939 errmsg = NULL;
940
941 if (!*args[*cur_arg + 1]) {
942 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
943 goto err;
944 }
945
946 /* 'sk' is statically allocated (no need to be freed). */
947 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
948 if (!sk) {
949 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
950 goto err;
951 }
952
953 proto = protocol_by_family(sk->ss_family);
954 if (!proto || !proto->connect) {
955 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
956 goto err;
957 }
958
959 newsrv->flags |= SRV_F_SOCKS4_PROXY;
960 newsrv->socks4_addr = *sk;
961
962 if (port_low != port_high) {
963 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
964 goto err;
965 }
966
967 if (!port_low) {
968 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
969 goto err;
970 }
971
972 return 0;
973
974 err:
975 free(errmsg);
976 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100977}
978
Frédéric Lécailledba97072017-03-17 15:33:50 +0100979
Willy Tarreau034c88c2017-01-23 23:36:45 +0100980/* parse the "tfo" server keyword */
981static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
982{
983 newsrv->flags |= SRV_F_FASTOPEN;
984 return 0;
985}
986
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200987/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200988 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200989 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200990 *
991 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200992 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200993void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200994{
Willy Tarreau87b09662015-04-03 00:22:06 +0200995 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200996
Willy Tarreau87b09662015-04-03 00:22:06 +0200997 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
998 if (stream->srv_conn == srv)
999 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001000}
1001
1002/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001003 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001004 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001005 *
1006 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001007 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001008void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001009{
1010 struct server *srv;
1011
1012 for (srv = px->srv; srv != NULL; srv = srv->next)
1013 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001014 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001015}
1016
Willy Tarreaubda92272014-05-20 21:55:30 +02001017/* Appends some information to a message string related to a server going UP or
1018 * DOWN. If both <forced> and <reason> are null and the server tracks another
1019 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001020 * If <check> is non-null, an entire string describing the check result will be
1021 * appended after a comma and a space (eg: to report some information from the
1022 * check that changed the state). In the other case, the string will be built
1023 * using the check results stored into the struct server if present.
1024 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001025 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001026 *
1027 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001029void srv_append_status(struct buffer *msg, struct server *s,
1030 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001031{
Emeric Brun5a133512017-10-19 14:42:30 +02001032 short status = s->op_st_chg.status;
1033 short code = s->op_st_chg.code;
1034 long duration = s->op_st_chg.duration;
1035 char *desc = s->op_st_chg.reason;
1036
1037 if (check) {
1038 status = check->status;
1039 code = check->code;
1040 duration = check->duration;
1041 desc = check->desc;
1042 }
1043
1044 if (status != -1) {
1045 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1046
1047 if (status >= HCHK_STATUS_L57DATA)
1048 chunk_appendf(msg, ", code: %d", code);
1049
1050 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001051 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001052
1053 chunk_appendf(msg, ", info: \"");
1054
1055 chunk_initlen(&src, desc, 0, strlen(desc));
1056 chunk_asciiencode(msg, &src, '"');
1057
1058 chunk_appendf(msg, "\"");
1059 }
1060
1061 if (duration >= 0)
1062 chunk_appendf(msg, ", check duration: %ldms", duration);
1063 }
1064 else if (desc && *desc) {
1065 chunk_appendf(msg, ", %s", desc);
1066 }
1067 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001068 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001069 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001070
1071 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001072 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001073 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1074 " %d sessions active, %d requeued, %d remaining in queue",
1075 s->proxy->srv_act, s->proxy->srv_bck,
1076 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1077 s->cur_sess, xferred, s->nbpend);
1078 else
1079 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1080 " %d sessions requeued, %d total in queue",
1081 s->proxy->srv_act, s->proxy->srv_bck,
1082 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1083 xferred, s->nbpend);
1084 }
1085}
1086
Emeric Brun5a133512017-10-19 14:42:30 +02001087/* Marks server <s> down, regardless of its checks' statuses. The server is
1088 * registered in a list to postpone the counting of the remaining servers on
1089 * the proxy and transfers queued streams whenever possible to other servers at
1090 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1091 * non-null as the reason for going down or the available data from the check
1092 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001093 *
1094 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001095 */
Emeric Brun5a133512017-10-19 14:42:30 +02001096void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001097{
1098 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001099
Emeric Brun64cc49c2017-10-03 14:46:45 +02001100 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001101 return;
1102
Emeric Brun52a91d32017-08-31 14:41:55 +02001103 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001104 *s->op_st_chg.reason = 0;
1105 s->op_st_chg.status = -1;
1106 if (reason) {
1107 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1108 }
1109 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001110 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001111 s->op_st_chg.code = check->code;
1112 s->op_st_chg.status = check->status;
1113 s->op_st_chg.duration = check->duration;
1114 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001115
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001116 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001117 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001118
Emeric Brun9f0b4582017-10-23 14:39:51 +02001119 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001120 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001121 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001122 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001123 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001124}
1125
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001126/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001127 * in maintenance. The server is registered in a list to postpone the counting
1128 * of the remaining servers on the proxy and tries to grab requests from the
1129 * proxy at a sync point. Maintenance servers are ignored. It stores the
1130 * <reason> if non-null as the reason for going down or the available data
1131 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001132 *
1133 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001134 */
Emeric Brun5a133512017-10-19 14:42:30 +02001135void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001136{
1137 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001138
Emeric Brun64cc49c2017-10-03 14:46:45 +02001139 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001140 return;
1141
Emeric Brun52a91d32017-08-31 14:41:55 +02001142 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001143 return;
1144
Emeric Brun52a91d32017-08-31 14:41:55 +02001145 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001146 *s->op_st_chg.reason = 0;
1147 s->op_st_chg.status = -1;
1148 if (reason) {
1149 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1150 }
1151 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001152 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001153 s->op_st_chg.code = check->code;
1154 s->op_st_chg.status = check->status;
1155 s->op_st_chg.duration = check->duration;
1156 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001157
Emeric Brun64cc49c2017-10-03 14:46:45 +02001158 if (s->slowstart <= 0)
1159 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001160
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001161 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001162 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001163
Emeric Brun9f0b4582017-10-23 14:39:51 +02001164 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001165 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001166 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001167 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001168 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001169}
1170
Willy Tarreau8eb77842014-05-21 13:54:57 +02001171/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001172 * isn't in maintenance. The server is registered in a list to postpone the
1173 * counting of the remaining servers on the proxy and tries to grab requests
1174 * from the proxy. Maintenance servers are ignored. It stores the
1175 * <reason> if non-null as the reason for going down or the available data
1176 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001177 * up. Note that it makes use of the trash to build the log strings, so <reason>
1178 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001179 *
1180 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001181 */
Emeric Brun5a133512017-10-19 14:42:30 +02001182void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001183{
1184 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001185
Emeric Brun64cc49c2017-10-03 14:46:45 +02001186 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001187 return;
1188
Emeric Brun52a91d32017-08-31 14:41:55 +02001189 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001190 return;
1191
Emeric Brun52a91d32017-08-31 14:41:55 +02001192 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001193 *s->op_st_chg.reason = 0;
1194 s->op_st_chg.status = -1;
1195 if (reason) {
1196 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1197 }
1198 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001199 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001200 s->op_st_chg.code = check->code;
1201 s->op_st_chg.status = check->status;
1202 s->op_st_chg.duration = check->duration;
1203 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001204
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001205 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001206 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001207
Emeric Brun9f0b4582017-10-23 14:39:51 +02001208 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001209 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001210 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001211 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001212 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001213}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001214
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001215/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1216 * enforce either maint mode or drain mode. It is not allowed to set more than
1217 * one flag at once. The equivalent "inherited" flag is propagated to all
1218 * tracking servers. Maintenance mode disables health checks (but not agent
1219 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001220 * is done. If <cause> is non-null, it will be displayed at the end of the log
1221 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001222 *
1223 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001224 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001225void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001226{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001227 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001228
1229 if (!mode)
1230 return;
1231
1232 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001233 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001234 return;
1235
Emeric Brun52a91d32017-08-31 14:41:55 +02001236 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001237 if (cause)
1238 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1239
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001240 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001241 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001242
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001243 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001244 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1245 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001246 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001247
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001248 /* compute the inherited flag to propagate */
1249 if (mode & SRV_ADMF_MAINT)
1250 mode = SRV_ADMF_IMAINT;
1251 else if (mode & SRV_ADMF_DRAIN)
1252 mode = SRV_ADMF_IDRAIN;
1253
Emeric Brun9f0b4582017-10-23 14:39:51 +02001254 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001255 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001256 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001257 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001258 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001259}
1260
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001261/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1262 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1263 * than one flag at once. The equivalent "inherited" flag is propagated to all
1264 * tracking servers. Leaving maintenance mode re-enables health checks. When
1265 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001266 *
1267 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001268 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001269void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001270{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001271 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001272
1273 if (!mode)
1274 return;
1275
1276 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001277 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001278 return;
1279
Emeric Brun52a91d32017-08-31 14:41:55 +02001280 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001281
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001282 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001283 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001284
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001285 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001286 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1287 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001288 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001289
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001290 if (mode & SRV_ADMF_MAINT)
1291 mode = SRV_ADMF_IMAINT;
1292 else if (mode & SRV_ADMF_DRAIN)
1293 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001294
Emeric Brun9f0b4582017-10-23 14:39:51 +02001295 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001296 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001297 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001298 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001299 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001300}
1301
Willy Tarreau757478e2016-11-03 19:22:19 +01001302/* principle: propagate maint and drain to tracking servers. This is useful
1303 * upon startup so that inherited states are correct.
1304 */
1305static void srv_propagate_admin_state(struct server *srv)
1306{
1307 struct server *srv2;
1308
1309 if (!srv->trackers)
1310 return;
1311
1312 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001313 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001314 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001315 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001316
Emeric Brun52a91d32017-08-31 14:41:55 +02001317 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001318 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001319 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001320 }
1321}
1322
1323/* Compute and propagate the admin states for all servers in proxy <px>.
1324 * Only servers *not* tracking another one are considered, because other
1325 * ones will be handled when the server they track is visited.
1326 */
1327void srv_compute_all_admin_states(struct proxy *px)
1328{
1329 struct server *srv;
1330
1331 for (srv = px->srv; srv; srv = srv->next) {
1332 if (srv->track)
1333 continue;
1334 srv_propagate_admin_state(srv);
1335 }
1336}
1337
Willy Tarreaudff55432012-10-10 17:51:05 +02001338/* Note: must not be declared <const> as its list will be overwritten.
1339 * Please take care of keeping this list alphabetically sorted, doing so helps
1340 * all code contributors.
1341 * Optional keywords are also declared with a NULL ->parse() function so that
1342 * the config parser can report an appropriate error when a known keyword was
1343 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001344 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001345 */
1346static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001347 { "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 +01001348 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001349 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001350 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001351 { "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 +01001352 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001353 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1354 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001355 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001356 { "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 +01001357 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001358 { "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 +01001359 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001360 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001361 { "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 +01001362 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1363 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécailleaeeb1c92019-07-04 14:19:06 +02001364 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001365 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001366 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001367 { "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 +01001368 { "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 +02001369 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001370 { "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 +01001371 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001372 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1373 { "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 +02001374 { "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 +01001375 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard1a9c25f2019-07-04 13:34:10 +02001376 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001377 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001378 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1379 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001380 { NULL, NULL, 0 },
1381}};
1382
Willy Tarreau0108d902018-11-25 19:14:37 +01001383INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001384
Willy Tarreau004e0452013-11-21 11:22:01 +01001385/* Recomputes the server's eweight based on its state, uweight, the current time,
1386 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001387 * state is automatically disabled if the time is elapsed. If <must_update> is
1388 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001389 *
1390 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001391 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001392void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001393{
1394 struct proxy *px = sv->proxy;
1395 unsigned w;
1396
1397 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1398 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001399 if (sv->next_state == SRV_ST_STARTING)
1400 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001401 }
1402
1403 /* We must take care of not pushing the server to full throttle during slow starts.
1404 * It must also start immediately, at least at the minimal step when leaving maintenance.
1405 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001406 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001407 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1408 else
1409 w = px->lbprm.wdiv;
1410
Emeric Brun52a91d32017-08-31 14:41:55 +02001411 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001412
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001413 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001414 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001415 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001416}
1417
Willy Tarreaubaaee002006-06-26 02:48:02 +02001418/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001419 * Parses weight_str and configures sv accordingly.
1420 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001421 *
1422 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001423 */
1424const char *server_parse_weight_change_request(struct server *sv,
1425 const char *weight_str)
1426{
1427 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001428 long int w;
1429 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001430
1431 px = sv->proxy;
1432
1433 /* if the weight is terminated with '%', it is set relative to
1434 * the initial weight, otherwise it is absolute.
1435 */
1436 if (!*weight_str)
1437 return "Require <weight> or <weight%>.\n";
1438
Simon Hormanb796afa2013-02-12 10:45:53 +09001439 w = strtol(weight_str, &end, 10);
1440 if (end == weight_str)
1441 return "Empty weight string empty or preceded by garbage";
1442 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001443 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001444 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001445 /* Avoid integer overflow */
1446 if (w > 25600)
1447 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001448 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001449 if (w > 256)
1450 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001451 }
1452 else if (w < 0 || w > 256)
1453 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001454 else if (end[0] != '\0')
1455 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001456
1457 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1458 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1459
1460 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001461 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001462
1463 return NULL;
1464}
1465
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001466/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001467 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1468 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001469 * Returns:
1470 * - error string on error
1471 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001472 *
1473 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001474 */
1475const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001476 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001477{
1478 unsigned char ip[INET6_ADDRSTRLEN];
1479
1480 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001481 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001482 return NULL;
1483 }
1484 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001485 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001486 return NULL;
1487 }
1488
1489 return "Could not understand IP address format.\n";
1490}
1491
Willy Tarreau46b7f532018-08-21 11:54:26 +02001492/*
1493 * Must be called with the server lock held.
1494 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001495const char *server_parse_maxconn_change_request(struct server *sv,
1496 const char *maxconn_str)
1497{
1498 long int v;
1499 char *end;
1500
1501 if (!*maxconn_str)
1502 return "Require <maxconn>.\n";
1503
1504 v = strtol(maxconn_str, &end, 10);
1505 if (end == maxconn_str)
1506 return "maxconn string empty or preceded by garbage";
1507 else if (end[0] != '\0')
1508 return "Trailing garbage in maxconn string";
1509
1510 if (sv->maxconn == sv->minconn) { // static maxconn
1511 sv->maxconn = sv->minconn = v;
1512 } else { // dynamic maxconn
1513 sv->maxconn = v;
1514 }
1515
1516 if (may_dequeue_tasks(sv, sv->proxy))
1517 process_srv_queue(sv);
1518
1519 return NULL;
1520}
1521
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001522#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001523static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1524 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001525{
1526 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001527 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001528 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001529 NULL,
1530 };
1531
1532 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001533 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001534
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001535 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1536}
1537
1538static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1539{
1540 struct sample_expr *expr;
1541
1542 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 +01001543 if (!expr) {
1544 memprintf(err, "error detected while parsing sni expression : %s", *err);
1545 return ERR_ALERT | ERR_FATAL;
1546 }
1547
1548 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1549 memprintf(err, "error detected while parsing sni expression : "
1550 " fetch method '%s' extracts information from '%s', "
1551 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001552 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001553 return ERR_ALERT | ERR_FATAL;
1554 }
1555
1556 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1557 release_sample_expr(newsrv->ssl_ctx.sni);
1558 newsrv->ssl_ctx.sni = expr;
1559
1560 return 0;
1561}
1562#endif
1563
1564static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1565{
1566 if (err && *err) {
1567 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001568 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 +01001569 }
1570 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001571 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1572 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001573}
1574
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001575static void srv_conn_src_sport_range_cpy(struct server *srv,
1576 struct server *src)
1577{
1578 int range_sz;
1579
1580 range_sz = src->conn_src.sport_range->size;
1581 if (range_sz > 0) {
1582 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1583 if (srv->conn_src.sport_range != NULL) {
1584 int i;
1585
1586 for (i = 0; i < range_sz; i++) {
1587 srv->conn_src.sport_range->ports[i] =
1588 src->conn_src.sport_range->ports[i];
1589 }
1590 }
1591 }
1592}
1593
1594/*
1595 * Copy <src> server connection source settings to <srv> server everything needed.
1596 */
1597static void srv_conn_src_cpy(struct server *srv, struct server *src)
1598{
1599 srv->conn_src.opts = src->conn_src.opts;
1600 srv->conn_src.source_addr = src->conn_src.source_addr;
1601
1602 /* Source port range copy. */
1603 if (src->conn_src.sport_range != NULL)
1604 srv_conn_src_sport_range_cpy(srv, src);
1605
1606#ifdef CONFIG_HAP_TRANSPARENT
1607 if (src->conn_src.bind_hdr_name != NULL) {
1608 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1609 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1610 }
1611 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1612 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1613#endif
1614 if (src->conn_src.iface_name != NULL)
1615 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1616}
1617
1618/*
1619 * Copy <src> server SSL settings to <srv> server allocating
1620 * everything needed.
1621 */
1622#if defined(USE_OPENSSL)
1623static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1624{
1625 if (src->ssl_ctx.ca_file != NULL)
1626 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1627 if (src->ssl_ctx.crl_file != NULL)
1628 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1629 if (src->ssl_ctx.client_crt != NULL)
1630 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1631
1632 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1633
1634 if (src->ssl_ctx.verify_host != NULL)
1635 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1636 if (src->ssl_ctx.ciphers != NULL)
1637 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin770bc8a2020-04-22 11:40:18 +02001638 if (src->ssl_ctx.options)
1639 srv->ssl_ctx.options = src->ssl_ctx.options;
1640 if (src->ssl_ctx.methods.flags)
1641 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1642 if (src->ssl_ctx.methods.min)
1643 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1644 if (src->ssl_ctx.methods.max)
1645 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1646
Willy Tarreau5db847a2019-05-09 14:13:35 +02001647#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001648 if (src->ssl_ctx.ciphersuites != NULL)
1649 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1650#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001651 if (src->sni_expr != NULL)
1652 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001653
1654#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1655 if (src->ssl_ctx.alpn_str) {
1656 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1657 if (srv->ssl_ctx.alpn_str) {
1658 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1659 src->ssl_ctx.alpn_len);
1660 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1661 }
1662 }
1663#endif
1664#ifdef OPENSSL_NPN_NEGOTIATED
1665 if (src->ssl_ctx.npn_str) {
1666 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1667 if (srv->ssl_ctx.npn_str) {
1668 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1669 src->ssl_ctx.npn_len);
1670 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1671 }
1672 }
1673#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001674}
1675#endif
1676
1677/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001678 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001679 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001680 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001681 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001682static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001683{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001684 char *hostname_dn;
1685 int hostname_len, hostname_dn_len;
1686
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001687 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001688 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001689
Christopher Faulet67957bd2017-09-27 11:00:59 +02001690 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001691 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1693 hostname_dn, trash.size);
1694 if (hostname_dn_len == -1)
1695 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001696
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001697
Christopher Faulet67957bd2017-09-27 11:00:59 +02001698 free(srv->hostname);
1699 free(srv->hostname_dn);
1700 srv->hostname = strdup(hostname);
1701 srv->hostname_dn = strdup(hostname_dn);
1702 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001703 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001704 goto err;
1705
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001706 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001707
1708 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001709 free(srv->hostname); srv->hostname = NULL;
1710 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001711 return -1;
1712}
1713
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001714/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001715 * Copy <src> server settings to <srv> server allocating
1716 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001717 * This function is not supposed to be called at any time, but only
1718 * during server settings parsing or during server allocations from
1719 * a server template, and just after having calloc()'ed a new server.
1720 * So, <src> may only be a default server (when parsing server settings)
1721 * or a server template (during server allocations from a server template).
1722 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1723 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001724 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001725static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001726{
1727 /* Connection source settings copy */
1728 srv_conn_src_cpy(srv, src);
1729
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001730 if (srv_tmpl) {
1731 srv->addr = src->addr;
1732 srv->svc_port = src->svc_port;
1733 }
1734
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001735 srv->pp_opts = src->pp_opts;
1736 if (src->rdr_pfx != NULL) {
1737 srv->rdr_pfx = strdup(src->rdr_pfx);
1738 srv->rdr_len = src->rdr_len;
1739 }
1740 if (src->cookie != NULL) {
1741 srv->cookie = strdup(src->cookie);
1742 srv->cklen = src->cklen;
1743 }
1744 srv->use_ssl = src->use_ssl;
Willy Tarreaufb389562019-12-11 15:43:45 +01001745 srv->check.addr = src->check.addr;
1746 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001747 srv->check.use_ssl = src->check.use_ssl;
1748 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001749 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001750 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001751 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001752 /* Note: 'flags' field has potentially been already initialized. */
1753 srv->flags |= src->flags;
1754 srv->do_check = src->do_check;
1755 srv->do_agent = src->do_agent;
1756 if (srv->check.port)
1757 srv->flags |= SRV_F_CHECKPORT;
1758 srv->check.inter = src->check.inter;
1759 srv->check.fastinter = src->check.fastinter;
1760 srv->check.downinter = src->check.downinter;
1761 srv->agent.use_ssl = src->agent.use_ssl;
1762 srv->agent.port = src->agent.port;
1763 if (src->agent.send_string != NULL)
1764 srv->agent.send_string = strdup(src->agent.send_string);
1765 srv->agent.send_string_len = src->agent.send_string_len;
1766 srv->agent.inter = src->agent.inter;
1767 srv->agent.fastinter = src->agent.fastinter;
1768 srv->agent.downinter = src->agent.downinter;
1769 srv->maxqueue = src->maxqueue;
1770 srv->minconn = src->minconn;
1771 srv->maxconn = src->maxconn;
1772 srv->slowstart = src->slowstart;
1773 srv->observe = src->observe;
1774 srv->onerror = src->onerror;
1775 srv->onmarkeddown = src->onmarkeddown;
1776 srv->onmarkedup = src->onmarkedup;
1777 if (src->trackit != NULL)
1778 srv->trackit = strdup(src->trackit);
1779 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1780 srv->uweight = srv->iweight = src->iweight;
1781
1782 srv->check.send_proxy = src->check.send_proxy;
1783 /* health: up, but will fall down at first failure */
1784 srv->check.rise = srv->check.health = src->check.rise;
1785 srv->check.fall = src->check.fall;
1786
1787 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001788 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1789 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1790 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001791 srv->check.state |= CHK_ST_PAUSED;
1792 srv->check.health = 0;
1793 }
1794
1795 /* health: up but will fall down at first failure */
1796 srv->agent.rise = srv->agent.health = src->agent.rise;
1797 srv->agent.fall = src->agent.fall;
1798
1799 if (src->resolvers_id != NULL)
1800 srv->resolvers_id = strdup(src->resolvers_id);
1801 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001802 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001803 if (srv->dns_opts.family_prio == AF_UNSPEC)
1804 srv->dns_opts.family_prio = AF_INET6;
1805 memcpy(srv->dns_opts.pref_net,
1806 src->dns_opts.pref_net,
1807 sizeof srv->dns_opts.pref_net);
1808 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1809
1810 srv->init_addr_methods = src->init_addr_methods;
1811 srv->init_addr = src->init_addr;
1812#if defined(USE_OPENSSL)
1813 srv_ssl_settings_cpy(srv, src);
1814#endif
1815#ifdef TCP_USER_TIMEOUT
1816 srv->tcp_ut = src->tcp_ut;
1817#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001818 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001819 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001820 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001821 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001822
Olivier Houchard8da5f982017-08-04 18:35:36 +02001823 if (srv_tmpl)
1824 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001825
1826 srv->check.via_socks4 = src->check.via_socks4;
1827 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001828}
1829
William Lallemand313bfd12018-10-26 14:47:32 +02001830struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001831{
1832 struct server *srv;
1833
1834 srv = calloc(1, sizeof *srv);
1835 if (!srv)
1836 return NULL;
1837
1838 srv->obj_type = OBJ_TYPE_SERVER;
1839 srv->proxy = proxy;
1840 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001841 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001842
Emeric Brun52a91d32017-08-31 14:41:55 +02001843 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001844 srv->last_change = now.tv_sec;
1845
1846 srv->check.status = HCHK_STATUS_INI;
1847 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001848 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001849 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1850
1851 srv->agent.status = HCHK_STATUS_INI;
1852 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001853 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001854 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1855
Willy Tarreau975b1552019-06-06 16:25:55 +02001856 /* please don't put default server settings here, they are set in
1857 * init_default_instance().
1858 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001859 return srv;
1860}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001861
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001862#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1863static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1864 struct server *srv, struct proxy *proxy)
1865{
1866 int ret;
1867 char *err = NULL;
1868
1869 if (!srv->sni_expr)
1870 return 0;
1871
1872 ret = server_parse_sni_expr(srv, proxy, &err);
1873 if (!ret)
1874 return 0;
1875
1876 display_parser_err(file, linenum, args, cur_arg, &err);
1877 free(err);
1878
1879 return ret;
1880}
1881#endif
1882
1883/*
1884 * Server initializations finalization.
1885 * Initialize health check, agent check and SNI expression if enabled.
1886 * Must not be called for a default server instance.
1887 */
1888static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1889 struct server *srv, struct proxy *px)
1890{
Christopher Faulet1ebb12c2020-04-27 11:17:10 +02001891#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001892 int ret;
Christopher Faulet1ebb12c2020-04-27 11:17:10 +02001893#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001894
Christopher Fauletc6e07702020-03-26 19:48:20 +01001895 if (srv->do_check && srv->trackit) {
1896 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1897 file, linenum);
1898 return ERR_ALERT | ERR_FATAL;
1899 }
1900
1901 if (srv->do_agent && !srv->agent.port) {
1902 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1903 file, linenum, srv->id);
1904 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001905 }
1906
1907#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1908 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1909 return ret;
1910#endif
1911
1912 if (srv->flags & SRV_F_BACKUP)
1913 px->srv_bck++;
1914 else
1915 px->srv_act++;
1916 srv_lb_commit_status(srv);
1917
1918 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001919err:
1920 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001921}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001922
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001923/*
1924 * Parse as much as possible such a range string argument: low[-high]
1925 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1926 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1927 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1928 * Returns 0 if succeeded, -1 if not.
1929 */
1930static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1931{
1932 char *nb_high_arg;
1933
1934 *nb_high = 0;
1935 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001936 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001937
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001938 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001939 *nb_high_arg++ = '\0';
1940 *nb_high = atoi(nb_high_arg);
1941 }
1942 else {
1943 *nb_high += *nb_low;
1944 *nb_low = 1;
1945 }
1946
1947 if (*nb_low < 0 || *nb_high < *nb_low)
1948 return -1;
1949
1950 return 0;
1951}
1952
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001953static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1954{
1955 chunk_printf(&trash, "%s%d", prefix, nb);
1956 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001957 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001958}
1959
1960/*
1961 * Initialize as much as possible servers from <srv> server template.
1962 * Note that a server template is a special server with
1963 * a few different parameters than a server which has
1964 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001965 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001966 * 'srv' template included.
1967 */
1968static int server_template_init(struct server *srv, struct proxy *px)
1969{
1970 int i;
1971 struct server *newsrv;
1972
1973 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001974 newsrv = new_server(px);
1975 if (!newsrv)
1976 goto err;
1977
1978 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001979 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001980#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1981 if (newsrv->sni_expr) {
1982 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1983 if (!newsrv->ssl_ctx.sni)
1984 goto err;
1985 }
1986#endif
1987 /* Set this new server ID. */
1988 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1989
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001990 /* Linked backwards first. This will be restablished after parsing. */
1991 newsrv->next = px->srv;
1992 px->srv = newsrv;
1993 }
1994 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1995
1996 return i - srv->tmpl_info.nb_low;
1997
1998 err:
1999 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2000 if (newsrv) {
2001#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2002 release_sample_expr(newsrv->ssl_ctx.sni);
2003#endif
2004 free_check(&newsrv->agent);
2005 free_check(&newsrv->check);
2006 }
2007 free(newsrv);
2008 return i - srv->tmpl_info.nb_low;
2009}
2010
Frédéric Lécaille9b0d59d2020-04-03 09:43:47 +02002011int 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 +02002012{
2013 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002014 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002015 char *errmsg = NULL;
2016 int err_code = 0;
2017 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002018 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002019
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002020 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002021 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002022 !strcmp(args[0], "default-server") ||
2023 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002024 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002025 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002026 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002027 int srv_tmpl = !defsrv && !srv;
2028 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002029
2030 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002031 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002032 err_code |= ERR_ALERT | ERR_FATAL;
2033 goto out;
2034 }
2035 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002036 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002037
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002038 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002039 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002040 if (!*args[2]) {
Frédéric Lécaille9b0d59d2020-04-03 09:43:47 +02002041 if (in_peers_section) {
2042 return 0;
2043 }
2044 else {
2045 /* 'server' line number of argument check. */
2046 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2047 file, linenum, args[0]);
2048 err_code |= ERR_ALERT | ERR_FATAL;
2049 goto out;
2050 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002051 }
2052
2053 err = invalid_char(args[1]);
2054 }
2055 else if (srv_tmpl) {
2056 if (!*args[3]) {
2057 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002058 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 +02002059 file, linenum, args[0]);
2060 err_code |= ERR_ALERT | ERR_FATAL;
2061 goto out;
2062 }
2063
2064 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002065 }
2066
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002067 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002068 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 +02002069 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002070 err_code |= ERR_ALERT | ERR_FATAL;
2071 goto out;
2072 }
2073
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002074 cur_arg = 2;
2075 if (srv_tmpl) {
2076 /* Parse server-template <nb | range> arg. */
2077 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002078 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002079 file, linenum, args[0], args[cur_arg]);
2080 err_code |= ERR_ALERT | ERR_FATAL;
2081 goto out;
2082 }
2083 cur_arg++;
2084 }
2085
Willy Tarreau272adea2014-03-31 10:39:59 +02002086 if (!defsrv) {
2087 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002088 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 struct protocol *proto;
2090
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002091 newsrv = new_server(curproxy);
2092 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002093 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002094 err_code |= ERR_ALERT | ERR_ABORT;
2095 goto out;
2096 }
2097
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002098 if (srv_tmpl) {
2099 newsrv->tmpl_info.nb_low = tmpl_range_low;
2100 newsrv->tmpl_info.nb_high = tmpl_range_high;
2101 }
2102
Willy Tarreau272adea2014-03-31 10:39:59 +02002103 /* the servers are linked backwards first */
2104 newsrv->next = curproxy->srv;
2105 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 newsrv->conf.file = strdup(file);
2107 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002108 /* Note: for a server template, its id is its prefix.
2109 * This is a temporary id which will be used for server allocations to come
2110 * after parsing.
2111 */
2112 if (srv)
2113 newsrv->id = strdup(args[1]);
2114 else
2115 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002116
2117 /* several ways to check the port component :
2118 * - IP => port=+0, relative (IPv4 only)
2119 * - IP: => port=+0, relative
2120 * - IP:N => port=N, absolute
2121 * - IP:+N => port=+N, relative
2122 * - IP:-N => port=-N, relative
2123 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002124 if (!parse_addr)
2125 goto skip_addr;
2126
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002127 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002128 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002129 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 err_code |= ERR_ALERT | ERR_FATAL;
2131 goto out;
2132 }
2133
2134 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002135 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002136 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002137 file, linenum, args[0], args[1]);
2138 err_code |= ERR_ALERT | ERR_FATAL;
2139 goto out;
2140 }
2141
2142 if (!port1 || !port2) {
2143 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002144 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002145 }
2146 else if (port1 != port2) {
2147 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002148 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002149 file, linenum, args[0], args[1], args[2]);
2150 err_code |= ERR_ALERT | ERR_FATAL;
2151 goto out;
2152 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002153
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002154 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002155 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002156 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002157 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002158 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2159 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2160 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002161 err_code |= ERR_ALERT | ERR_FATAL;
2162 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002163 }
2164 }
2165 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002166 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002167 file, linenum, newsrv->id);
2168 err_code |= ERR_ALERT | ERR_FATAL;
2169 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002170 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002171 }
2172
Willy Tarreau272adea2014-03-31 10:39:59 +02002173 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002174 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002175
Olivier Houchard8da5f982017-08-04 18:35:36 +02002176 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002177 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002178 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002179 err_code |= ERR_ALERT | ERR_FATAL;
2180 goto out;
2181 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002182
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002183 cur_arg++;
2184 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002185 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002186 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002187 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002188 } else {
2189 newsrv = &curproxy->defsrv;
2190 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002191 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002192 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002193 }
2194
2195 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002196 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002197 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002198
2199 if (err == PARSE_TIME_OVER) {
2200 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2201 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2202 err_code |= ERR_ALERT | ERR_FATAL;
2203 goto out;
2204 }
2205 else if (err == PARSE_TIME_UNDER) {
2206 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2207 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2208 err_code |= ERR_ALERT | ERR_FATAL;
2209 goto out;
2210 }
2211 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002212 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002213 file, linenum, *err, newsrv->id);
2214 err_code |= ERR_ALERT | ERR_FATAL;
2215 goto out;
2216 }
2217 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002218 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002219 file, linenum, val, args[cur_arg], newsrv->id);
2220 err_code |= ERR_ALERT | ERR_FATAL;
2221 goto out;
2222 }
2223 newsrv->agent.inter = val;
2224 cur_arg += 2;
2225 }
Misiekea849332017-01-09 09:39:51 +01002226 else if (!strcmp(args[cur_arg], "agent-addr")) {
2227 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002228 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002229 goto out;
2230 }
2231
2232 cur_arg += 2;
2233 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002234 else if (!strcmp(args[cur_arg], "agent-port")) {
2235 global.maxsock++;
2236 newsrv->agent.port = atol(args[cur_arg + 1]);
2237 cur_arg += 2;
2238 }
James Brown55f9ff12015-10-21 18:19:05 -07002239 else if (!strcmp(args[cur_arg], "agent-send")) {
2240 global.maxsock++;
2241 free(newsrv->agent.send_string);
2242 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2243 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2244 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2245 cur_arg += 2;
2246 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002247 else if (!strcmp(args[cur_arg], "init-addr")) {
2248 char *p, *end;
2249 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002250 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002251
2252 newsrv->init_addr_methods = 0;
2253 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2254
2255 for (p = args[cur_arg + 1]; *p; p = end) {
2256 /* cut on next comma */
2257 for (end = p; *end && *end != ','; end++);
2258 if (*end)
2259 *(end++) = 0;
2260
Willy Tarreau4310d362016-11-02 15:05:56 +01002261 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002262 if (!strcmp(p, "libc")) {
2263 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2264 }
2265 else if (!strcmp(p, "last")) {
2266 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2267 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002268 else if (!strcmp(p, "none")) {
2269 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2270 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002271 else if (str2ip2(p, &sa, 0)) {
2272 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002273 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002274 file, linenum, args[cur_arg], p);
2275 err_code |= ERR_ALERT | ERR_FATAL;
2276 goto out;
2277 }
2278 newsrv->init_addr = sa;
2279 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2280 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002281 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002282 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 +02002283 file, linenum, args[cur_arg], p);
2284 err_code |= ERR_ALERT | ERR_FATAL;
2285 goto out;
2286 }
2287 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002288 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002289 file, linenum, args[cur_arg], p);
2290 err_code |= ERR_ALERT | ERR_FATAL;
2291 goto out;
2292 }
2293 }
2294 cur_arg += 2;
2295 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002296 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002297 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002298 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2299 cur_arg += 2;
2300 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002301 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2302 char *p, *end;
2303
2304 for (p = args[cur_arg + 1]; *p; p = end) {
2305 /* cut on next comma */
2306 for (end = p; *end && *end != ','; end++);
2307 if (*end)
2308 *(end++) = 0;
2309
2310 if (!strcmp(p, "allow-dup-ip")) {
2311 newsrv->dns_opts.accept_duplicate_ip = 1;
2312 }
2313 else if (!strcmp(p, "prevent-dup-ip")) {
2314 newsrv->dns_opts.accept_duplicate_ip = 0;
2315 }
2316 else {
2317 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2318 file, linenum, args[cur_arg], p);
2319 err_code |= ERR_ALERT | ERR_FATAL;
2320 goto out;
2321 }
2322 }
2323
2324 cur_arg += 2;
2325 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002326 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2327 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002328 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002329 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002330 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002331 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002332 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002333 file, linenum, args[cur_arg]);
2334 err_code |= ERR_ALERT | ERR_FATAL;
2335 goto out;
2336 }
2337 cur_arg += 2;
2338 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002339 else if (!strcmp(args[cur_arg], "resolve-net")) {
2340 char *p, *e;
2341 unsigned char mask;
2342 struct dns_options *opt;
2343
2344 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002345 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002346 file, linenum, args[cur_arg]);
2347 err_code |= ERR_ALERT | ERR_FATAL;
2348 goto out;
2349 }
2350
2351 opt = &newsrv->dns_opts;
2352
2353 /* Split arguments by comma, and convert it from ipv4 or ipv6
2354 * string network in in_addr or in6_addr.
2355 */
2356 p = args[cur_arg + 1];
2357 e = p;
2358 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002359 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002360 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002361 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002362 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2363 err_code |= ERR_ALERT | ERR_FATAL;
2364 goto out;
2365 }
2366 /* look for end or comma. */
2367 while (*e != ',' && *e != '\0')
2368 e++;
2369 if (*e == ',') {
2370 *e = '\0';
2371 e++;
2372 }
2373 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2374 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2375 /* Try to convert input string from ipv4 or ipv6 network. */
2376 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2377 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2378 &mask)) {
2379 /* Try to convert input string from ipv6 network. */
2380 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2381 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2382 } else {
2383 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002384 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002385 file, linenum, args[cur_arg], p);
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
2388 }
2389 opt->pref_net_nb++;
2390 p = e;
2391 }
2392
2393 cur_arg += 2;
2394 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002395 else if (!strcmp(args[cur_arg], "rise")) {
2396 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002397 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002398 file, linenum, args[cur_arg]);
2399 err_code |= ERR_ALERT | ERR_FATAL;
2400 goto out;
2401 }
2402
2403 newsrv->check.rise = atol(args[cur_arg + 1]);
2404 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002405 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002406 file, linenum, args[cur_arg]);
2407 err_code |= ERR_ALERT | ERR_FATAL;
2408 goto out;
2409 }
2410
2411 if (newsrv->check.health)
2412 newsrv->check.health = newsrv->check.rise;
2413 cur_arg += 2;
2414 }
2415 else if (!strcmp(args[cur_arg], "fall")) {
2416 newsrv->check.fall = atol(args[cur_arg + 1]);
2417
2418 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002419 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002420 file, linenum, args[cur_arg]);
2421 err_code |= ERR_ALERT | ERR_FATAL;
2422 goto out;
2423 }
2424
2425 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002426 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002427 file, linenum, args[cur_arg]);
2428 err_code |= ERR_ALERT | ERR_FATAL;
2429 goto out;
2430 }
2431
2432 cur_arg += 2;
2433 }
2434 else if (!strcmp(args[cur_arg], "inter")) {
2435 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002436
2437 if (err == PARSE_TIME_OVER) {
2438 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2439 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2440 err_code |= ERR_ALERT | ERR_FATAL;
2441 goto out;
2442 }
2443 else if (err == PARSE_TIME_UNDER) {
2444 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2445 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2446 err_code |= ERR_ALERT | ERR_FATAL;
2447 goto out;
2448 }
2449 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002450 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002451 file, linenum, *err, newsrv->id);
2452 err_code |= ERR_ALERT | ERR_FATAL;
2453 goto out;
2454 }
2455 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002456 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002457 file, linenum, val, args[cur_arg], newsrv->id);
2458 err_code |= ERR_ALERT | ERR_FATAL;
2459 goto out;
2460 }
2461 newsrv->check.inter = val;
2462 cur_arg += 2;
2463 }
2464 else if (!strcmp(args[cur_arg], "fastinter")) {
2465 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002466
2467 if (err == PARSE_TIME_OVER) {
2468 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2469 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2470 err_code |= ERR_ALERT | ERR_FATAL;
2471 goto out;
2472 }
2473 else if (err == PARSE_TIME_UNDER) {
2474 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2475 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2476 err_code |= ERR_ALERT | ERR_FATAL;
2477 goto out;
2478 }
2479 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002480 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002481 file, linenum, *err, newsrv->id);
2482 err_code |= ERR_ALERT | ERR_FATAL;
2483 goto out;
2484 }
2485 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002486 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002487 file, linenum, val, args[cur_arg], newsrv->id);
2488 err_code |= ERR_ALERT | ERR_FATAL;
2489 goto out;
2490 }
2491 newsrv->check.fastinter = val;
2492 cur_arg += 2;
2493 }
2494 else if (!strcmp(args[cur_arg], "downinter")) {
2495 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002496
2497 if (err == PARSE_TIME_OVER) {
2498 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2499 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2500 err_code |= ERR_ALERT | ERR_FATAL;
2501 goto out;
2502 }
2503 else if (err == PARSE_TIME_UNDER) {
2504 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2505 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2506 err_code |= ERR_ALERT | ERR_FATAL;
2507 goto out;
2508 }
2509 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002510 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002511 file, linenum, *err, newsrv->id);
2512 err_code |= ERR_ALERT | ERR_FATAL;
2513 goto out;
2514 }
2515 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002516 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002517 file, linenum, val, args[cur_arg], newsrv->id);
2518 err_code |= ERR_ALERT | ERR_FATAL;
2519 goto out;
2520 }
2521 newsrv->check.downinter = val;
2522 cur_arg += 2;
2523 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002524 else if (!strcmp(args[cur_arg], "port")) {
2525 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002526 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002527 cur_arg += 2;
2528 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002529 else if (!strcmp(args[cur_arg], "weight")) {
2530 int w;
2531 w = atol(args[cur_arg + 1]);
2532 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002533 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002534 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2535 err_code |= ERR_ALERT | ERR_FATAL;
2536 goto out;
2537 }
2538 newsrv->uweight = newsrv->iweight = w;
2539 cur_arg += 2;
2540 }
2541 else if (!strcmp(args[cur_arg], "minconn")) {
2542 newsrv->minconn = atol(args[cur_arg + 1]);
2543 cur_arg += 2;
2544 }
2545 else if (!strcmp(args[cur_arg], "maxconn")) {
2546 newsrv->maxconn = atol(args[cur_arg + 1]);
2547 cur_arg += 2;
2548 }
2549 else if (!strcmp(args[cur_arg], "maxqueue")) {
2550 newsrv->maxqueue = atol(args[cur_arg + 1]);
2551 cur_arg += 2;
2552 }
2553 else if (!strcmp(args[cur_arg], "slowstart")) {
2554 /* slowstart is stored in seconds */
2555 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002556
2557 if (err == PARSE_TIME_OVER) {
2558 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2559 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2560 err_code |= ERR_ALERT | ERR_FATAL;
2561 goto out;
2562 }
2563 else if (err == PARSE_TIME_UNDER) {
2564 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2565 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2566 err_code |= ERR_ALERT | ERR_FATAL;
2567 goto out;
2568 }
2569 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002570 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002571 file, linenum, *err, newsrv->id);
2572 err_code |= ERR_ALERT | ERR_FATAL;
2573 goto out;
2574 }
2575 newsrv->slowstart = (val + 999) / 1000;
2576 cur_arg += 2;
2577 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002578 else if (!strcmp(args[cur_arg], "on-error")) {
2579 if (!strcmp(args[cur_arg + 1], "fastinter"))
2580 newsrv->onerror = HANA_ONERR_FASTINTER;
2581 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2582 newsrv->onerror = HANA_ONERR_FAILCHK;
2583 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2584 newsrv->onerror = HANA_ONERR_SUDDTH;
2585 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2586 newsrv->onerror = HANA_ONERR_MARKDWN;
2587 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002588 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002589 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2590 file, linenum, args[cur_arg], args[cur_arg + 1]);
2591 err_code |= ERR_ALERT | ERR_FATAL;
2592 goto out;
2593 }
2594
2595 cur_arg += 2;
2596 }
2597 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2598 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2599 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2600 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002601 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002602 file, linenum, args[cur_arg], args[cur_arg + 1]);
2603 err_code |= ERR_ALERT | ERR_FATAL;
2604 goto out;
2605 }
2606
2607 cur_arg += 2;
2608 }
2609 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2610 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2611 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2612 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002613 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002614 file, linenum, args[cur_arg], args[cur_arg + 1]);
2615 err_code |= ERR_ALERT | ERR_FATAL;
2616 goto out;
2617 }
2618
2619 cur_arg += 2;
2620 }
2621 else if (!strcmp(args[cur_arg], "error-limit")) {
2622 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002623 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002624 file, linenum, args[cur_arg]);
2625 err_code |= ERR_ALERT | ERR_FATAL;
2626 goto out;
2627 }
2628
2629 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2630
2631 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002632 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002633 file, linenum, args[cur_arg]);
2634 err_code |= ERR_ALERT | ERR_FATAL;
2635 goto out;
2636 }
2637 cur_arg += 2;
2638 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002639 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002640 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002641 file, linenum, "usesrc", "source");
2642 err_code |= ERR_ALERT | ERR_FATAL;
2643 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002644 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002645 else {
2646 static int srv_dumped;
2647 struct srv_kw *kw;
2648 char *err;
2649
2650 kw = srv_find_kw(args[cur_arg]);
2651 if (kw) {
2652 char *err = NULL;
2653 int code;
2654
2655 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002656 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 +02002657 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002658 if (kw->skip != -1)
2659 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002660 err_code |= ERR_ALERT | ERR_FATAL;
2661 goto out;
2662 }
2663
2664 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002665 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002666 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002667 if (kw->skip != -1)
2668 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002669 err_code |= ERR_ALERT;
2670 continue;
2671 }
2672
2673 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2674 err_code |= code;
2675
2676 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002677 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002678 if (code & ERR_FATAL) {
2679 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002680 if (kw->skip != -1)
2681 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002682 goto out;
2683 }
2684 }
2685 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002686 if (kw->skip != -1)
2687 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002688 continue;
2689 }
2690
2691 err = NULL;
2692 if (!srv_dumped) {
2693 srv_dump_kws(&err);
2694 indent_msg(&err, 4);
2695 srv_dumped = 1;
2696 }
2697
Christopher Faulet767a84b2017-11-24 16:50:31 +01002698 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002699 file, linenum, args[0], args[1], args[cur_arg],
2700 err ? " Registered keywords :" : "", err ? err : "");
2701 free(err);
2702
2703 err_code |= ERR_ALERT | ERR_FATAL;
2704 goto out;
2705 }
2706 }
2707
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002708 if (!defsrv)
2709 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2710 if (err_code & ERR_FATAL)
2711 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002712 if (srv_tmpl)
2713 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002714 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002715 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002716 return 0;
2717
2718 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002719 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002720 free(errmsg);
2721 return err_code;
2722}
2723
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002724/* Returns a pointer to the first server matching either id <id>.
2725 * NULL is returned if no match is found.
2726 * the lookup is performed in the backend <bk>
2727 */
2728struct server *server_find_by_id(struct proxy *bk, int id)
2729{
2730 struct eb32_node *eb32;
2731 struct server *curserver;
2732
2733 if (!bk || (id ==0))
2734 return NULL;
2735
2736 /* <bk> has no backend capabilities, so it can't have a server */
2737 if (!(bk->cap & PR_CAP_BE))
2738 return NULL;
2739
2740 curserver = NULL;
2741
2742 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2743 if (eb32)
2744 curserver = container_of(eb32, struct server, conf.id);
2745
2746 return curserver;
2747}
2748
2749/* Returns a pointer to the first server matching either name <name>, or id
2750 * if <name> starts with a '#'. NULL is returned if no match is found.
2751 * the lookup is performed in the backend <bk>
2752 */
2753struct server *server_find_by_name(struct proxy *bk, const char *name)
2754{
2755 struct server *curserver;
2756
2757 if (!bk || !name)
2758 return NULL;
2759
2760 /* <bk> has no backend capabilities, so it can't have a server */
2761 if (!(bk->cap & PR_CAP_BE))
2762 return NULL;
2763
2764 curserver = NULL;
2765 if (*name == '#') {
2766 curserver = server_find_by_id(bk, atoi(name + 1));
2767 if (curserver)
2768 return curserver;
2769 }
2770 else {
2771 curserver = bk->srv;
2772
2773 while (curserver && (strcmp(curserver->id, name) != 0))
2774 curserver = curserver->next;
2775
2776 if (curserver)
2777 return curserver;
2778 }
2779
2780 return NULL;
2781}
2782
2783struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2784{
2785 struct server *byname;
2786 struct server *byid;
2787
2788 if (!name && !id)
2789 return NULL;
2790
2791 if (diff)
2792 *diff = 0;
2793
2794 byname = byid = NULL;
2795
2796 if (name) {
2797 byname = server_find_by_name(bk, name);
2798 if (byname && (!id || byname->puid == id))
2799 return byname;
2800 }
2801
2802 /* remaining possibilities :
2803 * - name not set
2804 * - name set but not found
2805 * - name found but ID doesn't match
2806 */
2807 if (id) {
2808 byid = server_find_by_id(bk, id);
2809 if (byid) {
2810 if (byname) {
2811 /* use id only if forced by configuration */
2812 if (byid->flags & SRV_F_FORCED_ID) {
2813 if (diff)
2814 *diff |= 2;
2815 return byid;
2816 }
2817 else {
2818 if (diff)
2819 *diff |= 1;
2820 return byname;
2821 }
2822 }
2823
2824 /* remaining possibilities:
2825 * - name not set
2826 * - name set but not found
2827 */
2828 if (name && diff)
2829 *diff |= 2;
2830 return byid;
2831 }
2832
2833 /* id bot found */
2834 if (byname) {
2835 if (diff)
2836 *diff |= 1;
2837 return byname;
2838 }
2839 }
2840
2841 return NULL;
2842}
2843
Willy Tarreau46b7f532018-08-21 11:54:26 +02002844/* Update a server state using the parameters available in the params list.
2845 *
2846 * Grabs the server lock during operation.
2847 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002848static void srv_update_state(struct server *srv, int version, char **params)
2849{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002850 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002851 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002852
2853 /* fields since version 1
2854 * and common to all other upcoming versions
2855 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002856 enum srv_state srv_op_state;
2857 enum srv_admin srv_admin_state;
2858 unsigned srv_uweight, srv_iweight;
2859 unsigned long srv_last_time_change;
2860 short srv_check_status;
2861 enum chk_result srv_check_result;
2862 int srv_check_health;
2863 int srv_check_state, srv_agent_state;
2864 int bk_f_forced_id;
2865 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002866 int fqdn_set_by_cli;
2867 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002868 const char *port_str;
2869 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002870 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002871
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002872 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002873 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002874 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002875 switch (version) {
2876 case 1:
2877 /*
2878 * now we can proceed with server's state update:
2879 * srv_addr: params[0]
2880 * srv_op_state: params[1]
2881 * srv_admin_state: params[2]
2882 * srv_uweight: params[3]
2883 * srv_iweight: params[4]
2884 * srv_last_time_change: params[5]
2885 * srv_check_status: params[6]
2886 * srv_check_result: params[7]
2887 * srv_check_health: params[8]
2888 * srv_check_state: params[9]
2889 * srv_agent_state: params[10]
2890 * bk_f_forced_id: params[11]
2891 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002892 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002893 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002894 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002895 */
2896
2897 /* validating srv_op_state */
2898 p = NULL;
2899 errno = 0;
2900 srv_op_state = strtol(params[1], &p, 10);
2901 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2902 (srv_op_state != SRV_ST_STOPPED &&
2903 srv_op_state != SRV_ST_STARTING &&
2904 srv_op_state != SRV_ST_RUNNING &&
2905 srv_op_state != SRV_ST_STOPPING)) {
2906 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2907 }
2908
2909 /* validating srv_admin_state */
2910 p = NULL;
2911 errno = 0;
2912 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002913 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002914
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002915 /* inherited statuses will be recomputed later.
2916 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2917 */
2918 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002919
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002920 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2921 (srv_admin_state != 0 &&
2922 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002923 srv_admin_state != SRV_ADMF_CMAINT &&
2924 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002925 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002926 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002927 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2928 }
2929
2930 /* validating srv_uweight */
2931 p = NULL;
2932 errno = 0;
2933 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002934 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002935 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2936
2937 /* validating srv_iweight */
2938 p = NULL;
2939 errno = 0;
2940 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002941 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002942 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2943
2944 /* validating srv_last_time_change */
2945 p = NULL;
2946 errno = 0;
2947 srv_last_time_change = strtol(params[5], &p, 10);
2948 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2949 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2950
2951 /* validating srv_check_status */
2952 p = NULL;
2953 errno = 0;
2954 srv_check_status = strtol(params[6], &p, 10);
2955 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2956 (srv_check_status >= HCHK_STATUS_SIZE))
2957 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2958
2959 /* validating srv_check_result */
2960 p = NULL;
2961 errno = 0;
2962 srv_check_result = strtol(params[7], &p, 10);
2963 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2964 (srv_check_result != CHK_RES_UNKNOWN &&
2965 srv_check_result != CHK_RES_NEUTRAL &&
2966 srv_check_result != CHK_RES_FAILED &&
2967 srv_check_result != CHK_RES_PASSED &&
2968 srv_check_result != CHK_RES_CONDPASS)) {
2969 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2970 }
2971
2972 /* validating srv_check_health */
2973 p = NULL;
2974 errno = 0;
2975 srv_check_health = strtol(params[8], &p, 10);
2976 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2977 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2978
2979 /* validating srv_check_state */
2980 p = NULL;
2981 errno = 0;
2982 srv_check_state = strtol(params[9], &p, 10);
2983 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2984 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2985 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2986
2987 /* validating srv_agent_state */
2988 p = NULL;
2989 errno = 0;
2990 srv_agent_state = strtol(params[10], &p, 10);
2991 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2992 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2993 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2994
2995 /* validating bk_f_forced_id */
2996 p = NULL;
2997 errno = 0;
2998 bk_f_forced_id = strtol(params[11], &p, 10);
2999 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3000 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3001
3002 /* validating srv_f_forced_id */
3003 p = NULL;
3004 errno = 0;
3005 srv_f_forced_id = strtol(params[12], &p, 10);
3006 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3007 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3008
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003009 /* validating srv_fqdn */
3010 fqdn = params[13];
3011 if (fqdn && *fqdn == '-')
3012 fqdn = NULL;
3013 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3014 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3015 fqdn = NULL;
3016 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003017
Frédéric Lécaille31694712017-08-01 08:47:19 +02003018 port_str = params[14];
3019 if (port_str) {
3020 port = strl2uic(port_str, strlen(port_str));
3021 if (port > USHRT_MAX) {
3022 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3023 port_str = NULL;
3024 }
3025 }
3026
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003027 /* SRV record
3028 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3029 */
3030 srvrecord = params[15];
3031 if (srvrecord && *srvrecord != '_')
3032 srvrecord = NULL;
3033
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003034 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003035 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003036 goto out;
3037
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003038 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003039 /* recover operational state and apply it to this server
3040 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003041 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003042 switch (srv_op_state) {
3043 case SRV_ST_STOPPED:
3044 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003045 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003046 break;
3047 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003048 /* If rise == 1 there is no STARTING state, let's switch to
3049 * RUNNING
3050 */
3051 if (srv->check.rise == 1) {
3052 srv->check.health = srv->check.rise + srv->check.fall - 1;
3053 srv_set_running(srv, "", NULL);
3054 break;
3055 }
3056 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3057 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003058 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003059 break;
3060 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003061 /* If fall == 1 there is no STOPPING state, let's switch to
3062 * STOPPED
3063 */
3064 if (srv->check.fall == 1) {
3065 srv->check.health = 0;
3066 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3067 break;
3068 }
3069 if (srv->check.health < srv->check.rise ||
3070 srv->check.health > srv->check.rise + srv->check.fall - 2)
3071 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003072 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003073 break;
3074 case SRV_ST_RUNNING:
3075 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003076 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003077 break;
3078 }
3079
3080 /* When applying server state, the following rules apply:
3081 * - in case of a configuration change, we apply the setting from the new
3082 * configuration, regardless of old running state
3083 * - if no configuration change, we apply old running state only if old running
3084 * state is different from new configuration state
3085 */
3086 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003087 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3088 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003089 srv_adm_set_maint(srv);
3090 else
3091 srv_adm_set_ready(srv);
3092 }
3093 /* configuration is the same, let's compate old running state and new conf state */
3094 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003095 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003096 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003097 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003098 srv_adm_set_ready(srv);
3099 }
3100 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003101 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003102 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003103 * (srv->iweight is the weight set up in configuration).
3104 * There are two possible reasons for FDRAIN to have been present :
3105 * - previous config weight was zero
3106 * - "set server b/s drain" was sent to the CLI
3107 *
3108 * In the first case, we simply want to drop this drain state
3109 * if the new weight is not zero anymore, meaning the administrator
3110 * has intentionally turned the weight back to a positive value to
3111 * enable the server again after an operation. In the second case,
3112 * the drain state was forced on the CLI regardless of the config's
3113 * weight so we don't want a change to the config weight to lose this
3114 * status. What this means is :
3115 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3116 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003117 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003118 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003119 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003120 }
3121
3122 srv->last_change = date.tv_sec - srv_last_time_change;
3123 srv->check.status = srv_check_status;
3124 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003125
3126 /* Only case we want to apply is removing ENABLED flag which could have been
3127 * done by the "disable health" command over the stats socket
3128 */
3129 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3130 (srv_check_state & CHK_ST_CONFIGURED) &&
3131 !(srv_check_state & CHK_ST_ENABLED))
3132 srv->check.state &= ~CHK_ST_ENABLED;
3133
3134 /* Only case we want to apply is removing ENABLED flag which could have been
3135 * done by the "disable agent" command over the stats socket
3136 */
3137 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3138 (srv_agent_state & CHK_ST_CONFIGURED) &&
3139 !(srv_agent_state & CHK_ST_ENABLED))
3140 srv->agent.state &= ~CHK_ST_ENABLED;
3141
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003142 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3143 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003144 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003145 * It means that a configuration file change has precedence over a unix socket change
3146 * for server's weight
3147 *
3148 * by default, HAProxy applies the following weight when parsing the configuration
3149 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003150 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003151 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003152 srv->uweight = srv_uweight;
3153 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003154 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003155
Willy Tarreaue5a60682016-11-09 14:54:53 +01003156 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003157 if (strcmp(params[0], "-"))
3158 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003159
3160 if (fqdn && srv->hostname) {
3161 if (!strcmp(srv->hostname, fqdn)) {
3162 /* Here we reset the 'set from stats socket FQDN' flag
3163 * to support such transitions:
3164 * Let's say initial FQDN value is foo1 (in configuration file).
3165 * - FQDN changed from stats socket, from foo1 to foo2 value,
3166 * - FQDN changed again from file configuration (with the same previous value
3167 set from stats socket, from foo1 to foo2 value),
3168 * - reload for any other reason than a FQDN modification,
3169 * the configuration file FQDN matches the fqdn server state file value.
3170 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003171 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003172 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003173 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003174 }
3175 else {
3176 /* If the FDQN has been changed from stats socket,
3177 * apply fqdn state file value (which is the value set
3178 * from stats socket).
3179 */
3180 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003181 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003182 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003183 }
3184 }
3185 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003186 /* If all the conditions below are validated, this means
3187 * we're evaluating a server managed by SRV resolution
3188 */
3189 else if (fqdn && !srv->hostname && srvrecord) {
3190 int res;
3191
3192 /* we can't apply previous state if SRV record has changed */
3193 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3194 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);
3195 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3196 goto out;
3197 }
3198
3199 /* create or find a SRV resolution for this srv record */
3200 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3201 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3202 if (srv->srvrq == NULL) {
3203 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3204 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3205 goto out;
3206 }
3207
3208 /* prepare DNS resolution for this server */
3209 res = srv_prepare_for_resolution(srv, fqdn);
3210 if (res == -1) {
3211 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3212 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3213 goto out;
3214 }
3215
3216 /* configure check.port accordingly */
3217 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3218 !(srv->flags & SRV_F_CHECKPORT))
3219 srv->check.port = port;
3220
3221 /* Unset SRV_F_MAPPORTS for SRV records.
3222 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3223 * because no ports are provided in the configuration file.
3224 * This is because HAProxy will use the port found into the SRV record.
3225 */
3226 srv->flags &= ~SRV_F_MAPPORTS;
3227 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003228
Frédéric Lécaille31694712017-08-01 08:47:19 +02003229 if (port_str)
3230 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003231 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003232
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003233 break;
3234 default:
3235 chunk_appendf(msg, ", version '%d' not supported", version);
3236 }
3237
3238 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003239 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003240 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003241 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003242 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003243 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003244}
3245
3246/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3247 * For each proxy, it does the following:
3248 * - opens its server state file (either one or local one)
3249 * - read whole file, line by line
3250 * - analyse each line to check if it matches our current backend:
3251 * - backend name matches
3252 * - backend id matches if id is forced and name doesn't match
3253 * - if the server pointed by the line is found, then state is applied
3254 *
3255 * If the running backend uuid or id differs from the state file, then HAProxy reports
3256 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003257 *
3258 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003259 */
3260void apply_server_state(void)
3261{
3262 char *cur, *end;
3263 char mybuf[SRV_STATE_LINE_MAXLEN];
3264 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003265 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3266 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003267 int arg, srv_arg, version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003268 FILE *f;
3269 char *filepath;
3270 char globalfilepath[MAXPATHLEN + 1];
3271 char localfilepath[MAXPATHLEN + 1];
3272 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003273 struct proxy *curproxy, *bk;
3274 struct server *srv;
3275
3276 globalfilepathlen = 0;
3277 /* create the globalfilepath variable */
3278 if (global.server_state_file) {
3279 /* absolute path or no base directory provided */
3280 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3281 len = strlen(global.server_state_file);
3282 if (len > MAXPATHLEN) {
3283 globalfilepathlen = 0;
3284 goto globalfileerror;
3285 }
3286 memcpy(globalfilepath, global.server_state_file, len);
3287 globalfilepath[len] = '\0';
3288 globalfilepathlen = len;
3289 }
3290 else if (global.server_state_base) {
3291 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003292 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003293 globalfilepathlen = 0;
3294 goto globalfileerror;
3295 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003296 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003297 globalfilepath[len] = 0;
3298 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003299
3300 /* append a slash if needed */
3301 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3302 if (globalfilepathlen + 1 > MAXPATHLEN) {
3303 globalfilepathlen = 0;
3304 goto globalfileerror;
3305 }
3306 globalfilepath[globalfilepathlen++] = '/';
3307 }
3308
3309 len = strlen(global.server_state_file);
3310 if (globalfilepathlen + len > MAXPATHLEN) {
3311 globalfilepathlen = 0;
3312 goto globalfileerror;
3313 }
3314 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3315 globalfilepathlen += len;
3316 globalfilepath[globalfilepathlen++] = 0;
3317 }
3318 }
3319 globalfileerror:
3320 if (globalfilepathlen == 0)
3321 globalfilepath[0] = '\0';
3322
3323 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003324 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003325 /* servers are only in backends */
3326 if (!(curproxy->cap & PR_CAP_BE))
3327 continue;
3328 fileopenerr = 0;
3329 filepath = NULL;
3330
3331 /* search server state file path and name */
3332 switch (curproxy->load_server_state_from_file) {
3333 /* read servers state from global file */
3334 case PR_SRV_STATE_FILE_GLOBAL:
3335 /* there was an error while generating global server state file path */
3336 if (globalfilepathlen == 0)
3337 continue;
3338 filepath = globalfilepath;
3339 fileopenerr = 1;
3340 break;
3341 /* this backend has its own file */
3342 case PR_SRV_STATE_FILE_LOCAL:
3343 localfilepathlen = 0;
3344 localfilepath[0] = '\0';
3345 len = 0;
3346 /* create the localfilepath variable */
3347 /* absolute path or no base directory provided */
3348 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3349 len = strlen(curproxy->server_state_file_name);
3350 if (len > MAXPATHLEN) {
3351 localfilepathlen = 0;
3352 goto localfileerror;
3353 }
3354 memcpy(localfilepath, curproxy->server_state_file_name, len);
3355 localfilepath[len] = '\0';
3356 localfilepathlen = len;
3357 }
3358 else if (global.server_state_base) {
3359 len = strlen(global.server_state_base);
3360 localfilepathlen += len;
3361
3362 if (localfilepathlen > MAXPATHLEN) {
3363 localfilepathlen = 0;
3364 goto localfileerror;
3365 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003366 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003367 localfilepath[localfilepathlen] = 0;
3368
3369 /* append a slash if needed */
3370 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3371 if (localfilepathlen + 1 > MAXPATHLEN) {
3372 localfilepathlen = 0;
3373 goto localfileerror;
3374 }
3375 localfilepath[localfilepathlen++] = '/';
3376 }
3377
3378 len = strlen(curproxy->server_state_file_name);
3379 if (localfilepathlen + len > MAXPATHLEN) {
3380 localfilepathlen = 0;
3381 goto localfileerror;
3382 }
3383 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3384 localfilepathlen += len;
3385 localfilepath[localfilepathlen++] = 0;
3386 }
3387 filepath = localfilepath;
3388 localfileerror:
3389 if (localfilepathlen == 0)
3390 localfilepath[0] = '\0';
3391
3392 break;
3393 case PR_SRV_STATE_FILE_NONE:
3394 default:
3395 continue;
3396 }
3397
3398 /* preload global state file */
3399 errno = 0;
3400 f = fopen(filepath, "r");
3401 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003402 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003403 if (!f)
3404 continue;
3405
3406 mybuf[0] = '\0';
3407 mybuflen = 0;
3408 version = 0;
3409
3410 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003411 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003412 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003413 goto fileclose;
3414 }
3415
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003416 cur = mybuf;
3417 version = atoi(cur);
3418 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3419 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003420 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003421
3422 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3423 int bk_f_forced_id = 0;
3424 int check_id = 0;
3425 int check_name = 0;
3426
3427 mybuflen = strlen(mybuf);
3428 cur = mybuf;
3429 end = cur + mybuflen;
3430
3431 bk = NULL;
3432 srv = NULL;
3433
3434 /* we need at least one character */
3435 if (mybuflen == 0)
3436 continue;
3437
3438 /* ignore blank characters at the beginning of the line */
3439 while (isspace(*cur))
3440 ++cur;
3441
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003442 /* Ignore empty or commented lines */
3443 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003444 continue;
3445
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003446 /* truncated lines */
3447 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003449 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003450 }
3451
3452 /* Removes trailing '\n' */
3453 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003454
3455 /* we're now ready to move the line into *srv_params[] */
3456 params[0] = cur;
3457 arg = 1;
3458 srv_arg = 0;
3459 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3460 if (isspace(*cur)) {
3461 *cur = '\0';
3462 ++cur;
3463 while (isspace(*cur))
3464 ++cur;
3465 switch (version) {
3466 case 1:
3467 /*
3468 * srv_addr: params[4] => srv_params[0]
3469 * srv_op_state: params[5] => srv_params[1]
3470 * srv_admin_state: params[6] => srv_params[2]
3471 * srv_uweight: params[7] => srv_params[3]
3472 * srv_iweight: params[8] => srv_params[4]
3473 * srv_last_time_change: params[9] => srv_params[5]
3474 * srv_check_status: params[10] => srv_params[6]
3475 * srv_check_result: params[11] => srv_params[7]
3476 * srv_check_health: params[12] => srv_params[8]
3477 * srv_check_state: params[13] => srv_params[9]
3478 * srv_agent_state: params[14] => srv_params[10]
3479 * bk_f_forced_id: params[15] => srv_params[11]
3480 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003481 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003482 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003483 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003484 */
3485 if (arg >= 4) {
3486 srv_params[srv_arg] = cur;
3487 ++srv_arg;
3488 }
3489 break;
3490 }
3491
3492 params[arg] = cur;
3493 ++arg;
3494 }
3495 else {
3496 ++cur;
3497 }
3498 }
3499
3500 /* if line is incomplete line, then ignore it.
3501 * otherwise, update useful flags */
3502 switch (version) {
3503 case 1:
3504 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3505 continue;
3506 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3507 check_id = (atoi(params[0]) == curproxy->uuid);
3508 check_name = (strcmp(curproxy->id, params[1]) == 0);
3509 break;
3510 }
3511
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003512 bk = curproxy;
3513
3514 /* if backend can't be found, let's continue */
3515 if (!check_id && !check_name)
3516 continue;
3517 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003518 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003519 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3520 }
3521 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003522 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003523 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3524 /* if name doesn't match, we still want to update curproxy if the backend id
3525 * was forced in previous the previous configuration */
3526 if (!bk_f_forced_id)
3527 continue;
3528 }
3529
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003530 /* look for the server by its name: param[3] */
3531 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003532
3533 if (!srv) {
3534 /* if no server found, then warning and continue with next line */
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003535 ha_warning("can't find server '%s' in backend '%s'\n",
3536 params[3], params[1]);
3537 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3538 params[3], params[1]);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003539 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003540 }
3541
3542 /* now we can proceed with server's state update */
3543 srv_update_state(srv, version, srv_params);
3544 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003545fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003546 fclose(f);
3547 }
3548}
3549
Simon Horman7d09b9a2013-02-12 10:45:51 +09003550/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003551 * update a server's current IP address.
3552 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3553 * ip is in network format.
3554 * updater is a string which contains an information about the requester of the update.
3555 * updater is used if not NULL.
3556 *
3557 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003558 *
3559 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003560 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003561int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003562{
3563 /* generates a log line and a warning on stderr */
3564 if (1) {
3565 /* book enough space for both IPv4 and IPv6 */
3566 char oldip[INET6_ADDRSTRLEN];
3567 char newip[INET6_ADDRSTRLEN];
3568
3569 memset(oldip, '\0', INET6_ADDRSTRLEN);
3570 memset(newip, '\0', INET6_ADDRSTRLEN);
3571
3572 /* copy old IP address in a string */
3573 switch (s->addr.ss_family) {
3574 case AF_INET:
3575 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3576 break;
3577 case AF_INET6:
3578 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3579 break;
3580 };
3581
3582 /* copy new IP address in a string */
3583 switch (ip_sin_family) {
3584 case AF_INET:
3585 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3586 break;
3587 case AF_INET6:
3588 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3589 break;
3590 };
3591
3592 /* save log line into a buffer */
3593 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3594 s->proxy->id, s->id, oldip, newip, updater);
3595
3596 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003597 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003598
3599 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003600 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003601 }
3602
3603 /* save the new IP family */
3604 s->addr.ss_family = ip_sin_family;
3605 /* save the new IP address */
3606 switch (ip_sin_family) {
3607 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003608 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003609 break;
3610 case AF_INET6:
3611 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3612 break;
3613 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003614 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003615
3616 return 0;
3617}
3618
3619/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003620 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3621 *
3622 * Caller can pass its name through <updater> to get it integrated in the response message
3623 * returned by the function.
3624 *
3625 * The function first does the following, in that order:
3626 * - validates the new addr and/or port
3627 * - checks if an update is required (new IP or port is different than current ones)
3628 * - checks the update is allowed:
3629 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3630 * - allow all changes if no CHECKS are configured
3631 * - if CHECK is configured:
3632 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3633 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3634 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003635 *
3636 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003637 */
3638const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3639{
3640 struct sockaddr_storage sa;
3641 int ret, port_change_required;
3642 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003643 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003644 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003645 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003646
3647 msg = get_trash_chunk();
3648 chunk_reset(msg);
3649
3650 if (addr) {
3651 memset(&sa, 0, sizeof(struct sockaddr_storage));
3652 if (str2ip2(addr, &sa, 0) == NULL) {
3653 chunk_printf(msg, "Invalid addr '%s'", addr);
3654 goto out;
3655 }
3656
3657 /* changes are allowed on AF_INET* families only */
3658 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3659 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3660 goto out;
3661 }
3662
3663 /* collecting data currently setup */
3664 memset(current_addr, '\0', sizeof(current_addr));
3665 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3666 /* changes are allowed on AF_INET* families only */
3667 if ((ret != AF_INET) && (ret != AF_INET6)) {
3668 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3669 goto out;
3670 }
3671
3672 /* applying ADDR changes if required and allowed
3673 * ipcmp returns 0 when both ADDR are the same
3674 */
3675 if (ipcmp(&s->addr, &sa) == 0) {
3676 chunk_appendf(msg, "no need to change the addr");
3677 goto port;
3678 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003679 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003680 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003681
3682 /* we also need to update check's ADDR only if it uses the server's one */
3683 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003684 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003685 }
3686
3687 /* we also need to update agent ADDR only if it use the server's one */
3688 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003689 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003690 }
3691
3692 /* update report for caller */
3693 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3694 }
3695
3696 port:
3697 if (port) {
3698 char sign = '\0';
3699 char *endptr;
3700
3701 if (addr)
3702 chunk_appendf(msg, ", ");
3703
3704 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003705 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003706
3707 /* check if PORT change is required */
3708 port_change_required = 0;
3709
3710 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003711 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003712 new_port = strtol(port, &endptr, 10);
3713 if ((errno != 0) || (port == endptr)) {
3714 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3715 goto out;
3716 }
3717
3718 /* check if caller triggers a port mapped or offset */
3719 if (sign == '-' || (sign == '+')) {
3720 /* check if server currently uses port map */
3721 if (!(s->flags & SRV_F_MAPPORTS)) {
3722 /* switch from fixed port to port map mandatorily triggers
3723 * a port change */
3724 port_change_required = 1;
3725 /* check is configured
3726 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3727 * prevent PORT change if check doesn't have it's dedicated port while switching
3728 * to port mapping */
3729 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3730 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.");
3731 goto out;
3732 }
3733 }
3734 /* we're already using port maps */
3735 else {
3736 port_change_required = current_port != new_port;
3737 }
3738 }
3739 /* fixed port */
3740 else {
3741 port_change_required = current_port != new_port;
3742 }
3743
3744 /* applying PORT changes if required and update response message */
3745 if (port_change_required) {
3746 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003747 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003748 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003749
3750 /* prepare message */
3751 chunk_appendf(msg, "port changed from '");
3752 if (s->flags & SRV_F_MAPPORTS)
3753 chunk_appendf(msg, "+");
3754 chunk_appendf(msg, "%d' to '", current_port);
3755
3756 if (sign == '-') {
3757 s->flags |= SRV_F_MAPPORTS;
3758 chunk_appendf(msg, "%c", sign);
3759 /* just use for result output */
3760 new_port = -new_port;
3761 }
3762 else if (sign == '+') {
3763 s->flags |= SRV_F_MAPPORTS;
3764 chunk_appendf(msg, "%c", sign);
3765 }
3766 else {
3767 s->flags &= ~SRV_F_MAPPORTS;
3768 }
3769
3770 chunk_appendf(msg, "%d'", new_port);
3771
3772 /* we also need to update health checks port only if it uses server's realport */
3773 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3774 s->check.port = new_port;
3775 }
3776 }
3777 else {
3778 chunk_appendf(msg, "no need to change the port");
3779 }
3780 }
3781
3782out:
William Dauchy3b614332020-05-02 21:52:36 +02003783 if (changed) {
3784 /* force connection cleanup on the given server */
3785 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003786 srv_set_dyncookie(s);
William Dauchy3b614332020-05-02 21:52:36 +02003787 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003788 if (updater)
3789 chunk_appendf(msg, " by '%s'", updater);
3790 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003791 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003792}
3793
3794
3795/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003796 * update server status based on result of name resolution
3797 * returns:
3798 * 0 if server status is updated
3799 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003800 *
3801 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003802 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003803int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003804{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003805 struct dns_resolvers *resolvers = s->resolvers;
3806 struct dns_resolution *resolution = s->dns_requester->resolution;
3807 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808
3809 switch (resolution->status) {
3810 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003811 /* status when HAProxy has just (re)started.
3812 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003813 break;
3814
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003815 case RSLV_STATUS_VALID:
3816 /*
3817 * resume health checks
3818 * server will be turned back on if health check is safe
3819 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003820 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003821 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003822 return 1;
3823 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3824 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003825 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003826 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003827
Emeric Brun52a91d32017-08-31 14:41:55 +02003828 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003829 return 1;
3830 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3831 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3832 s->proxy->id, s->id);
3833
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003834 ha_warning("%s.\n", trash.area);
3835 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003836 return 0;
3837
3838 case RSLV_STATUS_NX:
3839 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003840 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3841 if (!tick_is_expired(exp, now_ms))
3842 break;
3843
3844 if (s->next_admin & SRV_ADMF_RMAINT)
3845 return 1;
3846 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3847 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003848
3849 case RSLV_STATUS_TIMEOUT:
3850 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003851 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3852 if (!tick_is_expired(exp, now_ms))
3853 break;
3854
3855 if (s->next_admin & SRV_ADMF_RMAINT)
3856 return 1;
3857 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3858 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003859
3860 case RSLV_STATUS_REFUSED:
3861 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003862 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3863 if (!tick_is_expired(exp, now_ms))
3864 break;
3865
3866 if (s->next_admin & SRV_ADMF_RMAINT)
3867 return 1;
3868 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3869 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003870
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003871 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003872 /* stop server if resolution failed for a long enough period */
3873 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3874 if (!tick_is_expired(exp, now_ms))
3875 break;
3876
3877 if (s->next_admin & SRV_ADMF_RMAINT)
3878 return 1;
3879 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3880 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003881 }
3882
3883 return 1;
3884}
3885
3886/*
3887 * Server Name Resolution valid response callback
3888 * It expects:
3889 * - <nameserver>: the name server which answered the valid response
3890 * - <response>: buffer containing a valid DNS response
3891 * - <response_len>: size of <response>
3892 * It performs the following actions:
3893 * - ignore response if current ip found and server family not met
3894 * - update with first new ip found if family is met and current IP is not found
3895 * returns:
3896 * 0 on error
3897 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003898 *
3899 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003900 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003901int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003902{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003903 struct server *s = NULL;
3904 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003905 void *serverip, *firstip;
3906 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003908 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003909 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003910
Christopher Faulet67957bd2017-09-27 11:00:59 +02003911 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003912 if (!s)
3913 return 1;
3914
Christopher Faulet67957bd2017-09-27 11:00:59 +02003915 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003916
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003917 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003918 firstip = NULL; /* pointer to the first valid response found */
3919 /* it will be used as the new IP if a change is required */
3920 firstip_sin_family = AF_UNSPEC;
3921 serverip = NULL; /* current server IP address */
3922
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003923 /* initializing server IP pointer */
3924 server_sin_family = s->addr.ss_family;
3925 switch (server_sin_family) {
3926 case AF_INET:
3927 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3928 break;
3929
3930 case AF_INET6:
3931 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3932 break;
3933
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003934 case AF_UNSPEC:
3935 break;
3936
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003937 default:
3938 goto invalid;
3939 }
3940
Baptiste Assmann729c9012017-05-22 15:13:10 +02003941 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003942 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003943 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003944
3945 switch (ret) {
3946 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003947 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003948
3949 case DNS_UPD_SRVIP_NOT_FOUND:
3950 goto save_ip;
3951
3952 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003953 goto invalid;
3954
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003955 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003956 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003957 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003958
Baptiste Assmannfad03182015-10-28 02:03:32 +01003959 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003960 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003961 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003962 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003963
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003964 default:
3965 goto invalid;
3966
3967 }
3968
3969 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003970 if (nameserver) {
3971 nameserver->counters.update++;
3972 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003973 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003974 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003975 else
3976 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003977 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003978
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003979 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003980 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003981 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003982
3983 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003984 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003985 nameserver->counters.invalid++;
3986 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003987 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003988 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003989 return 0;
3990}
3991
3992/*
3993 * Server Name Resolution error management callback
3994 * returns:
3995 * 0 on error
3996 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003997 *
3998 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003999 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004000int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004001{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004002 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004003
Christopher Faulet67957bd2017-09-27 11:00:59 +02004004 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004005 if (!s)
4006 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004007 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004008 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004009 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004010 return 1;
4011}
4012
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004013/*
4014 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004015 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004016 * It returns a pointer to the first server found or NULL if <ip> is not yet
4017 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004018 *
4019 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004020 */
4021struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4022{
4023 struct server *tmpsrv;
4024 struct proxy *be;
4025
4026 if (!srv)
4027 return NULL;
4028
4029 be = srv->proxy;
4030 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004031 /* we found the current server is the same, ignore it */
4032 if (srv == tmpsrv)
4033 continue;
4034
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004035 /* We want to compare the IP in the record with the IP of the servers in the
4036 * same backend, only if:
4037 * * DNS resolution is enabled on the server
4038 * * the hostname used for the resolution by our server is the same than the
4039 * one used for the server found in the backend
4040 * * the server found in the backend is not our current server
4041 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004042 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004043 if ((tmpsrv->hostname_dn == NULL) ||
4044 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4045 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004046 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004047 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004048 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004049 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004050
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004051 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004052 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004053 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004054 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004055 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004056
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004057 /* At this point, we have 2 different servers using the same DNS hostname
4058 * for their respective resolution.
4059 */
4060 if (*ip_family == tmpsrv->addr.ss_family &&
4061 ((tmpsrv->addr.ss_family == AF_INET &&
4062 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4063 (tmpsrv->addr.ss_family == AF_INET6 &&
4064 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004065 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004066 return tmpsrv;
4067 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004068 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004069 }
4070
Emeric Brune9fd6b52017-11-02 17:20:39 +01004071
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004072 return NULL;
4073}
4074
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004075/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4076 * resolver. This is suited for initial address configuration. Returns 0 on
4077 * success otherwise a non-zero error code. In case of error, *err_code, if
4078 * not NULL, is filled up.
4079 */
4080int srv_set_addr_via_libc(struct server *srv, int *err_code)
4081{
4082 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004083 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004084 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004085 return 1;
4086 }
4087 return 0;
4088}
4089
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004090/* Set the server's FDQN (->hostname) from <hostname>.
4091 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004092 *
4093 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004094 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004095int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004096{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004097 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004098 char *hostname_dn;
4099 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004100
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004101 /* Note that the server lock is already held. */
4102 if (!srv->resolvers)
4103 return -1;
4104
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004105 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004106 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004107 /* run time DNS resolution was not active for this server
4108 * and we can't enable it at run time for now.
4109 */
4110 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004111 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004112
4113 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004114 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004115 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004116 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4117 hostname_dn, trash.size);
4118 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004119 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004120
Christopher Faulet67957bd2017-09-27 11:00:59 +02004121 resolution = srv->dns_requester->resolution;
4122 if (resolution &&
4123 resolution->hostname_dn &&
4124 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004125 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004126
Christopher Faulet67957bd2017-09-27 11:00:59 +02004127 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004128
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004129 free(srv->hostname);
4130 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004131 srv->hostname = strdup(hostname);
4132 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004133 srv->hostname_dn_len = hostname_dn_len;
4134 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004135 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004136
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004137 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004138 goto err;
4139
4140 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004141 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004142 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004143 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004144
4145 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004146 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004147 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004148 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004149}
4150
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004151/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4152 * from the state file. This is suited for initial address configuration.
4153 * Returns 0 on success otherwise a non-zero error code. In case of error,
4154 * *err_code, if not NULL, is filled up.
4155 */
4156static int srv_apply_lastaddr(struct server *srv, int *err_code)
4157{
4158 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4159 if (err_code)
4160 *err_code |= ERR_WARN;
4161 return 1;
4162 }
4163 return 0;
4164}
4165
Willy Tarreau25e51522016-11-04 15:10:17 +01004166/* returns 0 if no error, otherwise a combination of ERR_* flags */
4167static int srv_iterate_initaddr(struct server *srv)
4168{
4169 int return_code = 0;
4170 int err_code;
4171 unsigned int methods;
4172
4173 methods = srv->init_addr_methods;
4174 if (!methods) { // default to "last,libc"
4175 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4176 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4177 }
4178
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004179 /* "-dr" : always append "none" so that server addresses resolution
4180 * failures are silently ignored, this is convenient to validate some
4181 * configs out of their environment.
4182 */
4183 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4184 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4185
Willy Tarreau25e51522016-11-04 15:10:17 +01004186 while (methods) {
4187 err_code = 0;
4188 switch (srv_get_next_initaddr(&methods)) {
4189 case SRV_IADDR_LAST:
4190 if (!srv->lastaddr)
4191 continue;
4192 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004193 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004194 return_code |= err_code;
4195 break;
4196
4197 case SRV_IADDR_LIBC:
4198 if (!srv->hostname)
4199 continue;
4200 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004201 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004202 return_code |= err_code;
4203 break;
4204
Willy Tarreau37ebe122016-11-04 15:17:58 +01004205 case SRV_IADDR_NONE:
4206 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004207 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004208 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4209 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004210 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004211 return return_code;
4212
Willy Tarreau4310d362016-11-02 15:05:56 +01004213 case SRV_IADDR_IP:
4214 ipcpy(&srv->init_addr, &srv->addr);
4215 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004216 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4217 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004218 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004219 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004220
Willy Tarreau25e51522016-11-04 15:10:17 +01004221 default: /* unhandled method */
4222 break;
4223 }
4224 }
4225
4226 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004227 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004228 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4229 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004230 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004231 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004232 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4233 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004234
4235 return_code |= ERR_ALERT | ERR_FATAL;
4236 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004237out:
4238 srv_set_dyncookie(srv);
4239 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004240}
4241
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004242/*
4243 * This function parses all backends and all servers within each backend
4244 * and performs servers' addr resolution based on information provided by:
4245 * - configuration file
4246 * - server-state file (states provided by an 'old' haproxy process)
4247 *
4248 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4249 */
4250int srv_init_addr(void)
4251{
4252 struct proxy *curproxy;
4253 int return_code = 0;
4254
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004255 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004256 while (curproxy) {
4257 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004258
4259 /* servers are in backend only */
4260 if (!(curproxy->cap & PR_CAP_BE))
4261 goto srv_init_addr_next;
4262
Willy Tarreau25e51522016-11-04 15:10:17 +01004263 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004264 if (srv->hostname)
4265 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004266
4267 srv_init_addr_next:
4268 curproxy = curproxy->next;
4269 }
4270
4271 return return_code;
4272}
4273
Willy Tarreau46b7f532018-08-21 11:54:26 +02004274/*
4275 * Must be called with the server lock held.
4276 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004277const 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 +02004278{
4279
Willy Tarreau83061a82018-07-13 11:56:34 +02004280 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004281
4282 msg = get_trash_chunk();
4283 chunk_reset(msg);
4284
Olivier Houchard8da5f982017-08-04 18:35:36 +02004285 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004286 chunk_appendf(msg, "no need to change the FDQN");
4287 goto out;
4288 }
4289
4290 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4291 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4292 goto out;
4293 }
4294
4295 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4296 server->proxy->id, server->id, server->hostname, fqdn);
4297
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004298 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004299 chunk_reset(msg);
4300 chunk_appendf(msg, "could not update %s/%s FQDN",
4301 server->proxy->id, server->id);
4302 goto out;
4303 }
4304
4305 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004306 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004307
4308 out:
4309 if (updater)
4310 chunk_appendf(msg, " by '%s'", updater);
4311 chunk_appendf(msg, "\n");
4312
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004313 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004314}
4315
4316
Willy Tarreau21b069d2016-11-23 17:15:08 +01004317/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4318 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004319 * 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 +01004320 * used for CLI commands requiring a server name.
4321 * Important: the <arg> is modified to remove the '/'.
4322 */
4323struct server *cli_find_server(struct appctx *appctx, char *arg)
4324{
4325 struct proxy *px;
4326 struct server *sv;
4327 char *line;
4328
4329 /* split "backend/server" and make <line> point to server */
4330 for (line = arg; *line; line++)
4331 if (*line == '/') {
4332 *line++ = '\0';
4333 break;
4334 }
4335
4336 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004337 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004338 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004339 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004340 return NULL;
4341 }
4342
4343 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004344 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004345 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004346 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004347 return NULL;
4348 }
4349
4350 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004351 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004352 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004353 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004354 return NULL;
4355 }
4356
4357 return sv;
4358}
4359
William Lallemand222baf22016-11-19 02:00:33 +01004360
Willy Tarreau46b7f532018-08-21 11:54:26 +02004361/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004362static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004363{
4364 struct server *sv;
4365 const char *warning;
4366
4367 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4368 return 1;
4369
4370 sv = cli_find_server(appctx, args[2]);
4371 if (!sv)
4372 return 1;
4373
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004374 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004375
William Lallemand222baf22016-11-19 02:00:33 +01004376 if (strcmp(args[3], "weight") == 0) {
4377 warning = server_parse_weight_change_request(sv, args[4]);
4378 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004379 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004380 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004381 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004382 }
4383 }
4384 else if (strcmp(args[3], "state") == 0) {
4385 if (strcmp(args[4], "ready") == 0)
4386 srv_adm_set_ready(sv);
4387 else if (strcmp(args[4], "drain") == 0)
4388 srv_adm_set_drain(sv);
4389 else if (strcmp(args[4], "maint") == 0)
4390 srv_adm_set_maint(sv);
4391 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004392 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004393 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004394 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004395 }
4396 }
4397 else if (strcmp(args[3], "health") == 0) {
4398 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004399 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004400 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004401 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004402 }
4403 else if (strcmp(args[4], "up") == 0) {
4404 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004405 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004406 }
4407 else if (strcmp(args[4], "stopping") == 0) {
4408 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004409 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004410 }
4411 else if (strcmp(args[4], "down") == 0) {
4412 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004413 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004414 }
4415 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004416 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004417 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004418 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004419 }
4420 }
4421 else if (strcmp(args[3], "agent") == 0) {
4422 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004423 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004424 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004425 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004426 }
4427 else if (strcmp(args[4], "up") == 0) {
4428 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004429 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004430 }
4431 else if (strcmp(args[4], "down") == 0) {
4432 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004433 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004434 }
4435 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004436 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004437 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004438 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004439 }
4440 }
Misiek2da082d2017-01-09 09:40:42 +01004441 else if (strcmp(args[3], "agent-addr") == 0) {
4442 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004443 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004444 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4445 appctx->st0 = CLI_ST_PRINT;
4446 } else {
4447 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004448 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004449 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4450 appctx->st0 = CLI_ST_PRINT;
4451 }
4452 }
4453 }
4454 else if (strcmp(args[3], "agent-send") == 0) {
4455 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004456 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004457 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4458 appctx->st0 = CLI_ST_PRINT;
4459 } else {
4460 char *nss = strdup(args[4]);
4461 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004462 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004463 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4464 appctx->st0 = CLI_ST_PRINT;
4465 } else {
4466 free(sv->agent.send_string);
4467 sv->agent.send_string = nss;
4468 sv->agent.send_string_len = strlen(args[4]);
4469 }
4470 }
4471 }
William Lallemand222baf22016-11-19 02:00:33 +01004472 else if (strcmp(args[3], "check-port") == 0) {
4473 int i = 0;
4474 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004475 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004476 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004477 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004478 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004479 }
4480 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004481 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004482 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004483 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004484 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004485 }
4486 /* prevent the update of port to 0 if MAPPORTS are in use */
4487 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004488 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004489 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004490 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004491 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004492 }
4493 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004494 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004495 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004496 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004497 }
4498 else if (strcmp(args[3], "addr") == 0) {
4499 char *addr = NULL;
4500 char *port = NULL;
4501 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004502 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004503 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004504 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004505 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004506 }
4507 else {
4508 addr = args[4];
4509 }
4510 if (strcmp(args[5], "port") == 0) {
4511 port = args[6];
4512 }
4513 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4514 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004515 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004516 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004517 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004518 }
4519 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4520 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004521 else if (strcmp(args[3], "fqdn") == 0) {
4522 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004523 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004524 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4525 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004526 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004527 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004528 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004529 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004530 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004531 appctx->ctx.cli.msg = warning;
4532 appctx->st0 = CLI_ST_PRINT;
4533 }
4534 }
William Lallemand222baf22016-11-19 02:00:33 +01004535 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004536 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004537 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr', 'fqdn' and 'check-port'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004538 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004539 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004540 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004541 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004542 return 1;
4543}
4544
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004545static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004546{
4547 struct stream_interface *si = appctx->owner;
4548 struct proxy *px;
4549 struct server *sv;
4550 char *line;
4551
4552
4553 /* split "backend/server" and make <line> point to server */
4554 for (line = args[2]; *line; line++)
4555 if (*line == '/') {
4556 *line++ = '\0';
4557 break;
4558 }
4559
4560 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004561 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004562 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004563 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004564 return 1;
4565 }
4566
4567 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004568 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004569 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004570 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004571 return 1;
4572 }
4573
4574 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004575 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4576 sv->iweight);
4577 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004578 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004579 return 0;
4580 }
William Lallemand6b160942016-11-22 12:34:35 +01004581 return 1;
4582}
4583
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004584/* Parse a "set weight" command.
4585 *
4586 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004587 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004588static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004589{
4590 struct server *sv;
4591 const char *warning;
4592
4593 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4594 return 1;
4595
4596 sv = cli_find_server(appctx, args[2]);
4597 if (!sv)
4598 return 1;
4599
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004600 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4601
William Lallemand6b160942016-11-22 12:34:35 +01004602 warning = server_parse_weight_change_request(sv, args[3]);
4603 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004604 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004605 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004606 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004607 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004608
4609 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4610
William Lallemand6b160942016-11-22 12:34:35 +01004611 return 1;
4612}
4613
Willy Tarreau46b7f532018-08-21 11:54:26 +02004614/* parse a "set maxconn server" command. It always returns 1.
4615 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004616 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004617 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004618static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004619{
4620 struct server *sv;
4621 const char *warning;
4622
4623 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4624 return 1;
4625
4626 sv = cli_find_server(appctx, args[3]);
4627 if (!sv)
4628 return 1;
4629
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004630 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4631
Willy Tarreaub8026272016-11-23 11:26:56 +01004632 warning = server_parse_maxconn_change_request(sv, args[4]);
4633 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004634 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004635 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004636 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004637 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004638
4639 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4640
Willy Tarreaub8026272016-11-23 11:26:56 +01004641 return 1;
4642}
William Lallemand6b160942016-11-22 12:34:35 +01004643
Willy Tarreau46b7f532018-08-21 11:54:26 +02004644/* parse a "disable agent" command. It always returns 1.
4645 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004646 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004647 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004648static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004649{
4650 struct server *sv;
4651
4652 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4653 return 1;
4654
4655 sv = cli_find_server(appctx, args[2]);
4656 if (!sv)
4657 return 1;
4658
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004659 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004660 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004661 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004662 return 1;
4663}
4664
Willy Tarreau46b7f532018-08-21 11:54:26 +02004665/* parse a "disable health" command. It always returns 1.
4666 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004667 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004668 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004669static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004670{
4671 struct server *sv;
4672
4673 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4674 return 1;
4675
4676 sv = cli_find_server(appctx, args[2]);
4677 if (!sv)
4678 return 1;
4679
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004680 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004681 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004682 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004683 return 1;
4684}
4685
Willy Tarreau46b7f532018-08-21 11:54:26 +02004686/* parse a "disable server" command. It always returns 1.
4687 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004688 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004689 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004690static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004691{
4692 struct server *sv;
4693
4694 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4695 return 1;
4696
4697 sv = cli_find_server(appctx, args[2]);
4698 if (!sv)
4699 return 1;
4700
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004701 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004702 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004703 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004704 return 1;
4705}
4706
Willy Tarreau46b7f532018-08-21 11:54:26 +02004707/* parse a "enable agent" command. It always returns 1.
4708 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004709 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004710 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004711static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004712{
4713 struct server *sv;
4714
4715 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4716 return 1;
4717
4718 sv = cli_find_server(appctx, args[2]);
4719 if (!sv)
4720 return 1;
4721
4722 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004723 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004724 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004725 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004726 return 1;
4727 }
4728
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004729 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004730 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004731 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004732 return 1;
4733}
4734
Willy Tarreau46b7f532018-08-21 11:54:26 +02004735/* parse a "enable health" command. It always returns 1.
4736 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004737 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004738 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004739static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004740{
4741 struct server *sv;
4742
4743 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4744 return 1;
4745
4746 sv = cli_find_server(appctx, args[2]);
4747 if (!sv)
4748 return 1;
4749
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004750 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004751 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004752 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004753 return 1;
4754}
4755
Willy Tarreau46b7f532018-08-21 11:54:26 +02004756/* parse a "enable server" command. It always returns 1.
4757 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004758 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004759 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004760static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004761{
4762 struct server *sv;
4763
4764 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4765 return 1;
4766
4767 sv = cli_find_server(appctx, args[2]);
4768 if (!sv)
4769 return 1;
4770
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004771 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004772 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004773 if (!(sv->flags & SRV_F_COOKIESET)
4774 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4775 sv->cookie)
4776 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004777 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004778 return 1;
4779}
4780
William Lallemand222baf22016-11-19 02:00:33 +01004781/* register cli keywords */
4782static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004783 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004784 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004785 { { "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 +01004786 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004787 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004788 { { "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 +01004789 { { "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 +01004790 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004791 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4792 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4793
William Lallemand222baf22016-11-19 02:00:33 +01004794 {{},}
4795}};
4796
Willy Tarreau0108d902018-11-25 19:14:37 +01004797INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004798
Emeric Brun64cc49c2017-10-03 14:46:45 +02004799/*
4800 * This function applies server's status changes, it is
4801 * is designed to be called asynchronously.
4802 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004803 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004804 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004805static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004806{
4807 struct check *check = &s->check;
4808 int xferred;
4809 struct proxy *px = s->proxy;
4810 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4811 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4812 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004813 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004814
Emeric Brun64cc49c2017-10-03 14:46:45 +02004815 /* If currently main is not set we try to apply pending state changes */
4816 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4817 int next_admin;
4818
4819 /* Backup next admin */
4820 next_admin = s->next_admin;
4821
4822 /* restore current admin state */
4823 s->next_admin = s->cur_admin;
4824
4825 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4826 s->last_change = now.tv_sec;
4827 if (s->proxy->lbprm.set_server_status_down)
4828 s->proxy->lbprm.set_server_status_down(s);
4829
4830 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4831 srv_shutdown_streams(s, SF_ERR_DOWN);
4832
4833 /* we might have streams queued on this server and waiting for
4834 * a connection. Those which are redispatchable will be queued
4835 * to another server or to the proxy itself.
4836 */
4837 xferred = pendconn_redistribute(s);
4838
4839 tmptrash = alloc_trash_chunk();
4840 if (tmptrash) {
4841 chunk_printf(tmptrash,
4842 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4843 s->proxy->id, s->id);
4844
Emeric Brun5a133512017-10-19 14:42:30 +02004845 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004846 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004847
4848 /* we don't send an alert if the server was previously paused */
4849 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004850 send_log(s->proxy, log_level, "%s.\n",
4851 tmptrash->area);
4852 send_email_alert(s, log_level, "%s",
4853 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004854 free_trash_chunk(tmptrash);
4855 tmptrash = NULL;
4856 }
4857 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4858 set_backend_down(s->proxy);
4859
4860 s->counters.down_trans++;
4861 }
4862 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4863 s->last_change = now.tv_sec;
4864 if (s->proxy->lbprm.set_server_status_down)
4865 s->proxy->lbprm.set_server_status_down(s);
4866
4867 /* we might have streams queued on this server and waiting for
4868 * a connection. Those which are redispatchable will be queued
4869 * to another server or to the proxy itself.
4870 */
4871 xferred = pendconn_redistribute(s);
4872
4873 tmptrash = alloc_trash_chunk();
4874 if (tmptrash) {
4875 chunk_printf(tmptrash,
4876 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4877 s->proxy->id, s->id);
4878
Emeric Brun5a133512017-10-19 14:42:30 +02004879 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004880
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004881 ha_warning("%s.\n", tmptrash->area);
4882 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4883 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004884 free_trash_chunk(tmptrash);
4885 tmptrash = NULL;
4886 }
4887
4888 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4889 set_backend_down(s->proxy);
4890 }
4891 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4892 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4893 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4894 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4895 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4896 s->proxy->last_change = now.tv_sec;
4897 }
4898
4899 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4900 s->down_time += now.tv_sec - s->last_change;
4901
4902 s->last_change = now.tv_sec;
4903 if (s->next_state == SRV_ST_STARTING)
4904 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4905
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004906 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004907 /* now propagate the status change to any LB algorithms */
4908 if (px->lbprm.update_server_eweight)
4909 px->lbprm.update_server_eweight(s);
4910 else if (srv_willbe_usable(s)) {
4911 if (px->lbprm.set_server_status_up)
4912 px->lbprm.set_server_status_up(s);
4913 }
4914 else {
4915 if (px->lbprm.set_server_status_down)
4916 px->lbprm.set_server_status_down(s);
4917 }
4918
4919 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4920 * and it's not a backup server and its effective weight is > 0,
4921 * then it can accept new connections, so we shut down all streams
4922 * on all backup servers.
4923 */
4924 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4925 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4926 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4927
4928 /* check if we can handle some connections queued at the proxy. We
4929 * will take as many as we can handle.
4930 */
4931 xferred = pendconn_grab_from_px(s);
4932
4933 tmptrash = alloc_trash_chunk();
4934 if (tmptrash) {
4935 chunk_printf(tmptrash,
4936 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4937 s->proxy->id, s->id);
4938
Emeric Brun5a133512017-10-19 14:42:30 +02004939 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004940 ha_warning("%s.\n", tmptrash->area);
4941 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4942 tmptrash->area);
4943 send_email_alert(s, LOG_NOTICE, "%s",
4944 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004945 free_trash_chunk(tmptrash);
4946 tmptrash = NULL;
4947 }
4948
4949 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4950 set_backend_down(s->proxy);
4951 }
4952 else if (s->cur_eweight != s->next_eweight) {
4953 /* now propagate the status change to any LB algorithms */
4954 if (px->lbprm.update_server_eweight)
4955 px->lbprm.update_server_eweight(s);
4956 else if (srv_willbe_usable(s)) {
4957 if (px->lbprm.set_server_status_up)
4958 px->lbprm.set_server_status_up(s);
4959 }
4960 else {
4961 if (px->lbprm.set_server_status_down)
4962 px->lbprm.set_server_status_down(s);
4963 }
4964
4965 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4966 set_backend_down(s->proxy);
4967 }
4968
4969 s->next_admin = next_admin;
4970 }
4971
Emeric Brun5a133512017-10-19 14:42:30 +02004972 /* reset operational state change */
4973 *s->op_st_chg.reason = 0;
4974 s->op_st_chg.status = s->op_st_chg.code = -1;
4975 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004976
4977 /* Now we try to apply pending admin changes */
4978
4979 /* Maintenance must also disable health checks */
4980 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4981 if (s->check.state & CHK_ST_ENABLED) {
4982 s->check.state |= CHK_ST_PAUSED;
4983 check->health = 0;
4984 }
4985
4986 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004987 tmptrash = alloc_trash_chunk();
4988 if (tmptrash) {
4989 chunk_printf(tmptrash,
4990 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4991 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4992 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004993
Olivier Houchard796a2b32017-10-24 17:42:47 +02004994 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004995
Olivier Houchard796a2b32017-10-24 17:42:47 +02004996 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004997 ha_warning("%s.\n", tmptrash->area);
4998 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4999 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005000 }
5001 free_trash_chunk(tmptrash);
5002 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005003 }
Emeric Brun8f298292017-12-06 16:47:17 +01005004 /* commit new admin status */
5005
5006 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005007 }
5008 else { /* server was still running */
5009 check->health = 0; /* failure */
5010 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005011
5012 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005013 if (s->proxy->lbprm.set_server_status_down)
5014 s->proxy->lbprm.set_server_status_down(s);
5015
Emeric Brun64cc49c2017-10-03 14:46:45 +02005016 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5017 srv_shutdown_streams(s, SF_ERR_DOWN);
5018
William Dauchy3b614332020-05-02 21:52:36 +02005019 /* force connection cleanup on the given server */
5020 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005021 /* we might have streams queued on this server and waiting for
5022 * a connection. Those which are redispatchable will be queued
5023 * to another server or to the proxy itself.
5024 */
5025 xferred = pendconn_redistribute(s);
5026
5027 tmptrash = alloc_trash_chunk();
5028 if (tmptrash) {
5029 chunk_printf(tmptrash,
5030 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5031 s->flags & SRV_F_BACKUP ? "Backup " : "",
5032 s->proxy->id, s->id,
5033 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5034
5035 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5036
5037 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005038 ha_warning("%s.\n", tmptrash->area);
5039 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5040 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005041 }
5042 free_trash_chunk(tmptrash);
5043 tmptrash = NULL;
5044 }
5045 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5046 set_backend_down(s->proxy);
5047
5048 s->counters.down_trans++;
5049 }
5050 }
5051 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5052 /* OK here we're leaving maintenance, we have many things to check,
5053 * because the server might possibly be coming back up depending on
5054 * its state. In practice, leaving maintenance means that we should
5055 * immediately turn to UP (more or less the slowstart) under the
5056 * following conditions :
5057 * - server is neither checked nor tracked
5058 * - server tracks another server which is not checked
5059 * - server tracks another server which is already up
5060 * Which sums up as something simpler :
5061 * "either the tracking server is up or the server's checks are disabled
5062 * or up". Otherwise we only re-enable health checks. There's a special
5063 * case associated to the stopping state which can be inherited. Note
5064 * that the server might still be in drain mode, which is naturally dealt
5065 * with by the lower level functions.
5066 */
5067
5068 if (s->check.state & CHK_ST_ENABLED) {
5069 s->check.state &= ~CHK_ST_PAUSED;
5070 check->health = check->rise; /* start OK but check immediately */
5071 }
5072
5073 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5074 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5075 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5076 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5077 s->next_state = SRV_ST_STOPPING;
5078 }
5079 else {
5080 s->next_state = SRV_ST_STARTING;
5081 if (s->slowstart > 0)
5082 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5083 else
5084 s->next_state = SRV_ST_RUNNING;
5085 }
5086
5087 }
5088
5089 tmptrash = alloc_trash_chunk();
5090 if (tmptrash) {
5091 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5092 chunk_printf(tmptrash,
5093 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5094 s->flags & SRV_F_BACKUP ? "Backup " : "",
5095 s->proxy->id, s->id,
5096 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5097 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5098 }
5099 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5100 chunk_printf(tmptrash,
5101 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5102 s->flags & SRV_F_BACKUP ? "Backup " : "",
5103 s->proxy->id, s->id, s->hostname,
5104 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5105 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5106 }
5107 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5108 chunk_printf(tmptrash,
5109 "%sServer %s/%s is %s/%s (leaving maintenance)",
5110 s->flags & SRV_F_BACKUP ? "Backup " : "",
5111 s->proxy->id, s->id,
5112 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5113 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5114 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005115 ha_warning("%s.\n", tmptrash->area);
5116 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5117 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005118 free_trash_chunk(tmptrash);
5119 tmptrash = NULL;
5120 }
5121
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005122 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005123 /* now propagate the status change to any LB algorithms */
5124 if (px->lbprm.update_server_eweight)
5125 px->lbprm.update_server_eweight(s);
5126 else if (srv_willbe_usable(s)) {
5127 if (px->lbprm.set_server_status_up)
5128 px->lbprm.set_server_status_up(s);
5129 }
5130 else {
5131 if (px->lbprm.set_server_status_down)
5132 px->lbprm.set_server_status_down(s);
5133 }
5134
5135 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5136 set_backend_down(s->proxy);
5137
Willy Tarreau6a78e612018-08-07 10:14:53 +02005138 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5139 * and it's not a backup server and its effective weight is > 0,
5140 * then it can accept new connections, so we shut down all streams
5141 * on all backup servers.
5142 */
5143 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5144 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5145 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5146
5147 /* check if we can handle some connections queued at the proxy. We
5148 * will take as many as we can handle.
5149 */
5150 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005151 }
5152 else if (s->next_admin & SRV_ADMF_MAINT) {
5153 /* remaining in maintenance mode, let's inform precisely about the
5154 * situation.
5155 */
5156 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5157 tmptrash = alloc_trash_chunk();
5158 if (tmptrash) {
5159 chunk_printf(tmptrash,
5160 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5161 s->flags & SRV_F_BACKUP ? "Backup " : "",
5162 s->proxy->id, s->id);
5163
5164 if (s->track) /* normally it's mandatory here */
5165 chunk_appendf(tmptrash, " via %s/%s",
5166 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005167 ha_warning("%s.\n", tmptrash->area);
5168 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5169 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005170 free_trash_chunk(tmptrash);
5171 tmptrash = NULL;
5172 }
5173 }
5174 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5175 tmptrash = alloc_trash_chunk();
5176 if (tmptrash) {
5177 chunk_printf(tmptrash,
5178 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5179 s->flags & SRV_F_BACKUP ? "Backup " : "",
5180 s->proxy->id, s->id, s->hostname);
5181
5182 if (s->track) /* normally it's mandatory here */
5183 chunk_appendf(tmptrash, " via %s/%s",
5184 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005185 ha_warning("%s.\n", tmptrash->area);
5186 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5187 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005188 free_trash_chunk(tmptrash);
5189 tmptrash = NULL;
5190 }
5191 }
5192 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5193 tmptrash = alloc_trash_chunk();
5194 if (tmptrash) {
5195 chunk_printf(tmptrash,
5196 "%sServer %s/%s remains in forced maintenance",
5197 s->flags & SRV_F_BACKUP ? "Backup " : "",
5198 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005199 ha_warning("%s.\n", tmptrash->area);
5200 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5201 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005202 free_trash_chunk(tmptrash);
5203 tmptrash = NULL;
5204 }
5205 }
5206 /* don't report anything when leaving drain mode and remaining in maintenance */
5207
5208 s->cur_admin = s->next_admin;
5209 }
5210
5211 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5212 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5213 /* drain state is applied only if not yet in maint */
5214
5215 s->last_change = now.tv_sec;
5216 if (px->lbprm.set_server_status_down)
5217 px->lbprm.set_server_status_down(s);
5218
5219 /* we might have streams queued on this server and waiting for
5220 * a connection. Those which are redispatchable will be queued
5221 * to another server or to the proxy itself.
5222 */
5223 xferred = pendconn_redistribute(s);
5224
5225 tmptrash = alloc_trash_chunk();
5226 if (tmptrash) {
5227 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5228 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5229 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5230
5231 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5232
5233 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005234 ha_warning("%s.\n", tmptrash->area);
5235 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5236 tmptrash->area);
5237 send_email_alert(s, LOG_NOTICE, "%s",
5238 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005239 }
5240 free_trash_chunk(tmptrash);
5241 tmptrash = NULL;
5242 }
5243
5244 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5245 set_backend_down(s->proxy);
5246 }
5247 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5248 /* OK completely leaving drain mode */
5249 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5250 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5251 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5252 s->proxy->last_change = now.tv_sec;
5253 }
5254
5255 if (s->last_change < now.tv_sec) // ignore negative times
5256 s->down_time += now.tv_sec - s->last_change;
5257 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005258 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005259
5260 tmptrash = alloc_trash_chunk();
5261 if (tmptrash) {
5262 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5263 chunk_printf(tmptrash,
5264 "%sServer %s/%s is %s (leaving forced drain)",
5265 s->flags & SRV_F_BACKUP ? "Backup " : "",
5266 s->proxy->id, s->id,
5267 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5268 }
5269 else {
5270 chunk_printf(tmptrash,
5271 "%sServer %s/%s is %s (leaving drain)",
5272 s->flags & SRV_F_BACKUP ? "Backup " : "",
5273 s->proxy->id, s->id,
5274 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5275 if (s->track) /* normally it's mandatory here */
5276 chunk_appendf(tmptrash, " via %s/%s",
5277 s->track->proxy->id, s->track->id);
5278 }
5279
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005280 ha_warning("%s.\n", tmptrash->area);
5281 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5282 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005283 free_trash_chunk(tmptrash);
5284 tmptrash = NULL;
5285 }
5286
5287 /* now propagate the status change to any LB algorithms */
5288 if (px->lbprm.update_server_eweight)
5289 px->lbprm.update_server_eweight(s);
5290 else if (srv_willbe_usable(s)) {
5291 if (px->lbprm.set_server_status_up)
5292 px->lbprm.set_server_status_up(s);
5293 }
5294 else {
5295 if (px->lbprm.set_server_status_down)
5296 px->lbprm.set_server_status_down(s);
5297 }
5298 }
5299 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5300 /* remaining in drain mode after removing one of its flags */
5301
5302 tmptrash = alloc_trash_chunk();
5303 if (tmptrash) {
5304 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5305 chunk_printf(tmptrash,
5306 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5307 s->flags & SRV_F_BACKUP ? "Backup " : "",
5308 s->proxy->id, s->id);
5309
5310 if (s->track) /* normally it's mandatory here */
5311 chunk_appendf(tmptrash, " via %s/%s",
5312 s->track->proxy->id, s->track->id);
5313 }
5314 else {
5315 chunk_printf(tmptrash,
5316 "%sServer %s/%s remains in forced drain mode",
5317 s->flags & SRV_F_BACKUP ? "Backup " : "",
5318 s->proxy->id, s->id);
5319 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005320 ha_warning("%s.\n", tmptrash->area);
5321 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5322 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005323 free_trash_chunk(tmptrash);
5324 tmptrash = NULL;
5325 }
5326
5327 /* commit new admin status */
5328
5329 s->cur_admin = s->next_admin;
5330 }
5331 }
5332
5333 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005334 *s->adm_st_chg_cause = 0;
5335}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005336
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005337struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5338{
5339 struct connection *conn;
5340
5341 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5342 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005343 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005344 }
5345
5346 return task;
5347}
5348
William Dauchy3b614332020-05-02 21:52:36 +02005349/* cleanup connections for a given server
5350 * might be useful when going on forced maintenance or live changing ip/port
5351 */
William Dauchye18f6032020-05-04 13:52:40 +02005352static void srv_cleanup_connections(struct server *srv)
William Dauchy3b614332020-05-02 21:52:36 +02005353{
5354 struct connection *conn;
5355 int did_remove;
5356 int i;
5357 int j;
5358
5359 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5360 for (i = 0; i < global.nbthread; i++) {
5361 did_remove = 0;
5362 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
5363 for (j = 0; j < srv->curr_idle_conns; j++) {
5364 conn = LIST_ELEM(srv->idle_conns[tid].n, struct connection *, list);
5365 if (!conn)
5366 conn = LIST_ELEM(srv->safe_conns[tid].n,
5367 struct connection *, list);
5368 if (!conn)
5369 break;
5370 did_remove = 1;
5371 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5372 }
5373 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
5374 if (did_remove)
5375 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5376 }
5377 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5378}
5379
Willy Tarreau980855b2019-02-07 14:59:29 +01005380struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005381{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005382 struct server *srv;
5383 struct eb32_node *eb;
5384 int i;
5385 unsigned int next_wakeup;
5386 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005387
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005388 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5389 while (1) {
5390 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005391
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005392 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5393 if (!eb) {
5394 /* we might have reached the end of the tree, typically because
5395 * <now_ms> is in the first half and we're first scanning the last
5396 * half. Let's loop back to the beginning of the tree now.
5397 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005398
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005399 eb = eb32_first(&idle_conn_srv);
5400 if (likely(!eb))
5401 break;
5402 }
5403 if (tick_is_lt(now_ms, eb->key)) {
5404 /* timer not expired yet, revisit it later */
5405 next_wakeup = eb->key;
5406 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005407 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005408 }
5409 srv = eb32_entry(eb, struct server, idle_node);
5410 for (i = 0; i < global.nbthread; i++) {
5411 int max_conn = (srv->curr_idle_thr[i] / 2) +
5412 (srv->curr_idle_thr[i] & 1);
5413 int j;
5414 int did_remove = 0;
5415
Olivier Houchardba936e52019-07-11 15:49:00 +02005416 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005417 for (j = 0; j < max_conn; j++) {
5418 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5419 if (!conn)
5420 break;
5421 did_remove = 1;
5422 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5423 }
Olivier Houchardba936e52019-07-11 15:49:00 +02005424 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005425 if (did_remove && max_conn < srv->curr_idle_thr[i])
5426 srv_is_empty = 0;
5427 if (did_remove)
5428 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5429 }
5430 eb32_delete(&srv->idle_node);
5431 if (!srv_is_empty) {
5432 /* There are still more idle connections, add the
5433 * server back in the tree.
5434 */
5435 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5436 now_ms);
5437 eb32_insert(&idle_conn_srv, &srv->idle_node);
5438 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005439 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005440 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5441
5442 if (need_wakeup)
5443 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005444 else
5445 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005446
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005447 return task;
5448}
Olivier Houchard88698d92019-04-16 19:07:22 +02005449
5450/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5451static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5452 struct proxy *defpx, const char *file, int line,
5453 char **err)
5454{
5455 int arg = -1;
5456
5457 if (too_many_args(1, args, err, NULL))
5458 return -1;
5459
5460 if (*(args[1]) != 0)
5461 arg = atoi(args[1]);
5462
5463 if (arg < 0 || arg > 100) {
5464 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5465 return -1;
5466 }
5467
5468 if (args[0][10] == 'h')
5469 global.tune.pool_high_ratio = arg;
5470 else
5471 global.tune.pool_low_ratio = arg;
5472 return 0;
5473}
5474
5475/* config keyword parsers */
5476static struct cfg_kw_list cfg_kws = {ILH, {
5477 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5478 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5479 { 0, NULL, NULL }
5480}};
5481
5482INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5483
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005484/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005485 * Local variables:
5486 * c-indent-level: 8
5487 * c-basic-offset: 8
5488 * End:
5489 */