blob: 02fa2a46cb91387c0a495f4b4a8864b3ed004be7 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Frédéric Lécaille7da71292019-05-20 09:47:07 +020028#include <types/dict.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010030#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020031#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010032#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020033
William Lallemand222baf22016-11-19 02:00:33 +010034#include <proto/applet.h>
35#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090036#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020037#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020038#include <proto/port_range.h>
39#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020040#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010041#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020042#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010044#include <proto/stream_interface.h>
45#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020047#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010048#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049
Baptiste Assmannda29fe22019-06-13 13:24:29 +020050#include <ebsttree.h>
51
Willy Tarreau3ff577e2018-08-02 11:48:52 +020052static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020053static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010054static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010055static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020056static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
57static int srv_state_get_version(FILE *f);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau21faa912012-10-10 08:27:36 +020059/* List head of all known server keywords */
60static struct srv_kw_list srv_keywords = {
61 .list = LIST_HEAD_INIT(srv_keywords.list)
62};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063
Olivier Houchard9ea5d362019-02-14 18:29:09 +010064__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
65struct eb_root idle_conn_srv = EB_ROOT;
66struct task *idle_conn_task = NULL;
67struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
68struct list toremove_connections[MAX_THREADS];
69
Frédéric Lécaille7da71292019-05-20 09:47:07 +020070/* The server names dictionary */
71struct dict server_name_dict = {
72 .name = "server names",
73 .values = EB_ROOT_UNIQUE,
74};
75
Baptiste Assmannda29fe22019-06-13 13:24:29 +020076/* tree where global state_file is loaded */
77struct eb_root state_file = EB_ROOT;
78
Simon Hormana3608442013-11-01 16:46:15 +090079int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020080{
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020082 return s->down_time;
83
84 return now.tv_sec - s->last_change + s->down_time;
85}
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050087int srv_lastsession(const struct server *s)
88{
89 if (s->counters.last_sess)
90 return now.tv_sec - s->counters.last_sess;
91
92 return -1;
93}
94
Simon Horman4a741432013-02-23 15:35:38 +090095int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020096{
Simon Horman4a741432013-02-23 15:35:38 +090097 const struct server *s = check->server;
98
Willy Tarreauff5ae352013-12-11 20:36:34 +010099 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900100 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100101
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900103 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100104
Simon Horman4a741432013-02-23 15:35:38 +0900105 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100106}
107
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100108/*
109 * Check that we did not get a hash collision.
110 * Unlikely, but it can happen.
111 */
112static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100113{
114 struct proxy *p = s->proxy;
115 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100116
117 for (tmpserv = p->srv; tmpserv != NULL;
118 tmpserv = tmpserv->next) {
119 if (tmpserv == s)
120 continue;
121 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
122 continue;
123 if (tmpserv->cookie &&
124 strcmp(tmpserv->cookie, s->cookie) == 0) {
125 ha_warning("We generated two equal cookies for two different servers.\n"
126 "Please change the secret key for '%s'.\n",
127 s->proxy->id);
128 }
129 }
130
131}
132
Willy Tarreau46b7f532018-08-21 11:54:26 +0200133/*
134 * Must be called with the server lock held.
135 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100136void srv_set_dyncookie(struct server *s)
137{
138 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100139 char *tmpbuf;
140 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100141 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100142 size_t buffer_len;
143 int addr_len;
144 int port;
145
146 if ((s->flags & SRV_F_COOKIESET) ||
147 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
148 s->proxy->dyncookie_key == NULL)
149 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100150 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100151
152 if (s->addr.ss_family != AF_INET &&
153 s->addr.ss_family != AF_INET6)
154 return;
155 /*
156 * Buffer to calculate the cookie value.
157 * The buffer contains the secret key + the server IP address
158 * + the TCP port.
159 */
160 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
161 /*
162 * The TCP port should use only 2 bytes, but is stored in
163 * an unsigned int in struct server, so let's use 4, to be
164 * on the safe side.
165 */
166 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200167 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100168 memcpy(tmpbuf, p->dyncookie_key, key_len);
169 memcpy(&(tmpbuf[key_len]),
170 s->addr.ss_family == AF_INET ?
171 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
172 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
173 addr_len);
174 /*
175 * Make sure it's the same across all the load balancers,
176 * no matter their endianness.
177 */
178 port = htonl(s->svc_port);
179 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
180 hash_value = XXH64(tmpbuf, buffer_len, 0);
181 memprintf(&s->cookie, "%016llx", hash_value);
182 if (!s->cookie)
183 return;
184 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100185
186 /* Don't bother checking if the dyncookie is duplicated if
187 * the server is marked as "disabled", maybe it doesn't have
188 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100189 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100190 if (!(s->next_admin & SRV_ADMF_FMAINT))
191 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100192}
193
Willy Tarreau21faa912012-10-10 08:27:36 +0200194/*
195 * Registers the server keyword list <kwl> as a list of valid keywords for next
196 * parsing sessions.
197 */
198void srv_register_keywords(struct srv_kw_list *kwl)
199{
200 LIST_ADDQ(&srv_keywords.list, &kwl->list);
201}
202
203/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
204 * keyword is found with a NULL ->parse() function, then an attempt is made to
205 * find one with a valid ->parse() function. This way it is possible to declare
206 * platform-dependant, known keywords as NULL, then only declare them as valid
207 * if some options are met. Note that if the requested keyword contains an
208 * opening parenthesis, everything from this point is ignored.
209 */
210struct srv_kw *srv_find_kw(const char *kw)
211{
212 int index;
213 const char *kwend;
214 struct srv_kw_list *kwl;
215 struct srv_kw *ret = NULL;
216
217 kwend = strchr(kw, '(');
218 if (!kwend)
219 kwend = kw + strlen(kw);
220
221 list_for_each_entry(kwl, &srv_keywords.list, list) {
222 for (index = 0; kwl->kw[index].kw != NULL; index++) {
223 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
224 kwl->kw[index].kw[kwend-kw] == 0) {
225 if (kwl->kw[index].parse)
226 return &kwl->kw[index]; /* found it !*/
227 else
228 ret = &kwl->kw[index]; /* may be OK */
229 }
230 }
231 }
232 return ret;
233}
234
235/* Dumps all registered "server" keywords to the <out> string pointer. The
236 * unsupported keywords are only dumped if their supported form was not
237 * found.
238 */
239void srv_dump_kws(char **out)
240{
241 struct srv_kw_list *kwl;
242 int index;
243
244 *out = NULL;
245 list_for_each_entry(kwl, &srv_keywords.list, list) {
246 for (index = 0; kwl->kw[index].kw != NULL; index++) {
247 if (kwl->kw[index].parse ||
248 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
249 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
250 kwl->scope,
251 kwl->kw[index].kw,
252 kwl->kw[index].skip ? " <arg>" : "",
253 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
254 kwl->kw[index].parse ? "" : " (not supported)");
255 }
256 }
257 }
258}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100259
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100260/* Parse the "addr" server keyword */
261static int srv_parse_addr(char **args, int *cur_arg,
262 struct proxy *curproxy, struct server *newsrv, char **err)
263{
264 char *errmsg, *arg;
265 struct sockaddr_storage *sk;
266 int port1, port2;
267 struct protocol *proto;
268
269 errmsg = NULL;
270 arg = args[*cur_arg + 1];
271
272 if (!*arg) {
273 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
274 goto err;
275 }
276
277 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
278 if (!sk) {
279 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
280 goto err;
281 }
282
283 proto = protocol_by_family(sk->ss_family);
284 if (!proto || !proto->connect) {
285 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
286 args[*cur_arg], arg);
287 goto err;
288 }
289
290 if (port1 != port2) {
291 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
292 args[*cur_arg], arg);
293 goto err;
294 }
295
296 newsrv->check.addr = newsrv->agent.addr = *sk;
297 newsrv->flags |= SRV_F_CHECKADDR;
298 newsrv->flags |= SRV_F_AGENTADDR;
299
300 return 0;
301
302 err:
303 free(errmsg);
304 return ERR_ALERT | ERR_FATAL;
305}
306
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100307/* Parse the "agent-check" server keyword */
308static int srv_parse_agent_check(char **args, int *cur_arg,
309 struct proxy *curproxy, struct server *newsrv, char **err)
310{
311 newsrv->do_agent = 1;
312 return 0;
313}
314
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100315/* Parse the "backup" server keyword */
316static int srv_parse_backup(char **args, int *cur_arg,
317 struct proxy *curproxy, struct server *newsrv, char **err)
318{
319 newsrv->flags |= SRV_F_BACKUP;
320 return 0;
321}
322
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100323/* Parse the "check" server keyword */
324static int srv_parse_check(char **args, int *cur_arg,
325 struct proxy *curproxy, struct server *newsrv, char **err)
326{
327 newsrv->do_check = 1;
328 return 0;
329}
330
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100331/* Parse the "check-send-proxy" server keyword */
332static int srv_parse_check_send_proxy(char **args, int *cur_arg,
333 struct proxy *curproxy, struct server *newsrv, char **err)
334{
335 newsrv->check.send_proxy = 1;
336 return 0;
337}
338
Alexander Liu2a54bb72019-05-22 19:44:48 +0800339/* Parse the "check-via-socks4" server keyword */
340static int srv_parse_check_via_socks4(char **args, int *cur_arg,
341 struct proxy *curproxy, struct server *newsrv, char **err)
342{
343 newsrv->check.via_socks4 = 1;
344 return 0;
345}
346
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100347/* Parse the "cookie" server keyword */
348static int srv_parse_cookie(char **args, int *cur_arg,
349 struct proxy *curproxy, struct server *newsrv, char **err)
350{
351 char *arg;
352
353 arg = args[*cur_arg + 1];
354 if (!*arg) {
355 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
356 return ERR_ALERT | ERR_FATAL;
357 }
358
359 free(newsrv->cookie);
360 newsrv->cookie = strdup(arg);
361 newsrv->cklen = strlen(arg);
362 newsrv->flags |= SRV_F_COOKIESET;
363 return 0;
364}
365
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100366/* Parse the "disabled" server keyword */
367static int srv_parse_disabled(char **args, int *cur_arg,
368 struct proxy *curproxy, struct server *newsrv, char **err)
369{
Emeric Brun52a91d32017-08-31 14:41:55 +0200370 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
371 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100372 newsrv->check.state |= CHK_ST_PAUSED;
373 newsrv->check.health = 0;
374 return 0;
375}
376
377/* Parse the "enabled" server keyword */
378static int srv_parse_enabled(char **args, int *cur_arg,
379 struct proxy *curproxy, struct server *newsrv, char **err)
380{
Emeric Brun52a91d32017-08-31 14:41:55 +0200381 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
382 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100383 newsrv->check.state &= ~CHK_ST_PAUSED;
384 newsrv->check.health = newsrv->check.rise;
385 return 0;
386}
387
Willy Tarreau9c538e02019-01-23 10:21:49 +0100388static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
389{
390 char *arg;
391
392 arg = args[*cur_arg + 1];
393 if (!*arg) {
394 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
395 return ERR_ALERT | ERR_FATAL;
396 }
397 newsrv->max_reuse = atoi(arg);
398
399 return 0;
400}
401
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100402static 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 +0100403{
404 const char *res;
405 char *arg;
406 unsigned int time;
407
408 arg = args[*cur_arg + 1];
409 if (!*arg) {
410 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
411 return ERR_ALERT | ERR_FATAL;
412 }
413 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200414 if (res == PARSE_TIME_OVER) {
415 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
416 args[*cur_arg+1], args[*cur_arg]);
417 return ERR_ALERT | ERR_FATAL;
418 }
419 else if (res == PARSE_TIME_UNDER) {
420 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
421 args[*cur_arg+1], args[*cur_arg]);
422 return ERR_ALERT | ERR_FATAL;
423 }
424 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100425 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
426 *res, args[*cur_arg]);
427 return ERR_ALERT | ERR_FATAL;
428 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100429 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100430
431 return 0;
432}
433
Olivier Houchard006e3102018-12-10 18:30:32 +0100434static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
435{
436 char *arg;
437
438 arg = args[*cur_arg + 1];
439 if (!*arg) {
440 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
441 return ERR_ALERT | ERR_FATAL;
442 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100443
Olivier Houchard006e3102018-12-10 18:30:32 +0100444 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100445 if ((int)newsrv->max_idle_conns < -1) {
446 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
447 return ERR_ALERT | ERR_FATAL;
448 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100449
450 return 0;
451}
452
Willy Tarreaudff55432012-10-10 17:51:05 +0200453/* parse the "id" server keyword */
454static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
455{
456 struct eb32_node *node;
457
458 if (!*args[*cur_arg + 1]) {
459 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
460 return ERR_ALERT | ERR_FATAL;
461 }
462
463 newsrv->puid = atol(args[*cur_arg + 1]);
464 newsrv->conf.id.key = newsrv->puid;
465
466 if (newsrv->puid <= 0) {
467 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
468 return ERR_ALERT | ERR_FATAL;
469 }
470
471 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
472 if (node) {
473 struct server *target = container_of(node, struct server, conf.id);
474 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
475 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
476 target->id);
477 return ERR_ALERT | ERR_FATAL;
478 }
479
480 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200481 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200482 return 0;
483}
484
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100485/* Parse the "namespace" server keyword */
486static int srv_parse_namespace(char **args, int *cur_arg,
487 struct proxy *curproxy, struct server *newsrv, char **err)
488{
Willy Tarreaue5733232019-05-22 19:24:06 +0200489#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100490 char *arg;
491
492 arg = args[*cur_arg + 1];
493 if (!*arg) {
494 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
495 return ERR_ALERT | ERR_FATAL;
496 }
497
498 if (!strcmp(arg, "*")) {
499 /* Use the namespace associated with the connection (if present). */
500 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
501 return 0;
502 }
503
504 /*
505 * As this parser may be called several times for the same 'default-server'
506 * object, or for a new 'server' instance deriving from a 'default-server'
507 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
508 */
509 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
510
511 newsrv->netns = netns_store_lookup(arg, strlen(arg));
512 if (!newsrv->netns)
513 newsrv->netns = netns_store_insert(arg);
514
515 if (!newsrv->netns) {
516 memprintf(err, "Cannot open namespace '%s'", arg);
517 return ERR_ALERT | ERR_FATAL;
518 }
519
520 return 0;
521#else
522 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
523 return ERR_ALERT | ERR_FATAL;
524#endif
525}
526
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100527/* Parse the "no-agent-check" server keyword */
528static int srv_parse_no_agent_check(char **args, int *cur_arg,
529 struct proxy *curproxy, struct server *newsrv, char **err)
530{
531 free_check(&newsrv->agent);
532 newsrv->agent.inter = 0;
533 newsrv->agent.port = 0;
534 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
535 newsrv->do_agent = 0;
536 return 0;
537}
538
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100539/* Parse the "no-backup" server keyword */
540static int srv_parse_no_backup(char **args, int *cur_arg,
541 struct proxy *curproxy, struct server *newsrv, char **err)
542{
543 newsrv->flags &= ~SRV_F_BACKUP;
544 return 0;
545}
546
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100547/* Parse the "no-check" server keyword */
548static int srv_parse_no_check(char **args, int *cur_arg,
549 struct proxy *curproxy, struct server *newsrv, char **err)
550{
551 free_check(&newsrv->check);
552 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
553 newsrv->do_check = 0;
554 return 0;
555}
556
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100557/* Parse the "no-check-send-proxy" server keyword */
558static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
559 struct proxy *curproxy, struct server *newsrv, char **err)
560{
561 newsrv->check.send_proxy = 0;
562 return 0;
563}
564
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100565/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200566static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100567{
568 srv->pp_opts &= ~flags;
569 return 0;
570}
571
572/* Parse the "no-send-proxy" server keyword */
573static int srv_parse_no_send_proxy(char **args, int *cur_arg,
574 struct proxy *curproxy, struct server *newsrv, char **err)
575{
576 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
577}
578
579/* Parse the "no-send-proxy-v2" server keyword */
580static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
581 struct proxy *curproxy, struct server *newsrv, char **err)
582{
583 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
584}
585
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200586/* Parse the "no-tfo" server keyword */
587static int srv_parse_no_tfo(char **args, int *cur_arg,
588 struct proxy *curproxy, struct server *newsrv, char **err)
589{
590 newsrv->flags &= ~SRV_F_FASTOPEN;
591 return 0;
592}
593
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100594/* Parse the "non-stick" server keyword */
595static int srv_parse_non_stick(char **args, int *cur_arg,
596 struct proxy *curproxy, struct server *newsrv, char **err)
597{
598 newsrv->flags |= SRV_F_NON_STICK;
599 return 0;
600}
601
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100602/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200603static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100604{
605 srv->pp_opts |= flags;
606 return 0;
607}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200608/* parse the "proto" server keyword */
609static int srv_parse_proto(char **args, int *cur_arg,
610 struct proxy *px, struct server *newsrv, char **err)
611{
612 struct ist proto;
613
614 if (!*args[*cur_arg + 1]) {
615 memprintf(err, "'%s' : missing value", args[*cur_arg]);
616 return ERR_ALERT | ERR_FATAL;
617 }
618 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
619 newsrv->mux_proto = get_mux_proto(proto);
620 if (!newsrv->mux_proto) {
621 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
622 return ERR_ALERT | ERR_FATAL;
623 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200624 return 0;
625}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100626
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100627/* parse the "proxy-v2-options" */
628static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
629 struct proxy *px, struct server *newsrv, char **err)
630{
631 char *p, *n;
632 for (p = args[*cur_arg+1]; p; p = n) {
633 n = strchr(p, ',');
634 if (n)
635 *n++ = '\0';
636 if (!strcmp(p, "ssl")) {
637 newsrv->pp_opts |= SRV_PP_V2_SSL;
638 } else if (!strcmp(p, "cert-cn")) {
639 newsrv->pp_opts |= SRV_PP_V2_SSL;
640 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100641 } else if (!strcmp(p, "cert-key")) {
642 newsrv->pp_opts |= SRV_PP_V2_SSL;
643 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
644 } else if (!strcmp(p, "cert-sig")) {
645 newsrv->pp_opts |= SRV_PP_V2_SSL;
646 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
647 } else if (!strcmp(p, "ssl-cipher")) {
648 newsrv->pp_opts |= SRV_PP_V2_SSL;
649 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100650 } else if (!strcmp(p, "authority")) {
651 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100652 } else if (!strcmp(p, "crc32c")) {
653 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100654 } else
655 goto fail;
656 }
657 return 0;
658 fail:
659 if (err)
660 memprintf(err, "'%s' : proxy v2 option not implemented", p);
661 return ERR_ALERT | ERR_FATAL;
662}
663
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100664/* Parse the "observe" server keyword */
665static int srv_parse_observe(char **args, int *cur_arg,
666 struct proxy *curproxy, struct server *newsrv, char **err)
667{
668 char *arg;
669
670 arg = args[*cur_arg + 1];
671 if (!*arg) {
672 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
673 return ERR_ALERT | ERR_FATAL;
674 }
675
676 if (!strcmp(arg, "none")) {
677 newsrv->observe = HANA_OBS_NONE;
678 }
679 else if (!strcmp(arg, "layer4")) {
680 newsrv->observe = HANA_OBS_LAYER4;
681 }
682 else if (!strcmp(arg, "layer7")) {
683 if (curproxy->mode != PR_MODE_HTTP) {
684 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
685 return ERR_ALERT;
686 }
687 newsrv->observe = HANA_OBS_LAYER7;
688 }
689 else {
690 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
691 "but got '%s'\n", args[*cur_arg], arg);
692 return ERR_ALERT | ERR_FATAL;
693 }
694
695 return 0;
696}
697
Frédéric Lécaille16186232017-03-14 16:42:49 +0100698/* Parse the "redir" server keyword */
699static int srv_parse_redir(char **args, int *cur_arg,
700 struct proxy *curproxy, struct server *newsrv, char **err)
701{
702 char *arg;
703
704 arg = args[*cur_arg + 1];
705 if (!*arg) {
706 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
707 return ERR_ALERT | ERR_FATAL;
708 }
709
710 free(newsrv->rdr_pfx);
711 newsrv->rdr_pfx = strdup(arg);
712 newsrv->rdr_len = strlen(arg);
713
714 return 0;
715}
716
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100717/* Parse the "send-proxy" server keyword */
718static int srv_parse_send_proxy(char **args, int *cur_arg,
719 struct proxy *curproxy, struct server *newsrv, char **err)
720{
721 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
722}
723
724/* Parse the "send-proxy-v2" server keyword */
725static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
726 struct proxy *curproxy, struct server *newsrv, char **err)
727{
728 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
729}
730
Frédéric Lécailledba97072017-03-17 15:33:50 +0100731
732/* Parse the "source" server keyword */
733static int srv_parse_source(char **args, int *cur_arg,
734 struct proxy *curproxy, struct server *newsrv, char **err)
735{
736 char *errmsg;
737 int port_low, port_high;
738 struct sockaddr_storage *sk;
739 struct protocol *proto;
740
741 errmsg = NULL;
742
743 if (!*args[*cur_arg + 1]) {
744 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
745 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
746 goto err;
747 }
748
749 /* 'sk' is statically allocated (no need to be freed). */
750 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
751 if (!sk) {
752 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
753 goto err;
754 }
755
756 proto = protocol_by_family(sk->ss_family);
757 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_alert("'%s %s' : connect() not supported for this address family.\n",
759 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 goto err;
761 }
762
763 newsrv->conn_src.opts |= CO_SRC_BIND;
764 newsrv->conn_src.source_addr = *sk;
765
766 if (port_low != port_high) {
767 int i;
768
769 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100770 ha_alert("'%s' does not support port offsets (found '%s').\n",
771 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100772 goto err;
773 }
774
775 if (port_low <= 0 || port_low > 65535 ||
776 port_high <= 0 || port_high > 65535 ||
777 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100778 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 +0100779 goto err;
780 }
781 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
782 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
783 newsrv->conn_src.sport_range->ports[i] = port_low + i;
784 }
785
786 *cur_arg += 2;
787 while (*(args[*cur_arg])) {
788 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
789#if defined(CONFIG_HAP_TRANSPARENT)
790 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
792 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100793 goto err;
794 }
795 if (!strcmp(args[*cur_arg + 1], "client")) {
796 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
797 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
798 }
799 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
800 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
801 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
802 }
803 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
804 char *name, *end;
805
806 name = args[*cur_arg + 1] + 7;
807 while (isspace(*name))
808 name++;
809
810 end = name;
811 while (*end && !isspace(*end) && *end != ',' && *end != ')')
812 end++;
813
814 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
815 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
816 free(newsrv->conn_src.bind_hdr_name);
817 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
818 newsrv->conn_src.bind_hdr_len = end - name;
819 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
820 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
821 newsrv->conn_src.bind_hdr_occ = -1;
822
823 /* now look for an occurrence number */
824 while (isspace(*end))
825 end++;
826 if (*end == ',') {
827 end++;
828 name = end;
829 if (*end == '-')
830 end++;
831 while (isdigit((int)*end))
832 end++;
833 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
834 }
835
836 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100837 ha_alert("usesrc hdr_ip(name,num) does not support negative"
838 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100839 goto err;
840 }
841 }
842 else {
843 struct sockaddr_storage *sk;
844 int port1, port2;
845
846 /* 'sk' is statically allocated (no need to be freed). */
847 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
848 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100849 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100850 goto err;
851 }
852
853 proto = protocol_by_family(sk->ss_family);
854 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100855 ha_alert("'%s %s' : connect() not supported for this address family.\n",
856 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100857 goto err;
858 }
859
860 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100861 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
862 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100863 goto err;
864 }
865 newsrv->conn_src.tproxy_addr = *sk;
866 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
867 }
868 global.last_checks |= LSTCHK_NETADM;
869 *cur_arg += 2;
870 continue;
871#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100872 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 +0100873 goto err;
874#endif /* defined(CONFIG_HAP_TRANSPARENT) */
875 } /* "usesrc" */
876
877 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
878#ifdef SO_BINDTODEVICE
879 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100880 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100881 goto err;
882 }
883 free(newsrv->conn_src.iface_name);
884 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
885 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
886 global.last_checks |= LSTCHK_NETADM;
887#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100888 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100889 goto err;
890#endif
891 *cur_arg += 2;
892 continue;
893 }
894 /* this keyword in not an option of "source" */
895 break;
896 } /* while */
897
898 return 0;
899
900 err:
901 free(errmsg);
902 return ERR_ALERT | ERR_FATAL;
903}
904
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100905/* Parse the "stick" server keyword */
906static int srv_parse_stick(char **args, int *cur_arg,
907 struct proxy *curproxy, struct server *newsrv, char **err)
908{
909 newsrv->flags &= ~SRV_F_NON_STICK;
910 return 0;
911}
912
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100913/* Parse the "track" server keyword */
914static int srv_parse_track(char **args, int *cur_arg,
915 struct proxy *curproxy, struct server *newsrv, char **err)
916{
917 char *arg;
918
919 arg = args[*cur_arg + 1];
920 if (!*arg) {
921 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
922 return ERR_ALERT | ERR_FATAL;
923 }
924
925 free(newsrv->trackit);
926 newsrv->trackit = strdup(arg);
927
928 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800929}
930
931/* Parse the "socks4" server keyword */
932static int srv_parse_socks4(char **args, int *cur_arg,
933 struct proxy *curproxy, struct server *newsrv, char **err)
934{
935 char *errmsg;
936 int port_low, port_high;
937 struct sockaddr_storage *sk;
938 struct protocol *proto;
939
940 errmsg = NULL;
941
942 if (!*args[*cur_arg + 1]) {
943 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
944 goto err;
945 }
946
947 /* 'sk' is statically allocated (no need to be freed). */
948 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
949 if (!sk) {
950 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
951 goto err;
952 }
953
954 proto = protocol_by_family(sk->ss_family);
955 if (!proto || !proto->connect) {
956 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
957 goto err;
958 }
959
960 newsrv->flags |= SRV_F_SOCKS4_PROXY;
961 newsrv->socks4_addr = *sk;
962
963 if (port_low != port_high) {
964 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
965 goto err;
966 }
967
968 if (!port_low) {
969 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
970 goto err;
971 }
972
973 return 0;
974
975 err:
976 free(errmsg);
977 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100978}
979
Frédéric Lécailledba97072017-03-17 15:33:50 +0100980
Willy Tarreau034c88c2017-01-23 23:36:45 +0100981/* parse the "tfo" server keyword */
982static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
983{
984 newsrv->flags |= SRV_F_FASTOPEN;
985 return 0;
986}
987
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200988/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200989 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200990 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200991 *
992 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200993 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200994void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200995{
Willy Tarreau87b09662015-04-03 00:22:06 +0200996 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200997
Willy Tarreau87b09662015-04-03 00:22:06 +0200998 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
999 if (stream->srv_conn == srv)
1000 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001001}
1002
1003/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001004 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001005 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001006 *
1007 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001008 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001009void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001010{
1011 struct server *srv;
1012
1013 for (srv = px->srv; srv != NULL; srv = srv->next)
1014 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001015 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001016}
1017
Willy Tarreaubda92272014-05-20 21:55:30 +02001018/* Appends some information to a message string related to a server going UP or
1019 * DOWN. If both <forced> and <reason> are null and the server tracks another
1020 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001021 * If <check> is non-null, an entire string describing the check result will be
1022 * appended after a comma and a space (eg: to report some information from the
1023 * check that changed the state). In the other case, the string will be built
1024 * using the check results stored into the struct server if present.
1025 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001026 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001027 *
1028 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001029 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001030void srv_append_status(struct buffer *msg, struct server *s,
1031 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001032{
Emeric Brun5a133512017-10-19 14:42:30 +02001033 short status = s->op_st_chg.status;
1034 short code = s->op_st_chg.code;
1035 long duration = s->op_st_chg.duration;
1036 char *desc = s->op_st_chg.reason;
1037
1038 if (check) {
1039 status = check->status;
1040 code = check->code;
1041 duration = check->duration;
1042 desc = check->desc;
1043 }
1044
1045 if (status != -1) {
1046 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1047
1048 if (status >= HCHK_STATUS_L57DATA)
1049 chunk_appendf(msg, ", code: %d", code);
1050
1051 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001052 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001053
1054 chunk_appendf(msg, ", info: \"");
1055
1056 chunk_initlen(&src, desc, 0, strlen(desc));
1057 chunk_asciiencode(msg, &src, '"');
1058
1059 chunk_appendf(msg, "\"");
1060 }
1061
1062 if (duration >= 0)
1063 chunk_appendf(msg, ", check duration: %ldms", duration);
1064 }
1065 else if (desc && *desc) {
1066 chunk_appendf(msg, ", %s", desc);
1067 }
1068 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001069 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001070 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001071
1072 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001073 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001074 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1075 " %d sessions active, %d requeued, %d remaining in queue",
1076 s->proxy->srv_act, s->proxy->srv_bck,
1077 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1078 s->cur_sess, xferred, s->nbpend);
1079 else
1080 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1081 " %d sessions requeued, %d total in queue",
1082 s->proxy->srv_act, s->proxy->srv_bck,
1083 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1084 xferred, s->nbpend);
1085 }
1086}
1087
Emeric Brun5a133512017-10-19 14:42:30 +02001088/* Marks server <s> down, regardless of its checks' statuses. The server is
1089 * registered in a list to postpone the counting of the remaining servers on
1090 * the proxy and transfers queued streams whenever possible to other servers at
1091 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1092 * non-null as the reason for going down or the available data from the check
1093 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001094 *
1095 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001096 */
Emeric Brun5a133512017-10-19 14:42:30 +02001097void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001098{
1099 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001100
Emeric Brun64cc49c2017-10-03 14:46:45 +02001101 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001102 return;
1103
Emeric Brun52a91d32017-08-31 14:41:55 +02001104 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001105 *s->op_st_chg.reason = 0;
1106 s->op_st_chg.status = -1;
1107 if (reason) {
1108 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1109 }
1110 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001111 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001112 s->op_st_chg.code = check->code;
1113 s->op_st_chg.status = check->status;
1114 s->op_st_chg.duration = check->duration;
1115 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001116
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001117 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001118 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001119
Emeric Brun9f0b4582017-10-23 14:39:51 +02001120 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001121 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001122 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001123 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001124 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001125}
1126
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001127/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001128 * in maintenance. The server is registered in a list to postpone the counting
1129 * of the remaining servers on the proxy and tries to grab requests from the
1130 * proxy at a sync point. Maintenance servers are ignored. It stores the
1131 * <reason> if non-null as the reason for going down or the available data
1132 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001133 *
1134 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001135 */
Emeric Brun5a133512017-10-19 14:42:30 +02001136void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001137{
1138 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001139
Emeric Brun64cc49c2017-10-03 14:46:45 +02001140 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001141 return;
1142
Emeric Brun52a91d32017-08-31 14:41:55 +02001143 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001144 return;
1145
Emeric Brun52a91d32017-08-31 14:41:55 +02001146 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001147 *s->op_st_chg.reason = 0;
1148 s->op_st_chg.status = -1;
1149 if (reason) {
1150 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1151 }
1152 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001153 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001154 s->op_st_chg.code = check->code;
1155 s->op_st_chg.status = check->status;
1156 s->op_st_chg.duration = check->duration;
1157 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001158
Emeric Brun64cc49c2017-10-03 14:46:45 +02001159 if (s->slowstart <= 0)
1160 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001161
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001162 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001163 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001164
Emeric Brun9f0b4582017-10-23 14:39:51 +02001165 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001166 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001167 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001168 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001169 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001170}
1171
Willy Tarreau8eb77842014-05-21 13:54:57 +02001172/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001173 * isn't in maintenance. The server is registered in a list to postpone the
1174 * counting of the remaining servers on the proxy and tries to grab requests
1175 * from the proxy. Maintenance servers are ignored. It stores the
1176 * <reason> if non-null as the reason for going down or the available data
1177 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001178 * up. Note that it makes use of the trash to build the log strings, so <reason>
1179 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001180 *
1181 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001182 */
Emeric Brun5a133512017-10-19 14:42:30 +02001183void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001184{
1185 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001186
Emeric Brun64cc49c2017-10-03 14:46:45 +02001187 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001188 return;
1189
Emeric Brun52a91d32017-08-31 14:41:55 +02001190 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001191 return;
1192
Emeric Brun52a91d32017-08-31 14:41:55 +02001193 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001194 *s->op_st_chg.reason = 0;
1195 s->op_st_chg.status = -1;
1196 if (reason) {
1197 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1198 }
1199 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001200 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001201 s->op_st_chg.code = check->code;
1202 s->op_st_chg.status = check->status;
1203 s->op_st_chg.duration = check->duration;
1204 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001205
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001206 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001207 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001208
Emeric Brun9f0b4582017-10-23 14:39:51 +02001209 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001210 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001211 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001212 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001213 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001214}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001215
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001216/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1217 * enforce either maint mode or drain mode. It is not allowed to set more than
1218 * one flag at once. The equivalent "inherited" flag is propagated to all
1219 * tracking servers. Maintenance mode disables health checks (but not agent
1220 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001221 * is done. If <cause> is non-null, it will be displayed at the end of the log
1222 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001223 *
1224 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001225 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001226void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001227{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001228 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001229
1230 if (!mode)
1231 return;
1232
1233 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001234 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001235 return;
1236
Emeric Brun52a91d32017-08-31 14:41:55 +02001237 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001238 if (cause)
1239 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1240
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001241 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001242 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001243
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001244 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001245 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1246 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001247 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001248
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001249 /* compute the inherited flag to propagate */
1250 if (mode & SRV_ADMF_MAINT)
1251 mode = SRV_ADMF_IMAINT;
1252 else if (mode & SRV_ADMF_DRAIN)
1253 mode = SRV_ADMF_IDRAIN;
1254
Emeric Brun9f0b4582017-10-23 14:39:51 +02001255 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001256 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001257 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001258 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001259 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001260}
1261
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001262/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1263 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1264 * than one flag at once. The equivalent "inherited" flag is propagated to all
1265 * tracking servers. Leaving maintenance mode re-enables health checks. When
1266 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001267 *
1268 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001269 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001270void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001271{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001272 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001273
1274 if (!mode)
1275 return;
1276
1277 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001278 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001279 return;
1280
Emeric Brun52a91d32017-08-31 14:41:55 +02001281 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001282
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001283 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001284 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001285
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001286 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001287 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1288 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001289 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001290
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001291 if (mode & SRV_ADMF_MAINT)
1292 mode = SRV_ADMF_IMAINT;
1293 else if (mode & SRV_ADMF_DRAIN)
1294 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001295
Emeric Brun9f0b4582017-10-23 14:39:51 +02001296 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001297 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001298 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001299 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001300 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001301}
1302
Willy Tarreau757478e2016-11-03 19:22:19 +01001303/* principle: propagate maint and drain to tracking servers. This is useful
1304 * upon startup so that inherited states are correct.
1305 */
1306static void srv_propagate_admin_state(struct server *srv)
1307{
1308 struct server *srv2;
1309
1310 if (!srv->trackers)
1311 return;
1312
1313 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001314 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001315 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001316 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001317
Emeric Brun52a91d32017-08-31 14:41:55 +02001318 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001319 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001320 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001321 }
1322}
1323
1324/* Compute and propagate the admin states for all servers in proxy <px>.
1325 * Only servers *not* tracking another one are considered, because other
1326 * ones will be handled when the server they track is visited.
1327 */
1328void srv_compute_all_admin_states(struct proxy *px)
1329{
1330 struct server *srv;
1331
1332 for (srv = px->srv; srv; srv = srv->next) {
1333 if (srv->track)
1334 continue;
1335 srv_propagate_admin_state(srv);
1336 }
1337}
1338
Willy Tarreaudff55432012-10-10 17:51:05 +02001339/* Note: must not be declared <const> as its list will be overwritten.
1340 * Please take care of keeping this list alphabetically sorted, doing so helps
1341 * all code contributors.
1342 * Optional keywords are also declared with a NULL ->parse() function so that
1343 * the config parser can report an appropriate error when a known keyword was
1344 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001345 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001346 */
1347static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001348 { "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 +01001349 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001350 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001351 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001352 { "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 +01001353 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001354 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1355 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001356 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001357 { "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 +01001358 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001359 { "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 +01001360 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001361 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001362 { "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 +01001363 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1364 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001365 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001366 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001367 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001368 { "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 +01001369 { "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 +02001370 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001371 { "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 +01001372 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001373 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1374 { "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 +02001375 { "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 +01001376 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001377 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001378 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001379 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1380 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001381 { NULL, NULL, 0 },
1382}};
1383
Willy Tarreau0108d902018-11-25 19:14:37 +01001384INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001385
Willy Tarreau004e0452013-11-21 11:22:01 +01001386/* Recomputes the server's eweight based on its state, uweight, the current time,
1387 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001388 * state is automatically disabled if the time is elapsed. If <must_update> is
1389 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001390 *
1391 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001392 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001393void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001394{
1395 struct proxy *px = sv->proxy;
1396 unsigned w;
1397
1398 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1399 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001400 if (sv->next_state == SRV_ST_STARTING)
1401 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001402 }
1403
1404 /* We must take care of not pushing the server to full throttle during slow starts.
1405 * It must also start immediately, at least at the minimal step when leaving maintenance.
1406 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001407 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001408 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1409 else
1410 w = px->lbprm.wdiv;
1411
Emeric Brun52a91d32017-08-31 14:41:55 +02001412 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001413
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001414 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001415 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001416 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001417}
1418
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001420 * Parses weight_str and configures sv accordingly.
1421 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001422 *
1423 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001424 */
1425const char *server_parse_weight_change_request(struct server *sv,
1426 const char *weight_str)
1427{
1428 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001429 long int w;
1430 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001431
1432 px = sv->proxy;
1433
1434 /* if the weight is terminated with '%', it is set relative to
1435 * the initial weight, otherwise it is absolute.
1436 */
1437 if (!*weight_str)
1438 return "Require <weight> or <weight%>.\n";
1439
Simon Hormanb796afa2013-02-12 10:45:53 +09001440 w = strtol(weight_str, &end, 10);
1441 if (end == weight_str)
1442 return "Empty weight string empty or preceded by garbage";
1443 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001444 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001445 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001446 /* Avoid integer overflow */
1447 if (w > 25600)
1448 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001449 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001450 if (w > 256)
1451 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001452 }
1453 else if (w < 0 || w > 256)
1454 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001455 else if (end[0] != '\0')
1456 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001457
1458 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1459 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1460
1461 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001462 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001463
1464 return NULL;
1465}
1466
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001467/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001468 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1469 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001470 * Returns:
1471 * - error string on error
1472 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001473 *
1474 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001475 */
1476const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001477 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001478{
1479 unsigned char ip[INET6_ADDRSTRLEN];
1480
1481 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001482 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001483 return NULL;
1484 }
1485 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001486 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001487 return NULL;
1488 }
1489
1490 return "Could not understand IP address format.\n";
1491}
1492
Willy Tarreau46b7f532018-08-21 11:54:26 +02001493/*
1494 * Must be called with the server lock held.
1495 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001496const char *server_parse_maxconn_change_request(struct server *sv,
1497 const char *maxconn_str)
1498{
1499 long int v;
1500 char *end;
1501
1502 if (!*maxconn_str)
1503 return "Require <maxconn>.\n";
1504
1505 v = strtol(maxconn_str, &end, 10);
1506 if (end == maxconn_str)
1507 return "maxconn string empty or preceded by garbage";
1508 else if (end[0] != '\0')
1509 return "Trailing garbage in maxconn string";
1510
1511 if (sv->maxconn == sv->minconn) { // static maxconn
1512 sv->maxconn = sv->minconn = v;
1513 } else { // dynamic maxconn
1514 sv->maxconn = v;
1515 }
1516
1517 if (may_dequeue_tasks(sv, sv->proxy))
1518 process_srv_queue(sv);
1519
1520 return NULL;
1521}
1522
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001523#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001524static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1525 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001526{
1527 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001528 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001529 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001530 NULL,
1531 };
1532
1533 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001534 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001535
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001536 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1537}
1538
1539static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1540{
1541 struct sample_expr *expr;
1542
1543 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 +01001544 if (!expr) {
1545 memprintf(err, "error detected while parsing sni expression : %s", *err);
1546 return ERR_ALERT | ERR_FATAL;
1547 }
1548
1549 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1550 memprintf(err, "error detected while parsing sni expression : "
1551 " fetch method '%s' extracts information from '%s', "
1552 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001553 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001554 return ERR_ALERT | ERR_FATAL;
1555 }
1556
1557 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1558 release_sample_expr(newsrv->ssl_ctx.sni);
1559 newsrv->ssl_ctx.sni = expr;
1560
1561 return 0;
1562}
1563#endif
1564
1565static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1566{
1567 if (err && *err) {
1568 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001569 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 +01001570 }
1571 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001572 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1573 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001574}
1575
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001576static void srv_conn_src_sport_range_cpy(struct server *srv,
1577 struct server *src)
1578{
1579 int range_sz;
1580
1581 range_sz = src->conn_src.sport_range->size;
1582 if (range_sz > 0) {
1583 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1584 if (srv->conn_src.sport_range != NULL) {
1585 int i;
1586
1587 for (i = 0; i < range_sz; i++) {
1588 srv->conn_src.sport_range->ports[i] =
1589 src->conn_src.sport_range->ports[i];
1590 }
1591 }
1592 }
1593}
1594
1595/*
1596 * Copy <src> server connection source settings to <srv> server everything needed.
1597 */
1598static void srv_conn_src_cpy(struct server *srv, struct server *src)
1599{
1600 srv->conn_src.opts = src->conn_src.opts;
1601 srv->conn_src.source_addr = src->conn_src.source_addr;
1602
1603 /* Source port range copy. */
1604 if (src->conn_src.sport_range != NULL)
1605 srv_conn_src_sport_range_cpy(srv, src);
1606
1607#ifdef CONFIG_HAP_TRANSPARENT
1608 if (src->conn_src.bind_hdr_name != NULL) {
1609 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1610 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1611 }
1612 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1613 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1614#endif
1615 if (src->conn_src.iface_name != NULL)
1616 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1617}
1618
1619/*
1620 * Copy <src> server SSL settings to <srv> server allocating
1621 * everything needed.
1622 */
1623#if defined(USE_OPENSSL)
1624static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1625{
1626 if (src->ssl_ctx.ca_file != NULL)
1627 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1628 if (src->ssl_ctx.crl_file != NULL)
1629 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1630 if (src->ssl_ctx.client_crt != NULL)
1631 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1632
1633 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1634
1635 if (src->ssl_ctx.verify_host != NULL)
1636 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1637 if (src->ssl_ctx.ciphers != NULL)
1638 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001639#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001640 if (src->ssl_ctx.ciphersuites != NULL)
1641 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1642#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001643 if (src->sni_expr != NULL)
1644 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001645
1646#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1647 if (src->ssl_ctx.alpn_str) {
1648 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1649 if (srv->ssl_ctx.alpn_str) {
1650 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1651 src->ssl_ctx.alpn_len);
1652 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1653 }
1654 }
1655#endif
1656#ifdef OPENSSL_NPN_NEGOTIATED
1657 if (src->ssl_ctx.npn_str) {
1658 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1659 if (srv->ssl_ctx.npn_str) {
1660 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1661 src->ssl_ctx.npn_len);
1662 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1663 }
1664 }
1665#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001666}
1667#endif
1668
1669/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001670 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001671 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001672 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001673 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001674static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001675{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001676 char *hostname_dn;
1677 int hostname_len, hostname_dn_len;
1678
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001679 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001680 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001681
Christopher Faulet67957bd2017-09-27 11:00:59 +02001682 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001683 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001684 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1685 hostname_dn, trash.size);
1686 if (hostname_dn_len == -1)
1687 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001688
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001689
Christopher Faulet67957bd2017-09-27 11:00:59 +02001690 free(srv->hostname);
1691 free(srv->hostname_dn);
1692 srv->hostname = strdup(hostname);
1693 srv->hostname_dn = strdup(hostname_dn);
1694 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001695 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001696 goto err;
1697
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001698 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001699
1700 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001701 free(srv->hostname); srv->hostname = NULL;
1702 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001703 return -1;
1704}
1705
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001706/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001707 * Copy <src> server settings to <srv> server allocating
1708 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001709 * This function is not supposed to be called at any time, but only
1710 * during server settings parsing or during server allocations from
1711 * a server template, and just after having calloc()'ed a new server.
1712 * So, <src> may only be a default server (when parsing server settings)
1713 * or a server template (during server allocations from a server template).
1714 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1715 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001716 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001717static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001718{
1719 /* Connection source settings copy */
1720 srv_conn_src_cpy(srv, src);
1721
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001722 if (srv_tmpl) {
1723 srv->addr = src->addr;
1724 srv->svc_port = src->svc_port;
1725 }
1726
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001727 srv->pp_opts = src->pp_opts;
1728 if (src->rdr_pfx != NULL) {
1729 srv->rdr_pfx = strdup(src->rdr_pfx);
1730 srv->rdr_len = src->rdr_len;
1731 }
1732 if (src->cookie != NULL) {
1733 srv->cookie = strdup(src->cookie);
1734 srv->cklen = src->cklen;
1735 }
1736 srv->use_ssl = src->use_ssl;
1737 srv->check.addr = srv->agent.addr = src->check.addr;
1738 srv->check.use_ssl = src->check.use_ssl;
1739 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001740 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001741 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001742 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001743 /* Note: 'flags' field has potentially been already initialized. */
1744 srv->flags |= src->flags;
1745 srv->do_check = src->do_check;
1746 srv->do_agent = src->do_agent;
1747 if (srv->check.port)
1748 srv->flags |= SRV_F_CHECKPORT;
1749 srv->check.inter = src->check.inter;
1750 srv->check.fastinter = src->check.fastinter;
1751 srv->check.downinter = src->check.downinter;
1752 srv->agent.use_ssl = src->agent.use_ssl;
1753 srv->agent.port = src->agent.port;
1754 if (src->agent.send_string != NULL)
1755 srv->agent.send_string = strdup(src->agent.send_string);
1756 srv->agent.send_string_len = src->agent.send_string_len;
1757 srv->agent.inter = src->agent.inter;
1758 srv->agent.fastinter = src->agent.fastinter;
1759 srv->agent.downinter = src->agent.downinter;
1760 srv->maxqueue = src->maxqueue;
1761 srv->minconn = src->minconn;
1762 srv->maxconn = src->maxconn;
1763 srv->slowstart = src->slowstart;
1764 srv->observe = src->observe;
1765 srv->onerror = src->onerror;
1766 srv->onmarkeddown = src->onmarkeddown;
1767 srv->onmarkedup = src->onmarkedup;
1768 if (src->trackit != NULL)
1769 srv->trackit = strdup(src->trackit);
1770 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1771 srv->uweight = srv->iweight = src->iweight;
1772
1773 srv->check.send_proxy = src->check.send_proxy;
1774 /* health: up, but will fall down at first failure */
1775 srv->check.rise = srv->check.health = src->check.rise;
1776 srv->check.fall = src->check.fall;
1777
1778 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001779 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1780 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1781 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001782 srv->check.state |= CHK_ST_PAUSED;
1783 srv->check.health = 0;
1784 }
1785
1786 /* health: up but will fall down at first failure */
1787 srv->agent.rise = srv->agent.health = src->agent.rise;
1788 srv->agent.fall = src->agent.fall;
1789
1790 if (src->resolvers_id != NULL)
1791 srv->resolvers_id = strdup(src->resolvers_id);
1792 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001793 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001794 if (srv->dns_opts.family_prio == AF_UNSPEC)
1795 srv->dns_opts.family_prio = AF_INET6;
1796 memcpy(srv->dns_opts.pref_net,
1797 src->dns_opts.pref_net,
1798 sizeof srv->dns_opts.pref_net);
1799 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1800
1801 srv->init_addr_methods = src->init_addr_methods;
1802 srv->init_addr = src->init_addr;
1803#if defined(USE_OPENSSL)
1804 srv_ssl_settings_cpy(srv, src);
1805#endif
1806#ifdef TCP_USER_TIMEOUT
1807 srv->tcp_ut = src->tcp_ut;
1808#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001809 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001810 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001811 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001812 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001813
Olivier Houchard8da5f982017-08-04 18:35:36 +02001814 if (srv_tmpl)
1815 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001816
1817 srv->check.via_socks4 = src->check.via_socks4;
1818 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001819}
1820
William Lallemand313bfd12018-10-26 14:47:32 +02001821struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001822{
1823 struct server *srv;
1824
1825 srv = calloc(1, sizeof *srv);
1826 if (!srv)
1827 return NULL;
1828
1829 srv->obj_type = OBJ_TYPE_SERVER;
1830 srv->proxy = proxy;
1831 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001832 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001833
Emeric Brun52a91d32017-08-31 14:41:55 +02001834 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001835 srv->last_change = now.tv_sec;
1836
1837 srv->check.status = HCHK_STATUS_INI;
1838 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001839 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001840 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1841
1842 srv->agent.status = HCHK_STATUS_INI;
1843 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001844 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001845 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1846
Willy Tarreau975b1552019-06-06 16:25:55 +02001847 /* please don't put default server settings here, they are set in
1848 * init_default_instance().
1849 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001850 return srv;
1851}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001852
1853/*
1854 * Validate <srv> server health-check settings.
1855 * Returns 0 if everything is OK, -1 if not.
1856 */
1857static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1858{
1859 struct tcpcheck_rule *r = NULL;
1860 struct list *l;
1861
1862 /*
1863 * We need at least a service port, a check port or the first tcp-check rule must
1864 * be a 'connect' one when checking an IPv4/IPv6 server.
1865 */
1866 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1867 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1868 return 0;
1869
1870 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1871 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001872 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1873 "Check has been disabled.\n",
1874 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001875 return -1;
1876 }
1877
1878 /* search the first action (connect / send / expect) in the list */
1879 l = &srv->proxy->tcpcheck_rules;
1880 list_for_each_entry(r, l, list) {
1881 if (r->action != TCPCHK_ACT_COMMENT)
1882 break;
1883 }
1884
1885 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001886 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1887 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1888 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001889 return -1;
1890 }
1891
1892 /* scan the tcp-check ruleset to ensure a port has been configured */
1893 l = &srv->proxy->tcpcheck_rules;
1894 list_for_each_entry(r, l, list) {
1895 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001896 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1897 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1898 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001899 return -1;
1900 }
1901 }
1902
1903 return 0;
1904}
1905
1906/*
1907 * Initialize <srv> health-check structure.
1908 * Returns the error string in case of memory allocation failure, NULL if not.
1909 */
1910static const char *do_health_check_init(struct server *srv, int check_type, int state)
1911{
1912 const char *ret;
1913
1914 if (!srv->do_check)
1915 return NULL;
1916
1917 ret = init_check(&srv->check, check_type);
1918 if (ret)
1919 return ret;
1920
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001921 srv->check.state |= state;
1922 global.maxsock++;
1923
1924 return NULL;
1925}
1926
1927static int server_health_check_init(const char *file, int linenum,
1928 struct server *srv, struct proxy *curproxy)
1929{
1930 const char *ret;
1931
1932 if (!srv->do_check)
1933 return 0;
1934
1935 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001936 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1937 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001938 return ERR_ALERT | ERR_FATAL;
1939 }
1940
1941 if (server_healthcheck_validate(file, linenum, srv) < 0)
1942 return ERR_ALERT | ERR_ABORT;
1943
1944 /* note: check type will be set during the config review phase */
1945 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1946 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001947 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001948 return ERR_ALERT | ERR_ABORT;
1949 }
1950
1951 return 0;
1952}
1953
1954/*
1955 * Initialize <srv> agent check structure.
1956 * Returns the error string in case of memory allocation failure, NULL if not.
1957 */
1958static const char *do_server_agent_check_init(struct server *srv, int state)
1959{
1960 const char *ret;
1961
1962 if (!srv->do_agent)
1963 return NULL;
1964
1965 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1966 if (ret)
1967 return ret;
1968
1969 if (!srv->agent.inter)
1970 srv->agent.inter = srv->check.inter;
1971
1972 srv->agent.state |= state;
1973 global.maxsock++;
1974
1975 return NULL;
1976}
1977
1978static int server_agent_check_init(const char *file, int linenum,
1979 struct server *srv, struct proxy *curproxy)
1980{
1981 const char *ret;
1982
1983 if (!srv->do_agent)
1984 return 0;
1985
1986 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001987 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 +02001988 file, linenum, srv->id);
1989 return ERR_ALERT | ERR_FATAL;
1990 }
1991
1992 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1993 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001994 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001995 return ERR_ALERT | ERR_ABORT;
1996 }
1997
1998 return 0;
1999}
2000
2001#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2002static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2003 struct server *srv, struct proxy *proxy)
2004{
2005 int ret;
2006 char *err = NULL;
2007
2008 if (!srv->sni_expr)
2009 return 0;
2010
2011 ret = server_parse_sni_expr(srv, proxy, &err);
2012 if (!ret)
2013 return 0;
2014
2015 display_parser_err(file, linenum, args, cur_arg, &err);
2016 free(err);
2017
2018 return ret;
2019}
2020#endif
2021
2022/*
2023 * Server initializations finalization.
2024 * Initialize health check, agent check and SNI expression if enabled.
2025 * Must not be called for a default server instance.
2026 */
2027static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2028 struct server *srv, struct proxy *px)
2029{
2030 int ret;
2031
2032 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2033 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2034 return ret;
2035 }
2036
2037#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2038 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2039 return ret;
2040#endif
2041
2042 if (srv->flags & SRV_F_BACKUP)
2043 px->srv_bck++;
2044 else
2045 px->srv_act++;
2046 srv_lb_commit_status(srv);
2047
2048 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002049err:
2050 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002051}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002052
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002053/*
2054 * Parse as much as possible such a range string argument: low[-high]
2055 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2056 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2057 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2058 * Returns 0 if succeeded, -1 if not.
2059 */
2060static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2061{
2062 char *nb_high_arg;
2063
2064 *nb_high = 0;
2065 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002066 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002067
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002068 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002069 *nb_high_arg++ = '\0';
2070 *nb_high = atoi(nb_high_arg);
2071 }
2072 else {
2073 *nb_high += *nb_low;
2074 *nb_low = 1;
2075 }
2076
2077 if (*nb_low < 0 || *nb_high < *nb_low)
2078 return -1;
2079
2080 return 0;
2081}
2082
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002083static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2084{
2085 chunk_printf(&trash, "%s%d", prefix, nb);
2086 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002087 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002088}
2089
2090/*
2091 * Initialize as much as possible servers from <srv> server template.
2092 * Note that a server template is a special server with
2093 * a few different parameters than a server which has
2094 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002095 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002096 * 'srv' template included.
2097 */
2098static int server_template_init(struct server *srv, struct proxy *px)
2099{
2100 int i;
2101 struct server *newsrv;
2102
2103 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2104 int check_init_state;
2105 int agent_init_state;
2106
2107 newsrv = new_server(px);
2108 if (!newsrv)
2109 goto err;
2110
2111 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002112 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002113#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2114 if (newsrv->sni_expr) {
2115 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2116 if (!newsrv->ssl_ctx.sni)
2117 goto err;
2118 }
2119#endif
2120 /* Set this new server ID. */
2121 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2122
2123 /* Initial checks states. */
2124 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2125 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2126
2127 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2128 do_server_agent_check_init(newsrv, agent_init_state))
2129 goto err;
2130
2131 /* Linked backwards first. This will be restablished after parsing. */
2132 newsrv->next = px->srv;
2133 px->srv = newsrv;
2134 }
2135 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2136
2137 return i - srv->tmpl_info.nb_low;
2138
2139 err:
2140 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2141 if (newsrv) {
2142#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2143 release_sample_expr(newsrv->ssl_ctx.sni);
2144#endif
2145 free_check(&newsrv->agent);
2146 free_check(&newsrv->check);
2147 }
2148 free(newsrv);
2149 return i - srv->tmpl_info.nb_low;
2150}
2151
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002152int 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 +02002153{
2154 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002155 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002156 char *errmsg = NULL;
2157 int err_code = 0;
2158 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002159 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002160
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002161 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002162 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002163 !strcmp(args[0], "default-server") ||
2164 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002165 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002166 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002167 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002168 int srv_tmpl = !defsrv && !srv;
2169 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002170
2171 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002172 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002173 err_code |= ERR_ALERT | ERR_FATAL;
2174 goto out;
2175 }
2176 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002177 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002178
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002179 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002180 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002181 if (!*args[2]) {
2182 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002183 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002184 file, linenum, args[0]);
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186 goto out;
2187 }
2188
2189 err = invalid_char(args[1]);
2190 }
2191 else if (srv_tmpl) {
2192 if (!*args[3]) {
2193 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002194 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 +02002195 file, linenum, args[0]);
2196 err_code |= ERR_ALERT | ERR_FATAL;
2197 goto out;
2198 }
2199
2200 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002201 }
2202
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002203 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002204 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 +02002205 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002206 err_code |= ERR_ALERT | ERR_FATAL;
2207 goto out;
2208 }
2209
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002210 cur_arg = 2;
2211 if (srv_tmpl) {
2212 /* Parse server-template <nb | range> arg. */
2213 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002214 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002215 file, linenum, args[0], args[cur_arg]);
2216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
2219 cur_arg++;
2220 }
2221
Willy Tarreau272adea2014-03-31 10:39:59 +02002222 if (!defsrv) {
2223 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002224 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002225 struct protocol *proto;
2226
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002227 newsrv = new_server(curproxy);
2228 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002229 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002230 err_code |= ERR_ALERT | ERR_ABORT;
2231 goto out;
2232 }
2233
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002234 if (srv_tmpl) {
2235 newsrv->tmpl_info.nb_low = tmpl_range_low;
2236 newsrv->tmpl_info.nb_high = tmpl_range_high;
2237 }
2238
Willy Tarreau272adea2014-03-31 10:39:59 +02002239 /* the servers are linked backwards first */
2240 newsrv->next = curproxy->srv;
2241 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002242 newsrv->conf.file = strdup(file);
2243 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002244 /* Note: for a server template, its id is its prefix.
2245 * This is a temporary id which will be used for server allocations to come
2246 * after parsing.
2247 */
2248 if (srv)
2249 newsrv->id = strdup(args[1]);
2250 else
2251 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002252
2253 /* several ways to check the port component :
2254 * - IP => port=+0, relative (IPv4 only)
2255 * - IP: => port=+0, relative
2256 * - IP:N => port=N, absolute
2257 * - IP:+N => port=+N, relative
2258 * - IP:-N => port=-N, relative
2259 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002260 if (!parse_addr)
2261 goto skip_addr;
2262
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002263 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002264 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002265 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002266 err_code |= ERR_ALERT | ERR_FATAL;
2267 goto out;
2268 }
2269
2270 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002271 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002272 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002273 file, linenum, args[0], args[1]);
2274 err_code |= ERR_ALERT | ERR_FATAL;
2275 goto out;
2276 }
2277
2278 if (!port1 || !port2) {
2279 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002280 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002281 }
2282 else if (port1 != port2) {
2283 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002284 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 file, linenum, args[0], args[1], args[2]);
2286 err_code |= ERR_ALERT | ERR_FATAL;
2287 goto out;
2288 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002289
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002290 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002291 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002292 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002293 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002294 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2295 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2296 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002297 err_code |= ERR_ALERT | ERR_FATAL;
2298 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002299 }
2300 }
2301 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002302 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002303 file, linenum, newsrv->id);
2304 err_code |= ERR_ALERT | ERR_FATAL;
2305 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002306 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002307 }
2308
Willy Tarreau272adea2014-03-31 10:39:59 +02002309 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002310 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002311
Olivier Houchard8da5f982017-08-04 18:35:36 +02002312 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002313 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002314 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002315 err_code |= ERR_ALERT | ERR_FATAL;
2316 goto out;
2317 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002318
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002319 cur_arg++;
2320 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002321 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002322 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002323 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002324 } else {
2325 newsrv = &curproxy->defsrv;
2326 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002327 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002328 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002329 }
2330
2331 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002332 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002333 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002334
2335 if (err == PARSE_TIME_OVER) {
2336 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2337 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2338 err_code |= ERR_ALERT | ERR_FATAL;
2339 goto out;
2340 }
2341 else if (err == PARSE_TIME_UNDER) {
2342 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2343 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2344 err_code |= ERR_ALERT | ERR_FATAL;
2345 goto out;
2346 }
2347 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002348 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002349 file, linenum, *err, newsrv->id);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
2352 }
2353 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002354 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002355 file, linenum, val, args[cur_arg], newsrv->id);
2356 err_code |= ERR_ALERT | ERR_FATAL;
2357 goto out;
2358 }
2359 newsrv->agent.inter = val;
2360 cur_arg += 2;
2361 }
Misiekea849332017-01-09 09:39:51 +01002362 else if (!strcmp(args[cur_arg], "agent-addr")) {
2363 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002364 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002365 goto out;
2366 }
2367
2368 cur_arg += 2;
2369 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002370 else if (!strcmp(args[cur_arg], "agent-port")) {
2371 global.maxsock++;
2372 newsrv->agent.port = atol(args[cur_arg + 1]);
2373 cur_arg += 2;
2374 }
James Brown55f9ff12015-10-21 18:19:05 -07002375 else if (!strcmp(args[cur_arg], "agent-send")) {
2376 global.maxsock++;
2377 free(newsrv->agent.send_string);
2378 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2379 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2380 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2381 cur_arg += 2;
2382 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002383 else if (!strcmp(args[cur_arg], "init-addr")) {
2384 char *p, *end;
2385 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002386 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002387
2388 newsrv->init_addr_methods = 0;
2389 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2390
2391 for (p = args[cur_arg + 1]; *p; p = end) {
2392 /* cut on next comma */
2393 for (end = p; *end && *end != ','; end++);
2394 if (*end)
2395 *(end++) = 0;
2396
Willy Tarreau4310d362016-11-02 15:05:56 +01002397 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002398 if (!strcmp(p, "libc")) {
2399 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2400 }
2401 else if (!strcmp(p, "last")) {
2402 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2403 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002404 else if (!strcmp(p, "none")) {
2405 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2406 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002407 else if (str2ip2(p, &sa, 0)) {
2408 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002409 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002410 file, linenum, args[cur_arg], p);
2411 err_code |= ERR_ALERT | ERR_FATAL;
2412 goto out;
2413 }
2414 newsrv->init_addr = sa;
2415 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2416 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002417 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002418 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 +02002419 file, linenum, args[cur_arg], p);
2420 err_code |= ERR_ALERT | ERR_FATAL;
2421 goto out;
2422 }
2423 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002424 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002425 file, linenum, args[cur_arg], p);
2426 err_code |= ERR_ALERT | ERR_FATAL;
2427 goto out;
2428 }
2429 }
2430 cur_arg += 2;
2431 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002432 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002433 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002434 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2435 cur_arg += 2;
2436 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002437 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2438 char *p, *end;
2439
2440 for (p = args[cur_arg + 1]; *p; p = end) {
2441 /* cut on next comma */
2442 for (end = p; *end && *end != ','; end++);
2443 if (*end)
2444 *(end++) = 0;
2445
2446 if (!strcmp(p, "allow-dup-ip")) {
2447 newsrv->dns_opts.accept_duplicate_ip = 1;
2448 }
2449 else if (!strcmp(p, "prevent-dup-ip")) {
2450 newsrv->dns_opts.accept_duplicate_ip = 0;
2451 }
2452 else {
2453 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2454 file, linenum, args[cur_arg], p);
2455 err_code |= ERR_ALERT | ERR_FATAL;
2456 goto out;
2457 }
2458 }
2459
2460 cur_arg += 2;
2461 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002462 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2463 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002464 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002465 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002466 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002467 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002468 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002469 file, linenum, args[cur_arg]);
2470 err_code |= ERR_ALERT | ERR_FATAL;
2471 goto out;
2472 }
2473 cur_arg += 2;
2474 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002475 else if (!strcmp(args[cur_arg], "resolve-net")) {
2476 char *p, *e;
2477 unsigned char mask;
2478 struct dns_options *opt;
2479
2480 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002481 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002482 file, linenum, args[cur_arg]);
2483 err_code |= ERR_ALERT | ERR_FATAL;
2484 goto out;
2485 }
2486
2487 opt = &newsrv->dns_opts;
2488
2489 /* Split arguments by comma, and convert it from ipv4 or ipv6
2490 * string network in in_addr or in6_addr.
2491 */
2492 p = args[cur_arg + 1];
2493 e = p;
2494 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002495 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002496 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002497 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002498 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2499 err_code |= ERR_ALERT | ERR_FATAL;
2500 goto out;
2501 }
2502 /* look for end or comma. */
2503 while (*e != ',' && *e != '\0')
2504 e++;
2505 if (*e == ',') {
2506 *e = '\0';
2507 e++;
2508 }
2509 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2510 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2511 /* Try to convert input string from ipv4 or ipv6 network. */
2512 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2513 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2514 &mask)) {
2515 /* Try to convert input string from ipv6 network. */
2516 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2517 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2518 } else {
2519 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002520 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002521 file, linenum, args[cur_arg], p);
2522 err_code |= ERR_ALERT | ERR_FATAL;
2523 goto out;
2524 }
2525 opt->pref_net_nb++;
2526 p = e;
2527 }
2528
2529 cur_arg += 2;
2530 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002531 else if (!strcmp(args[cur_arg], "rise")) {
2532 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002533 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002534 file, linenum, args[cur_arg]);
2535 err_code |= ERR_ALERT | ERR_FATAL;
2536 goto out;
2537 }
2538
2539 newsrv->check.rise = atol(args[cur_arg + 1]);
2540 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002541 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002542 file, linenum, args[cur_arg]);
2543 err_code |= ERR_ALERT | ERR_FATAL;
2544 goto out;
2545 }
2546
2547 if (newsrv->check.health)
2548 newsrv->check.health = newsrv->check.rise;
2549 cur_arg += 2;
2550 }
2551 else if (!strcmp(args[cur_arg], "fall")) {
2552 newsrv->check.fall = atol(args[cur_arg + 1]);
2553
2554 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002555 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002556 file, linenum, args[cur_arg]);
2557 err_code |= ERR_ALERT | ERR_FATAL;
2558 goto out;
2559 }
2560
2561 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002562 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002563 file, linenum, args[cur_arg]);
2564 err_code |= ERR_ALERT | ERR_FATAL;
2565 goto out;
2566 }
2567
2568 cur_arg += 2;
2569 }
2570 else if (!strcmp(args[cur_arg], "inter")) {
2571 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002572
2573 if (err == PARSE_TIME_OVER) {
2574 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2575 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2576 err_code |= ERR_ALERT | ERR_FATAL;
2577 goto out;
2578 }
2579 else if (err == PARSE_TIME_UNDER) {
2580 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2581 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2582 err_code |= ERR_ALERT | ERR_FATAL;
2583 goto out;
2584 }
2585 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002586 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002587 file, linenum, *err, newsrv->id);
2588 err_code |= ERR_ALERT | ERR_FATAL;
2589 goto out;
2590 }
2591 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002592 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002593 file, linenum, val, args[cur_arg], newsrv->id);
2594 err_code |= ERR_ALERT | ERR_FATAL;
2595 goto out;
2596 }
2597 newsrv->check.inter = val;
2598 cur_arg += 2;
2599 }
2600 else if (!strcmp(args[cur_arg], "fastinter")) {
2601 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002602
2603 if (err == PARSE_TIME_OVER) {
2604 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2605 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2606 err_code |= ERR_ALERT | ERR_FATAL;
2607 goto out;
2608 }
2609 else if (err == PARSE_TIME_UNDER) {
2610 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2611 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2612 err_code |= ERR_ALERT | ERR_FATAL;
2613 goto out;
2614 }
2615 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002616 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002617 file, linenum, *err, newsrv->id);
2618 err_code |= ERR_ALERT | ERR_FATAL;
2619 goto out;
2620 }
2621 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002622 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002623 file, linenum, val, args[cur_arg], newsrv->id);
2624 err_code |= ERR_ALERT | ERR_FATAL;
2625 goto out;
2626 }
2627 newsrv->check.fastinter = val;
2628 cur_arg += 2;
2629 }
2630 else if (!strcmp(args[cur_arg], "downinter")) {
2631 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002632
2633 if (err == PARSE_TIME_OVER) {
2634 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2635 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2636 err_code |= ERR_ALERT | ERR_FATAL;
2637 goto out;
2638 }
2639 else if (err == PARSE_TIME_UNDER) {
2640 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2641 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2642 err_code |= ERR_ALERT | ERR_FATAL;
2643 goto out;
2644 }
2645 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002646 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 file, linenum, *err, newsrv->id);
2648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto out;
2650 }
2651 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002652 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002653 file, linenum, val, args[cur_arg], newsrv->id);
2654 err_code |= ERR_ALERT | ERR_FATAL;
2655 goto out;
2656 }
2657 newsrv->check.downinter = val;
2658 cur_arg += 2;
2659 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002660 else if (!strcmp(args[cur_arg], "port")) {
2661 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002662 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002663 cur_arg += 2;
2664 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002665 else if (!strcmp(args[cur_arg], "weight")) {
2666 int w;
2667 w = atol(args[cur_arg + 1]);
2668 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002669 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002670 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2671 err_code |= ERR_ALERT | ERR_FATAL;
2672 goto out;
2673 }
2674 newsrv->uweight = newsrv->iweight = w;
2675 cur_arg += 2;
2676 }
2677 else if (!strcmp(args[cur_arg], "minconn")) {
2678 newsrv->minconn = atol(args[cur_arg + 1]);
2679 cur_arg += 2;
2680 }
2681 else if (!strcmp(args[cur_arg], "maxconn")) {
2682 newsrv->maxconn = atol(args[cur_arg + 1]);
2683 cur_arg += 2;
2684 }
2685 else if (!strcmp(args[cur_arg], "maxqueue")) {
2686 newsrv->maxqueue = atol(args[cur_arg + 1]);
2687 cur_arg += 2;
2688 }
2689 else if (!strcmp(args[cur_arg], "slowstart")) {
2690 /* slowstart is stored in seconds */
2691 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002692
2693 if (err == PARSE_TIME_OVER) {
2694 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2695 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2696 err_code |= ERR_ALERT | ERR_FATAL;
2697 goto out;
2698 }
2699 else if (err == PARSE_TIME_UNDER) {
2700 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2701 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2702 err_code |= ERR_ALERT | ERR_FATAL;
2703 goto out;
2704 }
2705 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002706 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002707 file, linenum, *err, newsrv->id);
2708 err_code |= ERR_ALERT | ERR_FATAL;
2709 goto out;
2710 }
2711 newsrv->slowstart = (val + 999) / 1000;
2712 cur_arg += 2;
2713 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002714 else if (!strcmp(args[cur_arg], "on-error")) {
2715 if (!strcmp(args[cur_arg + 1], "fastinter"))
2716 newsrv->onerror = HANA_ONERR_FASTINTER;
2717 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2718 newsrv->onerror = HANA_ONERR_FAILCHK;
2719 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2720 newsrv->onerror = HANA_ONERR_SUDDTH;
2721 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2722 newsrv->onerror = HANA_ONERR_MARKDWN;
2723 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002724 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002725 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2726 file, linenum, args[cur_arg], args[cur_arg + 1]);
2727 err_code |= ERR_ALERT | ERR_FATAL;
2728 goto out;
2729 }
2730
2731 cur_arg += 2;
2732 }
2733 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2734 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2735 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2736 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002737 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002738 file, linenum, args[cur_arg], args[cur_arg + 1]);
2739 err_code |= ERR_ALERT | ERR_FATAL;
2740 goto out;
2741 }
2742
2743 cur_arg += 2;
2744 }
2745 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2746 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2747 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2748 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002749 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002750 file, linenum, args[cur_arg], args[cur_arg + 1]);
2751 err_code |= ERR_ALERT | ERR_FATAL;
2752 goto out;
2753 }
2754
2755 cur_arg += 2;
2756 }
2757 else if (!strcmp(args[cur_arg], "error-limit")) {
2758 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002759 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002760 file, linenum, args[cur_arg]);
2761 err_code |= ERR_ALERT | ERR_FATAL;
2762 goto out;
2763 }
2764
2765 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2766
2767 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002768 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002769 file, linenum, args[cur_arg]);
2770 err_code |= ERR_ALERT | ERR_FATAL;
2771 goto out;
2772 }
2773 cur_arg += 2;
2774 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002775 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002776 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002777 file, linenum, "usesrc", "source");
2778 err_code |= ERR_ALERT | ERR_FATAL;
2779 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002780 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002781 else {
2782 static int srv_dumped;
2783 struct srv_kw *kw;
2784 char *err;
2785
2786 kw = srv_find_kw(args[cur_arg]);
2787 if (kw) {
2788 char *err = NULL;
2789 int code;
2790
2791 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002792 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 +02002793 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002794 if (kw->skip != -1)
2795 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002796 err_code |= ERR_ALERT | ERR_FATAL;
2797 goto out;
2798 }
2799
2800 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002801 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002802 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002803 if (kw->skip != -1)
2804 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002805 err_code |= ERR_ALERT;
2806 continue;
2807 }
2808
2809 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2810 err_code |= code;
2811
2812 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002813 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002814 if (code & ERR_FATAL) {
2815 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002816 if (kw->skip != -1)
2817 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002818 goto out;
2819 }
2820 }
2821 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002822 if (kw->skip != -1)
2823 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002824 continue;
2825 }
2826
2827 err = NULL;
2828 if (!srv_dumped) {
2829 srv_dump_kws(&err);
2830 indent_msg(&err, 4);
2831 srv_dumped = 1;
2832 }
2833
Christopher Faulet767a84b2017-11-24 16:50:31 +01002834 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002835 file, linenum, args[0], args[1], args[cur_arg],
2836 err ? " Registered keywords :" : "", err ? err : "");
2837 free(err);
2838
2839 err_code |= ERR_ALERT | ERR_FATAL;
2840 goto out;
2841 }
2842 }
2843
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002844 if (!defsrv)
2845 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2846 if (err_code & ERR_FATAL)
2847 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002848 if (srv_tmpl)
2849 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002850 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002851 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002852 return 0;
2853
2854 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002855 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002856 free(errmsg);
2857 return err_code;
2858}
2859
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002860/* Returns a pointer to the first server matching either id <id>.
2861 * NULL is returned if no match is found.
2862 * the lookup is performed in the backend <bk>
2863 */
2864struct server *server_find_by_id(struct proxy *bk, int id)
2865{
2866 struct eb32_node *eb32;
2867 struct server *curserver;
2868
2869 if (!bk || (id ==0))
2870 return NULL;
2871
2872 /* <bk> has no backend capabilities, so it can't have a server */
2873 if (!(bk->cap & PR_CAP_BE))
2874 return NULL;
2875
2876 curserver = NULL;
2877
2878 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2879 if (eb32)
2880 curserver = container_of(eb32, struct server, conf.id);
2881
2882 return curserver;
2883}
2884
2885/* Returns a pointer to the first server matching either name <name>, or id
2886 * if <name> starts with a '#'. NULL is returned if no match is found.
2887 * the lookup is performed in the backend <bk>
2888 */
2889struct server *server_find_by_name(struct proxy *bk, const char *name)
2890{
2891 struct server *curserver;
2892
2893 if (!bk || !name)
2894 return NULL;
2895
2896 /* <bk> has no backend capabilities, so it can't have a server */
2897 if (!(bk->cap & PR_CAP_BE))
2898 return NULL;
2899
2900 curserver = NULL;
2901 if (*name == '#') {
2902 curserver = server_find_by_id(bk, atoi(name + 1));
2903 if (curserver)
2904 return curserver;
2905 }
2906 else {
2907 curserver = bk->srv;
2908
2909 while (curserver && (strcmp(curserver->id, name) != 0))
2910 curserver = curserver->next;
2911
2912 if (curserver)
2913 return curserver;
2914 }
2915
2916 return NULL;
2917}
2918
2919struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2920{
2921 struct server *byname;
2922 struct server *byid;
2923
2924 if (!name && !id)
2925 return NULL;
2926
2927 if (diff)
2928 *diff = 0;
2929
2930 byname = byid = NULL;
2931
2932 if (name) {
2933 byname = server_find_by_name(bk, name);
2934 if (byname && (!id || byname->puid == id))
2935 return byname;
2936 }
2937
2938 /* remaining possibilities :
2939 * - name not set
2940 * - name set but not found
2941 * - name found but ID doesn't match
2942 */
2943 if (id) {
2944 byid = server_find_by_id(bk, id);
2945 if (byid) {
2946 if (byname) {
2947 /* use id only if forced by configuration */
2948 if (byid->flags & SRV_F_FORCED_ID) {
2949 if (diff)
2950 *diff |= 2;
2951 return byid;
2952 }
2953 else {
2954 if (diff)
2955 *diff |= 1;
2956 return byname;
2957 }
2958 }
2959
2960 /* remaining possibilities:
2961 * - name not set
2962 * - name set but not found
2963 */
2964 if (name && diff)
2965 *diff |= 2;
2966 return byid;
2967 }
2968
2969 /* id bot found */
2970 if (byname) {
2971 if (diff)
2972 *diff |= 1;
2973 return byname;
2974 }
2975 }
2976
2977 return NULL;
2978}
2979
Willy Tarreau46b7f532018-08-21 11:54:26 +02002980/* Update a server state using the parameters available in the params list.
2981 *
2982 * Grabs the server lock during operation.
2983 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002984static void srv_update_state(struct server *srv, int version, char **params)
2985{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002986 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002987 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002988
2989 /* fields since version 1
2990 * and common to all other upcoming versions
2991 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002992 enum srv_state srv_op_state;
2993 enum srv_admin srv_admin_state;
2994 unsigned srv_uweight, srv_iweight;
2995 unsigned long srv_last_time_change;
2996 short srv_check_status;
2997 enum chk_result srv_check_result;
2998 int srv_check_health;
2999 int srv_check_state, srv_agent_state;
3000 int bk_f_forced_id;
3001 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003002 int fqdn_set_by_cli;
3003 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003004 const char *port_str;
3005 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003006 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003007
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003008 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003009 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003010 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003011 switch (version) {
3012 case 1:
3013 /*
3014 * now we can proceed with server's state update:
3015 * srv_addr: params[0]
3016 * srv_op_state: params[1]
3017 * srv_admin_state: params[2]
3018 * srv_uweight: params[3]
3019 * srv_iweight: params[4]
3020 * srv_last_time_change: params[5]
3021 * srv_check_status: params[6]
3022 * srv_check_result: params[7]
3023 * srv_check_health: params[8]
3024 * srv_check_state: params[9]
3025 * srv_agent_state: params[10]
3026 * bk_f_forced_id: params[11]
3027 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003028 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003029 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003030 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003031 */
3032
3033 /* validating srv_op_state */
3034 p = NULL;
3035 errno = 0;
3036 srv_op_state = strtol(params[1], &p, 10);
3037 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3038 (srv_op_state != SRV_ST_STOPPED &&
3039 srv_op_state != SRV_ST_STARTING &&
3040 srv_op_state != SRV_ST_RUNNING &&
3041 srv_op_state != SRV_ST_STOPPING)) {
3042 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3043 }
3044
3045 /* validating srv_admin_state */
3046 p = NULL;
3047 errno = 0;
3048 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003049 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003050
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003051 /* inherited statuses will be recomputed later.
3052 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3053 */
3054 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003055
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003056 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3057 (srv_admin_state != 0 &&
3058 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003059 srv_admin_state != SRV_ADMF_CMAINT &&
3060 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003061 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003062 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003063 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3064 }
3065
3066 /* validating srv_uweight */
3067 p = NULL;
3068 errno = 0;
3069 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003070 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003071 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3072
3073 /* validating srv_iweight */
3074 p = NULL;
3075 errno = 0;
3076 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003077 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003078 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3079
3080 /* validating srv_last_time_change */
3081 p = NULL;
3082 errno = 0;
3083 srv_last_time_change = strtol(params[5], &p, 10);
3084 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3085 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3086
3087 /* validating srv_check_status */
3088 p = NULL;
3089 errno = 0;
3090 srv_check_status = strtol(params[6], &p, 10);
3091 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3092 (srv_check_status >= HCHK_STATUS_SIZE))
3093 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3094
3095 /* validating srv_check_result */
3096 p = NULL;
3097 errno = 0;
3098 srv_check_result = strtol(params[7], &p, 10);
3099 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3100 (srv_check_result != CHK_RES_UNKNOWN &&
3101 srv_check_result != CHK_RES_NEUTRAL &&
3102 srv_check_result != CHK_RES_FAILED &&
3103 srv_check_result != CHK_RES_PASSED &&
3104 srv_check_result != CHK_RES_CONDPASS)) {
3105 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3106 }
3107
3108 /* validating srv_check_health */
3109 p = NULL;
3110 errno = 0;
3111 srv_check_health = strtol(params[8], &p, 10);
3112 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3113 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3114
3115 /* validating srv_check_state */
3116 p = NULL;
3117 errno = 0;
3118 srv_check_state = strtol(params[9], &p, 10);
3119 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3120 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3121 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3122
3123 /* validating srv_agent_state */
3124 p = NULL;
3125 errno = 0;
3126 srv_agent_state = strtol(params[10], &p, 10);
3127 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3128 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3129 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3130
3131 /* validating bk_f_forced_id */
3132 p = NULL;
3133 errno = 0;
3134 bk_f_forced_id = strtol(params[11], &p, 10);
3135 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3136 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3137
3138 /* validating srv_f_forced_id */
3139 p = NULL;
3140 errno = 0;
3141 srv_f_forced_id = strtol(params[12], &p, 10);
3142 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3143 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3144
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003145 /* validating srv_fqdn */
3146 fqdn = params[13];
3147 if (fqdn && *fqdn == '-')
3148 fqdn = NULL;
3149 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3150 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3151 fqdn = NULL;
3152 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003153
Frédéric Lécaille31694712017-08-01 08:47:19 +02003154 port_str = params[14];
3155 if (port_str) {
3156 port = strl2uic(port_str, strlen(port_str));
3157 if (port > USHRT_MAX) {
3158 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3159 port_str = NULL;
3160 }
3161 }
3162
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003163 /* SRV record
3164 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3165 */
3166 srvrecord = params[15];
3167 if (srvrecord && *srvrecord != '_')
3168 srvrecord = NULL;
3169
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003171 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003172 goto out;
3173
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003174 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003175 /* recover operational state and apply it to this server
3176 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003177 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003178 switch (srv_op_state) {
3179 case SRV_ST_STOPPED:
3180 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003181 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003182 break;
3183 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003184 /* If rise == 1 there is no STARTING state, let's switch to
3185 * RUNNING
3186 */
3187 if (srv->check.rise == 1) {
3188 srv->check.health = srv->check.rise + srv->check.fall - 1;
3189 srv_set_running(srv, "", NULL);
3190 break;
3191 }
3192 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3193 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003194 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003195 break;
3196 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003197 /* If fall == 1 there is no STOPPING state, let's switch to
3198 * STOPPED
3199 */
3200 if (srv->check.fall == 1) {
3201 srv->check.health = 0;
3202 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3203 break;
3204 }
3205 if (srv->check.health < srv->check.rise ||
3206 srv->check.health > srv->check.rise + srv->check.fall - 2)
3207 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003208 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003209 break;
3210 case SRV_ST_RUNNING:
3211 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003212 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003213 break;
3214 }
3215
3216 /* When applying server state, the following rules apply:
3217 * - in case of a configuration change, we apply the setting from the new
3218 * configuration, regardless of old running state
3219 * - if no configuration change, we apply old running state only if old running
3220 * state is different from new configuration state
3221 */
3222 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003223 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3224 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003225 srv_adm_set_maint(srv);
3226 else
3227 srv_adm_set_ready(srv);
3228 }
3229 /* configuration is the same, let's compate old running state and new conf state */
3230 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003231 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003232 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003233 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003234 srv_adm_set_ready(srv);
3235 }
3236 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003237 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003238 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003239 * (srv->iweight is the weight set up in configuration).
3240 * There are two possible reasons for FDRAIN to have been present :
3241 * - previous config weight was zero
3242 * - "set server b/s drain" was sent to the CLI
3243 *
3244 * In the first case, we simply want to drop this drain state
3245 * if the new weight is not zero anymore, meaning the administrator
3246 * has intentionally turned the weight back to a positive value to
3247 * enable the server again after an operation. In the second case,
3248 * the drain state was forced on the CLI regardless of the config's
3249 * weight so we don't want a change to the config weight to lose this
3250 * status. What this means is :
3251 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3252 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003253 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003254 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003255 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003256 }
3257
3258 srv->last_change = date.tv_sec - srv_last_time_change;
3259 srv->check.status = srv_check_status;
3260 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003261
3262 /* Only case we want to apply is removing ENABLED flag which could have been
3263 * done by the "disable health" command over the stats socket
3264 */
3265 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3266 (srv_check_state & CHK_ST_CONFIGURED) &&
3267 !(srv_check_state & CHK_ST_ENABLED))
3268 srv->check.state &= ~CHK_ST_ENABLED;
3269
3270 /* Only case we want to apply is removing ENABLED flag which could have been
3271 * done by the "disable agent" command over the stats socket
3272 */
3273 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3274 (srv_agent_state & CHK_ST_CONFIGURED) &&
3275 !(srv_agent_state & CHK_ST_ENABLED))
3276 srv->agent.state &= ~CHK_ST_ENABLED;
3277
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003278 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3279 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003280 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003281 * It means that a configuration file change has precedence over a unix socket change
3282 * for server's weight
3283 *
3284 * by default, HAProxy applies the following weight when parsing the configuration
3285 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003286 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003287 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003288 srv->uweight = srv_uweight;
3289 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003290 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003291
Willy Tarreaue5a60682016-11-09 14:54:53 +01003292 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003293 if (strcmp(params[0], "-"))
3294 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003295
3296 if (fqdn && srv->hostname) {
3297 if (!strcmp(srv->hostname, fqdn)) {
3298 /* Here we reset the 'set from stats socket FQDN' flag
3299 * to support such transitions:
3300 * Let's say initial FQDN value is foo1 (in configuration file).
3301 * - FQDN changed from stats socket, from foo1 to foo2 value,
3302 * - FQDN changed again from file configuration (with the same previous value
3303 set from stats socket, from foo1 to foo2 value),
3304 * - reload for any other reason than a FQDN modification,
3305 * the configuration file FQDN matches the fqdn server state file value.
3306 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003307 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003308 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003309 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003310 }
3311 else {
3312 /* If the FDQN has been changed from stats socket,
3313 * apply fqdn state file value (which is the value set
3314 * from stats socket).
3315 */
3316 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003317 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003318 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003319 }
3320 }
3321 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003322 /* If all the conditions below are validated, this means
3323 * we're evaluating a server managed by SRV resolution
3324 */
3325 else if (fqdn && !srv->hostname && srvrecord) {
3326 int res;
3327
3328 /* we can't apply previous state if SRV record has changed */
3329 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3330 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);
3331 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3332 goto out;
3333 }
3334
3335 /* create or find a SRV resolution for this srv record */
3336 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3337 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3338 if (srv->srvrq == NULL) {
3339 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3340 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3341 goto out;
3342 }
3343
3344 /* prepare DNS resolution for this server */
3345 res = srv_prepare_for_resolution(srv, fqdn);
3346 if (res == -1) {
3347 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3348 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3349 goto out;
3350 }
3351
3352 /* configure check.port accordingly */
3353 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3354 !(srv->flags & SRV_F_CHECKPORT))
3355 srv->check.port = port;
3356
3357 /* Unset SRV_F_MAPPORTS for SRV records.
3358 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3359 * because no ports are provided in the configuration file.
3360 * This is because HAProxy will use the port found into the SRV record.
3361 */
3362 srv->flags &= ~SRV_F_MAPPORTS;
3363 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003364
Frédéric Lécaille31694712017-08-01 08:47:19 +02003365 if (port_str)
3366 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003367 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003368
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003369 break;
3370 default:
3371 chunk_appendf(msg, ", version '%d' not supported", version);
3372 }
3373
3374 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003375 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003376 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003377 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003378 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003379 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003380}
3381
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003382
3383/*
3384 * read next line from file <f> and return the server state version if one found.
3385 * If no version is found, then 0 is returned
3386 * Note that this should be the first read on <f>
3387 */
3388static int srv_state_get_version(FILE *f) {
3389 char buf[2];
3390 int ret;
3391
3392 /* first character of first line of the file must contain the version of the export */
3393 if (fgets(buf, 2, f) == NULL) {
3394 return 0;
3395 }
3396
3397 ret = atoi(buf);
3398 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3399 (ret > SRV_STATE_FILE_VERSION_MAX))
3400 return 0;
3401
3402 return ret;
3403}
3404
3405
3406/*
3407 * parses server state line stored in <buf> and supposedly in version <version>.
3408 * Set <params> and <srv_params> accordingly.
3409 * In case of error, params[0] is set to NULL.
3410 */
3411static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3412{
3413 int buflen, arg, srv_arg;
3414 char *cur, *end;
3415
3416 buflen = strlen(buf);
3417 cur = buf;
3418 end = cur + buflen;
3419
3420 /* we need at least one character */
3421 if (buflen == 0) {
3422 params[0] = NULL;
3423 return;
3424 }
3425
3426 /* ignore blank characters at the beginning of the line */
3427 while (isspace(*cur))
3428 ++cur;
3429
3430 /* Ignore empty or commented lines */
3431 if (cur == end || *cur == '#') {
3432 params[0] = NULL;
3433 return;
3434 }
3435
3436 /* truncated lines */
3437 if (buf[buflen - 1] != '\n') {
3438 //ha_warning("server-state file '%s': truncated line\n", filepath);
3439 params[0] = NULL;
3440 return;
3441 }
3442
3443 /* Removes trailing '\n' */
3444 buf[buflen - 1] = '\0';
3445
3446 /* we're now ready to move the line into *srv_params[] */
3447 params[0] = cur;
3448 arg = 1;
3449 srv_arg = 0;
3450 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3451 if (isspace(*cur)) {
3452 *cur = '\0';
3453 ++cur;
3454 while (isspace(*cur))
3455 ++cur;
3456 switch (version) {
3457 case 1:
3458 /*
3459 * srv_addr: params[4] => srv_params[0]
3460 * srv_op_state: params[5] => srv_params[1]
3461 * srv_admin_state: params[6] => srv_params[2]
3462 * srv_uweight: params[7] => srv_params[3]
3463 * srv_iweight: params[8] => srv_params[4]
3464 * srv_last_time_change: params[9] => srv_params[5]
3465 * srv_check_status: params[10] => srv_params[6]
3466 * srv_check_result: params[11] => srv_params[7]
3467 * srv_check_health: params[12] => srv_params[8]
3468 * srv_check_state: params[13] => srv_params[9]
3469 * srv_agent_state: params[14] => srv_params[10]
3470 * bk_f_forced_id: params[15] => srv_params[11]
3471 * srv_f_forced_id: params[16] => srv_params[12]
3472 * srv_fqdn: params[17] => srv_params[13]
3473 * srv_port: params[18] => srv_params[14]
3474 * srvrecord: params[19] => srv_params[15]
3475 */
3476 if (arg >= 4) {
3477 srv_params[srv_arg] = cur;
3478 ++srv_arg;
3479 }
3480 break;
3481 }
3482
3483 params[arg] = cur;
3484 ++arg;
3485 }
3486 else {
3487 ++cur;
3488 }
3489 }
3490
3491 /* if line is incomplete line, then ignore it.
3492 * otherwise, update useful flags */
3493 switch (version) {
3494 case 1:
3495 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3496 params[0] = NULL;
3497 return;
3498 }
3499 break;
3500 }
3501
3502 return;
3503}
3504
3505
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003506/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3507 * For each proxy, it does the following:
3508 * - opens its server state file (either one or local one)
3509 * - read whole file, line by line
3510 * - analyse each line to check if it matches our current backend:
3511 * - backend name matches
3512 * - backend id matches if id is forced and name doesn't match
3513 * - if the server pointed by the line is found, then state is applied
3514 *
3515 * If the running backend uuid or id differs from the state file, then HAProxy reports
3516 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003517 *
3518 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003519 */
3520void apply_server_state(void)
3521{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003522 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003523 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3524 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003525 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003526 FILE *f;
3527 char *filepath;
3528 char globalfilepath[MAXPATHLEN + 1];
3529 char localfilepath[MAXPATHLEN + 1];
3530 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003531 struct proxy *curproxy, *bk;
3532 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003533 char *line;
3534 char *bkname, *srvname;
3535 struct state_line *st;
3536 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003537
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003538
3539 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003540 globalfilepathlen = 0;
3541 /* create the globalfilepath variable */
3542 if (global.server_state_file) {
3543 /* absolute path or no base directory provided */
3544 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3545 len = strlen(global.server_state_file);
3546 if (len > MAXPATHLEN) {
3547 globalfilepathlen = 0;
3548 goto globalfileerror;
3549 }
3550 memcpy(globalfilepath, global.server_state_file, len);
3551 globalfilepath[len] = '\0';
3552 globalfilepathlen = len;
3553 }
3554 else if (global.server_state_base) {
3555 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003556 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003557 globalfilepathlen = 0;
3558 goto globalfileerror;
3559 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003560 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003561 globalfilepath[len] = 0;
3562 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003563
3564 /* append a slash if needed */
3565 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3566 if (globalfilepathlen + 1 > MAXPATHLEN) {
3567 globalfilepathlen = 0;
3568 goto globalfileerror;
3569 }
3570 globalfilepath[globalfilepathlen++] = '/';
3571 }
3572
3573 len = strlen(global.server_state_file);
3574 if (globalfilepathlen + len > MAXPATHLEN) {
3575 globalfilepathlen = 0;
3576 goto globalfileerror;
3577 }
3578 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3579 globalfilepathlen += len;
3580 globalfilepath[globalfilepathlen++] = 0;
3581 }
3582 }
3583 globalfileerror:
3584 if (globalfilepathlen == 0)
3585 globalfilepath[0] = '\0';
3586
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003587 /* Load global server state in a tree */
3588 if (globalfilepathlen > 0) {
3589 errno = 0;
3590 f = fopen(globalfilepath, "r");
3591 if (errno)
3592 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
3593
3594 global_file_version = srv_state_get_version(f);
3595 if (global_file_version == 0)
3596 goto out_load_server_state_in_tree;
3597
3598 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3599 line = NULL;
3600
3601 line = strdup(mybuf);
3602 if (line == NULL)
3603 continue;
3604
3605 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3606 if (params[0] == NULL)
3607 continue;
3608
3609 /* bkname */
3610 bkname = params[1];
3611 /* srvname */
3612 srvname = params[3];
3613
3614 /* key */
3615 chunk_printf(&trash, "%s %s", bkname, srvname);
3616
3617 /* store line in tree */
3618 st = calloc(1, sizeof(*st) + trash.size);
3619 if (st == NULL) {
3620 goto nextline;
3621 }
3622 memcpy(st->name_name.key, trash.area, trash.size);
3623 ebst_insert(&state_file, &st->name_name);
3624
3625 /* save line */
3626 st->line = line;
3627
3628 continue;
3629
3630 nextline:
3631 /* free up memory in case of error during the processing of the line */
3632 free(line);
3633 }
3634 }
3635 out_load_server_state_in_tree:
3636
3637 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003638 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003639 /* servers are only in backends */
3640 if (!(curproxy->cap & PR_CAP_BE))
3641 continue;
3642 fileopenerr = 0;
3643 filepath = NULL;
3644
3645 /* search server state file path and name */
3646 switch (curproxy->load_server_state_from_file) {
3647 /* read servers state from global file */
3648 case PR_SRV_STATE_FILE_GLOBAL:
3649 /* there was an error while generating global server state file path */
3650 if (globalfilepathlen == 0)
3651 continue;
3652 filepath = globalfilepath;
3653 fileopenerr = 1;
3654 break;
3655 /* this backend has its own file */
3656 case PR_SRV_STATE_FILE_LOCAL:
3657 localfilepathlen = 0;
3658 localfilepath[0] = '\0';
3659 len = 0;
3660 /* create the localfilepath variable */
3661 /* absolute path or no base directory provided */
3662 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3663 len = strlen(curproxy->server_state_file_name);
3664 if (len > MAXPATHLEN) {
3665 localfilepathlen = 0;
3666 goto localfileerror;
3667 }
3668 memcpy(localfilepath, curproxy->server_state_file_name, len);
3669 localfilepath[len] = '\0';
3670 localfilepathlen = len;
3671 }
3672 else if (global.server_state_base) {
3673 len = strlen(global.server_state_base);
3674 localfilepathlen += len;
3675
3676 if (localfilepathlen > MAXPATHLEN) {
3677 localfilepathlen = 0;
3678 goto localfileerror;
3679 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003680 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003681 localfilepath[localfilepathlen] = 0;
3682
3683 /* append a slash if needed */
3684 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3685 if (localfilepathlen + 1 > MAXPATHLEN) {
3686 localfilepathlen = 0;
3687 goto localfileerror;
3688 }
3689 localfilepath[localfilepathlen++] = '/';
3690 }
3691
3692 len = strlen(curproxy->server_state_file_name);
3693 if (localfilepathlen + len > MAXPATHLEN) {
3694 localfilepathlen = 0;
3695 goto localfileerror;
3696 }
3697 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3698 localfilepathlen += len;
3699 localfilepath[localfilepathlen++] = 0;
3700 }
3701 filepath = localfilepath;
3702 localfileerror:
3703 if (localfilepathlen == 0)
3704 localfilepath[0] = '\0';
3705
3706 break;
3707 case PR_SRV_STATE_FILE_NONE:
3708 default:
3709 continue;
3710 }
3711
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003712 /* when global file is used, we get data from the tree
3713 * Note that in such case we don't check backend name neither uuid.
3714 * Backend name can't be wrong since it's used as a key to retrieve the server state
3715 * line from the tree.
3716 */
3717 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3718 struct server *srv = curproxy->srv;
3719 while (srv) {
3720 struct ebmb_node *node;
3721 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003722
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003723 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3724 node = ebst_lookup(&state_file, trash.area);
3725 if (!node)
3726 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003727
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003728 st = container_of(node, struct state_line, name_name);
3729 memcpy(mybuf, st->line, strlen(st->line));
3730 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003731
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003732 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3733 if (params[0] == NULL)
3734 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003735
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003736 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003737
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003738 next:
3739 srv = srv->next;
3740 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003741
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003742 continue; /* next proxy in list */
3743 }
3744 else {
3745 /* load 'local' state file */
3746 errno = 0;
3747 f = fopen(filepath, "r");
3748 if (errno && fileopenerr)
3749 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3750 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003751 continue;
3752
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003753 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003754
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003755 /* first character of first line of the file must contain the version of the export */
3756 version = srv_state_get_version(f);
3757 if (version == 0) {
3758 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3759 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003760 }
3761
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003762 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3763 int bk_f_forced_id = 0;
3764 int check_id = 0;
3765 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003766
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003767 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003768
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003769 if (params[0] == NULL) {
3770 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003771 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003772
3773 /* if line is incomplete line, then ignore it.
3774 * otherwise, update useful flags */
3775 switch (version) {
3776 case 1:
3777 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3778 check_id = (atoi(params[0]) == curproxy->uuid);
3779 check_name = (strcmp(curproxy->id, params[1]) == 0);
3780 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003781 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003782
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003783 bk = curproxy;
3784
3785 /* if backend can't be found, let's continue */
3786 if (!check_id && !check_name)
3787 continue;
3788 else if (!check_id && check_name) {
3789 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3790 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3791 }
3792 else if (check_id && !check_name) {
3793 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3794 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3795 /* if name doesn't match, we still want to update curproxy if the backend id
3796 * was forced in previous the previous configuration */
3797 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003798 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003799 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003800
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003801 /* look for the server by its name: param[3] */
3802 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003803
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003804 if (!srv) {
3805 /* if no server found, then warning and continue with next line */
3806 ha_warning("can't find server '%s' in backend '%s'\n",
3807 params[3], params[1]);
3808 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3809 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003810 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003811 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003812
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003813 /* now we can proceed with server's state update */
3814 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003815 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003816 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003817fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003818 fclose(f);
3819 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003820
3821 /* now free memory allocated for the tree */
3822 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3823 node;
3824 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3825 st = container_of(node, struct state_line, name_name);
3826 ebmb_delete(&st->name_name);
3827 free(st->line);
3828 free(st);
3829 }
3830
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003831}
3832
Simon Horman7d09b9a2013-02-12 10:45:51 +09003833/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003834 * update a server's current IP address.
3835 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3836 * ip is in network format.
3837 * updater is a string which contains an information about the requester of the update.
3838 * updater is used if not NULL.
3839 *
3840 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003841 *
3842 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003843 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003844int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003845{
3846 /* generates a log line and a warning on stderr */
3847 if (1) {
3848 /* book enough space for both IPv4 and IPv6 */
3849 char oldip[INET6_ADDRSTRLEN];
3850 char newip[INET6_ADDRSTRLEN];
3851
3852 memset(oldip, '\0', INET6_ADDRSTRLEN);
3853 memset(newip, '\0', INET6_ADDRSTRLEN);
3854
3855 /* copy old IP address in a string */
3856 switch (s->addr.ss_family) {
3857 case AF_INET:
3858 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3859 break;
3860 case AF_INET6:
3861 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3862 break;
3863 };
3864
3865 /* copy new IP address in a string */
3866 switch (ip_sin_family) {
3867 case AF_INET:
3868 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3869 break;
3870 case AF_INET6:
3871 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3872 break;
3873 };
3874
3875 /* save log line into a buffer */
3876 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3877 s->proxy->id, s->id, oldip, newip, updater);
3878
3879 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003880 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003881
3882 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003883 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003884 }
3885
3886 /* save the new IP family */
3887 s->addr.ss_family = ip_sin_family;
3888 /* save the new IP address */
3889 switch (ip_sin_family) {
3890 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003891 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003892 break;
3893 case AF_INET6:
3894 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3895 break;
3896 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003897 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003898
3899 return 0;
3900}
3901
3902/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003903 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3904 *
3905 * Caller can pass its name through <updater> to get it integrated in the response message
3906 * returned by the function.
3907 *
3908 * The function first does the following, in that order:
3909 * - validates the new addr and/or port
3910 * - checks if an update is required (new IP or port is different than current ones)
3911 * - checks the update is allowed:
3912 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3913 * - allow all changes if no CHECKS are configured
3914 * - if CHECK is configured:
3915 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3916 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3917 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003918 *
3919 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003920 */
3921const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3922{
3923 struct sockaddr_storage sa;
3924 int ret, port_change_required;
3925 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003926 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003927 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003928 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003929
3930 msg = get_trash_chunk();
3931 chunk_reset(msg);
3932
3933 if (addr) {
3934 memset(&sa, 0, sizeof(struct sockaddr_storage));
3935 if (str2ip2(addr, &sa, 0) == NULL) {
3936 chunk_printf(msg, "Invalid addr '%s'", addr);
3937 goto out;
3938 }
3939
3940 /* changes are allowed on AF_INET* families only */
3941 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3942 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3943 goto out;
3944 }
3945
3946 /* collecting data currently setup */
3947 memset(current_addr, '\0', sizeof(current_addr));
3948 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3949 /* changes are allowed on AF_INET* families only */
3950 if ((ret != AF_INET) && (ret != AF_INET6)) {
3951 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3952 goto out;
3953 }
3954
3955 /* applying ADDR changes if required and allowed
3956 * ipcmp returns 0 when both ADDR are the same
3957 */
3958 if (ipcmp(&s->addr, &sa) == 0) {
3959 chunk_appendf(msg, "no need to change the addr");
3960 goto port;
3961 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003962 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003963 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003964
3965 /* we also need to update check's ADDR only if it uses the server's one */
3966 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003967 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003968 }
3969
3970 /* we also need to update agent ADDR only if it use the server's one */
3971 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003972 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003973 }
3974
3975 /* update report for caller */
3976 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3977 }
3978
3979 port:
3980 if (port) {
3981 char sign = '\0';
3982 char *endptr;
3983
3984 if (addr)
3985 chunk_appendf(msg, ", ");
3986
3987 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003988 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003989
3990 /* check if PORT change is required */
3991 port_change_required = 0;
3992
3993 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003994 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003995 new_port = strtol(port, &endptr, 10);
3996 if ((errno != 0) || (port == endptr)) {
3997 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3998 goto out;
3999 }
4000
4001 /* check if caller triggers a port mapped or offset */
4002 if (sign == '-' || (sign == '+')) {
4003 /* check if server currently uses port map */
4004 if (!(s->flags & SRV_F_MAPPORTS)) {
4005 /* switch from fixed port to port map mandatorily triggers
4006 * a port change */
4007 port_change_required = 1;
4008 /* check is configured
4009 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
4010 * prevent PORT change if check doesn't have it's dedicated port while switching
4011 * to port mapping */
4012 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4013 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.");
4014 goto out;
4015 }
4016 }
4017 /* we're already using port maps */
4018 else {
4019 port_change_required = current_port != new_port;
4020 }
4021 }
4022 /* fixed port */
4023 else {
4024 port_change_required = current_port != new_port;
4025 }
4026
4027 /* applying PORT changes if required and update response message */
4028 if (port_change_required) {
4029 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01004030 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01004031 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004032
4033 /* prepare message */
4034 chunk_appendf(msg, "port changed from '");
4035 if (s->flags & SRV_F_MAPPORTS)
4036 chunk_appendf(msg, "+");
4037 chunk_appendf(msg, "%d' to '", current_port);
4038
4039 if (sign == '-') {
4040 s->flags |= SRV_F_MAPPORTS;
4041 chunk_appendf(msg, "%c", sign);
4042 /* just use for result output */
4043 new_port = -new_port;
4044 }
4045 else if (sign == '+') {
4046 s->flags |= SRV_F_MAPPORTS;
4047 chunk_appendf(msg, "%c", sign);
4048 }
4049 else {
4050 s->flags &= ~SRV_F_MAPPORTS;
4051 }
4052
4053 chunk_appendf(msg, "%d'", new_port);
4054
4055 /* we also need to update health checks port only if it uses server's realport */
4056 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4057 s->check.port = new_port;
4058 }
4059 }
4060 else {
4061 chunk_appendf(msg, "no need to change the port");
4062 }
4063 }
4064
4065out:
Olivier Houchard4e694042017-03-14 20:01:29 +01004066 if (changed)
4067 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004068 if (updater)
4069 chunk_appendf(msg, " by '%s'", updater);
4070 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004071 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004072}
4073
4074
4075/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004076 * update server status based on result of name resolution
4077 * returns:
4078 * 0 if server status is updated
4079 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02004080 *
4081 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004082 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004083int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004084{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004085 struct dns_resolvers *resolvers = s->resolvers;
4086 struct dns_resolution *resolution = s->dns_requester->resolution;
4087 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004088
4089 switch (resolution->status) {
4090 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004091 /* status when HAProxy has just (re)started.
4092 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004093 break;
4094
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004095 case RSLV_STATUS_VALID:
4096 /*
4097 * resume health checks
4098 * server will be turned back on if health check is safe
4099 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004100 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02004101 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004102 return 1;
4103 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
4104 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02004105 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004106 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02004107
Emeric Brun52a91d32017-08-31 14:41:55 +02004108 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004109 return 1;
4110 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
4111 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
4112 s->proxy->id, s->id);
4113
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004114 ha_warning("%s.\n", trash.area);
4115 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004116 return 0;
4117
4118 case RSLV_STATUS_NX:
4119 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004120 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
4121 if (!tick_is_expired(exp, now_ms))
4122 break;
4123
4124 if (s->next_admin & SRV_ADMF_RMAINT)
4125 return 1;
4126 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
4127 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004128
4129 case RSLV_STATUS_TIMEOUT:
4130 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004131 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
4132 if (!tick_is_expired(exp, now_ms))
4133 break;
4134
4135 if (s->next_admin & SRV_ADMF_RMAINT)
4136 return 1;
4137 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
4138 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004139
4140 case RSLV_STATUS_REFUSED:
4141 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004142 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
4143 if (!tick_is_expired(exp, now_ms))
4144 break;
4145
4146 if (s->next_admin & SRV_ADMF_RMAINT)
4147 return 1;
4148 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
4149 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004150
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004151 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004152 /* stop server if resolution failed for a long enough period */
4153 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4154 if (!tick_is_expired(exp, now_ms))
4155 break;
4156
4157 if (s->next_admin & SRV_ADMF_RMAINT)
4158 return 1;
4159 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4160 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004161 }
4162
4163 return 1;
4164}
4165
4166/*
4167 * Server Name Resolution valid response callback
4168 * It expects:
4169 * - <nameserver>: the name server which answered the valid response
4170 * - <response>: buffer containing a valid DNS response
4171 * - <response_len>: size of <response>
4172 * It performs the following actions:
4173 * - ignore response if current ip found and server family not met
4174 * - update with first new ip found if family is met and current IP is not found
4175 * returns:
4176 * 0 on error
4177 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004178 *
4179 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004180 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004181int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004182{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004183 struct server *s = NULL;
4184 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004185 void *serverip, *firstip;
4186 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004187 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004188 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004189 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004190
Christopher Faulet67957bd2017-09-27 11:00:59 +02004191 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004192 if (!s)
4193 return 1;
4194
Christopher Faulet67957bd2017-09-27 11:00:59 +02004195 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004196
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004197 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004198 firstip = NULL; /* pointer to the first valid response found */
4199 /* it will be used as the new IP if a change is required */
4200 firstip_sin_family = AF_UNSPEC;
4201 serverip = NULL; /* current server IP address */
4202
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004203 /* initializing server IP pointer */
4204 server_sin_family = s->addr.ss_family;
4205 switch (server_sin_family) {
4206 case AF_INET:
4207 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4208 break;
4209
4210 case AF_INET6:
4211 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4212 break;
4213
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004214 case AF_UNSPEC:
4215 break;
4216
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004217 default:
4218 goto invalid;
4219 }
4220
Baptiste Assmann729c9012017-05-22 15:13:10 +02004221 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004222 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004223 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004224
4225 switch (ret) {
4226 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004227 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004228
4229 case DNS_UPD_SRVIP_NOT_FOUND:
4230 goto save_ip;
4231
4232 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004233 goto invalid;
4234
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004235 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004236 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004237 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004238
Baptiste Assmannfad03182015-10-28 02:03:32 +01004239 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004240 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004241 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004242 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004243
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004244 default:
4245 goto invalid;
4246
4247 }
4248
4249 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004250 if (nameserver) {
4251 nameserver->counters.update++;
4252 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004253 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004254 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004255 else
4256 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004257 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004258
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004259 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004260 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004261 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004262
4263 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004264 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004265 nameserver->counters.invalid++;
4266 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004267 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004268 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004269 return 0;
4270}
4271
4272/*
4273 * Server Name Resolution error management callback
4274 * returns:
4275 * 0 on error
4276 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004277 *
4278 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004279 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004280int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004281{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004282 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004283
Christopher Faulet67957bd2017-09-27 11:00:59 +02004284 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004285 if (!s)
4286 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004287 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004288 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004289 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004290 return 1;
4291}
4292
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004293/*
4294 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004295 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004296 * It returns a pointer to the first server found or NULL if <ip> is not yet
4297 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004298 *
4299 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004300 */
4301struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4302{
4303 struct server *tmpsrv;
4304 struct proxy *be;
4305
4306 if (!srv)
4307 return NULL;
4308
4309 be = srv->proxy;
4310 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004311 /* we found the current server is the same, ignore it */
4312 if (srv == tmpsrv)
4313 continue;
4314
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004315 /* We want to compare the IP in the record with the IP of the servers in the
4316 * same backend, only if:
4317 * * DNS resolution is enabled on the server
4318 * * the hostname used for the resolution by our server is the same than the
4319 * one used for the server found in the backend
4320 * * the server found in the backend is not our current server
4321 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004322 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004323 if ((tmpsrv->hostname_dn == NULL) ||
4324 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4325 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004326 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004327 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004328 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004329 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004330
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004331 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004332 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004333 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004334 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004335 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004336
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004337 /* At this point, we have 2 different servers using the same DNS hostname
4338 * for their respective resolution.
4339 */
4340 if (*ip_family == tmpsrv->addr.ss_family &&
4341 ((tmpsrv->addr.ss_family == AF_INET &&
4342 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4343 (tmpsrv->addr.ss_family == AF_INET6 &&
4344 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004345 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004346 return tmpsrv;
4347 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004348 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004349 }
4350
Emeric Brune9fd6b52017-11-02 17:20:39 +01004351
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004352 return NULL;
4353}
4354
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004355/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4356 * resolver. This is suited for initial address configuration. Returns 0 on
4357 * success otherwise a non-zero error code. In case of error, *err_code, if
4358 * not NULL, is filled up.
4359 */
4360int srv_set_addr_via_libc(struct server *srv, int *err_code)
4361{
4362 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004363 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004364 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004365 return 1;
4366 }
4367 return 0;
4368}
4369
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004370/* Set the server's FDQN (->hostname) from <hostname>.
4371 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004372 *
4373 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004374 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004375int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004376{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004377 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004378 char *hostname_dn;
4379 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004380
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004381 /* Note that the server lock is already held. */
4382 if (!srv->resolvers)
4383 return -1;
4384
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004385 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004386 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004387 /* run time DNS resolution was not active for this server
4388 * and we can't enable it at run time for now.
4389 */
4390 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004391 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004392
4393 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004394 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004395 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004396 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4397 hostname_dn, trash.size);
4398 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004399 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004400
Christopher Faulet67957bd2017-09-27 11:00:59 +02004401 resolution = srv->dns_requester->resolution;
4402 if (resolution &&
4403 resolution->hostname_dn &&
4404 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004405 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004406
Christopher Faulet67957bd2017-09-27 11:00:59 +02004407 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004408
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004409 free(srv->hostname);
4410 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004411 srv->hostname = strdup(hostname);
4412 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004413 srv->hostname_dn_len = hostname_dn_len;
4414 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004415 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004416
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004417 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004418 goto err;
4419
4420 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004421 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004422 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004423 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004424
4425 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004426 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004427 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004428 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004429}
4430
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004431/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4432 * from the state file. This is suited for initial address configuration.
4433 * Returns 0 on success otherwise a non-zero error code. In case of error,
4434 * *err_code, if not NULL, is filled up.
4435 */
4436static int srv_apply_lastaddr(struct server *srv, int *err_code)
4437{
4438 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4439 if (err_code)
4440 *err_code |= ERR_WARN;
4441 return 1;
4442 }
4443 return 0;
4444}
4445
Willy Tarreau25e51522016-11-04 15:10:17 +01004446/* returns 0 if no error, otherwise a combination of ERR_* flags */
4447static int srv_iterate_initaddr(struct server *srv)
4448{
4449 int return_code = 0;
4450 int err_code;
4451 unsigned int methods;
4452
4453 methods = srv->init_addr_methods;
4454 if (!methods) { // default to "last,libc"
4455 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4456 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4457 }
4458
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004459 /* "-dr" : always append "none" so that server addresses resolution
4460 * failures are silently ignored, this is convenient to validate some
4461 * configs out of their environment.
4462 */
4463 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4464 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4465
Willy Tarreau25e51522016-11-04 15:10:17 +01004466 while (methods) {
4467 err_code = 0;
4468 switch (srv_get_next_initaddr(&methods)) {
4469 case SRV_IADDR_LAST:
4470 if (!srv->lastaddr)
4471 continue;
4472 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004473 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004474 return_code |= err_code;
4475 break;
4476
4477 case SRV_IADDR_LIBC:
4478 if (!srv->hostname)
4479 continue;
4480 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004481 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004482 return_code |= err_code;
4483 break;
4484
Willy Tarreau37ebe122016-11-04 15:17:58 +01004485 case SRV_IADDR_NONE:
4486 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004487 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004488 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4489 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004490 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004491 return return_code;
4492
Willy Tarreau4310d362016-11-02 15:05:56 +01004493 case SRV_IADDR_IP:
4494 ipcpy(&srv->init_addr, &srv->addr);
4495 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004496 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4497 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004498 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004499 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004500
Willy Tarreau25e51522016-11-04 15:10:17 +01004501 default: /* unhandled method */
4502 break;
4503 }
4504 }
4505
4506 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004507 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004508 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4509 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004510 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004511 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004512 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4513 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004514
4515 return_code |= ERR_ALERT | ERR_FATAL;
4516 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004517out:
4518 srv_set_dyncookie(srv);
4519 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004520}
4521
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004522/*
4523 * This function parses all backends and all servers within each backend
4524 * and performs servers' addr resolution based on information provided by:
4525 * - configuration file
4526 * - server-state file (states provided by an 'old' haproxy process)
4527 *
4528 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4529 */
4530int srv_init_addr(void)
4531{
4532 struct proxy *curproxy;
4533 int return_code = 0;
4534
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004535 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004536 while (curproxy) {
4537 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004538
4539 /* servers are in backend only */
4540 if (!(curproxy->cap & PR_CAP_BE))
4541 goto srv_init_addr_next;
4542
Willy Tarreau25e51522016-11-04 15:10:17 +01004543 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004544 if (srv->hostname)
4545 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004546
4547 srv_init_addr_next:
4548 curproxy = curproxy->next;
4549 }
4550
4551 return return_code;
4552}
4553
Willy Tarreau46b7f532018-08-21 11:54:26 +02004554/*
4555 * Must be called with the server lock held.
4556 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004557const 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 +02004558{
4559
Willy Tarreau83061a82018-07-13 11:56:34 +02004560 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004561
4562 msg = get_trash_chunk();
4563 chunk_reset(msg);
4564
Olivier Houchard8da5f982017-08-04 18:35:36 +02004565 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004566 chunk_appendf(msg, "no need to change the FDQN");
4567 goto out;
4568 }
4569
4570 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4571 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4572 goto out;
4573 }
4574
4575 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4576 server->proxy->id, server->id, server->hostname, fqdn);
4577
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004578 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004579 chunk_reset(msg);
4580 chunk_appendf(msg, "could not update %s/%s FQDN",
4581 server->proxy->id, server->id);
4582 goto out;
4583 }
4584
4585 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004586 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004587
4588 out:
4589 if (updater)
4590 chunk_appendf(msg, " by '%s'", updater);
4591 chunk_appendf(msg, "\n");
4592
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004593 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004594}
4595
4596
Willy Tarreau21b069d2016-11-23 17:15:08 +01004597/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4598 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004599 * 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 +01004600 * used for CLI commands requiring a server name.
4601 * Important: the <arg> is modified to remove the '/'.
4602 */
4603struct server *cli_find_server(struct appctx *appctx, char *arg)
4604{
4605 struct proxy *px;
4606 struct server *sv;
4607 char *line;
4608
4609 /* split "backend/server" and make <line> point to server */
4610 for (line = arg; *line; line++)
4611 if (*line == '/') {
4612 *line++ = '\0';
4613 break;
4614 }
4615
4616 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004617 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004618 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004619 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004620 return NULL;
4621 }
4622
4623 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004624 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004625 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004626 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004627 return NULL;
4628 }
4629
4630 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004631 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004632 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004633 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004634 return NULL;
4635 }
4636
4637 return sv;
4638}
4639
William Lallemand222baf22016-11-19 02:00:33 +01004640
Willy Tarreau46b7f532018-08-21 11:54:26 +02004641/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004642static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004643{
4644 struct server *sv;
4645 const char *warning;
4646
4647 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4648 return 1;
4649
4650 sv = cli_find_server(appctx, args[2]);
4651 if (!sv)
4652 return 1;
4653
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004654 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004655
William Lallemand222baf22016-11-19 02:00:33 +01004656 if (strcmp(args[3], "weight") == 0) {
4657 warning = server_parse_weight_change_request(sv, args[4]);
4658 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004659 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004660 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004661 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004662 }
4663 }
4664 else if (strcmp(args[3], "state") == 0) {
4665 if (strcmp(args[4], "ready") == 0)
4666 srv_adm_set_ready(sv);
4667 else if (strcmp(args[4], "drain") == 0)
4668 srv_adm_set_drain(sv);
4669 else if (strcmp(args[4], "maint") == 0)
4670 srv_adm_set_maint(sv);
4671 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004672 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004673 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004674 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004675 }
4676 }
4677 else if (strcmp(args[3], "health") == 0) {
4678 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004679 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004680 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004681 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004682 }
4683 else if (strcmp(args[4], "up") == 0) {
4684 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004685 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004686 }
4687 else if (strcmp(args[4], "stopping") == 0) {
4688 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004689 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004690 }
4691 else if (strcmp(args[4], "down") == 0) {
4692 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004693 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004694 }
4695 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004696 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004697 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004698 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004699 }
4700 }
4701 else if (strcmp(args[3], "agent") == 0) {
4702 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004703 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004704 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004705 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004706 }
4707 else if (strcmp(args[4], "up") == 0) {
4708 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004709 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004710 }
4711 else if (strcmp(args[4], "down") == 0) {
4712 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004713 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004714 }
4715 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004716 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004717 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004718 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004719 }
4720 }
Misiek2da082d2017-01-09 09:40:42 +01004721 else if (strcmp(args[3], "agent-addr") == 0) {
4722 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004723 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004724 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4725 appctx->st0 = CLI_ST_PRINT;
4726 } else {
4727 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004728 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004729 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4730 appctx->st0 = CLI_ST_PRINT;
4731 }
4732 }
4733 }
4734 else if (strcmp(args[3], "agent-send") == 0) {
4735 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004736 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004737 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4738 appctx->st0 = CLI_ST_PRINT;
4739 } else {
4740 char *nss = strdup(args[4]);
4741 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004742 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004743 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4744 appctx->st0 = CLI_ST_PRINT;
4745 } else {
4746 free(sv->agent.send_string);
4747 sv->agent.send_string = nss;
4748 sv->agent.send_string_len = strlen(args[4]);
4749 }
4750 }
4751 }
William Lallemand222baf22016-11-19 02:00:33 +01004752 else if (strcmp(args[3], "check-port") == 0) {
4753 int i = 0;
4754 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004755 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004756 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004757 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004758 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004759 }
4760 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004761 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004762 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004763 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004764 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004765 }
4766 /* prevent the update of port to 0 if MAPPORTS are in use */
4767 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004768 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004769 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004770 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004771 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004772 }
4773 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004774 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004775 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004776 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004777 }
4778 else if (strcmp(args[3], "addr") == 0) {
4779 char *addr = NULL;
4780 char *port = NULL;
4781 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004782 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004783 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004784 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004785 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004786 }
4787 else {
4788 addr = args[4];
4789 }
4790 if (strcmp(args[5], "port") == 0) {
4791 port = args[6];
4792 }
4793 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4794 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004795 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004796 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004797 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004798 }
4799 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4800 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004801 else if (strcmp(args[3], "fqdn") == 0) {
4802 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004803 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004804 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4805 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004806 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004807 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004808 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004809 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004810 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004811 appctx->ctx.cli.msg = warning;
4812 appctx->st0 = CLI_ST_PRINT;
4813 }
4814 }
William Lallemand222baf22016-11-19 02:00:33 +01004815 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004816 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004817 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 +01004818 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004819 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004820 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004821 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004822 return 1;
4823}
4824
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004825static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004826{
4827 struct stream_interface *si = appctx->owner;
4828 struct proxy *px;
4829 struct server *sv;
4830 char *line;
4831
4832
4833 /* split "backend/server" and make <line> point to server */
4834 for (line = args[2]; *line; line++)
4835 if (*line == '/') {
4836 *line++ = '\0';
4837 break;
4838 }
4839
4840 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004841 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004842 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004843 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004844 return 1;
4845 }
4846
4847 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004848 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004849 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004850 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004851 return 1;
4852 }
4853
4854 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004855 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4856 sv->iweight);
4857 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004858 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004859 return 0;
4860 }
William Lallemand6b160942016-11-22 12:34:35 +01004861 return 1;
4862}
4863
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004864/* Parse a "set weight" command.
4865 *
4866 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004867 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004868static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004869{
4870 struct server *sv;
4871 const char *warning;
4872
4873 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4874 return 1;
4875
4876 sv = cli_find_server(appctx, args[2]);
4877 if (!sv)
4878 return 1;
4879
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004880 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4881
William Lallemand6b160942016-11-22 12:34:35 +01004882 warning = server_parse_weight_change_request(sv, args[3]);
4883 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004884 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004885 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004886 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004887 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004888
4889 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4890
William Lallemand6b160942016-11-22 12:34:35 +01004891 return 1;
4892}
4893
Willy Tarreau46b7f532018-08-21 11:54:26 +02004894/* parse a "set maxconn server" command. It always returns 1.
4895 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004896 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004897 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004898static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004899{
4900 struct server *sv;
4901 const char *warning;
4902
4903 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4904 return 1;
4905
4906 sv = cli_find_server(appctx, args[3]);
4907 if (!sv)
4908 return 1;
4909
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004910 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4911
Willy Tarreaub8026272016-11-23 11:26:56 +01004912 warning = server_parse_maxconn_change_request(sv, args[4]);
4913 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004914 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004915 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004916 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004917 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004918
4919 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4920
Willy Tarreaub8026272016-11-23 11:26:56 +01004921 return 1;
4922}
William Lallemand6b160942016-11-22 12:34:35 +01004923
Willy Tarreau46b7f532018-08-21 11:54:26 +02004924/* parse a "disable agent" command. It always returns 1.
4925 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004926 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004927 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004928static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004929{
4930 struct server *sv;
4931
4932 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4933 return 1;
4934
4935 sv = cli_find_server(appctx, args[2]);
4936 if (!sv)
4937 return 1;
4938
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004939 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004940 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004941 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004942 return 1;
4943}
4944
Willy Tarreau46b7f532018-08-21 11:54:26 +02004945/* parse a "disable health" command. It always returns 1.
4946 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004947 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004948 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004949static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004950{
4951 struct server *sv;
4952
4953 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4954 return 1;
4955
4956 sv = cli_find_server(appctx, args[2]);
4957 if (!sv)
4958 return 1;
4959
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004960 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004961 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004962 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004963 return 1;
4964}
4965
Willy Tarreau46b7f532018-08-21 11:54:26 +02004966/* parse a "disable server" command. It always returns 1.
4967 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004968 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004969 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004970static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004971{
4972 struct server *sv;
4973
4974 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4975 return 1;
4976
4977 sv = cli_find_server(appctx, args[2]);
4978 if (!sv)
4979 return 1;
4980
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004981 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004982 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004983 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004984 return 1;
4985}
4986
Willy Tarreau46b7f532018-08-21 11:54:26 +02004987/* parse a "enable agent" command. It always returns 1.
4988 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004989 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004990 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004991static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004992{
4993 struct server *sv;
4994
4995 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4996 return 1;
4997
4998 sv = cli_find_server(appctx, args[2]);
4999 if (!sv)
5000 return 1;
5001
5002 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02005003 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005004 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01005005 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005006 return 1;
5007 }
5008
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005009 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005010 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005011 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005012 return 1;
5013}
5014
Willy Tarreau46b7f532018-08-21 11:54:26 +02005015/* parse a "enable health" command. It always returns 1.
5016 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005017 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02005018 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005019static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005020{
5021 struct server *sv;
5022
5023 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5024 return 1;
5025
5026 sv = cli_find_server(appctx, args[2]);
5027 if (!sv)
5028 return 1;
5029
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005030 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005031 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005032 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005033 return 1;
5034}
5035
Willy Tarreau46b7f532018-08-21 11:54:26 +02005036/* parse a "enable server" command. It always returns 1.
5037 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005038 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02005039 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005040static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01005041{
5042 struct server *sv;
5043
5044 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5045 return 1;
5046
5047 sv = cli_find_server(appctx, args[2]);
5048 if (!sv)
5049 return 1;
5050
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005051 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005052 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01005053 if (!(sv->flags & SRV_F_COOKIESET)
5054 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
5055 sv->cookie)
5056 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005057 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005058 return 1;
5059}
5060
William Lallemand222baf22016-11-19 02:00:33 +01005061/* register cli keywords */
5062static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005063 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005064 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005065 { { "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 +01005066 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005067 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005068 { { "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 +01005069 { { "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 +01005070 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01005071 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
5072 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
5073
William Lallemand222baf22016-11-19 02:00:33 +01005074 {{},}
5075}};
5076
Willy Tarreau0108d902018-11-25 19:14:37 +01005077INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005078
Emeric Brun64cc49c2017-10-03 14:46:45 +02005079/*
5080 * This function applies server's status changes, it is
5081 * is designed to be called asynchronously.
5082 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02005083 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005084 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005085static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005086{
5087 struct check *check = &s->check;
5088 int xferred;
5089 struct proxy *px = s->proxy;
5090 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5091 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5092 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005093 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005094
Emeric Brun64cc49c2017-10-03 14:46:45 +02005095 /* If currently main is not set we try to apply pending state changes */
5096 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5097 int next_admin;
5098
5099 /* Backup next admin */
5100 next_admin = s->next_admin;
5101
5102 /* restore current admin state */
5103 s->next_admin = s->cur_admin;
5104
5105 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5106 s->last_change = now.tv_sec;
5107 if (s->proxy->lbprm.set_server_status_down)
5108 s->proxy->lbprm.set_server_status_down(s);
5109
5110 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5111 srv_shutdown_streams(s, SF_ERR_DOWN);
5112
5113 /* we might have streams queued on this server and waiting for
5114 * a connection. Those which are redispatchable will be queued
5115 * to another server or to the proxy itself.
5116 */
5117 xferred = pendconn_redistribute(s);
5118
5119 tmptrash = alloc_trash_chunk();
5120 if (tmptrash) {
5121 chunk_printf(tmptrash,
5122 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5123 s->proxy->id, s->id);
5124
Emeric Brun5a133512017-10-19 14:42:30 +02005125 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005126 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005127
5128 /* we don't send an alert if the server was previously paused */
5129 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005130 send_log(s->proxy, log_level, "%s.\n",
5131 tmptrash->area);
5132 send_email_alert(s, log_level, "%s",
5133 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005134 free_trash_chunk(tmptrash);
5135 tmptrash = NULL;
5136 }
5137 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5138 set_backend_down(s->proxy);
5139
5140 s->counters.down_trans++;
5141 }
5142 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5143 s->last_change = now.tv_sec;
5144 if (s->proxy->lbprm.set_server_status_down)
5145 s->proxy->lbprm.set_server_status_down(s);
5146
5147 /* we might have streams queued on this server and waiting for
5148 * a connection. Those which are redispatchable will be queued
5149 * to another server or to the proxy itself.
5150 */
5151 xferred = pendconn_redistribute(s);
5152
5153 tmptrash = alloc_trash_chunk();
5154 if (tmptrash) {
5155 chunk_printf(tmptrash,
5156 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5157 s->proxy->id, s->id);
5158
Emeric Brun5a133512017-10-19 14:42:30 +02005159 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005160
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005161 ha_warning("%s.\n", tmptrash->area);
5162 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5163 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005164 free_trash_chunk(tmptrash);
5165 tmptrash = NULL;
5166 }
5167
5168 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5169 set_backend_down(s->proxy);
5170 }
5171 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5172 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5173 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5174 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5175 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5176 s->proxy->last_change = now.tv_sec;
5177 }
5178
5179 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5180 s->down_time += now.tv_sec - s->last_change;
5181
5182 s->last_change = now.tv_sec;
5183 if (s->next_state == SRV_ST_STARTING)
5184 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5185
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005186 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005187 /* now propagate the status change to any LB algorithms */
5188 if (px->lbprm.update_server_eweight)
5189 px->lbprm.update_server_eweight(s);
5190 else if (srv_willbe_usable(s)) {
5191 if (px->lbprm.set_server_status_up)
5192 px->lbprm.set_server_status_up(s);
5193 }
5194 else {
5195 if (px->lbprm.set_server_status_down)
5196 px->lbprm.set_server_status_down(s);
5197 }
5198
5199 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5200 * and it's not a backup server and its effective weight is > 0,
5201 * then it can accept new connections, so we shut down all streams
5202 * on all backup servers.
5203 */
5204 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5205 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5206 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5207
5208 /* check if we can handle some connections queued at the proxy. We
5209 * will take as many as we can handle.
5210 */
5211 xferred = pendconn_grab_from_px(s);
5212
5213 tmptrash = alloc_trash_chunk();
5214 if (tmptrash) {
5215 chunk_printf(tmptrash,
5216 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5217 s->proxy->id, s->id);
5218
Emeric Brun5a133512017-10-19 14:42:30 +02005219 srv_append_status(tmptrash, s, NULL, xferred, 0);
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);
5223 send_email_alert(s, LOG_NOTICE, "%s",
5224 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005225 free_trash_chunk(tmptrash);
5226 tmptrash = NULL;
5227 }
5228
5229 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5230 set_backend_down(s->proxy);
5231 }
5232 else if (s->cur_eweight != s->next_eweight) {
5233 /* now propagate the status change to any LB algorithms */
5234 if (px->lbprm.update_server_eweight)
5235 px->lbprm.update_server_eweight(s);
5236 else if (srv_willbe_usable(s)) {
5237 if (px->lbprm.set_server_status_up)
5238 px->lbprm.set_server_status_up(s);
5239 }
5240 else {
5241 if (px->lbprm.set_server_status_down)
5242 px->lbprm.set_server_status_down(s);
5243 }
5244
5245 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5246 set_backend_down(s->proxy);
5247 }
5248
5249 s->next_admin = next_admin;
5250 }
5251
Emeric Brun5a133512017-10-19 14:42:30 +02005252 /* reset operational state change */
5253 *s->op_st_chg.reason = 0;
5254 s->op_st_chg.status = s->op_st_chg.code = -1;
5255 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005256
5257 /* Now we try to apply pending admin changes */
5258
5259 /* Maintenance must also disable health checks */
5260 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5261 if (s->check.state & CHK_ST_ENABLED) {
5262 s->check.state |= CHK_ST_PAUSED;
5263 check->health = 0;
5264 }
5265
5266 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005267 tmptrash = alloc_trash_chunk();
5268 if (tmptrash) {
5269 chunk_printf(tmptrash,
5270 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5271 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5272 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005273
Olivier Houchard796a2b32017-10-24 17:42:47 +02005274 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005275
Olivier Houchard796a2b32017-10-24 17:42:47 +02005276 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005277 ha_warning("%s.\n", tmptrash->area);
5278 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5279 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005280 }
5281 free_trash_chunk(tmptrash);
5282 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005283 }
Emeric Brun8f298292017-12-06 16:47:17 +01005284 /* commit new admin status */
5285
5286 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005287 }
5288 else { /* server was still running */
5289 check->health = 0; /* failure */
5290 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005291
5292 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005293 if (s->proxy->lbprm.set_server_status_down)
5294 s->proxy->lbprm.set_server_status_down(s);
5295
Emeric Brun64cc49c2017-10-03 14:46:45 +02005296 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5297 srv_shutdown_streams(s, SF_ERR_DOWN);
5298
5299 /* we might have streams queued on this server and waiting for
5300 * a connection. Those which are redispatchable will be queued
5301 * to another server or to the proxy itself.
5302 */
5303 xferred = pendconn_redistribute(s);
5304
5305 tmptrash = alloc_trash_chunk();
5306 if (tmptrash) {
5307 chunk_printf(tmptrash,
5308 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5309 s->flags & SRV_F_BACKUP ? "Backup " : "",
5310 s->proxy->id, s->id,
5311 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5312
5313 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5314
5315 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005316 ha_warning("%s.\n", tmptrash->area);
5317 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5318 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005319 }
5320 free_trash_chunk(tmptrash);
5321 tmptrash = NULL;
5322 }
5323 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5324 set_backend_down(s->proxy);
5325
5326 s->counters.down_trans++;
5327 }
5328 }
5329 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5330 /* OK here we're leaving maintenance, we have many things to check,
5331 * because the server might possibly be coming back up depending on
5332 * its state. In practice, leaving maintenance means that we should
5333 * immediately turn to UP (more or less the slowstart) under the
5334 * following conditions :
5335 * - server is neither checked nor tracked
5336 * - server tracks another server which is not checked
5337 * - server tracks another server which is already up
5338 * Which sums up as something simpler :
5339 * "either the tracking server is up or the server's checks are disabled
5340 * or up". Otherwise we only re-enable health checks. There's a special
5341 * case associated to the stopping state which can be inherited. Note
5342 * that the server might still be in drain mode, which is naturally dealt
5343 * with by the lower level functions.
5344 */
5345
5346 if (s->check.state & CHK_ST_ENABLED) {
5347 s->check.state &= ~CHK_ST_PAUSED;
5348 check->health = check->rise; /* start OK but check immediately */
5349 }
5350
5351 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5352 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5353 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5354 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5355 s->next_state = SRV_ST_STOPPING;
5356 }
5357 else {
5358 s->next_state = SRV_ST_STARTING;
5359 if (s->slowstart > 0)
5360 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5361 else
5362 s->next_state = SRV_ST_RUNNING;
5363 }
5364
5365 }
5366
5367 tmptrash = alloc_trash_chunk();
5368 if (tmptrash) {
5369 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5370 chunk_printf(tmptrash,
5371 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5372 s->flags & SRV_F_BACKUP ? "Backup " : "",
5373 s->proxy->id, s->id,
5374 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5375 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5376 }
5377 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5378 chunk_printf(tmptrash,
5379 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5380 s->flags & SRV_F_BACKUP ? "Backup " : "",
5381 s->proxy->id, s->id, s->hostname,
5382 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5383 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5384 }
5385 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5386 chunk_printf(tmptrash,
5387 "%sServer %s/%s is %s/%s (leaving maintenance)",
5388 s->flags & SRV_F_BACKUP ? "Backup " : "",
5389 s->proxy->id, s->id,
5390 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5391 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5392 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005393 ha_warning("%s.\n", tmptrash->area);
5394 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5395 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005396 free_trash_chunk(tmptrash);
5397 tmptrash = NULL;
5398 }
5399
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005400 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005401 /* now propagate the status change to any LB algorithms */
5402 if (px->lbprm.update_server_eweight)
5403 px->lbprm.update_server_eweight(s);
5404 else if (srv_willbe_usable(s)) {
5405 if (px->lbprm.set_server_status_up)
5406 px->lbprm.set_server_status_up(s);
5407 }
5408 else {
5409 if (px->lbprm.set_server_status_down)
5410 px->lbprm.set_server_status_down(s);
5411 }
5412
5413 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5414 set_backend_down(s->proxy);
5415
Willy Tarreau6a78e612018-08-07 10:14:53 +02005416 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5417 * and it's not a backup server and its effective weight is > 0,
5418 * then it can accept new connections, so we shut down all streams
5419 * on all backup servers.
5420 */
5421 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5422 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5423 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5424
5425 /* check if we can handle some connections queued at the proxy. We
5426 * will take as many as we can handle.
5427 */
5428 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005429 }
5430 else if (s->next_admin & SRV_ADMF_MAINT) {
5431 /* remaining in maintenance mode, let's inform precisely about the
5432 * situation.
5433 */
5434 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5435 tmptrash = alloc_trash_chunk();
5436 if (tmptrash) {
5437 chunk_printf(tmptrash,
5438 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5439 s->flags & SRV_F_BACKUP ? "Backup " : "",
5440 s->proxy->id, s->id);
5441
5442 if (s->track) /* normally it's mandatory here */
5443 chunk_appendf(tmptrash, " via %s/%s",
5444 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005445 ha_warning("%s.\n", tmptrash->area);
5446 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5447 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005448 free_trash_chunk(tmptrash);
5449 tmptrash = NULL;
5450 }
5451 }
5452 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5453 tmptrash = alloc_trash_chunk();
5454 if (tmptrash) {
5455 chunk_printf(tmptrash,
5456 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5457 s->flags & SRV_F_BACKUP ? "Backup " : "",
5458 s->proxy->id, s->id, s->hostname);
5459
5460 if (s->track) /* normally it's mandatory here */
5461 chunk_appendf(tmptrash, " via %s/%s",
5462 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005463 ha_warning("%s.\n", tmptrash->area);
5464 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5465 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005466 free_trash_chunk(tmptrash);
5467 tmptrash = NULL;
5468 }
5469 }
5470 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5471 tmptrash = alloc_trash_chunk();
5472 if (tmptrash) {
5473 chunk_printf(tmptrash,
5474 "%sServer %s/%s remains in forced maintenance",
5475 s->flags & SRV_F_BACKUP ? "Backup " : "",
5476 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005477 ha_warning("%s.\n", tmptrash->area);
5478 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5479 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005480 free_trash_chunk(tmptrash);
5481 tmptrash = NULL;
5482 }
5483 }
5484 /* don't report anything when leaving drain mode and remaining in maintenance */
5485
5486 s->cur_admin = s->next_admin;
5487 }
5488
5489 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5490 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5491 /* drain state is applied only if not yet in maint */
5492
5493 s->last_change = now.tv_sec;
5494 if (px->lbprm.set_server_status_down)
5495 px->lbprm.set_server_status_down(s);
5496
5497 /* we might have streams queued on this server and waiting for
5498 * a connection. Those which are redispatchable will be queued
5499 * to another server or to the proxy itself.
5500 */
5501 xferred = pendconn_redistribute(s);
5502
5503 tmptrash = alloc_trash_chunk();
5504 if (tmptrash) {
5505 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5506 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5507 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5508
5509 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5510
5511 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005512 ha_warning("%s.\n", tmptrash->area);
5513 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5514 tmptrash->area);
5515 send_email_alert(s, LOG_NOTICE, "%s",
5516 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005517 }
5518 free_trash_chunk(tmptrash);
5519 tmptrash = NULL;
5520 }
5521
5522 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5523 set_backend_down(s->proxy);
5524 }
5525 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5526 /* OK completely leaving drain mode */
5527 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5528 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5529 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5530 s->proxy->last_change = now.tv_sec;
5531 }
5532
5533 if (s->last_change < now.tv_sec) // ignore negative times
5534 s->down_time += now.tv_sec - s->last_change;
5535 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005536 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005537
5538 tmptrash = alloc_trash_chunk();
5539 if (tmptrash) {
5540 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5541 chunk_printf(tmptrash,
5542 "%sServer %s/%s is %s (leaving forced drain)",
5543 s->flags & SRV_F_BACKUP ? "Backup " : "",
5544 s->proxy->id, s->id,
5545 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5546 }
5547 else {
5548 chunk_printf(tmptrash,
5549 "%sServer %s/%s is %s (leaving drain)",
5550 s->flags & SRV_F_BACKUP ? "Backup " : "",
5551 s->proxy->id, s->id,
5552 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5553 if (s->track) /* normally it's mandatory here */
5554 chunk_appendf(tmptrash, " via %s/%s",
5555 s->track->proxy->id, s->track->id);
5556 }
5557
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005558 ha_warning("%s.\n", tmptrash->area);
5559 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5560 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005561 free_trash_chunk(tmptrash);
5562 tmptrash = NULL;
5563 }
5564
5565 /* now propagate the status change to any LB algorithms */
5566 if (px->lbprm.update_server_eweight)
5567 px->lbprm.update_server_eweight(s);
5568 else if (srv_willbe_usable(s)) {
5569 if (px->lbprm.set_server_status_up)
5570 px->lbprm.set_server_status_up(s);
5571 }
5572 else {
5573 if (px->lbprm.set_server_status_down)
5574 px->lbprm.set_server_status_down(s);
5575 }
5576 }
5577 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5578 /* remaining in drain mode after removing one of its flags */
5579
5580 tmptrash = alloc_trash_chunk();
5581 if (tmptrash) {
5582 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5583 chunk_printf(tmptrash,
5584 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5585 s->flags & SRV_F_BACKUP ? "Backup " : "",
5586 s->proxy->id, s->id);
5587
5588 if (s->track) /* normally it's mandatory here */
5589 chunk_appendf(tmptrash, " via %s/%s",
5590 s->track->proxy->id, s->track->id);
5591 }
5592 else {
5593 chunk_printf(tmptrash,
5594 "%sServer %s/%s remains in forced drain mode",
5595 s->flags & SRV_F_BACKUP ? "Backup " : "",
5596 s->proxy->id, s->id);
5597 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005598 ha_warning("%s.\n", tmptrash->area);
5599 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5600 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005601 free_trash_chunk(tmptrash);
5602 tmptrash = NULL;
5603 }
5604
5605 /* commit new admin status */
5606
5607 s->cur_admin = s->next_admin;
5608 }
5609 }
5610
5611 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005612 *s->adm_st_chg_cause = 0;
5613}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005614
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005615struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5616{
5617 struct connection *conn;
5618
5619 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5620 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005621 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005622 }
5623
5624 return task;
5625}
5626
Willy Tarreau980855b2019-02-07 14:59:29 +01005627struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005628{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005629 struct server *srv;
5630 struct eb32_node *eb;
5631 int i;
5632 unsigned int next_wakeup;
5633 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005634
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005635 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5636 while (1) {
5637 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005638
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005639 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5640 if (!eb) {
5641 /* we might have reached the end of the tree, typically because
5642 * <now_ms> is in the first half and we're first scanning the last
5643 * half. Let's loop back to the beginning of the tree now.
5644 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005645
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005646 eb = eb32_first(&idle_conn_srv);
5647 if (likely(!eb))
5648 break;
5649 }
5650 if (tick_is_lt(now_ms, eb->key)) {
5651 /* timer not expired yet, revisit it later */
5652 next_wakeup = eb->key;
5653 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005654 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005655 }
5656 srv = eb32_entry(eb, struct server, idle_node);
5657 for (i = 0; i < global.nbthread; i++) {
5658 int max_conn = (srv->curr_idle_thr[i] / 2) +
5659 (srv->curr_idle_thr[i] & 1);
5660 int j;
5661 int did_remove = 0;
5662
5663 for (j = 0; j < max_conn; j++) {
5664 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5665 if (!conn)
5666 break;
5667 did_remove = 1;
5668 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5669 }
5670 if (did_remove && max_conn < srv->curr_idle_thr[i])
5671 srv_is_empty = 0;
5672 if (did_remove)
5673 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5674 }
5675 eb32_delete(&srv->idle_node);
5676 if (!srv_is_empty) {
5677 /* There are still more idle connections, add the
5678 * server back in the tree.
5679 */
5680 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5681 now_ms);
5682 eb32_insert(&idle_conn_srv, &srv->idle_node);
5683 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005684 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005685 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5686
5687 if (need_wakeup)
5688 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005689 else
5690 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005691
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005692 return task;
5693}
Olivier Houchard88698d92019-04-16 19:07:22 +02005694
5695/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5696static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5697 struct proxy *defpx, const char *file, int line,
5698 char **err)
5699{
5700 int arg = -1;
5701
5702 if (too_many_args(1, args, err, NULL))
5703 return -1;
5704
5705 if (*(args[1]) != 0)
5706 arg = atoi(args[1]);
5707
5708 if (arg < 0 || arg > 100) {
5709 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5710 return -1;
5711 }
5712
5713 if (args[0][10] == 'h')
5714 global.tune.pool_high_ratio = arg;
5715 else
5716 global.tune.pool_low_ratio = arg;
5717 return 0;
5718}
5719
5720/* config keyword parsers */
5721static struct cfg_kw_list cfg_kws = {ILH, {
5722 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5723 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5724 { 0, NULL, NULL }
5725}};
5726
5727INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5728
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005729/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005730 * Local variables:
5731 * c-indent-level: 8
5732 * c-basic-offset: 8
5733 * End:
5734 */