blob: 541dc696fe6f87eec13508875ad03322f5654184 [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>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010029#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020030#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020036#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020037#include <proto/port_range.h>
38#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020039#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010040#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020041#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020042#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010043#include <proto/stream_interface.h>
44#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020045#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020046#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010047#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020050static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010052static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreau21faa912012-10-10 08:27:36 +020054/* List head of all known server keywords */
55static struct srv_kw_list srv_keywords = {
56 .list = LIST_HEAD_INIT(srv_keywords.list)
57};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058
Olivier Houchard9ea5d362019-02-14 18:29:09 +010059__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
60struct eb_root idle_conn_srv = EB_ROOT;
61struct task *idle_conn_task = NULL;
62struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
63struct list toremove_connections[MAX_THREADS];
64
Simon Hormana3608442013-11-01 16:46:15 +090065int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020066{
Emeric Brun52a91d32017-08-31 14:41:55 +020067 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020068 return s->down_time;
69
70 return now.tv_sec - s->last_change + s->down_time;
71}
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050073int srv_lastsession(const struct server *s)
74{
75 if (s->counters.last_sess)
76 return now.tv_sec - s->counters.last_sess;
77
78 return -1;
79}
80
Simon Horman4a741432013-02-23 15:35:38 +090081int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020082{
Simon Horman4a741432013-02-23 15:35:38 +090083 const struct server *s = check->server;
84
Willy Tarreauff5ae352013-12-11 20:36:34 +010085 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090086 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010087
Emeric Brun52a91d32017-08-31 14:41:55 +020088 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090089 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010090
Simon Horman4a741432013-02-23 15:35:38 +090091 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010092}
93
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010094/*
95 * Check that we did not get a hash collision.
96 * Unlikely, but it can happen.
97 */
98static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010099{
100 struct proxy *p = s->proxy;
101 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100102
103 for (tmpserv = p->srv; tmpserv != NULL;
104 tmpserv = tmpserv->next) {
105 if (tmpserv == s)
106 continue;
107 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
108 continue;
109 if (tmpserv->cookie &&
110 strcmp(tmpserv->cookie, s->cookie) == 0) {
111 ha_warning("We generated two equal cookies for two different servers.\n"
112 "Please change the secret key for '%s'.\n",
113 s->proxy->id);
114 }
115 }
116
117}
118
Willy Tarreau46b7f532018-08-21 11:54:26 +0200119/*
120 * Must be called with the server lock held.
121 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100122void srv_set_dyncookie(struct server *s)
123{
124 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100125 char *tmpbuf;
126 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100127 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100128 size_t buffer_len;
129 int addr_len;
130 int port;
131
132 if ((s->flags & SRV_F_COOKIESET) ||
133 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
134 s->proxy->dyncookie_key == NULL)
135 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100136 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100137
138 if (s->addr.ss_family != AF_INET &&
139 s->addr.ss_family != AF_INET6)
140 return;
141 /*
142 * Buffer to calculate the cookie value.
143 * The buffer contains the secret key + the server IP address
144 * + the TCP port.
145 */
146 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
147 /*
148 * The TCP port should use only 2 bytes, but is stored in
149 * an unsigned int in struct server, so let's use 4, to be
150 * on the safe side.
151 */
152 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200153 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100154 memcpy(tmpbuf, p->dyncookie_key, key_len);
155 memcpy(&(tmpbuf[key_len]),
156 s->addr.ss_family == AF_INET ?
157 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
158 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
159 addr_len);
160 /*
161 * Make sure it's the same across all the load balancers,
162 * no matter their endianness.
163 */
164 port = htonl(s->svc_port);
165 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
166 hash_value = XXH64(tmpbuf, buffer_len, 0);
167 memprintf(&s->cookie, "%016llx", hash_value);
168 if (!s->cookie)
169 return;
170 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100171
172 /* Don't bother checking if the dyncookie is duplicated if
173 * the server is marked as "disabled", maybe it doesn't have
174 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100175 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100176 if (!(s->next_admin & SRV_ADMF_FMAINT))
177 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100178}
179
Willy Tarreau21faa912012-10-10 08:27:36 +0200180/*
181 * Registers the server keyword list <kwl> as a list of valid keywords for next
182 * parsing sessions.
183 */
184void srv_register_keywords(struct srv_kw_list *kwl)
185{
186 LIST_ADDQ(&srv_keywords.list, &kwl->list);
187}
188
189/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
190 * keyword is found with a NULL ->parse() function, then an attempt is made to
191 * find one with a valid ->parse() function. This way it is possible to declare
192 * platform-dependant, known keywords as NULL, then only declare them as valid
193 * if some options are met. Note that if the requested keyword contains an
194 * opening parenthesis, everything from this point is ignored.
195 */
196struct srv_kw *srv_find_kw(const char *kw)
197{
198 int index;
199 const char *kwend;
200 struct srv_kw_list *kwl;
201 struct srv_kw *ret = NULL;
202
203 kwend = strchr(kw, '(');
204 if (!kwend)
205 kwend = kw + strlen(kw);
206
207 list_for_each_entry(kwl, &srv_keywords.list, list) {
208 for (index = 0; kwl->kw[index].kw != NULL; index++) {
209 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
210 kwl->kw[index].kw[kwend-kw] == 0) {
211 if (kwl->kw[index].parse)
212 return &kwl->kw[index]; /* found it !*/
213 else
214 ret = &kwl->kw[index]; /* may be OK */
215 }
216 }
217 }
218 return ret;
219}
220
221/* Dumps all registered "server" keywords to the <out> string pointer. The
222 * unsupported keywords are only dumped if their supported form was not
223 * found.
224 */
225void srv_dump_kws(char **out)
226{
227 struct srv_kw_list *kwl;
228 int index;
229
230 *out = NULL;
231 list_for_each_entry(kwl, &srv_keywords.list, list) {
232 for (index = 0; kwl->kw[index].kw != NULL; index++) {
233 if (kwl->kw[index].parse ||
234 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
235 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
236 kwl->scope,
237 kwl->kw[index].kw,
238 kwl->kw[index].skip ? " <arg>" : "",
239 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
240 kwl->kw[index].parse ? "" : " (not supported)");
241 }
242 }
243 }
244}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100245
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100246/* Parse the "addr" server keyword */
247static int srv_parse_addr(char **args, int *cur_arg,
248 struct proxy *curproxy, struct server *newsrv, char **err)
249{
250 char *errmsg, *arg;
251 struct sockaddr_storage *sk;
252 int port1, port2;
253 struct protocol *proto;
254
255 errmsg = NULL;
256 arg = args[*cur_arg + 1];
257
258 if (!*arg) {
259 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
260 goto err;
261 }
262
263 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
264 if (!sk) {
265 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
266 goto err;
267 }
268
269 proto = protocol_by_family(sk->ss_family);
270 if (!proto || !proto->connect) {
271 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
272 args[*cur_arg], arg);
273 goto err;
274 }
275
276 if (port1 != port2) {
277 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
278 args[*cur_arg], arg);
279 goto err;
280 }
281
282 newsrv->check.addr = newsrv->agent.addr = *sk;
283 newsrv->flags |= SRV_F_CHECKADDR;
284 newsrv->flags |= SRV_F_AGENTADDR;
285
286 return 0;
287
288 err:
289 free(errmsg);
290 return ERR_ALERT | ERR_FATAL;
291}
292
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100293/* Parse the "agent-check" server keyword */
294static int srv_parse_agent_check(char **args, int *cur_arg,
295 struct proxy *curproxy, struct server *newsrv, char **err)
296{
297 newsrv->do_agent = 1;
298 return 0;
299}
300
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100301/* Parse the "backup" server keyword */
302static int srv_parse_backup(char **args, int *cur_arg,
303 struct proxy *curproxy, struct server *newsrv, char **err)
304{
305 newsrv->flags |= SRV_F_BACKUP;
306 return 0;
307}
308
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100309/* Parse the "check" server keyword */
310static int srv_parse_check(char **args, int *cur_arg,
311 struct proxy *curproxy, struct server *newsrv, char **err)
312{
313 newsrv->do_check = 1;
314 return 0;
315}
316
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100317/* Parse the "check-send-proxy" server keyword */
318static int srv_parse_check_send_proxy(char **args, int *cur_arg,
319 struct proxy *curproxy, struct server *newsrv, char **err)
320{
321 newsrv->check.send_proxy = 1;
322 return 0;
323}
324
Alexander Liu2a54bb72019-05-22 19:44:48 +0800325/* Parse the "check-via-socks4" server keyword */
326static int srv_parse_check_via_socks4(char **args, int *cur_arg,
327 struct proxy *curproxy, struct server *newsrv, char **err)
328{
329 newsrv->check.via_socks4 = 1;
330 return 0;
331}
332
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100333/* Parse the "cookie" server keyword */
334static int srv_parse_cookie(char **args, int *cur_arg,
335 struct proxy *curproxy, struct server *newsrv, char **err)
336{
337 char *arg;
338
339 arg = args[*cur_arg + 1];
340 if (!*arg) {
341 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
342 return ERR_ALERT | ERR_FATAL;
343 }
344
345 free(newsrv->cookie);
346 newsrv->cookie = strdup(arg);
347 newsrv->cklen = strlen(arg);
348 newsrv->flags |= SRV_F_COOKIESET;
349 return 0;
350}
351
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100352/* Parse the "disabled" server keyword */
353static int srv_parse_disabled(char **args, int *cur_arg,
354 struct proxy *curproxy, struct server *newsrv, char **err)
355{
Emeric Brun52a91d32017-08-31 14:41:55 +0200356 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
357 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100358 newsrv->check.state |= CHK_ST_PAUSED;
359 newsrv->check.health = 0;
360 return 0;
361}
362
363/* Parse the "enabled" server keyword */
364static int srv_parse_enabled(char **args, int *cur_arg,
365 struct proxy *curproxy, struct server *newsrv, char **err)
366{
Emeric Brun52a91d32017-08-31 14:41:55 +0200367 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
368 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100369 newsrv->check.state &= ~CHK_ST_PAUSED;
370 newsrv->check.health = newsrv->check.rise;
371 return 0;
372}
373
Willy Tarreau9c538e02019-01-23 10:21:49 +0100374static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
375{
376 char *arg;
377
378 arg = args[*cur_arg + 1];
379 if (!*arg) {
380 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
381 return ERR_ALERT | ERR_FATAL;
382 }
383 newsrv->max_reuse = atoi(arg);
384
385 return 0;
386}
387
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100388static 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 +0100389{
390 const char *res;
391 char *arg;
392 unsigned int time;
393
394 arg = args[*cur_arg + 1];
395 if (!*arg) {
396 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
397 return ERR_ALERT | ERR_FATAL;
398 }
399 res = parse_time_err(arg, &time, TIME_UNIT_MS);
400 if (res) {
401 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
402 *res, args[*cur_arg]);
403 return ERR_ALERT | ERR_FATAL;
404 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100405 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100406
407 return 0;
408}
409
Olivier Houchard006e3102018-12-10 18:30:32 +0100410static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
411{
412 char *arg;
413
414 arg = args[*cur_arg + 1];
415 if (!*arg) {
416 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
417 return ERR_ALERT | ERR_FATAL;
418 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100419
Olivier Houchard006e3102018-12-10 18:30:32 +0100420 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100421 if ((int)newsrv->max_idle_conns < -1) {
422 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
423 return ERR_ALERT | ERR_FATAL;
424 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100425
426 return 0;
427}
428
Willy Tarreaudff55432012-10-10 17:51:05 +0200429/* parse the "id" server keyword */
430static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
431{
432 struct eb32_node *node;
433
434 if (!*args[*cur_arg + 1]) {
435 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
436 return ERR_ALERT | ERR_FATAL;
437 }
438
439 newsrv->puid = atol(args[*cur_arg + 1]);
440 newsrv->conf.id.key = newsrv->puid;
441
442 if (newsrv->puid <= 0) {
443 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
444 return ERR_ALERT | ERR_FATAL;
445 }
446
447 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
448 if (node) {
449 struct server *target = container_of(node, struct server, conf.id);
450 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
451 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
452 target->id);
453 return ERR_ALERT | ERR_FATAL;
454 }
455
456 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200457 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200458 return 0;
459}
460
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100461/* Parse the "namespace" server keyword */
462static int srv_parse_namespace(char **args, int *cur_arg,
463 struct proxy *curproxy, struct server *newsrv, char **err)
464{
Willy Tarreaue5733232019-05-22 19:24:06 +0200465#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100466 char *arg;
467
468 arg = args[*cur_arg + 1];
469 if (!*arg) {
470 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
471 return ERR_ALERT | ERR_FATAL;
472 }
473
474 if (!strcmp(arg, "*")) {
475 /* Use the namespace associated with the connection (if present). */
476 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
477 return 0;
478 }
479
480 /*
481 * As this parser may be called several times for the same 'default-server'
482 * object, or for a new 'server' instance deriving from a 'default-server'
483 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
484 */
485 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
486
487 newsrv->netns = netns_store_lookup(arg, strlen(arg));
488 if (!newsrv->netns)
489 newsrv->netns = netns_store_insert(arg);
490
491 if (!newsrv->netns) {
492 memprintf(err, "Cannot open namespace '%s'", arg);
493 return ERR_ALERT | ERR_FATAL;
494 }
495
496 return 0;
497#else
498 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
499 return ERR_ALERT | ERR_FATAL;
500#endif
501}
502
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100503/* Parse the "no-agent-check" server keyword */
504static int srv_parse_no_agent_check(char **args, int *cur_arg,
505 struct proxy *curproxy, struct server *newsrv, char **err)
506{
507 free_check(&newsrv->agent);
508 newsrv->agent.inter = 0;
509 newsrv->agent.port = 0;
510 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
511 newsrv->do_agent = 0;
512 return 0;
513}
514
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100515/* Parse the "no-backup" server keyword */
516static int srv_parse_no_backup(char **args, int *cur_arg,
517 struct proxy *curproxy, struct server *newsrv, char **err)
518{
519 newsrv->flags &= ~SRV_F_BACKUP;
520 return 0;
521}
522
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100523/* Parse the "no-check" server keyword */
524static int srv_parse_no_check(char **args, int *cur_arg,
525 struct proxy *curproxy, struct server *newsrv, char **err)
526{
527 free_check(&newsrv->check);
528 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
529 newsrv->do_check = 0;
530 return 0;
531}
532
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100533/* Parse the "no-check-send-proxy" server keyword */
534static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
535 struct proxy *curproxy, struct server *newsrv, char **err)
536{
537 newsrv->check.send_proxy = 0;
538 return 0;
539}
540
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100541/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200542static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100543{
544 srv->pp_opts &= ~flags;
545 return 0;
546}
547
548/* Parse the "no-send-proxy" server keyword */
549static int srv_parse_no_send_proxy(char **args, int *cur_arg,
550 struct proxy *curproxy, struct server *newsrv, char **err)
551{
552 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
553}
554
555/* Parse the "no-send-proxy-v2" server keyword */
556static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
557 struct proxy *curproxy, struct server *newsrv, char **err)
558{
559 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
560}
561
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100562/* Parse the "non-stick" server keyword */
563static int srv_parse_non_stick(char **args, int *cur_arg,
564 struct proxy *curproxy, struct server *newsrv, char **err)
565{
566 newsrv->flags |= SRV_F_NON_STICK;
567 return 0;
568}
569
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100570/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200571static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100572{
573 srv->pp_opts |= flags;
574 return 0;
575}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200576/* parse the "proto" server keyword */
577static int srv_parse_proto(char **args, int *cur_arg,
578 struct proxy *px, struct server *newsrv, char **err)
579{
580 struct ist proto;
581
582 if (!*args[*cur_arg + 1]) {
583 memprintf(err, "'%s' : missing value", args[*cur_arg]);
584 return ERR_ALERT | ERR_FATAL;
585 }
586 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
587 newsrv->mux_proto = get_mux_proto(proto);
588 if (!newsrv->mux_proto) {
589 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
590 return ERR_ALERT | ERR_FATAL;
591 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200592 return 0;
593}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100594
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100595/* parse the "proxy-v2-options" */
596static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
597 struct proxy *px, struct server *newsrv, char **err)
598{
599 char *p, *n;
600 for (p = args[*cur_arg+1]; p; p = n) {
601 n = strchr(p, ',');
602 if (n)
603 *n++ = '\0';
604 if (!strcmp(p, "ssl")) {
605 newsrv->pp_opts |= SRV_PP_V2_SSL;
606 } else if (!strcmp(p, "cert-cn")) {
607 newsrv->pp_opts |= SRV_PP_V2_SSL;
608 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100609 } else if (!strcmp(p, "cert-key")) {
610 newsrv->pp_opts |= SRV_PP_V2_SSL;
611 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
612 } else if (!strcmp(p, "cert-sig")) {
613 newsrv->pp_opts |= SRV_PP_V2_SSL;
614 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
615 } else if (!strcmp(p, "ssl-cipher")) {
616 newsrv->pp_opts |= SRV_PP_V2_SSL;
617 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100618 } else if (!strcmp(p, "authority")) {
619 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100620 } else if (!strcmp(p, "crc32c")) {
621 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100622 } else
623 goto fail;
624 }
625 return 0;
626 fail:
627 if (err)
628 memprintf(err, "'%s' : proxy v2 option not implemented", p);
629 return ERR_ALERT | ERR_FATAL;
630}
631
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100632/* Parse the "observe" server keyword */
633static int srv_parse_observe(char **args, int *cur_arg,
634 struct proxy *curproxy, struct server *newsrv, char **err)
635{
636 char *arg;
637
638 arg = args[*cur_arg + 1];
639 if (!*arg) {
640 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
641 return ERR_ALERT | ERR_FATAL;
642 }
643
644 if (!strcmp(arg, "none")) {
645 newsrv->observe = HANA_OBS_NONE;
646 }
647 else if (!strcmp(arg, "layer4")) {
648 newsrv->observe = HANA_OBS_LAYER4;
649 }
650 else if (!strcmp(arg, "layer7")) {
651 if (curproxy->mode != PR_MODE_HTTP) {
652 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
653 return ERR_ALERT;
654 }
655 newsrv->observe = HANA_OBS_LAYER7;
656 }
657 else {
658 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
659 "but got '%s'\n", args[*cur_arg], arg);
660 return ERR_ALERT | ERR_FATAL;
661 }
662
663 return 0;
664}
665
Frédéric Lécaille16186232017-03-14 16:42:49 +0100666/* Parse the "redir" server keyword */
667static int srv_parse_redir(char **args, int *cur_arg,
668 struct proxy *curproxy, struct server *newsrv, char **err)
669{
670 char *arg;
671
672 arg = args[*cur_arg + 1];
673 if (!*arg) {
674 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
675 return ERR_ALERT | ERR_FATAL;
676 }
677
678 free(newsrv->rdr_pfx);
679 newsrv->rdr_pfx = strdup(arg);
680 newsrv->rdr_len = strlen(arg);
681
682 return 0;
683}
684
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100685/* Parse the "send-proxy" server keyword */
686static int srv_parse_send_proxy(char **args, int *cur_arg,
687 struct proxy *curproxy, struct server *newsrv, char **err)
688{
689 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
690}
691
692/* Parse the "send-proxy-v2" server keyword */
693static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
694 struct proxy *curproxy, struct server *newsrv, char **err)
695{
696 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
697}
698
Frédéric Lécailledba97072017-03-17 15:33:50 +0100699
700/* Parse the "source" server keyword */
701static int srv_parse_source(char **args, int *cur_arg,
702 struct proxy *curproxy, struct server *newsrv, char **err)
703{
704 char *errmsg;
705 int port_low, port_high;
706 struct sockaddr_storage *sk;
707 struct protocol *proto;
708
709 errmsg = NULL;
710
711 if (!*args[*cur_arg + 1]) {
712 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
713 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
714 goto err;
715 }
716
717 /* 'sk' is statically allocated (no need to be freed). */
718 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
719 if (!sk) {
720 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
721 goto err;
722 }
723
724 proto = protocol_by_family(sk->ss_family);
725 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100726 ha_alert("'%s %s' : connect() not supported for this address family.\n",
727 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100728 goto err;
729 }
730
731 newsrv->conn_src.opts |= CO_SRC_BIND;
732 newsrv->conn_src.source_addr = *sk;
733
734 if (port_low != port_high) {
735 int i;
736
737 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100738 ha_alert("'%s' does not support port offsets (found '%s').\n",
739 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100740 goto err;
741 }
742
743 if (port_low <= 0 || port_low > 65535 ||
744 port_high <= 0 || port_high > 65535 ||
745 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100746 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 +0100747 goto err;
748 }
749 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
750 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
751 newsrv->conn_src.sport_range->ports[i] = port_low + i;
752 }
753
754 *cur_arg += 2;
755 while (*(args[*cur_arg])) {
756 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
757#if defined(CONFIG_HAP_TRANSPARENT)
758 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100759 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
760 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100761 goto err;
762 }
763 if (!strcmp(args[*cur_arg + 1], "client")) {
764 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
765 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
766 }
767 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
768 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
769 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
770 }
771 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
772 char *name, *end;
773
774 name = args[*cur_arg + 1] + 7;
775 while (isspace(*name))
776 name++;
777
778 end = name;
779 while (*end && !isspace(*end) && *end != ',' && *end != ')')
780 end++;
781
782 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
783 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
784 free(newsrv->conn_src.bind_hdr_name);
785 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
786 newsrv->conn_src.bind_hdr_len = end - name;
787 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
788 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
789 newsrv->conn_src.bind_hdr_occ = -1;
790
791 /* now look for an occurrence number */
792 while (isspace(*end))
793 end++;
794 if (*end == ',') {
795 end++;
796 name = end;
797 if (*end == '-')
798 end++;
799 while (isdigit((int)*end))
800 end++;
801 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
802 }
803
804 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100805 ha_alert("usesrc hdr_ip(name,num) does not support negative"
806 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100807 goto err;
808 }
809 }
810 else {
811 struct sockaddr_storage *sk;
812 int port1, port2;
813
814 /* 'sk' is statically allocated (no need to be freed). */
815 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
816 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100817 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100818 goto err;
819 }
820
821 proto = protocol_by_family(sk->ss_family);
822 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100823 ha_alert("'%s %s' : connect() not supported for this address family.\n",
824 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100825 goto err;
826 }
827
828 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100829 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
830 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100831 goto err;
832 }
833 newsrv->conn_src.tproxy_addr = *sk;
834 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
835 }
836 global.last_checks |= LSTCHK_NETADM;
837 *cur_arg += 2;
838 continue;
839#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100840 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 +0100841 goto err;
842#endif /* defined(CONFIG_HAP_TRANSPARENT) */
843 } /* "usesrc" */
844
845 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
846#ifdef SO_BINDTODEVICE
847 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100848 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100849 goto err;
850 }
851 free(newsrv->conn_src.iface_name);
852 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
853 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
854 global.last_checks |= LSTCHK_NETADM;
855#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100856 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100857 goto err;
858#endif
859 *cur_arg += 2;
860 continue;
861 }
862 /* this keyword in not an option of "source" */
863 break;
864 } /* while */
865
866 return 0;
867
868 err:
869 free(errmsg);
870 return ERR_ALERT | ERR_FATAL;
871}
872
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100873/* Parse the "stick" server keyword */
874static int srv_parse_stick(char **args, int *cur_arg,
875 struct proxy *curproxy, struct server *newsrv, char **err)
876{
877 newsrv->flags &= ~SRV_F_NON_STICK;
878 return 0;
879}
880
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100881/* Parse the "track" server keyword */
882static int srv_parse_track(char **args, int *cur_arg,
883 struct proxy *curproxy, struct server *newsrv, char **err)
884{
885 char *arg;
886
887 arg = args[*cur_arg + 1];
888 if (!*arg) {
889 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
890 return ERR_ALERT | ERR_FATAL;
891 }
892
893 free(newsrv->trackit);
894 newsrv->trackit = strdup(arg);
895
896 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800897}
898
899/* Parse the "socks4" server keyword */
900static int srv_parse_socks4(char **args, int *cur_arg,
901 struct proxy *curproxy, struct server *newsrv, char **err)
902{
903 char *errmsg;
904 int port_low, port_high;
905 struct sockaddr_storage *sk;
906 struct protocol *proto;
907
908 errmsg = NULL;
909
910 if (!*args[*cur_arg + 1]) {
911 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
912 goto err;
913 }
914
915 /* 'sk' is statically allocated (no need to be freed). */
916 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
917 if (!sk) {
918 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
919 goto err;
920 }
921
922 proto = protocol_by_family(sk->ss_family);
923 if (!proto || !proto->connect) {
924 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
925 goto err;
926 }
927
928 newsrv->flags |= SRV_F_SOCKS4_PROXY;
929 newsrv->socks4_addr = *sk;
930
931 if (port_low != port_high) {
932 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
933 goto err;
934 }
935
936 if (!port_low) {
937 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
938 goto err;
939 }
940
941 return 0;
942
943 err:
944 free(errmsg);
945 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100946}
947
Frédéric Lécailledba97072017-03-17 15:33:50 +0100948
Willy Tarreau034c88c2017-01-23 23:36:45 +0100949/* parse the "tfo" server keyword */
950static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
951{
952 newsrv->flags |= SRV_F_FASTOPEN;
953 return 0;
954}
955
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200956/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200957 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200958 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200959 *
960 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200961 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200962void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200963{
Willy Tarreau87b09662015-04-03 00:22:06 +0200964 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200965
Willy Tarreau87b09662015-04-03 00:22:06 +0200966 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
967 if (stream->srv_conn == srv)
968 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200969}
970
971/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200972 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200973 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200974 *
975 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200976 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200977void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200978{
979 struct server *srv;
980
981 for (srv = px->srv; srv != NULL; srv = srv->next)
982 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200983 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200984}
985
Willy Tarreaubda92272014-05-20 21:55:30 +0200986/* Appends some information to a message string related to a server going UP or
987 * DOWN. If both <forced> and <reason> are null and the server tracks another
988 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200989 * If <check> is non-null, an entire string describing the check result will be
990 * appended after a comma and a space (eg: to report some information from the
991 * check that changed the state). In the other case, the string will be built
992 * using the check results stored into the struct server if present.
993 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200994 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200995 *
996 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200997 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200998void srv_append_status(struct buffer *msg, struct server *s,
999 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001000{
Emeric Brun5a133512017-10-19 14:42:30 +02001001 short status = s->op_st_chg.status;
1002 short code = s->op_st_chg.code;
1003 long duration = s->op_st_chg.duration;
1004 char *desc = s->op_st_chg.reason;
1005
1006 if (check) {
1007 status = check->status;
1008 code = check->code;
1009 duration = check->duration;
1010 desc = check->desc;
1011 }
1012
1013 if (status != -1) {
1014 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1015
1016 if (status >= HCHK_STATUS_L57DATA)
1017 chunk_appendf(msg, ", code: %d", code);
1018
1019 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001020 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001021
1022 chunk_appendf(msg, ", info: \"");
1023
1024 chunk_initlen(&src, desc, 0, strlen(desc));
1025 chunk_asciiencode(msg, &src, '"');
1026
1027 chunk_appendf(msg, "\"");
1028 }
1029
1030 if (duration >= 0)
1031 chunk_appendf(msg, ", check duration: %ldms", duration);
1032 }
1033 else if (desc && *desc) {
1034 chunk_appendf(msg, ", %s", desc);
1035 }
1036 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001037 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001038 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001039
1040 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001041 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001042 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1043 " %d sessions active, %d requeued, %d remaining in queue",
1044 s->proxy->srv_act, s->proxy->srv_bck,
1045 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1046 s->cur_sess, xferred, s->nbpend);
1047 else
1048 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1049 " %d sessions requeued, %d total in queue",
1050 s->proxy->srv_act, s->proxy->srv_bck,
1051 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1052 xferred, s->nbpend);
1053 }
1054}
1055
Emeric Brun5a133512017-10-19 14:42:30 +02001056/* Marks server <s> down, regardless of its checks' statuses. The server is
1057 * registered in a list to postpone the counting of the remaining servers on
1058 * the proxy and transfers queued streams whenever possible to other servers at
1059 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1060 * non-null as the reason for going down or the available data from the check
1061 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001062 *
1063 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001064 */
Emeric Brun5a133512017-10-19 14:42:30 +02001065void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001066{
1067 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001068
Emeric Brun64cc49c2017-10-03 14:46:45 +02001069 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001070 return;
1071
Emeric Brun52a91d32017-08-31 14:41:55 +02001072 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001073 *s->op_st_chg.reason = 0;
1074 s->op_st_chg.status = -1;
1075 if (reason) {
1076 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1077 }
1078 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001079 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001080 s->op_st_chg.code = check->code;
1081 s->op_st_chg.status = check->status;
1082 s->op_st_chg.duration = check->duration;
1083 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001084
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001085 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001086 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001087
Emeric Brun9f0b4582017-10-23 14:39:51 +02001088 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001089 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001090 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001091 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001092 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001093}
1094
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001095/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001096 * in maintenance. The server is registered in a list to postpone the counting
1097 * of the remaining servers on the proxy and tries to grab requests from the
1098 * proxy at a sync point. Maintenance servers are ignored. It stores the
1099 * <reason> if non-null as the reason for going down or the available data
1100 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001101 *
1102 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001103 */
Emeric Brun5a133512017-10-19 14:42:30 +02001104void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001105{
1106 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001107
Emeric Brun64cc49c2017-10-03 14:46:45 +02001108 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001109 return;
1110
Emeric Brun52a91d32017-08-31 14:41:55 +02001111 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001112 return;
1113
Emeric Brun52a91d32017-08-31 14:41:55 +02001114 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001115 *s->op_st_chg.reason = 0;
1116 s->op_st_chg.status = -1;
1117 if (reason) {
1118 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1119 }
1120 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001121 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001122 s->op_st_chg.code = check->code;
1123 s->op_st_chg.status = check->status;
1124 s->op_st_chg.duration = check->duration;
1125 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001126
Emeric Brun64cc49c2017-10-03 14:46:45 +02001127 if (s->slowstart <= 0)
1128 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001129
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001130 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001131 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001132
Emeric Brun9f0b4582017-10-23 14:39:51 +02001133 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001134 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001135 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001136 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001137 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001138}
1139
Willy Tarreau8eb77842014-05-21 13:54:57 +02001140/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001141 * isn't in maintenance. The server is registered in a list to postpone the
1142 * counting of the remaining servers on the proxy and tries to grab requests
1143 * from the proxy. Maintenance servers are ignored. It stores the
1144 * <reason> if non-null as the reason for going down or the available data
1145 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001146 * up. Note that it makes use of the trash to build the log strings, so <reason>
1147 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001148 *
1149 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001150 */
Emeric Brun5a133512017-10-19 14:42:30 +02001151void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001152{
1153 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001154
Emeric Brun64cc49c2017-10-03 14:46:45 +02001155 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001156 return;
1157
Emeric Brun52a91d32017-08-31 14:41:55 +02001158 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001159 return;
1160
Emeric Brun52a91d32017-08-31 14:41:55 +02001161 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001162 *s->op_st_chg.reason = 0;
1163 s->op_st_chg.status = -1;
1164 if (reason) {
1165 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1166 }
1167 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001168 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001169 s->op_st_chg.code = check->code;
1170 s->op_st_chg.status = check->status;
1171 s->op_st_chg.duration = check->duration;
1172 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001173
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001174 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001175 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001176
Emeric Brun9f0b4582017-10-23 14:39:51 +02001177 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001178 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001179 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001180 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001181 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001182}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001183
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001184/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1185 * enforce either maint mode or drain mode. It is not allowed to set more than
1186 * one flag at once. The equivalent "inherited" flag is propagated to all
1187 * tracking servers. Maintenance mode disables health checks (but not agent
1188 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001189 * is done. If <cause> is non-null, it will be displayed at the end of the log
1190 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001191 *
1192 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001193 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001194void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001195{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001196 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001197
1198 if (!mode)
1199 return;
1200
1201 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001202 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001203 return;
1204
Emeric Brun52a91d32017-08-31 14:41:55 +02001205 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001206 if (cause)
1207 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1208
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001209 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001210 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001211
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001212 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001213 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1214 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001215 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001216
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001217 /* compute the inherited flag to propagate */
1218 if (mode & SRV_ADMF_MAINT)
1219 mode = SRV_ADMF_IMAINT;
1220 else if (mode & SRV_ADMF_DRAIN)
1221 mode = SRV_ADMF_IDRAIN;
1222
Emeric Brun9f0b4582017-10-23 14:39:51 +02001223 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001224 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001225 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001226 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001227 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001228}
1229
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001230/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1231 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1232 * than one flag at once. The equivalent "inherited" flag is propagated to all
1233 * tracking servers. Leaving maintenance mode re-enables health checks. When
1234 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001235 *
1236 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001237 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001238void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001239{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001240 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001241
1242 if (!mode)
1243 return;
1244
1245 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001246 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001247 return;
1248
Emeric Brun52a91d32017-08-31 14:41:55 +02001249 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001250
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001251 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001252 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001253
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001254 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001255 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1256 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001257 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001258
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001259 if (mode & SRV_ADMF_MAINT)
1260 mode = SRV_ADMF_IMAINT;
1261 else if (mode & SRV_ADMF_DRAIN)
1262 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001263
Emeric Brun9f0b4582017-10-23 14:39:51 +02001264 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001265 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001266 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001267 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001268 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001269}
1270
Willy Tarreau757478e2016-11-03 19:22:19 +01001271/* principle: propagate maint and drain to tracking servers. This is useful
1272 * upon startup so that inherited states are correct.
1273 */
1274static void srv_propagate_admin_state(struct server *srv)
1275{
1276 struct server *srv2;
1277
1278 if (!srv->trackers)
1279 return;
1280
1281 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001282 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001283 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001284 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001285
Emeric Brun52a91d32017-08-31 14:41:55 +02001286 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001287 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001288 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001289 }
1290}
1291
1292/* Compute and propagate the admin states for all servers in proxy <px>.
1293 * Only servers *not* tracking another one are considered, because other
1294 * ones will be handled when the server they track is visited.
1295 */
1296void srv_compute_all_admin_states(struct proxy *px)
1297{
1298 struct server *srv;
1299
1300 for (srv = px->srv; srv; srv = srv->next) {
1301 if (srv->track)
1302 continue;
1303 srv_propagate_admin_state(srv);
1304 }
1305}
1306
Willy Tarreaudff55432012-10-10 17:51:05 +02001307/* Note: must not be declared <const> as its list will be overwritten.
1308 * Please take care of keeping this list alphabetically sorted, doing so helps
1309 * all code contributors.
1310 * Optional keywords are also declared with a NULL ->parse() function so that
1311 * the config parser can report an appropriate error when a known keyword was
1312 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001313 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001314 */
1315static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001316 { "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 +01001317 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001318 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001319 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001320 { "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 +01001321 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001322 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1323 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001324 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001325 { "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 +01001326 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001327 { "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 +01001328 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001329 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001330 { "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 +01001331 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1332 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001333 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001334 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001335 { "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 +01001336 { "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 +02001337 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001338 { "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 +01001339 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001340 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1341 { "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 +02001342 { "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 +01001343 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Willy Tarreau034c88c2017-01-23 23:36:45 +01001344 { "tfo", srv_parse_tfo, 0, 0 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001345 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001346 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1347 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001348 { NULL, NULL, 0 },
1349}};
1350
Willy Tarreau0108d902018-11-25 19:14:37 +01001351INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001352
Willy Tarreau004e0452013-11-21 11:22:01 +01001353/* Recomputes the server's eweight based on its state, uweight, the current time,
1354 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001355 * state is automatically disabled if the time is elapsed. If <must_update> is
1356 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001357 *
1358 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001359 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001360void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001361{
1362 struct proxy *px = sv->proxy;
1363 unsigned w;
1364
1365 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1366 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001367 if (sv->next_state == SRV_ST_STARTING)
1368 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001369 }
1370
1371 /* We must take care of not pushing the server to full throttle during slow starts.
1372 * It must also start immediately, at least at the minimal step when leaving maintenance.
1373 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001374 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001375 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1376 else
1377 w = px->lbprm.wdiv;
1378
Emeric Brun52a91d32017-08-31 14:41:55 +02001379 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001380
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001381 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001382 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001383 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001384}
1385
Willy Tarreaubaaee002006-06-26 02:48:02 +02001386/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001387 * Parses weight_str and configures sv accordingly.
1388 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001389 *
1390 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001391 */
1392const char *server_parse_weight_change_request(struct server *sv,
1393 const char *weight_str)
1394{
1395 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001396 long int w;
1397 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001398
1399 px = sv->proxy;
1400
1401 /* if the weight is terminated with '%', it is set relative to
1402 * the initial weight, otherwise it is absolute.
1403 */
1404 if (!*weight_str)
1405 return "Require <weight> or <weight%>.\n";
1406
Simon Hormanb796afa2013-02-12 10:45:53 +09001407 w = strtol(weight_str, &end, 10);
1408 if (end == weight_str)
1409 return "Empty weight string empty or preceded by garbage";
1410 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001411 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001412 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001413 /* Avoid integer overflow */
1414 if (w > 25600)
1415 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001416 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001417 if (w > 256)
1418 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001419 }
1420 else if (w < 0 || w > 256)
1421 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001422 else if (end[0] != '\0')
1423 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001424
1425 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1426 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1427
1428 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001429 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001430
1431 return NULL;
1432}
1433
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001434/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001435 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1436 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001437 * Returns:
1438 * - error string on error
1439 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001440 *
1441 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001442 */
1443const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001444 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001445{
1446 unsigned char ip[INET6_ADDRSTRLEN];
1447
1448 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001449 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001450 return NULL;
1451 }
1452 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001453 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001454 return NULL;
1455 }
1456
1457 return "Could not understand IP address format.\n";
1458}
1459
Willy Tarreau46b7f532018-08-21 11:54:26 +02001460/*
1461 * Must be called with the server lock held.
1462 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001463const char *server_parse_maxconn_change_request(struct server *sv,
1464 const char *maxconn_str)
1465{
1466 long int v;
1467 char *end;
1468
1469 if (!*maxconn_str)
1470 return "Require <maxconn>.\n";
1471
1472 v = strtol(maxconn_str, &end, 10);
1473 if (end == maxconn_str)
1474 return "maxconn string empty or preceded by garbage";
1475 else if (end[0] != '\0')
1476 return "Trailing garbage in maxconn string";
1477
1478 if (sv->maxconn == sv->minconn) { // static maxconn
1479 sv->maxconn = sv->minconn = v;
1480 } else { // dynamic maxconn
1481 sv->maxconn = v;
1482 }
1483
1484 if (may_dequeue_tasks(sv, sv->proxy))
1485 process_srv_queue(sv);
1486
1487 return NULL;
1488}
1489
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001490#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001491static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1492 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001493{
1494 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001495 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001496 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001497 NULL,
1498 };
1499
1500 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001501 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001502
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001503 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1504}
1505
1506static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1507{
1508 struct sample_expr *expr;
1509
1510 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 +01001511 if (!expr) {
1512 memprintf(err, "error detected while parsing sni expression : %s", *err);
1513 return ERR_ALERT | ERR_FATAL;
1514 }
1515
1516 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1517 memprintf(err, "error detected while parsing sni expression : "
1518 " fetch method '%s' extracts information from '%s', "
1519 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001520 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001521 return ERR_ALERT | ERR_FATAL;
1522 }
1523
1524 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1525 release_sample_expr(newsrv->ssl_ctx.sni);
1526 newsrv->ssl_ctx.sni = expr;
1527
1528 return 0;
1529}
1530#endif
1531
1532static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1533{
1534 if (err && *err) {
1535 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001536 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 +01001537 }
1538 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001539 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1540 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001541}
1542
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001543static void srv_conn_src_sport_range_cpy(struct server *srv,
1544 struct server *src)
1545{
1546 int range_sz;
1547
1548 range_sz = src->conn_src.sport_range->size;
1549 if (range_sz > 0) {
1550 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1551 if (srv->conn_src.sport_range != NULL) {
1552 int i;
1553
1554 for (i = 0; i < range_sz; i++) {
1555 srv->conn_src.sport_range->ports[i] =
1556 src->conn_src.sport_range->ports[i];
1557 }
1558 }
1559 }
1560}
1561
1562/*
1563 * Copy <src> server connection source settings to <srv> server everything needed.
1564 */
1565static void srv_conn_src_cpy(struct server *srv, struct server *src)
1566{
1567 srv->conn_src.opts = src->conn_src.opts;
1568 srv->conn_src.source_addr = src->conn_src.source_addr;
1569
1570 /* Source port range copy. */
1571 if (src->conn_src.sport_range != NULL)
1572 srv_conn_src_sport_range_cpy(srv, src);
1573
1574#ifdef CONFIG_HAP_TRANSPARENT
1575 if (src->conn_src.bind_hdr_name != NULL) {
1576 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1577 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1578 }
1579 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1580 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1581#endif
1582 if (src->conn_src.iface_name != NULL)
1583 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1584}
1585
1586/*
1587 * Copy <src> server SSL settings to <srv> server allocating
1588 * everything needed.
1589 */
1590#if defined(USE_OPENSSL)
1591static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1592{
1593 if (src->ssl_ctx.ca_file != NULL)
1594 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1595 if (src->ssl_ctx.crl_file != NULL)
1596 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1597 if (src->ssl_ctx.client_crt != NULL)
1598 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1599
1600 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1601
1602 if (src->ssl_ctx.verify_host != NULL)
1603 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1604 if (src->ssl_ctx.ciphers != NULL)
1605 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001606#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001607 if (src->ssl_ctx.ciphersuites != NULL)
1608 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1609#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001610 if (src->sni_expr != NULL)
1611 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001612
1613#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1614 if (src->ssl_ctx.alpn_str) {
1615 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1616 if (srv->ssl_ctx.alpn_str) {
1617 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1618 src->ssl_ctx.alpn_len);
1619 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1620 }
1621 }
1622#endif
1623#ifdef OPENSSL_NPN_NEGOTIATED
1624 if (src->ssl_ctx.npn_str) {
1625 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1626 if (srv->ssl_ctx.npn_str) {
1627 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1628 src->ssl_ctx.npn_len);
1629 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1630 }
1631 }
1632#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001633}
1634#endif
1635
1636/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001637 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001638 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001639 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001640 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001641static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001642{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001643 char *hostname_dn;
1644 int hostname_len, hostname_dn_len;
1645
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001646 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001647 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001648
Christopher Faulet67957bd2017-09-27 11:00:59 +02001649 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001650 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001651 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1652 hostname_dn, trash.size);
1653 if (hostname_dn_len == -1)
1654 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001655
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001656
Christopher Faulet67957bd2017-09-27 11:00:59 +02001657 free(srv->hostname);
1658 free(srv->hostname_dn);
1659 srv->hostname = strdup(hostname);
1660 srv->hostname_dn = strdup(hostname_dn);
1661 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001662 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001663 goto err;
1664
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001665 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001666
1667 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001668 free(srv->hostname); srv->hostname = NULL;
1669 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001670 return -1;
1671}
1672
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001673/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001674 * Copy <src> server settings to <srv> server allocating
1675 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001676 * This function is not supposed to be called at any time, but only
1677 * during server settings parsing or during server allocations from
1678 * a server template, and just after having calloc()'ed a new server.
1679 * So, <src> may only be a default server (when parsing server settings)
1680 * or a server template (during server allocations from a server template).
1681 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1682 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001683 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001684static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001685{
1686 /* Connection source settings copy */
1687 srv_conn_src_cpy(srv, src);
1688
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001689 if (srv_tmpl) {
1690 srv->addr = src->addr;
1691 srv->svc_port = src->svc_port;
1692 }
1693
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001694 srv->pp_opts = src->pp_opts;
1695 if (src->rdr_pfx != NULL) {
1696 srv->rdr_pfx = strdup(src->rdr_pfx);
1697 srv->rdr_len = src->rdr_len;
1698 }
1699 if (src->cookie != NULL) {
1700 srv->cookie = strdup(src->cookie);
1701 srv->cklen = src->cklen;
1702 }
1703 srv->use_ssl = src->use_ssl;
1704 srv->check.addr = srv->agent.addr = src->check.addr;
1705 srv->check.use_ssl = src->check.use_ssl;
1706 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001707 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001708 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001709 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001710 /* Note: 'flags' field has potentially been already initialized. */
1711 srv->flags |= src->flags;
1712 srv->do_check = src->do_check;
1713 srv->do_agent = src->do_agent;
1714 if (srv->check.port)
1715 srv->flags |= SRV_F_CHECKPORT;
1716 srv->check.inter = src->check.inter;
1717 srv->check.fastinter = src->check.fastinter;
1718 srv->check.downinter = src->check.downinter;
1719 srv->agent.use_ssl = src->agent.use_ssl;
1720 srv->agent.port = src->agent.port;
1721 if (src->agent.send_string != NULL)
1722 srv->agent.send_string = strdup(src->agent.send_string);
1723 srv->agent.send_string_len = src->agent.send_string_len;
1724 srv->agent.inter = src->agent.inter;
1725 srv->agent.fastinter = src->agent.fastinter;
1726 srv->agent.downinter = src->agent.downinter;
1727 srv->maxqueue = src->maxqueue;
1728 srv->minconn = src->minconn;
1729 srv->maxconn = src->maxconn;
1730 srv->slowstart = src->slowstart;
1731 srv->observe = src->observe;
1732 srv->onerror = src->onerror;
1733 srv->onmarkeddown = src->onmarkeddown;
1734 srv->onmarkedup = src->onmarkedup;
1735 if (src->trackit != NULL)
1736 srv->trackit = strdup(src->trackit);
1737 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1738 srv->uweight = srv->iweight = src->iweight;
1739
1740 srv->check.send_proxy = src->check.send_proxy;
1741 /* health: up, but will fall down at first failure */
1742 srv->check.rise = srv->check.health = src->check.rise;
1743 srv->check.fall = src->check.fall;
1744
1745 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001746 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1747 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1748 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749 srv->check.state |= CHK_ST_PAUSED;
1750 srv->check.health = 0;
1751 }
1752
1753 /* health: up but will fall down at first failure */
1754 srv->agent.rise = srv->agent.health = src->agent.rise;
1755 srv->agent.fall = src->agent.fall;
1756
1757 if (src->resolvers_id != NULL)
1758 srv->resolvers_id = strdup(src->resolvers_id);
1759 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001760 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001761 if (srv->dns_opts.family_prio == AF_UNSPEC)
1762 srv->dns_opts.family_prio = AF_INET6;
1763 memcpy(srv->dns_opts.pref_net,
1764 src->dns_opts.pref_net,
1765 sizeof srv->dns_opts.pref_net);
1766 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1767
1768 srv->init_addr_methods = src->init_addr_methods;
1769 srv->init_addr = src->init_addr;
1770#if defined(USE_OPENSSL)
1771 srv_ssl_settings_cpy(srv, src);
1772#endif
1773#ifdef TCP_USER_TIMEOUT
1774 srv->tcp_ut = src->tcp_ut;
1775#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001776 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001777 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001778 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001779 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001780
Olivier Houchard8da5f982017-08-04 18:35:36 +02001781 if (srv_tmpl)
1782 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001783
1784 srv->check.via_socks4 = src->check.via_socks4;
1785 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001786}
1787
William Lallemand313bfd12018-10-26 14:47:32 +02001788struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001789{
1790 struct server *srv;
1791
1792 srv = calloc(1, sizeof *srv);
1793 if (!srv)
1794 return NULL;
1795
1796 srv->obj_type = OBJ_TYPE_SERVER;
1797 srv->proxy = proxy;
1798 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001799 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001800
Emeric Brun52a91d32017-08-31 14:41:55 +02001801 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001802 srv->last_change = now.tv_sec;
1803
1804 srv->check.status = HCHK_STATUS_INI;
1805 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001806 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001807 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1808
1809 srv->agent.status = HCHK_STATUS_INI;
1810 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001811 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001812 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1813
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001814 srv->pool_purge_delay = 1000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001815 srv->max_idle_conns = -1;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001816 srv->max_reuse = -1;
Olivier Houchard006e3102018-12-10 18:30:32 +01001817
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001818 return srv;
1819}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001820
1821/*
1822 * Validate <srv> server health-check settings.
1823 * Returns 0 if everything is OK, -1 if not.
1824 */
1825static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1826{
1827 struct tcpcheck_rule *r = NULL;
1828 struct list *l;
1829
1830 /*
1831 * We need at least a service port, a check port or the first tcp-check rule must
1832 * be a 'connect' one when checking an IPv4/IPv6 server.
1833 */
1834 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1835 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1836 return 0;
1837
1838 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1839 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001840 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1841 "Check has been disabled.\n",
1842 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001843 return -1;
1844 }
1845
1846 /* search the first action (connect / send / expect) in the list */
1847 l = &srv->proxy->tcpcheck_rules;
1848 list_for_each_entry(r, l, list) {
1849 if (r->action != TCPCHK_ACT_COMMENT)
1850 break;
1851 }
1852
1853 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001854 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1855 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1856 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001857 return -1;
1858 }
1859
1860 /* scan the tcp-check ruleset to ensure a port has been configured */
1861 l = &srv->proxy->tcpcheck_rules;
1862 list_for_each_entry(r, l, list) {
1863 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001864 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1865 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1866 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001867 return -1;
1868 }
1869 }
1870
1871 return 0;
1872}
1873
1874/*
1875 * Initialize <srv> health-check structure.
1876 * Returns the error string in case of memory allocation failure, NULL if not.
1877 */
1878static const char *do_health_check_init(struct server *srv, int check_type, int state)
1879{
1880 const char *ret;
1881
1882 if (!srv->do_check)
1883 return NULL;
1884
1885 ret = init_check(&srv->check, check_type);
1886 if (ret)
1887 return ret;
1888
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001889 srv->check.state |= state;
1890 global.maxsock++;
1891
1892 return NULL;
1893}
1894
1895static int server_health_check_init(const char *file, int linenum,
1896 struct server *srv, struct proxy *curproxy)
1897{
1898 const char *ret;
1899
1900 if (!srv->do_check)
1901 return 0;
1902
1903 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001904 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1905 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001906 return ERR_ALERT | ERR_FATAL;
1907 }
1908
1909 if (server_healthcheck_validate(file, linenum, srv) < 0)
1910 return ERR_ALERT | ERR_ABORT;
1911
1912 /* note: check type will be set during the config review phase */
1913 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1914 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001915 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001916 return ERR_ALERT | ERR_ABORT;
1917 }
1918
1919 return 0;
1920}
1921
1922/*
1923 * Initialize <srv> agent check structure.
1924 * Returns the error string in case of memory allocation failure, NULL if not.
1925 */
1926static const char *do_server_agent_check_init(struct server *srv, int state)
1927{
1928 const char *ret;
1929
1930 if (!srv->do_agent)
1931 return NULL;
1932
1933 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1934 if (ret)
1935 return ret;
1936
1937 if (!srv->agent.inter)
1938 srv->agent.inter = srv->check.inter;
1939
1940 srv->agent.state |= state;
1941 global.maxsock++;
1942
1943 return NULL;
1944}
1945
1946static int server_agent_check_init(const char *file, int linenum,
1947 struct server *srv, struct proxy *curproxy)
1948{
1949 const char *ret;
1950
1951 if (!srv->do_agent)
1952 return 0;
1953
1954 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001955 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001956 file, linenum, srv->id);
1957 return ERR_ALERT | ERR_FATAL;
1958 }
1959
1960 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1961 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001962 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001963 return ERR_ALERT | ERR_ABORT;
1964 }
1965
1966 return 0;
1967}
1968
1969#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1970static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1971 struct server *srv, struct proxy *proxy)
1972{
1973 int ret;
1974 char *err = NULL;
1975
1976 if (!srv->sni_expr)
1977 return 0;
1978
1979 ret = server_parse_sni_expr(srv, proxy, &err);
1980 if (!ret)
1981 return 0;
1982
1983 display_parser_err(file, linenum, args, cur_arg, &err);
1984 free(err);
1985
1986 return ret;
1987}
1988#endif
1989
1990/*
1991 * Server initializations finalization.
1992 * Initialize health check, agent check and SNI expression if enabled.
1993 * Must not be called for a default server instance.
1994 */
1995static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1996 struct server *srv, struct proxy *px)
1997{
1998 int ret;
1999
2000 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2001 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2002 return ret;
2003 }
2004
2005#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2006 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2007 return ret;
2008#endif
2009
2010 if (srv->flags & SRV_F_BACKUP)
2011 px->srv_bck++;
2012 else
2013 px->srv_act++;
2014 srv_lb_commit_status(srv);
2015
2016 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002017err:
2018 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002019}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002020
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002021/*
2022 * Parse as much as possible such a range string argument: low[-high]
2023 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2024 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2025 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2026 * Returns 0 if succeeded, -1 if not.
2027 */
2028static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2029{
2030 char *nb_high_arg;
2031
2032 *nb_high = 0;
2033 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002034 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002035
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002036 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002037 *nb_high_arg++ = '\0';
2038 *nb_high = atoi(nb_high_arg);
2039 }
2040 else {
2041 *nb_high += *nb_low;
2042 *nb_low = 1;
2043 }
2044
2045 if (*nb_low < 0 || *nb_high < *nb_low)
2046 return -1;
2047
2048 return 0;
2049}
2050
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002051static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2052{
2053 chunk_printf(&trash, "%s%d", prefix, nb);
2054 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002055 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002056}
2057
2058/*
2059 * Initialize as much as possible servers from <srv> server template.
2060 * Note that a server template is a special server with
2061 * a few different parameters than a server which has
2062 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002063 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002064 * 'srv' template included.
2065 */
2066static int server_template_init(struct server *srv, struct proxy *px)
2067{
2068 int i;
2069 struct server *newsrv;
2070
2071 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2072 int check_init_state;
2073 int agent_init_state;
2074
2075 newsrv = new_server(px);
2076 if (!newsrv)
2077 goto err;
2078
2079 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002080 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002081#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2082 if (newsrv->sni_expr) {
2083 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2084 if (!newsrv->ssl_ctx.sni)
2085 goto err;
2086 }
2087#endif
2088 /* Set this new server ID. */
2089 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2090
2091 /* Initial checks states. */
2092 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2093 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2094
2095 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2096 do_server_agent_check_init(newsrv, agent_init_state))
2097 goto err;
2098
2099 /* Linked backwards first. This will be restablished after parsing. */
2100 newsrv->next = px->srv;
2101 px->srv = newsrv;
2102 }
2103 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2104
2105 return i - srv->tmpl_info.nb_low;
2106
2107 err:
2108 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2109 if (newsrv) {
2110#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2111 release_sample_expr(newsrv->ssl_ctx.sni);
2112#endif
2113 free_check(&newsrv->agent);
2114 free_check(&newsrv->check);
2115 }
2116 free(newsrv);
2117 return i - srv->tmpl_info.nb_low;
2118}
2119
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002120int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr)
Willy Tarreau272adea2014-03-31 10:39:59 +02002121{
2122 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002123 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002124 char *errmsg = NULL;
2125 int err_code = 0;
2126 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002127 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002128
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002129 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002130 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002131 !strcmp(args[0], "default-server") ||
2132 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002133 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002134 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002135 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002136 int srv_tmpl = !defsrv && !srv;
2137 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002138
2139 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002140 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002141 err_code |= ERR_ALERT | ERR_FATAL;
2142 goto out;
2143 }
2144 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002145 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002146
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002147 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002148 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002149 if (!*args[2]) {
2150 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002151 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002152 file, linenum, args[0]);
2153 err_code |= ERR_ALERT | ERR_FATAL;
2154 goto out;
2155 }
2156
2157 err = invalid_char(args[1]);
2158 }
2159 else if (srv_tmpl) {
2160 if (!*args[3]) {
2161 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002162 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 +02002163 file, linenum, args[0]);
2164 err_code |= ERR_ALERT | ERR_FATAL;
2165 goto out;
2166 }
2167
2168 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002169 }
2170
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002171 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002172 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 +02002173 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002174 err_code |= ERR_ALERT | ERR_FATAL;
2175 goto out;
2176 }
2177
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002178 cur_arg = 2;
2179 if (srv_tmpl) {
2180 /* Parse server-template <nb | range> arg. */
2181 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002182 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002183 file, linenum, args[0], args[cur_arg]);
2184 err_code |= ERR_ALERT | ERR_FATAL;
2185 goto out;
2186 }
2187 cur_arg++;
2188 }
2189
Willy Tarreau272adea2014-03-31 10:39:59 +02002190 if (!defsrv) {
2191 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002192 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002193 struct protocol *proto;
2194
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002195 newsrv = new_server(curproxy);
2196 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002197 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002198 err_code |= ERR_ALERT | ERR_ABORT;
2199 goto out;
2200 }
2201
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002202 if (srv_tmpl) {
2203 newsrv->tmpl_info.nb_low = tmpl_range_low;
2204 newsrv->tmpl_info.nb_high = tmpl_range_high;
2205 }
2206
Willy Tarreau272adea2014-03-31 10:39:59 +02002207 /* the servers are linked backwards first */
2208 newsrv->next = curproxy->srv;
2209 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002210 newsrv->conf.file = strdup(file);
2211 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002212 /* Note: for a server template, its id is its prefix.
2213 * This is a temporary id which will be used for server allocations to come
2214 * after parsing.
2215 */
2216 if (srv)
2217 newsrv->id = strdup(args[1]);
2218 else
2219 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002220
2221 /* several ways to check the port component :
2222 * - IP => port=+0, relative (IPv4 only)
2223 * - IP: => port=+0, relative
2224 * - IP:N => port=N, absolute
2225 * - IP:+N => port=+N, relative
2226 * - IP:-N => port=-N, relative
2227 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002228 if (!parse_addr)
2229 goto skip_addr;
2230
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002231 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002232 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002233 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002234 err_code |= ERR_ALERT | ERR_FATAL;
2235 goto out;
2236 }
2237
2238 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002239 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002240 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002241 file, linenum, args[0], args[1]);
2242 err_code |= ERR_ALERT | ERR_FATAL;
2243 goto out;
2244 }
2245
2246 if (!port1 || !port2) {
2247 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002248 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002249 }
2250 else if (port1 != port2) {
2251 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002252 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002253 file, linenum, args[0], args[1], args[2]);
2254 err_code |= ERR_ALERT | ERR_FATAL;
2255 goto out;
2256 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002257
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002258 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002259 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002260 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002261 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002262 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2263 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2264 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002265 err_code |= ERR_ALERT | ERR_FATAL;
2266 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002267 }
2268 }
2269 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002270 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002271 file, linenum, newsrv->id);
2272 err_code |= ERR_ALERT | ERR_FATAL;
2273 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002274 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002275 }
2276
Willy Tarreau272adea2014-03-31 10:39:59 +02002277 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002278 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002279
Olivier Houchard8da5f982017-08-04 18:35:36 +02002280 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002281 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002282 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002283 err_code |= ERR_ALERT | ERR_FATAL;
2284 goto out;
2285 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002286
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002287 cur_arg++;
2288 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002289 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002290 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002291 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002292 } else {
2293 newsrv = &curproxy->defsrv;
2294 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002295 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002296 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002297 }
2298
2299 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002300 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002301 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2302 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002303 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002304 file, linenum, *err, newsrv->id);
2305 err_code |= ERR_ALERT | ERR_FATAL;
2306 goto out;
2307 }
2308 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002309 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002310 file, linenum, val, args[cur_arg], newsrv->id);
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
2313 }
2314 newsrv->agent.inter = val;
2315 cur_arg += 2;
2316 }
Misiekea849332017-01-09 09:39:51 +01002317 else if (!strcmp(args[cur_arg], "agent-addr")) {
2318 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002319 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002320 goto out;
2321 }
2322
2323 cur_arg += 2;
2324 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002325 else if (!strcmp(args[cur_arg], "agent-port")) {
2326 global.maxsock++;
2327 newsrv->agent.port = atol(args[cur_arg + 1]);
2328 cur_arg += 2;
2329 }
James Brown55f9ff12015-10-21 18:19:05 -07002330 else if (!strcmp(args[cur_arg], "agent-send")) {
2331 global.maxsock++;
2332 free(newsrv->agent.send_string);
2333 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2334 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2335 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2336 cur_arg += 2;
2337 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002338 else if (!strcmp(args[cur_arg], "init-addr")) {
2339 char *p, *end;
2340 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002341 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002342
2343 newsrv->init_addr_methods = 0;
2344 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2345
2346 for (p = args[cur_arg + 1]; *p; p = end) {
2347 /* cut on next comma */
2348 for (end = p; *end && *end != ','; end++);
2349 if (*end)
2350 *(end++) = 0;
2351
Willy Tarreau4310d362016-11-02 15:05:56 +01002352 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002353 if (!strcmp(p, "libc")) {
2354 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2355 }
2356 else if (!strcmp(p, "last")) {
2357 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2358 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002359 else if (!strcmp(p, "none")) {
2360 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2361 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002362 else if (str2ip2(p, &sa, 0)) {
2363 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002364 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002365 file, linenum, args[cur_arg], p);
2366 err_code |= ERR_ALERT | ERR_FATAL;
2367 goto out;
2368 }
2369 newsrv->init_addr = sa;
2370 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2371 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002372 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002373 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 +02002374 file, linenum, args[cur_arg], p);
2375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
2377 }
2378 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002379 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002380 file, linenum, args[cur_arg], p);
2381 err_code |= ERR_ALERT | ERR_FATAL;
2382 goto out;
2383 }
2384 }
2385 cur_arg += 2;
2386 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002387 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002388 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002389 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2390 cur_arg += 2;
2391 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002392 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2393 char *p, *end;
2394
2395 for (p = args[cur_arg + 1]; *p; p = end) {
2396 /* cut on next comma */
2397 for (end = p; *end && *end != ','; end++);
2398 if (*end)
2399 *(end++) = 0;
2400
2401 if (!strcmp(p, "allow-dup-ip")) {
2402 newsrv->dns_opts.accept_duplicate_ip = 1;
2403 }
2404 else if (!strcmp(p, "prevent-dup-ip")) {
2405 newsrv->dns_opts.accept_duplicate_ip = 0;
2406 }
2407 else {
2408 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2409 file, linenum, args[cur_arg], p);
2410 err_code |= ERR_ALERT | ERR_FATAL;
2411 goto out;
2412 }
2413 }
2414
2415 cur_arg += 2;
2416 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002417 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2418 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002419 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002420 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002421 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002422 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002423 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002424 file, linenum, args[cur_arg]);
2425 err_code |= ERR_ALERT | ERR_FATAL;
2426 goto out;
2427 }
2428 cur_arg += 2;
2429 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002430 else if (!strcmp(args[cur_arg], "resolve-net")) {
2431 char *p, *e;
2432 unsigned char mask;
2433 struct dns_options *opt;
2434
2435 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002436 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002437 file, linenum, args[cur_arg]);
2438 err_code |= ERR_ALERT | ERR_FATAL;
2439 goto out;
2440 }
2441
2442 opt = &newsrv->dns_opts;
2443
2444 /* Split arguments by comma, and convert it from ipv4 or ipv6
2445 * string network in in_addr or in6_addr.
2446 */
2447 p = args[cur_arg + 1];
2448 e = p;
2449 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002450 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002451 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002452 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002453 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2454 err_code |= ERR_ALERT | ERR_FATAL;
2455 goto out;
2456 }
2457 /* look for end or comma. */
2458 while (*e != ',' && *e != '\0')
2459 e++;
2460 if (*e == ',') {
2461 *e = '\0';
2462 e++;
2463 }
2464 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2465 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2466 /* Try to convert input string from ipv4 or ipv6 network. */
2467 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2468 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2469 &mask)) {
2470 /* Try to convert input string from ipv6 network. */
2471 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2472 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2473 } else {
2474 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002475 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002476 file, linenum, args[cur_arg], p);
2477 err_code |= ERR_ALERT | ERR_FATAL;
2478 goto out;
2479 }
2480 opt->pref_net_nb++;
2481 p = e;
2482 }
2483
2484 cur_arg += 2;
2485 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002486 else if (!strcmp(args[cur_arg], "rise")) {
2487 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002488 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002489 file, linenum, args[cur_arg]);
2490 err_code |= ERR_ALERT | ERR_FATAL;
2491 goto out;
2492 }
2493
2494 newsrv->check.rise = atol(args[cur_arg + 1]);
2495 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002496 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002497 file, linenum, args[cur_arg]);
2498 err_code |= ERR_ALERT | ERR_FATAL;
2499 goto out;
2500 }
2501
2502 if (newsrv->check.health)
2503 newsrv->check.health = newsrv->check.rise;
2504 cur_arg += 2;
2505 }
2506 else if (!strcmp(args[cur_arg], "fall")) {
2507 newsrv->check.fall = atol(args[cur_arg + 1]);
2508
2509 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002510 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002511 file, linenum, args[cur_arg]);
2512 err_code |= ERR_ALERT | ERR_FATAL;
2513 goto out;
2514 }
2515
2516 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002517 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002518 file, linenum, args[cur_arg]);
2519 err_code |= ERR_ALERT | ERR_FATAL;
2520 goto out;
2521 }
2522
2523 cur_arg += 2;
2524 }
2525 else if (!strcmp(args[cur_arg], "inter")) {
2526 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2527 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002528 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002529 file, linenum, *err, newsrv->id);
2530 err_code |= ERR_ALERT | ERR_FATAL;
2531 goto out;
2532 }
2533 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002534 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002535 file, linenum, val, args[cur_arg], newsrv->id);
2536 err_code |= ERR_ALERT | ERR_FATAL;
2537 goto out;
2538 }
2539 newsrv->check.inter = val;
2540 cur_arg += 2;
2541 }
2542 else if (!strcmp(args[cur_arg], "fastinter")) {
2543 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2544 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002545 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002546 file, linenum, *err, newsrv->id);
2547 err_code |= ERR_ALERT | ERR_FATAL;
2548 goto out;
2549 }
2550 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002551 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002552 file, linenum, val, args[cur_arg], newsrv->id);
2553 err_code |= ERR_ALERT | ERR_FATAL;
2554 goto out;
2555 }
2556 newsrv->check.fastinter = val;
2557 cur_arg += 2;
2558 }
2559 else if (!strcmp(args[cur_arg], "downinter")) {
2560 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2561 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002562 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002563 file, linenum, *err, newsrv->id);
2564 err_code |= ERR_ALERT | ERR_FATAL;
2565 goto out;
2566 }
2567 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002568 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002569 file, linenum, val, args[cur_arg], newsrv->id);
2570 err_code |= ERR_ALERT | ERR_FATAL;
2571 goto out;
2572 }
2573 newsrv->check.downinter = val;
2574 cur_arg += 2;
2575 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002576 else if (!strcmp(args[cur_arg], "port")) {
2577 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002578 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002579 cur_arg += 2;
2580 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002581 else if (!strcmp(args[cur_arg], "weight")) {
2582 int w;
2583 w = atol(args[cur_arg + 1]);
2584 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002585 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002586 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2587 err_code |= ERR_ALERT | ERR_FATAL;
2588 goto out;
2589 }
2590 newsrv->uweight = newsrv->iweight = w;
2591 cur_arg += 2;
2592 }
2593 else if (!strcmp(args[cur_arg], "minconn")) {
2594 newsrv->minconn = atol(args[cur_arg + 1]);
2595 cur_arg += 2;
2596 }
2597 else if (!strcmp(args[cur_arg], "maxconn")) {
2598 newsrv->maxconn = atol(args[cur_arg + 1]);
2599 cur_arg += 2;
2600 }
2601 else if (!strcmp(args[cur_arg], "maxqueue")) {
2602 newsrv->maxqueue = atol(args[cur_arg + 1]);
2603 cur_arg += 2;
2604 }
2605 else if (!strcmp(args[cur_arg], "slowstart")) {
2606 /* slowstart is stored in seconds */
2607 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2608 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002609 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002610 file, linenum, *err, newsrv->id);
2611 err_code |= ERR_ALERT | ERR_FATAL;
2612 goto out;
2613 }
2614 newsrv->slowstart = (val + 999) / 1000;
2615 cur_arg += 2;
2616 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002617 else if (!strcmp(args[cur_arg], "on-error")) {
2618 if (!strcmp(args[cur_arg + 1], "fastinter"))
2619 newsrv->onerror = HANA_ONERR_FASTINTER;
2620 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2621 newsrv->onerror = HANA_ONERR_FAILCHK;
2622 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2623 newsrv->onerror = HANA_ONERR_SUDDTH;
2624 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2625 newsrv->onerror = HANA_ONERR_MARKDWN;
2626 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002627 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002628 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2629 file, linenum, args[cur_arg], args[cur_arg + 1]);
2630 err_code |= ERR_ALERT | ERR_FATAL;
2631 goto out;
2632 }
2633
2634 cur_arg += 2;
2635 }
2636 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2637 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2638 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2639 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002640 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002641 file, linenum, args[cur_arg], args[cur_arg + 1]);
2642 err_code |= ERR_ALERT | ERR_FATAL;
2643 goto out;
2644 }
2645
2646 cur_arg += 2;
2647 }
2648 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2649 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2650 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2651 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002652 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002653 file, linenum, args[cur_arg], args[cur_arg + 1]);
2654 err_code |= ERR_ALERT | ERR_FATAL;
2655 goto out;
2656 }
2657
2658 cur_arg += 2;
2659 }
2660 else if (!strcmp(args[cur_arg], "error-limit")) {
2661 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002662 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002663 file, linenum, args[cur_arg]);
2664 err_code |= ERR_ALERT | ERR_FATAL;
2665 goto out;
2666 }
2667
2668 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2669
2670 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002671 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002672 file, linenum, args[cur_arg]);
2673 err_code |= ERR_ALERT | ERR_FATAL;
2674 goto out;
2675 }
2676 cur_arg += 2;
2677 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002678 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002679 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002680 file, linenum, "usesrc", "source");
2681 err_code |= ERR_ALERT | ERR_FATAL;
2682 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002683 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002684 else {
2685 static int srv_dumped;
2686 struct srv_kw *kw;
2687 char *err;
2688
2689 kw = srv_find_kw(args[cur_arg]);
2690 if (kw) {
2691 char *err = NULL;
2692 int code;
2693
2694 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002695 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 +02002696 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002697 if (kw->skip != -1)
2698 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002699 err_code |= ERR_ALERT | ERR_FATAL;
2700 goto out;
2701 }
2702
2703 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002704 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002705 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002706 if (kw->skip != -1)
2707 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002708 err_code |= ERR_ALERT;
2709 continue;
2710 }
2711
2712 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2713 err_code |= code;
2714
2715 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002716 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002717 if (code & ERR_FATAL) {
2718 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002719 if (kw->skip != -1)
2720 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002721 goto out;
2722 }
2723 }
2724 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002725 if (kw->skip != -1)
2726 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002727 continue;
2728 }
2729
2730 err = NULL;
2731 if (!srv_dumped) {
2732 srv_dump_kws(&err);
2733 indent_msg(&err, 4);
2734 srv_dumped = 1;
2735 }
2736
Christopher Faulet767a84b2017-11-24 16:50:31 +01002737 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002738 file, linenum, args[0], args[1], args[cur_arg],
2739 err ? " Registered keywords :" : "", err ? err : "");
2740 free(err);
2741
2742 err_code |= ERR_ALERT | ERR_FATAL;
2743 goto out;
2744 }
2745 }
2746
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002747 if (!defsrv)
2748 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2749 if (err_code & ERR_FATAL)
2750 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002751 if (srv_tmpl)
2752 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002753 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002754 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002755 return 0;
2756
2757 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002758 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002759 free(errmsg);
2760 return err_code;
2761}
2762
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002763/* Returns a pointer to the first server matching either id <id>.
2764 * NULL is returned if no match is found.
2765 * the lookup is performed in the backend <bk>
2766 */
2767struct server *server_find_by_id(struct proxy *bk, int id)
2768{
2769 struct eb32_node *eb32;
2770 struct server *curserver;
2771
2772 if (!bk || (id ==0))
2773 return NULL;
2774
2775 /* <bk> has no backend capabilities, so it can't have a server */
2776 if (!(bk->cap & PR_CAP_BE))
2777 return NULL;
2778
2779 curserver = NULL;
2780
2781 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2782 if (eb32)
2783 curserver = container_of(eb32, struct server, conf.id);
2784
2785 return curserver;
2786}
2787
2788/* Returns a pointer to the first server matching either name <name>, or id
2789 * if <name> starts with a '#'. NULL is returned if no match is found.
2790 * the lookup is performed in the backend <bk>
2791 */
2792struct server *server_find_by_name(struct proxy *bk, const char *name)
2793{
2794 struct server *curserver;
2795
2796 if (!bk || !name)
2797 return NULL;
2798
2799 /* <bk> has no backend capabilities, so it can't have a server */
2800 if (!(bk->cap & PR_CAP_BE))
2801 return NULL;
2802
2803 curserver = NULL;
2804 if (*name == '#') {
2805 curserver = server_find_by_id(bk, atoi(name + 1));
2806 if (curserver)
2807 return curserver;
2808 }
2809 else {
2810 curserver = bk->srv;
2811
2812 while (curserver && (strcmp(curserver->id, name) != 0))
2813 curserver = curserver->next;
2814
2815 if (curserver)
2816 return curserver;
2817 }
2818
2819 return NULL;
2820}
2821
2822struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2823{
2824 struct server *byname;
2825 struct server *byid;
2826
2827 if (!name && !id)
2828 return NULL;
2829
2830 if (diff)
2831 *diff = 0;
2832
2833 byname = byid = NULL;
2834
2835 if (name) {
2836 byname = server_find_by_name(bk, name);
2837 if (byname && (!id || byname->puid == id))
2838 return byname;
2839 }
2840
2841 /* remaining possibilities :
2842 * - name not set
2843 * - name set but not found
2844 * - name found but ID doesn't match
2845 */
2846 if (id) {
2847 byid = server_find_by_id(bk, id);
2848 if (byid) {
2849 if (byname) {
2850 /* use id only if forced by configuration */
2851 if (byid->flags & SRV_F_FORCED_ID) {
2852 if (diff)
2853 *diff |= 2;
2854 return byid;
2855 }
2856 else {
2857 if (diff)
2858 *diff |= 1;
2859 return byname;
2860 }
2861 }
2862
2863 /* remaining possibilities:
2864 * - name not set
2865 * - name set but not found
2866 */
2867 if (name && diff)
2868 *diff |= 2;
2869 return byid;
2870 }
2871
2872 /* id bot found */
2873 if (byname) {
2874 if (diff)
2875 *diff |= 1;
2876 return byname;
2877 }
2878 }
2879
2880 return NULL;
2881}
2882
Willy Tarreau46b7f532018-08-21 11:54:26 +02002883/* Update a server state using the parameters available in the params list.
2884 *
2885 * Grabs the server lock during operation.
2886 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002887static void srv_update_state(struct server *srv, int version, char **params)
2888{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002889 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002890 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002891
2892 /* fields since version 1
2893 * and common to all other upcoming versions
2894 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002895 enum srv_state srv_op_state;
2896 enum srv_admin srv_admin_state;
2897 unsigned srv_uweight, srv_iweight;
2898 unsigned long srv_last_time_change;
2899 short srv_check_status;
2900 enum chk_result srv_check_result;
2901 int srv_check_health;
2902 int srv_check_state, srv_agent_state;
2903 int bk_f_forced_id;
2904 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002905 int fqdn_set_by_cli;
2906 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002907 const char *port_str;
2908 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002909 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002910
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002911 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002912 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002913 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 switch (version) {
2915 case 1:
2916 /*
2917 * now we can proceed with server's state update:
2918 * srv_addr: params[0]
2919 * srv_op_state: params[1]
2920 * srv_admin_state: params[2]
2921 * srv_uweight: params[3]
2922 * srv_iweight: params[4]
2923 * srv_last_time_change: params[5]
2924 * srv_check_status: params[6]
2925 * srv_check_result: params[7]
2926 * srv_check_health: params[8]
2927 * srv_check_state: params[9]
2928 * srv_agent_state: params[10]
2929 * bk_f_forced_id: params[11]
2930 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002931 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002932 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002933 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002934 */
2935
2936 /* validating srv_op_state */
2937 p = NULL;
2938 errno = 0;
2939 srv_op_state = strtol(params[1], &p, 10);
2940 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2941 (srv_op_state != SRV_ST_STOPPED &&
2942 srv_op_state != SRV_ST_STARTING &&
2943 srv_op_state != SRV_ST_RUNNING &&
2944 srv_op_state != SRV_ST_STOPPING)) {
2945 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2946 }
2947
2948 /* validating srv_admin_state */
2949 p = NULL;
2950 errno = 0;
2951 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002952 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002953
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002954 /* inherited statuses will be recomputed later.
2955 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2956 */
2957 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002958
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002959 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2960 (srv_admin_state != 0 &&
2961 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002962 srv_admin_state != SRV_ADMF_CMAINT &&
2963 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002964 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002965 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002966 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2967 }
2968
2969 /* validating srv_uweight */
2970 p = NULL;
2971 errno = 0;
2972 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002973 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002974 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2975
2976 /* validating srv_iweight */
2977 p = NULL;
2978 errno = 0;
2979 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002980 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002981 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2982
2983 /* validating srv_last_time_change */
2984 p = NULL;
2985 errno = 0;
2986 srv_last_time_change = strtol(params[5], &p, 10);
2987 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2988 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2989
2990 /* validating srv_check_status */
2991 p = NULL;
2992 errno = 0;
2993 srv_check_status = strtol(params[6], &p, 10);
2994 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2995 (srv_check_status >= HCHK_STATUS_SIZE))
2996 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2997
2998 /* validating srv_check_result */
2999 p = NULL;
3000 errno = 0;
3001 srv_check_result = strtol(params[7], &p, 10);
3002 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3003 (srv_check_result != CHK_RES_UNKNOWN &&
3004 srv_check_result != CHK_RES_NEUTRAL &&
3005 srv_check_result != CHK_RES_FAILED &&
3006 srv_check_result != CHK_RES_PASSED &&
3007 srv_check_result != CHK_RES_CONDPASS)) {
3008 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3009 }
3010
3011 /* validating srv_check_health */
3012 p = NULL;
3013 errno = 0;
3014 srv_check_health = strtol(params[8], &p, 10);
3015 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3016 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3017
3018 /* validating srv_check_state */
3019 p = NULL;
3020 errno = 0;
3021 srv_check_state = strtol(params[9], &p, 10);
3022 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3023 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3024 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3025
3026 /* validating srv_agent_state */
3027 p = NULL;
3028 errno = 0;
3029 srv_agent_state = strtol(params[10], &p, 10);
3030 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3031 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3032 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3033
3034 /* validating bk_f_forced_id */
3035 p = NULL;
3036 errno = 0;
3037 bk_f_forced_id = strtol(params[11], &p, 10);
3038 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3039 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3040
3041 /* validating srv_f_forced_id */
3042 p = NULL;
3043 errno = 0;
3044 srv_f_forced_id = strtol(params[12], &p, 10);
3045 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3046 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3047
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003048 /* validating srv_fqdn */
3049 fqdn = params[13];
3050 if (fqdn && *fqdn == '-')
3051 fqdn = NULL;
3052 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3053 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3054 fqdn = NULL;
3055 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003056
Frédéric Lécaille31694712017-08-01 08:47:19 +02003057 port_str = params[14];
3058 if (port_str) {
3059 port = strl2uic(port_str, strlen(port_str));
3060 if (port > USHRT_MAX) {
3061 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3062 port_str = NULL;
3063 }
3064 }
3065
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003066 /* SRV record
3067 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3068 */
3069 srvrecord = params[15];
3070 if (srvrecord && *srvrecord != '_')
3071 srvrecord = NULL;
3072
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003073 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003074 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003075 goto out;
3076
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003077 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003078 /* recover operational state and apply it to this server
3079 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003080 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003081 switch (srv_op_state) {
3082 case SRV_ST_STOPPED:
3083 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003084 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003085 break;
3086 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003087 /* If rise == 1 there is no STARTING state, let's switch to
3088 * RUNNING
3089 */
3090 if (srv->check.rise == 1) {
3091 srv->check.health = srv->check.rise + srv->check.fall - 1;
3092 srv_set_running(srv, "", NULL);
3093 break;
3094 }
3095 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3096 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003097 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003098 break;
3099 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003100 /* If fall == 1 there is no STOPPING state, let's switch to
3101 * STOPPED
3102 */
3103 if (srv->check.fall == 1) {
3104 srv->check.health = 0;
3105 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3106 break;
3107 }
3108 if (srv->check.health < srv->check.rise ||
3109 srv->check.health > srv->check.rise + srv->check.fall - 2)
3110 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003111 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003112 break;
3113 case SRV_ST_RUNNING:
3114 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003115 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003116 break;
3117 }
3118
3119 /* When applying server state, the following rules apply:
3120 * - in case of a configuration change, we apply the setting from the new
3121 * configuration, regardless of old running state
3122 * - if no configuration change, we apply old running state only if old running
3123 * state is different from new configuration state
3124 */
3125 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003126 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3127 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003128 srv_adm_set_maint(srv);
3129 else
3130 srv_adm_set_ready(srv);
3131 }
3132 /* configuration is the same, let's compate old running state and new conf state */
3133 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003134 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003135 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003136 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003137 srv_adm_set_ready(srv);
3138 }
3139 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003140 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003141 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003142 * (srv->iweight is the weight set up in configuration).
3143 * There are two possible reasons for FDRAIN to have been present :
3144 * - previous config weight was zero
3145 * - "set server b/s drain" was sent to the CLI
3146 *
3147 * In the first case, we simply want to drop this drain state
3148 * if the new weight is not zero anymore, meaning the administrator
3149 * has intentionally turned the weight back to a positive value to
3150 * enable the server again after an operation. In the second case,
3151 * the drain state was forced on the CLI regardless of the config's
3152 * weight so we don't want a change to the config weight to lose this
3153 * status. What this means is :
3154 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3155 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003156 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003157 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003158 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003159 }
3160
3161 srv->last_change = date.tv_sec - srv_last_time_change;
3162 srv->check.status = srv_check_status;
3163 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003164
3165 /* Only case we want to apply is removing ENABLED flag which could have been
3166 * done by the "disable health" command over the stats socket
3167 */
3168 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3169 (srv_check_state & CHK_ST_CONFIGURED) &&
3170 !(srv_check_state & CHK_ST_ENABLED))
3171 srv->check.state &= ~CHK_ST_ENABLED;
3172
3173 /* Only case we want to apply is removing ENABLED flag which could have been
3174 * done by the "disable agent" command over the stats socket
3175 */
3176 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3177 (srv_agent_state & CHK_ST_CONFIGURED) &&
3178 !(srv_agent_state & CHK_ST_ENABLED))
3179 srv->agent.state &= ~CHK_ST_ENABLED;
3180
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003181 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3182 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003183 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003184 * It means that a configuration file change has precedence over a unix socket change
3185 * for server's weight
3186 *
3187 * by default, HAProxy applies the following weight when parsing the configuration
3188 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003189 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003190 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003191 srv->uweight = srv_uweight;
3192 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003193 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003194
Willy Tarreaue5a60682016-11-09 14:54:53 +01003195 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003196 if (strcmp(params[0], "-"))
3197 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003198
3199 if (fqdn && srv->hostname) {
3200 if (!strcmp(srv->hostname, fqdn)) {
3201 /* Here we reset the 'set from stats socket FQDN' flag
3202 * to support such transitions:
3203 * Let's say initial FQDN value is foo1 (in configuration file).
3204 * - FQDN changed from stats socket, from foo1 to foo2 value,
3205 * - FQDN changed again from file configuration (with the same previous value
3206 set from stats socket, from foo1 to foo2 value),
3207 * - reload for any other reason than a FQDN modification,
3208 * the configuration file FQDN matches the fqdn server state file value.
3209 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003210 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003211 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003212 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003213 }
3214 else {
3215 /* If the FDQN has been changed from stats socket,
3216 * apply fqdn state file value (which is the value set
3217 * from stats socket).
3218 */
3219 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003220 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003221 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003222 }
3223 }
3224 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003225 /* If all the conditions below are validated, this means
3226 * we're evaluating a server managed by SRV resolution
3227 */
3228 else if (fqdn && !srv->hostname && srvrecord) {
3229 int res;
3230
3231 /* we can't apply previous state if SRV record has changed */
3232 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3233 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);
3234 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3235 goto out;
3236 }
3237
3238 /* create or find a SRV resolution for this srv record */
3239 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3240 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3241 if (srv->srvrq == NULL) {
3242 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3243 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3244 goto out;
3245 }
3246
3247 /* prepare DNS resolution for this server */
3248 res = srv_prepare_for_resolution(srv, fqdn);
3249 if (res == -1) {
3250 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3251 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3252 goto out;
3253 }
3254
3255 /* configure check.port accordingly */
3256 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3257 !(srv->flags & SRV_F_CHECKPORT))
3258 srv->check.port = port;
3259
3260 /* Unset SRV_F_MAPPORTS for SRV records.
3261 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3262 * because no ports are provided in the configuration file.
3263 * This is because HAProxy will use the port found into the SRV record.
3264 */
3265 srv->flags &= ~SRV_F_MAPPORTS;
3266 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003267
Frédéric Lécaille31694712017-08-01 08:47:19 +02003268 if (port_str)
3269 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003270 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003271
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003272 break;
3273 default:
3274 chunk_appendf(msg, ", version '%d' not supported", version);
3275 }
3276
3277 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003278 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003279 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003280 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003281 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003282 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003283}
3284
3285/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3286 * For each proxy, it does the following:
3287 * - opens its server state file (either one or local one)
3288 * - read whole file, line by line
3289 * - analyse each line to check if it matches our current backend:
3290 * - backend name matches
3291 * - backend id matches if id is forced and name doesn't match
3292 * - if the server pointed by the line is found, then state is applied
3293 *
3294 * If the running backend uuid or id differs from the state file, then HAProxy reports
3295 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003296 *
3297 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003298 */
3299void apply_server_state(void)
3300{
3301 char *cur, *end;
3302 char mybuf[SRV_STATE_LINE_MAXLEN];
3303 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003304 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3305 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003306 int arg, srv_arg, version, diff;
3307 FILE *f;
3308 char *filepath;
3309 char globalfilepath[MAXPATHLEN + 1];
3310 char localfilepath[MAXPATHLEN + 1];
3311 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003312 struct proxy *curproxy, *bk;
3313 struct server *srv;
3314
3315 globalfilepathlen = 0;
3316 /* create the globalfilepath variable */
3317 if (global.server_state_file) {
3318 /* absolute path or no base directory provided */
3319 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3320 len = strlen(global.server_state_file);
3321 if (len > MAXPATHLEN) {
3322 globalfilepathlen = 0;
3323 goto globalfileerror;
3324 }
3325 memcpy(globalfilepath, global.server_state_file, len);
3326 globalfilepath[len] = '\0';
3327 globalfilepathlen = len;
3328 }
3329 else if (global.server_state_base) {
3330 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003331 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003332 globalfilepathlen = 0;
3333 goto globalfileerror;
3334 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003335 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003336 globalfilepath[len] = 0;
3337 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003338
3339 /* append a slash if needed */
3340 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3341 if (globalfilepathlen + 1 > MAXPATHLEN) {
3342 globalfilepathlen = 0;
3343 goto globalfileerror;
3344 }
3345 globalfilepath[globalfilepathlen++] = '/';
3346 }
3347
3348 len = strlen(global.server_state_file);
3349 if (globalfilepathlen + len > MAXPATHLEN) {
3350 globalfilepathlen = 0;
3351 goto globalfileerror;
3352 }
3353 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3354 globalfilepathlen += len;
3355 globalfilepath[globalfilepathlen++] = 0;
3356 }
3357 }
3358 globalfileerror:
3359 if (globalfilepathlen == 0)
3360 globalfilepath[0] = '\0';
3361
3362 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003363 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003364 /* servers are only in backends */
3365 if (!(curproxy->cap & PR_CAP_BE))
3366 continue;
3367 fileopenerr = 0;
3368 filepath = NULL;
3369
3370 /* search server state file path and name */
3371 switch (curproxy->load_server_state_from_file) {
3372 /* read servers state from global file */
3373 case PR_SRV_STATE_FILE_GLOBAL:
3374 /* there was an error while generating global server state file path */
3375 if (globalfilepathlen == 0)
3376 continue;
3377 filepath = globalfilepath;
3378 fileopenerr = 1;
3379 break;
3380 /* this backend has its own file */
3381 case PR_SRV_STATE_FILE_LOCAL:
3382 localfilepathlen = 0;
3383 localfilepath[0] = '\0';
3384 len = 0;
3385 /* create the localfilepath variable */
3386 /* absolute path or no base directory provided */
3387 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3388 len = strlen(curproxy->server_state_file_name);
3389 if (len > MAXPATHLEN) {
3390 localfilepathlen = 0;
3391 goto localfileerror;
3392 }
3393 memcpy(localfilepath, curproxy->server_state_file_name, len);
3394 localfilepath[len] = '\0';
3395 localfilepathlen = len;
3396 }
3397 else if (global.server_state_base) {
3398 len = strlen(global.server_state_base);
3399 localfilepathlen += len;
3400
3401 if (localfilepathlen > MAXPATHLEN) {
3402 localfilepathlen = 0;
3403 goto localfileerror;
3404 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003405 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003406 localfilepath[localfilepathlen] = 0;
3407
3408 /* append a slash if needed */
3409 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3410 if (localfilepathlen + 1 > MAXPATHLEN) {
3411 localfilepathlen = 0;
3412 goto localfileerror;
3413 }
3414 localfilepath[localfilepathlen++] = '/';
3415 }
3416
3417 len = strlen(curproxy->server_state_file_name);
3418 if (localfilepathlen + len > MAXPATHLEN) {
3419 localfilepathlen = 0;
3420 goto localfileerror;
3421 }
3422 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3423 localfilepathlen += len;
3424 localfilepath[localfilepathlen++] = 0;
3425 }
3426 filepath = localfilepath;
3427 localfileerror:
3428 if (localfilepathlen == 0)
3429 localfilepath[0] = '\0';
3430
3431 break;
3432 case PR_SRV_STATE_FILE_NONE:
3433 default:
3434 continue;
3435 }
3436
3437 /* preload global state file */
3438 errno = 0;
3439 f = fopen(filepath, "r");
3440 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003442 if (!f)
3443 continue;
3444
3445 mybuf[0] = '\0';
3446 mybuflen = 0;
3447 version = 0;
3448
3449 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003450 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003451 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003452 goto fileclose;
3453 }
3454
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003455 cur = mybuf;
3456 version = atoi(cur);
3457 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3458 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003459 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003460
3461 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3462 int bk_f_forced_id = 0;
3463 int check_id = 0;
3464 int check_name = 0;
3465
3466 mybuflen = strlen(mybuf);
3467 cur = mybuf;
3468 end = cur + mybuflen;
3469
3470 bk = NULL;
3471 srv = NULL;
3472
3473 /* we need at least one character */
3474 if (mybuflen == 0)
3475 continue;
3476
3477 /* ignore blank characters at the beginning of the line */
3478 while (isspace(*cur))
3479 ++cur;
3480
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003481 /* Ignore empty or commented lines */
3482 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003483 continue;
3484
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003485 /* truncated lines */
3486 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003487 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003488 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003489 }
3490
3491 /* Removes trailing '\n' */
3492 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003493
3494 /* we're now ready to move the line into *srv_params[] */
3495 params[0] = cur;
3496 arg = 1;
3497 srv_arg = 0;
3498 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3499 if (isspace(*cur)) {
3500 *cur = '\0';
3501 ++cur;
3502 while (isspace(*cur))
3503 ++cur;
3504 switch (version) {
3505 case 1:
3506 /*
3507 * srv_addr: params[4] => srv_params[0]
3508 * srv_op_state: params[5] => srv_params[1]
3509 * srv_admin_state: params[6] => srv_params[2]
3510 * srv_uweight: params[7] => srv_params[3]
3511 * srv_iweight: params[8] => srv_params[4]
3512 * srv_last_time_change: params[9] => srv_params[5]
3513 * srv_check_status: params[10] => srv_params[6]
3514 * srv_check_result: params[11] => srv_params[7]
3515 * srv_check_health: params[12] => srv_params[8]
3516 * srv_check_state: params[13] => srv_params[9]
3517 * srv_agent_state: params[14] => srv_params[10]
3518 * bk_f_forced_id: params[15] => srv_params[11]
3519 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003520 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003521 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003522 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003523 */
3524 if (arg >= 4) {
3525 srv_params[srv_arg] = cur;
3526 ++srv_arg;
3527 }
3528 break;
3529 }
3530
3531 params[arg] = cur;
3532 ++arg;
3533 }
3534 else {
3535 ++cur;
3536 }
3537 }
3538
3539 /* if line is incomplete line, then ignore it.
3540 * otherwise, update useful flags */
3541 switch (version) {
3542 case 1:
3543 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3544 continue;
3545 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3546 check_id = (atoi(params[0]) == curproxy->uuid);
3547 check_name = (strcmp(curproxy->id, params[1]) == 0);
3548 break;
3549 }
3550
3551 diff = 0;
3552 bk = curproxy;
3553
3554 /* if backend can't be found, let's continue */
3555 if (!check_id && !check_name)
3556 continue;
3557 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003558 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 +02003559 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3560 }
3561 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003562 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 +02003563 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3564 /* if name doesn't match, we still want to update curproxy if the backend id
3565 * was forced in previous the previous configuration */
3566 if (!bk_f_forced_id)
3567 continue;
3568 }
3569
3570 /* look for the server by its id: param[2] */
3571 /* else look for the server by its name: param[3] */
3572 diff = 0;
3573 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3574
3575 if (!srv) {
3576 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003577 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3578 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003579 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3580 params[3], params[2], params[0], params[1]);
3581 continue;
3582 }
3583 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003584 ha_warning("In backend '%s' (id: '%d'): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003585 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003586 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003587 }
3588 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003589 ha_warning("In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003590 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003591 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003592 }
3593
3594 /* now we can proceed with server's state update */
3595 srv_update_state(srv, version, srv_params);
3596 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003597fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003598 fclose(f);
3599 }
3600}
3601
Simon Horman7d09b9a2013-02-12 10:45:51 +09003602/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003603 * update a server's current IP address.
3604 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3605 * ip is in network format.
3606 * updater is a string which contains an information about the requester of the update.
3607 * updater is used if not NULL.
3608 *
3609 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003610 *
3611 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003612 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003613int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003614{
3615 /* generates a log line and a warning on stderr */
3616 if (1) {
3617 /* book enough space for both IPv4 and IPv6 */
3618 char oldip[INET6_ADDRSTRLEN];
3619 char newip[INET6_ADDRSTRLEN];
3620
3621 memset(oldip, '\0', INET6_ADDRSTRLEN);
3622 memset(newip, '\0', INET6_ADDRSTRLEN);
3623
3624 /* copy old IP address in a string */
3625 switch (s->addr.ss_family) {
3626 case AF_INET:
3627 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3628 break;
3629 case AF_INET6:
3630 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3631 break;
3632 };
3633
3634 /* copy new IP address in a string */
3635 switch (ip_sin_family) {
3636 case AF_INET:
3637 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3638 break;
3639 case AF_INET6:
3640 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3641 break;
3642 };
3643
3644 /* save log line into a buffer */
3645 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3646 s->proxy->id, s->id, oldip, newip, updater);
3647
3648 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003649 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003650
3651 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003652 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003653 }
3654
3655 /* save the new IP family */
3656 s->addr.ss_family = ip_sin_family;
3657 /* save the new IP address */
3658 switch (ip_sin_family) {
3659 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003660 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003661 break;
3662 case AF_INET6:
3663 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3664 break;
3665 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003666 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003667
3668 return 0;
3669}
3670
3671/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003672 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3673 *
3674 * Caller can pass its name through <updater> to get it integrated in the response message
3675 * returned by the function.
3676 *
3677 * The function first does the following, in that order:
3678 * - validates the new addr and/or port
3679 * - checks if an update is required (new IP or port is different than current ones)
3680 * - checks the update is allowed:
3681 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3682 * - allow all changes if no CHECKS are configured
3683 * - if CHECK is configured:
3684 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3685 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3686 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003687 *
3688 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003689 */
3690const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3691{
3692 struct sockaddr_storage sa;
3693 int ret, port_change_required;
3694 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003695 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003696 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003697 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003698
3699 msg = get_trash_chunk();
3700 chunk_reset(msg);
3701
3702 if (addr) {
3703 memset(&sa, 0, sizeof(struct sockaddr_storage));
3704 if (str2ip2(addr, &sa, 0) == NULL) {
3705 chunk_printf(msg, "Invalid addr '%s'", addr);
3706 goto out;
3707 }
3708
3709 /* changes are allowed on AF_INET* families only */
3710 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3711 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3712 goto out;
3713 }
3714
3715 /* collecting data currently setup */
3716 memset(current_addr, '\0', sizeof(current_addr));
3717 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3718 /* changes are allowed on AF_INET* families only */
3719 if ((ret != AF_INET) && (ret != AF_INET6)) {
3720 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3721 goto out;
3722 }
3723
3724 /* applying ADDR changes if required and allowed
3725 * ipcmp returns 0 when both ADDR are the same
3726 */
3727 if (ipcmp(&s->addr, &sa) == 0) {
3728 chunk_appendf(msg, "no need to change the addr");
3729 goto port;
3730 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003731 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003732 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003733
3734 /* we also need to update check's ADDR only if it uses the server's one */
3735 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003736 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003737 }
3738
3739 /* we also need to update agent ADDR only if it use the server's one */
3740 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003741 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003742 }
3743
3744 /* update report for caller */
3745 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3746 }
3747
3748 port:
3749 if (port) {
3750 char sign = '\0';
3751 char *endptr;
3752
3753 if (addr)
3754 chunk_appendf(msg, ", ");
3755
3756 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003757 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003758
3759 /* check if PORT change is required */
3760 port_change_required = 0;
3761
3762 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003763 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003764 new_port = strtol(port, &endptr, 10);
3765 if ((errno != 0) || (port == endptr)) {
3766 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3767 goto out;
3768 }
3769
3770 /* check if caller triggers a port mapped or offset */
3771 if (sign == '-' || (sign == '+')) {
3772 /* check if server currently uses port map */
3773 if (!(s->flags & SRV_F_MAPPORTS)) {
3774 /* switch from fixed port to port map mandatorily triggers
3775 * a port change */
3776 port_change_required = 1;
3777 /* check is configured
3778 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3779 * prevent PORT change if check doesn't have it's dedicated port while switching
3780 * to port mapping */
3781 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3782 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.");
3783 goto out;
3784 }
3785 }
3786 /* we're already using port maps */
3787 else {
3788 port_change_required = current_port != new_port;
3789 }
3790 }
3791 /* fixed port */
3792 else {
3793 port_change_required = current_port != new_port;
3794 }
3795
3796 /* applying PORT changes if required and update response message */
3797 if (port_change_required) {
3798 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003799 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003800 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003801
3802 /* prepare message */
3803 chunk_appendf(msg, "port changed from '");
3804 if (s->flags & SRV_F_MAPPORTS)
3805 chunk_appendf(msg, "+");
3806 chunk_appendf(msg, "%d' to '", current_port);
3807
3808 if (sign == '-') {
3809 s->flags |= SRV_F_MAPPORTS;
3810 chunk_appendf(msg, "%c", sign);
3811 /* just use for result output */
3812 new_port = -new_port;
3813 }
3814 else if (sign == '+') {
3815 s->flags |= SRV_F_MAPPORTS;
3816 chunk_appendf(msg, "%c", sign);
3817 }
3818 else {
3819 s->flags &= ~SRV_F_MAPPORTS;
3820 }
3821
3822 chunk_appendf(msg, "%d'", new_port);
3823
3824 /* we also need to update health checks port only if it uses server's realport */
3825 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3826 s->check.port = new_port;
3827 }
3828 }
3829 else {
3830 chunk_appendf(msg, "no need to change the port");
3831 }
3832 }
3833
3834out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003835 if (changed)
3836 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003837 if (updater)
3838 chunk_appendf(msg, " by '%s'", updater);
3839 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003840 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003841}
3842
3843
3844/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003845 * update server status based on result of name resolution
3846 * returns:
3847 * 0 if server status is updated
3848 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003849 *
3850 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003851 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003852int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003853{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003854 struct dns_resolvers *resolvers = s->resolvers;
3855 struct dns_resolution *resolution = s->dns_requester->resolution;
3856 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003857
3858 switch (resolution->status) {
3859 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003860 /* status when HAProxy has just (re)started.
3861 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003862 break;
3863
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003864 case RSLV_STATUS_VALID:
3865 /*
3866 * resume health checks
3867 * server will be turned back on if health check is safe
3868 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003869 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003870 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003871 return 1;
3872 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3873 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003874 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003875 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003876
Emeric Brun52a91d32017-08-31 14:41:55 +02003877 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003878 return 1;
3879 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3880 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3881 s->proxy->id, s->id);
3882
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003883 ha_warning("%s.\n", trash.area);
3884 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003885 return 0;
3886
3887 case RSLV_STATUS_NX:
3888 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003889 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3890 if (!tick_is_expired(exp, now_ms))
3891 break;
3892
3893 if (s->next_admin & SRV_ADMF_RMAINT)
3894 return 1;
3895 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3896 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003897
3898 case RSLV_STATUS_TIMEOUT:
3899 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003900 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3901 if (!tick_is_expired(exp, now_ms))
3902 break;
3903
3904 if (s->next_admin & SRV_ADMF_RMAINT)
3905 return 1;
3906 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3907 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003908
3909 case RSLV_STATUS_REFUSED:
3910 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003911 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3912 if (!tick_is_expired(exp, now_ms))
3913 break;
3914
3915 if (s->next_admin & SRV_ADMF_RMAINT)
3916 return 1;
3917 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3918 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003919
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003920 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003921 /* stop server if resolution failed for a long enough period */
3922 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3923 if (!tick_is_expired(exp, now_ms))
3924 break;
3925
3926 if (s->next_admin & SRV_ADMF_RMAINT)
3927 return 1;
3928 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3929 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003930 }
3931
3932 return 1;
3933}
3934
3935/*
3936 * Server Name Resolution valid response callback
3937 * It expects:
3938 * - <nameserver>: the name server which answered the valid response
3939 * - <response>: buffer containing a valid DNS response
3940 * - <response_len>: size of <response>
3941 * It performs the following actions:
3942 * - ignore response if current ip found and server family not met
3943 * - update with first new ip found if family is met and current IP is not found
3944 * returns:
3945 * 0 on error
3946 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003947 *
3948 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003949 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003950int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003951{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003952 struct server *s = NULL;
3953 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003954 void *serverip, *firstip;
3955 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003956 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003957 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003958 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003959
Christopher Faulet67957bd2017-09-27 11:00:59 +02003960 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003961 if (!s)
3962 return 1;
3963
Christopher Faulet67957bd2017-09-27 11:00:59 +02003964 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003965
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003966 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003967 firstip = NULL; /* pointer to the first valid response found */
3968 /* it will be used as the new IP if a change is required */
3969 firstip_sin_family = AF_UNSPEC;
3970 serverip = NULL; /* current server IP address */
3971
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003972 /* initializing server IP pointer */
3973 server_sin_family = s->addr.ss_family;
3974 switch (server_sin_family) {
3975 case AF_INET:
3976 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3977 break;
3978
3979 case AF_INET6:
3980 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3981 break;
3982
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003983 case AF_UNSPEC:
3984 break;
3985
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003986 default:
3987 goto invalid;
3988 }
3989
Baptiste Assmann729c9012017-05-22 15:13:10 +02003990 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003991 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003992 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003993
3994 switch (ret) {
3995 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003996 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003997
3998 case DNS_UPD_SRVIP_NOT_FOUND:
3999 goto save_ip;
4000
4001 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004002 goto invalid;
4003
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004004 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004005 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004006 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004007
Baptiste Assmannfad03182015-10-28 02:03:32 +01004008 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004009 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004010 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004011 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004012
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004013 default:
4014 goto invalid;
4015
4016 }
4017
4018 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004019 if (nameserver) {
4020 nameserver->counters.update++;
4021 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004022 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004023 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004024 else
4025 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004026 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004027
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004028 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004029 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004030 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004031
4032 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004033 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004034 nameserver->counters.invalid++;
4035 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004036 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004037 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004038 return 0;
4039}
4040
4041/*
4042 * Server Name Resolution error management callback
4043 * returns:
4044 * 0 on error
4045 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004046 *
4047 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004048 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004049int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004050{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004051 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004052
Christopher Faulet67957bd2017-09-27 11:00:59 +02004053 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004054 if (!s)
4055 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004056 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004057 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004058 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004059 return 1;
4060}
4061
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004062/*
4063 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004064 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004065 * It returns a pointer to the first server found or NULL if <ip> is not yet
4066 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004067 *
4068 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004069 */
4070struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4071{
4072 struct server *tmpsrv;
4073 struct proxy *be;
4074
4075 if (!srv)
4076 return NULL;
4077
4078 be = srv->proxy;
4079 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004080 /* we found the current server is the same, ignore it */
4081 if (srv == tmpsrv)
4082 continue;
4083
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004084 /* We want to compare the IP in the record with the IP of the servers in the
4085 * same backend, only if:
4086 * * DNS resolution is enabled on the server
4087 * * the hostname used for the resolution by our server is the same than the
4088 * one used for the server found in the backend
4089 * * the server found in the backend is not our current server
4090 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004091 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004092 if ((tmpsrv->hostname_dn == NULL) ||
4093 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4094 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004095 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004096 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004097 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004098 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004099
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004100 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004101 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004102 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004103 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004104 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004105
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004106 /* At this point, we have 2 different servers using the same DNS hostname
4107 * for their respective resolution.
4108 */
4109 if (*ip_family == tmpsrv->addr.ss_family &&
4110 ((tmpsrv->addr.ss_family == AF_INET &&
4111 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4112 (tmpsrv->addr.ss_family == AF_INET6 &&
4113 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004114 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004115 return tmpsrv;
4116 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004117 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004118 }
4119
Emeric Brune9fd6b52017-11-02 17:20:39 +01004120
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004121 return NULL;
4122}
4123
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004124/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4125 * resolver. This is suited for initial address configuration. Returns 0 on
4126 * success otherwise a non-zero error code. In case of error, *err_code, if
4127 * not NULL, is filled up.
4128 */
4129int srv_set_addr_via_libc(struct server *srv, int *err_code)
4130{
4131 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004132 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004133 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004134 return 1;
4135 }
4136 return 0;
4137}
4138
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004139/* Set the server's FDQN (->hostname) from <hostname>.
4140 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004141 *
4142 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004143 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004144int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004145{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004146 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004147 char *hostname_dn;
4148 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004149
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004150 /* Note that the server lock is already held. */
4151 if (!srv->resolvers)
4152 return -1;
4153
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004154 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004155 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004156 /* run time DNS resolution was not active for this server
4157 * and we can't enable it at run time for now.
4158 */
4159 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004160 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004161
4162 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004163 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004164 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004165 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4166 hostname_dn, trash.size);
4167 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004168 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004169
Christopher Faulet67957bd2017-09-27 11:00:59 +02004170 resolution = srv->dns_requester->resolution;
4171 if (resolution &&
4172 resolution->hostname_dn &&
4173 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004174 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004175
Christopher Faulet67957bd2017-09-27 11:00:59 +02004176 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004177
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004178 free(srv->hostname);
4179 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004180 srv->hostname = strdup(hostname);
4181 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004182 srv->hostname_dn_len = hostname_dn_len;
4183 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004184 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004185
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004186 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004187 goto err;
4188
4189 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004190 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004191 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004192 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004193
4194 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004195 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004196 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004197 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004198}
4199
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004200/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4201 * from the state file. This is suited for initial address configuration.
4202 * Returns 0 on success otherwise a non-zero error code. In case of error,
4203 * *err_code, if not NULL, is filled up.
4204 */
4205static int srv_apply_lastaddr(struct server *srv, int *err_code)
4206{
4207 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4208 if (err_code)
4209 *err_code |= ERR_WARN;
4210 return 1;
4211 }
4212 return 0;
4213}
4214
Willy Tarreau25e51522016-11-04 15:10:17 +01004215/* returns 0 if no error, otherwise a combination of ERR_* flags */
4216static int srv_iterate_initaddr(struct server *srv)
4217{
4218 int return_code = 0;
4219 int err_code;
4220 unsigned int methods;
4221
4222 methods = srv->init_addr_methods;
4223 if (!methods) { // default to "last,libc"
4224 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4225 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4226 }
4227
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004228 /* "-dr" : always append "none" so that server addresses resolution
4229 * failures are silently ignored, this is convenient to validate some
4230 * configs out of their environment.
4231 */
4232 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4233 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4234
Willy Tarreau25e51522016-11-04 15:10:17 +01004235 while (methods) {
4236 err_code = 0;
4237 switch (srv_get_next_initaddr(&methods)) {
4238 case SRV_IADDR_LAST:
4239 if (!srv->lastaddr)
4240 continue;
4241 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004242 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004243 return_code |= err_code;
4244 break;
4245
4246 case SRV_IADDR_LIBC:
4247 if (!srv->hostname)
4248 continue;
4249 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004250 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004251 return_code |= err_code;
4252 break;
4253
Willy Tarreau37ebe122016-11-04 15:17:58 +01004254 case SRV_IADDR_NONE:
4255 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004256 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004257 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4258 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004259 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004260 return return_code;
4261
Willy Tarreau4310d362016-11-02 15:05:56 +01004262 case SRV_IADDR_IP:
4263 ipcpy(&srv->init_addr, &srv->addr);
4264 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004265 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4266 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004267 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004268 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004269
Willy Tarreau25e51522016-11-04 15:10:17 +01004270 default: /* unhandled method */
4271 break;
4272 }
4273 }
4274
4275 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004276 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004277 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4278 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004279 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004280 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004281 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4282 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004283
4284 return_code |= ERR_ALERT | ERR_FATAL;
4285 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004286out:
4287 srv_set_dyncookie(srv);
4288 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004289}
4290
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004291/*
4292 * This function parses all backends and all servers within each backend
4293 * and performs servers' addr resolution based on information provided by:
4294 * - configuration file
4295 * - server-state file (states provided by an 'old' haproxy process)
4296 *
4297 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4298 */
4299int srv_init_addr(void)
4300{
4301 struct proxy *curproxy;
4302 int return_code = 0;
4303
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004304 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004305 while (curproxy) {
4306 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004307
4308 /* servers are in backend only */
4309 if (!(curproxy->cap & PR_CAP_BE))
4310 goto srv_init_addr_next;
4311
Willy Tarreau25e51522016-11-04 15:10:17 +01004312 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004313 if (srv->hostname)
4314 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004315
4316 srv_init_addr_next:
4317 curproxy = curproxy->next;
4318 }
4319
4320 return return_code;
4321}
4322
Willy Tarreau46b7f532018-08-21 11:54:26 +02004323/*
4324 * Must be called with the server lock held.
4325 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004326const 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 +02004327{
4328
Willy Tarreau83061a82018-07-13 11:56:34 +02004329 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004330
4331 msg = get_trash_chunk();
4332 chunk_reset(msg);
4333
Olivier Houchard8da5f982017-08-04 18:35:36 +02004334 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004335 chunk_appendf(msg, "no need to change the FDQN");
4336 goto out;
4337 }
4338
4339 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4340 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4341 goto out;
4342 }
4343
4344 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4345 server->proxy->id, server->id, server->hostname, fqdn);
4346
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004347 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004348 chunk_reset(msg);
4349 chunk_appendf(msg, "could not update %s/%s FQDN",
4350 server->proxy->id, server->id);
4351 goto out;
4352 }
4353
4354 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004355 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004356
4357 out:
4358 if (updater)
4359 chunk_appendf(msg, " by '%s'", updater);
4360 chunk_appendf(msg, "\n");
4361
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004362 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004363}
4364
4365
Willy Tarreau21b069d2016-11-23 17:15:08 +01004366/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4367 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004368 * 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 +01004369 * used for CLI commands requiring a server name.
4370 * Important: the <arg> is modified to remove the '/'.
4371 */
4372struct server *cli_find_server(struct appctx *appctx, char *arg)
4373{
4374 struct proxy *px;
4375 struct server *sv;
4376 char *line;
4377
4378 /* split "backend/server" and make <line> point to server */
4379 for (line = arg; *line; line++)
4380 if (*line == '/') {
4381 *line++ = '\0';
4382 break;
4383 }
4384
4385 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004386 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004387 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004388 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004389 return NULL;
4390 }
4391
4392 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004393 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004394 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004395 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004396 return NULL;
4397 }
4398
4399 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004400 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004401 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004402 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004403 return NULL;
4404 }
4405
4406 return sv;
4407}
4408
William Lallemand222baf22016-11-19 02:00:33 +01004409
Willy Tarreau46b7f532018-08-21 11:54:26 +02004410/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004411static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004412{
4413 struct server *sv;
4414 const char *warning;
4415
4416 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4417 return 1;
4418
4419 sv = cli_find_server(appctx, args[2]);
4420 if (!sv)
4421 return 1;
4422
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004423 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004424
William Lallemand222baf22016-11-19 02:00:33 +01004425 if (strcmp(args[3], "weight") == 0) {
4426 warning = server_parse_weight_change_request(sv, args[4]);
4427 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004428 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004429 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004430 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004431 }
4432 }
4433 else if (strcmp(args[3], "state") == 0) {
4434 if (strcmp(args[4], "ready") == 0)
4435 srv_adm_set_ready(sv);
4436 else if (strcmp(args[4], "drain") == 0)
4437 srv_adm_set_drain(sv);
4438 else if (strcmp(args[4], "maint") == 0)
4439 srv_adm_set_maint(sv);
4440 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004441 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004442 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004443 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004444 }
4445 }
4446 else if (strcmp(args[3], "health") == 0) {
4447 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004448 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004449 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004450 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004451 }
4452 else if (strcmp(args[4], "up") == 0) {
4453 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004454 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004455 }
4456 else if (strcmp(args[4], "stopping") == 0) {
4457 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004458 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004459 }
4460 else if (strcmp(args[4], "down") == 0) {
4461 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004462 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004463 }
4464 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004465 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004466 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004467 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004468 }
4469 }
4470 else if (strcmp(args[3], "agent") == 0) {
4471 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004472 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004473 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004474 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004475 }
4476 else if (strcmp(args[4], "up") == 0) {
4477 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004478 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004479 }
4480 else if (strcmp(args[4], "down") == 0) {
4481 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004482 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004483 }
4484 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004485 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004486 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004487 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004488 }
4489 }
Misiek2da082d2017-01-09 09:40:42 +01004490 else if (strcmp(args[3], "agent-addr") == 0) {
4491 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004492 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004493 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4494 appctx->st0 = CLI_ST_PRINT;
4495 } else {
4496 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004497 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004498 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4499 appctx->st0 = CLI_ST_PRINT;
4500 }
4501 }
4502 }
4503 else if (strcmp(args[3], "agent-send") == 0) {
4504 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004505 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004506 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4507 appctx->st0 = CLI_ST_PRINT;
4508 } else {
4509 char *nss = strdup(args[4]);
4510 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004511 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004512 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4513 appctx->st0 = CLI_ST_PRINT;
4514 } else {
4515 free(sv->agent.send_string);
4516 sv->agent.send_string = nss;
4517 sv->agent.send_string_len = strlen(args[4]);
4518 }
4519 }
4520 }
William Lallemand222baf22016-11-19 02:00:33 +01004521 else if (strcmp(args[3], "check-port") == 0) {
4522 int i = 0;
4523 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004524 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004525 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004526 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004527 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004528 }
4529 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004530 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004531 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004532 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004533 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004534 }
4535 /* prevent the update of port to 0 if MAPPORTS are in use */
4536 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004537 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004538 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004539 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004540 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004541 }
4542 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004543 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004544 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004545 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004546 }
4547 else if (strcmp(args[3], "addr") == 0) {
4548 char *addr = NULL;
4549 char *port = NULL;
4550 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004551 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004552 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004553 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004554 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004555 }
4556 else {
4557 addr = args[4];
4558 }
4559 if (strcmp(args[5], "port") == 0) {
4560 port = args[6];
4561 }
4562 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4563 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004564 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004565 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004566 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004567 }
4568 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4569 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004570 else if (strcmp(args[3], "fqdn") == 0) {
4571 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004572 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004573 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4574 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004575 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004576 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004577 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004578 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004579 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004580 appctx->ctx.cli.msg = warning;
4581 appctx->st0 = CLI_ST_PRINT;
4582 }
4583 }
William Lallemand222baf22016-11-19 02:00:33 +01004584 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004585 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004586 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 +01004587 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004588 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004589 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004590 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004591 return 1;
4592}
4593
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004594static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004595{
4596 struct stream_interface *si = appctx->owner;
4597 struct proxy *px;
4598 struct server *sv;
4599 char *line;
4600
4601
4602 /* split "backend/server" and make <line> point to server */
4603 for (line = args[2]; *line; line++)
4604 if (*line == '/') {
4605 *line++ = '\0';
4606 break;
4607 }
4608
4609 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004610 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004611 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004612 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004613 return 1;
4614 }
4615
4616 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004617 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004618 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004619 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004620 return 1;
4621 }
4622
4623 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004624 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4625 sv->iweight);
4626 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004627 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004628 return 0;
4629 }
William Lallemand6b160942016-11-22 12:34:35 +01004630 return 1;
4631}
4632
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004633/* Parse a "set weight" command.
4634 *
4635 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004636 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004637static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004638{
4639 struct server *sv;
4640 const char *warning;
4641
4642 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4643 return 1;
4644
4645 sv = cli_find_server(appctx, args[2]);
4646 if (!sv)
4647 return 1;
4648
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004649 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4650
William Lallemand6b160942016-11-22 12:34:35 +01004651 warning = server_parse_weight_change_request(sv, args[3]);
4652 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004653 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004654 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004655 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004656 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004657
4658 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4659
William Lallemand6b160942016-11-22 12:34:35 +01004660 return 1;
4661}
4662
Willy Tarreau46b7f532018-08-21 11:54:26 +02004663/* parse a "set maxconn server" command. It always returns 1.
4664 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004665 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004666 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004667static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004668{
4669 struct server *sv;
4670 const char *warning;
4671
4672 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4673 return 1;
4674
4675 sv = cli_find_server(appctx, args[3]);
4676 if (!sv)
4677 return 1;
4678
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004679 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4680
Willy Tarreaub8026272016-11-23 11:26:56 +01004681 warning = server_parse_maxconn_change_request(sv, args[4]);
4682 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004683 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004684 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004685 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004686 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004687
4688 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4689
Willy Tarreaub8026272016-11-23 11:26:56 +01004690 return 1;
4691}
William Lallemand6b160942016-11-22 12:34:35 +01004692
Willy Tarreau46b7f532018-08-21 11:54:26 +02004693/* parse a "disable agent" command. It always returns 1.
4694 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004695 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004696 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004697static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004698{
4699 struct server *sv;
4700
4701 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4702 return 1;
4703
4704 sv = cli_find_server(appctx, args[2]);
4705 if (!sv)
4706 return 1;
4707
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004708 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004709 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004710 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004711 return 1;
4712}
4713
Willy Tarreau46b7f532018-08-21 11:54:26 +02004714/* parse a "disable health" command. It always returns 1.
4715 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004716 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004717 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004718static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004719{
4720 struct server *sv;
4721
4722 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4723 return 1;
4724
4725 sv = cli_find_server(appctx, args[2]);
4726 if (!sv)
4727 return 1;
4728
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004729 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004730 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004731 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004732 return 1;
4733}
4734
Willy Tarreau46b7f532018-08-21 11:54:26 +02004735/* parse a "disable server" 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_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +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 Tarreauffb4d582016-11-24 12:47:00 +01004751 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004752 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004753 return 1;
4754}
4755
Willy Tarreau46b7f532018-08-21 11:54:26 +02004756/* parse a "enable agent" 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_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +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
4771 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004772 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004773 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004774 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004775 return 1;
4776 }
4777
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004778 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004779 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004780 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004781 return 1;
4782}
4783
Willy Tarreau46b7f532018-08-21 11:54:26 +02004784/* parse a "enable health" command. It always returns 1.
4785 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004786 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004787 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004788static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004789{
4790 struct server *sv;
4791
4792 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4793 return 1;
4794
4795 sv = cli_find_server(appctx, args[2]);
4796 if (!sv)
4797 return 1;
4798
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004799 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004800 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004801 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004802 return 1;
4803}
4804
Willy Tarreau46b7f532018-08-21 11:54:26 +02004805/* parse a "enable server" command. It always returns 1.
4806 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004807 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004808 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004809static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004810{
4811 struct server *sv;
4812
4813 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4814 return 1;
4815
4816 sv = cli_find_server(appctx, args[2]);
4817 if (!sv)
4818 return 1;
4819
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004820 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004821 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004822 if (!(sv->flags & SRV_F_COOKIESET)
4823 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4824 sv->cookie)
4825 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004826 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004827 return 1;
4828}
4829
William Lallemand222baf22016-11-19 02:00:33 +01004830/* register cli keywords */
4831static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004832 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004833 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004834 { { "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 +01004835 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004836 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004837 { { "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 +01004838 { { "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 +01004839 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004840 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4841 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4842
William Lallemand222baf22016-11-19 02:00:33 +01004843 {{},}
4844}};
4845
Willy Tarreau0108d902018-11-25 19:14:37 +01004846INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004847
Emeric Brun64cc49c2017-10-03 14:46:45 +02004848/*
4849 * This function applies server's status changes, it is
4850 * is designed to be called asynchronously.
4851 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004852 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004853 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004854static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004855{
4856 struct check *check = &s->check;
4857 int xferred;
4858 struct proxy *px = s->proxy;
4859 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4860 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4861 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004862 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004863
Emeric Brun64cc49c2017-10-03 14:46:45 +02004864 /* If currently main is not set we try to apply pending state changes */
4865 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4866 int next_admin;
4867
4868 /* Backup next admin */
4869 next_admin = s->next_admin;
4870
4871 /* restore current admin state */
4872 s->next_admin = s->cur_admin;
4873
4874 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4875 s->last_change = now.tv_sec;
4876 if (s->proxy->lbprm.set_server_status_down)
4877 s->proxy->lbprm.set_server_status_down(s);
4878
4879 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4880 srv_shutdown_streams(s, SF_ERR_DOWN);
4881
4882 /* we might have streams queued on this server and waiting for
4883 * a connection. Those which are redispatchable will be queued
4884 * to another server or to the proxy itself.
4885 */
4886 xferred = pendconn_redistribute(s);
4887
4888 tmptrash = alloc_trash_chunk();
4889 if (tmptrash) {
4890 chunk_printf(tmptrash,
4891 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4892 s->proxy->id, s->id);
4893
Emeric Brun5a133512017-10-19 14:42:30 +02004894 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004895 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004896
4897 /* we don't send an alert if the server was previously paused */
4898 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004899 send_log(s->proxy, log_level, "%s.\n",
4900 tmptrash->area);
4901 send_email_alert(s, log_level, "%s",
4902 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004903 free_trash_chunk(tmptrash);
4904 tmptrash = NULL;
4905 }
4906 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4907 set_backend_down(s->proxy);
4908
4909 s->counters.down_trans++;
4910 }
4911 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4912 s->last_change = now.tv_sec;
4913 if (s->proxy->lbprm.set_server_status_down)
4914 s->proxy->lbprm.set_server_status_down(s);
4915
4916 /* we might have streams queued on this server and waiting for
4917 * a connection. Those which are redispatchable will be queued
4918 * to another server or to the proxy itself.
4919 */
4920 xferred = pendconn_redistribute(s);
4921
4922 tmptrash = alloc_trash_chunk();
4923 if (tmptrash) {
4924 chunk_printf(tmptrash,
4925 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4926 s->proxy->id, s->id);
4927
Emeric Brun5a133512017-10-19 14:42:30 +02004928 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004929
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004930 ha_warning("%s.\n", tmptrash->area);
4931 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4932 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004933 free_trash_chunk(tmptrash);
4934 tmptrash = NULL;
4935 }
4936
4937 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4938 set_backend_down(s->proxy);
4939 }
4940 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4941 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4942 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4943 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4944 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4945 s->proxy->last_change = now.tv_sec;
4946 }
4947
4948 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4949 s->down_time += now.tv_sec - s->last_change;
4950
4951 s->last_change = now.tv_sec;
4952 if (s->next_state == SRV_ST_STARTING)
4953 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4954
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004955 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004956 /* now propagate the status change to any LB algorithms */
4957 if (px->lbprm.update_server_eweight)
4958 px->lbprm.update_server_eweight(s);
4959 else if (srv_willbe_usable(s)) {
4960 if (px->lbprm.set_server_status_up)
4961 px->lbprm.set_server_status_up(s);
4962 }
4963 else {
4964 if (px->lbprm.set_server_status_down)
4965 px->lbprm.set_server_status_down(s);
4966 }
4967
4968 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4969 * and it's not a backup server and its effective weight is > 0,
4970 * then it can accept new connections, so we shut down all streams
4971 * on all backup servers.
4972 */
4973 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4974 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4975 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4976
4977 /* check if we can handle some connections queued at the proxy. We
4978 * will take as many as we can handle.
4979 */
4980 xferred = pendconn_grab_from_px(s);
4981
4982 tmptrash = alloc_trash_chunk();
4983 if (tmptrash) {
4984 chunk_printf(tmptrash,
4985 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4986 s->proxy->id, s->id);
4987
Emeric Brun5a133512017-10-19 14:42:30 +02004988 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004989 ha_warning("%s.\n", tmptrash->area);
4990 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4991 tmptrash->area);
4992 send_email_alert(s, LOG_NOTICE, "%s",
4993 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004994 free_trash_chunk(tmptrash);
4995 tmptrash = NULL;
4996 }
4997
4998 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4999 set_backend_down(s->proxy);
5000 }
5001 else if (s->cur_eweight != s->next_eweight) {
5002 /* now propagate the status change to any LB algorithms */
5003 if (px->lbprm.update_server_eweight)
5004 px->lbprm.update_server_eweight(s);
5005 else if (srv_willbe_usable(s)) {
5006 if (px->lbprm.set_server_status_up)
5007 px->lbprm.set_server_status_up(s);
5008 }
5009 else {
5010 if (px->lbprm.set_server_status_down)
5011 px->lbprm.set_server_status_down(s);
5012 }
5013
5014 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5015 set_backend_down(s->proxy);
5016 }
5017
5018 s->next_admin = next_admin;
5019 }
5020
Emeric Brun5a133512017-10-19 14:42:30 +02005021 /* reset operational state change */
5022 *s->op_st_chg.reason = 0;
5023 s->op_st_chg.status = s->op_st_chg.code = -1;
5024 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005025
5026 /* Now we try to apply pending admin changes */
5027
5028 /* Maintenance must also disable health checks */
5029 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5030 if (s->check.state & CHK_ST_ENABLED) {
5031 s->check.state |= CHK_ST_PAUSED;
5032 check->health = 0;
5033 }
5034
5035 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005036 tmptrash = alloc_trash_chunk();
5037 if (tmptrash) {
5038 chunk_printf(tmptrash,
5039 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5040 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5041 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005042
Olivier Houchard796a2b32017-10-24 17:42:47 +02005043 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005044
Olivier Houchard796a2b32017-10-24 17:42:47 +02005045 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005046 ha_warning("%s.\n", tmptrash->area);
5047 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5048 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005049 }
5050 free_trash_chunk(tmptrash);
5051 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005052 }
Emeric Brun8f298292017-12-06 16:47:17 +01005053 /* commit new admin status */
5054
5055 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005056 }
5057 else { /* server was still running */
5058 check->health = 0; /* failure */
5059 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005060
5061 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005062 if (s->proxy->lbprm.set_server_status_down)
5063 s->proxy->lbprm.set_server_status_down(s);
5064
Emeric Brun64cc49c2017-10-03 14:46:45 +02005065 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5066 srv_shutdown_streams(s, SF_ERR_DOWN);
5067
5068 /* we might have streams queued on this server and waiting for
5069 * a connection. Those which are redispatchable will be queued
5070 * to another server or to the proxy itself.
5071 */
5072 xferred = pendconn_redistribute(s);
5073
5074 tmptrash = alloc_trash_chunk();
5075 if (tmptrash) {
5076 chunk_printf(tmptrash,
5077 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5078 s->flags & SRV_F_BACKUP ? "Backup " : "",
5079 s->proxy->id, s->id,
5080 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5081
5082 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5083
5084 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005085 ha_warning("%s.\n", tmptrash->area);
5086 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5087 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005088 }
5089 free_trash_chunk(tmptrash);
5090 tmptrash = NULL;
5091 }
5092 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5093 set_backend_down(s->proxy);
5094
5095 s->counters.down_trans++;
5096 }
5097 }
5098 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5099 /* OK here we're leaving maintenance, we have many things to check,
5100 * because the server might possibly be coming back up depending on
5101 * its state. In practice, leaving maintenance means that we should
5102 * immediately turn to UP (more or less the slowstart) under the
5103 * following conditions :
5104 * - server is neither checked nor tracked
5105 * - server tracks another server which is not checked
5106 * - server tracks another server which is already up
5107 * Which sums up as something simpler :
5108 * "either the tracking server is up or the server's checks are disabled
5109 * or up". Otherwise we only re-enable health checks. There's a special
5110 * case associated to the stopping state which can be inherited. Note
5111 * that the server might still be in drain mode, which is naturally dealt
5112 * with by the lower level functions.
5113 */
5114
5115 if (s->check.state & CHK_ST_ENABLED) {
5116 s->check.state &= ~CHK_ST_PAUSED;
5117 check->health = check->rise; /* start OK but check immediately */
5118 }
5119
5120 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5121 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5122 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5123 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5124 s->next_state = SRV_ST_STOPPING;
5125 }
5126 else {
5127 s->next_state = SRV_ST_STARTING;
5128 if (s->slowstart > 0)
5129 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5130 else
5131 s->next_state = SRV_ST_RUNNING;
5132 }
5133
5134 }
5135
5136 tmptrash = alloc_trash_chunk();
5137 if (tmptrash) {
5138 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5139 chunk_printf(tmptrash,
5140 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5141 s->flags & SRV_F_BACKUP ? "Backup " : "",
5142 s->proxy->id, s->id,
5143 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5144 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5145 }
5146 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5147 chunk_printf(tmptrash,
5148 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5149 s->flags & SRV_F_BACKUP ? "Backup " : "",
5150 s->proxy->id, s->id, s->hostname,
5151 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5152 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5153 }
5154 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5155 chunk_printf(tmptrash,
5156 "%sServer %s/%s is %s/%s (leaving maintenance)",
5157 s->flags & SRV_F_BACKUP ? "Backup " : "",
5158 s->proxy->id, s->id,
5159 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5160 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5161 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005162 ha_warning("%s.\n", tmptrash->area);
5163 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5164 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005165 free_trash_chunk(tmptrash);
5166 tmptrash = NULL;
5167 }
5168
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005169 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005170 /* now propagate the status change to any LB algorithms */
5171 if (px->lbprm.update_server_eweight)
5172 px->lbprm.update_server_eweight(s);
5173 else if (srv_willbe_usable(s)) {
5174 if (px->lbprm.set_server_status_up)
5175 px->lbprm.set_server_status_up(s);
5176 }
5177 else {
5178 if (px->lbprm.set_server_status_down)
5179 px->lbprm.set_server_status_down(s);
5180 }
5181
5182 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5183 set_backend_down(s->proxy);
5184
Willy Tarreau6a78e612018-08-07 10:14:53 +02005185 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5186 * and it's not a backup server and its effective weight is > 0,
5187 * then it can accept new connections, so we shut down all streams
5188 * on all backup servers.
5189 */
5190 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5191 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5192 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5193
5194 /* check if we can handle some connections queued at the proxy. We
5195 * will take as many as we can handle.
5196 */
5197 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005198 }
5199 else if (s->next_admin & SRV_ADMF_MAINT) {
5200 /* remaining in maintenance mode, let's inform precisely about the
5201 * situation.
5202 */
5203 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5204 tmptrash = alloc_trash_chunk();
5205 if (tmptrash) {
5206 chunk_printf(tmptrash,
5207 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5208 s->flags & SRV_F_BACKUP ? "Backup " : "",
5209 s->proxy->id, s->id);
5210
5211 if (s->track) /* normally it's mandatory here */
5212 chunk_appendf(tmptrash, " via %s/%s",
5213 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005214 ha_warning("%s.\n", tmptrash->area);
5215 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5216 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005217 free_trash_chunk(tmptrash);
5218 tmptrash = NULL;
5219 }
5220 }
5221 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5222 tmptrash = alloc_trash_chunk();
5223 if (tmptrash) {
5224 chunk_printf(tmptrash,
5225 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5226 s->flags & SRV_F_BACKUP ? "Backup " : "",
5227 s->proxy->id, s->id, s->hostname);
5228
5229 if (s->track) /* normally it's mandatory here */
5230 chunk_appendf(tmptrash, " via %s/%s",
5231 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005232 ha_warning("%s.\n", tmptrash->area);
5233 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5234 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005235 free_trash_chunk(tmptrash);
5236 tmptrash = NULL;
5237 }
5238 }
5239 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5240 tmptrash = alloc_trash_chunk();
5241 if (tmptrash) {
5242 chunk_printf(tmptrash,
5243 "%sServer %s/%s remains in forced maintenance",
5244 s->flags & SRV_F_BACKUP ? "Backup " : "",
5245 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005246 ha_warning("%s.\n", tmptrash->area);
5247 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5248 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005249 free_trash_chunk(tmptrash);
5250 tmptrash = NULL;
5251 }
5252 }
5253 /* don't report anything when leaving drain mode and remaining in maintenance */
5254
5255 s->cur_admin = s->next_admin;
5256 }
5257
5258 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5259 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5260 /* drain state is applied only if not yet in maint */
5261
5262 s->last_change = now.tv_sec;
5263 if (px->lbprm.set_server_status_down)
5264 px->lbprm.set_server_status_down(s);
5265
5266 /* we might have streams queued on this server and waiting for
5267 * a connection. Those which are redispatchable will be queued
5268 * to another server or to the proxy itself.
5269 */
5270 xferred = pendconn_redistribute(s);
5271
5272 tmptrash = alloc_trash_chunk();
5273 if (tmptrash) {
5274 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5275 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5276 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5277
5278 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5279
5280 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005281 ha_warning("%s.\n", tmptrash->area);
5282 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5283 tmptrash->area);
5284 send_email_alert(s, LOG_NOTICE, "%s",
5285 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005286 }
5287 free_trash_chunk(tmptrash);
5288 tmptrash = NULL;
5289 }
5290
5291 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5292 set_backend_down(s->proxy);
5293 }
5294 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5295 /* OK completely leaving drain mode */
5296 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5297 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5298 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5299 s->proxy->last_change = now.tv_sec;
5300 }
5301
5302 if (s->last_change < now.tv_sec) // ignore negative times
5303 s->down_time += now.tv_sec - s->last_change;
5304 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005305 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005306
5307 tmptrash = alloc_trash_chunk();
5308 if (tmptrash) {
5309 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5310 chunk_printf(tmptrash,
5311 "%sServer %s/%s is %s (leaving forced drain)",
5312 s->flags & SRV_F_BACKUP ? "Backup " : "",
5313 s->proxy->id, s->id,
5314 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5315 }
5316 else {
5317 chunk_printf(tmptrash,
5318 "%sServer %s/%s is %s (leaving drain)",
5319 s->flags & SRV_F_BACKUP ? "Backup " : "",
5320 s->proxy->id, s->id,
5321 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5322 if (s->track) /* normally it's mandatory here */
5323 chunk_appendf(tmptrash, " via %s/%s",
5324 s->track->proxy->id, s->track->id);
5325 }
5326
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005327 ha_warning("%s.\n", tmptrash->area);
5328 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5329 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005330 free_trash_chunk(tmptrash);
5331 tmptrash = NULL;
5332 }
5333
5334 /* now propagate the status change to any LB algorithms */
5335 if (px->lbprm.update_server_eweight)
5336 px->lbprm.update_server_eweight(s);
5337 else if (srv_willbe_usable(s)) {
5338 if (px->lbprm.set_server_status_up)
5339 px->lbprm.set_server_status_up(s);
5340 }
5341 else {
5342 if (px->lbprm.set_server_status_down)
5343 px->lbprm.set_server_status_down(s);
5344 }
5345 }
5346 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5347 /* remaining in drain mode after removing one of its flags */
5348
5349 tmptrash = alloc_trash_chunk();
5350 if (tmptrash) {
5351 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5352 chunk_printf(tmptrash,
5353 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5354 s->flags & SRV_F_BACKUP ? "Backup " : "",
5355 s->proxy->id, s->id);
5356
5357 if (s->track) /* normally it's mandatory here */
5358 chunk_appendf(tmptrash, " via %s/%s",
5359 s->track->proxy->id, s->track->id);
5360 }
5361 else {
5362 chunk_printf(tmptrash,
5363 "%sServer %s/%s remains in forced drain mode",
5364 s->flags & SRV_F_BACKUP ? "Backup " : "",
5365 s->proxy->id, s->id);
5366 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005367 ha_warning("%s.\n", tmptrash->area);
5368 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5369 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005370 free_trash_chunk(tmptrash);
5371 tmptrash = NULL;
5372 }
5373
5374 /* commit new admin status */
5375
5376 s->cur_admin = s->next_admin;
5377 }
5378 }
5379
5380 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005381 *s->adm_st_chg_cause = 0;
5382}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005383
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005384struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5385{
5386 struct connection *conn;
5387
5388 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5389 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005390 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005391 }
5392
5393 return task;
5394}
5395
Willy Tarreau980855b2019-02-07 14:59:29 +01005396struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005397{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005398 struct server *srv;
5399 struct eb32_node *eb;
5400 int i;
5401 unsigned int next_wakeup;
5402 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005403
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005404 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5405 while (1) {
5406 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005407
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005408 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5409 if (!eb) {
5410 /* we might have reached the end of the tree, typically because
5411 * <now_ms> is in the first half and we're first scanning the last
5412 * half. Let's loop back to the beginning of the tree now.
5413 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005414
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005415 eb = eb32_first(&idle_conn_srv);
5416 if (likely(!eb))
5417 break;
5418 }
5419 if (tick_is_lt(now_ms, eb->key)) {
5420 /* timer not expired yet, revisit it later */
5421 next_wakeup = eb->key;
5422 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005423 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005424 }
5425 srv = eb32_entry(eb, struct server, idle_node);
5426 for (i = 0; i < global.nbthread; i++) {
5427 int max_conn = (srv->curr_idle_thr[i] / 2) +
5428 (srv->curr_idle_thr[i] & 1);
5429 int j;
5430 int did_remove = 0;
5431
5432 for (j = 0; j < max_conn; j++) {
5433 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5434 if (!conn)
5435 break;
5436 did_remove = 1;
5437 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5438 }
5439 if (did_remove && max_conn < srv->curr_idle_thr[i])
5440 srv_is_empty = 0;
5441 if (did_remove)
5442 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5443 }
5444 eb32_delete(&srv->idle_node);
5445 if (!srv_is_empty) {
5446 /* There are still more idle connections, add the
5447 * server back in the tree.
5448 */
5449 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5450 now_ms);
5451 eb32_insert(&idle_conn_srv, &srv->idle_node);
5452 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005453 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005454 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5455
5456 if (need_wakeup)
5457 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005458 else
5459 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005460
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005461 return task;
5462}
Olivier Houchard88698d92019-04-16 19:07:22 +02005463
5464/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5465static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5466 struct proxy *defpx, const char *file, int line,
5467 char **err)
5468{
5469 int arg = -1;
5470
5471 if (too_many_args(1, args, err, NULL))
5472 return -1;
5473
5474 if (*(args[1]) != 0)
5475 arg = atoi(args[1]);
5476
5477 if (arg < 0 || arg > 100) {
5478 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5479 return -1;
5480 }
5481
5482 if (args[0][10] == 'h')
5483 global.tune.pool_high_ratio = arg;
5484 else
5485 global.tune.pool_low_ratio = arg;
5486 return 0;
5487}
5488
5489/* config keyword parsers */
5490static struct cfg_kw_list cfg_kws = {ILH, {
5491 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5492 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5493 { 0, NULL, NULL }
5494}};
5495
5496INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5497
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005498/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005499 * Local variables:
5500 * c-indent-level: 8
5501 * c-basic-offset: 8
5502 * End:
5503 */