blob: bec498c2a44e5a3468efe2c8783376c4a2e6ba92 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Frédéric Lécaille7da71292019-05-20 09:47:07 +020028#include <types/dict.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010030#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020031#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010032#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020033
William Lallemand222baf22016-11-19 02:00:33 +010034#include <proto/applet.h>
35#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090036#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020037#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020038#include <proto/port_range.h>
39#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020040#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010041#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020042#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010044#include <proto/stream_interface.h>
45#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020047#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010048#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049
Willy Tarreau3ff577e2018-08-02 11:48:52 +020050static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020051static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010052static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010053static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau21faa912012-10-10 08:27:36 +020055/* List head of all known server keywords */
56static struct srv_kw_list srv_keywords = {
57 .list = LIST_HEAD_INIT(srv_keywords.list)
58};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059
Olivier Houchard9ea5d362019-02-14 18:29:09 +010060__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
61struct eb_root idle_conn_srv = EB_ROOT;
62struct task *idle_conn_task = NULL;
63struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
64struct list toremove_connections[MAX_THREADS];
65
Frédéric Lécaille7da71292019-05-20 09:47:07 +020066/* The server names dictionary */
67struct dict server_name_dict = {
68 .name = "server names",
69 .values = EB_ROOT_UNIQUE,
70};
71
Simon Hormana3608442013-11-01 16:46:15 +090072int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020073{
Emeric Brun52a91d32017-08-31 14:41:55 +020074 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020075 return s->down_time;
76
77 return now.tv_sec - s->last_change + s->down_time;
78}
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050080int srv_lastsession(const struct server *s)
81{
82 if (s->counters.last_sess)
83 return now.tv_sec - s->counters.last_sess;
84
85 return -1;
86}
87
Simon Horman4a741432013-02-23 15:35:38 +090088int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020089{
Simon Horman4a741432013-02-23 15:35:38 +090090 const struct server *s = check->server;
91
Willy Tarreauff5ae352013-12-11 20:36:34 +010092 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090093 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010094
Emeric Brun52a91d32017-08-31 14:41:55 +020095 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090096 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010097
Simon Horman4a741432013-02-23 15:35:38 +090098 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010099}
100
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100101/*
102 * Check that we did not get a hash collision.
103 * Unlikely, but it can happen.
104 */
105static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100106{
107 struct proxy *p = s->proxy;
108 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100109
110 for (tmpserv = p->srv; tmpserv != NULL;
111 tmpserv = tmpserv->next) {
112 if (tmpserv == s)
113 continue;
114 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
115 continue;
116 if (tmpserv->cookie &&
117 strcmp(tmpserv->cookie, s->cookie) == 0) {
118 ha_warning("We generated two equal cookies for two different servers.\n"
119 "Please change the secret key for '%s'.\n",
120 s->proxy->id);
121 }
122 }
123
124}
125
Willy Tarreau46b7f532018-08-21 11:54:26 +0200126/*
127 * Must be called with the server lock held.
128 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100129void srv_set_dyncookie(struct server *s)
130{
131 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100132 char *tmpbuf;
133 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100134 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100135 size_t buffer_len;
136 int addr_len;
137 int port;
138
139 if ((s->flags & SRV_F_COOKIESET) ||
140 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
141 s->proxy->dyncookie_key == NULL)
142 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100143 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100144
145 if (s->addr.ss_family != AF_INET &&
146 s->addr.ss_family != AF_INET6)
147 return;
148 /*
149 * Buffer to calculate the cookie value.
150 * The buffer contains the secret key + the server IP address
151 * + the TCP port.
152 */
153 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
154 /*
155 * The TCP port should use only 2 bytes, but is stored in
156 * an unsigned int in struct server, so let's use 4, to be
157 * on the safe side.
158 */
159 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200160 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100161 memcpy(tmpbuf, p->dyncookie_key, key_len);
162 memcpy(&(tmpbuf[key_len]),
163 s->addr.ss_family == AF_INET ?
164 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
165 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
166 addr_len);
167 /*
168 * Make sure it's the same across all the load balancers,
169 * no matter their endianness.
170 */
171 port = htonl(s->svc_port);
172 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
173 hash_value = XXH64(tmpbuf, buffer_len, 0);
174 memprintf(&s->cookie, "%016llx", hash_value);
175 if (!s->cookie)
176 return;
177 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100178
179 /* Don't bother checking if the dyncookie is duplicated if
180 * the server is marked as "disabled", maybe it doesn't have
181 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100182 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100183 if (!(s->next_admin & SRV_ADMF_FMAINT))
184 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100185}
186
Willy Tarreau21faa912012-10-10 08:27:36 +0200187/*
188 * Registers the server keyword list <kwl> as a list of valid keywords for next
189 * parsing sessions.
190 */
191void srv_register_keywords(struct srv_kw_list *kwl)
192{
193 LIST_ADDQ(&srv_keywords.list, &kwl->list);
194}
195
196/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
197 * keyword is found with a NULL ->parse() function, then an attempt is made to
198 * find one with a valid ->parse() function. This way it is possible to declare
199 * platform-dependant, known keywords as NULL, then only declare them as valid
200 * if some options are met. Note that if the requested keyword contains an
201 * opening parenthesis, everything from this point is ignored.
202 */
203struct srv_kw *srv_find_kw(const char *kw)
204{
205 int index;
206 const char *kwend;
207 struct srv_kw_list *kwl;
208 struct srv_kw *ret = NULL;
209
210 kwend = strchr(kw, '(');
211 if (!kwend)
212 kwend = kw + strlen(kw);
213
214 list_for_each_entry(kwl, &srv_keywords.list, list) {
215 for (index = 0; kwl->kw[index].kw != NULL; index++) {
216 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
217 kwl->kw[index].kw[kwend-kw] == 0) {
218 if (kwl->kw[index].parse)
219 return &kwl->kw[index]; /* found it !*/
220 else
221 ret = &kwl->kw[index]; /* may be OK */
222 }
223 }
224 }
225 return ret;
226}
227
228/* Dumps all registered "server" keywords to the <out> string pointer. The
229 * unsupported keywords are only dumped if their supported form was not
230 * found.
231 */
232void srv_dump_kws(char **out)
233{
234 struct srv_kw_list *kwl;
235 int index;
236
237 *out = NULL;
238 list_for_each_entry(kwl, &srv_keywords.list, list) {
239 for (index = 0; kwl->kw[index].kw != NULL; index++) {
240 if (kwl->kw[index].parse ||
241 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
242 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
243 kwl->scope,
244 kwl->kw[index].kw,
245 kwl->kw[index].skip ? " <arg>" : "",
246 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
247 kwl->kw[index].parse ? "" : " (not supported)");
248 }
249 }
250 }
251}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100252
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100253/* Parse the "addr" server keyword */
254static int srv_parse_addr(char **args, int *cur_arg,
255 struct proxy *curproxy, struct server *newsrv, char **err)
256{
257 char *errmsg, *arg;
258 struct sockaddr_storage *sk;
259 int port1, port2;
260 struct protocol *proto;
261
262 errmsg = NULL;
263 arg = args[*cur_arg + 1];
264
265 if (!*arg) {
266 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
267 goto err;
268 }
269
270 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
271 if (!sk) {
272 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
273 goto err;
274 }
275
276 proto = protocol_by_family(sk->ss_family);
277 if (!proto || !proto->connect) {
278 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
279 args[*cur_arg], arg);
280 goto err;
281 }
282
283 if (port1 != port2) {
284 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
285 args[*cur_arg], arg);
286 goto err;
287 }
288
289 newsrv->check.addr = newsrv->agent.addr = *sk;
290 newsrv->flags |= SRV_F_CHECKADDR;
291 newsrv->flags |= SRV_F_AGENTADDR;
292
293 return 0;
294
295 err:
296 free(errmsg);
297 return ERR_ALERT | ERR_FATAL;
298}
299
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100300/* Parse the "agent-check" server keyword */
301static int srv_parse_agent_check(char **args, int *cur_arg,
302 struct proxy *curproxy, struct server *newsrv, char **err)
303{
304 newsrv->do_agent = 1;
305 return 0;
306}
307
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100308/* Parse the "backup" server keyword */
309static int srv_parse_backup(char **args, int *cur_arg,
310 struct proxy *curproxy, struct server *newsrv, char **err)
311{
312 newsrv->flags |= SRV_F_BACKUP;
313 return 0;
314}
315
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100316/* Parse the "check" server keyword */
317static int srv_parse_check(char **args, int *cur_arg,
318 struct proxy *curproxy, struct server *newsrv, char **err)
319{
320 newsrv->do_check = 1;
321 return 0;
322}
323
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100324/* Parse the "check-send-proxy" server keyword */
325static int srv_parse_check_send_proxy(char **args, int *cur_arg,
326 struct proxy *curproxy, struct server *newsrv, char **err)
327{
328 newsrv->check.send_proxy = 1;
329 return 0;
330}
331
Alexander Liu2a54bb72019-05-22 19:44:48 +0800332/* Parse the "check-via-socks4" server keyword */
333static int srv_parse_check_via_socks4(char **args, int *cur_arg,
334 struct proxy *curproxy, struct server *newsrv, char **err)
335{
336 newsrv->check.via_socks4 = 1;
337 return 0;
338}
339
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100340/* Parse the "cookie" server keyword */
341static int srv_parse_cookie(char **args, int *cur_arg,
342 struct proxy *curproxy, struct server *newsrv, char **err)
343{
344 char *arg;
345
346 arg = args[*cur_arg + 1];
347 if (!*arg) {
348 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
349 return ERR_ALERT | ERR_FATAL;
350 }
351
352 free(newsrv->cookie);
353 newsrv->cookie = strdup(arg);
354 newsrv->cklen = strlen(arg);
355 newsrv->flags |= SRV_F_COOKIESET;
356 return 0;
357}
358
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100359/* Parse the "disabled" server keyword */
360static int srv_parse_disabled(char **args, int *cur_arg,
361 struct proxy *curproxy, struct server *newsrv, char **err)
362{
Emeric Brun52a91d32017-08-31 14:41:55 +0200363 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
364 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100365 newsrv->check.state |= CHK_ST_PAUSED;
366 newsrv->check.health = 0;
367 return 0;
368}
369
370/* Parse the "enabled" server keyword */
371static int srv_parse_enabled(char **args, int *cur_arg,
372 struct proxy *curproxy, struct server *newsrv, char **err)
373{
Emeric Brun52a91d32017-08-31 14:41:55 +0200374 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
375 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100376 newsrv->check.state &= ~CHK_ST_PAUSED;
377 newsrv->check.health = newsrv->check.rise;
378 return 0;
379}
380
Willy Tarreau9c538e02019-01-23 10:21:49 +0100381static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
382{
383 char *arg;
384
385 arg = args[*cur_arg + 1];
386 if (!*arg) {
387 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
388 return ERR_ALERT | ERR_FATAL;
389 }
390 newsrv->max_reuse = atoi(arg);
391
392 return 0;
393}
394
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100395static 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 +0100396{
397 const char *res;
398 char *arg;
399 unsigned int time;
400
401 arg = args[*cur_arg + 1];
402 if (!*arg) {
403 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
404 return ERR_ALERT | ERR_FATAL;
405 }
406 res = parse_time_err(arg, &time, TIME_UNIT_MS);
407 if (res) {
408 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
409 *res, args[*cur_arg]);
410 return ERR_ALERT | ERR_FATAL;
411 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100412 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100413
414 return 0;
415}
416
Olivier Houchard006e3102018-12-10 18:30:32 +0100417static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
418{
419 char *arg;
420
421 arg = args[*cur_arg + 1];
422 if (!*arg) {
423 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
424 return ERR_ALERT | ERR_FATAL;
425 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100426
Olivier Houchard006e3102018-12-10 18:30:32 +0100427 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100428 if ((int)newsrv->max_idle_conns < -1) {
429 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
430 return ERR_ALERT | ERR_FATAL;
431 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100432
433 return 0;
434}
435
Willy Tarreaudff55432012-10-10 17:51:05 +0200436/* parse the "id" server keyword */
437static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
438{
439 struct eb32_node *node;
440
441 if (!*args[*cur_arg + 1]) {
442 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
443 return ERR_ALERT | ERR_FATAL;
444 }
445
446 newsrv->puid = atol(args[*cur_arg + 1]);
447 newsrv->conf.id.key = newsrv->puid;
448
449 if (newsrv->puid <= 0) {
450 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
451 return ERR_ALERT | ERR_FATAL;
452 }
453
454 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
455 if (node) {
456 struct server *target = container_of(node, struct server, conf.id);
457 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
458 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
459 target->id);
460 return ERR_ALERT | ERR_FATAL;
461 }
462
463 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200464 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200465 return 0;
466}
467
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100468/* Parse the "namespace" server keyword */
469static int srv_parse_namespace(char **args, int *cur_arg,
470 struct proxy *curproxy, struct server *newsrv, char **err)
471{
Willy Tarreaue5733232019-05-22 19:24:06 +0200472#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100473 char *arg;
474
475 arg = args[*cur_arg + 1];
476 if (!*arg) {
477 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
478 return ERR_ALERT | ERR_FATAL;
479 }
480
481 if (!strcmp(arg, "*")) {
482 /* Use the namespace associated with the connection (if present). */
483 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
484 return 0;
485 }
486
487 /*
488 * As this parser may be called several times for the same 'default-server'
489 * object, or for a new 'server' instance deriving from a 'default-server'
490 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
491 */
492 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
493
494 newsrv->netns = netns_store_lookup(arg, strlen(arg));
495 if (!newsrv->netns)
496 newsrv->netns = netns_store_insert(arg);
497
498 if (!newsrv->netns) {
499 memprintf(err, "Cannot open namespace '%s'", arg);
500 return ERR_ALERT | ERR_FATAL;
501 }
502
503 return 0;
504#else
505 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
506 return ERR_ALERT | ERR_FATAL;
507#endif
508}
509
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100510/* Parse the "no-agent-check" server keyword */
511static int srv_parse_no_agent_check(char **args, int *cur_arg,
512 struct proxy *curproxy, struct server *newsrv, char **err)
513{
514 free_check(&newsrv->agent);
515 newsrv->agent.inter = 0;
516 newsrv->agent.port = 0;
517 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
518 newsrv->do_agent = 0;
519 return 0;
520}
521
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100522/* Parse the "no-backup" server keyword */
523static int srv_parse_no_backup(char **args, int *cur_arg,
524 struct proxy *curproxy, struct server *newsrv, char **err)
525{
526 newsrv->flags &= ~SRV_F_BACKUP;
527 return 0;
528}
529
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100530/* Parse the "no-check" server keyword */
531static int srv_parse_no_check(char **args, int *cur_arg,
532 struct proxy *curproxy, struct server *newsrv, char **err)
533{
534 free_check(&newsrv->check);
535 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
536 newsrv->do_check = 0;
537 return 0;
538}
539
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100540/* Parse the "no-check-send-proxy" server keyword */
541static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
542 struct proxy *curproxy, struct server *newsrv, char **err)
543{
544 newsrv->check.send_proxy = 0;
545 return 0;
546}
547
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100548/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200549static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100550{
551 srv->pp_opts &= ~flags;
552 return 0;
553}
554
555/* Parse the "no-send-proxy" server keyword */
556static int srv_parse_no_send_proxy(char **args, int *cur_arg,
557 struct proxy *curproxy, struct server *newsrv, char **err)
558{
559 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
560}
561
562/* Parse the "no-send-proxy-v2" server keyword */
563static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
564 struct proxy *curproxy, struct server *newsrv, char **err)
565{
566 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
567}
568
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100569/* Parse the "non-stick" server keyword */
570static int srv_parse_non_stick(char **args, int *cur_arg,
571 struct proxy *curproxy, struct server *newsrv, char **err)
572{
573 newsrv->flags |= SRV_F_NON_STICK;
574 return 0;
575}
576
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100577/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200578static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100579{
580 srv->pp_opts |= flags;
581 return 0;
582}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200583/* parse the "proto" server keyword */
584static int srv_parse_proto(char **args, int *cur_arg,
585 struct proxy *px, struct server *newsrv, char **err)
586{
587 struct ist proto;
588
589 if (!*args[*cur_arg + 1]) {
590 memprintf(err, "'%s' : missing value", args[*cur_arg]);
591 return ERR_ALERT | ERR_FATAL;
592 }
593 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
594 newsrv->mux_proto = get_mux_proto(proto);
595 if (!newsrv->mux_proto) {
596 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
597 return ERR_ALERT | ERR_FATAL;
598 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200599 return 0;
600}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100601
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100602/* parse the "proxy-v2-options" */
603static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
604 struct proxy *px, struct server *newsrv, char **err)
605{
606 char *p, *n;
607 for (p = args[*cur_arg+1]; p; p = n) {
608 n = strchr(p, ',');
609 if (n)
610 *n++ = '\0';
611 if (!strcmp(p, "ssl")) {
612 newsrv->pp_opts |= SRV_PP_V2_SSL;
613 } else if (!strcmp(p, "cert-cn")) {
614 newsrv->pp_opts |= SRV_PP_V2_SSL;
615 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100616 } else if (!strcmp(p, "cert-key")) {
617 newsrv->pp_opts |= SRV_PP_V2_SSL;
618 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
619 } else if (!strcmp(p, "cert-sig")) {
620 newsrv->pp_opts |= SRV_PP_V2_SSL;
621 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
622 } else if (!strcmp(p, "ssl-cipher")) {
623 newsrv->pp_opts |= SRV_PP_V2_SSL;
624 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100625 } else if (!strcmp(p, "authority")) {
626 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100627 } else if (!strcmp(p, "crc32c")) {
628 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100629 } else
630 goto fail;
631 }
632 return 0;
633 fail:
634 if (err)
635 memprintf(err, "'%s' : proxy v2 option not implemented", p);
636 return ERR_ALERT | ERR_FATAL;
637}
638
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100639/* Parse the "observe" server keyword */
640static int srv_parse_observe(char **args, int *cur_arg,
641 struct proxy *curproxy, struct server *newsrv, char **err)
642{
643 char *arg;
644
645 arg = args[*cur_arg + 1];
646 if (!*arg) {
647 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
648 return ERR_ALERT | ERR_FATAL;
649 }
650
651 if (!strcmp(arg, "none")) {
652 newsrv->observe = HANA_OBS_NONE;
653 }
654 else if (!strcmp(arg, "layer4")) {
655 newsrv->observe = HANA_OBS_LAYER4;
656 }
657 else if (!strcmp(arg, "layer7")) {
658 if (curproxy->mode != PR_MODE_HTTP) {
659 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
660 return ERR_ALERT;
661 }
662 newsrv->observe = HANA_OBS_LAYER7;
663 }
664 else {
665 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
666 "but got '%s'\n", args[*cur_arg], arg);
667 return ERR_ALERT | ERR_FATAL;
668 }
669
670 return 0;
671}
672
Frédéric Lécaille16186232017-03-14 16:42:49 +0100673/* Parse the "redir" server keyword */
674static int srv_parse_redir(char **args, int *cur_arg,
675 struct proxy *curproxy, struct server *newsrv, char **err)
676{
677 char *arg;
678
679 arg = args[*cur_arg + 1];
680 if (!*arg) {
681 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
682 return ERR_ALERT | ERR_FATAL;
683 }
684
685 free(newsrv->rdr_pfx);
686 newsrv->rdr_pfx = strdup(arg);
687 newsrv->rdr_len = strlen(arg);
688
689 return 0;
690}
691
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100692/* Parse the "send-proxy" server keyword */
693static int srv_parse_send_proxy(char **args, int *cur_arg,
694 struct proxy *curproxy, struct server *newsrv, char **err)
695{
696 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
697}
698
699/* Parse the "send-proxy-v2" server keyword */
700static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
701 struct proxy *curproxy, struct server *newsrv, char **err)
702{
703 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
704}
705
Frédéric Lécailledba97072017-03-17 15:33:50 +0100706
707/* Parse the "source" server keyword */
708static int srv_parse_source(char **args, int *cur_arg,
709 struct proxy *curproxy, struct server *newsrv, char **err)
710{
711 char *errmsg;
712 int port_low, port_high;
713 struct sockaddr_storage *sk;
714 struct protocol *proto;
715
716 errmsg = NULL;
717
718 if (!*args[*cur_arg + 1]) {
719 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
720 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
721 goto err;
722 }
723
724 /* 'sk' is statically allocated (no need to be freed). */
725 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
726 if (!sk) {
727 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
728 goto err;
729 }
730
731 proto = protocol_by_family(sk->ss_family);
732 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100733 ha_alert("'%s %s' : connect() not supported for this address family.\n",
734 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100735 goto err;
736 }
737
738 newsrv->conn_src.opts |= CO_SRC_BIND;
739 newsrv->conn_src.source_addr = *sk;
740
741 if (port_low != port_high) {
742 int i;
743
744 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100745 ha_alert("'%s' does not support port offsets (found '%s').\n",
746 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100747 goto err;
748 }
749
750 if (port_low <= 0 || port_low > 65535 ||
751 port_high <= 0 || port_high > 65535 ||
752 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100753 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 +0100754 goto err;
755 }
756 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
757 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
758 newsrv->conn_src.sport_range->ports[i] = port_low + i;
759 }
760
761 *cur_arg += 2;
762 while (*(args[*cur_arg])) {
763 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
764#if defined(CONFIG_HAP_TRANSPARENT)
765 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100766 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
767 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100768 goto err;
769 }
770 if (!strcmp(args[*cur_arg + 1], "client")) {
771 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
772 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
773 }
774 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
775 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
776 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
777 }
778 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
779 char *name, *end;
780
781 name = args[*cur_arg + 1] + 7;
782 while (isspace(*name))
783 name++;
784
785 end = name;
786 while (*end && !isspace(*end) && *end != ',' && *end != ')')
787 end++;
788
789 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
790 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
791 free(newsrv->conn_src.bind_hdr_name);
792 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
793 newsrv->conn_src.bind_hdr_len = end - name;
794 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
795 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
796 newsrv->conn_src.bind_hdr_occ = -1;
797
798 /* now look for an occurrence number */
799 while (isspace(*end))
800 end++;
801 if (*end == ',') {
802 end++;
803 name = end;
804 if (*end == '-')
805 end++;
806 while (isdigit((int)*end))
807 end++;
808 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
809 }
810
811 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100812 ha_alert("usesrc hdr_ip(name,num) does not support negative"
813 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100814 goto err;
815 }
816 }
817 else {
818 struct sockaddr_storage *sk;
819 int port1, port2;
820
821 /* 'sk' is statically allocated (no need to be freed). */
822 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
823 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100824 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100825 goto err;
826 }
827
828 proto = protocol_by_family(sk->ss_family);
829 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100830 ha_alert("'%s %s' : connect() not supported for this address family.\n",
831 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100832 goto err;
833 }
834
835 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100836 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
837 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100838 goto err;
839 }
840 newsrv->conn_src.tproxy_addr = *sk;
841 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
842 }
843 global.last_checks |= LSTCHK_NETADM;
844 *cur_arg += 2;
845 continue;
846#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100847 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 +0100848 goto err;
849#endif /* defined(CONFIG_HAP_TRANSPARENT) */
850 } /* "usesrc" */
851
852 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
853#ifdef SO_BINDTODEVICE
854 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100855 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100856 goto err;
857 }
858 free(newsrv->conn_src.iface_name);
859 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
860 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
861 global.last_checks |= LSTCHK_NETADM;
862#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100863 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100864 goto err;
865#endif
866 *cur_arg += 2;
867 continue;
868 }
869 /* this keyword in not an option of "source" */
870 break;
871 } /* while */
872
873 return 0;
874
875 err:
876 free(errmsg);
877 return ERR_ALERT | ERR_FATAL;
878}
879
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100880/* Parse the "stick" server keyword */
881static int srv_parse_stick(char **args, int *cur_arg,
882 struct proxy *curproxy, struct server *newsrv, char **err)
883{
884 newsrv->flags &= ~SRV_F_NON_STICK;
885 return 0;
886}
887
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100888/* Parse the "track" server keyword */
889static int srv_parse_track(char **args, int *cur_arg,
890 struct proxy *curproxy, struct server *newsrv, char **err)
891{
892 char *arg;
893
894 arg = args[*cur_arg + 1];
895 if (!*arg) {
896 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
897 return ERR_ALERT | ERR_FATAL;
898 }
899
900 free(newsrv->trackit);
901 newsrv->trackit = strdup(arg);
902
903 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800904}
905
906/* Parse the "socks4" server keyword */
907static int srv_parse_socks4(char **args, int *cur_arg,
908 struct proxy *curproxy, struct server *newsrv, char **err)
909{
910 char *errmsg;
911 int port_low, port_high;
912 struct sockaddr_storage *sk;
913 struct protocol *proto;
914
915 errmsg = NULL;
916
917 if (!*args[*cur_arg + 1]) {
918 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
919 goto err;
920 }
921
922 /* 'sk' is statically allocated (no need to be freed). */
923 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
924 if (!sk) {
925 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
926 goto err;
927 }
928
929 proto = protocol_by_family(sk->ss_family);
930 if (!proto || !proto->connect) {
931 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
932 goto err;
933 }
934
935 newsrv->flags |= SRV_F_SOCKS4_PROXY;
936 newsrv->socks4_addr = *sk;
937
938 if (port_low != port_high) {
939 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
940 goto err;
941 }
942
943 if (!port_low) {
944 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
945 goto err;
946 }
947
948 return 0;
949
950 err:
951 free(errmsg);
952 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100953}
954
Frédéric Lécailledba97072017-03-17 15:33:50 +0100955
Willy Tarreau034c88c2017-01-23 23:36:45 +0100956/* parse the "tfo" server keyword */
957static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
958{
959 newsrv->flags |= SRV_F_FASTOPEN;
960 return 0;
961}
962
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200963/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200964 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200965 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200966 *
967 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200968 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200969void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200970{
Willy Tarreau87b09662015-04-03 00:22:06 +0200971 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200972
Willy Tarreau87b09662015-04-03 00:22:06 +0200973 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
974 if (stream->srv_conn == srv)
975 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200976}
977
978/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200979 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200980 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200981 *
982 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200983 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200984void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200985{
986 struct server *srv;
987
988 for (srv = px->srv; srv != NULL; srv = srv->next)
989 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200990 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200991}
992
Willy Tarreaubda92272014-05-20 21:55:30 +0200993/* Appends some information to a message string related to a server going UP or
994 * DOWN. If both <forced> and <reason> are null and the server tracks another
995 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200996 * If <check> is non-null, an entire string describing the check result will be
997 * appended after a comma and a space (eg: to report some information from the
998 * check that changed the state). In the other case, the string will be built
999 * using the check results stored into the struct server if present.
1000 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001001 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001002 *
1003 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001004 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001005void srv_append_status(struct buffer *msg, struct server *s,
1006 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001007{
Emeric Brun5a133512017-10-19 14:42:30 +02001008 short status = s->op_st_chg.status;
1009 short code = s->op_st_chg.code;
1010 long duration = s->op_st_chg.duration;
1011 char *desc = s->op_st_chg.reason;
1012
1013 if (check) {
1014 status = check->status;
1015 code = check->code;
1016 duration = check->duration;
1017 desc = check->desc;
1018 }
1019
1020 if (status != -1) {
1021 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1022
1023 if (status >= HCHK_STATUS_L57DATA)
1024 chunk_appendf(msg, ", code: %d", code);
1025
1026 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001027 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001028
1029 chunk_appendf(msg, ", info: \"");
1030
1031 chunk_initlen(&src, desc, 0, strlen(desc));
1032 chunk_asciiencode(msg, &src, '"');
1033
1034 chunk_appendf(msg, "\"");
1035 }
1036
1037 if (duration >= 0)
1038 chunk_appendf(msg, ", check duration: %ldms", duration);
1039 }
1040 else if (desc && *desc) {
1041 chunk_appendf(msg, ", %s", desc);
1042 }
1043 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001044 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001045 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001046
1047 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001048 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001049 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1050 " %d sessions active, %d requeued, %d remaining in queue",
1051 s->proxy->srv_act, s->proxy->srv_bck,
1052 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1053 s->cur_sess, xferred, s->nbpend);
1054 else
1055 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1056 " %d sessions requeued, %d total in queue",
1057 s->proxy->srv_act, s->proxy->srv_bck,
1058 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1059 xferred, s->nbpend);
1060 }
1061}
1062
Emeric Brun5a133512017-10-19 14:42:30 +02001063/* Marks server <s> down, regardless of its checks' statuses. The server is
1064 * registered in a list to postpone the counting of the remaining servers on
1065 * the proxy and transfers queued streams whenever possible to other servers at
1066 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1067 * non-null as the reason for going down or the available data from the check
1068 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001069 *
1070 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001071 */
Emeric Brun5a133512017-10-19 14:42:30 +02001072void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001073{
1074 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001075
Emeric Brun64cc49c2017-10-03 14:46:45 +02001076 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001077 return;
1078
Emeric Brun52a91d32017-08-31 14:41:55 +02001079 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001080 *s->op_st_chg.reason = 0;
1081 s->op_st_chg.status = -1;
1082 if (reason) {
1083 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1084 }
1085 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001086 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001087 s->op_st_chg.code = check->code;
1088 s->op_st_chg.status = check->status;
1089 s->op_st_chg.duration = check->duration;
1090 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001091
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001092 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001093 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001094
Emeric Brun9f0b4582017-10-23 14:39:51 +02001095 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001096 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001097 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001098 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001099 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001100}
1101
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001102/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001103 * in maintenance. The server is registered in a list to postpone the counting
1104 * of the remaining servers on the proxy and tries to grab requests from the
1105 * proxy at a sync point. Maintenance servers are ignored. It stores the
1106 * <reason> if non-null as the reason for going down or the available data
1107 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001108 *
1109 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001110 */
Emeric Brun5a133512017-10-19 14:42:30 +02001111void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001112{
1113 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001114
Emeric Brun64cc49c2017-10-03 14:46:45 +02001115 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001116 return;
1117
Emeric Brun52a91d32017-08-31 14:41:55 +02001118 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001119 return;
1120
Emeric Brun52a91d32017-08-31 14:41:55 +02001121 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001122 *s->op_st_chg.reason = 0;
1123 s->op_st_chg.status = -1;
1124 if (reason) {
1125 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1126 }
1127 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001128 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001129 s->op_st_chg.code = check->code;
1130 s->op_st_chg.status = check->status;
1131 s->op_st_chg.duration = check->duration;
1132 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001133
Emeric Brun64cc49c2017-10-03 14:46:45 +02001134 if (s->slowstart <= 0)
1135 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001136
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001137 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001138 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001139
Emeric Brun9f0b4582017-10-23 14:39:51 +02001140 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001141 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001142 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001143 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001144 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001145}
1146
Willy Tarreau8eb77842014-05-21 13:54:57 +02001147/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001148 * isn't in maintenance. The server is registered in a list to postpone the
1149 * counting of the remaining servers on the proxy and tries to grab requests
1150 * from the proxy. Maintenance servers are ignored. It stores the
1151 * <reason> if non-null as the reason for going down or the available data
1152 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001153 * up. Note that it makes use of the trash to build the log strings, so <reason>
1154 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001155 *
1156 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001157 */
Emeric Brun5a133512017-10-19 14:42:30 +02001158void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001159{
1160 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001161
Emeric Brun64cc49c2017-10-03 14:46:45 +02001162 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001163 return;
1164
Emeric Brun52a91d32017-08-31 14:41:55 +02001165 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001166 return;
1167
Emeric Brun52a91d32017-08-31 14:41:55 +02001168 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001169 *s->op_st_chg.reason = 0;
1170 s->op_st_chg.status = -1;
1171 if (reason) {
1172 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1173 }
1174 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001175 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001176 s->op_st_chg.code = check->code;
1177 s->op_st_chg.status = check->status;
1178 s->op_st_chg.duration = check->duration;
1179 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001180
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001181 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001182 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001183
Emeric Brun9f0b4582017-10-23 14:39:51 +02001184 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001185 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001186 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001187 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001188 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001189}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001190
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001191/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1192 * enforce either maint mode or drain mode. It is not allowed to set more than
1193 * one flag at once. The equivalent "inherited" flag is propagated to all
1194 * tracking servers. Maintenance mode disables health checks (but not agent
1195 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001196 * is done. If <cause> is non-null, it will be displayed at the end of the log
1197 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001198 *
1199 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001200 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001201void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001202{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001203 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001204
1205 if (!mode)
1206 return;
1207
1208 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001209 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001210 return;
1211
Emeric Brun52a91d32017-08-31 14:41:55 +02001212 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001213 if (cause)
1214 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1215
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001216 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001217 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001218
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001219 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001220 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1221 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001222 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001223
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001224 /* compute the inherited flag to propagate */
1225 if (mode & SRV_ADMF_MAINT)
1226 mode = SRV_ADMF_IMAINT;
1227 else if (mode & SRV_ADMF_DRAIN)
1228 mode = SRV_ADMF_IDRAIN;
1229
Emeric Brun9f0b4582017-10-23 14:39:51 +02001230 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001231 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001232 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001233 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001234 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001235}
1236
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001237/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1238 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1239 * than one flag at once. The equivalent "inherited" flag is propagated to all
1240 * tracking servers. Leaving maintenance mode re-enables health checks. When
1241 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001242 *
1243 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001244 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001245void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001246{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001247 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001248
1249 if (!mode)
1250 return;
1251
1252 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001253 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001254 return;
1255
Emeric Brun52a91d32017-08-31 14:41:55 +02001256 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001257
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001258 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001259 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001260
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001261 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001262 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1263 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001264 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001265
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001266 if (mode & SRV_ADMF_MAINT)
1267 mode = SRV_ADMF_IMAINT;
1268 else if (mode & SRV_ADMF_DRAIN)
1269 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001270
Emeric Brun9f0b4582017-10-23 14:39:51 +02001271 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001272 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001273 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001274 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001275 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001276}
1277
Willy Tarreau757478e2016-11-03 19:22:19 +01001278/* principle: propagate maint and drain to tracking servers. This is useful
1279 * upon startup so that inherited states are correct.
1280 */
1281static void srv_propagate_admin_state(struct server *srv)
1282{
1283 struct server *srv2;
1284
1285 if (!srv->trackers)
1286 return;
1287
1288 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001289 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001290 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001291 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001292
Emeric Brun52a91d32017-08-31 14:41:55 +02001293 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001294 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001295 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001296 }
1297}
1298
1299/* Compute and propagate the admin states for all servers in proxy <px>.
1300 * Only servers *not* tracking another one are considered, because other
1301 * ones will be handled when the server they track is visited.
1302 */
1303void srv_compute_all_admin_states(struct proxy *px)
1304{
1305 struct server *srv;
1306
1307 for (srv = px->srv; srv; srv = srv->next) {
1308 if (srv->track)
1309 continue;
1310 srv_propagate_admin_state(srv);
1311 }
1312}
1313
Willy Tarreaudff55432012-10-10 17:51:05 +02001314/* Note: must not be declared <const> as its list will be overwritten.
1315 * Please take care of keeping this list alphabetically sorted, doing so helps
1316 * all code contributors.
1317 * Optional keywords are also declared with a NULL ->parse() function so that
1318 * the config parser can report an appropriate error when a known keyword was
1319 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001320 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001321 */
1322static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001323 { "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 +01001324 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001325 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001326 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001327 { "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 +01001328 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001329 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1330 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001331 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001332 { "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 +01001333 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001334 { "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 +01001335 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001336 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001337 { "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 +01001338 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1339 { "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 +01001340 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001341 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001342 { "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 +01001343 { "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 +02001344 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001345 { "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 +01001346 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001347 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1348 { "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 +02001349 { "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 +01001350 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Willy Tarreau034c88c2017-01-23 23:36:45 +01001351 { "tfo", srv_parse_tfo, 0, 0 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001352 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001353 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1354 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001355 { NULL, NULL, 0 },
1356}};
1357
Willy Tarreau0108d902018-11-25 19:14:37 +01001358INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001359
Willy Tarreau004e0452013-11-21 11:22:01 +01001360/* Recomputes the server's eweight based on its state, uweight, the current time,
1361 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001362 * state is automatically disabled if the time is elapsed. If <must_update> is
1363 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001364 *
1365 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001366 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001367void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001368{
1369 struct proxy *px = sv->proxy;
1370 unsigned w;
1371
1372 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1373 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001374 if (sv->next_state == SRV_ST_STARTING)
1375 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001376 }
1377
1378 /* We must take care of not pushing the server to full throttle during slow starts.
1379 * It must also start immediately, at least at the minimal step when leaving maintenance.
1380 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001381 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001382 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1383 else
1384 w = px->lbprm.wdiv;
1385
Emeric Brun52a91d32017-08-31 14:41:55 +02001386 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001387
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001388 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001389 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001390 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001391}
1392
Willy Tarreaubaaee002006-06-26 02:48:02 +02001393/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001394 * Parses weight_str and configures sv accordingly.
1395 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001396 *
1397 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001398 */
1399const char *server_parse_weight_change_request(struct server *sv,
1400 const char *weight_str)
1401{
1402 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001403 long int w;
1404 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001405
1406 px = sv->proxy;
1407
1408 /* if the weight is terminated with '%', it is set relative to
1409 * the initial weight, otherwise it is absolute.
1410 */
1411 if (!*weight_str)
1412 return "Require <weight> or <weight%>.\n";
1413
Simon Hormanb796afa2013-02-12 10:45:53 +09001414 w = strtol(weight_str, &end, 10);
1415 if (end == weight_str)
1416 return "Empty weight string empty or preceded by garbage";
1417 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001418 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001419 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001420 /* Avoid integer overflow */
1421 if (w > 25600)
1422 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001423 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001424 if (w > 256)
1425 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001426 }
1427 else if (w < 0 || w > 256)
1428 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001429 else if (end[0] != '\0')
1430 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001431
1432 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1433 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1434
1435 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001436 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001437
1438 return NULL;
1439}
1440
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001441/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001442 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1443 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001444 * Returns:
1445 * - error string on error
1446 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001447 *
1448 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001449 */
1450const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001451 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001452{
1453 unsigned char ip[INET6_ADDRSTRLEN];
1454
1455 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001456 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001457 return NULL;
1458 }
1459 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001460 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001461 return NULL;
1462 }
1463
1464 return "Could not understand IP address format.\n";
1465}
1466
Willy Tarreau46b7f532018-08-21 11:54:26 +02001467/*
1468 * Must be called with the server lock held.
1469 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001470const char *server_parse_maxconn_change_request(struct server *sv,
1471 const char *maxconn_str)
1472{
1473 long int v;
1474 char *end;
1475
1476 if (!*maxconn_str)
1477 return "Require <maxconn>.\n";
1478
1479 v = strtol(maxconn_str, &end, 10);
1480 if (end == maxconn_str)
1481 return "maxconn string empty or preceded by garbage";
1482 else if (end[0] != '\0')
1483 return "Trailing garbage in maxconn string";
1484
1485 if (sv->maxconn == sv->minconn) { // static maxconn
1486 sv->maxconn = sv->minconn = v;
1487 } else { // dynamic maxconn
1488 sv->maxconn = v;
1489 }
1490
1491 if (may_dequeue_tasks(sv, sv->proxy))
1492 process_srv_queue(sv);
1493
1494 return NULL;
1495}
1496
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001497#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001498static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1499 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001500{
1501 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001502 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001503 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001504 NULL,
1505 };
1506
1507 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001508 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001509
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001510 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1511}
1512
1513static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1514{
1515 struct sample_expr *expr;
1516
1517 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 +01001518 if (!expr) {
1519 memprintf(err, "error detected while parsing sni expression : %s", *err);
1520 return ERR_ALERT | ERR_FATAL;
1521 }
1522
1523 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1524 memprintf(err, "error detected while parsing sni expression : "
1525 " fetch method '%s' extracts information from '%s', "
1526 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001527 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001528 return ERR_ALERT | ERR_FATAL;
1529 }
1530
1531 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1532 release_sample_expr(newsrv->ssl_ctx.sni);
1533 newsrv->ssl_ctx.sni = expr;
1534
1535 return 0;
1536}
1537#endif
1538
1539static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1540{
1541 if (err && *err) {
1542 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001543 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 +01001544 }
1545 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001546 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1547 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001548}
1549
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001550static void srv_conn_src_sport_range_cpy(struct server *srv,
1551 struct server *src)
1552{
1553 int range_sz;
1554
1555 range_sz = src->conn_src.sport_range->size;
1556 if (range_sz > 0) {
1557 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1558 if (srv->conn_src.sport_range != NULL) {
1559 int i;
1560
1561 for (i = 0; i < range_sz; i++) {
1562 srv->conn_src.sport_range->ports[i] =
1563 src->conn_src.sport_range->ports[i];
1564 }
1565 }
1566 }
1567}
1568
1569/*
1570 * Copy <src> server connection source settings to <srv> server everything needed.
1571 */
1572static void srv_conn_src_cpy(struct server *srv, struct server *src)
1573{
1574 srv->conn_src.opts = src->conn_src.opts;
1575 srv->conn_src.source_addr = src->conn_src.source_addr;
1576
1577 /* Source port range copy. */
1578 if (src->conn_src.sport_range != NULL)
1579 srv_conn_src_sport_range_cpy(srv, src);
1580
1581#ifdef CONFIG_HAP_TRANSPARENT
1582 if (src->conn_src.bind_hdr_name != NULL) {
1583 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1584 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1585 }
1586 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1587 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1588#endif
1589 if (src->conn_src.iface_name != NULL)
1590 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1591}
1592
1593/*
1594 * Copy <src> server SSL settings to <srv> server allocating
1595 * everything needed.
1596 */
1597#if defined(USE_OPENSSL)
1598static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1599{
1600 if (src->ssl_ctx.ca_file != NULL)
1601 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1602 if (src->ssl_ctx.crl_file != NULL)
1603 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1604 if (src->ssl_ctx.client_crt != NULL)
1605 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1606
1607 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1608
1609 if (src->ssl_ctx.verify_host != NULL)
1610 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1611 if (src->ssl_ctx.ciphers != NULL)
1612 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001613#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001614 if (src->ssl_ctx.ciphersuites != NULL)
1615 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1616#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001617 if (src->sni_expr != NULL)
1618 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001619
1620#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1621 if (src->ssl_ctx.alpn_str) {
1622 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1623 if (srv->ssl_ctx.alpn_str) {
1624 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1625 src->ssl_ctx.alpn_len);
1626 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1627 }
1628 }
1629#endif
1630#ifdef OPENSSL_NPN_NEGOTIATED
1631 if (src->ssl_ctx.npn_str) {
1632 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1633 if (srv->ssl_ctx.npn_str) {
1634 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1635 src->ssl_ctx.npn_len);
1636 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1637 }
1638 }
1639#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001640}
1641#endif
1642
1643/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001644 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001645 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001646 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001647 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001648static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001649{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001650 char *hostname_dn;
1651 int hostname_len, hostname_dn_len;
1652
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001653 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001654 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001655
Christopher Faulet67957bd2017-09-27 11:00:59 +02001656 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001657 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001658 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1659 hostname_dn, trash.size);
1660 if (hostname_dn_len == -1)
1661 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001662
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001663
Christopher Faulet67957bd2017-09-27 11:00:59 +02001664 free(srv->hostname);
1665 free(srv->hostname_dn);
1666 srv->hostname = strdup(hostname);
1667 srv->hostname_dn = strdup(hostname_dn);
1668 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001669 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001670 goto err;
1671
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001672 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001673
1674 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001675 free(srv->hostname); srv->hostname = NULL;
1676 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001677 return -1;
1678}
1679
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001680/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001681 * Copy <src> server settings to <srv> server allocating
1682 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001683 * This function is not supposed to be called at any time, but only
1684 * during server settings parsing or during server allocations from
1685 * a server template, and just after having calloc()'ed a new server.
1686 * So, <src> may only be a default server (when parsing server settings)
1687 * or a server template (during server allocations from a server template).
1688 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1689 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001690 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001691static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001692{
1693 /* Connection source settings copy */
1694 srv_conn_src_cpy(srv, src);
1695
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001696 if (srv_tmpl) {
1697 srv->addr = src->addr;
1698 srv->svc_port = src->svc_port;
1699 }
1700
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001701 srv->pp_opts = src->pp_opts;
1702 if (src->rdr_pfx != NULL) {
1703 srv->rdr_pfx = strdup(src->rdr_pfx);
1704 srv->rdr_len = src->rdr_len;
1705 }
1706 if (src->cookie != NULL) {
1707 srv->cookie = strdup(src->cookie);
1708 srv->cklen = src->cklen;
1709 }
1710 srv->use_ssl = src->use_ssl;
1711 srv->check.addr = srv->agent.addr = src->check.addr;
1712 srv->check.use_ssl = src->check.use_ssl;
1713 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001714 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001715 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001716 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001717 /* Note: 'flags' field has potentially been already initialized. */
1718 srv->flags |= src->flags;
1719 srv->do_check = src->do_check;
1720 srv->do_agent = src->do_agent;
1721 if (srv->check.port)
1722 srv->flags |= SRV_F_CHECKPORT;
1723 srv->check.inter = src->check.inter;
1724 srv->check.fastinter = src->check.fastinter;
1725 srv->check.downinter = src->check.downinter;
1726 srv->agent.use_ssl = src->agent.use_ssl;
1727 srv->agent.port = src->agent.port;
1728 if (src->agent.send_string != NULL)
1729 srv->agent.send_string = strdup(src->agent.send_string);
1730 srv->agent.send_string_len = src->agent.send_string_len;
1731 srv->agent.inter = src->agent.inter;
1732 srv->agent.fastinter = src->agent.fastinter;
1733 srv->agent.downinter = src->agent.downinter;
1734 srv->maxqueue = src->maxqueue;
1735 srv->minconn = src->minconn;
1736 srv->maxconn = src->maxconn;
1737 srv->slowstart = src->slowstart;
1738 srv->observe = src->observe;
1739 srv->onerror = src->onerror;
1740 srv->onmarkeddown = src->onmarkeddown;
1741 srv->onmarkedup = src->onmarkedup;
1742 if (src->trackit != NULL)
1743 srv->trackit = strdup(src->trackit);
1744 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1745 srv->uweight = srv->iweight = src->iweight;
1746
1747 srv->check.send_proxy = src->check.send_proxy;
1748 /* health: up, but will fall down at first failure */
1749 srv->check.rise = srv->check.health = src->check.rise;
1750 srv->check.fall = src->check.fall;
1751
1752 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001753 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1754 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1755 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001756 srv->check.state |= CHK_ST_PAUSED;
1757 srv->check.health = 0;
1758 }
1759
1760 /* health: up but will fall down at first failure */
1761 srv->agent.rise = srv->agent.health = src->agent.rise;
1762 srv->agent.fall = src->agent.fall;
1763
1764 if (src->resolvers_id != NULL)
1765 srv->resolvers_id = strdup(src->resolvers_id);
1766 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001767 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001768 if (srv->dns_opts.family_prio == AF_UNSPEC)
1769 srv->dns_opts.family_prio = AF_INET6;
1770 memcpy(srv->dns_opts.pref_net,
1771 src->dns_opts.pref_net,
1772 sizeof srv->dns_opts.pref_net);
1773 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1774
1775 srv->init_addr_methods = src->init_addr_methods;
1776 srv->init_addr = src->init_addr;
1777#if defined(USE_OPENSSL)
1778 srv_ssl_settings_cpy(srv, src);
1779#endif
1780#ifdef TCP_USER_TIMEOUT
1781 srv->tcp_ut = src->tcp_ut;
1782#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001783 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001784 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001785 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001786 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001787
Olivier Houchard8da5f982017-08-04 18:35:36 +02001788 if (srv_tmpl)
1789 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001790
1791 srv->check.via_socks4 = src->check.via_socks4;
1792 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001793}
1794
William Lallemand313bfd12018-10-26 14:47:32 +02001795struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001796{
1797 struct server *srv;
1798
1799 srv = calloc(1, sizeof *srv);
1800 if (!srv)
1801 return NULL;
1802
1803 srv->obj_type = OBJ_TYPE_SERVER;
1804 srv->proxy = proxy;
1805 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001806 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001807
Emeric Brun52a91d32017-08-31 14:41:55 +02001808 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001809 srv->last_change = now.tv_sec;
1810
1811 srv->check.status = HCHK_STATUS_INI;
1812 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001813 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001814 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1815
1816 srv->agent.status = HCHK_STATUS_INI;
1817 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001818 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001819 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1820
Willy Tarreaufb553652019-06-04 14:06:31 +02001821 srv->pool_purge_delay = 5000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001822 srv->max_idle_conns = -1;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001823 srv->max_reuse = -1;
Olivier Houchard006e3102018-12-10 18:30:32 +01001824
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001825 return srv;
1826}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001827
1828/*
1829 * Validate <srv> server health-check settings.
1830 * Returns 0 if everything is OK, -1 if not.
1831 */
1832static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1833{
1834 struct tcpcheck_rule *r = NULL;
1835 struct list *l;
1836
1837 /*
1838 * We need at least a service port, a check port or the first tcp-check rule must
1839 * be a 'connect' one when checking an IPv4/IPv6 server.
1840 */
1841 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1842 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1843 return 0;
1844
1845 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1846 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001847 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1848 "Check has been disabled.\n",
1849 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001850 return -1;
1851 }
1852
1853 /* search the first action (connect / send / expect) in the list */
1854 l = &srv->proxy->tcpcheck_rules;
1855 list_for_each_entry(r, l, list) {
1856 if (r->action != TCPCHK_ACT_COMMENT)
1857 break;
1858 }
1859
1860 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001861 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1862 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1863 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001864 return -1;
1865 }
1866
1867 /* scan the tcp-check ruleset to ensure a port has been configured */
1868 l = &srv->proxy->tcpcheck_rules;
1869 list_for_each_entry(r, l, list) {
1870 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001871 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1872 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1873 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001874 return -1;
1875 }
1876 }
1877
1878 return 0;
1879}
1880
1881/*
1882 * Initialize <srv> health-check structure.
1883 * Returns the error string in case of memory allocation failure, NULL if not.
1884 */
1885static const char *do_health_check_init(struct server *srv, int check_type, int state)
1886{
1887 const char *ret;
1888
1889 if (!srv->do_check)
1890 return NULL;
1891
1892 ret = init_check(&srv->check, check_type);
1893 if (ret)
1894 return ret;
1895
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001896 srv->check.state |= state;
1897 global.maxsock++;
1898
1899 return NULL;
1900}
1901
1902static int server_health_check_init(const char *file, int linenum,
1903 struct server *srv, struct proxy *curproxy)
1904{
1905 const char *ret;
1906
1907 if (!srv->do_check)
1908 return 0;
1909
1910 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001911 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1912 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001913 return ERR_ALERT | ERR_FATAL;
1914 }
1915
1916 if (server_healthcheck_validate(file, linenum, srv) < 0)
1917 return ERR_ALERT | ERR_ABORT;
1918
1919 /* note: check type will be set during the config review phase */
1920 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1921 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001922 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001923 return ERR_ALERT | ERR_ABORT;
1924 }
1925
1926 return 0;
1927}
1928
1929/*
1930 * Initialize <srv> agent check structure.
1931 * Returns the error string in case of memory allocation failure, NULL if not.
1932 */
1933static const char *do_server_agent_check_init(struct server *srv, int state)
1934{
1935 const char *ret;
1936
1937 if (!srv->do_agent)
1938 return NULL;
1939
1940 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1941 if (ret)
1942 return ret;
1943
1944 if (!srv->agent.inter)
1945 srv->agent.inter = srv->check.inter;
1946
1947 srv->agent.state |= state;
1948 global.maxsock++;
1949
1950 return NULL;
1951}
1952
1953static int server_agent_check_init(const char *file, int linenum,
1954 struct server *srv, struct proxy *curproxy)
1955{
1956 const char *ret;
1957
1958 if (!srv->do_agent)
1959 return 0;
1960
1961 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001962 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 +02001963 file, linenum, srv->id);
1964 return ERR_ALERT | ERR_FATAL;
1965 }
1966
1967 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1968 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001969 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001970 return ERR_ALERT | ERR_ABORT;
1971 }
1972
1973 return 0;
1974}
1975
1976#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1977static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1978 struct server *srv, struct proxy *proxy)
1979{
1980 int ret;
1981 char *err = NULL;
1982
1983 if (!srv->sni_expr)
1984 return 0;
1985
1986 ret = server_parse_sni_expr(srv, proxy, &err);
1987 if (!ret)
1988 return 0;
1989
1990 display_parser_err(file, linenum, args, cur_arg, &err);
1991 free(err);
1992
1993 return ret;
1994}
1995#endif
1996
1997/*
1998 * Server initializations finalization.
1999 * Initialize health check, agent check and SNI expression if enabled.
2000 * Must not be called for a default server instance.
2001 */
2002static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2003 struct server *srv, struct proxy *px)
2004{
2005 int ret;
2006
2007 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2008 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2009 return ret;
2010 }
2011
2012#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2013 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2014 return ret;
2015#endif
2016
2017 if (srv->flags & SRV_F_BACKUP)
2018 px->srv_bck++;
2019 else
2020 px->srv_act++;
2021 srv_lb_commit_status(srv);
2022
2023 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002024err:
2025 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002026}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002027
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002028/*
2029 * Parse as much as possible such a range string argument: low[-high]
2030 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2031 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2032 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2033 * Returns 0 if succeeded, -1 if not.
2034 */
2035static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2036{
2037 char *nb_high_arg;
2038
2039 *nb_high = 0;
2040 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002041 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002042
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002043 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002044 *nb_high_arg++ = '\0';
2045 *nb_high = atoi(nb_high_arg);
2046 }
2047 else {
2048 *nb_high += *nb_low;
2049 *nb_low = 1;
2050 }
2051
2052 if (*nb_low < 0 || *nb_high < *nb_low)
2053 return -1;
2054
2055 return 0;
2056}
2057
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002058static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2059{
2060 chunk_printf(&trash, "%s%d", prefix, nb);
2061 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002062 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002063}
2064
2065/*
2066 * Initialize as much as possible servers from <srv> server template.
2067 * Note that a server template is a special server with
2068 * a few different parameters than a server which has
2069 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002070 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002071 * 'srv' template included.
2072 */
2073static int server_template_init(struct server *srv, struct proxy *px)
2074{
2075 int i;
2076 struct server *newsrv;
2077
2078 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2079 int check_init_state;
2080 int agent_init_state;
2081
2082 newsrv = new_server(px);
2083 if (!newsrv)
2084 goto err;
2085
2086 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002087 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002088#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2089 if (newsrv->sni_expr) {
2090 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2091 if (!newsrv->ssl_ctx.sni)
2092 goto err;
2093 }
2094#endif
2095 /* Set this new server ID. */
2096 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2097
2098 /* Initial checks states. */
2099 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2100 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2101
2102 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2103 do_server_agent_check_init(newsrv, agent_init_state))
2104 goto err;
2105
2106 /* Linked backwards first. This will be restablished after parsing. */
2107 newsrv->next = px->srv;
2108 px->srv = newsrv;
2109 }
2110 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2111
2112 return i - srv->tmpl_info.nb_low;
2113
2114 err:
2115 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2116 if (newsrv) {
2117#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2118 release_sample_expr(newsrv->ssl_ctx.sni);
2119#endif
2120 free_check(&newsrv->agent);
2121 free_check(&newsrv->check);
2122 }
2123 free(newsrv);
2124 return i - srv->tmpl_info.nb_low;
2125}
2126
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002127int 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 +02002128{
2129 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002130 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002131 char *errmsg = NULL;
2132 int err_code = 0;
2133 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002134 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002135
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002136 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002137 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002138 !strcmp(args[0], "default-server") ||
2139 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002140 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002141 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002142 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002143 int srv_tmpl = !defsrv && !srv;
2144 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002145
2146 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002147 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002148 err_code |= ERR_ALERT | ERR_FATAL;
2149 goto out;
2150 }
2151 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002152 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002153
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002154 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002155 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002156 if (!*args[2]) {
2157 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002158 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002159 file, linenum, args[0]);
2160 err_code |= ERR_ALERT | ERR_FATAL;
2161 goto out;
2162 }
2163
2164 err = invalid_char(args[1]);
2165 }
2166 else if (srv_tmpl) {
2167 if (!*args[3]) {
2168 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002169 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 +02002170 file, linenum, args[0]);
2171 err_code |= ERR_ALERT | ERR_FATAL;
2172 goto out;
2173 }
2174
2175 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002176 }
2177
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002178 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002179 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 +02002180 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002181 err_code |= ERR_ALERT | ERR_FATAL;
2182 goto out;
2183 }
2184
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002185 cur_arg = 2;
2186 if (srv_tmpl) {
2187 /* Parse server-template <nb | range> arg. */
2188 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002189 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002190 file, linenum, args[0], args[cur_arg]);
2191 err_code |= ERR_ALERT | ERR_FATAL;
2192 goto out;
2193 }
2194 cur_arg++;
2195 }
2196
Willy Tarreau272adea2014-03-31 10:39:59 +02002197 if (!defsrv) {
2198 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002199 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002200 struct protocol *proto;
2201
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002202 newsrv = new_server(curproxy);
2203 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002204 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002205 err_code |= ERR_ALERT | ERR_ABORT;
2206 goto out;
2207 }
2208
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002209 if (srv_tmpl) {
2210 newsrv->tmpl_info.nb_low = tmpl_range_low;
2211 newsrv->tmpl_info.nb_high = tmpl_range_high;
2212 }
2213
Willy Tarreau272adea2014-03-31 10:39:59 +02002214 /* the servers are linked backwards first */
2215 newsrv->next = curproxy->srv;
2216 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002217 newsrv->conf.file = strdup(file);
2218 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002219 /* Note: for a server template, its id is its prefix.
2220 * This is a temporary id which will be used for server allocations to come
2221 * after parsing.
2222 */
2223 if (srv)
2224 newsrv->id = strdup(args[1]);
2225 else
2226 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002227
2228 /* several ways to check the port component :
2229 * - IP => port=+0, relative (IPv4 only)
2230 * - IP: => port=+0, relative
2231 * - IP:N => port=N, absolute
2232 * - IP:+N => port=+N, relative
2233 * - IP:-N => port=-N, relative
2234 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002235 if (!parse_addr)
2236 goto skip_addr;
2237
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002238 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002239 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002240 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002241 err_code |= ERR_ALERT | ERR_FATAL;
2242 goto out;
2243 }
2244
2245 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002246 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002247 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002248 file, linenum, args[0], args[1]);
2249 err_code |= ERR_ALERT | ERR_FATAL;
2250 goto out;
2251 }
2252
2253 if (!port1 || !port2) {
2254 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002255 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002256 }
2257 else if (port1 != port2) {
2258 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002259 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002260 file, linenum, args[0], args[1], args[2]);
2261 err_code |= ERR_ALERT | ERR_FATAL;
2262 goto out;
2263 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002264
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002265 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002266 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002267 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002268 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002269 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2270 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2271 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002272 err_code |= ERR_ALERT | ERR_FATAL;
2273 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002274 }
2275 }
2276 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002277 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002278 file, linenum, newsrv->id);
2279 err_code |= ERR_ALERT | ERR_FATAL;
2280 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002281 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002282 }
2283
Willy Tarreau272adea2014-03-31 10:39:59 +02002284 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002285 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002286
Olivier Houchard8da5f982017-08-04 18:35:36 +02002287 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002288 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002289 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002290 err_code |= ERR_ALERT | ERR_FATAL;
2291 goto out;
2292 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002293
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002294 cur_arg++;
2295 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002296 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002297 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002298 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002299 } else {
2300 newsrv = &curproxy->defsrv;
2301 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002302 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002303 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002304 }
2305
2306 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002307 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002308 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2309 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002310 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002311 file, linenum, *err, newsrv->id);
2312 err_code |= ERR_ALERT | ERR_FATAL;
2313 goto out;
2314 }
2315 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002316 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002317 file, linenum, val, args[cur_arg], newsrv->id);
2318 err_code |= ERR_ALERT | ERR_FATAL;
2319 goto out;
2320 }
2321 newsrv->agent.inter = val;
2322 cur_arg += 2;
2323 }
Misiekea849332017-01-09 09:39:51 +01002324 else if (!strcmp(args[cur_arg], "agent-addr")) {
2325 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002326 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002327 goto out;
2328 }
2329
2330 cur_arg += 2;
2331 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002332 else if (!strcmp(args[cur_arg], "agent-port")) {
2333 global.maxsock++;
2334 newsrv->agent.port = atol(args[cur_arg + 1]);
2335 cur_arg += 2;
2336 }
James Brown55f9ff12015-10-21 18:19:05 -07002337 else if (!strcmp(args[cur_arg], "agent-send")) {
2338 global.maxsock++;
2339 free(newsrv->agent.send_string);
2340 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2341 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2342 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2343 cur_arg += 2;
2344 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002345 else if (!strcmp(args[cur_arg], "init-addr")) {
2346 char *p, *end;
2347 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002348 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002349
2350 newsrv->init_addr_methods = 0;
2351 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2352
2353 for (p = args[cur_arg + 1]; *p; p = end) {
2354 /* cut on next comma */
2355 for (end = p; *end && *end != ','; end++);
2356 if (*end)
2357 *(end++) = 0;
2358
Willy Tarreau4310d362016-11-02 15:05:56 +01002359 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002360 if (!strcmp(p, "libc")) {
2361 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2362 }
2363 else if (!strcmp(p, "last")) {
2364 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2365 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002366 else if (!strcmp(p, "none")) {
2367 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2368 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002369 else if (str2ip2(p, &sa, 0)) {
2370 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002371 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002372 file, linenum, args[cur_arg], p);
2373 err_code |= ERR_ALERT | ERR_FATAL;
2374 goto out;
2375 }
2376 newsrv->init_addr = sa;
2377 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2378 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002379 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002380 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 +02002381 file, linenum, args[cur_arg], p);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002386 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002387 file, linenum, args[cur_arg], p);
2388 err_code |= ERR_ALERT | ERR_FATAL;
2389 goto out;
2390 }
2391 }
2392 cur_arg += 2;
2393 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002394 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002395 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002396 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2397 cur_arg += 2;
2398 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002399 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2400 char *p, *end;
2401
2402 for (p = args[cur_arg + 1]; *p; p = end) {
2403 /* cut on next comma */
2404 for (end = p; *end && *end != ','; end++);
2405 if (*end)
2406 *(end++) = 0;
2407
2408 if (!strcmp(p, "allow-dup-ip")) {
2409 newsrv->dns_opts.accept_duplicate_ip = 1;
2410 }
2411 else if (!strcmp(p, "prevent-dup-ip")) {
2412 newsrv->dns_opts.accept_duplicate_ip = 0;
2413 }
2414 else {
2415 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2416 file, linenum, args[cur_arg], p);
2417 err_code |= ERR_ALERT | ERR_FATAL;
2418 goto out;
2419 }
2420 }
2421
2422 cur_arg += 2;
2423 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002424 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2425 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002426 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002427 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002428 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002429 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002430 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002431 file, linenum, args[cur_arg]);
2432 err_code |= ERR_ALERT | ERR_FATAL;
2433 goto out;
2434 }
2435 cur_arg += 2;
2436 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002437 else if (!strcmp(args[cur_arg], "resolve-net")) {
2438 char *p, *e;
2439 unsigned char mask;
2440 struct dns_options *opt;
2441
2442 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002443 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002444 file, linenum, args[cur_arg]);
2445 err_code |= ERR_ALERT | ERR_FATAL;
2446 goto out;
2447 }
2448
2449 opt = &newsrv->dns_opts;
2450
2451 /* Split arguments by comma, and convert it from ipv4 or ipv6
2452 * string network in in_addr or in6_addr.
2453 */
2454 p = args[cur_arg + 1];
2455 e = p;
2456 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002457 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002458 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002459 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002460 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2461 err_code |= ERR_ALERT | ERR_FATAL;
2462 goto out;
2463 }
2464 /* look for end or comma. */
2465 while (*e != ',' && *e != '\0')
2466 e++;
2467 if (*e == ',') {
2468 *e = '\0';
2469 e++;
2470 }
2471 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2472 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2473 /* Try to convert input string from ipv4 or ipv6 network. */
2474 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2475 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2476 &mask)) {
2477 /* Try to convert input string from ipv6 network. */
2478 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2479 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2480 } else {
2481 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002482 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002483 file, linenum, args[cur_arg], p);
2484 err_code |= ERR_ALERT | ERR_FATAL;
2485 goto out;
2486 }
2487 opt->pref_net_nb++;
2488 p = e;
2489 }
2490
2491 cur_arg += 2;
2492 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002493 else if (!strcmp(args[cur_arg], "rise")) {
2494 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002495 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002496 file, linenum, args[cur_arg]);
2497 err_code |= ERR_ALERT | ERR_FATAL;
2498 goto out;
2499 }
2500
2501 newsrv->check.rise = atol(args[cur_arg + 1]);
2502 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002503 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002504 file, linenum, args[cur_arg]);
2505 err_code |= ERR_ALERT | ERR_FATAL;
2506 goto out;
2507 }
2508
2509 if (newsrv->check.health)
2510 newsrv->check.health = newsrv->check.rise;
2511 cur_arg += 2;
2512 }
2513 else if (!strcmp(args[cur_arg], "fall")) {
2514 newsrv->check.fall = atol(args[cur_arg + 1]);
2515
2516 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002517 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\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 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002524 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002525 file, linenum, args[cur_arg]);
2526 err_code |= ERR_ALERT | ERR_FATAL;
2527 goto out;
2528 }
2529
2530 cur_arg += 2;
2531 }
2532 else if (!strcmp(args[cur_arg], "inter")) {
2533 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2534 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002535 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 file, linenum, *err, newsrv->id);
2537 err_code |= ERR_ALERT | ERR_FATAL;
2538 goto out;
2539 }
2540 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002541 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002542 file, linenum, val, args[cur_arg], newsrv->id);
2543 err_code |= ERR_ALERT | ERR_FATAL;
2544 goto out;
2545 }
2546 newsrv->check.inter = val;
2547 cur_arg += 2;
2548 }
2549 else if (!strcmp(args[cur_arg], "fastinter")) {
2550 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2551 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002552 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002553 file, linenum, *err, newsrv->id);
2554 err_code |= ERR_ALERT | ERR_FATAL;
2555 goto out;
2556 }
2557 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002558 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002559 file, linenum, val, args[cur_arg], newsrv->id);
2560 err_code |= ERR_ALERT | ERR_FATAL;
2561 goto out;
2562 }
2563 newsrv->check.fastinter = val;
2564 cur_arg += 2;
2565 }
2566 else if (!strcmp(args[cur_arg], "downinter")) {
2567 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2568 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002569 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002570 file, linenum, *err, newsrv->id);
2571 err_code |= ERR_ALERT | ERR_FATAL;
2572 goto out;
2573 }
2574 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002575 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002576 file, linenum, val, args[cur_arg], newsrv->id);
2577 err_code |= ERR_ALERT | ERR_FATAL;
2578 goto out;
2579 }
2580 newsrv->check.downinter = val;
2581 cur_arg += 2;
2582 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002583 else if (!strcmp(args[cur_arg], "port")) {
2584 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002585 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002586 cur_arg += 2;
2587 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002588 else if (!strcmp(args[cur_arg], "weight")) {
2589 int w;
2590 w = atol(args[cur_arg + 1]);
2591 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002592 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002593 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2594 err_code |= ERR_ALERT | ERR_FATAL;
2595 goto out;
2596 }
2597 newsrv->uweight = newsrv->iweight = w;
2598 cur_arg += 2;
2599 }
2600 else if (!strcmp(args[cur_arg], "minconn")) {
2601 newsrv->minconn = atol(args[cur_arg + 1]);
2602 cur_arg += 2;
2603 }
2604 else if (!strcmp(args[cur_arg], "maxconn")) {
2605 newsrv->maxconn = atol(args[cur_arg + 1]);
2606 cur_arg += 2;
2607 }
2608 else if (!strcmp(args[cur_arg], "maxqueue")) {
2609 newsrv->maxqueue = atol(args[cur_arg + 1]);
2610 cur_arg += 2;
2611 }
2612 else if (!strcmp(args[cur_arg], "slowstart")) {
2613 /* slowstart is stored in seconds */
2614 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2615 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002616 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002617 file, linenum, *err, newsrv->id);
2618 err_code |= ERR_ALERT | ERR_FATAL;
2619 goto out;
2620 }
2621 newsrv->slowstart = (val + 999) / 1000;
2622 cur_arg += 2;
2623 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002624 else if (!strcmp(args[cur_arg], "on-error")) {
2625 if (!strcmp(args[cur_arg + 1], "fastinter"))
2626 newsrv->onerror = HANA_ONERR_FASTINTER;
2627 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2628 newsrv->onerror = HANA_ONERR_FAILCHK;
2629 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2630 newsrv->onerror = HANA_ONERR_SUDDTH;
2631 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2632 newsrv->onerror = HANA_ONERR_MARKDWN;
2633 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002634 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002635 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2636 file, linenum, args[cur_arg], args[cur_arg + 1]);
2637 err_code |= ERR_ALERT | ERR_FATAL;
2638 goto out;
2639 }
2640
2641 cur_arg += 2;
2642 }
2643 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2644 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2645 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2646 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002647 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002648 file, linenum, args[cur_arg], args[cur_arg + 1]);
2649 err_code |= ERR_ALERT | ERR_FATAL;
2650 goto out;
2651 }
2652
2653 cur_arg += 2;
2654 }
2655 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2656 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2657 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2658 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002659 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002660 file, linenum, args[cur_arg], args[cur_arg + 1]);
2661 err_code |= ERR_ALERT | ERR_FATAL;
2662 goto out;
2663 }
2664
2665 cur_arg += 2;
2666 }
2667 else if (!strcmp(args[cur_arg], "error-limit")) {
2668 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002669 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002670 file, linenum, args[cur_arg]);
2671 err_code |= ERR_ALERT | ERR_FATAL;
2672 goto out;
2673 }
2674
2675 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2676
2677 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002678 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002679 file, linenum, args[cur_arg]);
2680 err_code |= ERR_ALERT | ERR_FATAL;
2681 goto out;
2682 }
2683 cur_arg += 2;
2684 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002685 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002686 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002687 file, linenum, "usesrc", "source");
2688 err_code |= ERR_ALERT | ERR_FATAL;
2689 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002690 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002691 else {
2692 static int srv_dumped;
2693 struct srv_kw *kw;
2694 char *err;
2695
2696 kw = srv_find_kw(args[cur_arg]);
2697 if (kw) {
2698 char *err = NULL;
2699 int code;
2700
2701 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002702 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 +02002703 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002704 if (kw->skip != -1)
2705 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002706 err_code |= ERR_ALERT | ERR_FATAL;
2707 goto out;
2708 }
2709
2710 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002711 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002712 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002713 if (kw->skip != -1)
2714 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002715 err_code |= ERR_ALERT;
2716 continue;
2717 }
2718
2719 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2720 err_code |= code;
2721
2722 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002723 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002724 if (code & ERR_FATAL) {
2725 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002726 if (kw->skip != -1)
2727 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002728 goto out;
2729 }
2730 }
2731 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002732 if (kw->skip != -1)
2733 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002734 continue;
2735 }
2736
2737 err = NULL;
2738 if (!srv_dumped) {
2739 srv_dump_kws(&err);
2740 indent_msg(&err, 4);
2741 srv_dumped = 1;
2742 }
2743
Christopher Faulet767a84b2017-11-24 16:50:31 +01002744 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002745 file, linenum, args[0], args[1], args[cur_arg],
2746 err ? " Registered keywords :" : "", err ? err : "");
2747 free(err);
2748
2749 err_code |= ERR_ALERT | ERR_FATAL;
2750 goto out;
2751 }
2752 }
2753
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002754 if (!defsrv)
2755 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2756 if (err_code & ERR_FATAL)
2757 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002758 if (srv_tmpl)
2759 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002760 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002761 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002762 return 0;
2763
2764 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002765 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002766 free(errmsg);
2767 return err_code;
2768}
2769
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002770/* Returns a pointer to the first server matching either id <id>.
2771 * NULL is returned if no match is found.
2772 * the lookup is performed in the backend <bk>
2773 */
2774struct server *server_find_by_id(struct proxy *bk, int id)
2775{
2776 struct eb32_node *eb32;
2777 struct server *curserver;
2778
2779 if (!bk || (id ==0))
2780 return NULL;
2781
2782 /* <bk> has no backend capabilities, so it can't have a server */
2783 if (!(bk->cap & PR_CAP_BE))
2784 return NULL;
2785
2786 curserver = NULL;
2787
2788 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2789 if (eb32)
2790 curserver = container_of(eb32, struct server, conf.id);
2791
2792 return curserver;
2793}
2794
2795/* Returns a pointer to the first server matching either name <name>, or id
2796 * if <name> starts with a '#'. NULL is returned if no match is found.
2797 * the lookup is performed in the backend <bk>
2798 */
2799struct server *server_find_by_name(struct proxy *bk, const char *name)
2800{
2801 struct server *curserver;
2802
2803 if (!bk || !name)
2804 return NULL;
2805
2806 /* <bk> has no backend capabilities, so it can't have a server */
2807 if (!(bk->cap & PR_CAP_BE))
2808 return NULL;
2809
2810 curserver = NULL;
2811 if (*name == '#') {
2812 curserver = server_find_by_id(bk, atoi(name + 1));
2813 if (curserver)
2814 return curserver;
2815 }
2816 else {
2817 curserver = bk->srv;
2818
2819 while (curserver && (strcmp(curserver->id, name) != 0))
2820 curserver = curserver->next;
2821
2822 if (curserver)
2823 return curserver;
2824 }
2825
2826 return NULL;
2827}
2828
2829struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2830{
2831 struct server *byname;
2832 struct server *byid;
2833
2834 if (!name && !id)
2835 return NULL;
2836
2837 if (diff)
2838 *diff = 0;
2839
2840 byname = byid = NULL;
2841
2842 if (name) {
2843 byname = server_find_by_name(bk, name);
2844 if (byname && (!id || byname->puid == id))
2845 return byname;
2846 }
2847
2848 /* remaining possibilities :
2849 * - name not set
2850 * - name set but not found
2851 * - name found but ID doesn't match
2852 */
2853 if (id) {
2854 byid = server_find_by_id(bk, id);
2855 if (byid) {
2856 if (byname) {
2857 /* use id only if forced by configuration */
2858 if (byid->flags & SRV_F_FORCED_ID) {
2859 if (diff)
2860 *diff |= 2;
2861 return byid;
2862 }
2863 else {
2864 if (diff)
2865 *diff |= 1;
2866 return byname;
2867 }
2868 }
2869
2870 /* remaining possibilities:
2871 * - name not set
2872 * - name set but not found
2873 */
2874 if (name && diff)
2875 *diff |= 2;
2876 return byid;
2877 }
2878
2879 /* id bot found */
2880 if (byname) {
2881 if (diff)
2882 *diff |= 1;
2883 return byname;
2884 }
2885 }
2886
2887 return NULL;
2888}
2889
Willy Tarreau46b7f532018-08-21 11:54:26 +02002890/* Update a server state using the parameters available in the params list.
2891 *
2892 * Grabs the server lock during operation.
2893 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002894static void srv_update_state(struct server *srv, int version, char **params)
2895{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002896 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002897 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002898
2899 /* fields since version 1
2900 * and common to all other upcoming versions
2901 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002902 enum srv_state srv_op_state;
2903 enum srv_admin srv_admin_state;
2904 unsigned srv_uweight, srv_iweight;
2905 unsigned long srv_last_time_change;
2906 short srv_check_status;
2907 enum chk_result srv_check_result;
2908 int srv_check_health;
2909 int srv_check_state, srv_agent_state;
2910 int bk_f_forced_id;
2911 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002912 int fqdn_set_by_cli;
2913 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002914 const char *port_str;
2915 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002916 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002917
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002918 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002919 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002920 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002921 switch (version) {
2922 case 1:
2923 /*
2924 * now we can proceed with server's state update:
2925 * srv_addr: params[0]
2926 * srv_op_state: params[1]
2927 * srv_admin_state: params[2]
2928 * srv_uweight: params[3]
2929 * srv_iweight: params[4]
2930 * srv_last_time_change: params[5]
2931 * srv_check_status: params[6]
2932 * srv_check_result: params[7]
2933 * srv_check_health: params[8]
2934 * srv_check_state: params[9]
2935 * srv_agent_state: params[10]
2936 * bk_f_forced_id: params[11]
2937 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002938 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002939 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002940 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002941 */
2942
2943 /* validating srv_op_state */
2944 p = NULL;
2945 errno = 0;
2946 srv_op_state = strtol(params[1], &p, 10);
2947 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2948 (srv_op_state != SRV_ST_STOPPED &&
2949 srv_op_state != SRV_ST_STARTING &&
2950 srv_op_state != SRV_ST_RUNNING &&
2951 srv_op_state != SRV_ST_STOPPING)) {
2952 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2953 }
2954
2955 /* validating srv_admin_state */
2956 p = NULL;
2957 errno = 0;
2958 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002959 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002960
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002961 /* inherited statuses will be recomputed later.
2962 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2963 */
2964 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002965
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002966 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2967 (srv_admin_state != 0 &&
2968 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002969 srv_admin_state != SRV_ADMF_CMAINT &&
2970 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002971 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002972 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002973 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2974 }
2975
2976 /* validating srv_uweight */
2977 p = NULL;
2978 errno = 0;
2979 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002980 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002981 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2982
2983 /* validating srv_iweight */
2984 p = NULL;
2985 errno = 0;
2986 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002987 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002988 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2989
2990 /* validating srv_last_time_change */
2991 p = NULL;
2992 errno = 0;
2993 srv_last_time_change = strtol(params[5], &p, 10);
2994 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2995 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2996
2997 /* validating srv_check_status */
2998 p = NULL;
2999 errno = 0;
3000 srv_check_status = strtol(params[6], &p, 10);
3001 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3002 (srv_check_status >= HCHK_STATUS_SIZE))
3003 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3004
3005 /* validating srv_check_result */
3006 p = NULL;
3007 errno = 0;
3008 srv_check_result = strtol(params[7], &p, 10);
3009 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3010 (srv_check_result != CHK_RES_UNKNOWN &&
3011 srv_check_result != CHK_RES_NEUTRAL &&
3012 srv_check_result != CHK_RES_FAILED &&
3013 srv_check_result != CHK_RES_PASSED &&
3014 srv_check_result != CHK_RES_CONDPASS)) {
3015 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3016 }
3017
3018 /* validating srv_check_health */
3019 p = NULL;
3020 errno = 0;
3021 srv_check_health = strtol(params[8], &p, 10);
3022 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3023 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3024
3025 /* validating srv_check_state */
3026 p = NULL;
3027 errno = 0;
3028 srv_check_state = strtol(params[9], &p, 10);
3029 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3030 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3031 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3032
3033 /* validating srv_agent_state */
3034 p = NULL;
3035 errno = 0;
3036 srv_agent_state = strtol(params[10], &p, 10);
3037 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3038 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3039 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3040
3041 /* validating bk_f_forced_id */
3042 p = NULL;
3043 errno = 0;
3044 bk_f_forced_id = strtol(params[11], &p, 10);
3045 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3046 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3047
3048 /* validating srv_f_forced_id */
3049 p = NULL;
3050 errno = 0;
3051 srv_f_forced_id = strtol(params[12], &p, 10);
3052 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3053 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3054
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003055 /* validating srv_fqdn */
3056 fqdn = params[13];
3057 if (fqdn && *fqdn == '-')
3058 fqdn = NULL;
3059 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3060 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3061 fqdn = NULL;
3062 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003063
Frédéric Lécaille31694712017-08-01 08:47:19 +02003064 port_str = params[14];
3065 if (port_str) {
3066 port = strl2uic(port_str, strlen(port_str));
3067 if (port > USHRT_MAX) {
3068 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3069 port_str = NULL;
3070 }
3071 }
3072
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003073 /* SRV record
3074 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3075 */
3076 srvrecord = params[15];
3077 if (srvrecord && *srvrecord != '_')
3078 srvrecord = NULL;
3079
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003080 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003081 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003082 goto out;
3083
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003084 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003085 /* recover operational state and apply it to this server
3086 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003087 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003088 switch (srv_op_state) {
3089 case SRV_ST_STOPPED:
3090 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003091 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003092 break;
3093 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003094 /* If rise == 1 there is no STARTING state, let's switch to
3095 * RUNNING
3096 */
3097 if (srv->check.rise == 1) {
3098 srv->check.health = srv->check.rise + srv->check.fall - 1;
3099 srv_set_running(srv, "", NULL);
3100 break;
3101 }
3102 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3103 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003104 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003105 break;
3106 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003107 /* If fall == 1 there is no STOPPING state, let's switch to
3108 * STOPPED
3109 */
3110 if (srv->check.fall == 1) {
3111 srv->check.health = 0;
3112 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3113 break;
3114 }
3115 if (srv->check.health < srv->check.rise ||
3116 srv->check.health > srv->check.rise + srv->check.fall - 2)
3117 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003118 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003119 break;
3120 case SRV_ST_RUNNING:
3121 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003122 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003123 break;
3124 }
3125
3126 /* When applying server state, the following rules apply:
3127 * - in case of a configuration change, we apply the setting from the new
3128 * configuration, regardless of old running state
3129 * - if no configuration change, we apply old running state only if old running
3130 * state is different from new configuration state
3131 */
3132 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003133 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3134 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003135 srv_adm_set_maint(srv);
3136 else
3137 srv_adm_set_ready(srv);
3138 }
3139 /* configuration is the same, let's compate old running state and new conf state */
3140 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003141 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003143 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003144 srv_adm_set_ready(srv);
3145 }
3146 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003147 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003148 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003149 * (srv->iweight is the weight set up in configuration).
3150 * There are two possible reasons for FDRAIN to have been present :
3151 * - previous config weight was zero
3152 * - "set server b/s drain" was sent to the CLI
3153 *
3154 * In the first case, we simply want to drop this drain state
3155 * if the new weight is not zero anymore, meaning the administrator
3156 * has intentionally turned the weight back to a positive value to
3157 * enable the server again after an operation. In the second case,
3158 * the drain state was forced on the CLI regardless of the config's
3159 * weight so we don't want a change to the config weight to lose this
3160 * status. What this means is :
3161 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3162 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003163 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003164 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003165 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003166 }
3167
3168 srv->last_change = date.tv_sec - srv_last_time_change;
3169 srv->check.status = srv_check_status;
3170 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003171
3172 /* Only case we want to apply is removing ENABLED flag which could have been
3173 * done by the "disable health" command over the stats socket
3174 */
3175 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3176 (srv_check_state & CHK_ST_CONFIGURED) &&
3177 !(srv_check_state & CHK_ST_ENABLED))
3178 srv->check.state &= ~CHK_ST_ENABLED;
3179
3180 /* Only case we want to apply is removing ENABLED flag which could have been
3181 * done by the "disable agent" command over the stats socket
3182 */
3183 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3184 (srv_agent_state & CHK_ST_CONFIGURED) &&
3185 !(srv_agent_state & CHK_ST_ENABLED))
3186 srv->agent.state &= ~CHK_ST_ENABLED;
3187
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003188 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3189 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003190 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003191 * It means that a configuration file change has precedence over a unix socket change
3192 * for server's weight
3193 *
3194 * by default, HAProxy applies the following weight when parsing the configuration
3195 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003196 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003197 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003198 srv->uweight = srv_uweight;
3199 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003200 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003201
Willy Tarreaue5a60682016-11-09 14:54:53 +01003202 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003203 if (strcmp(params[0], "-"))
3204 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003205
3206 if (fqdn && srv->hostname) {
3207 if (!strcmp(srv->hostname, fqdn)) {
3208 /* Here we reset the 'set from stats socket FQDN' flag
3209 * to support such transitions:
3210 * Let's say initial FQDN value is foo1 (in configuration file).
3211 * - FQDN changed from stats socket, from foo1 to foo2 value,
3212 * - FQDN changed again from file configuration (with the same previous value
3213 set from stats socket, from foo1 to foo2 value),
3214 * - reload for any other reason than a FQDN modification,
3215 * the configuration file FQDN matches the fqdn server state file value.
3216 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003217 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003218 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003219 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003220 }
3221 else {
3222 /* If the FDQN has been changed from stats socket,
3223 * apply fqdn state file value (which is the value set
3224 * from stats socket).
3225 */
3226 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003227 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003228 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003229 }
3230 }
3231 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003232 /* If all the conditions below are validated, this means
3233 * we're evaluating a server managed by SRV resolution
3234 */
3235 else if (fqdn && !srv->hostname && srvrecord) {
3236 int res;
3237
3238 /* we can't apply previous state if SRV record has changed */
3239 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3240 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);
3241 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3242 goto out;
3243 }
3244
3245 /* create or find a SRV resolution for this srv record */
3246 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3247 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3248 if (srv->srvrq == NULL) {
3249 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3250 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3251 goto out;
3252 }
3253
3254 /* prepare DNS resolution for this server */
3255 res = srv_prepare_for_resolution(srv, fqdn);
3256 if (res == -1) {
3257 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3258 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3259 goto out;
3260 }
3261
3262 /* configure check.port accordingly */
3263 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3264 !(srv->flags & SRV_F_CHECKPORT))
3265 srv->check.port = port;
3266
3267 /* Unset SRV_F_MAPPORTS for SRV records.
3268 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3269 * because no ports are provided in the configuration file.
3270 * This is because HAProxy will use the port found into the SRV record.
3271 */
3272 srv->flags &= ~SRV_F_MAPPORTS;
3273 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003274
Frédéric Lécaille31694712017-08-01 08:47:19 +02003275 if (port_str)
3276 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003277 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003278
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003279 break;
3280 default:
3281 chunk_appendf(msg, ", version '%d' not supported", version);
3282 }
3283
3284 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003286 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003287 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003288 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003289 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003290}
3291
3292/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3293 * For each proxy, it does the following:
3294 * - opens its server state file (either one or local one)
3295 * - read whole file, line by line
3296 * - analyse each line to check if it matches our current backend:
3297 * - backend name matches
3298 * - backend id matches if id is forced and name doesn't match
3299 * - if the server pointed by the line is found, then state is applied
3300 *
3301 * If the running backend uuid or id differs from the state file, then HAProxy reports
3302 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003303 *
3304 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003305 */
3306void apply_server_state(void)
3307{
3308 char *cur, *end;
3309 char mybuf[SRV_STATE_LINE_MAXLEN];
3310 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003311 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3312 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003313 int arg, srv_arg, version, diff;
3314 FILE *f;
3315 char *filepath;
3316 char globalfilepath[MAXPATHLEN + 1];
3317 char localfilepath[MAXPATHLEN + 1];
3318 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003319 struct proxy *curproxy, *bk;
3320 struct server *srv;
3321
3322 globalfilepathlen = 0;
3323 /* create the globalfilepath variable */
3324 if (global.server_state_file) {
3325 /* absolute path or no base directory provided */
3326 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3327 len = strlen(global.server_state_file);
3328 if (len > MAXPATHLEN) {
3329 globalfilepathlen = 0;
3330 goto globalfileerror;
3331 }
3332 memcpy(globalfilepath, global.server_state_file, len);
3333 globalfilepath[len] = '\0';
3334 globalfilepathlen = len;
3335 }
3336 else if (global.server_state_base) {
3337 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003338 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003339 globalfilepathlen = 0;
3340 goto globalfileerror;
3341 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003342 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003343 globalfilepath[len] = 0;
3344 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003345
3346 /* append a slash if needed */
3347 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3348 if (globalfilepathlen + 1 > MAXPATHLEN) {
3349 globalfilepathlen = 0;
3350 goto globalfileerror;
3351 }
3352 globalfilepath[globalfilepathlen++] = '/';
3353 }
3354
3355 len = strlen(global.server_state_file);
3356 if (globalfilepathlen + len > MAXPATHLEN) {
3357 globalfilepathlen = 0;
3358 goto globalfileerror;
3359 }
3360 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3361 globalfilepathlen += len;
3362 globalfilepath[globalfilepathlen++] = 0;
3363 }
3364 }
3365 globalfileerror:
3366 if (globalfilepathlen == 0)
3367 globalfilepath[0] = '\0';
3368
3369 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003370 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003371 /* servers are only in backends */
3372 if (!(curproxy->cap & PR_CAP_BE))
3373 continue;
3374 fileopenerr = 0;
3375 filepath = NULL;
3376
3377 /* search server state file path and name */
3378 switch (curproxy->load_server_state_from_file) {
3379 /* read servers state from global file */
3380 case PR_SRV_STATE_FILE_GLOBAL:
3381 /* there was an error while generating global server state file path */
3382 if (globalfilepathlen == 0)
3383 continue;
3384 filepath = globalfilepath;
3385 fileopenerr = 1;
3386 break;
3387 /* this backend has its own file */
3388 case PR_SRV_STATE_FILE_LOCAL:
3389 localfilepathlen = 0;
3390 localfilepath[0] = '\0';
3391 len = 0;
3392 /* create the localfilepath variable */
3393 /* absolute path or no base directory provided */
3394 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3395 len = strlen(curproxy->server_state_file_name);
3396 if (len > MAXPATHLEN) {
3397 localfilepathlen = 0;
3398 goto localfileerror;
3399 }
3400 memcpy(localfilepath, curproxy->server_state_file_name, len);
3401 localfilepath[len] = '\0';
3402 localfilepathlen = len;
3403 }
3404 else if (global.server_state_base) {
3405 len = strlen(global.server_state_base);
3406 localfilepathlen += len;
3407
3408 if (localfilepathlen > MAXPATHLEN) {
3409 localfilepathlen = 0;
3410 goto localfileerror;
3411 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003412 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003413 localfilepath[localfilepathlen] = 0;
3414
3415 /* append a slash if needed */
3416 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3417 if (localfilepathlen + 1 > MAXPATHLEN) {
3418 localfilepathlen = 0;
3419 goto localfileerror;
3420 }
3421 localfilepath[localfilepathlen++] = '/';
3422 }
3423
3424 len = strlen(curproxy->server_state_file_name);
3425 if (localfilepathlen + len > MAXPATHLEN) {
3426 localfilepathlen = 0;
3427 goto localfileerror;
3428 }
3429 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3430 localfilepathlen += len;
3431 localfilepath[localfilepathlen++] = 0;
3432 }
3433 filepath = localfilepath;
3434 localfileerror:
3435 if (localfilepathlen == 0)
3436 localfilepath[0] = '\0';
3437
3438 break;
3439 case PR_SRV_STATE_FILE_NONE:
3440 default:
3441 continue;
3442 }
3443
3444 /* preload global state file */
3445 errno = 0;
3446 f = fopen(filepath, "r");
3447 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003449 if (!f)
3450 continue;
3451
3452 mybuf[0] = '\0';
3453 mybuflen = 0;
3454 version = 0;
3455
3456 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003457 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003458 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003459 goto fileclose;
3460 }
3461
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003462 cur = mybuf;
3463 version = atoi(cur);
3464 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3465 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003466 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003467
3468 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3469 int bk_f_forced_id = 0;
3470 int check_id = 0;
3471 int check_name = 0;
3472
3473 mybuflen = strlen(mybuf);
3474 cur = mybuf;
3475 end = cur + mybuflen;
3476
3477 bk = NULL;
3478 srv = NULL;
3479
3480 /* we need at least one character */
3481 if (mybuflen == 0)
3482 continue;
3483
3484 /* ignore blank characters at the beginning of the line */
3485 while (isspace(*cur))
3486 ++cur;
3487
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003488 /* Ignore empty or commented lines */
3489 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003490 continue;
3491
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003492 /* truncated lines */
3493 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003494 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003495 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003496 }
3497
3498 /* Removes trailing '\n' */
3499 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003500
3501 /* we're now ready to move the line into *srv_params[] */
3502 params[0] = cur;
3503 arg = 1;
3504 srv_arg = 0;
3505 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3506 if (isspace(*cur)) {
3507 *cur = '\0';
3508 ++cur;
3509 while (isspace(*cur))
3510 ++cur;
3511 switch (version) {
3512 case 1:
3513 /*
3514 * srv_addr: params[4] => srv_params[0]
3515 * srv_op_state: params[5] => srv_params[1]
3516 * srv_admin_state: params[6] => srv_params[2]
3517 * srv_uweight: params[7] => srv_params[3]
3518 * srv_iweight: params[8] => srv_params[4]
3519 * srv_last_time_change: params[9] => srv_params[5]
3520 * srv_check_status: params[10] => srv_params[6]
3521 * srv_check_result: params[11] => srv_params[7]
3522 * srv_check_health: params[12] => srv_params[8]
3523 * srv_check_state: params[13] => srv_params[9]
3524 * srv_agent_state: params[14] => srv_params[10]
3525 * bk_f_forced_id: params[15] => srv_params[11]
3526 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003527 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003528 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003529 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003530 */
3531 if (arg >= 4) {
3532 srv_params[srv_arg] = cur;
3533 ++srv_arg;
3534 }
3535 break;
3536 }
3537
3538 params[arg] = cur;
3539 ++arg;
3540 }
3541 else {
3542 ++cur;
3543 }
3544 }
3545
3546 /* if line is incomplete line, then ignore it.
3547 * otherwise, update useful flags */
3548 switch (version) {
3549 case 1:
3550 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3551 continue;
3552 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3553 check_id = (atoi(params[0]) == curproxy->uuid);
3554 check_name = (strcmp(curproxy->id, params[1]) == 0);
3555 break;
3556 }
3557
3558 diff = 0;
3559 bk = curproxy;
3560
3561 /* if backend can't be found, let's continue */
3562 if (!check_id && !check_name)
3563 continue;
3564 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003565 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 +02003566 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3567 }
3568 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003569 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 +02003570 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3571 /* if name doesn't match, we still want to update curproxy if the backend id
3572 * was forced in previous the previous configuration */
3573 if (!bk_f_forced_id)
3574 continue;
3575 }
3576
3577 /* look for the server by its id: param[2] */
3578 /* else look for the server by its name: param[3] */
3579 diff = 0;
3580 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3581
3582 if (!srv) {
3583 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003584 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3585 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003586 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3587 params[3], params[2], params[0], params[1]);
3588 continue;
3589 }
3590 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003591 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 +02003592 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 +02003593 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003594 }
3595 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003596 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 +02003597 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 +02003598 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003599 }
3600
3601 /* now we can proceed with server's state update */
3602 srv_update_state(srv, version, srv_params);
3603 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003604fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003605 fclose(f);
3606 }
3607}
3608
Simon Horman7d09b9a2013-02-12 10:45:51 +09003609/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003610 * update a server's current IP address.
3611 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3612 * ip is in network format.
3613 * updater is a string which contains an information about the requester of the update.
3614 * updater is used if not NULL.
3615 *
3616 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003617 *
3618 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003619 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003620int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003621{
3622 /* generates a log line and a warning on stderr */
3623 if (1) {
3624 /* book enough space for both IPv4 and IPv6 */
3625 char oldip[INET6_ADDRSTRLEN];
3626 char newip[INET6_ADDRSTRLEN];
3627
3628 memset(oldip, '\0', INET6_ADDRSTRLEN);
3629 memset(newip, '\0', INET6_ADDRSTRLEN);
3630
3631 /* copy old IP address in a string */
3632 switch (s->addr.ss_family) {
3633 case AF_INET:
3634 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3635 break;
3636 case AF_INET6:
3637 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3638 break;
3639 };
3640
3641 /* copy new IP address in a string */
3642 switch (ip_sin_family) {
3643 case AF_INET:
3644 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3645 break;
3646 case AF_INET6:
3647 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3648 break;
3649 };
3650
3651 /* save log line into a buffer */
3652 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3653 s->proxy->id, s->id, oldip, newip, updater);
3654
3655 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003656 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003657
3658 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003659 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003660 }
3661
3662 /* save the new IP family */
3663 s->addr.ss_family = ip_sin_family;
3664 /* save the new IP address */
3665 switch (ip_sin_family) {
3666 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003667 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003668 break;
3669 case AF_INET6:
3670 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3671 break;
3672 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003673 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003674
3675 return 0;
3676}
3677
3678/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003679 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3680 *
3681 * Caller can pass its name through <updater> to get it integrated in the response message
3682 * returned by the function.
3683 *
3684 * The function first does the following, in that order:
3685 * - validates the new addr and/or port
3686 * - checks if an update is required (new IP or port is different than current ones)
3687 * - checks the update is allowed:
3688 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3689 * - allow all changes if no CHECKS are configured
3690 * - if CHECK is configured:
3691 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3692 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3693 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003694 *
3695 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003696 */
3697const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3698{
3699 struct sockaddr_storage sa;
3700 int ret, port_change_required;
3701 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003702 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003703 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003704 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003705
3706 msg = get_trash_chunk();
3707 chunk_reset(msg);
3708
3709 if (addr) {
3710 memset(&sa, 0, sizeof(struct sockaddr_storage));
3711 if (str2ip2(addr, &sa, 0) == NULL) {
3712 chunk_printf(msg, "Invalid addr '%s'", addr);
3713 goto out;
3714 }
3715
3716 /* changes are allowed on AF_INET* families only */
3717 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3718 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3719 goto out;
3720 }
3721
3722 /* collecting data currently setup */
3723 memset(current_addr, '\0', sizeof(current_addr));
3724 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3725 /* changes are allowed on AF_INET* families only */
3726 if ((ret != AF_INET) && (ret != AF_INET6)) {
3727 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3728 goto out;
3729 }
3730
3731 /* applying ADDR changes if required and allowed
3732 * ipcmp returns 0 when both ADDR are the same
3733 */
3734 if (ipcmp(&s->addr, &sa) == 0) {
3735 chunk_appendf(msg, "no need to change the addr");
3736 goto port;
3737 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003738 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003739 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003740
3741 /* we also need to update check's ADDR only if it uses the server's one */
3742 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003743 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003744 }
3745
3746 /* we also need to update agent ADDR only if it use the server's one */
3747 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003748 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003749 }
3750
3751 /* update report for caller */
3752 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3753 }
3754
3755 port:
3756 if (port) {
3757 char sign = '\0';
3758 char *endptr;
3759
3760 if (addr)
3761 chunk_appendf(msg, ", ");
3762
3763 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003764 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003765
3766 /* check if PORT change is required */
3767 port_change_required = 0;
3768
3769 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003770 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003771 new_port = strtol(port, &endptr, 10);
3772 if ((errno != 0) || (port == endptr)) {
3773 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3774 goto out;
3775 }
3776
3777 /* check if caller triggers a port mapped or offset */
3778 if (sign == '-' || (sign == '+')) {
3779 /* check if server currently uses port map */
3780 if (!(s->flags & SRV_F_MAPPORTS)) {
3781 /* switch from fixed port to port map mandatorily triggers
3782 * a port change */
3783 port_change_required = 1;
3784 /* check is configured
3785 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3786 * prevent PORT change if check doesn't have it's dedicated port while switching
3787 * to port mapping */
3788 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3789 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.");
3790 goto out;
3791 }
3792 }
3793 /* we're already using port maps */
3794 else {
3795 port_change_required = current_port != new_port;
3796 }
3797 }
3798 /* fixed port */
3799 else {
3800 port_change_required = current_port != new_port;
3801 }
3802
3803 /* applying PORT changes if required and update response message */
3804 if (port_change_required) {
3805 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003806 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003807 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003808
3809 /* prepare message */
3810 chunk_appendf(msg, "port changed from '");
3811 if (s->flags & SRV_F_MAPPORTS)
3812 chunk_appendf(msg, "+");
3813 chunk_appendf(msg, "%d' to '", current_port);
3814
3815 if (sign == '-') {
3816 s->flags |= SRV_F_MAPPORTS;
3817 chunk_appendf(msg, "%c", sign);
3818 /* just use for result output */
3819 new_port = -new_port;
3820 }
3821 else if (sign == '+') {
3822 s->flags |= SRV_F_MAPPORTS;
3823 chunk_appendf(msg, "%c", sign);
3824 }
3825 else {
3826 s->flags &= ~SRV_F_MAPPORTS;
3827 }
3828
3829 chunk_appendf(msg, "%d'", new_port);
3830
3831 /* we also need to update health checks port only if it uses server's realport */
3832 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3833 s->check.port = new_port;
3834 }
3835 }
3836 else {
3837 chunk_appendf(msg, "no need to change the port");
3838 }
3839 }
3840
3841out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003842 if (changed)
3843 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003844 if (updater)
3845 chunk_appendf(msg, " by '%s'", updater);
3846 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003847 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003848}
3849
3850
3851/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003852 * update server status based on result of name resolution
3853 * returns:
3854 * 0 if server status is updated
3855 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003856 *
3857 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003858 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003859int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003860{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003861 struct dns_resolvers *resolvers = s->resolvers;
3862 struct dns_resolution *resolution = s->dns_requester->resolution;
3863 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003864
3865 switch (resolution->status) {
3866 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003867 /* status when HAProxy has just (re)started.
3868 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003869 break;
3870
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003871 case RSLV_STATUS_VALID:
3872 /*
3873 * resume health checks
3874 * server will be turned back on if health check is safe
3875 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003876 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003877 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003878 return 1;
3879 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3880 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003881 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003882 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003883
Emeric Brun52a91d32017-08-31 14:41:55 +02003884 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003885 return 1;
3886 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3887 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3888 s->proxy->id, s->id);
3889
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003890 ha_warning("%s.\n", trash.area);
3891 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003892 return 0;
3893
3894 case RSLV_STATUS_NX:
3895 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003896 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3897 if (!tick_is_expired(exp, now_ms))
3898 break;
3899
3900 if (s->next_admin & SRV_ADMF_RMAINT)
3901 return 1;
3902 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3903 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003904
3905 case RSLV_STATUS_TIMEOUT:
3906 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003907 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3908 if (!tick_is_expired(exp, now_ms))
3909 break;
3910
3911 if (s->next_admin & SRV_ADMF_RMAINT)
3912 return 1;
3913 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3914 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003915
3916 case RSLV_STATUS_REFUSED:
3917 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003918 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3919 if (!tick_is_expired(exp, now_ms))
3920 break;
3921
3922 if (s->next_admin & SRV_ADMF_RMAINT)
3923 return 1;
3924 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3925 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003926
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003927 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003928 /* stop server if resolution failed for a long enough period */
3929 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3930 if (!tick_is_expired(exp, now_ms))
3931 break;
3932
3933 if (s->next_admin & SRV_ADMF_RMAINT)
3934 return 1;
3935 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3936 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003937 }
3938
3939 return 1;
3940}
3941
3942/*
3943 * Server Name Resolution valid response callback
3944 * It expects:
3945 * - <nameserver>: the name server which answered the valid response
3946 * - <response>: buffer containing a valid DNS response
3947 * - <response_len>: size of <response>
3948 * It performs the following actions:
3949 * - ignore response if current ip found and server family not met
3950 * - update with first new ip found if family is met and current IP is not found
3951 * returns:
3952 * 0 on error
3953 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003954 *
3955 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003956 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003957int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003958{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003959 struct server *s = NULL;
3960 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003961 void *serverip, *firstip;
3962 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003963 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003964 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003965 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003966
Christopher Faulet67957bd2017-09-27 11:00:59 +02003967 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003968 if (!s)
3969 return 1;
3970
Christopher Faulet67957bd2017-09-27 11:00:59 +02003971 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003972
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003973 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003974 firstip = NULL; /* pointer to the first valid response found */
3975 /* it will be used as the new IP if a change is required */
3976 firstip_sin_family = AF_UNSPEC;
3977 serverip = NULL; /* current server IP address */
3978
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003979 /* initializing server IP pointer */
3980 server_sin_family = s->addr.ss_family;
3981 switch (server_sin_family) {
3982 case AF_INET:
3983 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3984 break;
3985
3986 case AF_INET6:
3987 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3988 break;
3989
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003990 case AF_UNSPEC:
3991 break;
3992
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003993 default:
3994 goto invalid;
3995 }
3996
Baptiste Assmann729c9012017-05-22 15:13:10 +02003997 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003998 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003999 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004000
4001 switch (ret) {
4002 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004003 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004004
4005 case DNS_UPD_SRVIP_NOT_FOUND:
4006 goto save_ip;
4007
4008 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004009 goto invalid;
4010
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004011 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004012 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004013 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004014
Baptiste Assmannfad03182015-10-28 02:03:32 +01004015 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004016 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004017 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004019
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004020 default:
4021 goto invalid;
4022
4023 }
4024
4025 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004026 if (nameserver) {
4027 nameserver->counters.update++;
4028 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004029 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004030 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004031 else
4032 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004033 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004034
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004035 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004036 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004037 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004038
4039 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004040 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004041 nameserver->counters.invalid++;
4042 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004043 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004044 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004045 return 0;
4046}
4047
4048/*
4049 * Server Name Resolution error management callback
4050 * returns:
4051 * 0 on error
4052 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004053 *
4054 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004055 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004056int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004057{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004058 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004059
Christopher Faulet67957bd2017-09-27 11:00:59 +02004060 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004061 if (!s)
4062 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004063 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004064 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004065 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004066 return 1;
4067}
4068
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004069/*
4070 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004071 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004072 * It returns a pointer to the first server found or NULL if <ip> is not yet
4073 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004074 *
4075 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004076 */
4077struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4078{
4079 struct server *tmpsrv;
4080 struct proxy *be;
4081
4082 if (!srv)
4083 return NULL;
4084
4085 be = srv->proxy;
4086 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004087 /* we found the current server is the same, ignore it */
4088 if (srv == tmpsrv)
4089 continue;
4090
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004091 /* We want to compare the IP in the record with the IP of the servers in the
4092 * same backend, only if:
4093 * * DNS resolution is enabled on the server
4094 * * the hostname used for the resolution by our server is the same than the
4095 * one used for the server found in the backend
4096 * * the server found in the backend is not our current server
4097 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004098 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004099 if ((tmpsrv->hostname_dn == NULL) ||
4100 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4101 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004102 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004103 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004104 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004105 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004106
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004107 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004108 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004109 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004110 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004111 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004112
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004113 /* At this point, we have 2 different servers using the same DNS hostname
4114 * for their respective resolution.
4115 */
4116 if (*ip_family == tmpsrv->addr.ss_family &&
4117 ((tmpsrv->addr.ss_family == AF_INET &&
4118 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4119 (tmpsrv->addr.ss_family == AF_INET6 &&
4120 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004121 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004122 return tmpsrv;
4123 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004124 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004125 }
4126
Emeric Brune9fd6b52017-11-02 17:20:39 +01004127
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004128 return NULL;
4129}
4130
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004131/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4132 * resolver. This is suited for initial address configuration. Returns 0 on
4133 * success otherwise a non-zero error code. In case of error, *err_code, if
4134 * not NULL, is filled up.
4135 */
4136int srv_set_addr_via_libc(struct server *srv, int *err_code)
4137{
4138 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004139 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004140 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004141 return 1;
4142 }
4143 return 0;
4144}
4145
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004146/* Set the server's FDQN (->hostname) from <hostname>.
4147 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004148 *
4149 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004150 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004151int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004152{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004153 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004154 char *hostname_dn;
4155 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004156
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004157 /* Note that the server lock is already held. */
4158 if (!srv->resolvers)
4159 return -1;
4160
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004161 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004162 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004163 /* run time DNS resolution was not active for this server
4164 * and we can't enable it at run time for now.
4165 */
4166 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004167 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004168
4169 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004170 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004171 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004172 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4173 hostname_dn, trash.size);
4174 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004175 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004176
Christopher Faulet67957bd2017-09-27 11:00:59 +02004177 resolution = srv->dns_requester->resolution;
4178 if (resolution &&
4179 resolution->hostname_dn &&
4180 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004181 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004182
Christopher Faulet67957bd2017-09-27 11:00:59 +02004183 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004184
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004185 free(srv->hostname);
4186 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004187 srv->hostname = strdup(hostname);
4188 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004189 srv->hostname_dn_len = hostname_dn_len;
4190 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004191 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004192
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004193 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004194 goto err;
4195
4196 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004197 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004198 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004199 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004200
4201 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004202 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004203 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004204 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004205}
4206
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004207/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4208 * from the state file. This is suited for initial address configuration.
4209 * Returns 0 on success otherwise a non-zero error code. In case of error,
4210 * *err_code, if not NULL, is filled up.
4211 */
4212static int srv_apply_lastaddr(struct server *srv, int *err_code)
4213{
4214 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4215 if (err_code)
4216 *err_code |= ERR_WARN;
4217 return 1;
4218 }
4219 return 0;
4220}
4221
Willy Tarreau25e51522016-11-04 15:10:17 +01004222/* returns 0 if no error, otherwise a combination of ERR_* flags */
4223static int srv_iterate_initaddr(struct server *srv)
4224{
4225 int return_code = 0;
4226 int err_code;
4227 unsigned int methods;
4228
4229 methods = srv->init_addr_methods;
4230 if (!methods) { // default to "last,libc"
4231 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4232 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4233 }
4234
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004235 /* "-dr" : always append "none" so that server addresses resolution
4236 * failures are silently ignored, this is convenient to validate some
4237 * configs out of their environment.
4238 */
4239 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4240 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4241
Willy Tarreau25e51522016-11-04 15:10:17 +01004242 while (methods) {
4243 err_code = 0;
4244 switch (srv_get_next_initaddr(&methods)) {
4245 case SRV_IADDR_LAST:
4246 if (!srv->lastaddr)
4247 continue;
4248 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004249 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004250 return_code |= err_code;
4251 break;
4252
4253 case SRV_IADDR_LIBC:
4254 if (!srv->hostname)
4255 continue;
4256 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004257 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004258 return_code |= err_code;
4259 break;
4260
Willy Tarreau37ebe122016-11-04 15:17:58 +01004261 case SRV_IADDR_NONE:
4262 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004263 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004264 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4265 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004266 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004267 return return_code;
4268
Willy Tarreau4310d362016-11-02 15:05:56 +01004269 case SRV_IADDR_IP:
4270 ipcpy(&srv->init_addr, &srv->addr);
4271 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004272 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4273 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004274 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004275 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004276
Willy Tarreau25e51522016-11-04 15:10:17 +01004277 default: /* unhandled method */
4278 break;
4279 }
4280 }
4281
4282 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004283 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004284 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4285 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004286 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004287 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004288 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4289 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004290
4291 return_code |= ERR_ALERT | ERR_FATAL;
4292 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004293out:
4294 srv_set_dyncookie(srv);
4295 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004296}
4297
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004298/*
4299 * This function parses all backends and all servers within each backend
4300 * and performs servers' addr resolution based on information provided by:
4301 * - configuration file
4302 * - server-state file (states provided by an 'old' haproxy process)
4303 *
4304 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4305 */
4306int srv_init_addr(void)
4307{
4308 struct proxy *curproxy;
4309 int return_code = 0;
4310
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004311 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004312 while (curproxy) {
4313 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004314
4315 /* servers are in backend only */
4316 if (!(curproxy->cap & PR_CAP_BE))
4317 goto srv_init_addr_next;
4318
Willy Tarreau25e51522016-11-04 15:10:17 +01004319 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004320 if (srv->hostname)
4321 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004322
4323 srv_init_addr_next:
4324 curproxy = curproxy->next;
4325 }
4326
4327 return return_code;
4328}
4329
Willy Tarreau46b7f532018-08-21 11:54:26 +02004330/*
4331 * Must be called with the server lock held.
4332 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004333const 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 +02004334{
4335
Willy Tarreau83061a82018-07-13 11:56:34 +02004336 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004337
4338 msg = get_trash_chunk();
4339 chunk_reset(msg);
4340
Olivier Houchard8da5f982017-08-04 18:35:36 +02004341 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004342 chunk_appendf(msg, "no need to change the FDQN");
4343 goto out;
4344 }
4345
4346 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4347 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4348 goto out;
4349 }
4350
4351 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4352 server->proxy->id, server->id, server->hostname, fqdn);
4353
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004354 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004355 chunk_reset(msg);
4356 chunk_appendf(msg, "could not update %s/%s FQDN",
4357 server->proxy->id, server->id);
4358 goto out;
4359 }
4360
4361 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004362 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004363
4364 out:
4365 if (updater)
4366 chunk_appendf(msg, " by '%s'", updater);
4367 chunk_appendf(msg, "\n");
4368
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004369 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004370}
4371
4372
Willy Tarreau21b069d2016-11-23 17:15:08 +01004373/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4374 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004375 * 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 +01004376 * used for CLI commands requiring a server name.
4377 * Important: the <arg> is modified to remove the '/'.
4378 */
4379struct server *cli_find_server(struct appctx *appctx, char *arg)
4380{
4381 struct proxy *px;
4382 struct server *sv;
4383 char *line;
4384
4385 /* split "backend/server" and make <line> point to server */
4386 for (line = arg; *line; line++)
4387 if (*line == '/') {
4388 *line++ = '\0';
4389 break;
4390 }
4391
4392 if (!*line || !*arg) {
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 = "Require 'backend/server'.\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 (!get_backend_server(arg, line, &px, &sv)) {
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 = px ? "No such server.\n" : "No such backend.\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 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004407 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004408 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004409 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004410 return NULL;
4411 }
4412
4413 return sv;
4414}
4415
William Lallemand222baf22016-11-19 02:00:33 +01004416
Willy Tarreau46b7f532018-08-21 11:54:26 +02004417/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004418static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004419{
4420 struct server *sv;
4421 const char *warning;
4422
4423 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4424 return 1;
4425
4426 sv = cli_find_server(appctx, args[2]);
4427 if (!sv)
4428 return 1;
4429
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004430 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004431
William Lallemand222baf22016-11-19 02:00:33 +01004432 if (strcmp(args[3], "weight") == 0) {
4433 warning = server_parse_weight_change_request(sv, args[4]);
4434 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004435 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004436 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004437 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004438 }
4439 }
4440 else if (strcmp(args[3], "state") == 0) {
4441 if (strcmp(args[4], "ready") == 0)
4442 srv_adm_set_ready(sv);
4443 else if (strcmp(args[4], "drain") == 0)
4444 srv_adm_set_drain(sv);
4445 else if (strcmp(args[4], "maint") == 0)
4446 srv_adm_set_maint(sv);
4447 else {
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 = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004450 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004451 }
4452 }
4453 else if (strcmp(args[3], "health") == 0) {
4454 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004455 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004456 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004457 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004458 }
4459 else if (strcmp(args[4], "up") == 0) {
4460 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004461 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004462 }
4463 else if (strcmp(args[4], "stopping") == 0) {
4464 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004465 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004466 }
4467 else if (strcmp(args[4], "down") == 0) {
4468 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004469 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004470 }
4471 else {
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 = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004474 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004475 }
4476 }
4477 else if (strcmp(args[3], "agent") == 0) {
4478 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004479 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004480 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004481 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004482 }
4483 else if (strcmp(args[4], "up") == 0) {
4484 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004485 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004486 }
4487 else if (strcmp(args[4], "down") == 0) {
4488 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004489 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004490 }
4491 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004492 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004493 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004494 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004495 }
4496 }
Misiek2da082d2017-01-09 09:40:42 +01004497 else if (strcmp(args[3], "agent-addr") == 0) {
4498 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004499 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004500 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4501 appctx->st0 = CLI_ST_PRINT;
4502 } else {
4503 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004504 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004505 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4506 appctx->st0 = CLI_ST_PRINT;
4507 }
4508 }
4509 }
4510 else if (strcmp(args[3], "agent-send") == 0) {
4511 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004512 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004513 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4514 appctx->st0 = CLI_ST_PRINT;
4515 } else {
4516 char *nss = strdup(args[4]);
4517 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004518 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004519 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4520 appctx->st0 = CLI_ST_PRINT;
4521 } else {
4522 free(sv->agent.send_string);
4523 sv->agent.send_string = nss;
4524 sv->agent.send_string_len = strlen(args[4]);
4525 }
4526 }
4527 }
William Lallemand222baf22016-11-19 02:00:33 +01004528 else if (strcmp(args[3], "check-port") == 0) {
4529 int i = 0;
4530 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004531 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004532 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004533 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004534 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004535 }
4536 if ((i < 0) || (i > 65535)) {
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 = "provided port is not valid.\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 /* prevent the update of port to 0 if MAPPORTS are in use */
4543 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004544 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004545 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004546 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004547 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004548 }
4549 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004550 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004551 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004552 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004553 }
4554 else if (strcmp(args[3], "addr") == 0) {
4555 char *addr = NULL;
4556 char *port = NULL;
4557 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004558 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004559 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004560 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004561 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004562 }
4563 else {
4564 addr = args[4];
4565 }
4566 if (strcmp(args[5], "port") == 0) {
4567 port = args[6];
4568 }
4569 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4570 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004571 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004572 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004573 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004574 }
4575 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4576 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004577 else if (strcmp(args[3], "fqdn") == 0) {
4578 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004579 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004580 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4581 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004582 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004583 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004584 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004585 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004586 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004587 appctx->ctx.cli.msg = warning;
4588 appctx->st0 = CLI_ST_PRINT;
4589 }
4590 }
William Lallemand222baf22016-11-19 02:00:33 +01004591 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004592 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004593 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 +01004594 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004595 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004596 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004597 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004598 return 1;
4599}
4600
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004601static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004602{
4603 struct stream_interface *si = appctx->owner;
4604 struct proxy *px;
4605 struct server *sv;
4606 char *line;
4607
4608
4609 /* split "backend/server" and make <line> point to server */
4610 for (line = args[2]; *line; line++)
4611 if (*line == '/') {
4612 *line++ = '\0';
4613 break;
4614 }
4615
4616 if (!*line) {
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 = "Require 'backend/server'.\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 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004624 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004625 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004626 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004627 return 1;
4628 }
4629
4630 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004631 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4632 sv->iweight);
4633 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004634 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004635 return 0;
4636 }
William Lallemand6b160942016-11-22 12:34:35 +01004637 return 1;
4638}
4639
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004640/* Parse a "set weight" command.
4641 *
4642 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004643 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004644static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004645{
4646 struct server *sv;
4647 const char *warning;
4648
4649 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4650 return 1;
4651
4652 sv = cli_find_server(appctx, args[2]);
4653 if (!sv)
4654 return 1;
4655
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004656 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4657
William Lallemand6b160942016-11-22 12:34:35 +01004658 warning = server_parse_weight_change_request(sv, args[3]);
4659 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004660 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004661 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004662 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004663 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004664
4665 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4666
William Lallemand6b160942016-11-22 12:34:35 +01004667 return 1;
4668}
4669
Willy Tarreau46b7f532018-08-21 11:54:26 +02004670/* parse a "set maxconn server" command. It always returns 1.
4671 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004672 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004673 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004674static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004675{
4676 struct server *sv;
4677 const char *warning;
4678
4679 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4680 return 1;
4681
4682 sv = cli_find_server(appctx, args[3]);
4683 if (!sv)
4684 return 1;
4685
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004686 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4687
Willy Tarreaub8026272016-11-23 11:26:56 +01004688 warning = server_parse_maxconn_change_request(sv, args[4]);
4689 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004690 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004691 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004692 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004693 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004694
4695 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4696
Willy Tarreaub8026272016-11-23 11:26:56 +01004697 return 1;
4698}
William Lallemand6b160942016-11-22 12:34:35 +01004699
Willy Tarreau46b7f532018-08-21 11:54:26 +02004700/* parse a "disable agent" command. It always returns 1.
4701 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004702 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004703 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004704static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004705{
4706 struct server *sv;
4707
4708 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4709 return 1;
4710
4711 sv = cli_find_server(appctx, args[2]);
4712 if (!sv)
4713 return 1;
4714
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004715 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004716 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004717 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004718 return 1;
4719}
4720
Willy Tarreau46b7f532018-08-21 11:54:26 +02004721/* parse a "disable health" command. It always returns 1.
4722 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004723 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004724 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004725static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004726{
4727 struct server *sv;
4728
4729 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4730 return 1;
4731
4732 sv = cli_find_server(appctx, args[2]);
4733 if (!sv)
4734 return 1;
4735
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004736 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004737 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004738 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004739 return 1;
4740}
4741
Willy Tarreau46b7f532018-08-21 11:54:26 +02004742/* parse a "disable server" command. It always returns 1.
4743 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004744 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004745 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004746static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004747{
4748 struct server *sv;
4749
4750 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4751 return 1;
4752
4753 sv = cli_find_server(appctx, args[2]);
4754 if (!sv)
4755 return 1;
4756
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004757 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004758 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004759 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004760 return 1;
4761}
4762
Willy Tarreau46b7f532018-08-21 11:54:26 +02004763/* parse a "enable agent" command. It always returns 1.
4764 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004765 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004766 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004767static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004768{
4769 struct server *sv;
4770
4771 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4772 return 1;
4773
4774 sv = cli_find_server(appctx, args[2]);
4775 if (!sv)
4776 return 1;
4777
4778 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004779 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004780 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004781 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004782 return 1;
4783 }
4784
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004785 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004786 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004787 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004788 return 1;
4789}
4790
Willy Tarreau46b7f532018-08-21 11:54:26 +02004791/* parse a "enable health" command. It always returns 1.
4792 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004793 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004794 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004795static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004796{
4797 struct server *sv;
4798
4799 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4800 return 1;
4801
4802 sv = cli_find_server(appctx, args[2]);
4803 if (!sv)
4804 return 1;
4805
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004806 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004807 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004808 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004809 return 1;
4810}
4811
Willy Tarreau46b7f532018-08-21 11:54:26 +02004812/* parse a "enable server" command. It always returns 1.
4813 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004814 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004815 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004816static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004817{
4818 struct server *sv;
4819
4820 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4821 return 1;
4822
4823 sv = cli_find_server(appctx, args[2]);
4824 if (!sv)
4825 return 1;
4826
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004827 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004828 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004829 if (!(sv->flags & SRV_F_COOKIESET)
4830 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4831 sv->cookie)
4832 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004833 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004834 return 1;
4835}
4836
William Lallemand222baf22016-11-19 02:00:33 +01004837/* register cli keywords */
4838static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004839 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004840 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004841 { { "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 +01004842 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004843 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004844 { { "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 +01004845 { { "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 +01004846 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004847 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4848 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4849
William Lallemand222baf22016-11-19 02:00:33 +01004850 {{},}
4851}};
4852
Willy Tarreau0108d902018-11-25 19:14:37 +01004853INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004854
Emeric Brun64cc49c2017-10-03 14:46:45 +02004855/*
4856 * This function applies server's status changes, it is
4857 * is designed to be called asynchronously.
4858 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004859 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004860 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004861static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004862{
4863 struct check *check = &s->check;
4864 int xferred;
4865 struct proxy *px = s->proxy;
4866 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4867 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4868 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004869 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004870
Emeric Brun64cc49c2017-10-03 14:46:45 +02004871 /* If currently main is not set we try to apply pending state changes */
4872 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4873 int next_admin;
4874
4875 /* Backup next admin */
4876 next_admin = s->next_admin;
4877
4878 /* restore current admin state */
4879 s->next_admin = s->cur_admin;
4880
4881 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4882 s->last_change = now.tv_sec;
4883 if (s->proxy->lbprm.set_server_status_down)
4884 s->proxy->lbprm.set_server_status_down(s);
4885
4886 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4887 srv_shutdown_streams(s, SF_ERR_DOWN);
4888
4889 /* we might have streams queued on this server and waiting for
4890 * a connection. Those which are redispatchable will be queued
4891 * to another server or to the proxy itself.
4892 */
4893 xferred = pendconn_redistribute(s);
4894
4895 tmptrash = alloc_trash_chunk();
4896 if (tmptrash) {
4897 chunk_printf(tmptrash,
4898 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4899 s->proxy->id, s->id);
4900
Emeric Brun5a133512017-10-19 14:42:30 +02004901 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004902 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004903
4904 /* we don't send an alert if the server was previously paused */
4905 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004906 send_log(s->proxy, log_level, "%s.\n",
4907 tmptrash->area);
4908 send_email_alert(s, log_level, "%s",
4909 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004910 free_trash_chunk(tmptrash);
4911 tmptrash = NULL;
4912 }
4913 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4914 set_backend_down(s->proxy);
4915
4916 s->counters.down_trans++;
4917 }
4918 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4919 s->last_change = now.tv_sec;
4920 if (s->proxy->lbprm.set_server_status_down)
4921 s->proxy->lbprm.set_server_status_down(s);
4922
4923 /* we might have streams queued on this server and waiting for
4924 * a connection. Those which are redispatchable will be queued
4925 * to another server or to the proxy itself.
4926 */
4927 xferred = pendconn_redistribute(s);
4928
4929 tmptrash = alloc_trash_chunk();
4930 if (tmptrash) {
4931 chunk_printf(tmptrash,
4932 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4933 s->proxy->id, s->id);
4934
Emeric Brun5a133512017-10-19 14:42:30 +02004935 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004936
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004937 ha_warning("%s.\n", tmptrash->area);
4938 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4939 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004940 free_trash_chunk(tmptrash);
4941 tmptrash = NULL;
4942 }
4943
4944 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4945 set_backend_down(s->proxy);
4946 }
4947 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4948 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4949 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4950 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4951 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4952 s->proxy->last_change = now.tv_sec;
4953 }
4954
4955 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4956 s->down_time += now.tv_sec - s->last_change;
4957
4958 s->last_change = now.tv_sec;
4959 if (s->next_state == SRV_ST_STARTING)
4960 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4961
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004962 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004963 /* now propagate the status change to any LB algorithms */
4964 if (px->lbprm.update_server_eweight)
4965 px->lbprm.update_server_eweight(s);
4966 else if (srv_willbe_usable(s)) {
4967 if (px->lbprm.set_server_status_up)
4968 px->lbprm.set_server_status_up(s);
4969 }
4970 else {
4971 if (px->lbprm.set_server_status_down)
4972 px->lbprm.set_server_status_down(s);
4973 }
4974
4975 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4976 * and it's not a backup server and its effective weight is > 0,
4977 * then it can accept new connections, so we shut down all streams
4978 * on all backup servers.
4979 */
4980 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4981 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4982 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4983
4984 /* check if we can handle some connections queued at the proxy. We
4985 * will take as many as we can handle.
4986 */
4987 xferred = pendconn_grab_from_px(s);
4988
4989 tmptrash = alloc_trash_chunk();
4990 if (tmptrash) {
4991 chunk_printf(tmptrash,
4992 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4993 s->proxy->id, s->id);
4994
Emeric Brun5a133512017-10-19 14:42:30 +02004995 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004996 ha_warning("%s.\n", tmptrash->area);
4997 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4998 tmptrash->area);
4999 send_email_alert(s, LOG_NOTICE, "%s",
5000 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005001 free_trash_chunk(tmptrash);
5002 tmptrash = NULL;
5003 }
5004
5005 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5006 set_backend_down(s->proxy);
5007 }
5008 else if (s->cur_eweight != s->next_eweight) {
5009 /* now propagate the status change to any LB algorithms */
5010 if (px->lbprm.update_server_eweight)
5011 px->lbprm.update_server_eweight(s);
5012 else if (srv_willbe_usable(s)) {
5013 if (px->lbprm.set_server_status_up)
5014 px->lbprm.set_server_status_up(s);
5015 }
5016 else {
5017 if (px->lbprm.set_server_status_down)
5018 px->lbprm.set_server_status_down(s);
5019 }
5020
5021 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5022 set_backend_down(s->proxy);
5023 }
5024
5025 s->next_admin = next_admin;
5026 }
5027
Emeric Brun5a133512017-10-19 14:42:30 +02005028 /* reset operational state change */
5029 *s->op_st_chg.reason = 0;
5030 s->op_st_chg.status = s->op_st_chg.code = -1;
5031 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005032
5033 /* Now we try to apply pending admin changes */
5034
5035 /* Maintenance must also disable health checks */
5036 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5037 if (s->check.state & CHK_ST_ENABLED) {
5038 s->check.state |= CHK_ST_PAUSED;
5039 check->health = 0;
5040 }
5041
5042 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005043 tmptrash = alloc_trash_chunk();
5044 if (tmptrash) {
5045 chunk_printf(tmptrash,
5046 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5047 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5048 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005049
Olivier Houchard796a2b32017-10-24 17:42:47 +02005050 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005051
Olivier Houchard796a2b32017-10-24 17:42:47 +02005052 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005053 ha_warning("%s.\n", tmptrash->area);
5054 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5055 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005056 }
5057 free_trash_chunk(tmptrash);
5058 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005059 }
Emeric Brun8f298292017-12-06 16:47:17 +01005060 /* commit new admin status */
5061
5062 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005063 }
5064 else { /* server was still running */
5065 check->health = 0; /* failure */
5066 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005067
5068 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005069 if (s->proxy->lbprm.set_server_status_down)
5070 s->proxy->lbprm.set_server_status_down(s);
5071
Emeric Brun64cc49c2017-10-03 14:46:45 +02005072 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5073 srv_shutdown_streams(s, SF_ERR_DOWN);
5074
5075 /* we might have streams queued on this server and waiting for
5076 * a connection. Those which are redispatchable will be queued
5077 * to another server or to the proxy itself.
5078 */
5079 xferred = pendconn_redistribute(s);
5080
5081 tmptrash = alloc_trash_chunk();
5082 if (tmptrash) {
5083 chunk_printf(tmptrash,
5084 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5085 s->flags & SRV_F_BACKUP ? "Backup " : "",
5086 s->proxy->id, s->id,
5087 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5088
5089 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5090
5091 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005092 ha_warning("%s.\n", tmptrash->area);
5093 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5094 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005095 }
5096 free_trash_chunk(tmptrash);
5097 tmptrash = NULL;
5098 }
5099 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5100 set_backend_down(s->proxy);
5101
5102 s->counters.down_trans++;
5103 }
5104 }
5105 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5106 /* OK here we're leaving maintenance, we have many things to check,
5107 * because the server might possibly be coming back up depending on
5108 * its state. In practice, leaving maintenance means that we should
5109 * immediately turn to UP (more or less the slowstart) under the
5110 * following conditions :
5111 * - server is neither checked nor tracked
5112 * - server tracks another server which is not checked
5113 * - server tracks another server which is already up
5114 * Which sums up as something simpler :
5115 * "either the tracking server is up or the server's checks are disabled
5116 * or up". Otherwise we only re-enable health checks. There's a special
5117 * case associated to the stopping state which can be inherited. Note
5118 * that the server might still be in drain mode, which is naturally dealt
5119 * with by the lower level functions.
5120 */
5121
5122 if (s->check.state & CHK_ST_ENABLED) {
5123 s->check.state &= ~CHK_ST_PAUSED;
5124 check->health = check->rise; /* start OK but check immediately */
5125 }
5126
5127 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5128 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5129 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5130 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5131 s->next_state = SRV_ST_STOPPING;
5132 }
5133 else {
5134 s->next_state = SRV_ST_STARTING;
5135 if (s->slowstart > 0)
5136 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5137 else
5138 s->next_state = SRV_ST_RUNNING;
5139 }
5140
5141 }
5142
5143 tmptrash = alloc_trash_chunk();
5144 if (tmptrash) {
5145 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5146 chunk_printf(tmptrash,
5147 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5148 s->flags & SRV_F_BACKUP ? "Backup " : "",
5149 s->proxy->id, s->id,
5150 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5151 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5152 }
5153 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5154 chunk_printf(tmptrash,
5155 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5156 s->flags & SRV_F_BACKUP ? "Backup " : "",
5157 s->proxy->id, s->id, s->hostname,
5158 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5159 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5160 }
5161 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5162 chunk_printf(tmptrash,
5163 "%sServer %s/%s is %s/%s (leaving maintenance)",
5164 s->flags & SRV_F_BACKUP ? "Backup " : "",
5165 s->proxy->id, s->id,
5166 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5167 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5168 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005169 ha_warning("%s.\n", tmptrash->area);
5170 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5171 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005172 free_trash_chunk(tmptrash);
5173 tmptrash = NULL;
5174 }
5175
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005176 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005177 /* now propagate the status change to any LB algorithms */
5178 if (px->lbprm.update_server_eweight)
5179 px->lbprm.update_server_eweight(s);
5180 else if (srv_willbe_usable(s)) {
5181 if (px->lbprm.set_server_status_up)
5182 px->lbprm.set_server_status_up(s);
5183 }
5184 else {
5185 if (px->lbprm.set_server_status_down)
5186 px->lbprm.set_server_status_down(s);
5187 }
5188
5189 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5190 set_backend_down(s->proxy);
5191
Willy Tarreau6a78e612018-08-07 10:14:53 +02005192 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5193 * and it's not a backup server and its effective weight is > 0,
5194 * then it can accept new connections, so we shut down all streams
5195 * on all backup servers.
5196 */
5197 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5198 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5199 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5200
5201 /* check if we can handle some connections queued at the proxy. We
5202 * will take as many as we can handle.
5203 */
5204 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005205 }
5206 else if (s->next_admin & SRV_ADMF_MAINT) {
5207 /* remaining in maintenance mode, let's inform precisely about the
5208 * situation.
5209 */
5210 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5211 tmptrash = alloc_trash_chunk();
5212 if (tmptrash) {
5213 chunk_printf(tmptrash,
5214 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5215 s->flags & SRV_F_BACKUP ? "Backup " : "",
5216 s->proxy->id, s->id);
5217
5218 if (s->track) /* normally it's mandatory here */
5219 chunk_appendf(tmptrash, " via %s/%s",
5220 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005221 ha_warning("%s.\n", tmptrash->area);
5222 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5223 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005224 free_trash_chunk(tmptrash);
5225 tmptrash = NULL;
5226 }
5227 }
5228 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5229 tmptrash = alloc_trash_chunk();
5230 if (tmptrash) {
5231 chunk_printf(tmptrash,
5232 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5233 s->flags & SRV_F_BACKUP ? "Backup " : "",
5234 s->proxy->id, s->id, s->hostname);
5235
5236 if (s->track) /* normally it's mandatory here */
5237 chunk_appendf(tmptrash, " via %s/%s",
5238 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005239 ha_warning("%s.\n", tmptrash->area);
5240 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5241 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005242 free_trash_chunk(tmptrash);
5243 tmptrash = NULL;
5244 }
5245 }
5246 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5247 tmptrash = alloc_trash_chunk();
5248 if (tmptrash) {
5249 chunk_printf(tmptrash,
5250 "%sServer %s/%s remains in forced maintenance",
5251 s->flags & SRV_F_BACKUP ? "Backup " : "",
5252 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005253 ha_warning("%s.\n", tmptrash->area);
5254 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5255 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005256 free_trash_chunk(tmptrash);
5257 tmptrash = NULL;
5258 }
5259 }
5260 /* don't report anything when leaving drain mode and remaining in maintenance */
5261
5262 s->cur_admin = s->next_admin;
5263 }
5264
5265 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5266 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5267 /* drain state is applied only if not yet in maint */
5268
5269 s->last_change = now.tv_sec;
5270 if (px->lbprm.set_server_status_down)
5271 px->lbprm.set_server_status_down(s);
5272
5273 /* we might have streams queued on this server and waiting for
5274 * a connection. Those which are redispatchable will be queued
5275 * to another server or to the proxy itself.
5276 */
5277 xferred = pendconn_redistribute(s);
5278
5279 tmptrash = alloc_trash_chunk();
5280 if (tmptrash) {
5281 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5282 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5283 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5284
5285 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5286
5287 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005288 ha_warning("%s.\n", tmptrash->area);
5289 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5290 tmptrash->area);
5291 send_email_alert(s, LOG_NOTICE, "%s",
5292 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005293 }
5294 free_trash_chunk(tmptrash);
5295 tmptrash = NULL;
5296 }
5297
5298 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5299 set_backend_down(s->proxy);
5300 }
5301 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5302 /* OK completely leaving drain mode */
5303 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5304 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5305 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5306 s->proxy->last_change = now.tv_sec;
5307 }
5308
5309 if (s->last_change < now.tv_sec) // ignore negative times
5310 s->down_time += now.tv_sec - s->last_change;
5311 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005312 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005313
5314 tmptrash = alloc_trash_chunk();
5315 if (tmptrash) {
5316 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5317 chunk_printf(tmptrash,
5318 "%sServer %s/%s is %s (leaving forced drain)",
5319 s->flags & SRV_F_BACKUP ? "Backup " : "",
5320 s->proxy->id, s->id,
5321 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5322 }
5323 else {
5324 chunk_printf(tmptrash,
5325 "%sServer %s/%s is %s (leaving drain)",
5326 s->flags & SRV_F_BACKUP ? "Backup " : "",
5327 s->proxy->id, s->id,
5328 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5329 if (s->track) /* normally it's mandatory here */
5330 chunk_appendf(tmptrash, " via %s/%s",
5331 s->track->proxy->id, s->track->id);
5332 }
5333
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005334 ha_warning("%s.\n", tmptrash->area);
5335 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5336 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005337 free_trash_chunk(tmptrash);
5338 tmptrash = NULL;
5339 }
5340
5341 /* now propagate the status change to any LB algorithms */
5342 if (px->lbprm.update_server_eweight)
5343 px->lbprm.update_server_eweight(s);
5344 else if (srv_willbe_usable(s)) {
5345 if (px->lbprm.set_server_status_up)
5346 px->lbprm.set_server_status_up(s);
5347 }
5348 else {
5349 if (px->lbprm.set_server_status_down)
5350 px->lbprm.set_server_status_down(s);
5351 }
5352 }
5353 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5354 /* remaining in drain mode after removing one of its flags */
5355
5356 tmptrash = alloc_trash_chunk();
5357 if (tmptrash) {
5358 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5359 chunk_printf(tmptrash,
5360 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5361 s->flags & SRV_F_BACKUP ? "Backup " : "",
5362 s->proxy->id, s->id);
5363
5364 if (s->track) /* normally it's mandatory here */
5365 chunk_appendf(tmptrash, " via %s/%s",
5366 s->track->proxy->id, s->track->id);
5367 }
5368 else {
5369 chunk_printf(tmptrash,
5370 "%sServer %s/%s remains in forced drain mode",
5371 s->flags & SRV_F_BACKUP ? "Backup " : "",
5372 s->proxy->id, s->id);
5373 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005374 ha_warning("%s.\n", tmptrash->area);
5375 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5376 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005377 free_trash_chunk(tmptrash);
5378 tmptrash = NULL;
5379 }
5380
5381 /* commit new admin status */
5382
5383 s->cur_admin = s->next_admin;
5384 }
5385 }
5386
5387 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005388 *s->adm_st_chg_cause = 0;
5389}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005390
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005391struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5392{
5393 struct connection *conn;
5394
5395 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5396 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005397 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005398 }
5399
5400 return task;
5401}
5402
Willy Tarreau980855b2019-02-07 14:59:29 +01005403struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005404{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005405 struct server *srv;
5406 struct eb32_node *eb;
5407 int i;
5408 unsigned int next_wakeup;
5409 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005410
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005411 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5412 while (1) {
5413 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005414
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005415 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5416 if (!eb) {
5417 /* we might have reached the end of the tree, typically because
5418 * <now_ms> is in the first half and we're first scanning the last
5419 * half. Let's loop back to the beginning of the tree now.
5420 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005421
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005422 eb = eb32_first(&idle_conn_srv);
5423 if (likely(!eb))
5424 break;
5425 }
5426 if (tick_is_lt(now_ms, eb->key)) {
5427 /* timer not expired yet, revisit it later */
5428 next_wakeup = eb->key;
5429 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005430 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005431 }
5432 srv = eb32_entry(eb, struct server, idle_node);
5433 for (i = 0; i < global.nbthread; i++) {
5434 int max_conn = (srv->curr_idle_thr[i] / 2) +
5435 (srv->curr_idle_thr[i] & 1);
5436 int j;
5437 int did_remove = 0;
5438
5439 for (j = 0; j < max_conn; j++) {
5440 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5441 if (!conn)
5442 break;
5443 did_remove = 1;
5444 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5445 }
5446 if (did_remove && max_conn < srv->curr_idle_thr[i])
5447 srv_is_empty = 0;
5448 if (did_remove)
5449 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5450 }
5451 eb32_delete(&srv->idle_node);
5452 if (!srv_is_empty) {
5453 /* There are still more idle connections, add the
5454 * server back in the tree.
5455 */
5456 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5457 now_ms);
5458 eb32_insert(&idle_conn_srv, &srv->idle_node);
5459 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005460 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005461 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5462
5463 if (need_wakeup)
5464 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005465 else
5466 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005467
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005468 return task;
5469}
Olivier Houchard88698d92019-04-16 19:07:22 +02005470
5471/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5472static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5473 struct proxy *defpx, const char *file, int line,
5474 char **err)
5475{
5476 int arg = -1;
5477
5478 if (too_many_args(1, args, err, NULL))
5479 return -1;
5480
5481 if (*(args[1]) != 0)
5482 arg = atoi(args[1]);
5483
5484 if (arg < 0 || arg > 100) {
5485 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5486 return -1;
5487 }
5488
5489 if (args[0][10] == 'h')
5490 global.tune.pool_high_ratio = arg;
5491 else
5492 global.tune.pool_low_ratio = arg;
5493 return 0;
5494}
5495
5496/* config keyword parsers */
5497static struct cfg_kw_list cfg_kws = {ILH, {
5498 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5499 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5500 { 0, NULL, NULL }
5501}};
5502
5503INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5504
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005505/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005506 * Local variables:
5507 * c-indent-level: 8
5508 * c-basic-offset: 8
5509 * End:
5510 */