blob: 14e1a938c3c71fe66a02a093cbf6d3878e5958b0 [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 Tarreau975b1552019-06-06 16:25:55 +02001821 /* please don't put default server settings here, they are set in
1822 * init_default_instance().
1823 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001824 return srv;
1825}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001826
1827/*
1828 * Validate <srv> server health-check settings.
1829 * Returns 0 if everything is OK, -1 if not.
1830 */
1831static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1832{
1833 struct tcpcheck_rule *r = NULL;
1834 struct list *l;
1835
1836 /*
1837 * We need at least a service port, a check port or the first tcp-check rule must
1838 * be a 'connect' one when checking an IPv4/IPv6 server.
1839 */
1840 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1841 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1842 return 0;
1843
1844 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1845 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001846 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1847 "Check has been disabled.\n",
1848 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001849 return -1;
1850 }
1851
1852 /* search the first action (connect / send / expect) in the list */
1853 l = &srv->proxy->tcpcheck_rules;
1854 list_for_each_entry(r, l, list) {
1855 if (r->action != TCPCHK_ACT_COMMENT)
1856 break;
1857 }
1858
1859 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001860 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1861 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1862 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001863 return -1;
1864 }
1865
1866 /* scan the tcp-check ruleset to ensure a port has been configured */
1867 l = &srv->proxy->tcpcheck_rules;
1868 list_for_each_entry(r, l, list) {
1869 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001870 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1871 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1872 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001873 return -1;
1874 }
1875 }
1876
1877 return 0;
1878}
1879
1880/*
1881 * Initialize <srv> health-check structure.
1882 * Returns the error string in case of memory allocation failure, NULL if not.
1883 */
1884static const char *do_health_check_init(struct server *srv, int check_type, int state)
1885{
1886 const char *ret;
1887
1888 if (!srv->do_check)
1889 return NULL;
1890
1891 ret = init_check(&srv->check, check_type);
1892 if (ret)
1893 return ret;
1894
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001895 srv->check.state |= state;
1896 global.maxsock++;
1897
1898 return NULL;
1899}
1900
1901static int server_health_check_init(const char *file, int linenum,
1902 struct server *srv, struct proxy *curproxy)
1903{
1904 const char *ret;
1905
1906 if (!srv->do_check)
1907 return 0;
1908
1909 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001910 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1911 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001912 return ERR_ALERT | ERR_FATAL;
1913 }
1914
1915 if (server_healthcheck_validate(file, linenum, srv) < 0)
1916 return ERR_ALERT | ERR_ABORT;
1917
1918 /* note: check type will be set during the config review phase */
1919 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1920 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001921 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001922 return ERR_ALERT | ERR_ABORT;
1923 }
1924
1925 return 0;
1926}
1927
1928/*
1929 * Initialize <srv> agent check structure.
1930 * Returns the error string in case of memory allocation failure, NULL if not.
1931 */
1932static const char *do_server_agent_check_init(struct server *srv, int state)
1933{
1934 const char *ret;
1935
1936 if (!srv->do_agent)
1937 return NULL;
1938
1939 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1940 if (ret)
1941 return ret;
1942
1943 if (!srv->agent.inter)
1944 srv->agent.inter = srv->check.inter;
1945
1946 srv->agent.state |= state;
1947 global.maxsock++;
1948
1949 return NULL;
1950}
1951
1952static int server_agent_check_init(const char *file, int linenum,
1953 struct server *srv, struct proxy *curproxy)
1954{
1955 const char *ret;
1956
1957 if (!srv->do_agent)
1958 return 0;
1959
1960 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001961 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 +02001962 file, linenum, srv->id);
1963 return ERR_ALERT | ERR_FATAL;
1964 }
1965
1966 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1967 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001968 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001969 return ERR_ALERT | ERR_ABORT;
1970 }
1971
1972 return 0;
1973}
1974
1975#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1976static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1977 struct server *srv, struct proxy *proxy)
1978{
1979 int ret;
1980 char *err = NULL;
1981
1982 if (!srv->sni_expr)
1983 return 0;
1984
1985 ret = server_parse_sni_expr(srv, proxy, &err);
1986 if (!ret)
1987 return 0;
1988
1989 display_parser_err(file, linenum, args, cur_arg, &err);
1990 free(err);
1991
1992 return ret;
1993}
1994#endif
1995
1996/*
1997 * Server initializations finalization.
1998 * Initialize health check, agent check and SNI expression if enabled.
1999 * Must not be called for a default server instance.
2000 */
2001static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2002 struct server *srv, struct proxy *px)
2003{
2004 int ret;
2005
2006 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2007 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2008 return ret;
2009 }
2010
2011#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2012 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2013 return ret;
2014#endif
2015
2016 if (srv->flags & SRV_F_BACKUP)
2017 px->srv_bck++;
2018 else
2019 px->srv_act++;
2020 srv_lb_commit_status(srv);
2021
2022 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002023err:
2024 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002025}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002026
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002027/*
2028 * Parse as much as possible such a range string argument: low[-high]
2029 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2030 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2031 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2032 * Returns 0 if succeeded, -1 if not.
2033 */
2034static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2035{
2036 char *nb_high_arg;
2037
2038 *nb_high = 0;
2039 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002040 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002041
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002042 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002043 *nb_high_arg++ = '\0';
2044 *nb_high = atoi(nb_high_arg);
2045 }
2046 else {
2047 *nb_high += *nb_low;
2048 *nb_low = 1;
2049 }
2050
2051 if (*nb_low < 0 || *nb_high < *nb_low)
2052 return -1;
2053
2054 return 0;
2055}
2056
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002057static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2058{
2059 chunk_printf(&trash, "%s%d", prefix, nb);
2060 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002061 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002062}
2063
2064/*
2065 * Initialize as much as possible servers from <srv> server template.
2066 * Note that a server template is a special server with
2067 * a few different parameters than a server which has
2068 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002069 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002070 * 'srv' template included.
2071 */
2072static int server_template_init(struct server *srv, struct proxy *px)
2073{
2074 int i;
2075 struct server *newsrv;
2076
2077 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2078 int check_init_state;
2079 int agent_init_state;
2080
2081 newsrv = new_server(px);
2082 if (!newsrv)
2083 goto err;
2084
2085 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002086 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002087#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2088 if (newsrv->sni_expr) {
2089 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2090 if (!newsrv->ssl_ctx.sni)
2091 goto err;
2092 }
2093#endif
2094 /* Set this new server ID. */
2095 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2096
2097 /* Initial checks states. */
2098 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2099 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2100
2101 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2102 do_server_agent_check_init(newsrv, agent_init_state))
2103 goto err;
2104
2105 /* Linked backwards first. This will be restablished after parsing. */
2106 newsrv->next = px->srv;
2107 px->srv = newsrv;
2108 }
2109 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2110
2111 return i - srv->tmpl_info.nb_low;
2112
2113 err:
2114 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2115 if (newsrv) {
2116#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2117 release_sample_expr(newsrv->ssl_ctx.sni);
2118#endif
2119 free_check(&newsrv->agent);
2120 free_check(&newsrv->check);
2121 }
2122 free(newsrv);
2123 return i - srv->tmpl_info.nb_low;
2124}
2125
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002126int 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 +02002127{
2128 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002129 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 char *errmsg = NULL;
2131 int err_code = 0;
2132 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002133 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002134
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002135 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002136 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002137 !strcmp(args[0], "default-server") ||
2138 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002139 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002140 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002141 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002142 int srv_tmpl = !defsrv && !srv;
2143 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002144
2145 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002146 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002147 err_code |= ERR_ALERT | ERR_FATAL;
2148 goto out;
2149 }
2150 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002151 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002152
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002153 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002154 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002155 if (!*args[2]) {
2156 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002157 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002158 file, linenum, args[0]);
2159 err_code |= ERR_ALERT | ERR_FATAL;
2160 goto out;
2161 }
2162
2163 err = invalid_char(args[1]);
2164 }
2165 else if (srv_tmpl) {
2166 if (!*args[3]) {
2167 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002168 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 +02002169 file, linenum, args[0]);
2170 err_code |= ERR_ALERT | ERR_FATAL;
2171 goto out;
2172 }
2173
2174 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002175 }
2176
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002177 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002178 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 +02002179 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002180 err_code |= ERR_ALERT | ERR_FATAL;
2181 goto out;
2182 }
2183
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002184 cur_arg = 2;
2185 if (srv_tmpl) {
2186 /* Parse server-template <nb | range> arg. */
2187 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002188 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002189 file, linenum, args[0], args[cur_arg]);
2190 err_code |= ERR_ALERT | ERR_FATAL;
2191 goto out;
2192 }
2193 cur_arg++;
2194 }
2195
Willy Tarreau272adea2014-03-31 10:39:59 +02002196 if (!defsrv) {
2197 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002198 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002199 struct protocol *proto;
2200
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002201 newsrv = new_server(curproxy);
2202 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002203 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002204 err_code |= ERR_ALERT | ERR_ABORT;
2205 goto out;
2206 }
2207
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002208 if (srv_tmpl) {
2209 newsrv->tmpl_info.nb_low = tmpl_range_low;
2210 newsrv->tmpl_info.nb_high = tmpl_range_high;
2211 }
2212
Willy Tarreau272adea2014-03-31 10:39:59 +02002213 /* the servers are linked backwards first */
2214 newsrv->next = curproxy->srv;
2215 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 newsrv->conf.file = strdup(file);
2217 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002218 /* Note: for a server template, its id is its prefix.
2219 * This is a temporary id which will be used for server allocations to come
2220 * after parsing.
2221 */
2222 if (srv)
2223 newsrv->id = strdup(args[1]);
2224 else
2225 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002226
2227 /* several ways to check the port component :
2228 * - IP => port=+0, relative (IPv4 only)
2229 * - IP: => port=+0, relative
2230 * - IP:N => port=N, absolute
2231 * - IP:+N => port=+N, relative
2232 * - IP:-N => port=-N, relative
2233 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002234 if (!parse_addr)
2235 goto skip_addr;
2236
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002237 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002238 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002239 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002240 err_code |= ERR_ALERT | ERR_FATAL;
2241 goto out;
2242 }
2243
2244 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002245 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002246 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002247 file, linenum, args[0], args[1]);
2248 err_code |= ERR_ALERT | ERR_FATAL;
2249 goto out;
2250 }
2251
2252 if (!port1 || !port2) {
2253 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002254 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002255 }
2256 else if (port1 != port2) {
2257 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002258 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002259 file, linenum, args[0], args[1], args[2]);
2260 err_code |= ERR_ALERT | ERR_FATAL;
2261 goto out;
2262 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002263
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002264 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002265 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002266 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002267 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002268 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2269 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2270 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002271 err_code |= ERR_ALERT | ERR_FATAL;
2272 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002273 }
2274 }
2275 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002276 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002277 file, linenum, newsrv->id);
2278 err_code |= ERR_ALERT | ERR_FATAL;
2279 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002280 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002281 }
2282
Willy Tarreau272adea2014-03-31 10:39:59 +02002283 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002284 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002285
Olivier Houchard8da5f982017-08-04 18:35:36 +02002286 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002287 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002288 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002289 err_code |= ERR_ALERT | ERR_FATAL;
2290 goto out;
2291 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002292
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002293 cur_arg++;
2294 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002295 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002296 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002297 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002298 } else {
2299 newsrv = &curproxy->defsrv;
2300 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002301 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002302 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002303 }
2304
2305 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002306 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002307 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2308 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002309 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002310 file, linenum, *err, newsrv->id);
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
2313 }
2314 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002315 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002316 file, linenum, val, args[cur_arg], newsrv->id);
2317 err_code |= ERR_ALERT | ERR_FATAL;
2318 goto out;
2319 }
2320 newsrv->agent.inter = val;
2321 cur_arg += 2;
2322 }
Misiekea849332017-01-09 09:39:51 +01002323 else if (!strcmp(args[cur_arg], "agent-addr")) {
2324 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002325 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002326 goto out;
2327 }
2328
2329 cur_arg += 2;
2330 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002331 else if (!strcmp(args[cur_arg], "agent-port")) {
2332 global.maxsock++;
2333 newsrv->agent.port = atol(args[cur_arg + 1]);
2334 cur_arg += 2;
2335 }
James Brown55f9ff12015-10-21 18:19:05 -07002336 else if (!strcmp(args[cur_arg], "agent-send")) {
2337 global.maxsock++;
2338 free(newsrv->agent.send_string);
2339 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2340 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2341 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2342 cur_arg += 2;
2343 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002344 else if (!strcmp(args[cur_arg], "init-addr")) {
2345 char *p, *end;
2346 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002347 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002348
2349 newsrv->init_addr_methods = 0;
2350 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2351
2352 for (p = args[cur_arg + 1]; *p; p = end) {
2353 /* cut on next comma */
2354 for (end = p; *end && *end != ','; end++);
2355 if (*end)
2356 *(end++) = 0;
2357
Willy Tarreau4310d362016-11-02 15:05:56 +01002358 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002359 if (!strcmp(p, "libc")) {
2360 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2361 }
2362 else if (!strcmp(p, "last")) {
2363 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2364 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002365 else if (!strcmp(p, "none")) {
2366 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2367 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002368 else if (str2ip2(p, &sa, 0)) {
2369 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002370 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002371 file, linenum, args[cur_arg], p);
2372 err_code |= ERR_ALERT | ERR_FATAL;
2373 goto out;
2374 }
2375 newsrv->init_addr = sa;
2376 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2377 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002378 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002379 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 +02002380 file, linenum, args[cur_arg], p);
2381 err_code |= ERR_ALERT | ERR_FATAL;
2382 goto out;
2383 }
2384 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002385 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002386 file, linenum, args[cur_arg], p);
2387 err_code |= ERR_ALERT | ERR_FATAL;
2388 goto out;
2389 }
2390 }
2391 cur_arg += 2;
2392 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002393 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002394 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002395 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2396 cur_arg += 2;
2397 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002398 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2399 char *p, *end;
2400
2401 for (p = args[cur_arg + 1]; *p; p = end) {
2402 /* cut on next comma */
2403 for (end = p; *end && *end != ','; end++);
2404 if (*end)
2405 *(end++) = 0;
2406
2407 if (!strcmp(p, "allow-dup-ip")) {
2408 newsrv->dns_opts.accept_duplicate_ip = 1;
2409 }
2410 else if (!strcmp(p, "prevent-dup-ip")) {
2411 newsrv->dns_opts.accept_duplicate_ip = 0;
2412 }
2413 else {
2414 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2415 file, linenum, args[cur_arg], p);
2416 err_code |= ERR_ALERT | ERR_FATAL;
2417 goto out;
2418 }
2419 }
2420
2421 cur_arg += 2;
2422 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002423 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2424 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002425 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002426 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002427 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002428 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002429 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002430 file, linenum, args[cur_arg]);
2431 err_code |= ERR_ALERT | ERR_FATAL;
2432 goto out;
2433 }
2434 cur_arg += 2;
2435 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002436 else if (!strcmp(args[cur_arg], "resolve-net")) {
2437 char *p, *e;
2438 unsigned char mask;
2439 struct dns_options *opt;
2440
2441 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002442 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002443 file, linenum, args[cur_arg]);
2444 err_code |= ERR_ALERT | ERR_FATAL;
2445 goto out;
2446 }
2447
2448 opt = &newsrv->dns_opts;
2449
2450 /* Split arguments by comma, and convert it from ipv4 or ipv6
2451 * string network in in_addr or in6_addr.
2452 */
2453 p = args[cur_arg + 1];
2454 e = p;
2455 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002456 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002457 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002458 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002459 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
2462 }
2463 /* look for end or comma. */
2464 while (*e != ',' && *e != '\0')
2465 e++;
2466 if (*e == ',') {
2467 *e = '\0';
2468 e++;
2469 }
2470 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2471 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2472 /* Try to convert input string from ipv4 or ipv6 network. */
2473 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2474 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2475 &mask)) {
2476 /* Try to convert input string from ipv6 network. */
2477 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2478 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2479 } else {
2480 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002481 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002482 file, linenum, args[cur_arg], p);
2483 err_code |= ERR_ALERT | ERR_FATAL;
2484 goto out;
2485 }
2486 opt->pref_net_nb++;
2487 p = e;
2488 }
2489
2490 cur_arg += 2;
2491 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002492 else if (!strcmp(args[cur_arg], "rise")) {
2493 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002494 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002495 file, linenum, args[cur_arg]);
2496 err_code |= ERR_ALERT | ERR_FATAL;
2497 goto out;
2498 }
2499
2500 newsrv->check.rise = atol(args[cur_arg + 1]);
2501 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002502 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002503 file, linenum, args[cur_arg]);
2504 err_code |= ERR_ALERT | ERR_FATAL;
2505 goto out;
2506 }
2507
2508 if (newsrv->check.health)
2509 newsrv->check.health = newsrv->check.rise;
2510 cur_arg += 2;
2511 }
2512 else if (!strcmp(args[cur_arg], "fall")) {
2513 newsrv->check.fall = atol(args[cur_arg + 1]);
2514
2515 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002516 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002517 file, linenum, args[cur_arg]);
2518 err_code |= ERR_ALERT | ERR_FATAL;
2519 goto out;
2520 }
2521
2522 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002523 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002524 file, linenum, args[cur_arg]);
2525 err_code |= ERR_ALERT | ERR_FATAL;
2526 goto out;
2527 }
2528
2529 cur_arg += 2;
2530 }
2531 else if (!strcmp(args[cur_arg], "inter")) {
2532 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2533 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002534 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002535 file, linenum, *err, newsrv->id);
2536 err_code |= ERR_ALERT | ERR_FATAL;
2537 goto out;
2538 }
2539 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002540 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002541 file, linenum, val, args[cur_arg], newsrv->id);
2542 err_code |= ERR_ALERT | ERR_FATAL;
2543 goto out;
2544 }
2545 newsrv->check.inter = val;
2546 cur_arg += 2;
2547 }
2548 else if (!strcmp(args[cur_arg], "fastinter")) {
2549 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2550 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002551 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002552 file, linenum, *err, newsrv->id);
2553 err_code |= ERR_ALERT | ERR_FATAL;
2554 goto out;
2555 }
2556 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002557 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002558 file, linenum, val, args[cur_arg], newsrv->id);
2559 err_code |= ERR_ALERT | ERR_FATAL;
2560 goto out;
2561 }
2562 newsrv->check.fastinter = val;
2563 cur_arg += 2;
2564 }
2565 else if (!strcmp(args[cur_arg], "downinter")) {
2566 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2567 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002568 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002569 file, linenum, *err, newsrv->id);
2570 err_code |= ERR_ALERT | ERR_FATAL;
2571 goto out;
2572 }
2573 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002574 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002575 file, linenum, val, args[cur_arg], newsrv->id);
2576 err_code |= ERR_ALERT | ERR_FATAL;
2577 goto out;
2578 }
2579 newsrv->check.downinter = val;
2580 cur_arg += 2;
2581 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002582 else if (!strcmp(args[cur_arg], "port")) {
2583 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002584 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002585 cur_arg += 2;
2586 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002587 else if (!strcmp(args[cur_arg], "weight")) {
2588 int w;
2589 w = atol(args[cur_arg + 1]);
2590 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002591 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002592 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2593 err_code |= ERR_ALERT | ERR_FATAL;
2594 goto out;
2595 }
2596 newsrv->uweight = newsrv->iweight = w;
2597 cur_arg += 2;
2598 }
2599 else if (!strcmp(args[cur_arg], "minconn")) {
2600 newsrv->minconn = atol(args[cur_arg + 1]);
2601 cur_arg += 2;
2602 }
2603 else if (!strcmp(args[cur_arg], "maxconn")) {
2604 newsrv->maxconn = atol(args[cur_arg + 1]);
2605 cur_arg += 2;
2606 }
2607 else if (!strcmp(args[cur_arg], "maxqueue")) {
2608 newsrv->maxqueue = atol(args[cur_arg + 1]);
2609 cur_arg += 2;
2610 }
2611 else if (!strcmp(args[cur_arg], "slowstart")) {
2612 /* slowstart is stored in seconds */
2613 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2614 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002615 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002616 file, linenum, *err, newsrv->id);
2617 err_code |= ERR_ALERT | ERR_FATAL;
2618 goto out;
2619 }
2620 newsrv->slowstart = (val + 999) / 1000;
2621 cur_arg += 2;
2622 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002623 else if (!strcmp(args[cur_arg], "on-error")) {
2624 if (!strcmp(args[cur_arg + 1], "fastinter"))
2625 newsrv->onerror = HANA_ONERR_FASTINTER;
2626 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2627 newsrv->onerror = HANA_ONERR_FAILCHK;
2628 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2629 newsrv->onerror = HANA_ONERR_SUDDTH;
2630 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2631 newsrv->onerror = HANA_ONERR_MARKDWN;
2632 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002633 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002634 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2635 file, linenum, args[cur_arg], args[cur_arg + 1]);
2636 err_code |= ERR_ALERT | ERR_FATAL;
2637 goto out;
2638 }
2639
2640 cur_arg += 2;
2641 }
2642 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2643 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2644 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2645 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002646 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 file, linenum, args[cur_arg], args[cur_arg + 1]);
2648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto out;
2650 }
2651
2652 cur_arg += 2;
2653 }
2654 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2655 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2656 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2657 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002658 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002659 file, linenum, args[cur_arg], args[cur_arg + 1]);
2660 err_code |= ERR_ALERT | ERR_FATAL;
2661 goto out;
2662 }
2663
2664 cur_arg += 2;
2665 }
2666 else if (!strcmp(args[cur_arg], "error-limit")) {
2667 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002668 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002669 file, linenum, args[cur_arg]);
2670 err_code |= ERR_ALERT | ERR_FATAL;
2671 goto out;
2672 }
2673
2674 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2675
2676 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002677 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002678 file, linenum, args[cur_arg]);
2679 err_code |= ERR_ALERT | ERR_FATAL;
2680 goto out;
2681 }
2682 cur_arg += 2;
2683 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002684 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002685 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002686 file, linenum, "usesrc", "source");
2687 err_code |= ERR_ALERT | ERR_FATAL;
2688 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002689 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002690 else {
2691 static int srv_dumped;
2692 struct srv_kw *kw;
2693 char *err;
2694
2695 kw = srv_find_kw(args[cur_arg]);
2696 if (kw) {
2697 char *err = NULL;
2698 int code;
2699
2700 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002701 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 +02002702 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002703 if (kw->skip != -1)
2704 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002705 err_code |= ERR_ALERT | ERR_FATAL;
2706 goto out;
2707 }
2708
2709 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002710 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002711 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002712 if (kw->skip != -1)
2713 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002714 err_code |= ERR_ALERT;
2715 continue;
2716 }
2717
2718 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2719 err_code |= code;
2720
2721 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002722 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002723 if (code & ERR_FATAL) {
2724 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002725 if (kw->skip != -1)
2726 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002727 goto out;
2728 }
2729 }
2730 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002731 if (kw->skip != -1)
2732 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002733 continue;
2734 }
2735
2736 err = NULL;
2737 if (!srv_dumped) {
2738 srv_dump_kws(&err);
2739 indent_msg(&err, 4);
2740 srv_dumped = 1;
2741 }
2742
Christopher Faulet767a84b2017-11-24 16:50:31 +01002743 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002744 file, linenum, args[0], args[1], args[cur_arg],
2745 err ? " Registered keywords :" : "", err ? err : "");
2746 free(err);
2747
2748 err_code |= ERR_ALERT | ERR_FATAL;
2749 goto out;
2750 }
2751 }
2752
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002753 if (!defsrv)
2754 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2755 if (err_code & ERR_FATAL)
2756 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002757 if (srv_tmpl)
2758 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002759 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002760 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002761 return 0;
2762
2763 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002764 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002765 free(errmsg);
2766 return err_code;
2767}
2768
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002769/* Returns a pointer to the first server matching either id <id>.
2770 * NULL is returned if no match is found.
2771 * the lookup is performed in the backend <bk>
2772 */
2773struct server *server_find_by_id(struct proxy *bk, int id)
2774{
2775 struct eb32_node *eb32;
2776 struct server *curserver;
2777
2778 if (!bk || (id ==0))
2779 return NULL;
2780
2781 /* <bk> has no backend capabilities, so it can't have a server */
2782 if (!(bk->cap & PR_CAP_BE))
2783 return NULL;
2784
2785 curserver = NULL;
2786
2787 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2788 if (eb32)
2789 curserver = container_of(eb32, struct server, conf.id);
2790
2791 return curserver;
2792}
2793
2794/* Returns a pointer to the first server matching either name <name>, or id
2795 * if <name> starts with a '#'. NULL is returned if no match is found.
2796 * the lookup is performed in the backend <bk>
2797 */
2798struct server *server_find_by_name(struct proxy *bk, const char *name)
2799{
2800 struct server *curserver;
2801
2802 if (!bk || !name)
2803 return NULL;
2804
2805 /* <bk> has no backend capabilities, so it can't have a server */
2806 if (!(bk->cap & PR_CAP_BE))
2807 return NULL;
2808
2809 curserver = NULL;
2810 if (*name == '#') {
2811 curserver = server_find_by_id(bk, atoi(name + 1));
2812 if (curserver)
2813 return curserver;
2814 }
2815 else {
2816 curserver = bk->srv;
2817
2818 while (curserver && (strcmp(curserver->id, name) != 0))
2819 curserver = curserver->next;
2820
2821 if (curserver)
2822 return curserver;
2823 }
2824
2825 return NULL;
2826}
2827
2828struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2829{
2830 struct server *byname;
2831 struct server *byid;
2832
2833 if (!name && !id)
2834 return NULL;
2835
2836 if (diff)
2837 *diff = 0;
2838
2839 byname = byid = NULL;
2840
2841 if (name) {
2842 byname = server_find_by_name(bk, name);
2843 if (byname && (!id || byname->puid == id))
2844 return byname;
2845 }
2846
2847 /* remaining possibilities :
2848 * - name not set
2849 * - name set but not found
2850 * - name found but ID doesn't match
2851 */
2852 if (id) {
2853 byid = server_find_by_id(bk, id);
2854 if (byid) {
2855 if (byname) {
2856 /* use id only if forced by configuration */
2857 if (byid->flags & SRV_F_FORCED_ID) {
2858 if (diff)
2859 *diff |= 2;
2860 return byid;
2861 }
2862 else {
2863 if (diff)
2864 *diff |= 1;
2865 return byname;
2866 }
2867 }
2868
2869 /* remaining possibilities:
2870 * - name not set
2871 * - name set but not found
2872 */
2873 if (name && diff)
2874 *diff |= 2;
2875 return byid;
2876 }
2877
2878 /* id bot found */
2879 if (byname) {
2880 if (diff)
2881 *diff |= 1;
2882 return byname;
2883 }
2884 }
2885
2886 return NULL;
2887}
2888
Willy Tarreau46b7f532018-08-21 11:54:26 +02002889/* Update a server state using the parameters available in the params list.
2890 *
2891 * Grabs the server lock during operation.
2892 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002893static void srv_update_state(struct server *srv, int version, char **params)
2894{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002895 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002896 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002897
2898 /* fields since version 1
2899 * and common to all other upcoming versions
2900 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002901 enum srv_state srv_op_state;
2902 enum srv_admin srv_admin_state;
2903 unsigned srv_uweight, srv_iweight;
2904 unsigned long srv_last_time_change;
2905 short srv_check_status;
2906 enum chk_result srv_check_result;
2907 int srv_check_health;
2908 int srv_check_state, srv_agent_state;
2909 int bk_f_forced_id;
2910 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002911 int fqdn_set_by_cli;
2912 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002913 const char *port_str;
2914 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002915 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002916
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002917 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002918 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002919 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002920 switch (version) {
2921 case 1:
2922 /*
2923 * now we can proceed with server's state update:
2924 * srv_addr: params[0]
2925 * srv_op_state: params[1]
2926 * srv_admin_state: params[2]
2927 * srv_uweight: params[3]
2928 * srv_iweight: params[4]
2929 * srv_last_time_change: params[5]
2930 * srv_check_status: params[6]
2931 * srv_check_result: params[7]
2932 * srv_check_health: params[8]
2933 * srv_check_state: params[9]
2934 * srv_agent_state: params[10]
2935 * bk_f_forced_id: params[11]
2936 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002937 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002938 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002939 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002940 */
2941
2942 /* validating srv_op_state */
2943 p = NULL;
2944 errno = 0;
2945 srv_op_state = strtol(params[1], &p, 10);
2946 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2947 (srv_op_state != SRV_ST_STOPPED &&
2948 srv_op_state != SRV_ST_STARTING &&
2949 srv_op_state != SRV_ST_RUNNING &&
2950 srv_op_state != SRV_ST_STOPPING)) {
2951 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2952 }
2953
2954 /* validating srv_admin_state */
2955 p = NULL;
2956 errno = 0;
2957 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002958 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002959
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002960 /* inherited statuses will be recomputed later.
2961 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2962 */
2963 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002964
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002965 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2966 (srv_admin_state != 0 &&
2967 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002968 srv_admin_state != SRV_ADMF_CMAINT &&
2969 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002970 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002971 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002972 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2973 }
2974
2975 /* validating srv_uweight */
2976 p = NULL;
2977 errno = 0;
2978 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002979 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002980 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2981
2982 /* validating srv_iweight */
2983 p = NULL;
2984 errno = 0;
2985 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002986 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002987 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2988
2989 /* validating srv_last_time_change */
2990 p = NULL;
2991 errno = 0;
2992 srv_last_time_change = strtol(params[5], &p, 10);
2993 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2994 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2995
2996 /* validating srv_check_status */
2997 p = NULL;
2998 errno = 0;
2999 srv_check_status = strtol(params[6], &p, 10);
3000 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3001 (srv_check_status >= HCHK_STATUS_SIZE))
3002 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3003
3004 /* validating srv_check_result */
3005 p = NULL;
3006 errno = 0;
3007 srv_check_result = strtol(params[7], &p, 10);
3008 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3009 (srv_check_result != CHK_RES_UNKNOWN &&
3010 srv_check_result != CHK_RES_NEUTRAL &&
3011 srv_check_result != CHK_RES_FAILED &&
3012 srv_check_result != CHK_RES_PASSED &&
3013 srv_check_result != CHK_RES_CONDPASS)) {
3014 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3015 }
3016
3017 /* validating srv_check_health */
3018 p = NULL;
3019 errno = 0;
3020 srv_check_health = strtol(params[8], &p, 10);
3021 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3022 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3023
3024 /* validating srv_check_state */
3025 p = NULL;
3026 errno = 0;
3027 srv_check_state = strtol(params[9], &p, 10);
3028 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3029 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3030 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3031
3032 /* validating srv_agent_state */
3033 p = NULL;
3034 errno = 0;
3035 srv_agent_state = strtol(params[10], &p, 10);
3036 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3037 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3038 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3039
3040 /* validating bk_f_forced_id */
3041 p = NULL;
3042 errno = 0;
3043 bk_f_forced_id = strtol(params[11], &p, 10);
3044 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3045 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3046
3047 /* validating srv_f_forced_id */
3048 p = NULL;
3049 errno = 0;
3050 srv_f_forced_id = strtol(params[12], &p, 10);
3051 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3052 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3053
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003054 /* validating srv_fqdn */
3055 fqdn = params[13];
3056 if (fqdn && *fqdn == '-')
3057 fqdn = NULL;
3058 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3059 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3060 fqdn = NULL;
3061 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003062
Frédéric Lécaille31694712017-08-01 08:47:19 +02003063 port_str = params[14];
3064 if (port_str) {
3065 port = strl2uic(port_str, strlen(port_str));
3066 if (port > USHRT_MAX) {
3067 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3068 port_str = NULL;
3069 }
3070 }
3071
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003072 /* SRV record
3073 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3074 */
3075 srvrecord = params[15];
3076 if (srvrecord && *srvrecord != '_')
3077 srvrecord = NULL;
3078
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003079 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003080 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003081 goto out;
3082
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003083 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003084 /* recover operational state and apply it to this server
3085 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003086 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003087 switch (srv_op_state) {
3088 case SRV_ST_STOPPED:
3089 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003090 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003091 break;
3092 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003093 /* If rise == 1 there is no STARTING state, let's switch to
3094 * RUNNING
3095 */
3096 if (srv->check.rise == 1) {
3097 srv->check.health = srv->check.rise + srv->check.fall - 1;
3098 srv_set_running(srv, "", NULL);
3099 break;
3100 }
3101 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3102 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003103 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003104 break;
3105 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003106 /* If fall == 1 there is no STOPPING state, let's switch to
3107 * STOPPED
3108 */
3109 if (srv->check.fall == 1) {
3110 srv->check.health = 0;
3111 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3112 break;
3113 }
3114 if (srv->check.health < srv->check.rise ||
3115 srv->check.health > srv->check.rise + srv->check.fall - 2)
3116 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003117 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003118 break;
3119 case SRV_ST_RUNNING:
3120 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003121 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003122 break;
3123 }
3124
3125 /* When applying server state, the following rules apply:
3126 * - in case of a configuration change, we apply the setting from the new
3127 * configuration, regardless of old running state
3128 * - if no configuration change, we apply old running state only if old running
3129 * state is different from new configuration state
3130 */
3131 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003132 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3133 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003134 srv_adm_set_maint(srv);
3135 else
3136 srv_adm_set_ready(srv);
3137 }
3138 /* configuration is the same, let's compate old running state and new conf state */
3139 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003140 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003141 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003142 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003143 srv_adm_set_ready(srv);
3144 }
3145 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003146 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003147 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003148 * (srv->iweight is the weight set up in configuration).
3149 * There are two possible reasons for FDRAIN to have been present :
3150 * - previous config weight was zero
3151 * - "set server b/s drain" was sent to the CLI
3152 *
3153 * In the first case, we simply want to drop this drain state
3154 * if the new weight is not zero anymore, meaning the administrator
3155 * has intentionally turned the weight back to a positive value to
3156 * enable the server again after an operation. In the second case,
3157 * the drain state was forced on the CLI regardless of the config's
3158 * weight so we don't want a change to the config weight to lose this
3159 * status. What this means is :
3160 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3161 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003162 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003163 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003164 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003165 }
3166
3167 srv->last_change = date.tv_sec - srv_last_time_change;
3168 srv->check.status = srv_check_status;
3169 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170
3171 /* Only case we want to apply is removing ENABLED flag which could have been
3172 * done by the "disable health" command over the stats socket
3173 */
3174 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3175 (srv_check_state & CHK_ST_CONFIGURED) &&
3176 !(srv_check_state & CHK_ST_ENABLED))
3177 srv->check.state &= ~CHK_ST_ENABLED;
3178
3179 /* Only case we want to apply is removing ENABLED flag which could have been
3180 * done by the "disable agent" command over the stats socket
3181 */
3182 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3183 (srv_agent_state & CHK_ST_CONFIGURED) &&
3184 !(srv_agent_state & CHK_ST_ENABLED))
3185 srv->agent.state &= ~CHK_ST_ENABLED;
3186
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003187 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3188 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003189 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003190 * It means that a configuration file change has precedence over a unix socket change
3191 * for server's weight
3192 *
3193 * by default, HAProxy applies the following weight when parsing the configuration
3194 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003195 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003196 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003197 srv->uweight = srv_uweight;
3198 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003199 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003200
Willy Tarreaue5a60682016-11-09 14:54:53 +01003201 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003202 if (strcmp(params[0], "-"))
3203 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003204
3205 if (fqdn && srv->hostname) {
3206 if (!strcmp(srv->hostname, fqdn)) {
3207 /* Here we reset the 'set from stats socket FQDN' flag
3208 * to support such transitions:
3209 * Let's say initial FQDN value is foo1 (in configuration file).
3210 * - FQDN changed from stats socket, from foo1 to foo2 value,
3211 * - FQDN changed again from file configuration (with the same previous value
3212 set from stats socket, from foo1 to foo2 value),
3213 * - reload for any other reason than a FQDN modification,
3214 * the configuration file FQDN matches the fqdn server state file value.
3215 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003216 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003217 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003218 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003219 }
3220 else {
3221 /* If the FDQN has been changed from stats socket,
3222 * apply fqdn state file value (which is the value set
3223 * from stats socket).
3224 */
3225 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003226 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003227 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003228 }
3229 }
3230 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003231 /* If all the conditions below are validated, this means
3232 * we're evaluating a server managed by SRV resolution
3233 */
3234 else if (fqdn && !srv->hostname && srvrecord) {
3235 int res;
3236
3237 /* we can't apply previous state if SRV record has changed */
3238 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3239 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);
3240 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3241 goto out;
3242 }
3243
3244 /* create or find a SRV resolution for this srv record */
3245 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3246 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3247 if (srv->srvrq == NULL) {
3248 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3249 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3250 goto out;
3251 }
3252
3253 /* prepare DNS resolution for this server */
3254 res = srv_prepare_for_resolution(srv, fqdn);
3255 if (res == -1) {
3256 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3257 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3258 goto out;
3259 }
3260
3261 /* configure check.port accordingly */
3262 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3263 !(srv->flags & SRV_F_CHECKPORT))
3264 srv->check.port = port;
3265
3266 /* Unset SRV_F_MAPPORTS for SRV records.
3267 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3268 * because no ports are provided in the configuration file.
3269 * This is because HAProxy will use the port found into the SRV record.
3270 */
3271 srv->flags &= ~SRV_F_MAPPORTS;
3272 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003273
Frédéric Lécaille31694712017-08-01 08:47:19 +02003274 if (port_str)
3275 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003276 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003277
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003278 break;
3279 default:
3280 chunk_appendf(msg, ", version '%d' not supported", version);
3281 }
3282
3283 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003284 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003285 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003286 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003287 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003288 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003289}
3290
3291/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3292 * For each proxy, it does the following:
3293 * - opens its server state file (either one or local one)
3294 * - read whole file, line by line
3295 * - analyse each line to check if it matches our current backend:
3296 * - backend name matches
3297 * - backend id matches if id is forced and name doesn't match
3298 * - if the server pointed by the line is found, then state is applied
3299 *
3300 * If the running backend uuid or id differs from the state file, then HAProxy reports
3301 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003302 *
3303 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003304 */
3305void apply_server_state(void)
3306{
3307 char *cur, *end;
3308 char mybuf[SRV_STATE_LINE_MAXLEN];
3309 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003310 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3311 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003312 int arg, srv_arg, version, diff;
3313 FILE *f;
3314 char *filepath;
3315 char globalfilepath[MAXPATHLEN + 1];
3316 char localfilepath[MAXPATHLEN + 1];
3317 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003318 struct proxy *curproxy, *bk;
3319 struct server *srv;
3320
3321 globalfilepathlen = 0;
3322 /* create the globalfilepath variable */
3323 if (global.server_state_file) {
3324 /* absolute path or no base directory provided */
3325 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3326 len = strlen(global.server_state_file);
3327 if (len > MAXPATHLEN) {
3328 globalfilepathlen = 0;
3329 goto globalfileerror;
3330 }
3331 memcpy(globalfilepath, global.server_state_file, len);
3332 globalfilepath[len] = '\0';
3333 globalfilepathlen = len;
3334 }
3335 else if (global.server_state_base) {
3336 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003337 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003338 globalfilepathlen = 0;
3339 goto globalfileerror;
3340 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003341 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003342 globalfilepath[len] = 0;
3343 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003344
3345 /* append a slash if needed */
3346 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3347 if (globalfilepathlen + 1 > MAXPATHLEN) {
3348 globalfilepathlen = 0;
3349 goto globalfileerror;
3350 }
3351 globalfilepath[globalfilepathlen++] = '/';
3352 }
3353
3354 len = strlen(global.server_state_file);
3355 if (globalfilepathlen + len > MAXPATHLEN) {
3356 globalfilepathlen = 0;
3357 goto globalfileerror;
3358 }
3359 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3360 globalfilepathlen += len;
3361 globalfilepath[globalfilepathlen++] = 0;
3362 }
3363 }
3364 globalfileerror:
3365 if (globalfilepathlen == 0)
3366 globalfilepath[0] = '\0';
3367
3368 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003369 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003370 /* servers are only in backends */
3371 if (!(curproxy->cap & PR_CAP_BE))
3372 continue;
3373 fileopenerr = 0;
3374 filepath = NULL;
3375
3376 /* search server state file path and name */
3377 switch (curproxy->load_server_state_from_file) {
3378 /* read servers state from global file */
3379 case PR_SRV_STATE_FILE_GLOBAL:
3380 /* there was an error while generating global server state file path */
3381 if (globalfilepathlen == 0)
3382 continue;
3383 filepath = globalfilepath;
3384 fileopenerr = 1;
3385 break;
3386 /* this backend has its own file */
3387 case PR_SRV_STATE_FILE_LOCAL:
3388 localfilepathlen = 0;
3389 localfilepath[0] = '\0';
3390 len = 0;
3391 /* create the localfilepath variable */
3392 /* absolute path or no base directory provided */
3393 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3394 len = strlen(curproxy->server_state_file_name);
3395 if (len > MAXPATHLEN) {
3396 localfilepathlen = 0;
3397 goto localfileerror;
3398 }
3399 memcpy(localfilepath, curproxy->server_state_file_name, len);
3400 localfilepath[len] = '\0';
3401 localfilepathlen = len;
3402 }
3403 else if (global.server_state_base) {
3404 len = strlen(global.server_state_base);
3405 localfilepathlen += len;
3406
3407 if (localfilepathlen > MAXPATHLEN) {
3408 localfilepathlen = 0;
3409 goto localfileerror;
3410 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003411 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003412 localfilepath[localfilepathlen] = 0;
3413
3414 /* append a slash if needed */
3415 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3416 if (localfilepathlen + 1 > MAXPATHLEN) {
3417 localfilepathlen = 0;
3418 goto localfileerror;
3419 }
3420 localfilepath[localfilepathlen++] = '/';
3421 }
3422
3423 len = strlen(curproxy->server_state_file_name);
3424 if (localfilepathlen + len > MAXPATHLEN) {
3425 localfilepathlen = 0;
3426 goto localfileerror;
3427 }
3428 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3429 localfilepathlen += len;
3430 localfilepath[localfilepathlen++] = 0;
3431 }
3432 filepath = localfilepath;
3433 localfileerror:
3434 if (localfilepathlen == 0)
3435 localfilepath[0] = '\0';
3436
3437 break;
3438 case PR_SRV_STATE_FILE_NONE:
3439 default:
3440 continue;
3441 }
3442
3443 /* preload global state file */
3444 errno = 0;
3445 f = fopen(filepath, "r");
3446 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003447 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003448 if (!f)
3449 continue;
3450
3451 mybuf[0] = '\0';
3452 mybuflen = 0;
3453 version = 0;
3454
3455 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003456 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003457 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003458 goto fileclose;
3459 }
3460
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003461 cur = mybuf;
3462 version = atoi(cur);
3463 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3464 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003465 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003466
3467 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3468 int bk_f_forced_id = 0;
3469 int check_id = 0;
3470 int check_name = 0;
3471
3472 mybuflen = strlen(mybuf);
3473 cur = mybuf;
3474 end = cur + mybuflen;
3475
3476 bk = NULL;
3477 srv = NULL;
3478
3479 /* we need at least one character */
3480 if (mybuflen == 0)
3481 continue;
3482
3483 /* ignore blank characters at the beginning of the line */
3484 while (isspace(*cur))
3485 ++cur;
3486
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003487 /* Ignore empty or commented lines */
3488 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003489 continue;
3490
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003491 /* truncated lines */
3492 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003493 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003494 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003495 }
3496
3497 /* Removes trailing '\n' */
3498 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003499
3500 /* we're now ready to move the line into *srv_params[] */
3501 params[0] = cur;
3502 arg = 1;
3503 srv_arg = 0;
3504 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3505 if (isspace(*cur)) {
3506 *cur = '\0';
3507 ++cur;
3508 while (isspace(*cur))
3509 ++cur;
3510 switch (version) {
3511 case 1:
3512 /*
3513 * srv_addr: params[4] => srv_params[0]
3514 * srv_op_state: params[5] => srv_params[1]
3515 * srv_admin_state: params[6] => srv_params[2]
3516 * srv_uweight: params[7] => srv_params[3]
3517 * srv_iweight: params[8] => srv_params[4]
3518 * srv_last_time_change: params[9] => srv_params[5]
3519 * srv_check_status: params[10] => srv_params[6]
3520 * srv_check_result: params[11] => srv_params[7]
3521 * srv_check_health: params[12] => srv_params[8]
3522 * srv_check_state: params[13] => srv_params[9]
3523 * srv_agent_state: params[14] => srv_params[10]
3524 * bk_f_forced_id: params[15] => srv_params[11]
3525 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003526 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003527 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003528 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003529 */
3530 if (arg >= 4) {
3531 srv_params[srv_arg] = cur;
3532 ++srv_arg;
3533 }
3534 break;
3535 }
3536
3537 params[arg] = cur;
3538 ++arg;
3539 }
3540 else {
3541 ++cur;
3542 }
3543 }
3544
3545 /* if line is incomplete line, then ignore it.
3546 * otherwise, update useful flags */
3547 switch (version) {
3548 case 1:
3549 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3550 continue;
3551 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3552 check_id = (atoi(params[0]) == curproxy->uuid);
3553 check_name = (strcmp(curproxy->id, params[1]) == 0);
3554 break;
3555 }
3556
3557 diff = 0;
3558 bk = curproxy;
3559
3560 /* if backend can't be found, let's continue */
3561 if (!check_id && !check_name)
3562 continue;
3563 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003564 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 +02003565 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3566 }
3567 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003568 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 +02003569 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3570 /* if name doesn't match, we still want to update curproxy if the backend id
3571 * was forced in previous the previous configuration */
3572 if (!bk_f_forced_id)
3573 continue;
3574 }
3575
3576 /* look for the server by its id: param[2] */
3577 /* else look for the server by its name: param[3] */
3578 diff = 0;
3579 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3580
3581 if (!srv) {
3582 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003583 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3584 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003585 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3586 params[3], params[2], params[0], params[1]);
3587 continue;
3588 }
3589 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003590 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 +02003591 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 +02003592 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003593 }
3594 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003595 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 +02003596 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 +02003597 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003598 }
3599
3600 /* now we can proceed with server's state update */
3601 srv_update_state(srv, version, srv_params);
3602 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003603fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003604 fclose(f);
3605 }
3606}
3607
Simon Horman7d09b9a2013-02-12 10:45:51 +09003608/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003609 * update a server's current IP address.
3610 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3611 * ip is in network format.
3612 * updater is a string which contains an information about the requester of the update.
3613 * updater is used if not NULL.
3614 *
3615 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003616 *
3617 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003618 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003619int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003620{
3621 /* generates a log line and a warning on stderr */
3622 if (1) {
3623 /* book enough space for both IPv4 and IPv6 */
3624 char oldip[INET6_ADDRSTRLEN];
3625 char newip[INET6_ADDRSTRLEN];
3626
3627 memset(oldip, '\0', INET6_ADDRSTRLEN);
3628 memset(newip, '\0', INET6_ADDRSTRLEN);
3629
3630 /* copy old IP address in a string */
3631 switch (s->addr.ss_family) {
3632 case AF_INET:
3633 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3634 break;
3635 case AF_INET6:
3636 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3637 break;
3638 };
3639
3640 /* copy new IP address in a string */
3641 switch (ip_sin_family) {
3642 case AF_INET:
3643 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3644 break;
3645 case AF_INET6:
3646 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3647 break;
3648 };
3649
3650 /* save log line into a buffer */
3651 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3652 s->proxy->id, s->id, oldip, newip, updater);
3653
3654 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003655 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003656
3657 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003658 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003659 }
3660
3661 /* save the new IP family */
3662 s->addr.ss_family = ip_sin_family;
3663 /* save the new IP address */
3664 switch (ip_sin_family) {
3665 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003666 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003667 break;
3668 case AF_INET6:
3669 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3670 break;
3671 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003672 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003673
3674 return 0;
3675}
3676
3677/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003678 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3679 *
3680 * Caller can pass its name through <updater> to get it integrated in the response message
3681 * returned by the function.
3682 *
3683 * The function first does the following, in that order:
3684 * - validates the new addr and/or port
3685 * - checks if an update is required (new IP or port is different than current ones)
3686 * - checks the update is allowed:
3687 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3688 * - allow all changes if no CHECKS are configured
3689 * - if CHECK is configured:
3690 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3691 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3692 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003693 *
3694 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003695 */
3696const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3697{
3698 struct sockaddr_storage sa;
3699 int ret, port_change_required;
3700 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003701 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003702 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003703 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003704
3705 msg = get_trash_chunk();
3706 chunk_reset(msg);
3707
3708 if (addr) {
3709 memset(&sa, 0, sizeof(struct sockaddr_storage));
3710 if (str2ip2(addr, &sa, 0) == NULL) {
3711 chunk_printf(msg, "Invalid addr '%s'", addr);
3712 goto out;
3713 }
3714
3715 /* changes are allowed on AF_INET* families only */
3716 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3717 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3718 goto out;
3719 }
3720
3721 /* collecting data currently setup */
3722 memset(current_addr, '\0', sizeof(current_addr));
3723 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3724 /* changes are allowed on AF_INET* families only */
3725 if ((ret != AF_INET) && (ret != AF_INET6)) {
3726 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3727 goto out;
3728 }
3729
3730 /* applying ADDR changes if required and allowed
3731 * ipcmp returns 0 when both ADDR are the same
3732 */
3733 if (ipcmp(&s->addr, &sa) == 0) {
3734 chunk_appendf(msg, "no need to change the addr");
3735 goto port;
3736 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003737 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003738 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003739
3740 /* we also need to update check's ADDR only if it uses the server's one */
3741 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003742 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003743 }
3744
3745 /* we also need to update agent ADDR only if it use the server's one */
3746 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003747 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003748 }
3749
3750 /* update report for caller */
3751 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3752 }
3753
3754 port:
3755 if (port) {
3756 char sign = '\0';
3757 char *endptr;
3758
3759 if (addr)
3760 chunk_appendf(msg, ", ");
3761
3762 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003763 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003764
3765 /* check if PORT change is required */
3766 port_change_required = 0;
3767
3768 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003769 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003770 new_port = strtol(port, &endptr, 10);
3771 if ((errno != 0) || (port == endptr)) {
3772 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3773 goto out;
3774 }
3775
3776 /* check if caller triggers a port mapped or offset */
3777 if (sign == '-' || (sign == '+')) {
3778 /* check if server currently uses port map */
3779 if (!(s->flags & SRV_F_MAPPORTS)) {
3780 /* switch from fixed port to port map mandatorily triggers
3781 * a port change */
3782 port_change_required = 1;
3783 /* check is configured
3784 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3785 * prevent PORT change if check doesn't have it's dedicated port while switching
3786 * to port mapping */
3787 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3788 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.");
3789 goto out;
3790 }
3791 }
3792 /* we're already using port maps */
3793 else {
3794 port_change_required = current_port != new_port;
3795 }
3796 }
3797 /* fixed port */
3798 else {
3799 port_change_required = current_port != new_port;
3800 }
3801
3802 /* applying PORT changes if required and update response message */
3803 if (port_change_required) {
3804 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003805 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003806 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003807
3808 /* prepare message */
3809 chunk_appendf(msg, "port changed from '");
3810 if (s->flags & SRV_F_MAPPORTS)
3811 chunk_appendf(msg, "+");
3812 chunk_appendf(msg, "%d' to '", current_port);
3813
3814 if (sign == '-') {
3815 s->flags |= SRV_F_MAPPORTS;
3816 chunk_appendf(msg, "%c", sign);
3817 /* just use for result output */
3818 new_port = -new_port;
3819 }
3820 else if (sign == '+') {
3821 s->flags |= SRV_F_MAPPORTS;
3822 chunk_appendf(msg, "%c", sign);
3823 }
3824 else {
3825 s->flags &= ~SRV_F_MAPPORTS;
3826 }
3827
3828 chunk_appendf(msg, "%d'", new_port);
3829
3830 /* we also need to update health checks port only if it uses server's realport */
3831 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3832 s->check.port = new_port;
3833 }
3834 }
3835 else {
3836 chunk_appendf(msg, "no need to change the port");
3837 }
3838 }
3839
3840out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003841 if (changed)
3842 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003843 if (updater)
3844 chunk_appendf(msg, " by '%s'", updater);
3845 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003846 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003847}
3848
3849
3850/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003851 * update server status based on result of name resolution
3852 * returns:
3853 * 0 if server status is updated
3854 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003855 *
3856 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003857 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003858int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003859{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003860 struct dns_resolvers *resolvers = s->resolvers;
3861 struct dns_resolution *resolution = s->dns_requester->resolution;
3862 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003863
3864 switch (resolution->status) {
3865 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003866 /* status when HAProxy has just (re)started.
3867 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003868 break;
3869
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003870 case RSLV_STATUS_VALID:
3871 /*
3872 * resume health checks
3873 * server will be turned back on if health check is safe
3874 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003875 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003876 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003877 return 1;
3878 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3879 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003880 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003881 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003882
Emeric Brun52a91d32017-08-31 14:41:55 +02003883 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003884 return 1;
3885 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3886 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3887 s->proxy->id, s->id);
3888
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003889 ha_warning("%s.\n", trash.area);
3890 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003891 return 0;
3892
3893 case RSLV_STATUS_NX:
3894 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003895 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3896 if (!tick_is_expired(exp, now_ms))
3897 break;
3898
3899 if (s->next_admin & SRV_ADMF_RMAINT)
3900 return 1;
3901 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3902 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003903
3904 case RSLV_STATUS_TIMEOUT:
3905 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003906 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3907 if (!tick_is_expired(exp, now_ms))
3908 break;
3909
3910 if (s->next_admin & SRV_ADMF_RMAINT)
3911 return 1;
3912 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3913 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003914
3915 case RSLV_STATUS_REFUSED:
3916 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003917 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3918 if (!tick_is_expired(exp, now_ms))
3919 break;
3920
3921 if (s->next_admin & SRV_ADMF_RMAINT)
3922 return 1;
3923 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3924 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003925
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003927 /* stop server if resolution failed for a long enough period */
3928 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3929 if (!tick_is_expired(exp, now_ms))
3930 break;
3931
3932 if (s->next_admin & SRV_ADMF_RMAINT)
3933 return 1;
3934 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3935 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003936 }
3937
3938 return 1;
3939}
3940
3941/*
3942 * Server Name Resolution valid response callback
3943 * It expects:
3944 * - <nameserver>: the name server which answered the valid response
3945 * - <response>: buffer containing a valid DNS response
3946 * - <response_len>: size of <response>
3947 * It performs the following actions:
3948 * - ignore response if current ip found and server family not met
3949 * - update with first new ip found if family is met and current IP is not found
3950 * returns:
3951 * 0 on error
3952 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003953 *
3954 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003955 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003956int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003957{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003958 struct server *s = NULL;
3959 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003960 void *serverip, *firstip;
3961 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003962 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003963 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003964 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003965
Christopher Faulet67957bd2017-09-27 11:00:59 +02003966 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003967 if (!s)
3968 return 1;
3969
Christopher Faulet67957bd2017-09-27 11:00:59 +02003970 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003971
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003972 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003973 firstip = NULL; /* pointer to the first valid response found */
3974 /* it will be used as the new IP if a change is required */
3975 firstip_sin_family = AF_UNSPEC;
3976 serverip = NULL; /* current server IP address */
3977
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003978 /* initializing server IP pointer */
3979 server_sin_family = s->addr.ss_family;
3980 switch (server_sin_family) {
3981 case AF_INET:
3982 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3983 break;
3984
3985 case AF_INET6:
3986 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3987 break;
3988
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003989 case AF_UNSPEC:
3990 break;
3991
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003992 default:
3993 goto invalid;
3994 }
3995
Baptiste Assmann729c9012017-05-22 15:13:10 +02003996 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003997 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003998 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003999
4000 switch (ret) {
4001 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004002 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004003
4004 case DNS_UPD_SRVIP_NOT_FOUND:
4005 goto save_ip;
4006
4007 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004008 goto invalid;
4009
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004010 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004011 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004012 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004013
Baptiste Assmannfad03182015-10-28 02:03:32 +01004014 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004015 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004016 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004017 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004018
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004019 default:
4020 goto invalid;
4021
4022 }
4023
4024 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004025 if (nameserver) {
4026 nameserver->counters.update++;
4027 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004028 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004029 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004030 else
4031 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004032 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004033
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004034 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004035 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004036 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004037
4038 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004039 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004040 nameserver->counters.invalid++;
4041 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004042 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004043 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004044 return 0;
4045}
4046
4047/*
4048 * Server Name Resolution error management callback
4049 * returns:
4050 * 0 on error
4051 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004052 *
4053 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004054 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004055int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004056{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004057 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004058
Christopher Faulet67957bd2017-09-27 11:00:59 +02004059 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004060 if (!s)
4061 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004062 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004063 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004064 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004065 return 1;
4066}
4067
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004068/*
4069 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004070 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004071 * It returns a pointer to the first server found or NULL if <ip> is not yet
4072 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004073 *
4074 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004075 */
4076struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4077{
4078 struct server *tmpsrv;
4079 struct proxy *be;
4080
4081 if (!srv)
4082 return NULL;
4083
4084 be = srv->proxy;
4085 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004086 /* we found the current server is the same, ignore it */
4087 if (srv == tmpsrv)
4088 continue;
4089
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004090 /* We want to compare the IP in the record with the IP of the servers in the
4091 * same backend, only if:
4092 * * DNS resolution is enabled on the server
4093 * * the hostname used for the resolution by our server is the same than the
4094 * one used for the server found in the backend
4095 * * the server found in the backend is not our current server
4096 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004097 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004098 if ((tmpsrv->hostname_dn == NULL) ||
4099 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4100 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004101 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004102 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004103 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004104 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004105
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004106 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004107 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004108 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004109 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004110 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004111
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004112 /* At this point, we have 2 different servers using the same DNS hostname
4113 * for their respective resolution.
4114 */
4115 if (*ip_family == tmpsrv->addr.ss_family &&
4116 ((tmpsrv->addr.ss_family == AF_INET &&
4117 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4118 (tmpsrv->addr.ss_family == AF_INET6 &&
4119 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004120 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004121 return tmpsrv;
4122 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004123 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004124 }
4125
Emeric Brune9fd6b52017-11-02 17:20:39 +01004126
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004127 return NULL;
4128}
4129
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004130/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4131 * resolver. This is suited for initial address configuration. Returns 0 on
4132 * success otherwise a non-zero error code. In case of error, *err_code, if
4133 * not NULL, is filled up.
4134 */
4135int srv_set_addr_via_libc(struct server *srv, int *err_code)
4136{
4137 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004138 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004139 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004140 return 1;
4141 }
4142 return 0;
4143}
4144
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004145/* Set the server's FDQN (->hostname) from <hostname>.
4146 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004147 *
4148 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004149 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004150int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004151{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004152 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004153 char *hostname_dn;
4154 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004155
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004156 /* Note that the server lock is already held. */
4157 if (!srv->resolvers)
4158 return -1;
4159
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004160 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004161 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004162 /* run time DNS resolution was not active for this server
4163 * and we can't enable it at run time for now.
4164 */
4165 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004166 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004167
4168 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004169 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004170 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004171 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4172 hostname_dn, trash.size);
4173 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004174 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004175
Christopher Faulet67957bd2017-09-27 11:00:59 +02004176 resolution = srv->dns_requester->resolution;
4177 if (resolution &&
4178 resolution->hostname_dn &&
4179 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004180 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004181
Christopher Faulet67957bd2017-09-27 11:00:59 +02004182 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004183
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004184 free(srv->hostname);
4185 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004186 srv->hostname = strdup(hostname);
4187 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004188 srv->hostname_dn_len = hostname_dn_len;
4189 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004190 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004191
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004192 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004193 goto err;
4194
4195 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004196 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004197 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004198 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004199
4200 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004201 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004202 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004203 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004204}
4205
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004206/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4207 * from the state file. This is suited for initial address configuration.
4208 * Returns 0 on success otherwise a non-zero error code. In case of error,
4209 * *err_code, if not NULL, is filled up.
4210 */
4211static int srv_apply_lastaddr(struct server *srv, int *err_code)
4212{
4213 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4214 if (err_code)
4215 *err_code |= ERR_WARN;
4216 return 1;
4217 }
4218 return 0;
4219}
4220
Willy Tarreau25e51522016-11-04 15:10:17 +01004221/* returns 0 if no error, otherwise a combination of ERR_* flags */
4222static int srv_iterate_initaddr(struct server *srv)
4223{
4224 int return_code = 0;
4225 int err_code;
4226 unsigned int methods;
4227
4228 methods = srv->init_addr_methods;
4229 if (!methods) { // default to "last,libc"
4230 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4231 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4232 }
4233
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004234 /* "-dr" : always append "none" so that server addresses resolution
4235 * failures are silently ignored, this is convenient to validate some
4236 * configs out of their environment.
4237 */
4238 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4239 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4240
Willy Tarreau25e51522016-11-04 15:10:17 +01004241 while (methods) {
4242 err_code = 0;
4243 switch (srv_get_next_initaddr(&methods)) {
4244 case SRV_IADDR_LAST:
4245 if (!srv->lastaddr)
4246 continue;
4247 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004248 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004249 return_code |= err_code;
4250 break;
4251
4252 case SRV_IADDR_LIBC:
4253 if (!srv->hostname)
4254 continue;
4255 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004256 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004257 return_code |= err_code;
4258 break;
4259
Willy Tarreau37ebe122016-11-04 15:17:58 +01004260 case SRV_IADDR_NONE:
4261 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004262 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004263 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4264 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004265 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004266 return return_code;
4267
Willy Tarreau4310d362016-11-02 15:05:56 +01004268 case SRV_IADDR_IP:
4269 ipcpy(&srv->init_addr, &srv->addr);
4270 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004271 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4272 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004273 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004274 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004275
Willy Tarreau25e51522016-11-04 15:10:17 +01004276 default: /* unhandled method */
4277 break;
4278 }
4279 }
4280
4281 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004282 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004283 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4284 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004285 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004286 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004287 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4288 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004289
4290 return_code |= ERR_ALERT | ERR_FATAL;
4291 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004292out:
4293 srv_set_dyncookie(srv);
4294 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004295}
4296
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004297/*
4298 * This function parses all backends and all servers within each backend
4299 * and performs servers' addr resolution based on information provided by:
4300 * - configuration file
4301 * - server-state file (states provided by an 'old' haproxy process)
4302 *
4303 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4304 */
4305int srv_init_addr(void)
4306{
4307 struct proxy *curproxy;
4308 int return_code = 0;
4309
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004310 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004311 while (curproxy) {
4312 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004313
4314 /* servers are in backend only */
4315 if (!(curproxy->cap & PR_CAP_BE))
4316 goto srv_init_addr_next;
4317
Willy Tarreau25e51522016-11-04 15:10:17 +01004318 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004319 if (srv->hostname)
4320 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004321
4322 srv_init_addr_next:
4323 curproxy = curproxy->next;
4324 }
4325
4326 return return_code;
4327}
4328
Willy Tarreau46b7f532018-08-21 11:54:26 +02004329/*
4330 * Must be called with the server lock held.
4331 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004332const 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 +02004333{
4334
Willy Tarreau83061a82018-07-13 11:56:34 +02004335 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004336
4337 msg = get_trash_chunk();
4338 chunk_reset(msg);
4339
Olivier Houchard8da5f982017-08-04 18:35:36 +02004340 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004341 chunk_appendf(msg, "no need to change the FDQN");
4342 goto out;
4343 }
4344
4345 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4346 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4347 goto out;
4348 }
4349
4350 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4351 server->proxy->id, server->id, server->hostname, fqdn);
4352
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004353 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004354 chunk_reset(msg);
4355 chunk_appendf(msg, "could not update %s/%s FQDN",
4356 server->proxy->id, server->id);
4357 goto out;
4358 }
4359
4360 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004361 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004362
4363 out:
4364 if (updater)
4365 chunk_appendf(msg, " by '%s'", updater);
4366 chunk_appendf(msg, "\n");
4367
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004368 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004369}
4370
4371
Willy Tarreau21b069d2016-11-23 17:15:08 +01004372/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4373 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004374 * 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 +01004375 * used for CLI commands requiring a server name.
4376 * Important: the <arg> is modified to remove the '/'.
4377 */
4378struct server *cli_find_server(struct appctx *appctx, char *arg)
4379{
4380 struct proxy *px;
4381 struct server *sv;
4382 char *line;
4383
4384 /* split "backend/server" and make <line> point to server */
4385 for (line = arg; *line; line++)
4386 if (*line == '/') {
4387 *line++ = '\0';
4388 break;
4389 }
4390
4391 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004392 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004393 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004394 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004395 return NULL;
4396 }
4397
4398 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004399 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004400 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004401 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004402 return NULL;
4403 }
4404
4405 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004406 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004407 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004408 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004409 return NULL;
4410 }
4411
4412 return sv;
4413}
4414
William Lallemand222baf22016-11-19 02:00:33 +01004415
Willy Tarreau46b7f532018-08-21 11:54:26 +02004416/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004417static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004418{
4419 struct server *sv;
4420 const char *warning;
4421
4422 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4423 return 1;
4424
4425 sv = cli_find_server(appctx, args[2]);
4426 if (!sv)
4427 return 1;
4428
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004429 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004430
William Lallemand222baf22016-11-19 02:00:33 +01004431 if (strcmp(args[3], "weight") == 0) {
4432 warning = server_parse_weight_change_request(sv, args[4]);
4433 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004434 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004435 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004436 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004437 }
4438 }
4439 else if (strcmp(args[3], "state") == 0) {
4440 if (strcmp(args[4], "ready") == 0)
4441 srv_adm_set_ready(sv);
4442 else if (strcmp(args[4], "drain") == 0)
4443 srv_adm_set_drain(sv);
4444 else if (strcmp(args[4], "maint") == 0)
4445 srv_adm_set_maint(sv);
4446 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004447 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004448 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004449 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004450 }
4451 }
4452 else if (strcmp(args[3], "health") == 0) {
4453 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004454 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004455 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004456 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004457 }
4458 else if (strcmp(args[4], "up") == 0) {
4459 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004460 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004461 }
4462 else if (strcmp(args[4], "stopping") == 0) {
4463 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004464 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004465 }
4466 else if (strcmp(args[4], "down") == 0) {
4467 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004468 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004469 }
4470 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004471 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004472 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004473 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004474 }
4475 }
4476 else if (strcmp(args[3], "agent") == 0) {
4477 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004478 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004479 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004480 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004481 }
4482 else if (strcmp(args[4], "up") == 0) {
4483 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004484 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004485 }
4486 else if (strcmp(args[4], "down") == 0) {
4487 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004488 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004489 }
4490 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004491 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004492 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004493 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004494 }
4495 }
Misiek2da082d2017-01-09 09:40:42 +01004496 else if (strcmp(args[3], "agent-addr") == 0) {
4497 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004498 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004499 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4500 appctx->st0 = CLI_ST_PRINT;
4501 } else {
4502 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004503 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004504 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4505 appctx->st0 = CLI_ST_PRINT;
4506 }
4507 }
4508 }
4509 else if (strcmp(args[3], "agent-send") == 0) {
4510 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004511 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004512 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4513 appctx->st0 = CLI_ST_PRINT;
4514 } else {
4515 char *nss = strdup(args[4]);
4516 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004517 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004518 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4519 appctx->st0 = CLI_ST_PRINT;
4520 } else {
4521 free(sv->agent.send_string);
4522 sv->agent.send_string = nss;
4523 sv->agent.send_string_len = strlen(args[4]);
4524 }
4525 }
4526 }
William Lallemand222baf22016-11-19 02:00:33 +01004527 else if (strcmp(args[3], "check-port") == 0) {
4528 int i = 0;
4529 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004530 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004531 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004532 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004533 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004534 }
4535 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004536 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004537 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004538 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004539 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004540 }
4541 /* prevent the update of port to 0 if MAPPORTS are in use */
4542 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004543 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004544 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004545 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004546 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004547 }
4548 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004549 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004550 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004551 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004552 }
4553 else if (strcmp(args[3], "addr") == 0) {
4554 char *addr = NULL;
4555 char *port = NULL;
4556 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004557 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004558 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004559 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004560 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004561 }
4562 else {
4563 addr = args[4];
4564 }
4565 if (strcmp(args[5], "port") == 0) {
4566 port = args[6];
4567 }
4568 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4569 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004570 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004571 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004572 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004573 }
4574 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4575 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004576 else if (strcmp(args[3], "fqdn") == 0) {
4577 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004578 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004579 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4580 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004581 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004582 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004583 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004584 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004585 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004586 appctx->ctx.cli.msg = warning;
4587 appctx->st0 = CLI_ST_PRINT;
4588 }
4589 }
William Lallemand222baf22016-11-19 02:00:33 +01004590 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004591 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004592 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 +01004593 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004594 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004595 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004596 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004597 return 1;
4598}
4599
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004600static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004601{
4602 struct stream_interface *si = appctx->owner;
4603 struct proxy *px;
4604 struct server *sv;
4605 char *line;
4606
4607
4608 /* split "backend/server" and make <line> point to server */
4609 for (line = args[2]; *line; line++)
4610 if (*line == '/') {
4611 *line++ = '\0';
4612 break;
4613 }
4614
4615 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004616 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004617 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004618 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004619 return 1;
4620 }
4621
4622 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004623 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004624 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004625 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004626 return 1;
4627 }
4628
4629 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004630 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4631 sv->iweight);
4632 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004633 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004634 return 0;
4635 }
William Lallemand6b160942016-11-22 12:34:35 +01004636 return 1;
4637}
4638
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004639/* Parse a "set weight" command.
4640 *
4641 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004642 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004643static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004644{
4645 struct server *sv;
4646 const char *warning;
4647
4648 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4649 return 1;
4650
4651 sv = cli_find_server(appctx, args[2]);
4652 if (!sv)
4653 return 1;
4654
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004655 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4656
William Lallemand6b160942016-11-22 12:34:35 +01004657 warning = server_parse_weight_change_request(sv, args[3]);
4658 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004659 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004660 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004661 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004662 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004663
4664 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4665
William Lallemand6b160942016-11-22 12:34:35 +01004666 return 1;
4667}
4668
Willy Tarreau46b7f532018-08-21 11:54:26 +02004669/* parse a "set maxconn server" command. It always returns 1.
4670 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004671 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004672 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004673static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004674{
4675 struct server *sv;
4676 const char *warning;
4677
4678 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4679 return 1;
4680
4681 sv = cli_find_server(appctx, args[3]);
4682 if (!sv)
4683 return 1;
4684
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004685 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4686
Willy Tarreaub8026272016-11-23 11:26:56 +01004687 warning = server_parse_maxconn_change_request(sv, args[4]);
4688 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004689 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004690 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004691 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004692 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004693
4694 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4695
Willy Tarreaub8026272016-11-23 11:26:56 +01004696 return 1;
4697}
William Lallemand6b160942016-11-22 12:34:35 +01004698
Willy Tarreau46b7f532018-08-21 11:54:26 +02004699/* parse a "disable agent" command. It always returns 1.
4700 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004701 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004702 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004703static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004704{
4705 struct server *sv;
4706
4707 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4708 return 1;
4709
4710 sv = cli_find_server(appctx, args[2]);
4711 if (!sv)
4712 return 1;
4713
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004714 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004715 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004716 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004717 return 1;
4718}
4719
Willy Tarreau46b7f532018-08-21 11:54:26 +02004720/* parse a "disable health" command. It always returns 1.
4721 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004722 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004723 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004724static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004725{
4726 struct server *sv;
4727
4728 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4729 return 1;
4730
4731 sv = cli_find_server(appctx, args[2]);
4732 if (!sv)
4733 return 1;
4734
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004735 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004736 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004737 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004738 return 1;
4739}
4740
Willy Tarreau46b7f532018-08-21 11:54:26 +02004741/* parse a "disable server" command. It always returns 1.
4742 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004743 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004744 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004745static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004746{
4747 struct server *sv;
4748
4749 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4750 return 1;
4751
4752 sv = cli_find_server(appctx, args[2]);
4753 if (!sv)
4754 return 1;
4755
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004756 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004757 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004758 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004759 return 1;
4760}
4761
Willy Tarreau46b7f532018-08-21 11:54:26 +02004762/* parse a "enable agent" command. It always returns 1.
4763 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004764 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004765 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004766static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004767{
4768 struct server *sv;
4769
4770 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4771 return 1;
4772
4773 sv = cli_find_server(appctx, args[2]);
4774 if (!sv)
4775 return 1;
4776
4777 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004778 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004779 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004780 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004781 return 1;
4782 }
4783
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004784 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004785 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004786 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004787 return 1;
4788}
4789
Willy Tarreau46b7f532018-08-21 11:54:26 +02004790/* parse a "enable health" command. It always returns 1.
4791 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004792 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004793 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004794static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004795{
4796 struct server *sv;
4797
4798 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4799 return 1;
4800
4801 sv = cli_find_server(appctx, args[2]);
4802 if (!sv)
4803 return 1;
4804
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004805 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004806 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004807 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004808 return 1;
4809}
4810
Willy Tarreau46b7f532018-08-21 11:54:26 +02004811/* parse a "enable server" command. It always returns 1.
4812 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004813 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004814 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004815static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004816{
4817 struct server *sv;
4818
4819 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4820 return 1;
4821
4822 sv = cli_find_server(appctx, args[2]);
4823 if (!sv)
4824 return 1;
4825
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004826 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004827 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004828 if (!(sv->flags & SRV_F_COOKIESET)
4829 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4830 sv->cookie)
4831 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004832 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004833 return 1;
4834}
4835
William Lallemand222baf22016-11-19 02:00:33 +01004836/* register cli keywords */
4837static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004838 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004839 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004840 { { "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 +01004841 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004842 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004843 { { "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 +01004844 { { "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 +01004845 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004846 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4847 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4848
William Lallemand222baf22016-11-19 02:00:33 +01004849 {{},}
4850}};
4851
Willy Tarreau0108d902018-11-25 19:14:37 +01004852INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004853
Emeric Brun64cc49c2017-10-03 14:46:45 +02004854/*
4855 * This function applies server's status changes, it is
4856 * is designed to be called asynchronously.
4857 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004858 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004859 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004860static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004861{
4862 struct check *check = &s->check;
4863 int xferred;
4864 struct proxy *px = s->proxy;
4865 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4866 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4867 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004868 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004869
Emeric Brun64cc49c2017-10-03 14:46:45 +02004870 /* If currently main is not set we try to apply pending state changes */
4871 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4872 int next_admin;
4873
4874 /* Backup next admin */
4875 next_admin = s->next_admin;
4876
4877 /* restore current admin state */
4878 s->next_admin = s->cur_admin;
4879
4880 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4881 s->last_change = now.tv_sec;
4882 if (s->proxy->lbprm.set_server_status_down)
4883 s->proxy->lbprm.set_server_status_down(s);
4884
4885 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4886 srv_shutdown_streams(s, SF_ERR_DOWN);
4887
4888 /* we might have streams queued on this server and waiting for
4889 * a connection. Those which are redispatchable will be queued
4890 * to another server or to the proxy itself.
4891 */
4892 xferred = pendconn_redistribute(s);
4893
4894 tmptrash = alloc_trash_chunk();
4895 if (tmptrash) {
4896 chunk_printf(tmptrash,
4897 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4898 s->proxy->id, s->id);
4899
Emeric Brun5a133512017-10-19 14:42:30 +02004900 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004901 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004902
4903 /* we don't send an alert if the server was previously paused */
4904 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004905 send_log(s->proxy, log_level, "%s.\n",
4906 tmptrash->area);
4907 send_email_alert(s, log_level, "%s",
4908 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004909 free_trash_chunk(tmptrash);
4910 tmptrash = NULL;
4911 }
4912 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4913 set_backend_down(s->proxy);
4914
4915 s->counters.down_trans++;
4916 }
4917 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4918 s->last_change = now.tv_sec;
4919 if (s->proxy->lbprm.set_server_status_down)
4920 s->proxy->lbprm.set_server_status_down(s);
4921
4922 /* we might have streams queued on this server and waiting for
4923 * a connection. Those which are redispatchable will be queued
4924 * to another server or to the proxy itself.
4925 */
4926 xferred = pendconn_redistribute(s);
4927
4928 tmptrash = alloc_trash_chunk();
4929 if (tmptrash) {
4930 chunk_printf(tmptrash,
4931 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4932 s->proxy->id, s->id);
4933
Emeric Brun5a133512017-10-19 14:42:30 +02004934 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004935
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004936 ha_warning("%s.\n", tmptrash->area);
4937 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4938 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004939 free_trash_chunk(tmptrash);
4940 tmptrash = NULL;
4941 }
4942
4943 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4944 set_backend_down(s->proxy);
4945 }
4946 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4947 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4948 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4949 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4950 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4951 s->proxy->last_change = now.tv_sec;
4952 }
4953
4954 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4955 s->down_time += now.tv_sec - s->last_change;
4956
4957 s->last_change = now.tv_sec;
4958 if (s->next_state == SRV_ST_STARTING)
4959 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4960
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004961 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004962 /* now propagate the status change to any LB algorithms */
4963 if (px->lbprm.update_server_eweight)
4964 px->lbprm.update_server_eweight(s);
4965 else if (srv_willbe_usable(s)) {
4966 if (px->lbprm.set_server_status_up)
4967 px->lbprm.set_server_status_up(s);
4968 }
4969 else {
4970 if (px->lbprm.set_server_status_down)
4971 px->lbprm.set_server_status_down(s);
4972 }
4973
4974 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4975 * and it's not a backup server and its effective weight is > 0,
4976 * then it can accept new connections, so we shut down all streams
4977 * on all backup servers.
4978 */
4979 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4980 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4981 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4982
4983 /* check if we can handle some connections queued at the proxy. We
4984 * will take as many as we can handle.
4985 */
4986 xferred = pendconn_grab_from_px(s);
4987
4988 tmptrash = alloc_trash_chunk();
4989 if (tmptrash) {
4990 chunk_printf(tmptrash,
4991 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4992 s->proxy->id, s->id);
4993
Emeric Brun5a133512017-10-19 14:42:30 +02004994 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004995 ha_warning("%s.\n", tmptrash->area);
4996 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4997 tmptrash->area);
4998 send_email_alert(s, LOG_NOTICE, "%s",
4999 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005000 free_trash_chunk(tmptrash);
5001 tmptrash = NULL;
5002 }
5003
5004 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5005 set_backend_down(s->proxy);
5006 }
5007 else if (s->cur_eweight != s->next_eweight) {
5008 /* now propagate the status change to any LB algorithms */
5009 if (px->lbprm.update_server_eweight)
5010 px->lbprm.update_server_eweight(s);
5011 else if (srv_willbe_usable(s)) {
5012 if (px->lbprm.set_server_status_up)
5013 px->lbprm.set_server_status_up(s);
5014 }
5015 else {
5016 if (px->lbprm.set_server_status_down)
5017 px->lbprm.set_server_status_down(s);
5018 }
5019
5020 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5021 set_backend_down(s->proxy);
5022 }
5023
5024 s->next_admin = next_admin;
5025 }
5026
Emeric Brun5a133512017-10-19 14:42:30 +02005027 /* reset operational state change */
5028 *s->op_st_chg.reason = 0;
5029 s->op_st_chg.status = s->op_st_chg.code = -1;
5030 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005031
5032 /* Now we try to apply pending admin changes */
5033
5034 /* Maintenance must also disable health checks */
5035 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5036 if (s->check.state & CHK_ST_ENABLED) {
5037 s->check.state |= CHK_ST_PAUSED;
5038 check->health = 0;
5039 }
5040
5041 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005042 tmptrash = alloc_trash_chunk();
5043 if (tmptrash) {
5044 chunk_printf(tmptrash,
5045 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5046 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5047 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005048
Olivier Houchard796a2b32017-10-24 17:42:47 +02005049 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005050
Olivier Houchard796a2b32017-10-24 17:42:47 +02005051 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005052 ha_warning("%s.\n", tmptrash->area);
5053 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5054 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005055 }
5056 free_trash_chunk(tmptrash);
5057 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005058 }
Emeric Brun8f298292017-12-06 16:47:17 +01005059 /* commit new admin status */
5060
5061 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005062 }
5063 else { /* server was still running */
5064 check->health = 0; /* failure */
5065 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005066
5067 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005068 if (s->proxy->lbprm.set_server_status_down)
5069 s->proxy->lbprm.set_server_status_down(s);
5070
Emeric Brun64cc49c2017-10-03 14:46:45 +02005071 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5072 srv_shutdown_streams(s, SF_ERR_DOWN);
5073
5074 /* we might have streams queued on this server and waiting for
5075 * a connection. Those which are redispatchable will be queued
5076 * to another server or to the proxy itself.
5077 */
5078 xferred = pendconn_redistribute(s);
5079
5080 tmptrash = alloc_trash_chunk();
5081 if (tmptrash) {
5082 chunk_printf(tmptrash,
5083 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5084 s->flags & SRV_F_BACKUP ? "Backup " : "",
5085 s->proxy->id, s->id,
5086 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5087
5088 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5089
5090 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005091 ha_warning("%s.\n", tmptrash->area);
5092 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5093 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005094 }
5095 free_trash_chunk(tmptrash);
5096 tmptrash = NULL;
5097 }
5098 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5099 set_backend_down(s->proxy);
5100
5101 s->counters.down_trans++;
5102 }
5103 }
5104 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5105 /* OK here we're leaving maintenance, we have many things to check,
5106 * because the server might possibly be coming back up depending on
5107 * its state. In practice, leaving maintenance means that we should
5108 * immediately turn to UP (more or less the slowstart) under the
5109 * following conditions :
5110 * - server is neither checked nor tracked
5111 * - server tracks another server which is not checked
5112 * - server tracks another server which is already up
5113 * Which sums up as something simpler :
5114 * "either the tracking server is up or the server's checks are disabled
5115 * or up". Otherwise we only re-enable health checks. There's a special
5116 * case associated to the stopping state which can be inherited. Note
5117 * that the server might still be in drain mode, which is naturally dealt
5118 * with by the lower level functions.
5119 */
5120
5121 if (s->check.state & CHK_ST_ENABLED) {
5122 s->check.state &= ~CHK_ST_PAUSED;
5123 check->health = check->rise; /* start OK but check immediately */
5124 }
5125
5126 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5127 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5128 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5129 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5130 s->next_state = SRV_ST_STOPPING;
5131 }
5132 else {
5133 s->next_state = SRV_ST_STARTING;
5134 if (s->slowstart > 0)
5135 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5136 else
5137 s->next_state = SRV_ST_RUNNING;
5138 }
5139
5140 }
5141
5142 tmptrash = alloc_trash_chunk();
5143 if (tmptrash) {
5144 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5145 chunk_printf(tmptrash,
5146 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5147 s->flags & SRV_F_BACKUP ? "Backup " : "",
5148 s->proxy->id, s->id,
5149 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5150 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5151 }
5152 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5153 chunk_printf(tmptrash,
5154 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5155 s->flags & SRV_F_BACKUP ? "Backup " : "",
5156 s->proxy->id, s->id, s->hostname,
5157 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5158 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5159 }
5160 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5161 chunk_printf(tmptrash,
5162 "%sServer %s/%s is %s/%s (leaving maintenance)",
5163 s->flags & SRV_F_BACKUP ? "Backup " : "",
5164 s->proxy->id, s->id,
5165 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5166 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5167 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005168 ha_warning("%s.\n", tmptrash->area);
5169 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5170 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005171 free_trash_chunk(tmptrash);
5172 tmptrash = NULL;
5173 }
5174
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005175 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005176 /* now propagate the status change to any LB algorithms */
5177 if (px->lbprm.update_server_eweight)
5178 px->lbprm.update_server_eweight(s);
5179 else if (srv_willbe_usable(s)) {
5180 if (px->lbprm.set_server_status_up)
5181 px->lbprm.set_server_status_up(s);
5182 }
5183 else {
5184 if (px->lbprm.set_server_status_down)
5185 px->lbprm.set_server_status_down(s);
5186 }
5187
5188 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5189 set_backend_down(s->proxy);
5190
Willy Tarreau6a78e612018-08-07 10:14:53 +02005191 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5192 * and it's not a backup server and its effective weight is > 0,
5193 * then it can accept new connections, so we shut down all streams
5194 * on all backup servers.
5195 */
5196 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5197 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5198 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5199
5200 /* check if we can handle some connections queued at the proxy. We
5201 * will take as many as we can handle.
5202 */
5203 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005204 }
5205 else if (s->next_admin & SRV_ADMF_MAINT) {
5206 /* remaining in maintenance mode, let's inform precisely about the
5207 * situation.
5208 */
5209 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5210 tmptrash = alloc_trash_chunk();
5211 if (tmptrash) {
5212 chunk_printf(tmptrash,
5213 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5214 s->flags & SRV_F_BACKUP ? "Backup " : "",
5215 s->proxy->id, s->id);
5216
5217 if (s->track) /* normally it's mandatory here */
5218 chunk_appendf(tmptrash, " via %s/%s",
5219 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005220 ha_warning("%s.\n", tmptrash->area);
5221 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5222 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005223 free_trash_chunk(tmptrash);
5224 tmptrash = NULL;
5225 }
5226 }
5227 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5228 tmptrash = alloc_trash_chunk();
5229 if (tmptrash) {
5230 chunk_printf(tmptrash,
5231 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5232 s->flags & SRV_F_BACKUP ? "Backup " : "",
5233 s->proxy->id, s->id, s->hostname);
5234
5235 if (s->track) /* normally it's mandatory here */
5236 chunk_appendf(tmptrash, " via %s/%s",
5237 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005238 ha_warning("%s.\n", tmptrash->area);
5239 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5240 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005241 free_trash_chunk(tmptrash);
5242 tmptrash = NULL;
5243 }
5244 }
5245 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5246 tmptrash = alloc_trash_chunk();
5247 if (tmptrash) {
5248 chunk_printf(tmptrash,
5249 "%sServer %s/%s remains in forced maintenance",
5250 s->flags & SRV_F_BACKUP ? "Backup " : "",
5251 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005252 ha_warning("%s.\n", tmptrash->area);
5253 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5254 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005255 free_trash_chunk(tmptrash);
5256 tmptrash = NULL;
5257 }
5258 }
5259 /* don't report anything when leaving drain mode and remaining in maintenance */
5260
5261 s->cur_admin = s->next_admin;
5262 }
5263
5264 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5265 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5266 /* drain state is applied only if not yet in maint */
5267
5268 s->last_change = now.tv_sec;
5269 if (px->lbprm.set_server_status_down)
5270 px->lbprm.set_server_status_down(s);
5271
5272 /* we might have streams queued on this server and waiting for
5273 * a connection. Those which are redispatchable will be queued
5274 * to another server or to the proxy itself.
5275 */
5276 xferred = pendconn_redistribute(s);
5277
5278 tmptrash = alloc_trash_chunk();
5279 if (tmptrash) {
5280 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5281 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5282 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5283
5284 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5285
5286 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005287 ha_warning("%s.\n", tmptrash->area);
5288 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5289 tmptrash->area);
5290 send_email_alert(s, LOG_NOTICE, "%s",
5291 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005292 }
5293 free_trash_chunk(tmptrash);
5294 tmptrash = NULL;
5295 }
5296
5297 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5298 set_backend_down(s->proxy);
5299 }
5300 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5301 /* OK completely leaving drain mode */
5302 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5303 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5304 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5305 s->proxy->last_change = now.tv_sec;
5306 }
5307
5308 if (s->last_change < now.tv_sec) // ignore negative times
5309 s->down_time += now.tv_sec - s->last_change;
5310 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005311 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005312
5313 tmptrash = alloc_trash_chunk();
5314 if (tmptrash) {
5315 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5316 chunk_printf(tmptrash,
5317 "%sServer %s/%s is %s (leaving forced drain)",
5318 s->flags & SRV_F_BACKUP ? "Backup " : "",
5319 s->proxy->id, s->id,
5320 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5321 }
5322 else {
5323 chunk_printf(tmptrash,
5324 "%sServer %s/%s is %s (leaving drain)",
5325 s->flags & SRV_F_BACKUP ? "Backup " : "",
5326 s->proxy->id, s->id,
5327 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5328 if (s->track) /* normally it's mandatory here */
5329 chunk_appendf(tmptrash, " via %s/%s",
5330 s->track->proxy->id, s->track->id);
5331 }
5332
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005333 ha_warning("%s.\n", tmptrash->area);
5334 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5335 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005336 free_trash_chunk(tmptrash);
5337 tmptrash = NULL;
5338 }
5339
5340 /* now propagate the status change to any LB algorithms */
5341 if (px->lbprm.update_server_eweight)
5342 px->lbprm.update_server_eweight(s);
5343 else if (srv_willbe_usable(s)) {
5344 if (px->lbprm.set_server_status_up)
5345 px->lbprm.set_server_status_up(s);
5346 }
5347 else {
5348 if (px->lbprm.set_server_status_down)
5349 px->lbprm.set_server_status_down(s);
5350 }
5351 }
5352 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5353 /* remaining in drain mode after removing one of its flags */
5354
5355 tmptrash = alloc_trash_chunk();
5356 if (tmptrash) {
5357 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5358 chunk_printf(tmptrash,
5359 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5360 s->flags & SRV_F_BACKUP ? "Backup " : "",
5361 s->proxy->id, s->id);
5362
5363 if (s->track) /* normally it's mandatory here */
5364 chunk_appendf(tmptrash, " via %s/%s",
5365 s->track->proxy->id, s->track->id);
5366 }
5367 else {
5368 chunk_printf(tmptrash,
5369 "%sServer %s/%s remains in forced drain mode",
5370 s->flags & SRV_F_BACKUP ? "Backup " : "",
5371 s->proxy->id, s->id);
5372 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005373 ha_warning("%s.\n", tmptrash->area);
5374 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5375 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005376 free_trash_chunk(tmptrash);
5377 tmptrash = NULL;
5378 }
5379
5380 /* commit new admin status */
5381
5382 s->cur_admin = s->next_admin;
5383 }
5384 }
5385
5386 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005387 *s->adm_st_chg_cause = 0;
5388}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005389
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005390struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5391{
5392 struct connection *conn;
5393
5394 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5395 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005396 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005397 }
5398
5399 return task;
5400}
5401
Willy Tarreau980855b2019-02-07 14:59:29 +01005402struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005403{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005404 struct server *srv;
5405 struct eb32_node *eb;
5406 int i;
5407 unsigned int next_wakeup;
5408 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005409
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005410 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5411 while (1) {
5412 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005413
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005414 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5415 if (!eb) {
5416 /* we might have reached the end of the tree, typically because
5417 * <now_ms> is in the first half and we're first scanning the last
5418 * half. Let's loop back to the beginning of the tree now.
5419 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005420
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005421 eb = eb32_first(&idle_conn_srv);
5422 if (likely(!eb))
5423 break;
5424 }
5425 if (tick_is_lt(now_ms, eb->key)) {
5426 /* timer not expired yet, revisit it later */
5427 next_wakeup = eb->key;
5428 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005429 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005430 }
5431 srv = eb32_entry(eb, struct server, idle_node);
5432 for (i = 0; i < global.nbthread; i++) {
5433 int max_conn = (srv->curr_idle_thr[i] / 2) +
5434 (srv->curr_idle_thr[i] & 1);
5435 int j;
5436 int did_remove = 0;
5437
5438 for (j = 0; j < max_conn; j++) {
5439 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5440 if (!conn)
5441 break;
5442 did_remove = 1;
5443 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5444 }
5445 if (did_remove && max_conn < srv->curr_idle_thr[i])
5446 srv_is_empty = 0;
5447 if (did_remove)
5448 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5449 }
5450 eb32_delete(&srv->idle_node);
5451 if (!srv_is_empty) {
5452 /* There are still more idle connections, add the
5453 * server back in the tree.
5454 */
5455 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5456 now_ms);
5457 eb32_insert(&idle_conn_srv, &srv->idle_node);
5458 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005459 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005460 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5461
5462 if (need_wakeup)
5463 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005464 else
5465 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005466
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005467 return task;
5468}
Olivier Houchard88698d92019-04-16 19:07:22 +02005469
5470/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5471static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5472 struct proxy *defpx, const char *file, int line,
5473 char **err)
5474{
5475 int arg = -1;
5476
5477 if (too_many_args(1, args, err, NULL))
5478 return -1;
5479
5480 if (*(args[1]) != 0)
5481 arg = atoi(args[1]);
5482
5483 if (arg < 0 || arg > 100) {
5484 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5485 return -1;
5486 }
5487
5488 if (args[0][10] == 'h')
5489 global.tune.pool_high_ratio = arg;
5490 else
5491 global.tune.pool_low_ratio = arg;
5492 return 0;
5493}
5494
5495/* config keyword parsers */
5496static struct cfg_kw_list cfg_kws = {ILH, {
5497 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5498 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5499 { 0, NULL, NULL }
5500}};
5501
5502INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5503
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005504/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005505 * Local variables:
5506 * c-indent-level: 8
5507 * c-basic-offset: 8
5508 * End:
5509 */