blob: 1a9dd16173e366b321514c2ab64f36ec533b0e33 [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écaillef9bc1d62017-03-10 15:50:49 +0100586/* Parse the "non-stick" server keyword */
587static int srv_parse_non_stick(char **args, int *cur_arg,
588 struct proxy *curproxy, struct server *newsrv, char **err)
589{
590 newsrv->flags |= SRV_F_NON_STICK;
591 return 0;
592}
593
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100594/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200595static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100596{
597 srv->pp_opts |= flags;
598 return 0;
599}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200600/* parse the "proto" server keyword */
601static int srv_parse_proto(char **args, int *cur_arg,
602 struct proxy *px, struct server *newsrv, char **err)
603{
604 struct ist proto;
605
606 if (!*args[*cur_arg + 1]) {
607 memprintf(err, "'%s' : missing value", args[*cur_arg]);
608 return ERR_ALERT | ERR_FATAL;
609 }
610 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
611 newsrv->mux_proto = get_mux_proto(proto);
612 if (!newsrv->mux_proto) {
613 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
614 return ERR_ALERT | ERR_FATAL;
615 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200616 return 0;
617}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100618
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100619/* parse the "proxy-v2-options" */
620static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
621 struct proxy *px, struct server *newsrv, char **err)
622{
623 char *p, *n;
624 for (p = args[*cur_arg+1]; p; p = n) {
625 n = strchr(p, ',');
626 if (n)
627 *n++ = '\0';
628 if (!strcmp(p, "ssl")) {
629 newsrv->pp_opts |= SRV_PP_V2_SSL;
630 } else if (!strcmp(p, "cert-cn")) {
631 newsrv->pp_opts |= SRV_PP_V2_SSL;
632 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100633 } else if (!strcmp(p, "cert-key")) {
634 newsrv->pp_opts |= SRV_PP_V2_SSL;
635 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
636 } else if (!strcmp(p, "cert-sig")) {
637 newsrv->pp_opts |= SRV_PP_V2_SSL;
638 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
639 } else if (!strcmp(p, "ssl-cipher")) {
640 newsrv->pp_opts |= SRV_PP_V2_SSL;
641 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100642 } else if (!strcmp(p, "authority")) {
643 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100644 } else if (!strcmp(p, "crc32c")) {
645 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100646 } else
647 goto fail;
648 }
649 return 0;
650 fail:
651 if (err)
652 memprintf(err, "'%s' : proxy v2 option not implemented", p);
653 return ERR_ALERT | ERR_FATAL;
654}
655
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100656/* Parse the "observe" server keyword */
657static int srv_parse_observe(char **args, int *cur_arg,
658 struct proxy *curproxy, struct server *newsrv, char **err)
659{
660 char *arg;
661
662 arg = args[*cur_arg + 1];
663 if (!*arg) {
664 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
665 return ERR_ALERT | ERR_FATAL;
666 }
667
668 if (!strcmp(arg, "none")) {
669 newsrv->observe = HANA_OBS_NONE;
670 }
671 else if (!strcmp(arg, "layer4")) {
672 newsrv->observe = HANA_OBS_LAYER4;
673 }
674 else if (!strcmp(arg, "layer7")) {
675 if (curproxy->mode != PR_MODE_HTTP) {
676 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
677 return ERR_ALERT;
678 }
679 newsrv->observe = HANA_OBS_LAYER7;
680 }
681 else {
682 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
683 "but got '%s'\n", args[*cur_arg], arg);
684 return ERR_ALERT | ERR_FATAL;
685 }
686
687 return 0;
688}
689
Frédéric Lécaille16186232017-03-14 16:42:49 +0100690/* Parse the "redir" server keyword */
691static int srv_parse_redir(char **args, int *cur_arg,
692 struct proxy *curproxy, struct server *newsrv, char **err)
693{
694 char *arg;
695
696 arg = args[*cur_arg + 1];
697 if (!*arg) {
698 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
699 return ERR_ALERT | ERR_FATAL;
700 }
701
702 free(newsrv->rdr_pfx);
703 newsrv->rdr_pfx = strdup(arg);
704 newsrv->rdr_len = strlen(arg);
705
706 return 0;
707}
708
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100709/* Parse the "send-proxy" server keyword */
710static int srv_parse_send_proxy(char **args, int *cur_arg,
711 struct proxy *curproxy, struct server *newsrv, char **err)
712{
713 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
714}
715
716/* Parse the "send-proxy-v2" server keyword */
717static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
718 struct proxy *curproxy, struct server *newsrv, char **err)
719{
720 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
721}
722
Frédéric Lécailledba97072017-03-17 15:33:50 +0100723
724/* Parse the "source" server keyword */
725static int srv_parse_source(char **args, int *cur_arg,
726 struct proxy *curproxy, struct server *newsrv, char **err)
727{
728 char *errmsg;
729 int port_low, port_high;
730 struct sockaddr_storage *sk;
731 struct protocol *proto;
732
733 errmsg = NULL;
734
735 if (!*args[*cur_arg + 1]) {
736 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
737 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
738 goto err;
739 }
740
741 /* 'sk' is statically allocated (no need to be freed). */
742 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
743 if (!sk) {
744 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
745 goto err;
746 }
747
748 proto = protocol_by_family(sk->ss_family);
749 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100750 ha_alert("'%s %s' : connect() not supported for this address family.\n",
751 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100752 goto err;
753 }
754
755 newsrv->conn_src.opts |= CO_SRC_BIND;
756 newsrv->conn_src.source_addr = *sk;
757
758 if (port_low != port_high) {
759 int i;
760
761 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100762 ha_alert("'%s' does not support port offsets (found '%s').\n",
763 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100764 goto err;
765 }
766
767 if (port_low <= 0 || port_low > 65535 ||
768 port_high <= 0 || port_high > 65535 ||
769 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100770 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 +0100771 goto err;
772 }
773 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
774 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
775 newsrv->conn_src.sport_range->ports[i] = port_low + i;
776 }
777
778 *cur_arg += 2;
779 while (*(args[*cur_arg])) {
780 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
781#if defined(CONFIG_HAP_TRANSPARENT)
782 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
784 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100785 goto err;
786 }
787 if (!strcmp(args[*cur_arg + 1], "client")) {
788 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
789 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
790 }
791 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
792 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
793 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
794 }
795 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
796 char *name, *end;
797
798 name = args[*cur_arg + 1] + 7;
799 while (isspace(*name))
800 name++;
801
802 end = name;
803 while (*end && !isspace(*end) && *end != ',' && *end != ')')
804 end++;
805
806 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
807 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
808 free(newsrv->conn_src.bind_hdr_name);
809 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
810 newsrv->conn_src.bind_hdr_len = end - name;
811 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
812 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
813 newsrv->conn_src.bind_hdr_occ = -1;
814
815 /* now look for an occurrence number */
816 while (isspace(*end))
817 end++;
818 if (*end == ',') {
819 end++;
820 name = end;
821 if (*end == '-')
822 end++;
823 while (isdigit((int)*end))
824 end++;
825 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
826 }
827
828 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100829 ha_alert("usesrc hdr_ip(name,num) does not support negative"
830 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100831 goto err;
832 }
833 }
834 else {
835 struct sockaddr_storage *sk;
836 int port1, port2;
837
838 /* 'sk' is statically allocated (no need to be freed). */
839 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
840 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100841 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100842 goto err;
843 }
844
845 proto = protocol_by_family(sk->ss_family);
846 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100847 ha_alert("'%s %s' : connect() not supported for this address family.\n",
848 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100849 goto err;
850 }
851
852 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100853 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
854 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100855 goto err;
856 }
857 newsrv->conn_src.tproxy_addr = *sk;
858 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
859 }
860 global.last_checks |= LSTCHK_NETADM;
861 *cur_arg += 2;
862 continue;
863#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100864 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 +0100865 goto err;
866#endif /* defined(CONFIG_HAP_TRANSPARENT) */
867 } /* "usesrc" */
868
869 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
870#ifdef SO_BINDTODEVICE
871 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100872 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100873 goto err;
874 }
875 free(newsrv->conn_src.iface_name);
876 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
877 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
878 global.last_checks |= LSTCHK_NETADM;
879#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100880 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100881 goto err;
882#endif
883 *cur_arg += 2;
884 continue;
885 }
886 /* this keyword in not an option of "source" */
887 break;
888 } /* while */
889
890 return 0;
891
892 err:
893 free(errmsg);
894 return ERR_ALERT | ERR_FATAL;
895}
896
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100897/* Parse the "stick" server keyword */
898static int srv_parse_stick(char **args, int *cur_arg,
899 struct proxy *curproxy, struct server *newsrv, char **err)
900{
901 newsrv->flags &= ~SRV_F_NON_STICK;
902 return 0;
903}
904
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100905/* Parse the "track" server keyword */
906static int srv_parse_track(char **args, int *cur_arg,
907 struct proxy *curproxy, struct server *newsrv, char **err)
908{
909 char *arg;
910
911 arg = args[*cur_arg + 1];
912 if (!*arg) {
913 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
914 return ERR_ALERT | ERR_FATAL;
915 }
916
917 free(newsrv->trackit);
918 newsrv->trackit = strdup(arg);
919
920 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800921}
922
923/* Parse the "socks4" server keyword */
924static int srv_parse_socks4(char **args, int *cur_arg,
925 struct proxy *curproxy, struct server *newsrv, char **err)
926{
927 char *errmsg;
928 int port_low, port_high;
929 struct sockaddr_storage *sk;
930 struct protocol *proto;
931
932 errmsg = NULL;
933
934 if (!*args[*cur_arg + 1]) {
935 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
936 goto err;
937 }
938
939 /* 'sk' is statically allocated (no need to be freed). */
940 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
941 if (!sk) {
942 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
943 goto err;
944 }
945
946 proto = protocol_by_family(sk->ss_family);
947 if (!proto || !proto->connect) {
948 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
949 goto err;
950 }
951
952 newsrv->flags |= SRV_F_SOCKS4_PROXY;
953 newsrv->socks4_addr = *sk;
954
955 if (port_low != port_high) {
956 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
957 goto err;
958 }
959
960 if (!port_low) {
961 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
962 goto err;
963 }
964
965 return 0;
966
967 err:
968 free(errmsg);
969 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100970}
971
Frédéric Lécailledba97072017-03-17 15:33:50 +0100972
Willy Tarreau034c88c2017-01-23 23:36:45 +0100973/* parse the "tfo" server keyword */
974static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
975{
976 newsrv->flags |= SRV_F_FASTOPEN;
977 return 0;
978}
979
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200980/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200981 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200982 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200983 *
984 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200985 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200986void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200987{
Willy Tarreau87b09662015-04-03 00:22:06 +0200988 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200989
Willy Tarreau87b09662015-04-03 00:22:06 +0200990 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
991 if (stream->srv_conn == srv)
992 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200993}
994
995/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200996 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200997 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200998 *
999 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001000 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001001void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001002{
1003 struct server *srv;
1004
1005 for (srv = px->srv; srv != NULL; srv = srv->next)
1006 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001007 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001008}
1009
Willy Tarreaubda92272014-05-20 21:55:30 +02001010/* Appends some information to a message string related to a server going UP or
1011 * DOWN. If both <forced> and <reason> are null and the server tracks another
1012 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001013 * If <check> is non-null, an entire string describing the check result will be
1014 * appended after a comma and a space (eg: to report some information from the
1015 * check that changed the state). In the other case, the string will be built
1016 * using the check results stored into the struct server if present.
1017 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001018 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001019 *
1020 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001021 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001022void srv_append_status(struct buffer *msg, struct server *s,
1023 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001024{
Emeric Brun5a133512017-10-19 14:42:30 +02001025 short status = s->op_st_chg.status;
1026 short code = s->op_st_chg.code;
1027 long duration = s->op_st_chg.duration;
1028 char *desc = s->op_st_chg.reason;
1029
1030 if (check) {
1031 status = check->status;
1032 code = check->code;
1033 duration = check->duration;
1034 desc = check->desc;
1035 }
1036
1037 if (status != -1) {
1038 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1039
1040 if (status >= HCHK_STATUS_L57DATA)
1041 chunk_appendf(msg, ", code: %d", code);
1042
1043 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001044 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001045
1046 chunk_appendf(msg, ", info: \"");
1047
1048 chunk_initlen(&src, desc, 0, strlen(desc));
1049 chunk_asciiencode(msg, &src, '"');
1050
1051 chunk_appendf(msg, "\"");
1052 }
1053
1054 if (duration >= 0)
1055 chunk_appendf(msg, ", check duration: %ldms", duration);
1056 }
1057 else if (desc && *desc) {
1058 chunk_appendf(msg, ", %s", desc);
1059 }
1060 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001061 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001062 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001063
1064 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001065 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001066 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1067 " %d sessions active, %d requeued, %d remaining in queue",
1068 s->proxy->srv_act, s->proxy->srv_bck,
1069 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1070 s->cur_sess, xferred, s->nbpend);
1071 else
1072 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1073 " %d sessions requeued, %d total in queue",
1074 s->proxy->srv_act, s->proxy->srv_bck,
1075 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1076 xferred, s->nbpend);
1077 }
1078}
1079
Emeric Brun5a133512017-10-19 14:42:30 +02001080/* Marks server <s> down, regardless of its checks' statuses. The server is
1081 * registered in a list to postpone the counting of the remaining servers on
1082 * the proxy and transfers queued streams whenever possible to other servers at
1083 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1084 * non-null as the reason for going down or the available data from the check
1085 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001086 *
1087 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001088 */
Emeric Brun5a133512017-10-19 14:42:30 +02001089void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001090{
1091 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001092
Emeric Brun64cc49c2017-10-03 14:46:45 +02001093 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001094 return;
1095
Emeric Brun52a91d32017-08-31 14:41:55 +02001096 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001097 *s->op_st_chg.reason = 0;
1098 s->op_st_chg.status = -1;
1099 if (reason) {
1100 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1101 }
1102 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001103 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001104 s->op_st_chg.code = check->code;
1105 s->op_st_chg.status = check->status;
1106 s->op_st_chg.duration = check->duration;
1107 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001108
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001109 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001110 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001111
Emeric Brun9f0b4582017-10-23 14:39:51 +02001112 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001113 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001114 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001115 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001116 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001117}
1118
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001119/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001120 * in maintenance. The server is registered in a list to postpone the counting
1121 * of the remaining servers on the proxy and tries to grab requests from the
1122 * proxy at a sync point. Maintenance servers are ignored. It stores the
1123 * <reason> if non-null as the reason for going down or the available data
1124 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001125 *
1126 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001127 */
Emeric Brun5a133512017-10-19 14:42:30 +02001128void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001129{
1130 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001131
Emeric Brun64cc49c2017-10-03 14:46:45 +02001132 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001133 return;
1134
Emeric Brun52a91d32017-08-31 14:41:55 +02001135 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001136 return;
1137
Emeric Brun52a91d32017-08-31 14:41:55 +02001138 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001139 *s->op_st_chg.reason = 0;
1140 s->op_st_chg.status = -1;
1141 if (reason) {
1142 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1143 }
1144 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001145 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001146 s->op_st_chg.code = check->code;
1147 s->op_st_chg.status = check->status;
1148 s->op_st_chg.duration = check->duration;
1149 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001150
Emeric Brun64cc49c2017-10-03 14:46:45 +02001151 if (s->slowstart <= 0)
1152 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001153
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001154 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001155 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001156
Emeric Brun9f0b4582017-10-23 14:39:51 +02001157 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001158 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001159 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001160 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001161 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001162}
1163
Willy Tarreau8eb77842014-05-21 13:54:57 +02001164/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001165 * isn't in maintenance. The server is registered in a list to postpone the
1166 * counting of the remaining servers on the proxy and tries to grab requests
1167 * from the proxy. Maintenance servers are ignored. It stores the
1168 * <reason> if non-null as the reason for going down or the available data
1169 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001170 * up. Note that it makes use of the trash to build the log strings, so <reason>
1171 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001172 *
1173 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001174 */
Emeric Brun5a133512017-10-19 14:42:30 +02001175void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001176{
1177 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001178
Emeric Brun64cc49c2017-10-03 14:46:45 +02001179 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001180 return;
1181
Emeric Brun52a91d32017-08-31 14:41:55 +02001182 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001183 return;
1184
Emeric Brun52a91d32017-08-31 14:41:55 +02001185 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001186 *s->op_st_chg.reason = 0;
1187 s->op_st_chg.status = -1;
1188 if (reason) {
1189 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1190 }
1191 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001192 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001193 s->op_st_chg.code = check->code;
1194 s->op_st_chg.status = check->status;
1195 s->op_st_chg.duration = check->duration;
1196 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001197
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001198 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001199 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001200
Emeric Brun9f0b4582017-10-23 14:39:51 +02001201 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001202 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001203 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001204 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001205 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001206}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001207
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001208/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1209 * enforce either maint mode or drain mode. It is not allowed to set more than
1210 * one flag at once. The equivalent "inherited" flag is propagated to all
1211 * tracking servers. Maintenance mode disables health checks (but not agent
1212 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001213 * is done. If <cause> is non-null, it will be displayed at the end of the log
1214 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001215 *
1216 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001217 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001218void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001219{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001220 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001221
1222 if (!mode)
1223 return;
1224
1225 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001226 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001227 return;
1228
Emeric Brun52a91d32017-08-31 14:41:55 +02001229 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001230 if (cause)
1231 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1232
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001233 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001234 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001235
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001236 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001237 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1238 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001239 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001240
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001241 /* compute the inherited flag to propagate */
1242 if (mode & SRV_ADMF_MAINT)
1243 mode = SRV_ADMF_IMAINT;
1244 else if (mode & SRV_ADMF_DRAIN)
1245 mode = SRV_ADMF_IDRAIN;
1246
Emeric Brun9f0b4582017-10-23 14:39:51 +02001247 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001248 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001249 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001250 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001251 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001252}
1253
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001254/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1255 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1256 * than one flag at once. The equivalent "inherited" flag is propagated to all
1257 * tracking servers. Leaving maintenance mode re-enables health checks. When
1258 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001259 *
1260 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001261 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001262void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001263{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001264 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001265
1266 if (!mode)
1267 return;
1268
1269 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001270 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001271 return;
1272
Emeric Brun52a91d32017-08-31 14:41:55 +02001273 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001274
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001275 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001276 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001277
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001278 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001279 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1280 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001281 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001282
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001283 if (mode & SRV_ADMF_MAINT)
1284 mode = SRV_ADMF_IMAINT;
1285 else if (mode & SRV_ADMF_DRAIN)
1286 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001287
Emeric Brun9f0b4582017-10-23 14:39:51 +02001288 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001289 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001290 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001291 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001292 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001293}
1294
Willy Tarreau757478e2016-11-03 19:22:19 +01001295/* principle: propagate maint and drain to tracking servers. This is useful
1296 * upon startup so that inherited states are correct.
1297 */
1298static void srv_propagate_admin_state(struct server *srv)
1299{
1300 struct server *srv2;
1301
1302 if (!srv->trackers)
1303 return;
1304
1305 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001306 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001307 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001308 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001309
Emeric Brun52a91d32017-08-31 14:41:55 +02001310 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001311 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001312 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001313 }
1314}
1315
1316/* Compute and propagate the admin states for all servers in proxy <px>.
1317 * Only servers *not* tracking another one are considered, because other
1318 * ones will be handled when the server they track is visited.
1319 */
1320void srv_compute_all_admin_states(struct proxy *px)
1321{
1322 struct server *srv;
1323
1324 for (srv = px->srv; srv; srv = srv->next) {
1325 if (srv->track)
1326 continue;
1327 srv_propagate_admin_state(srv);
1328 }
1329}
1330
Willy Tarreaudff55432012-10-10 17:51:05 +02001331/* Note: must not be declared <const> as its list will be overwritten.
1332 * Please take care of keeping this list alphabetically sorted, doing so helps
1333 * all code contributors.
1334 * Optional keywords are also declared with a NULL ->parse() function so that
1335 * the config parser can report an appropriate error when a known keyword was
1336 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001337 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001338 */
1339static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001340 { "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 +01001341 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001342 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001343 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001344 { "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 +01001345 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001346 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1347 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001348 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001349 { "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 +01001350 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001351 { "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 +01001352 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001353 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001354 { "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 +01001355 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1356 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001357 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001358 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001359 { "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 +01001360 { "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 +02001361 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001362 { "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 +01001363 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001364 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1365 { "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 +02001366 { "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 +01001367 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Willy Tarreau034c88c2017-01-23 23:36:45 +01001368 { "tfo", srv_parse_tfo, 0, 0 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001369 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001370 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1371 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001372 { NULL, NULL, 0 },
1373}};
1374
Willy Tarreau0108d902018-11-25 19:14:37 +01001375INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001376
Willy Tarreau004e0452013-11-21 11:22:01 +01001377/* Recomputes the server's eweight based on its state, uweight, the current time,
1378 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001379 * state is automatically disabled if the time is elapsed. If <must_update> is
1380 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001381 *
1382 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001383 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001384void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001385{
1386 struct proxy *px = sv->proxy;
1387 unsigned w;
1388
1389 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1390 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001391 if (sv->next_state == SRV_ST_STARTING)
1392 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001393 }
1394
1395 /* We must take care of not pushing the server to full throttle during slow starts.
1396 * It must also start immediately, at least at the minimal step when leaving maintenance.
1397 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001398 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001399 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1400 else
1401 w = px->lbprm.wdiv;
1402
Emeric Brun52a91d32017-08-31 14:41:55 +02001403 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001404
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001405 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001406 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001407 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001408}
1409
Willy Tarreaubaaee002006-06-26 02:48:02 +02001410/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001411 * Parses weight_str and configures sv accordingly.
1412 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001413 *
1414 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001415 */
1416const char *server_parse_weight_change_request(struct server *sv,
1417 const char *weight_str)
1418{
1419 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001420 long int w;
1421 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001422
1423 px = sv->proxy;
1424
1425 /* if the weight is terminated with '%', it is set relative to
1426 * the initial weight, otherwise it is absolute.
1427 */
1428 if (!*weight_str)
1429 return "Require <weight> or <weight%>.\n";
1430
Simon Hormanb796afa2013-02-12 10:45:53 +09001431 w = strtol(weight_str, &end, 10);
1432 if (end == weight_str)
1433 return "Empty weight string empty or preceded by garbage";
1434 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001435 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001436 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001437 /* Avoid integer overflow */
1438 if (w > 25600)
1439 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001440 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001441 if (w > 256)
1442 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001443 }
1444 else if (w < 0 || w > 256)
1445 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001446 else if (end[0] != '\0')
1447 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001448
1449 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1450 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1451
1452 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001453 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001454
1455 return NULL;
1456}
1457
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001458/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001459 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1460 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001461 * Returns:
1462 * - error string on error
1463 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001464 *
1465 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001466 */
1467const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001468 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001469{
1470 unsigned char ip[INET6_ADDRSTRLEN];
1471
1472 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001473 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001474 return NULL;
1475 }
1476 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001477 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001478 return NULL;
1479 }
1480
1481 return "Could not understand IP address format.\n";
1482}
1483
Willy Tarreau46b7f532018-08-21 11:54:26 +02001484/*
1485 * Must be called with the server lock held.
1486 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001487const char *server_parse_maxconn_change_request(struct server *sv,
1488 const char *maxconn_str)
1489{
1490 long int v;
1491 char *end;
1492
1493 if (!*maxconn_str)
1494 return "Require <maxconn>.\n";
1495
1496 v = strtol(maxconn_str, &end, 10);
1497 if (end == maxconn_str)
1498 return "maxconn string empty or preceded by garbage";
1499 else if (end[0] != '\0')
1500 return "Trailing garbage in maxconn string";
1501
1502 if (sv->maxconn == sv->minconn) { // static maxconn
1503 sv->maxconn = sv->minconn = v;
1504 } else { // dynamic maxconn
1505 sv->maxconn = v;
1506 }
1507
1508 if (may_dequeue_tasks(sv, sv->proxy))
1509 process_srv_queue(sv);
1510
1511 return NULL;
1512}
1513
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001514#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001515static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1516 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001517{
1518 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001519 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001520 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001521 NULL,
1522 };
1523
1524 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001525 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001526
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001527 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1528}
1529
1530static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1531{
1532 struct sample_expr *expr;
1533
1534 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 +01001535 if (!expr) {
1536 memprintf(err, "error detected while parsing sni expression : %s", *err);
1537 return ERR_ALERT | ERR_FATAL;
1538 }
1539
1540 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1541 memprintf(err, "error detected while parsing sni expression : "
1542 " fetch method '%s' extracts information from '%s', "
1543 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001544 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001545 return ERR_ALERT | ERR_FATAL;
1546 }
1547
1548 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1549 release_sample_expr(newsrv->ssl_ctx.sni);
1550 newsrv->ssl_ctx.sni = expr;
1551
1552 return 0;
1553}
1554#endif
1555
1556static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1557{
1558 if (err && *err) {
1559 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001560 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 +01001561 }
1562 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001563 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1564 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001565}
1566
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001567static void srv_conn_src_sport_range_cpy(struct server *srv,
1568 struct server *src)
1569{
1570 int range_sz;
1571
1572 range_sz = src->conn_src.sport_range->size;
1573 if (range_sz > 0) {
1574 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1575 if (srv->conn_src.sport_range != NULL) {
1576 int i;
1577
1578 for (i = 0; i < range_sz; i++) {
1579 srv->conn_src.sport_range->ports[i] =
1580 src->conn_src.sport_range->ports[i];
1581 }
1582 }
1583 }
1584}
1585
1586/*
1587 * Copy <src> server connection source settings to <srv> server everything needed.
1588 */
1589static void srv_conn_src_cpy(struct server *srv, struct server *src)
1590{
1591 srv->conn_src.opts = src->conn_src.opts;
1592 srv->conn_src.source_addr = src->conn_src.source_addr;
1593
1594 /* Source port range copy. */
1595 if (src->conn_src.sport_range != NULL)
1596 srv_conn_src_sport_range_cpy(srv, src);
1597
1598#ifdef CONFIG_HAP_TRANSPARENT
1599 if (src->conn_src.bind_hdr_name != NULL) {
1600 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1601 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1602 }
1603 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1604 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1605#endif
1606 if (src->conn_src.iface_name != NULL)
1607 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1608}
1609
1610/*
1611 * Copy <src> server SSL settings to <srv> server allocating
1612 * everything needed.
1613 */
1614#if defined(USE_OPENSSL)
1615static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1616{
1617 if (src->ssl_ctx.ca_file != NULL)
1618 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1619 if (src->ssl_ctx.crl_file != NULL)
1620 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1621 if (src->ssl_ctx.client_crt != NULL)
1622 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1623
1624 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1625
1626 if (src->ssl_ctx.verify_host != NULL)
1627 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1628 if (src->ssl_ctx.ciphers != NULL)
1629 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001630#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001631 if (src->ssl_ctx.ciphersuites != NULL)
1632 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1633#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001634 if (src->sni_expr != NULL)
1635 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001636
1637#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1638 if (src->ssl_ctx.alpn_str) {
1639 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1640 if (srv->ssl_ctx.alpn_str) {
1641 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1642 src->ssl_ctx.alpn_len);
1643 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1644 }
1645 }
1646#endif
1647#ifdef OPENSSL_NPN_NEGOTIATED
1648 if (src->ssl_ctx.npn_str) {
1649 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1650 if (srv->ssl_ctx.npn_str) {
1651 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1652 src->ssl_ctx.npn_len);
1653 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1654 }
1655 }
1656#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001657}
1658#endif
1659
1660/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001661 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001662 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001663 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001664 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001665static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001666{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001667 char *hostname_dn;
1668 int hostname_len, hostname_dn_len;
1669
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001670 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001671 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001672
Christopher Faulet67957bd2017-09-27 11:00:59 +02001673 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001674 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001675 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1676 hostname_dn, trash.size);
1677 if (hostname_dn_len == -1)
1678 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001679
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001680
Christopher Faulet67957bd2017-09-27 11:00:59 +02001681 free(srv->hostname);
1682 free(srv->hostname_dn);
1683 srv->hostname = strdup(hostname);
1684 srv->hostname_dn = strdup(hostname_dn);
1685 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001686 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001687 goto err;
1688
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001689 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001690
1691 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692 free(srv->hostname); srv->hostname = NULL;
1693 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001694 return -1;
1695}
1696
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001697/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001698 * Copy <src> server settings to <srv> server allocating
1699 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001700 * This function is not supposed to be called at any time, but only
1701 * during server settings parsing or during server allocations from
1702 * a server template, and just after having calloc()'ed a new server.
1703 * So, <src> may only be a default server (when parsing server settings)
1704 * or a server template (during server allocations from a server template).
1705 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1706 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001707 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001708static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001709{
1710 /* Connection source settings copy */
1711 srv_conn_src_cpy(srv, src);
1712
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001713 if (srv_tmpl) {
1714 srv->addr = src->addr;
1715 srv->svc_port = src->svc_port;
1716 }
1717
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001718 srv->pp_opts = src->pp_opts;
1719 if (src->rdr_pfx != NULL) {
1720 srv->rdr_pfx = strdup(src->rdr_pfx);
1721 srv->rdr_len = src->rdr_len;
1722 }
1723 if (src->cookie != NULL) {
1724 srv->cookie = strdup(src->cookie);
1725 srv->cklen = src->cklen;
1726 }
1727 srv->use_ssl = src->use_ssl;
1728 srv->check.addr = srv->agent.addr = src->check.addr;
1729 srv->check.use_ssl = src->check.use_ssl;
1730 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001731 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001732 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001733 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001734 /* Note: 'flags' field has potentially been already initialized. */
1735 srv->flags |= src->flags;
1736 srv->do_check = src->do_check;
1737 srv->do_agent = src->do_agent;
1738 if (srv->check.port)
1739 srv->flags |= SRV_F_CHECKPORT;
1740 srv->check.inter = src->check.inter;
1741 srv->check.fastinter = src->check.fastinter;
1742 srv->check.downinter = src->check.downinter;
1743 srv->agent.use_ssl = src->agent.use_ssl;
1744 srv->agent.port = src->agent.port;
1745 if (src->agent.send_string != NULL)
1746 srv->agent.send_string = strdup(src->agent.send_string);
1747 srv->agent.send_string_len = src->agent.send_string_len;
1748 srv->agent.inter = src->agent.inter;
1749 srv->agent.fastinter = src->agent.fastinter;
1750 srv->agent.downinter = src->agent.downinter;
1751 srv->maxqueue = src->maxqueue;
1752 srv->minconn = src->minconn;
1753 srv->maxconn = src->maxconn;
1754 srv->slowstart = src->slowstart;
1755 srv->observe = src->observe;
1756 srv->onerror = src->onerror;
1757 srv->onmarkeddown = src->onmarkeddown;
1758 srv->onmarkedup = src->onmarkedup;
1759 if (src->trackit != NULL)
1760 srv->trackit = strdup(src->trackit);
1761 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1762 srv->uweight = srv->iweight = src->iweight;
1763
1764 srv->check.send_proxy = src->check.send_proxy;
1765 /* health: up, but will fall down at first failure */
1766 srv->check.rise = srv->check.health = src->check.rise;
1767 srv->check.fall = src->check.fall;
1768
1769 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001770 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1771 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1772 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001773 srv->check.state |= CHK_ST_PAUSED;
1774 srv->check.health = 0;
1775 }
1776
1777 /* health: up but will fall down at first failure */
1778 srv->agent.rise = srv->agent.health = src->agent.rise;
1779 srv->agent.fall = src->agent.fall;
1780
1781 if (src->resolvers_id != NULL)
1782 srv->resolvers_id = strdup(src->resolvers_id);
1783 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001784 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001785 if (srv->dns_opts.family_prio == AF_UNSPEC)
1786 srv->dns_opts.family_prio = AF_INET6;
1787 memcpy(srv->dns_opts.pref_net,
1788 src->dns_opts.pref_net,
1789 sizeof srv->dns_opts.pref_net);
1790 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1791
1792 srv->init_addr_methods = src->init_addr_methods;
1793 srv->init_addr = src->init_addr;
1794#if defined(USE_OPENSSL)
1795 srv_ssl_settings_cpy(srv, src);
1796#endif
1797#ifdef TCP_USER_TIMEOUT
1798 srv->tcp_ut = src->tcp_ut;
1799#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001800 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001801 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001802 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001803 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001804
Olivier Houchard8da5f982017-08-04 18:35:36 +02001805 if (srv_tmpl)
1806 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001807
1808 srv->check.via_socks4 = src->check.via_socks4;
1809 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001810}
1811
William Lallemand313bfd12018-10-26 14:47:32 +02001812struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001813{
1814 struct server *srv;
1815
1816 srv = calloc(1, sizeof *srv);
1817 if (!srv)
1818 return NULL;
1819
1820 srv->obj_type = OBJ_TYPE_SERVER;
1821 srv->proxy = proxy;
1822 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001823 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001824
Emeric Brun52a91d32017-08-31 14:41:55 +02001825 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001826 srv->last_change = now.tv_sec;
1827
1828 srv->check.status = HCHK_STATUS_INI;
1829 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001830 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001831 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1832
1833 srv->agent.status = HCHK_STATUS_INI;
1834 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001835 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001836 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1837
Willy Tarreau975b1552019-06-06 16:25:55 +02001838 /* please don't put default server settings here, they are set in
1839 * init_default_instance().
1840 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001841 return srv;
1842}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001843
1844/*
1845 * Validate <srv> server health-check settings.
1846 * Returns 0 if everything is OK, -1 if not.
1847 */
1848static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1849{
1850 struct tcpcheck_rule *r = NULL;
1851 struct list *l;
1852
1853 /*
1854 * We need at least a service port, a check port or the first tcp-check rule must
1855 * be a 'connect' one when checking an IPv4/IPv6 server.
1856 */
1857 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1858 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1859 return 0;
1860
1861 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1862 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001863 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1864 "Check has been disabled.\n",
1865 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001866 return -1;
1867 }
1868
1869 /* search the first action (connect / send / expect) in the list */
1870 l = &srv->proxy->tcpcheck_rules;
1871 list_for_each_entry(r, l, list) {
1872 if (r->action != TCPCHK_ACT_COMMENT)
1873 break;
1874 }
1875
1876 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001877 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1878 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1879 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001880 return -1;
1881 }
1882
1883 /* scan the tcp-check ruleset to ensure a port has been configured */
1884 l = &srv->proxy->tcpcheck_rules;
1885 list_for_each_entry(r, l, list) {
1886 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001887 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1888 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1889 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001890 return -1;
1891 }
1892 }
1893
1894 return 0;
1895}
1896
1897/*
1898 * Initialize <srv> health-check structure.
1899 * Returns the error string in case of memory allocation failure, NULL if not.
1900 */
1901static const char *do_health_check_init(struct server *srv, int check_type, int state)
1902{
1903 const char *ret;
1904
1905 if (!srv->do_check)
1906 return NULL;
1907
1908 ret = init_check(&srv->check, check_type);
1909 if (ret)
1910 return ret;
1911
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001912 srv->check.state |= state;
1913 global.maxsock++;
1914
1915 return NULL;
1916}
1917
1918static int server_health_check_init(const char *file, int linenum,
1919 struct server *srv, struct proxy *curproxy)
1920{
1921 const char *ret;
1922
1923 if (!srv->do_check)
1924 return 0;
1925
1926 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001927 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1928 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001929 return ERR_ALERT | ERR_FATAL;
1930 }
1931
1932 if (server_healthcheck_validate(file, linenum, srv) < 0)
1933 return ERR_ALERT | ERR_ABORT;
1934
1935 /* note: check type will be set during the config review phase */
1936 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1937 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001938 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001939 return ERR_ALERT | ERR_ABORT;
1940 }
1941
1942 return 0;
1943}
1944
1945/*
1946 * Initialize <srv> agent check structure.
1947 * Returns the error string in case of memory allocation failure, NULL if not.
1948 */
1949static const char *do_server_agent_check_init(struct server *srv, int state)
1950{
1951 const char *ret;
1952
1953 if (!srv->do_agent)
1954 return NULL;
1955
1956 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1957 if (ret)
1958 return ret;
1959
1960 if (!srv->agent.inter)
1961 srv->agent.inter = srv->check.inter;
1962
1963 srv->agent.state |= state;
1964 global.maxsock++;
1965
1966 return NULL;
1967}
1968
1969static int server_agent_check_init(const char *file, int linenum,
1970 struct server *srv, struct proxy *curproxy)
1971{
1972 const char *ret;
1973
1974 if (!srv->do_agent)
1975 return 0;
1976
1977 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001978 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 +02001979 file, linenum, srv->id);
1980 return ERR_ALERT | ERR_FATAL;
1981 }
1982
1983 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1984 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001985 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001986 return ERR_ALERT | ERR_ABORT;
1987 }
1988
1989 return 0;
1990}
1991
1992#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1993static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1994 struct server *srv, struct proxy *proxy)
1995{
1996 int ret;
1997 char *err = NULL;
1998
1999 if (!srv->sni_expr)
2000 return 0;
2001
2002 ret = server_parse_sni_expr(srv, proxy, &err);
2003 if (!ret)
2004 return 0;
2005
2006 display_parser_err(file, linenum, args, cur_arg, &err);
2007 free(err);
2008
2009 return ret;
2010}
2011#endif
2012
2013/*
2014 * Server initializations finalization.
2015 * Initialize health check, agent check and SNI expression if enabled.
2016 * Must not be called for a default server instance.
2017 */
2018static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2019 struct server *srv, struct proxy *px)
2020{
2021 int ret;
2022
2023 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2024 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2025 return ret;
2026 }
2027
2028#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2029 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2030 return ret;
2031#endif
2032
2033 if (srv->flags & SRV_F_BACKUP)
2034 px->srv_bck++;
2035 else
2036 px->srv_act++;
2037 srv_lb_commit_status(srv);
2038
2039 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002040err:
2041 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002042}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002043
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002044/*
2045 * Parse as much as possible such a range string argument: low[-high]
2046 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2047 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2048 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2049 * Returns 0 if succeeded, -1 if not.
2050 */
2051static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2052{
2053 char *nb_high_arg;
2054
2055 *nb_high = 0;
2056 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002057 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002058
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002059 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002060 *nb_high_arg++ = '\0';
2061 *nb_high = atoi(nb_high_arg);
2062 }
2063 else {
2064 *nb_high += *nb_low;
2065 *nb_low = 1;
2066 }
2067
2068 if (*nb_low < 0 || *nb_high < *nb_low)
2069 return -1;
2070
2071 return 0;
2072}
2073
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002074static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2075{
2076 chunk_printf(&trash, "%s%d", prefix, nb);
2077 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002078 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002079}
2080
2081/*
2082 * Initialize as much as possible servers from <srv> server template.
2083 * Note that a server template is a special server with
2084 * a few different parameters than a server which has
2085 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002086 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002087 * 'srv' template included.
2088 */
2089static int server_template_init(struct server *srv, struct proxy *px)
2090{
2091 int i;
2092 struct server *newsrv;
2093
2094 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2095 int check_init_state;
2096 int agent_init_state;
2097
2098 newsrv = new_server(px);
2099 if (!newsrv)
2100 goto err;
2101
2102 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002103 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002104#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2105 if (newsrv->sni_expr) {
2106 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2107 if (!newsrv->ssl_ctx.sni)
2108 goto err;
2109 }
2110#endif
2111 /* Set this new server ID. */
2112 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2113
2114 /* Initial checks states. */
2115 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2116 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2117
2118 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2119 do_server_agent_check_init(newsrv, agent_init_state))
2120 goto err;
2121
2122 /* Linked backwards first. This will be restablished after parsing. */
2123 newsrv->next = px->srv;
2124 px->srv = newsrv;
2125 }
2126 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2127
2128 return i - srv->tmpl_info.nb_low;
2129
2130 err:
2131 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2132 if (newsrv) {
2133#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2134 release_sample_expr(newsrv->ssl_ctx.sni);
2135#endif
2136 free_check(&newsrv->agent);
2137 free_check(&newsrv->check);
2138 }
2139 free(newsrv);
2140 return i - srv->tmpl_info.nb_low;
2141}
2142
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002143int 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 +02002144{
2145 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002146 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002147 char *errmsg = NULL;
2148 int err_code = 0;
2149 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002150 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002151
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002152 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002153 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002154 !strcmp(args[0], "default-server") ||
2155 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002156 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002157 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002158 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002159 int srv_tmpl = !defsrv && !srv;
2160 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002161
2162 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002163 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002164 err_code |= ERR_ALERT | ERR_FATAL;
2165 goto out;
2166 }
2167 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002168 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002169
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002170 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002171 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002172 if (!*args[2]) {
2173 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002174 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002175 file, linenum, args[0]);
2176 err_code |= ERR_ALERT | ERR_FATAL;
2177 goto out;
2178 }
2179
2180 err = invalid_char(args[1]);
2181 }
2182 else if (srv_tmpl) {
2183 if (!*args[3]) {
2184 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002185 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 +02002186 file, linenum, args[0]);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
2189 }
2190
2191 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002192 }
2193
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002194 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002195 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 +02002196 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002197 err_code |= ERR_ALERT | ERR_FATAL;
2198 goto out;
2199 }
2200
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002201 cur_arg = 2;
2202 if (srv_tmpl) {
2203 /* Parse server-template <nb | range> arg. */
2204 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002205 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002206 file, linenum, args[0], args[cur_arg]);
2207 err_code |= ERR_ALERT | ERR_FATAL;
2208 goto out;
2209 }
2210 cur_arg++;
2211 }
2212
Willy Tarreau272adea2014-03-31 10:39:59 +02002213 if (!defsrv) {
2214 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002215 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 struct protocol *proto;
2217
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002218 newsrv = new_server(curproxy);
2219 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002220 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002221 err_code |= ERR_ALERT | ERR_ABORT;
2222 goto out;
2223 }
2224
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002225 if (srv_tmpl) {
2226 newsrv->tmpl_info.nb_low = tmpl_range_low;
2227 newsrv->tmpl_info.nb_high = tmpl_range_high;
2228 }
2229
Willy Tarreau272adea2014-03-31 10:39:59 +02002230 /* the servers are linked backwards first */
2231 newsrv->next = curproxy->srv;
2232 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002233 newsrv->conf.file = strdup(file);
2234 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002235 /* Note: for a server template, its id is its prefix.
2236 * This is a temporary id which will be used for server allocations to come
2237 * after parsing.
2238 */
2239 if (srv)
2240 newsrv->id = strdup(args[1]);
2241 else
2242 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002243
2244 /* several ways to check the port component :
2245 * - IP => port=+0, relative (IPv4 only)
2246 * - IP: => port=+0, relative
2247 * - IP:N => port=N, absolute
2248 * - IP:+N => port=+N, relative
2249 * - IP:-N => port=-N, relative
2250 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002251 if (!parse_addr)
2252 goto skip_addr;
2253
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002254 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002255 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002256 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002257 err_code |= ERR_ALERT | ERR_FATAL;
2258 goto out;
2259 }
2260
2261 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002262 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002263 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002264 file, linenum, args[0], args[1]);
2265 err_code |= ERR_ALERT | ERR_FATAL;
2266 goto out;
2267 }
2268
2269 if (!port1 || !port2) {
2270 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002271 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002272 }
2273 else if (port1 != port2) {
2274 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002275 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002276 file, linenum, args[0], args[1], args[2]);
2277 err_code |= ERR_ALERT | ERR_FATAL;
2278 goto out;
2279 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002280
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002281 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002282 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002283 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002284 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002285 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2286 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2287 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002288 err_code |= ERR_ALERT | ERR_FATAL;
2289 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002290 }
2291 }
2292 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002293 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002294 file, linenum, newsrv->id);
2295 err_code |= ERR_ALERT | ERR_FATAL;
2296 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002297 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002298 }
2299
Willy Tarreau272adea2014-03-31 10:39:59 +02002300 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002301 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002302
Olivier Houchard8da5f982017-08-04 18:35:36 +02002303 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002304 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002305 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002306 err_code |= ERR_ALERT | ERR_FATAL;
2307 goto out;
2308 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002309
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002310 cur_arg++;
2311 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002312 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002313 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002314 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002315 } else {
2316 newsrv = &curproxy->defsrv;
2317 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002318 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002319 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002320 }
2321
2322 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002323 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002324 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002325
2326 if (err == PARSE_TIME_OVER) {
2327 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2328 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2329 err_code |= ERR_ALERT | ERR_FATAL;
2330 goto out;
2331 }
2332 else if (err == PARSE_TIME_UNDER) {
2333 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2334 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2335 err_code |= ERR_ALERT | ERR_FATAL;
2336 goto out;
2337 }
2338 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002339 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002340 file, linenum, *err, newsrv->id);
2341 err_code |= ERR_ALERT | ERR_FATAL;
2342 goto out;
2343 }
2344 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002345 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002346 file, linenum, val, args[cur_arg], newsrv->id);
2347 err_code |= ERR_ALERT | ERR_FATAL;
2348 goto out;
2349 }
2350 newsrv->agent.inter = val;
2351 cur_arg += 2;
2352 }
Misiekea849332017-01-09 09:39:51 +01002353 else if (!strcmp(args[cur_arg], "agent-addr")) {
2354 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002355 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002356 goto out;
2357 }
2358
2359 cur_arg += 2;
2360 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002361 else if (!strcmp(args[cur_arg], "agent-port")) {
2362 global.maxsock++;
2363 newsrv->agent.port = atol(args[cur_arg + 1]);
2364 cur_arg += 2;
2365 }
James Brown55f9ff12015-10-21 18:19:05 -07002366 else if (!strcmp(args[cur_arg], "agent-send")) {
2367 global.maxsock++;
2368 free(newsrv->agent.send_string);
2369 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2370 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2371 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2372 cur_arg += 2;
2373 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002374 else if (!strcmp(args[cur_arg], "init-addr")) {
2375 char *p, *end;
2376 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002377 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002378
2379 newsrv->init_addr_methods = 0;
2380 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2381
2382 for (p = args[cur_arg + 1]; *p; p = end) {
2383 /* cut on next comma */
2384 for (end = p; *end && *end != ','; end++);
2385 if (*end)
2386 *(end++) = 0;
2387
Willy Tarreau4310d362016-11-02 15:05:56 +01002388 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002389 if (!strcmp(p, "libc")) {
2390 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2391 }
2392 else if (!strcmp(p, "last")) {
2393 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2394 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002395 else if (!strcmp(p, "none")) {
2396 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2397 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002398 else if (str2ip2(p, &sa, 0)) {
2399 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002400 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002401 file, linenum, args[cur_arg], p);
2402 err_code |= ERR_ALERT | ERR_FATAL;
2403 goto out;
2404 }
2405 newsrv->init_addr = sa;
2406 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2407 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002408 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002409 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 +02002410 file, linenum, args[cur_arg], p);
2411 err_code |= ERR_ALERT | ERR_FATAL;
2412 goto out;
2413 }
2414 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002415 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002416 file, linenum, args[cur_arg], p);
2417 err_code |= ERR_ALERT | ERR_FATAL;
2418 goto out;
2419 }
2420 }
2421 cur_arg += 2;
2422 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002423 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002424 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002425 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2426 cur_arg += 2;
2427 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002428 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2429 char *p, *end;
2430
2431 for (p = args[cur_arg + 1]; *p; p = end) {
2432 /* cut on next comma */
2433 for (end = p; *end && *end != ','; end++);
2434 if (*end)
2435 *(end++) = 0;
2436
2437 if (!strcmp(p, "allow-dup-ip")) {
2438 newsrv->dns_opts.accept_duplicate_ip = 1;
2439 }
2440 else if (!strcmp(p, "prevent-dup-ip")) {
2441 newsrv->dns_opts.accept_duplicate_ip = 0;
2442 }
2443 else {
2444 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2445 file, linenum, args[cur_arg], p);
2446 err_code |= ERR_ALERT | ERR_FATAL;
2447 goto out;
2448 }
2449 }
2450
2451 cur_arg += 2;
2452 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002453 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2454 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002455 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002456 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002457 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002458 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002459 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002460 file, linenum, args[cur_arg]);
2461 err_code |= ERR_ALERT | ERR_FATAL;
2462 goto out;
2463 }
2464 cur_arg += 2;
2465 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002466 else if (!strcmp(args[cur_arg], "resolve-net")) {
2467 char *p, *e;
2468 unsigned char mask;
2469 struct dns_options *opt;
2470
2471 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002472 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002473 file, linenum, args[cur_arg]);
2474 err_code |= ERR_ALERT | ERR_FATAL;
2475 goto out;
2476 }
2477
2478 opt = &newsrv->dns_opts;
2479
2480 /* Split arguments by comma, and convert it from ipv4 or ipv6
2481 * string network in in_addr or in6_addr.
2482 */
2483 p = args[cur_arg + 1];
2484 e = p;
2485 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002486 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002487 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002488 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002489 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2490 err_code |= ERR_ALERT | ERR_FATAL;
2491 goto out;
2492 }
2493 /* look for end or comma. */
2494 while (*e != ',' && *e != '\0')
2495 e++;
2496 if (*e == ',') {
2497 *e = '\0';
2498 e++;
2499 }
2500 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2501 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2502 /* Try to convert input string from ipv4 or ipv6 network. */
2503 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2504 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2505 &mask)) {
2506 /* Try to convert input string from ipv6 network. */
2507 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2508 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2509 } else {
2510 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002511 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002512 file, linenum, args[cur_arg], p);
2513 err_code |= ERR_ALERT | ERR_FATAL;
2514 goto out;
2515 }
2516 opt->pref_net_nb++;
2517 p = e;
2518 }
2519
2520 cur_arg += 2;
2521 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002522 else if (!strcmp(args[cur_arg], "rise")) {
2523 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002524 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002525 file, linenum, args[cur_arg]);
2526 err_code |= ERR_ALERT | ERR_FATAL;
2527 goto out;
2528 }
2529
2530 newsrv->check.rise = atol(args[cur_arg + 1]);
2531 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002532 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002533 file, linenum, args[cur_arg]);
2534 err_code |= ERR_ALERT | ERR_FATAL;
2535 goto out;
2536 }
2537
2538 if (newsrv->check.health)
2539 newsrv->check.health = newsrv->check.rise;
2540 cur_arg += 2;
2541 }
2542 else if (!strcmp(args[cur_arg], "fall")) {
2543 newsrv->check.fall = atol(args[cur_arg + 1]);
2544
2545 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002546 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002547 file, linenum, args[cur_arg]);
2548 err_code |= ERR_ALERT | ERR_FATAL;
2549 goto out;
2550 }
2551
2552 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002553 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002554 file, linenum, args[cur_arg]);
2555 err_code |= ERR_ALERT | ERR_FATAL;
2556 goto out;
2557 }
2558
2559 cur_arg += 2;
2560 }
2561 else if (!strcmp(args[cur_arg], "inter")) {
2562 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002563
2564 if (err == PARSE_TIME_OVER) {
2565 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2566 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2567 err_code |= ERR_ALERT | ERR_FATAL;
2568 goto out;
2569 }
2570 else if (err == PARSE_TIME_UNDER) {
2571 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2572 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2573 err_code |= ERR_ALERT | ERR_FATAL;
2574 goto out;
2575 }
2576 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002577 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002578 file, linenum, *err, newsrv->id);
2579 err_code |= ERR_ALERT | ERR_FATAL;
2580 goto out;
2581 }
2582 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002583 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002584 file, linenum, val, args[cur_arg], newsrv->id);
2585 err_code |= ERR_ALERT | ERR_FATAL;
2586 goto out;
2587 }
2588 newsrv->check.inter = val;
2589 cur_arg += 2;
2590 }
2591 else if (!strcmp(args[cur_arg], "fastinter")) {
2592 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002593
2594 if (err == PARSE_TIME_OVER) {
2595 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2596 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2597 err_code |= ERR_ALERT | ERR_FATAL;
2598 goto out;
2599 }
2600 else if (err == PARSE_TIME_UNDER) {
2601 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2602 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2603 err_code |= ERR_ALERT | ERR_FATAL;
2604 goto out;
2605 }
2606 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002607 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002608 file, linenum, *err, newsrv->id);
2609 err_code |= ERR_ALERT | ERR_FATAL;
2610 goto out;
2611 }
2612 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002613 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002614 file, linenum, val, args[cur_arg], newsrv->id);
2615 err_code |= ERR_ALERT | ERR_FATAL;
2616 goto out;
2617 }
2618 newsrv->check.fastinter = val;
2619 cur_arg += 2;
2620 }
2621 else if (!strcmp(args[cur_arg], "downinter")) {
2622 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002623
2624 if (err == PARSE_TIME_OVER) {
2625 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2626 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2627 err_code |= ERR_ALERT | ERR_FATAL;
2628 goto out;
2629 }
2630 else if (err == PARSE_TIME_UNDER) {
2631 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2632 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2633 err_code |= ERR_ALERT | ERR_FATAL;
2634 goto out;
2635 }
2636 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002637 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002638 file, linenum, *err, newsrv->id);
2639 err_code |= ERR_ALERT | ERR_FATAL;
2640 goto out;
2641 }
2642 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002643 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002644 file, linenum, val, args[cur_arg], newsrv->id);
2645 err_code |= ERR_ALERT | ERR_FATAL;
2646 goto out;
2647 }
2648 newsrv->check.downinter = val;
2649 cur_arg += 2;
2650 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002651 else if (!strcmp(args[cur_arg], "port")) {
2652 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002653 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002654 cur_arg += 2;
2655 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002656 else if (!strcmp(args[cur_arg], "weight")) {
2657 int w;
2658 w = atol(args[cur_arg + 1]);
2659 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002660 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002661 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2662 err_code |= ERR_ALERT | ERR_FATAL;
2663 goto out;
2664 }
2665 newsrv->uweight = newsrv->iweight = w;
2666 cur_arg += 2;
2667 }
2668 else if (!strcmp(args[cur_arg], "minconn")) {
2669 newsrv->minconn = atol(args[cur_arg + 1]);
2670 cur_arg += 2;
2671 }
2672 else if (!strcmp(args[cur_arg], "maxconn")) {
2673 newsrv->maxconn = atol(args[cur_arg + 1]);
2674 cur_arg += 2;
2675 }
2676 else if (!strcmp(args[cur_arg], "maxqueue")) {
2677 newsrv->maxqueue = atol(args[cur_arg + 1]);
2678 cur_arg += 2;
2679 }
2680 else if (!strcmp(args[cur_arg], "slowstart")) {
2681 /* slowstart is stored in seconds */
2682 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002683
2684 if (err == PARSE_TIME_OVER) {
2685 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2686 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2687 err_code |= ERR_ALERT | ERR_FATAL;
2688 goto out;
2689 }
2690 else if (err == PARSE_TIME_UNDER) {
2691 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2692 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2693 err_code |= ERR_ALERT | ERR_FATAL;
2694 goto out;
2695 }
2696 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002697 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002698 file, linenum, *err, newsrv->id);
2699 err_code |= ERR_ALERT | ERR_FATAL;
2700 goto out;
2701 }
2702 newsrv->slowstart = (val + 999) / 1000;
2703 cur_arg += 2;
2704 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002705 else if (!strcmp(args[cur_arg], "on-error")) {
2706 if (!strcmp(args[cur_arg + 1], "fastinter"))
2707 newsrv->onerror = HANA_ONERR_FASTINTER;
2708 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2709 newsrv->onerror = HANA_ONERR_FAILCHK;
2710 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2711 newsrv->onerror = HANA_ONERR_SUDDTH;
2712 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2713 newsrv->onerror = HANA_ONERR_MARKDWN;
2714 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002715 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002716 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2717 file, linenum, args[cur_arg], args[cur_arg + 1]);
2718 err_code |= ERR_ALERT | ERR_FATAL;
2719 goto out;
2720 }
2721
2722 cur_arg += 2;
2723 }
2724 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2725 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2726 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2727 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002728 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002729 file, linenum, args[cur_arg], args[cur_arg + 1]);
2730 err_code |= ERR_ALERT | ERR_FATAL;
2731 goto out;
2732 }
2733
2734 cur_arg += 2;
2735 }
2736 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2737 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2738 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2739 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002740 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002741 file, linenum, args[cur_arg], args[cur_arg + 1]);
2742 err_code |= ERR_ALERT | ERR_FATAL;
2743 goto out;
2744 }
2745
2746 cur_arg += 2;
2747 }
2748 else if (!strcmp(args[cur_arg], "error-limit")) {
2749 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002750 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002751 file, linenum, args[cur_arg]);
2752 err_code |= ERR_ALERT | ERR_FATAL;
2753 goto out;
2754 }
2755
2756 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2757
2758 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002759 ha_alert("parsing [%s:%d]: %s has to be > 0.\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 cur_arg += 2;
2765 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002766 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002767 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002768 file, linenum, "usesrc", "source");
2769 err_code |= ERR_ALERT | ERR_FATAL;
2770 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002771 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002772 else {
2773 static int srv_dumped;
2774 struct srv_kw *kw;
2775 char *err;
2776
2777 kw = srv_find_kw(args[cur_arg]);
2778 if (kw) {
2779 char *err = NULL;
2780 int code;
2781
2782 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002783 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 +02002784 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002785 if (kw->skip != -1)
2786 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002787 err_code |= ERR_ALERT | ERR_FATAL;
2788 goto out;
2789 }
2790
2791 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002792 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\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;
2797 continue;
2798 }
2799
2800 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2801 err_code |= code;
2802
2803 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002804 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002805 if (code & ERR_FATAL) {
2806 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002807 if (kw->skip != -1)
2808 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002809 goto out;
2810 }
2811 }
2812 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002813 if (kw->skip != -1)
2814 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002815 continue;
2816 }
2817
2818 err = NULL;
2819 if (!srv_dumped) {
2820 srv_dump_kws(&err);
2821 indent_msg(&err, 4);
2822 srv_dumped = 1;
2823 }
2824
Christopher Faulet767a84b2017-11-24 16:50:31 +01002825 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002826 file, linenum, args[0], args[1], args[cur_arg],
2827 err ? " Registered keywords :" : "", err ? err : "");
2828 free(err);
2829
2830 err_code |= ERR_ALERT | ERR_FATAL;
2831 goto out;
2832 }
2833 }
2834
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002835 if (!defsrv)
2836 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2837 if (err_code & ERR_FATAL)
2838 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002839 if (srv_tmpl)
2840 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002841 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002842 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002843 return 0;
2844
2845 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002846 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002847 free(errmsg);
2848 return err_code;
2849}
2850
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002851/* Returns a pointer to the first server matching either id <id>.
2852 * NULL is returned if no match is found.
2853 * the lookup is performed in the backend <bk>
2854 */
2855struct server *server_find_by_id(struct proxy *bk, int id)
2856{
2857 struct eb32_node *eb32;
2858 struct server *curserver;
2859
2860 if (!bk || (id ==0))
2861 return NULL;
2862
2863 /* <bk> has no backend capabilities, so it can't have a server */
2864 if (!(bk->cap & PR_CAP_BE))
2865 return NULL;
2866
2867 curserver = NULL;
2868
2869 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2870 if (eb32)
2871 curserver = container_of(eb32, struct server, conf.id);
2872
2873 return curserver;
2874}
2875
2876/* Returns a pointer to the first server matching either name <name>, or id
2877 * if <name> starts with a '#'. NULL is returned if no match is found.
2878 * the lookup is performed in the backend <bk>
2879 */
2880struct server *server_find_by_name(struct proxy *bk, const char *name)
2881{
2882 struct server *curserver;
2883
2884 if (!bk || !name)
2885 return NULL;
2886
2887 /* <bk> has no backend capabilities, so it can't have a server */
2888 if (!(bk->cap & PR_CAP_BE))
2889 return NULL;
2890
2891 curserver = NULL;
2892 if (*name == '#') {
2893 curserver = server_find_by_id(bk, atoi(name + 1));
2894 if (curserver)
2895 return curserver;
2896 }
2897 else {
2898 curserver = bk->srv;
2899
2900 while (curserver && (strcmp(curserver->id, name) != 0))
2901 curserver = curserver->next;
2902
2903 if (curserver)
2904 return curserver;
2905 }
2906
2907 return NULL;
2908}
2909
2910struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2911{
2912 struct server *byname;
2913 struct server *byid;
2914
2915 if (!name && !id)
2916 return NULL;
2917
2918 if (diff)
2919 *diff = 0;
2920
2921 byname = byid = NULL;
2922
2923 if (name) {
2924 byname = server_find_by_name(bk, name);
2925 if (byname && (!id || byname->puid == id))
2926 return byname;
2927 }
2928
2929 /* remaining possibilities :
2930 * - name not set
2931 * - name set but not found
2932 * - name found but ID doesn't match
2933 */
2934 if (id) {
2935 byid = server_find_by_id(bk, id);
2936 if (byid) {
2937 if (byname) {
2938 /* use id only if forced by configuration */
2939 if (byid->flags & SRV_F_FORCED_ID) {
2940 if (diff)
2941 *diff |= 2;
2942 return byid;
2943 }
2944 else {
2945 if (diff)
2946 *diff |= 1;
2947 return byname;
2948 }
2949 }
2950
2951 /* remaining possibilities:
2952 * - name not set
2953 * - name set but not found
2954 */
2955 if (name && diff)
2956 *diff |= 2;
2957 return byid;
2958 }
2959
2960 /* id bot found */
2961 if (byname) {
2962 if (diff)
2963 *diff |= 1;
2964 return byname;
2965 }
2966 }
2967
2968 return NULL;
2969}
2970
Willy Tarreau46b7f532018-08-21 11:54:26 +02002971/* Update a server state using the parameters available in the params list.
2972 *
2973 * Grabs the server lock during operation.
2974 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002975static void srv_update_state(struct server *srv, int version, char **params)
2976{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002977 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002978 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002979
2980 /* fields since version 1
2981 * and common to all other upcoming versions
2982 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002983 enum srv_state srv_op_state;
2984 enum srv_admin srv_admin_state;
2985 unsigned srv_uweight, srv_iweight;
2986 unsigned long srv_last_time_change;
2987 short srv_check_status;
2988 enum chk_result srv_check_result;
2989 int srv_check_health;
2990 int srv_check_state, srv_agent_state;
2991 int bk_f_forced_id;
2992 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002993 int fqdn_set_by_cli;
2994 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002995 const char *port_str;
2996 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002997 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002998
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002999 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003000 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003001 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003002 switch (version) {
3003 case 1:
3004 /*
3005 * now we can proceed with server's state update:
3006 * srv_addr: params[0]
3007 * srv_op_state: params[1]
3008 * srv_admin_state: params[2]
3009 * srv_uweight: params[3]
3010 * srv_iweight: params[4]
3011 * srv_last_time_change: params[5]
3012 * srv_check_status: params[6]
3013 * srv_check_result: params[7]
3014 * srv_check_health: params[8]
3015 * srv_check_state: params[9]
3016 * srv_agent_state: params[10]
3017 * bk_f_forced_id: params[11]
3018 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003019 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003020 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003021 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003022 */
3023
3024 /* validating srv_op_state */
3025 p = NULL;
3026 errno = 0;
3027 srv_op_state = strtol(params[1], &p, 10);
3028 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3029 (srv_op_state != SRV_ST_STOPPED &&
3030 srv_op_state != SRV_ST_STARTING &&
3031 srv_op_state != SRV_ST_RUNNING &&
3032 srv_op_state != SRV_ST_STOPPING)) {
3033 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3034 }
3035
3036 /* validating srv_admin_state */
3037 p = NULL;
3038 errno = 0;
3039 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003040 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003041
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003042 /* inherited statuses will be recomputed later.
3043 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3044 */
3045 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003046
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003047 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3048 (srv_admin_state != 0 &&
3049 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003050 srv_admin_state != SRV_ADMF_CMAINT &&
3051 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003052 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003053 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003054 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3055 }
3056
3057 /* validating srv_uweight */
3058 p = NULL;
3059 errno = 0;
3060 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003061 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003062 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3063
3064 /* validating srv_iweight */
3065 p = NULL;
3066 errno = 0;
3067 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003068 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003069 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3070
3071 /* validating srv_last_time_change */
3072 p = NULL;
3073 errno = 0;
3074 srv_last_time_change = strtol(params[5], &p, 10);
3075 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3076 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3077
3078 /* validating srv_check_status */
3079 p = NULL;
3080 errno = 0;
3081 srv_check_status = strtol(params[6], &p, 10);
3082 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3083 (srv_check_status >= HCHK_STATUS_SIZE))
3084 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3085
3086 /* validating srv_check_result */
3087 p = NULL;
3088 errno = 0;
3089 srv_check_result = strtol(params[7], &p, 10);
3090 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3091 (srv_check_result != CHK_RES_UNKNOWN &&
3092 srv_check_result != CHK_RES_NEUTRAL &&
3093 srv_check_result != CHK_RES_FAILED &&
3094 srv_check_result != CHK_RES_PASSED &&
3095 srv_check_result != CHK_RES_CONDPASS)) {
3096 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3097 }
3098
3099 /* validating srv_check_health */
3100 p = NULL;
3101 errno = 0;
3102 srv_check_health = strtol(params[8], &p, 10);
3103 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3104 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3105
3106 /* validating srv_check_state */
3107 p = NULL;
3108 errno = 0;
3109 srv_check_state = strtol(params[9], &p, 10);
3110 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3111 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3112 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3113
3114 /* validating srv_agent_state */
3115 p = NULL;
3116 errno = 0;
3117 srv_agent_state = strtol(params[10], &p, 10);
3118 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3119 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3120 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3121
3122 /* validating bk_f_forced_id */
3123 p = NULL;
3124 errno = 0;
3125 bk_f_forced_id = strtol(params[11], &p, 10);
3126 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3127 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3128
3129 /* validating srv_f_forced_id */
3130 p = NULL;
3131 errno = 0;
3132 srv_f_forced_id = strtol(params[12], &p, 10);
3133 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3134 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3135
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003136 /* validating srv_fqdn */
3137 fqdn = params[13];
3138 if (fqdn && *fqdn == '-')
3139 fqdn = NULL;
3140 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3141 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3142 fqdn = NULL;
3143 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003144
Frédéric Lécaille31694712017-08-01 08:47:19 +02003145 port_str = params[14];
3146 if (port_str) {
3147 port = strl2uic(port_str, strlen(port_str));
3148 if (port > USHRT_MAX) {
3149 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3150 port_str = NULL;
3151 }
3152 }
3153
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003154 /* SRV record
3155 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3156 */
3157 srvrecord = params[15];
3158 if (srvrecord && *srvrecord != '_')
3159 srvrecord = NULL;
3160
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003161 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003162 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003163 goto out;
3164
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003165 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003166 /* recover operational state and apply it to this server
3167 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003168 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003169 switch (srv_op_state) {
3170 case SRV_ST_STOPPED:
3171 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003172 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003173 break;
3174 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003175 /* If rise == 1 there is no STARTING state, let's switch to
3176 * RUNNING
3177 */
3178 if (srv->check.rise == 1) {
3179 srv->check.health = srv->check.rise + srv->check.fall - 1;
3180 srv_set_running(srv, "", NULL);
3181 break;
3182 }
3183 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3184 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003185 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003186 break;
3187 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003188 /* If fall == 1 there is no STOPPING state, let's switch to
3189 * STOPPED
3190 */
3191 if (srv->check.fall == 1) {
3192 srv->check.health = 0;
3193 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3194 break;
3195 }
3196 if (srv->check.health < srv->check.rise ||
3197 srv->check.health > srv->check.rise + srv->check.fall - 2)
3198 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003199 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003200 break;
3201 case SRV_ST_RUNNING:
3202 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003203 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003204 break;
3205 }
3206
3207 /* When applying server state, the following rules apply:
3208 * - in case of a configuration change, we apply the setting from the new
3209 * configuration, regardless of old running state
3210 * - if no configuration change, we apply old running state only if old running
3211 * state is different from new configuration state
3212 */
3213 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003214 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3215 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003216 srv_adm_set_maint(srv);
3217 else
3218 srv_adm_set_ready(srv);
3219 }
3220 /* configuration is the same, let's compate old running state and new conf state */
3221 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003222 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003223 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003224 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003225 srv_adm_set_ready(srv);
3226 }
3227 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003228 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003229 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003230 * (srv->iweight is the weight set up in configuration).
3231 * There are two possible reasons for FDRAIN to have been present :
3232 * - previous config weight was zero
3233 * - "set server b/s drain" was sent to the CLI
3234 *
3235 * In the first case, we simply want to drop this drain state
3236 * if the new weight is not zero anymore, meaning the administrator
3237 * has intentionally turned the weight back to a positive value to
3238 * enable the server again after an operation. In the second case,
3239 * the drain state was forced on the CLI regardless of the config's
3240 * weight so we don't want a change to the config weight to lose this
3241 * status. What this means is :
3242 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3243 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003244 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003245 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003246 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003247 }
3248
3249 srv->last_change = date.tv_sec - srv_last_time_change;
3250 srv->check.status = srv_check_status;
3251 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003252
3253 /* Only case we want to apply is removing ENABLED flag which could have been
3254 * done by the "disable health" command over the stats socket
3255 */
3256 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3257 (srv_check_state & CHK_ST_CONFIGURED) &&
3258 !(srv_check_state & CHK_ST_ENABLED))
3259 srv->check.state &= ~CHK_ST_ENABLED;
3260
3261 /* Only case we want to apply is removing ENABLED flag which could have been
3262 * done by the "disable agent" command over the stats socket
3263 */
3264 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3265 (srv_agent_state & CHK_ST_CONFIGURED) &&
3266 !(srv_agent_state & CHK_ST_ENABLED))
3267 srv->agent.state &= ~CHK_ST_ENABLED;
3268
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003269 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3270 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003271 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003272 * It means that a configuration file change has precedence over a unix socket change
3273 * for server's weight
3274 *
3275 * by default, HAProxy applies the following weight when parsing the configuration
3276 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003277 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003278 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003279 srv->uweight = srv_uweight;
3280 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003281 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003282
Willy Tarreaue5a60682016-11-09 14:54:53 +01003283 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003284 if (strcmp(params[0], "-"))
3285 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003286
3287 if (fqdn && srv->hostname) {
3288 if (!strcmp(srv->hostname, fqdn)) {
3289 /* Here we reset the 'set from stats socket FQDN' flag
3290 * to support such transitions:
3291 * Let's say initial FQDN value is foo1 (in configuration file).
3292 * - FQDN changed from stats socket, from foo1 to foo2 value,
3293 * - FQDN changed again from file configuration (with the same previous value
3294 set from stats socket, from foo1 to foo2 value),
3295 * - reload for any other reason than a FQDN modification,
3296 * the configuration file FQDN matches the fqdn server state file value.
3297 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003298 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003299 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003300 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003301 }
3302 else {
3303 /* If the FDQN has been changed from stats socket,
3304 * apply fqdn state file value (which is the value set
3305 * from stats socket).
3306 */
3307 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003308 srv_set_fqdn(srv, fqdn, 0);
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 }
3312 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003313 /* If all the conditions below are validated, this means
3314 * we're evaluating a server managed by SRV resolution
3315 */
3316 else if (fqdn && !srv->hostname && srvrecord) {
3317 int res;
3318
3319 /* we can't apply previous state if SRV record has changed */
3320 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3321 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);
3322 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3323 goto out;
3324 }
3325
3326 /* create or find a SRV resolution for this srv record */
3327 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3328 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3329 if (srv->srvrq == NULL) {
3330 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3331 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3332 goto out;
3333 }
3334
3335 /* prepare DNS resolution for this server */
3336 res = srv_prepare_for_resolution(srv, fqdn);
3337 if (res == -1) {
3338 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3339 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3340 goto out;
3341 }
3342
3343 /* configure check.port accordingly */
3344 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3345 !(srv->flags & SRV_F_CHECKPORT))
3346 srv->check.port = port;
3347
3348 /* Unset SRV_F_MAPPORTS for SRV records.
3349 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3350 * because no ports are provided in the configuration file.
3351 * This is because HAProxy will use the port found into the SRV record.
3352 */
3353 srv->flags &= ~SRV_F_MAPPORTS;
3354 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003355
Frédéric Lécaille31694712017-08-01 08:47:19 +02003356 if (port_str)
3357 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003358 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003359
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003360 break;
3361 default:
3362 chunk_appendf(msg, ", version '%d' not supported", version);
3363 }
3364
3365 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003366 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003367 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003368 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003369 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003370 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003371}
3372
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003373
3374/*
3375 * read next line from file <f> and return the server state version if one found.
3376 * If no version is found, then 0 is returned
3377 * Note that this should be the first read on <f>
3378 */
3379static int srv_state_get_version(FILE *f) {
3380 char buf[2];
3381 int ret;
3382
3383 /* first character of first line of the file must contain the version of the export */
3384 if (fgets(buf, 2, f) == NULL) {
3385 return 0;
3386 }
3387
3388 ret = atoi(buf);
3389 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3390 (ret > SRV_STATE_FILE_VERSION_MAX))
3391 return 0;
3392
3393 return ret;
3394}
3395
3396
3397/*
3398 * parses server state line stored in <buf> and supposedly in version <version>.
3399 * Set <params> and <srv_params> accordingly.
3400 * In case of error, params[0] is set to NULL.
3401 */
3402static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3403{
3404 int buflen, arg, srv_arg;
3405 char *cur, *end;
3406
3407 buflen = strlen(buf);
3408 cur = buf;
3409 end = cur + buflen;
3410
3411 /* we need at least one character */
3412 if (buflen == 0) {
3413 params[0] = NULL;
3414 return;
3415 }
3416
3417 /* ignore blank characters at the beginning of the line */
3418 while (isspace(*cur))
3419 ++cur;
3420
3421 /* Ignore empty or commented lines */
3422 if (cur == end || *cur == '#') {
3423 params[0] = NULL;
3424 return;
3425 }
3426
3427 /* truncated lines */
3428 if (buf[buflen - 1] != '\n') {
3429 //ha_warning("server-state file '%s': truncated line\n", filepath);
3430 params[0] = NULL;
3431 return;
3432 }
3433
3434 /* Removes trailing '\n' */
3435 buf[buflen - 1] = '\0';
3436
3437 /* we're now ready to move the line into *srv_params[] */
3438 params[0] = cur;
3439 arg = 1;
3440 srv_arg = 0;
3441 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3442 if (isspace(*cur)) {
3443 *cur = '\0';
3444 ++cur;
3445 while (isspace(*cur))
3446 ++cur;
3447 switch (version) {
3448 case 1:
3449 /*
3450 * srv_addr: params[4] => srv_params[0]
3451 * srv_op_state: params[5] => srv_params[1]
3452 * srv_admin_state: params[6] => srv_params[2]
3453 * srv_uweight: params[7] => srv_params[3]
3454 * srv_iweight: params[8] => srv_params[4]
3455 * srv_last_time_change: params[9] => srv_params[5]
3456 * srv_check_status: params[10] => srv_params[6]
3457 * srv_check_result: params[11] => srv_params[7]
3458 * srv_check_health: params[12] => srv_params[8]
3459 * srv_check_state: params[13] => srv_params[9]
3460 * srv_agent_state: params[14] => srv_params[10]
3461 * bk_f_forced_id: params[15] => srv_params[11]
3462 * srv_f_forced_id: params[16] => srv_params[12]
3463 * srv_fqdn: params[17] => srv_params[13]
3464 * srv_port: params[18] => srv_params[14]
3465 * srvrecord: params[19] => srv_params[15]
3466 */
3467 if (arg >= 4) {
3468 srv_params[srv_arg] = cur;
3469 ++srv_arg;
3470 }
3471 break;
3472 }
3473
3474 params[arg] = cur;
3475 ++arg;
3476 }
3477 else {
3478 ++cur;
3479 }
3480 }
3481
3482 /* if line is incomplete line, then ignore it.
3483 * otherwise, update useful flags */
3484 switch (version) {
3485 case 1:
3486 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3487 params[0] = NULL;
3488 return;
3489 }
3490 break;
3491 }
3492
3493 return;
3494}
3495
3496
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003497/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3498 * For each proxy, it does the following:
3499 * - opens its server state file (either one or local one)
3500 * - read whole file, line by line
3501 * - analyse each line to check if it matches our current backend:
3502 * - backend name matches
3503 * - backend id matches if id is forced and name doesn't match
3504 * - if the server pointed by the line is found, then state is applied
3505 *
3506 * If the running backend uuid or id differs from the state file, then HAProxy reports
3507 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003508 *
3509 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003510 */
3511void apply_server_state(void)
3512{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003513 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003514 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3515 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003516 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003517 FILE *f;
3518 char *filepath;
3519 char globalfilepath[MAXPATHLEN + 1];
3520 char localfilepath[MAXPATHLEN + 1];
3521 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003522 struct proxy *curproxy, *bk;
3523 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003524 char *line;
3525 char *bkname, *srvname;
3526 struct state_line *st;
3527 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003528
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003529
3530 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003531 globalfilepathlen = 0;
3532 /* create the globalfilepath variable */
3533 if (global.server_state_file) {
3534 /* absolute path or no base directory provided */
3535 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3536 len = strlen(global.server_state_file);
3537 if (len > MAXPATHLEN) {
3538 globalfilepathlen = 0;
3539 goto globalfileerror;
3540 }
3541 memcpy(globalfilepath, global.server_state_file, len);
3542 globalfilepath[len] = '\0';
3543 globalfilepathlen = len;
3544 }
3545 else if (global.server_state_base) {
3546 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003547 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003548 globalfilepathlen = 0;
3549 goto globalfileerror;
3550 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003551 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003552 globalfilepath[len] = 0;
3553 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003554
3555 /* append a slash if needed */
3556 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3557 if (globalfilepathlen + 1 > MAXPATHLEN) {
3558 globalfilepathlen = 0;
3559 goto globalfileerror;
3560 }
3561 globalfilepath[globalfilepathlen++] = '/';
3562 }
3563
3564 len = strlen(global.server_state_file);
3565 if (globalfilepathlen + len > MAXPATHLEN) {
3566 globalfilepathlen = 0;
3567 goto globalfileerror;
3568 }
3569 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3570 globalfilepathlen += len;
3571 globalfilepath[globalfilepathlen++] = 0;
3572 }
3573 }
3574 globalfileerror:
3575 if (globalfilepathlen == 0)
3576 globalfilepath[0] = '\0';
3577
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003578 /* Load global server state in a tree */
3579 if (globalfilepathlen > 0) {
3580 errno = 0;
3581 f = fopen(globalfilepath, "r");
3582 if (errno)
3583 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
3584
3585 global_file_version = srv_state_get_version(f);
3586 if (global_file_version == 0)
3587 goto out_load_server_state_in_tree;
3588
3589 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3590 line = NULL;
3591
3592 line = strdup(mybuf);
3593 if (line == NULL)
3594 continue;
3595
3596 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3597 if (params[0] == NULL)
3598 continue;
3599
3600 /* bkname */
3601 bkname = params[1];
3602 /* srvname */
3603 srvname = params[3];
3604
3605 /* key */
3606 chunk_printf(&trash, "%s %s", bkname, srvname);
3607
3608 /* store line in tree */
3609 st = calloc(1, sizeof(*st) + trash.size);
3610 if (st == NULL) {
3611 goto nextline;
3612 }
3613 memcpy(st->name_name.key, trash.area, trash.size);
3614 ebst_insert(&state_file, &st->name_name);
3615
3616 /* save line */
3617 st->line = line;
3618
3619 continue;
3620
3621 nextline:
3622 /* free up memory in case of error during the processing of the line */
3623 free(line);
3624 }
3625 }
3626 out_load_server_state_in_tree:
3627
3628 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003629 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003630 /* servers are only in backends */
3631 if (!(curproxy->cap & PR_CAP_BE))
3632 continue;
3633 fileopenerr = 0;
3634 filepath = NULL;
3635
3636 /* search server state file path and name */
3637 switch (curproxy->load_server_state_from_file) {
3638 /* read servers state from global file */
3639 case PR_SRV_STATE_FILE_GLOBAL:
3640 /* there was an error while generating global server state file path */
3641 if (globalfilepathlen == 0)
3642 continue;
3643 filepath = globalfilepath;
3644 fileopenerr = 1;
3645 break;
3646 /* this backend has its own file */
3647 case PR_SRV_STATE_FILE_LOCAL:
3648 localfilepathlen = 0;
3649 localfilepath[0] = '\0';
3650 len = 0;
3651 /* create the localfilepath variable */
3652 /* absolute path or no base directory provided */
3653 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3654 len = strlen(curproxy->server_state_file_name);
3655 if (len > MAXPATHLEN) {
3656 localfilepathlen = 0;
3657 goto localfileerror;
3658 }
3659 memcpy(localfilepath, curproxy->server_state_file_name, len);
3660 localfilepath[len] = '\0';
3661 localfilepathlen = len;
3662 }
3663 else if (global.server_state_base) {
3664 len = strlen(global.server_state_base);
3665 localfilepathlen += len;
3666
3667 if (localfilepathlen > MAXPATHLEN) {
3668 localfilepathlen = 0;
3669 goto localfileerror;
3670 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003671 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003672 localfilepath[localfilepathlen] = 0;
3673
3674 /* append a slash if needed */
3675 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3676 if (localfilepathlen + 1 > MAXPATHLEN) {
3677 localfilepathlen = 0;
3678 goto localfileerror;
3679 }
3680 localfilepath[localfilepathlen++] = '/';
3681 }
3682
3683 len = strlen(curproxy->server_state_file_name);
3684 if (localfilepathlen + len > MAXPATHLEN) {
3685 localfilepathlen = 0;
3686 goto localfileerror;
3687 }
3688 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3689 localfilepathlen += len;
3690 localfilepath[localfilepathlen++] = 0;
3691 }
3692 filepath = localfilepath;
3693 localfileerror:
3694 if (localfilepathlen == 0)
3695 localfilepath[0] = '\0';
3696
3697 break;
3698 case PR_SRV_STATE_FILE_NONE:
3699 default:
3700 continue;
3701 }
3702
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003703 /* when global file is used, we get data from the tree
3704 * Note that in such case we don't check backend name neither uuid.
3705 * Backend name can't be wrong since it's used as a key to retrieve the server state
3706 * line from the tree.
3707 */
3708 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3709 struct server *srv = curproxy->srv;
3710 while (srv) {
3711 struct ebmb_node *node;
3712 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003713
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003714 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3715 node = ebst_lookup(&state_file, trash.area);
3716 if (!node)
3717 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003718
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003719 st = container_of(node, struct state_line, name_name);
3720 memcpy(mybuf, st->line, strlen(st->line));
3721 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003722
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003723 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3724 if (params[0] == NULL)
3725 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003726
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003727 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003728
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003729 next:
3730 srv = srv->next;
3731 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003732
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003733 continue; /* next proxy in list */
3734 }
3735 else {
3736 /* load 'local' state file */
3737 errno = 0;
3738 f = fopen(filepath, "r");
3739 if (errno && fileopenerr)
3740 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3741 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003742 continue;
3743
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003744 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003745
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003746 /* first character of first line of the file must contain the version of the export */
3747 version = srv_state_get_version(f);
3748 if (version == 0) {
3749 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3750 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003751 }
3752
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003753 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3754 int bk_f_forced_id = 0;
3755 int check_id = 0;
3756 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003757
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003758 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003759
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003760 if (params[0] == NULL) {
3761 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003762 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003763
3764 /* if line is incomplete line, then ignore it.
3765 * otherwise, update useful flags */
3766 switch (version) {
3767 case 1:
3768 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3769 check_id = (atoi(params[0]) == curproxy->uuid);
3770 check_name = (strcmp(curproxy->id, params[1]) == 0);
3771 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003772 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003773
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003774 bk = curproxy;
3775
3776 /* if backend can't be found, let's continue */
3777 if (!check_id && !check_name)
3778 continue;
3779 else if (!check_id && check_name) {
3780 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3781 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3782 }
3783 else if (check_id && !check_name) {
3784 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3785 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3786 /* if name doesn't match, we still want to update curproxy if the backend id
3787 * was forced in previous the previous configuration */
3788 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003789 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003790 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003791
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003792 /* look for the server by its name: param[3] */
3793 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003794
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003795 if (!srv) {
3796 /* if no server found, then warning and continue with next line */
3797 ha_warning("can't find server '%s' in backend '%s'\n",
3798 params[3], params[1]);
3799 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3800 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003801 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003802 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003803
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003804 /* now we can proceed with server's state update */
3805 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003806 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003807 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003808fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003809 fclose(f);
3810 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003811
3812 /* now free memory allocated for the tree */
3813 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3814 node;
3815 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3816 st = container_of(node, struct state_line, name_name);
3817 ebmb_delete(&st->name_name);
3818 free(st->line);
3819 free(st);
3820 }
3821
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003822}
3823
Simon Horman7d09b9a2013-02-12 10:45:51 +09003824/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003825 * update a server's current IP address.
3826 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3827 * ip is in network format.
3828 * updater is a string which contains an information about the requester of the update.
3829 * updater is used if not NULL.
3830 *
3831 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003832 *
3833 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003834 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003835int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003836{
3837 /* generates a log line and a warning on stderr */
3838 if (1) {
3839 /* book enough space for both IPv4 and IPv6 */
3840 char oldip[INET6_ADDRSTRLEN];
3841 char newip[INET6_ADDRSTRLEN];
3842
3843 memset(oldip, '\0', INET6_ADDRSTRLEN);
3844 memset(newip, '\0', INET6_ADDRSTRLEN);
3845
3846 /* copy old IP address in a string */
3847 switch (s->addr.ss_family) {
3848 case AF_INET:
3849 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3850 break;
3851 case AF_INET6:
3852 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3853 break;
3854 };
3855
3856 /* copy new IP address in a string */
3857 switch (ip_sin_family) {
3858 case AF_INET:
3859 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3860 break;
3861 case AF_INET6:
3862 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3863 break;
3864 };
3865
3866 /* save log line into a buffer */
3867 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3868 s->proxy->id, s->id, oldip, newip, updater);
3869
3870 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003871 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003872
3873 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003874 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003875 }
3876
3877 /* save the new IP family */
3878 s->addr.ss_family = ip_sin_family;
3879 /* save the new IP address */
3880 switch (ip_sin_family) {
3881 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003882 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003883 break;
3884 case AF_INET6:
3885 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3886 break;
3887 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003888 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003889
3890 return 0;
3891}
3892
3893/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003894 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3895 *
3896 * Caller can pass its name through <updater> to get it integrated in the response message
3897 * returned by the function.
3898 *
3899 * The function first does the following, in that order:
3900 * - validates the new addr and/or port
3901 * - checks if an update is required (new IP or port is different than current ones)
3902 * - checks the update is allowed:
3903 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3904 * - allow all changes if no CHECKS are configured
3905 * - if CHECK is configured:
3906 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3907 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3908 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003909 *
3910 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003911 */
3912const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3913{
3914 struct sockaddr_storage sa;
3915 int ret, port_change_required;
3916 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003917 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003918 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003919 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003920
3921 msg = get_trash_chunk();
3922 chunk_reset(msg);
3923
3924 if (addr) {
3925 memset(&sa, 0, sizeof(struct sockaddr_storage));
3926 if (str2ip2(addr, &sa, 0) == NULL) {
3927 chunk_printf(msg, "Invalid addr '%s'", addr);
3928 goto out;
3929 }
3930
3931 /* changes are allowed on AF_INET* families only */
3932 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3933 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3934 goto out;
3935 }
3936
3937 /* collecting data currently setup */
3938 memset(current_addr, '\0', sizeof(current_addr));
3939 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3940 /* changes are allowed on AF_INET* families only */
3941 if ((ret != AF_INET) && (ret != AF_INET6)) {
3942 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3943 goto out;
3944 }
3945
3946 /* applying ADDR changes if required and allowed
3947 * ipcmp returns 0 when both ADDR are the same
3948 */
3949 if (ipcmp(&s->addr, &sa) == 0) {
3950 chunk_appendf(msg, "no need to change the addr");
3951 goto port;
3952 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003953 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003954 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003955
3956 /* we also need to update check's ADDR only if it uses the server's one */
3957 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003958 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003959 }
3960
3961 /* we also need to update agent ADDR only if it use the server's one */
3962 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003963 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003964 }
3965
3966 /* update report for caller */
3967 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3968 }
3969
3970 port:
3971 if (port) {
3972 char sign = '\0';
3973 char *endptr;
3974
3975 if (addr)
3976 chunk_appendf(msg, ", ");
3977
3978 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003979 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003980
3981 /* check if PORT change is required */
3982 port_change_required = 0;
3983
3984 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003985 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003986 new_port = strtol(port, &endptr, 10);
3987 if ((errno != 0) || (port == endptr)) {
3988 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3989 goto out;
3990 }
3991
3992 /* check if caller triggers a port mapped or offset */
3993 if (sign == '-' || (sign == '+')) {
3994 /* check if server currently uses port map */
3995 if (!(s->flags & SRV_F_MAPPORTS)) {
3996 /* switch from fixed port to port map mandatorily triggers
3997 * a port change */
3998 port_change_required = 1;
3999 /* check is configured
4000 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
4001 * prevent PORT change if check doesn't have it's dedicated port while switching
4002 * to port mapping */
4003 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4004 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.");
4005 goto out;
4006 }
4007 }
4008 /* we're already using port maps */
4009 else {
4010 port_change_required = current_port != new_port;
4011 }
4012 }
4013 /* fixed port */
4014 else {
4015 port_change_required = current_port != new_port;
4016 }
4017
4018 /* applying PORT changes if required and update response message */
4019 if (port_change_required) {
4020 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01004021 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01004022 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004023
4024 /* prepare message */
4025 chunk_appendf(msg, "port changed from '");
4026 if (s->flags & SRV_F_MAPPORTS)
4027 chunk_appendf(msg, "+");
4028 chunk_appendf(msg, "%d' to '", current_port);
4029
4030 if (sign == '-') {
4031 s->flags |= SRV_F_MAPPORTS;
4032 chunk_appendf(msg, "%c", sign);
4033 /* just use for result output */
4034 new_port = -new_port;
4035 }
4036 else if (sign == '+') {
4037 s->flags |= SRV_F_MAPPORTS;
4038 chunk_appendf(msg, "%c", sign);
4039 }
4040 else {
4041 s->flags &= ~SRV_F_MAPPORTS;
4042 }
4043
4044 chunk_appendf(msg, "%d'", new_port);
4045
4046 /* we also need to update health checks port only if it uses server's realport */
4047 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4048 s->check.port = new_port;
4049 }
4050 }
4051 else {
4052 chunk_appendf(msg, "no need to change the port");
4053 }
4054 }
4055
4056out:
Olivier Houchard4e694042017-03-14 20:01:29 +01004057 if (changed)
4058 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004059 if (updater)
4060 chunk_appendf(msg, " by '%s'", updater);
4061 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004062 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004063}
4064
4065
4066/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004067 * update server status based on result of name resolution
4068 * returns:
4069 * 0 if server status is updated
4070 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02004071 *
4072 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004073 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004074int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004075{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004076 struct dns_resolvers *resolvers = s->resolvers;
4077 struct dns_resolution *resolution = s->dns_requester->resolution;
4078 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004079
4080 switch (resolution->status) {
4081 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004082 /* status when HAProxy has just (re)started.
4083 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004084 break;
4085
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004086 case RSLV_STATUS_VALID:
4087 /*
4088 * resume health checks
4089 * server will be turned back on if health check is safe
4090 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004091 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02004092 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004093 return 1;
4094 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
4095 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02004096 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004097 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02004098
Emeric Brun52a91d32017-08-31 14:41:55 +02004099 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004100 return 1;
4101 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
4102 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
4103 s->proxy->id, s->id);
4104
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004105 ha_warning("%s.\n", trash.area);
4106 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004107 return 0;
4108
4109 case RSLV_STATUS_NX:
4110 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004111 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
4112 if (!tick_is_expired(exp, now_ms))
4113 break;
4114
4115 if (s->next_admin & SRV_ADMF_RMAINT)
4116 return 1;
4117 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
4118 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004119
4120 case RSLV_STATUS_TIMEOUT:
4121 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004122 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
4123 if (!tick_is_expired(exp, now_ms))
4124 break;
4125
4126 if (s->next_admin & SRV_ADMF_RMAINT)
4127 return 1;
4128 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
4129 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004130
4131 case RSLV_STATUS_REFUSED:
4132 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004133 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
4134 if (!tick_is_expired(exp, now_ms))
4135 break;
4136
4137 if (s->next_admin & SRV_ADMF_RMAINT)
4138 return 1;
4139 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
4140 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004141
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004142 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004143 /* stop server if resolution failed for a long enough period */
4144 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4145 if (!tick_is_expired(exp, now_ms))
4146 break;
4147
4148 if (s->next_admin & SRV_ADMF_RMAINT)
4149 return 1;
4150 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4151 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004152 }
4153
4154 return 1;
4155}
4156
4157/*
4158 * Server Name Resolution valid response callback
4159 * It expects:
4160 * - <nameserver>: the name server which answered the valid response
4161 * - <response>: buffer containing a valid DNS response
4162 * - <response_len>: size of <response>
4163 * It performs the following actions:
4164 * - ignore response if current ip found and server family not met
4165 * - update with first new ip found if family is met and current IP is not found
4166 * returns:
4167 * 0 on error
4168 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004169 *
4170 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004171 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004172int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004173{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004174 struct server *s = NULL;
4175 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004176 void *serverip, *firstip;
4177 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004178 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004179 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004180 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004181
Christopher Faulet67957bd2017-09-27 11:00:59 +02004182 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004183 if (!s)
4184 return 1;
4185
Christopher Faulet67957bd2017-09-27 11:00:59 +02004186 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004187
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004188 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004189 firstip = NULL; /* pointer to the first valid response found */
4190 /* it will be used as the new IP if a change is required */
4191 firstip_sin_family = AF_UNSPEC;
4192 serverip = NULL; /* current server IP address */
4193
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004194 /* initializing server IP pointer */
4195 server_sin_family = s->addr.ss_family;
4196 switch (server_sin_family) {
4197 case AF_INET:
4198 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4199 break;
4200
4201 case AF_INET6:
4202 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4203 break;
4204
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004205 case AF_UNSPEC:
4206 break;
4207
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004208 default:
4209 goto invalid;
4210 }
4211
Baptiste Assmann729c9012017-05-22 15:13:10 +02004212 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004213 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004214 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004215
4216 switch (ret) {
4217 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004218 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004219
4220 case DNS_UPD_SRVIP_NOT_FOUND:
4221 goto save_ip;
4222
4223 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004224 goto invalid;
4225
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004226 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004227 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004228 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004229
Baptiste Assmannfad03182015-10-28 02:03:32 +01004230 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004231 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004232 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004233 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004234
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004235 default:
4236 goto invalid;
4237
4238 }
4239
4240 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004241 if (nameserver) {
4242 nameserver->counters.update++;
4243 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004244 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004245 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004246 else
4247 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004248 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004249
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004250 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004251 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004252 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004253
4254 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004255 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004256 nameserver->counters.invalid++;
4257 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004258 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004259 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004260 return 0;
4261}
4262
4263/*
4264 * Server Name Resolution error management callback
4265 * returns:
4266 * 0 on error
4267 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004268 *
4269 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004270 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004271int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004272{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004273 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004274
Christopher Faulet67957bd2017-09-27 11:00:59 +02004275 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004276 if (!s)
4277 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004278 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004279 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004280 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004281 return 1;
4282}
4283
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004284/*
4285 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004286 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004287 * It returns a pointer to the first server found or NULL if <ip> is not yet
4288 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004289 *
4290 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004291 */
4292struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4293{
4294 struct server *tmpsrv;
4295 struct proxy *be;
4296
4297 if (!srv)
4298 return NULL;
4299
4300 be = srv->proxy;
4301 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004302 /* we found the current server is the same, ignore it */
4303 if (srv == tmpsrv)
4304 continue;
4305
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004306 /* We want to compare the IP in the record with the IP of the servers in the
4307 * same backend, only if:
4308 * * DNS resolution is enabled on the server
4309 * * the hostname used for the resolution by our server is the same than the
4310 * one used for the server found in the backend
4311 * * the server found in the backend is not our current server
4312 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004313 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004314 if ((tmpsrv->hostname_dn == NULL) ||
4315 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4316 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004317 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004318 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004319 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004320 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004321
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004322 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004323 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004324 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004325 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004326 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004327
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004328 /* At this point, we have 2 different servers using the same DNS hostname
4329 * for their respective resolution.
4330 */
4331 if (*ip_family == tmpsrv->addr.ss_family &&
4332 ((tmpsrv->addr.ss_family == AF_INET &&
4333 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4334 (tmpsrv->addr.ss_family == AF_INET6 &&
4335 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004336 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004337 return tmpsrv;
4338 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004339 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004340 }
4341
Emeric Brune9fd6b52017-11-02 17:20:39 +01004342
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004343 return NULL;
4344}
4345
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004346/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4347 * resolver. This is suited for initial address configuration. Returns 0 on
4348 * success otherwise a non-zero error code. In case of error, *err_code, if
4349 * not NULL, is filled up.
4350 */
4351int srv_set_addr_via_libc(struct server *srv, int *err_code)
4352{
4353 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004354 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004355 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004356 return 1;
4357 }
4358 return 0;
4359}
4360
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004361/* Set the server's FDQN (->hostname) from <hostname>.
4362 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004363 *
4364 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004365 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004366int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004367{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004368 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004369 char *hostname_dn;
4370 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004371
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004372 /* Note that the server lock is already held. */
4373 if (!srv->resolvers)
4374 return -1;
4375
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004376 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004377 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004378 /* run time DNS resolution was not active for this server
4379 * and we can't enable it at run time for now.
4380 */
4381 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004382 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004383
4384 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004385 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004386 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004387 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4388 hostname_dn, trash.size);
4389 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004390 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004391
Christopher Faulet67957bd2017-09-27 11:00:59 +02004392 resolution = srv->dns_requester->resolution;
4393 if (resolution &&
4394 resolution->hostname_dn &&
4395 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004396 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004397
Christopher Faulet67957bd2017-09-27 11:00:59 +02004398 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004399
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004400 free(srv->hostname);
4401 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004402 srv->hostname = strdup(hostname);
4403 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004404 srv->hostname_dn_len = hostname_dn_len;
4405 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004406 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004407
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004408 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004409 goto err;
4410
4411 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004412 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004413 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004414 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004415
4416 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004417 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004418 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004419 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004420}
4421
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004422/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4423 * from the state file. This is suited for initial address configuration.
4424 * Returns 0 on success otherwise a non-zero error code. In case of error,
4425 * *err_code, if not NULL, is filled up.
4426 */
4427static int srv_apply_lastaddr(struct server *srv, int *err_code)
4428{
4429 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4430 if (err_code)
4431 *err_code |= ERR_WARN;
4432 return 1;
4433 }
4434 return 0;
4435}
4436
Willy Tarreau25e51522016-11-04 15:10:17 +01004437/* returns 0 if no error, otherwise a combination of ERR_* flags */
4438static int srv_iterate_initaddr(struct server *srv)
4439{
4440 int return_code = 0;
4441 int err_code;
4442 unsigned int methods;
4443
4444 methods = srv->init_addr_methods;
4445 if (!methods) { // default to "last,libc"
4446 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4447 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4448 }
4449
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004450 /* "-dr" : always append "none" so that server addresses resolution
4451 * failures are silently ignored, this is convenient to validate some
4452 * configs out of their environment.
4453 */
4454 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4455 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4456
Willy Tarreau25e51522016-11-04 15:10:17 +01004457 while (methods) {
4458 err_code = 0;
4459 switch (srv_get_next_initaddr(&methods)) {
4460 case SRV_IADDR_LAST:
4461 if (!srv->lastaddr)
4462 continue;
4463 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004464 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004465 return_code |= err_code;
4466 break;
4467
4468 case SRV_IADDR_LIBC:
4469 if (!srv->hostname)
4470 continue;
4471 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004472 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004473 return_code |= err_code;
4474 break;
4475
Willy Tarreau37ebe122016-11-04 15:17:58 +01004476 case SRV_IADDR_NONE:
4477 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004478 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004479 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4480 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004481 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004482 return return_code;
4483
Willy Tarreau4310d362016-11-02 15:05:56 +01004484 case SRV_IADDR_IP:
4485 ipcpy(&srv->init_addr, &srv->addr);
4486 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004487 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4488 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004489 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004490 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004491
Willy Tarreau25e51522016-11-04 15:10:17 +01004492 default: /* unhandled method */
4493 break;
4494 }
4495 }
4496
4497 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004498 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004499 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4500 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004501 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004502 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004503 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4504 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004505
4506 return_code |= ERR_ALERT | ERR_FATAL;
4507 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004508out:
4509 srv_set_dyncookie(srv);
4510 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004511}
4512
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004513/*
4514 * This function parses all backends and all servers within each backend
4515 * and performs servers' addr resolution based on information provided by:
4516 * - configuration file
4517 * - server-state file (states provided by an 'old' haproxy process)
4518 *
4519 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4520 */
4521int srv_init_addr(void)
4522{
4523 struct proxy *curproxy;
4524 int return_code = 0;
4525
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004526 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004527 while (curproxy) {
4528 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004529
4530 /* servers are in backend only */
4531 if (!(curproxy->cap & PR_CAP_BE))
4532 goto srv_init_addr_next;
4533
Willy Tarreau25e51522016-11-04 15:10:17 +01004534 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004535 if (srv->hostname)
4536 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004537
4538 srv_init_addr_next:
4539 curproxy = curproxy->next;
4540 }
4541
4542 return return_code;
4543}
4544
Willy Tarreau46b7f532018-08-21 11:54:26 +02004545/*
4546 * Must be called with the server lock held.
4547 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004548const 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 +02004549{
4550
Willy Tarreau83061a82018-07-13 11:56:34 +02004551 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004552
4553 msg = get_trash_chunk();
4554 chunk_reset(msg);
4555
Olivier Houchard8da5f982017-08-04 18:35:36 +02004556 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004557 chunk_appendf(msg, "no need to change the FDQN");
4558 goto out;
4559 }
4560
4561 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4562 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4563 goto out;
4564 }
4565
4566 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4567 server->proxy->id, server->id, server->hostname, fqdn);
4568
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004569 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004570 chunk_reset(msg);
4571 chunk_appendf(msg, "could not update %s/%s FQDN",
4572 server->proxy->id, server->id);
4573 goto out;
4574 }
4575
4576 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004577 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004578
4579 out:
4580 if (updater)
4581 chunk_appendf(msg, " by '%s'", updater);
4582 chunk_appendf(msg, "\n");
4583
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004584 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004585}
4586
4587
Willy Tarreau21b069d2016-11-23 17:15:08 +01004588/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4589 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004590 * 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 +01004591 * used for CLI commands requiring a server name.
4592 * Important: the <arg> is modified to remove the '/'.
4593 */
4594struct server *cli_find_server(struct appctx *appctx, char *arg)
4595{
4596 struct proxy *px;
4597 struct server *sv;
4598 char *line;
4599
4600 /* split "backend/server" and make <line> point to server */
4601 for (line = arg; *line; line++)
4602 if (*line == '/') {
4603 *line++ = '\0';
4604 break;
4605 }
4606
4607 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004608 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004609 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004610 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004611 return NULL;
4612 }
4613
4614 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004615 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004616 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004617 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004618 return NULL;
4619 }
4620
4621 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004622 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004623 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004624 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004625 return NULL;
4626 }
4627
4628 return sv;
4629}
4630
William Lallemand222baf22016-11-19 02:00:33 +01004631
Willy Tarreau46b7f532018-08-21 11:54:26 +02004632/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004633static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004634{
4635 struct server *sv;
4636 const char *warning;
4637
4638 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4639 return 1;
4640
4641 sv = cli_find_server(appctx, args[2]);
4642 if (!sv)
4643 return 1;
4644
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004645 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004646
William Lallemand222baf22016-11-19 02:00:33 +01004647 if (strcmp(args[3], "weight") == 0) {
4648 warning = server_parse_weight_change_request(sv, args[4]);
4649 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004650 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004651 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004652 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004653 }
4654 }
4655 else if (strcmp(args[3], "state") == 0) {
4656 if (strcmp(args[4], "ready") == 0)
4657 srv_adm_set_ready(sv);
4658 else if (strcmp(args[4], "drain") == 0)
4659 srv_adm_set_drain(sv);
4660 else if (strcmp(args[4], "maint") == 0)
4661 srv_adm_set_maint(sv);
4662 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004663 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004664 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004665 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004666 }
4667 }
4668 else if (strcmp(args[3], "health") == 0) {
4669 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004670 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004671 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004672 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004673 }
4674 else if (strcmp(args[4], "up") == 0) {
4675 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004676 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004677 }
4678 else if (strcmp(args[4], "stopping") == 0) {
4679 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004680 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004681 }
4682 else if (strcmp(args[4], "down") == 0) {
4683 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004684 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004685 }
4686 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004687 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004688 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004689 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004690 }
4691 }
4692 else if (strcmp(args[3], "agent") == 0) {
4693 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004694 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004695 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004696 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004697 }
4698 else if (strcmp(args[4], "up") == 0) {
4699 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004700 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004701 }
4702 else if (strcmp(args[4], "down") == 0) {
4703 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004704 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004705 }
4706 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004707 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004708 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004709 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004710 }
4711 }
Misiek2da082d2017-01-09 09:40:42 +01004712 else if (strcmp(args[3], "agent-addr") == 0) {
4713 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004714 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004715 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4716 appctx->st0 = CLI_ST_PRINT;
4717 } else {
4718 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004719 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004720 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4721 appctx->st0 = CLI_ST_PRINT;
4722 }
4723 }
4724 }
4725 else if (strcmp(args[3], "agent-send") == 0) {
4726 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004727 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004728 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4729 appctx->st0 = CLI_ST_PRINT;
4730 } else {
4731 char *nss = strdup(args[4]);
4732 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004733 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004734 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4735 appctx->st0 = CLI_ST_PRINT;
4736 } else {
4737 free(sv->agent.send_string);
4738 sv->agent.send_string = nss;
4739 sv->agent.send_string_len = strlen(args[4]);
4740 }
4741 }
4742 }
William Lallemand222baf22016-11-19 02:00:33 +01004743 else if (strcmp(args[3], "check-port") == 0) {
4744 int i = 0;
4745 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004746 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004747 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004748 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004749 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004750 }
4751 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004752 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004753 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004754 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004755 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004756 }
4757 /* prevent the update of port to 0 if MAPPORTS are in use */
4758 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004759 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004760 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004761 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004762 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004763 }
4764 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004765 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004766 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004767 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004768 }
4769 else if (strcmp(args[3], "addr") == 0) {
4770 char *addr = NULL;
4771 char *port = NULL;
4772 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004773 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004774 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004775 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004776 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004777 }
4778 else {
4779 addr = args[4];
4780 }
4781 if (strcmp(args[5], "port") == 0) {
4782 port = args[6];
4783 }
4784 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4785 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004786 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004787 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004788 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004789 }
4790 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4791 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004792 else if (strcmp(args[3], "fqdn") == 0) {
4793 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004794 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004795 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4796 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004797 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004798 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004799 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004800 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004801 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004802 appctx->ctx.cli.msg = warning;
4803 appctx->st0 = CLI_ST_PRINT;
4804 }
4805 }
William Lallemand222baf22016-11-19 02:00:33 +01004806 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004807 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004808 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 +01004809 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004810 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004811 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004812 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004813 return 1;
4814}
4815
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004816static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004817{
4818 struct stream_interface *si = appctx->owner;
4819 struct proxy *px;
4820 struct server *sv;
4821 char *line;
4822
4823
4824 /* split "backend/server" and make <line> point to server */
4825 for (line = args[2]; *line; line++)
4826 if (*line == '/') {
4827 *line++ = '\0';
4828 break;
4829 }
4830
4831 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004832 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004833 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004834 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004835 return 1;
4836 }
4837
4838 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004839 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004840 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004841 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004842 return 1;
4843 }
4844
4845 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004846 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4847 sv->iweight);
4848 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004849 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004850 return 0;
4851 }
William Lallemand6b160942016-11-22 12:34:35 +01004852 return 1;
4853}
4854
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004855/* Parse a "set weight" command.
4856 *
4857 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004858 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004859static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004860{
4861 struct server *sv;
4862 const char *warning;
4863
4864 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4865 return 1;
4866
4867 sv = cli_find_server(appctx, args[2]);
4868 if (!sv)
4869 return 1;
4870
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004871 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4872
William Lallemand6b160942016-11-22 12:34:35 +01004873 warning = server_parse_weight_change_request(sv, args[3]);
4874 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004875 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004876 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004877 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004878 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004879
4880 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4881
William Lallemand6b160942016-11-22 12:34:35 +01004882 return 1;
4883}
4884
Willy Tarreau46b7f532018-08-21 11:54:26 +02004885/* parse a "set maxconn server" command. It always returns 1.
4886 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004887 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004888 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004889static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004890{
4891 struct server *sv;
4892 const char *warning;
4893
4894 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4895 return 1;
4896
4897 sv = cli_find_server(appctx, args[3]);
4898 if (!sv)
4899 return 1;
4900
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004901 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4902
Willy Tarreaub8026272016-11-23 11:26:56 +01004903 warning = server_parse_maxconn_change_request(sv, args[4]);
4904 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004905 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004906 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004907 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004908 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004909
4910 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4911
Willy Tarreaub8026272016-11-23 11:26:56 +01004912 return 1;
4913}
William Lallemand6b160942016-11-22 12:34:35 +01004914
Willy Tarreau46b7f532018-08-21 11:54:26 +02004915/* parse a "disable agent" command. It always returns 1.
4916 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004917 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004918 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004919static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004920{
4921 struct server *sv;
4922
4923 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4924 return 1;
4925
4926 sv = cli_find_server(appctx, args[2]);
4927 if (!sv)
4928 return 1;
4929
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004930 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004931 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004932 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004933 return 1;
4934}
4935
Willy Tarreau46b7f532018-08-21 11:54:26 +02004936/* parse a "disable health" command. It always returns 1.
4937 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004938 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004939 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004940static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004941{
4942 struct server *sv;
4943
4944 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4945 return 1;
4946
4947 sv = cli_find_server(appctx, args[2]);
4948 if (!sv)
4949 return 1;
4950
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004951 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004952 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004953 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004954 return 1;
4955}
4956
Willy Tarreau46b7f532018-08-21 11:54:26 +02004957/* parse a "disable server" command. It always returns 1.
4958 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004959 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004960 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004961static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004962{
4963 struct server *sv;
4964
4965 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4966 return 1;
4967
4968 sv = cli_find_server(appctx, args[2]);
4969 if (!sv)
4970 return 1;
4971
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004972 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004973 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004974 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004975 return 1;
4976}
4977
Willy Tarreau46b7f532018-08-21 11:54:26 +02004978/* parse a "enable agent" command. It always returns 1.
4979 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004980 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004981 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004982static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004983{
4984 struct server *sv;
4985
4986 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4987 return 1;
4988
4989 sv = cli_find_server(appctx, args[2]);
4990 if (!sv)
4991 return 1;
4992
4993 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004994 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004995 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004996 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004997 return 1;
4998 }
4999
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005000 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005001 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005002 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005003 return 1;
5004}
5005
Willy Tarreau46b7f532018-08-21 11:54:26 +02005006/* parse a "enable health" command. It always returns 1.
5007 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005008 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02005009 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005010static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005011{
5012 struct server *sv;
5013
5014 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5015 return 1;
5016
5017 sv = cli_find_server(appctx, args[2]);
5018 if (!sv)
5019 return 1;
5020
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005021 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005022 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005023 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005024 return 1;
5025}
5026
Willy Tarreau46b7f532018-08-21 11:54:26 +02005027/* parse a "enable server" command. It always returns 1.
5028 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005029 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02005030 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005031static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01005032{
5033 struct server *sv;
5034
5035 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5036 return 1;
5037
5038 sv = cli_find_server(appctx, args[2]);
5039 if (!sv)
5040 return 1;
5041
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005042 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005043 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01005044 if (!(sv->flags & SRV_F_COOKIESET)
5045 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
5046 sv->cookie)
5047 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005048 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005049 return 1;
5050}
5051
William Lallemand222baf22016-11-19 02:00:33 +01005052/* register cli keywords */
5053static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005054 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005055 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005056 { { "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 +01005057 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005058 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005059 { { "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 +01005060 { { "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 +01005061 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01005062 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
5063 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
5064
William Lallemand222baf22016-11-19 02:00:33 +01005065 {{},}
5066}};
5067
Willy Tarreau0108d902018-11-25 19:14:37 +01005068INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005069
Emeric Brun64cc49c2017-10-03 14:46:45 +02005070/*
5071 * This function applies server's status changes, it is
5072 * is designed to be called asynchronously.
5073 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02005074 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005075 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005076static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005077{
5078 struct check *check = &s->check;
5079 int xferred;
5080 struct proxy *px = s->proxy;
5081 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5082 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5083 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005084 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005085
Emeric Brun64cc49c2017-10-03 14:46:45 +02005086 /* If currently main is not set we try to apply pending state changes */
5087 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5088 int next_admin;
5089
5090 /* Backup next admin */
5091 next_admin = s->next_admin;
5092
5093 /* restore current admin state */
5094 s->next_admin = s->cur_admin;
5095
5096 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5097 s->last_change = now.tv_sec;
5098 if (s->proxy->lbprm.set_server_status_down)
5099 s->proxy->lbprm.set_server_status_down(s);
5100
5101 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5102 srv_shutdown_streams(s, SF_ERR_DOWN);
5103
5104 /* we might have streams queued on this server and waiting for
5105 * a connection. Those which are redispatchable will be queued
5106 * to another server or to the proxy itself.
5107 */
5108 xferred = pendconn_redistribute(s);
5109
5110 tmptrash = alloc_trash_chunk();
5111 if (tmptrash) {
5112 chunk_printf(tmptrash,
5113 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5114 s->proxy->id, s->id);
5115
Emeric Brun5a133512017-10-19 14:42:30 +02005116 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005117 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005118
5119 /* we don't send an alert if the server was previously paused */
5120 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005121 send_log(s->proxy, log_level, "%s.\n",
5122 tmptrash->area);
5123 send_email_alert(s, log_level, "%s",
5124 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005125 free_trash_chunk(tmptrash);
5126 tmptrash = NULL;
5127 }
5128 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5129 set_backend_down(s->proxy);
5130
5131 s->counters.down_trans++;
5132 }
5133 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5134 s->last_change = now.tv_sec;
5135 if (s->proxy->lbprm.set_server_status_down)
5136 s->proxy->lbprm.set_server_status_down(s);
5137
5138 /* we might have streams queued on this server and waiting for
5139 * a connection. Those which are redispatchable will be queued
5140 * to another server or to the proxy itself.
5141 */
5142 xferred = pendconn_redistribute(s);
5143
5144 tmptrash = alloc_trash_chunk();
5145 if (tmptrash) {
5146 chunk_printf(tmptrash,
5147 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5148 s->proxy->id, s->id);
5149
Emeric Brun5a133512017-10-19 14:42:30 +02005150 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005151
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005152 ha_warning("%s.\n", tmptrash->area);
5153 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5154 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005155 free_trash_chunk(tmptrash);
5156 tmptrash = NULL;
5157 }
5158
5159 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5160 set_backend_down(s->proxy);
5161 }
5162 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5163 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5164 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5165 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5166 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5167 s->proxy->last_change = now.tv_sec;
5168 }
5169
5170 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5171 s->down_time += now.tv_sec - s->last_change;
5172
5173 s->last_change = now.tv_sec;
5174 if (s->next_state == SRV_ST_STARTING)
5175 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5176
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005177 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005178 /* now propagate the status change to any LB algorithms */
5179 if (px->lbprm.update_server_eweight)
5180 px->lbprm.update_server_eweight(s);
5181 else if (srv_willbe_usable(s)) {
5182 if (px->lbprm.set_server_status_up)
5183 px->lbprm.set_server_status_up(s);
5184 }
5185 else {
5186 if (px->lbprm.set_server_status_down)
5187 px->lbprm.set_server_status_down(s);
5188 }
5189
5190 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5191 * and it's not a backup server and its effective weight is > 0,
5192 * then it can accept new connections, so we shut down all streams
5193 * on all backup servers.
5194 */
5195 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5196 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5197 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5198
5199 /* check if we can handle some connections queued at the proxy. We
5200 * will take as many as we can handle.
5201 */
5202 xferred = pendconn_grab_from_px(s);
5203
5204 tmptrash = alloc_trash_chunk();
5205 if (tmptrash) {
5206 chunk_printf(tmptrash,
5207 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5208 s->proxy->id, s->id);
5209
Emeric Brun5a133512017-10-19 14:42:30 +02005210 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005211 ha_warning("%s.\n", tmptrash->area);
5212 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5213 tmptrash->area);
5214 send_email_alert(s, LOG_NOTICE, "%s",
5215 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005216 free_trash_chunk(tmptrash);
5217 tmptrash = NULL;
5218 }
5219
5220 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5221 set_backend_down(s->proxy);
5222 }
5223 else if (s->cur_eweight != s->next_eweight) {
5224 /* now propagate the status change to any LB algorithms */
5225 if (px->lbprm.update_server_eweight)
5226 px->lbprm.update_server_eweight(s);
5227 else if (srv_willbe_usable(s)) {
5228 if (px->lbprm.set_server_status_up)
5229 px->lbprm.set_server_status_up(s);
5230 }
5231 else {
5232 if (px->lbprm.set_server_status_down)
5233 px->lbprm.set_server_status_down(s);
5234 }
5235
5236 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5237 set_backend_down(s->proxy);
5238 }
5239
5240 s->next_admin = next_admin;
5241 }
5242
Emeric Brun5a133512017-10-19 14:42:30 +02005243 /* reset operational state change */
5244 *s->op_st_chg.reason = 0;
5245 s->op_st_chg.status = s->op_st_chg.code = -1;
5246 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005247
5248 /* Now we try to apply pending admin changes */
5249
5250 /* Maintenance must also disable health checks */
5251 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5252 if (s->check.state & CHK_ST_ENABLED) {
5253 s->check.state |= CHK_ST_PAUSED;
5254 check->health = 0;
5255 }
5256
5257 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005258 tmptrash = alloc_trash_chunk();
5259 if (tmptrash) {
5260 chunk_printf(tmptrash,
5261 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5262 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5263 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005264
Olivier Houchard796a2b32017-10-24 17:42:47 +02005265 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005266
Olivier Houchard796a2b32017-10-24 17:42:47 +02005267 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005268 ha_warning("%s.\n", tmptrash->area);
5269 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5270 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005271 }
5272 free_trash_chunk(tmptrash);
5273 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005274 }
Emeric Brun8f298292017-12-06 16:47:17 +01005275 /* commit new admin status */
5276
5277 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005278 }
5279 else { /* server was still running */
5280 check->health = 0; /* failure */
5281 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005282
5283 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005284 if (s->proxy->lbprm.set_server_status_down)
5285 s->proxy->lbprm.set_server_status_down(s);
5286
Emeric Brun64cc49c2017-10-03 14:46:45 +02005287 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5288 srv_shutdown_streams(s, SF_ERR_DOWN);
5289
5290 /* we might have streams queued on this server and waiting for
5291 * a connection. Those which are redispatchable will be queued
5292 * to another server or to the proxy itself.
5293 */
5294 xferred = pendconn_redistribute(s);
5295
5296 tmptrash = alloc_trash_chunk();
5297 if (tmptrash) {
5298 chunk_printf(tmptrash,
5299 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5300 s->flags & SRV_F_BACKUP ? "Backup " : "",
5301 s->proxy->id, s->id,
5302 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5303
5304 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5305
5306 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005307 ha_warning("%s.\n", tmptrash->area);
5308 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5309 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005310 }
5311 free_trash_chunk(tmptrash);
5312 tmptrash = NULL;
5313 }
5314 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5315 set_backend_down(s->proxy);
5316
5317 s->counters.down_trans++;
5318 }
5319 }
5320 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5321 /* OK here we're leaving maintenance, we have many things to check,
5322 * because the server might possibly be coming back up depending on
5323 * its state. In practice, leaving maintenance means that we should
5324 * immediately turn to UP (more or less the slowstart) under the
5325 * following conditions :
5326 * - server is neither checked nor tracked
5327 * - server tracks another server which is not checked
5328 * - server tracks another server which is already up
5329 * Which sums up as something simpler :
5330 * "either the tracking server is up or the server's checks are disabled
5331 * or up". Otherwise we only re-enable health checks. There's a special
5332 * case associated to the stopping state which can be inherited. Note
5333 * that the server might still be in drain mode, which is naturally dealt
5334 * with by the lower level functions.
5335 */
5336
5337 if (s->check.state & CHK_ST_ENABLED) {
5338 s->check.state &= ~CHK_ST_PAUSED;
5339 check->health = check->rise; /* start OK but check immediately */
5340 }
5341
5342 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5343 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5344 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5345 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5346 s->next_state = SRV_ST_STOPPING;
5347 }
5348 else {
5349 s->next_state = SRV_ST_STARTING;
5350 if (s->slowstart > 0)
5351 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5352 else
5353 s->next_state = SRV_ST_RUNNING;
5354 }
5355
5356 }
5357
5358 tmptrash = alloc_trash_chunk();
5359 if (tmptrash) {
5360 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5361 chunk_printf(tmptrash,
5362 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5363 s->flags & SRV_F_BACKUP ? "Backup " : "",
5364 s->proxy->id, s->id,
5365 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5366 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5367 }
5368 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5369 chunk_printf(tmptrash,
5370 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5371 s->flags & SRV_F_BACKUP ? "Backup " : "",
5372 s->proxy->id, s->id, s->hostname,
5373 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5374 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5375 }
5376 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5377 chunk_printf(tmptrash,
5378 "%sServer %s/%s is %s/%s (leaving maintenance)",
5379 s->flags & SRV_F_BACKUP ? "Backup " : "",
5380 s->proxy->id, s->id,
5381 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5382 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5383 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005384 ha_warning("%s.\n", tmptrash->area);
5385 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5386 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005387 free_trash_chunk(tmptrash);
5388 tmptrash = NULL;
5389 }
5390
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005391 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005392 /* now propagate the status change to any LB algorithms */
5393 if (px->lbprm.update_server_eweight)
5394 px->lbprm.update_server_eweight(s);
5395 else if (srv_willbe_usable(s)) {
5396 if (px->lbprm.set_server_status_up)
5397 px->lbprm.set_server_status_up(s);
5398 }
5399 else {
5400 if (px->lbprm.set_server_status_down)
5401 px->lbprm.set_server_status_down(s);
5402 }
5403
5404 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5405 set_backend_down(s->proxy);
5406
Willy Tarreau6a78e612018-08-07 10:14:53 +02005407 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5408 * and it's not a backup server and its effective weight is > 0,
5409 * then it can accept new connections, so we shut down all streams
5410 * on all backup servers.
5411 */
5412 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5413 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5414 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5415
5416 /* check if we can handle some connections queued at the proxy. We
5417 * will take as many as we can handle.
5418 */
5419 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005420 }
5421 else if (s->next_admin & SRV_ADMF_MAINT) {
5422 /* remaining in maintenance mode, let's inform precisely about the
5423 * situation.
5424 */
5425 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5426 tmptrash = alloc_trash_chunk();
5427 if (tmptrash) {
5428 chunk_printf(tmptrash,
5429 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5430 s->flags & SRV_F_BACKUP ? "Backup " : "",
5431 s->proxy->id, s->id);
5432
5433 if (s->track) /* normally it's mandatory here */
5434 chunk_appendf(tmptrash, " via %s/%s",
5435 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005436 ha_warning("%s.\n", tmptrash->area);
5437 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5438 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005439 free_trash_chunk(tmptrash);
5440 tmptrash = NULL;
5441 }
5442 }
5443 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5444 tmptrash = alloc_trash_chunk();
5445 if (tmptrash) {
5446 chunk_printf(tmptrash,
5447 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5448 s->flags & SRV_F_BACKUP ? "Backup " : "",
5449 s->proxy->id, s->id, s->hostname);
5450
5451 if (s->track) /* normally it's mandatory here */
5452 chunk_appendf(tmptrash, " via %s/%s",
5453 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005454 ha_warning("%s.\n", tmptrash->area);
5455 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5456 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005457 free_trash_chunk(tmptrash);
5458 tmptrash = NULL;
5459 }
5460 }
5461 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5462 tmptrash = alloc_trash_chunk();
5463 if (tmptrash) {
5464 chunk_printf(tmptrash,
5465 "%sServer %s/%s remains in forced maintenance",
5466 s->flags & SRV_F_BACKUP ? "Backup " : "",
5467 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005468 ha_warning("%s.\n", tmptrash->area);
5469 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5470 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005471 free_trash_chunk(tmptrash);
5472 tmptrash = NULL;
5473 }
5474 }
5475 /* don't report anything when leaving drain mode and remaining in maintenance */
5476
5477 s->cur_admin = s->next_admin;
5478 }
5479
5480 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5481 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5482 /* drain state is applied only if not yet in maint */
5483
5484 s->last_change = now.tv_sec;
5485 if (px->lbprm.set_server_status_down)
5486 px->lbprm.set_server_status_down(s);
5487
5488 /* we might have streams queued on this server and waiting for
5489 * a connection. Those which are redispatchable will be queued
5490 * to another server or to the proxy itself.
5491 */
5492 xferred = pendconn_redistribute(s);
5493
5494 tmptrash = alloc_trash_chunk();
5495 if (tmptrash) {
5496 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5497 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5498 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5499
5500 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5501
5502 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005503 ha_warning("%s.\n", tmptrash->area);
5504 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5505 tmptrash->area);
5506 send_email_alert(s, LOG_NOTICE, "%s",
5507 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005508 }
5509 free_trash_chunk(tmptrash);
5510 tmptrash = NULL;
5511 }
5512
5513 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5514 set_backend_down(s->proxy);
5515 }
5516 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5517 /* OK completely leaving drain mode */
5518 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5519 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5520 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5521 s->proxy->last_change = now.tv_sec;
5522 }
5523
5524 if (s->last_change < now.tv_sec) // ignore negative times
5525 s->down_time += now.tv_sec - s->last_change;
5526 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005527 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005528
5529 tmptrash = alloc_trash_chunk();
5530 if (tmptrash) {
5531 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5532 chunk_printf(tmptrash,
5533 "%sServer %s/%s is %s (leaving forced drain)",
5534 s->flags & SRV_F_BACKUP ? "Backup " : "",
5535 s->proxy->id, s->id,
5536 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5537 }
5538 else {
5539 chunk_printf(tmptrash,
5540 "%sServer %s/%s is %s (leaving drain)",
5541 s->flags & SRV_F_BACKUP ? "Backup " : "",
5542 s->proxy->id, s->id,
5543 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5544 if (s->track) /* normally it's mandatory here */
5545 chunk_appendf(tmptrash, " via %s/%s",
5546 s->track->proxy->id, s->track->id);
5547 }
5548
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005549 ha_warning("%s.\n", tmptrash->area);
5550 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5551 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005552 free_trash_chunk(tmptrash);
5553 tmptrash = NULL;
5554 }
5555
5556 /* now propagate the status change to any LB algorithms */
5557 if (px->lbprm.update_server_eweight)
5558 px->lbprm.update_server_eweight(s);
5559 else if (srv_willbe_usable(s)) {
5560 if (px->lbprm.set_server_status_up)
5561 px->lbprm.set_server_status_up(s);
5562 }
5563 else {
5564 if (px->lbprm.set_server_status_down)
5565 px->lbprm.set_server_status_down(s);
5566 }
5567 }
5568 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5569 /* remaining in drain mode after removing one of its flags */
5570
5571 tmptrash = alloc_trash_chunk();
5572 if (tmptrash) {
5573 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5574 chunk_printf(tmptrash,
5575 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5576 s->flags & SRV_F_BACKUP ? "Backup " : "",
5577 s->proxy->id, s->id);
5578
5579 if (s->track) /* normally it's mandatory here */
5580 chunk_appendf(tmptrash, " via %s/%s",
5581 s->track->proxy->id, s->track->id);
5582 }
5583 else {
5584 chunk_printf(tmptrash,
5585 "%sServer %s/%s remains in forced drain mode",
5586 s->flags & SRV_F_BACKUP ? "Backup " : "",
5587 s->proxy->id, s->id);
5588 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005589 ha_warning("%s.\n", tmptrash->area);
5590 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5591 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005592 free_trash_chunk(tmptrash);
5593 tmptrash = NULL;
5594 }
5595
5596 /* commit new admin status */
5597
5598 s->cur_admin = s->next_admin;
5599 }
5600 }
5601
5602 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005603 *s->adm_st_chg_cause = 0;
5604}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005605
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005606struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5607{
5608 struct connection *conn;
5609
5610 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5611 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005612 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005613 }
5614
5615 return task;
5616}
5617
Willy Tarreau980855b2019-02-07 14:59:29 +01005618struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005619{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005620 struct server *srv;
5621 struct eb32_node *eb;
5622 int i;
5623 unsigned int next_wakeup;
5624 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005625
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005626 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5627 while (1) {
5628 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005629
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005630 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5631 if (!eb) {
5632 /* we might have reached the end of the tree, typically because
5633 * <now_ms> is in the first half and we're first scanning the last
5634 * half. Let's loop back to the beginning of the tree now.
5635 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005636
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005637 eb = eb32_first(&idle_conn_srv);
5638 if (likely(!eb))
5639 break;
5640 }
5641 if (tick_is_lt(now_ms, eb->key)) {
5642 /* timer not expired yet, revisit it later */
5643 next_wakeup = eb->key;
5644 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005645 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005646 }
5647 srv = eb32_entry(eb, struct server, idle_node);
5648 for (i = 0; i < global.nbthread; i++) {
5649 int max_conn = (srv->curr_idle_thr[i] / 2) +
5650 (srv->curr_idle_thr[i] & 1);
5651 int j;
5652 int did_remove = 0;
5653
5654 for (j = 0; j < max_conn; j++) {
5655 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5656 if (!conn)
5657 break;
5658 did_remove = 1;
5659 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5660 }
5661 if (did_remove && max_conn < srv->curr_idle_thr[i])
5662 srv_is_empty = 0;
5663 if (did_remove)
5664 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5665 }
5666 eb32_delete(&srv->idle_node);
5667 if (!srv_is_empty) {
5668 /* There are still more idle connections, add the
5669 * server back in the tree.
5670 */
5671 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5672 now_ms);
5673 eb32_insert(&idle_conn_srv, &srv->idle_node);
5674 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005675 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005676 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5677
5678 if (need_wakeup)
5679 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005680 else
5681 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005682
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005683 return task;
5684}
Olivier Houchard88698d92019-04-16 19:07:22 +02005685
5686/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5687static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5688 struct proxy *defpx, const char *file, int line,
5689 char **err)
5690{
5691 int arg = -1;
5692
5693 if (too_many_args(1, args, err, NULL))
5694 return -1;
5695
5696 if (*(args[1]) != 0)
5697 arg = atoi(args[1]);
5698
5699 if (arg < 0 || arg > 100) {
5700 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5701 return -1;
5702 }
5703
5704 if (args[0][10] == 'h')
5705 global.tune.pool_high_ratio = arg;
5706 else
5707 global.tune.pool_low_ratio = arg;
5708 return 0;
5709}
5710
5711/* config keyword parsers */
5712static struct cfg_kw_list cfg_kws = {ILH, {
5713 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5714 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5715 { 0, NULL, NULL }
5716}};
5717
5718INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5719
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005720/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005721 * Local variables:
5722 * c-indent-level: 8
5723 * c-basic-offset: 8
5724 * End:
5725 */