blob: a815f40010b3c05e193bef2360c15d00887a6ad2 [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];
Olivier Houchard4be71902019-07-11 15:49:00 +020069__decl_hathreads(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010070
Frédéric Lécaille7da71292019-05-20 09:47:07 +020071/* The server names dictionary */
72struct dict server_name_dict = {
73 .name = "server names",
74 .values = EB_ROOT_UNIQUE,
75};
76
Baptiste Assmannda29fe22019-06-13 13:24:29 +020077/* tree where global state_file is loaded */
78struct eb_root state_file = EB_ROOT;
79
Simon Hormana3608442013-11-01 16:46:15 +090080int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020081{
Emeric Brun52a91d32017-08-31 14:41:55 +020082 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020083 return s->down_time;
84
85 return now.tv_sec - s->last_change + s->down_time;
86}
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050088int srv_lastsession(const struct server *s)
89{
90 if (s->counters.last_sess)
91 return now.tv_sec - s->counters.last_sess;
92
93 return -1;
94}
95
Simon Horman4a741432013-02-23 15:35:38 +090096int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020097{
Simon Horman4a741432013-02-23 15:35:38 +090098 const struct server *s = check->server;
99
Willy Tarreauff5ae352013-12-11 20:36:34 +0100100 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900101 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100102
Emeric Brun52a91d32017-08-31 14:41:55 +0200103 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900104 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100105
Simon Horman4a741432013-02-23 15:35:38 +0900106 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100107}
108
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100109/*
110 * Check that we did not get a hash collision.
111 * Unlikely, but it can happen.
112 */
113static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100114{
115 struct proxy *p = s->proxy;
116 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117
118 for (tmpserv = p->srv; tmpserv != NULL;
119 tmpserv = tmpserv->next) {
120 if (tmpserv == s)
121 continue;
122 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
123 continue;
124 if (tmpserv->cookie &&
125 strcmp(tmpserv->cookie, s->cookie) == 0) {
126 ha_warning("We generated two equal cookies for two different servers.\n"
127 "Please change the secret key for '%s'.\n",
128 s->proxy->id);
129 }
130 }
131
132}
133
Willy Tarreau46b7f532018-08-21 11:54:26 +0200134/*
135 * Must be called with the server lock held.
136 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137void srv_set_dyncookie(struct server *s)
138{
139 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100140 char *tmpbuf;
141 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100142 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 size_t buffer_len;
144 int addr_len;
145 int port;
146
147 if ((s->flags & SRV_F_COOKIESET) ||
148 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
149 s->proxy->dyncookie_key == NULL)
150 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100151 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100152
153 if (s->addr.ss_family != AF_INET &&
154 s->addr.ss_family != AF_INET6)
155 return;
156 /*
157 * Buffer to calculate the cookie value.
158 * The buffer contains the secret key + the server IP address
159 * + the TCP port.
160 */
161 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
162 /*
163 * The TCP port should use only 2 bytes, but is stored in
164 * an unsigned int in struct server, so let's use 4, to be
165 * on the safe side.
166 */
167 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200168 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100169 memcpy(tmpbuf, p->dyncookie_key, key_len);
170 memcpy(&(tmpbuf[key_len]),
171 s->addr.ss_family == AF_INET ?
172 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
173 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
174 addr_len);
175 /*
176 * Make sure it's the same across all the load balancers,
177 * no matter their endianness.
178 */
179 port = htonl(s->svc_port);
180 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
181 hash_value = XXH64(tmpbuf, buffer_len, 0);
182 memprintf(&s->cookie, "%016llx", hash_value);
183 if (!s->cookie)
184 return;
185 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100186
187 /* Don't bother checking if the dyncookie is duplicated if
188 * the server is marked as "disabled", maybe it doesn't have
189 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100190 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100191 if (!(s->next_admin & SRV_ADMF_FMAINT))
192 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100193}
194
Willy Tarreau21faa912012-10-10 08:27:36 +0200195/*
196 * Registers the server keyword list <kwl> as a list of valid keywords for next
197 * parsing sessions.
198 */
199void srv_register_keywords(struct srv_kw_list *kwl)
200{
201 LIST_ADDQ(&srv_keywords.list, &kwl->list);
202}
203
204/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
205 * keyword is found with a NULL ->parse() function, then an attempt is made to
206 * find one with a valid ->parse() function. This way it is possible to declare
207 * platform-dependant, known keywords as NULL, then only declare them as valid
208 * if some options are met. Note that if the requested keyword contains an
209 * opening parenthesis, everything from this point is ignored.
210 */
211struct srv_kw *srv_find_kw(const char *kw)
212{
213 int index;
214 const char *kwend;
215 struct srv_kw_list *kwl;
216 struct srv_kw *ret = NULL;
217
218 kwend = strchr(kw, '(');
219 if (!kwend)
220 kwend = kw + strlen(kw);
221
222 list_for_each_entry(kwl, &srv_keywords.list, list) {
223 for (index = 0; kwl->kw[index].kw != NULL; index++) {
224 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
225 kwl->kw[index].kw[kwend-kw] == 0) {
226 if (kwl->kw[index].parse)
227 return &kwl->kw[index]; /* found it !*/
228 else
229 ret = &kwl->kw[index]; /* may be OK */
230 }
231 }
232 }
233 return ret;
234}
235
236/* Dumps all registered "server" keywords to the <out> string pointer. The
237 * unsupported keywords are only dumped if their supported form was not
238 * found.
239 */
240void srv_dump_kws(char **out)
241{
242 struct srv_kw_list *kwl;
243 int index;
244
245 *out = NULL;
246 list_for_each_entry(kwl, &srv_keywords.list, list) {
247 for (index = 0; kwl->kw[index].kw != NULL; index++) {
248 if (kwl->kw[index].parse ||
249 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
250 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
251 kwl->scope,
252 kwl->kw[index].kw,
253 kwl->kw[index].skip ? " <arg>" : "",
254 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
255 kwl->kw[index].parse ? "" : " (not supported)");
256 }
257 }
258 }
259}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100260
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100261/* Parse the "addr" server keyword */
262static int srv_parse_addr(char **args, int *cur_arg,
263 struct proxy *curproxy, struct server *newsrv, char **err)
264{
265 char *errmsg, *arg;
266 struct sockaddr_storage *sk;
267 int port1, port2;
268 struct protocol *proto;
269
270 errmsg = NULL;
271 arg = args[*cur_arg + 1];
272
273 if (!*arg) {
274 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
275 goto err;
276 }
277
278 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
279 if (!sk) {
280 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
281 goto err;
282 }
283
284 proto = protocol_by_family(sk->ss_family);
285 if (!proto || !proto->connect) {
286 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
287 args[*cur_arg], arg);
288 goto err;
289 }
290
291 if (port1 != port2) {
292 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
293 args[*cur_arg], arg);
294 goto err;
295 }
296
297 newsrv->check.addr = newsrv->agent.addr = *sk;
298 newsrv->flags |= SRV_F_CHECKADDR;
299 newsrv->flags |= SRV_F_AGENTADDR;
300
301 return 0;
302
303 err:
304 free(errmsg);
305 return ERR_ALERT | ERR_FATAL;
306}
307
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100308/* Parse the "agent-check" server keyword */
309static int srv_parse_agent_check(char **args, int *cur_arg,
310 struct proxy *curproxy, struct server *newsrv, char **err)
311{
312 newsrv->do_agent = 1;
313 return 0;
314}
315
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100316/* Parse the "backup" server keyword */
317static int srv_parse_backup(char **args, int *cur_arg,
318 struct proxy *curproxy, struct server *newsrv, char **err)
319{
320 newsrv->flags |= SRV_F_BACKUP;
321 return 0;
322}
323
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100324/* Parse the "check" server keyword */
325static int srv_parse_check(char **args, int *cur_arg,
326 struct proxy *curproxy, struct server *newsrv, char **err)
327{
328 newsrv->do_check = 1;
329 return 0;
330}
331
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100332/* Parse the "check-send-proxy" server keyword */
333static int srv_parse_check_send_proxy(char **args, int *cur_arg,
334 struct proxy *curproxy, struct server *newsrv, char **err)
335{
336 newsrv->check.send_proxy = 1;
337 return 0;
338}
339
Alexander Liu2a54bb72019-05-22 19:44:48 +0800340/* Parse the "check-via-socks4" server keyword */
341static int srv_parse_check_via_socks4(char **args, int *cur_arg,
342 struct proxy *curproxy, struct server *newsrv, char **err)
343{
344 newsrv->check.via_socks4 = 1;
345 return 0;
346}
347
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100348/* Parse the "cookie" server keyword */
349static int srv_parse_cookie(char **args, int *cur_arg,
350 struct proxy *curproxy, struct server *newsrv, char **err)
351{
352 char *arg;
353
354 arg = args[*cur_arg + 1];
355 if (!*arg) {
356 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
357 return ERR_ALERT | ERR_FATAL;
358 }
359
360 free(newsrv->cookie);
361 newsrv->cookie = strdup(arg);
362 newsrv->cklen = strlen(arg);
363 newsrv->flags |= SRV_F_COOKIESET;
364 return 0;
365}
366
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100367/* Parse the "disabled" server keyword */
368static int srv_parse_disabled(char **args, int *cur_arg,
369 struct proxy *curproxy, struct server *newsrv, char **err)
370{
Emeric Brun52a91d32017-08-31 14:41:55 +0200371 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
372 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100373 newsrv->check.state |= CHK_ST_PAUSED;
374 newsrv->check.health = 0;
375 return 0;
376}
377
378/* Parse the "enabled" server keyword */
379static int srv_parse_enabled(char **args, int *cur_arg,
380 struct proxy *curproxy, struct server *newsrv, char **err)
381{
Emeric Brun52a91d32017-08-31 14:41:55 +0200382 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
383 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100384 newsrv->check.state &= ~CHK_ST_PAUSED;
385 newsrv->check.health = newsrv->check.rise;
386 return 0;
387}
388
Willy Tarreau9c538e02019-01-23 10:21:49 +0100389static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
390{
391 char *arg;
392
393 arg = args[*cur_arg + 1];
394 if (!*arg) {
395 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
396 return ERR_ALERT | ERR_FATAL;
397 }
398 newsrv->max_reuse = atoi(arg);
399
400 return 0;
401}
402
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100403static 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 +0100404{
405 const char *res;
406 char *arg;
407 unsigned int time;
408
409 arg = args[*cur_arg + 1];
410 if (!*arg) {
411 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
412 return ERR_ALERT | ERR_FATAL;
413 }
414 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200415 if (res == PARSE_TIME_OVER) {
416 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
417 args[*cur_arg+1], args[*cur_arg]);
418 return ERR_ALERT | ERR_FATAL;
419 }
420 else if (res == PARSE_TIME_UNDER) {
421 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
422 args[*cur_arg+1], args[*cur_arg]);
423 return ERR_ALERT | ERR_FATAL;
424 }
425 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100426 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
427 *res, args[*cur_arg]);
428 return ERR_ALERT | ERR_FATAL;
429 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100430 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100431
432 return 0;
433}
434
Olivier Houchard006e3102018-12-10 18:30:32 +0100435static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
436{
437 char *arg;
438
439 arg = args[*cur_arg + 1];
440 if (!*arg) {
441 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
442 return ERR_ALERT | ERR_FATAL;
443 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100444
Olivier Houchard006e3102018-12-10 18:30:32 +0100445 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100446 if ((int)newsrv->max_idle_conns < -1) {
447 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
448 return ERR_ALERT | ERR_FATAL;
449 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100450
451 return 0;
452}
453
Willy Tarreaudff55432012-10-10 17:51:05 +0200454/* parse the "id" server keyword */
455static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
456{
457 struct eb32_node *node;
458
459 if (!*args[*cur_arg + 1]) {
460 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
461 return ERR_ALERT | ERR_FATAL;
462 }
463
464 newsrv->puid = atol(args[*cur_arg + 1]);
465 newsrv->conf.id.key = newsrv->puid;
466
467 if (newsrv->puid <= 0) {
468 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
469 return ERR_ALERT | ERR_FATAL;
470 }
471
472 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
473 if (node) {
474 struct server *target = container_of(node, struct server, conf.id);
475 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
476 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
477 target->id);
478 return ERR_ALERT | ERR_FATAL;
479 }
480
481 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200482 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200483 return 0;
484}
485
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100486/* Parse the "namespace" server keyword */
487static int srv_parse_namespace(char **args, int *cur_arg,
488 struct proxy *curproxy, struct server *newsrv, char **err)
489{
Willy Tarreaue5733232019-05-22 19:24:06 +0200490#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100491 char *arg;
492
493 arg = args[*cur_arg + 1];
494 if (!*arg) {
495 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
496 return ERR_ALERT | ERR_FATAL;
497 }
498
499 if (!strcmp(arg, "*")) {
500 /* Use the namespace associated with the connection (if present). */
501 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
502 return 0;
503 }
504
505 /*
506 * As this parser may be called several times for the same 'default-server'
507 * object, or for a new 'server' instance deriving from a 'default-server'
508 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
509 */
510 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
511
512 newsrv->netns = netns_store_lookup(arg, strlen(arg));
513 if (!newsrv->netns)
514 newsrv->netns = netns_store_insert(arg);
515
516 if (!newsrv->netns) {
517 memprintf(err, "Cannot open namespace '%s'", arg);
518 return ERR_ALERT | ERR_FATAL;
519 }
520
521 return 0;
522#else
523 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
524 return ERR_ALERT | ERR_FATAL;
525#endif
526}
527
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100528/* Parse the "no-agent-check" server keyword */
529static int srv_parse_no_agent_check(char **args, int *cur_arg,
530 struct proxy *curproxy, struct server *newsrv, char **err)
531{
532 free_check(&newsrv->agent);
533 newsrv->agent.inter = 0;
534 newsrv->agent.port = 0;
535 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
536 newsrv->do_agent = 0;
537 return 0;
538}
539
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100540/* Parse the "no-backup" server keyword */
541static int srv_parse_no_backup(char **args, int *cur_arg,
542 struct proxy *curproxy, struct server *newsrv, char **err)
543{
544 newsrv->flags &= ~SRV_F_BACKUP;
545 return 0;
546}
547
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100548/* Parse the "no-check" server keyword */
549static int srv_parse_no_check(char **args, int *cur_arg,
550 struct proxy *curproxy, struct server *newsrv, char **err)
551{
552 free_check(&newsrv->check);
553 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
554 newsrv->do_check = 0;
555 return 0;
556}
557
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100558/* Parse the "no-check-send-proxy" server keyword */
559static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
560 struct proxy *curproxy, struct server *newsrv, char **err)
561{
562 newsrv->check.send_proxy = 0;
563 return 0;
564}
565
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100566/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200567static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100568{
569 srv->pp_opts &= ~flags;
570 return 0;
571}
572
573/* Parse the "no-send-proxy" server keyword */
574static int srv_parse_no_send_proxy(char **args, int *cur_arg,
575 struct proxy *curproxy, struct server *newsrv, char **err)
576{
577 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
578}
579
580/* Parse the "no-send-proxy-v2" server keyword */
581static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
582 struct proxy *curproxy, struct server *newsrv, char **err)
583{
584 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
585}
586
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200587/* Parse the "no-tfo" server keyword */
588static int srv_parse_no_tfo(char **args, int *cur_arg,
589 struct proxy *curproxy, struct server *newsrv, char **err)
590{
591 newsrv->flags &= ~SRV_F_FASTOPEN;
592 return 0;
593}
594
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100595/* Parse the "non-stick" server keyword */
596static int srv_parse_non_stick(char **args, int *cur_arg,
597 struct proxy *curproxy, struct server *newsrv, char **err)
598{
599 newsrv->flags |= SRV_F_NON_STICK;
600 return 0;
601}
602
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100603/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200604static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100605{
606 srv->pp_opts |= flags;
607 return 0;
608}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200609/* parse the "proto" server keyword */
610static int srv_parse_proto(char **args, int *cur_arg,
611 struct proxy *px, struct server *newsrv, char **err)
612{
613 struct ist proto;
614
615 if (!*args[*cur_arg + 1]) {
616 memprintf(err, "'%s' : missing value", args[*cur_arg]);
617 return ERR_ALERT | ERR_FATAL;
618 }
619 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
620 newsrv->mux_proto = get_mux_proto(proto);
621 if (!newsrv->mux_proto) {
622 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
623 return ERR_ALERT | ERR_FATAL;
624 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200625 return 0;
626}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100627
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100628/* parse the "proxy-v2-options" */
629static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
630 struct proxy *px, struct server *newsrv, char **err)
631{
632 char *p, *n;
633 for (p = args[*cur_arg+1]; p; p = n) {
634 n = strchr(p, ',');
635 if (n)
636 *n++ = '\0';
637 if (!strcmp(p, "ssl")) {
638 newsrv->pp_opts |= SRV_PP_V2_SSL;
639 } else if (!strcmp(p, "cert-cn")) {
640 newsrv->pp_opts |= SRV_PP_V2_SSL;
641 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100642 } else if (!strcmp(p, "cert-key")) {
643 newsrv->pp_opts |= SRV_PP_V2_SSL;
644 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
645 } else if (!strcmp(p, "cert-sig")) {
646 newsrv->pp_opts |= SRV_PP_V2_SSL;
647 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
648 } else if (!strcmp(p, "ssl-cipher")) {
649 newsrv->pp_opts |= SRV_PP_V2_SSL;
650 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100651 } else if (!strcmp(p, "authority")) {
652 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100653 } else if (!strcmp(p, "crc32c")) {
654 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100655 } else
656 goto fail;
657 }
658 return 0;
659 fail:
660 if (err)
661 memprintf(err, "'%s' : proxy v2 option not implemented", p);
662 return ERR_ALERT | ERR_FATAL;
663}
664
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100665/* Parse the "observe" server keyword */
666static int srv_parse_observe(char **args, int *cur_arg,
667 struct proxy *curproxy, struct server *newsrv, char **err)
668{
669 char *arg;
670
671 arg = args[*cur_arg + 1];
672 if (!*arg) {
673 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
674 return ERR_ALERT | ERR_FATAL;
675 }
676
677 if (!strcmp(arg, "none")) {
678 newsrv->observe = HANA_OBS_NONE;
679 }
680 else if (!strcmp(arg, "layer4")) {
681 newsrv->observe = HANA_OBS_LAYER4;
682 }
683 else if (!strcmp(arg, "layer7")) {
684 if (curproxy->mode != PR_MODE_HTTP) {
685 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
686 return ERR_ALERT;
687 }
688 newsrv->observe = HANA_OBS_LAYER7;
689 }
690 else {
691 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
692 "but got '%s'\n", args[*cur_arg], arg);
693 return ERR_ALERT | ERR_FATAL;
694 }
695
696 return 0;
697}
698
Frédéric Lécaille16186232017-03-14 16:42:49 +0100699/* Parse the "redir" server keyword */
700static int srv_parse_redir(char **args, int *cur_arg,
701 struct proxy *curproxy, struct server *newsrv, char **err)
702{
703 char *arg;
704
705 arg = args[*cur_arg + 1];
706 if (!*arg) {
707 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
708 return ERR_ALERT | ERR_FATAL;
709 }
710
711 free(newsrv->rdr_pfx);
712 newsrv->rdr_pfx = strdup(arg);
713 newsrv->rdr_len = strlen(arg);
714
715 return 0;
716}
717
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100718/* Parse the "send-proxy" server keyword */
719static int srv_parse_send_proxy(char **args, int *cur_arg,
720 struct proxy *curproxy, struct server *newsrv, char **err)
721{
722 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
723}
724
725/* Parse the "send-proxy-v2" server keyword */
726static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
727 struct proxy *curproxy, struct server *newsrv, char **err)
728{
729 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
730}
731
Frédéric Lécailledba97072017-03-17 15:33:50 +0100732
733/* Parse the "source" server keyword */
734static int srv_parse_source(char **args, int *cur_arg,
735 struct proxy *curproxy, struct server *newsrv, char **err)
736{
737 char *errmsg;
738 int port_low, port_high;
739 struct sockaddr_storage *sk;
740 struct protocol *proto;
741
742 errmsg = NULL;
743
744 if (!*args[*cur_arg + 1]) {
745 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
746 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
747 goto err;
748 }
749
750 /* 'sk' is statically allocated (no need to be freed). */
751 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
752 if (!sk) {
753 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
754 goto err;
755 }
756
757 proto = protocol_by_family(sk->ss_family);
758 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100759 ha_alert("'%s %s' : connect() not supported for this address family.\n",
760 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100761 goto err;
762 }
763
764 newsrv->conn_src.opts |= CO_SRC_BIND;
765 newsrv->conn_src.source_addr = *sk;
766
767 if (port_low != port_high) {
768 int i;
769
770 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100771 ha_alert("'%s' does not support port offsets (found '%s').\n",
772 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100773 goto err;
774 }
775
776 if (port_low <= 0 || port_low > 65535 ||
777 port_high <= 0 || port_high > 65535 ||
778 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100779 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 +0100780 goto err;
781 }
782 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
783 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
784 newsrv->conn_src.sport_range->ports[i] = port_low + i;
785 }
786
787 *cur_arg += 2;
788 while (*(args[*cur_arg])) {
789 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
790#if defined(CONFIG_HAP_TRANSPARENT)
791 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100792 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
793 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100794 goto err;
795 }
796 if (!strcmp(args[*cur_arg + 1], "client")) {
797 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
798 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
799 }
800 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
801 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
802 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
803 }
804 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
805 char *name, *end;
806
807 name = args[*cur_arg + 1] + 7;
808 while (isspace(*name))
809 name++;
810
811 end = name;
812 while (*end && !isspace(*end) && *end != ',' && *end != ')')
813 end++;
814
815 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
816 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
817 free(newsrv->conn_src.bind_hdr_name);
818 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
819 newsrv->conn_src.bind_hdr_len = end - name;
820 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
821 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
822 newsrv->conn_src.bind_hdr_occ = -1;
823
824 /* now look for an occurrence number */
825 while (isspace(*end))
826 end++;
827 if (*end == ',') {
828 end++;
829 name = end;
830 if (*end == '-')
831 end++;
832 while (isdigit((int)*end))
833 end++;
834 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
835 }
836
837 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100838 ha_alert("usesrc hdr_ip(name,num) does not support negative"
839 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100840 goto err;
841 }
842 }
843 else {
844 struct sockaddr_storage *sk;
845 int port1, port2;
846
847 /* 'sk' is statically allocated (no need to be freed). */
848 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
849 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100850 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100851 goto err;
852 }
853
854 proto = protocol_by_family(sk->ss_family);
855 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100856 ha_alert("'%s %s' : connect() not supported for this address family.\n",
857 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100858 goto err;
859 }
860
861 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100862 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
863 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100864 goto err;
865 }
866 newsrv->conn_src.tproxy_addr = *sk;
867 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
868 }
869 global.last_checks |= LSTCHK_NETADM;
870 *cur_arg += 2;
871 continue;
872#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100873 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 +0100874 goto err;
875#endif /* defined(CONFIG_HAP_TRANSPARENT) */
876 } /* "usesrc" */
877
878 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
879#ifdef SO_BINDTODEVICE
880 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100881 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100882 goto err;
883 }
884 free(newsrv->conn_src.iface_name);
885 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
886 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
887 global.last_checks |= LSTCHK_NETADM;
888#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100889 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100890 goto err;
891#endif
892 *cur_arg += 2;
893 continue;
894 }
895 /* this keyword in not an option of "source" */
896 break;
897 } /* while */
898
899 return 0;
900
901 err:
902 free(errmsg);
903 return ERR_ALERT | ERR_FATAL;
904}
905
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100906/* Parse the "stick" server keyword */
907static int srv_parse_stick(char **args, int *cur_arg,
908 struct proxy *curproxy, struct server *newsrv, char **err)
909{
910 newsrv->flags &= ~SRV_F_NON_STICK;
911 return 0;
912}
913
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100914/* Parse the "track" server keyword */
915static int srv_parse_track(char **args, int *cur_arg,
916 struct proxy *curproxy, struct server *newsrv, char **err)
917{
918 char *arg;
919
920 arg = args[*cur_arg + 1];
921 if (!*arg) {
922 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
923 return ERR_ALERT | ERR_FATAL;
924 }
925
926 free(newsrv->trackit);
927 newsrv->trackit = strdup(arg);
928
929 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800930}
931
932/* Parse the "socks4" server keyword */
933static int srv_parse_socks4(char **args, int *cur_arg,
934 struct proxy *curproxy, struct server *newsrv, char **err)
935{
936 char *errmsg;
937 int port_low, port_high;
938 struct sockaddr_storage *sk;
939 struct protocol *proto;
940
941 errmsg = NULL;
942
943 if (!*args[*cur_arg + 1]) {
944 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
945 goto err;
946 }
947
948 /* 'sk' is statically allocated (no need to be freed). */
949 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
950 if (!sk) {
951 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
952 goto err;
953 }
954
955 proto = protocol_by_family(sk->ss_family);
956 if (!proto || !proto->connect) {
957 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
958 goto err;
959 }
960
961 newsrv->flags |= SRV_F_SOCKS4_PROXY;
962 newsrv->socks4_addr = *sk;
963
964 if (port_low != port_high) {
965 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
966 goto err;
967 }
968
969 if (!port_low) {
970 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
971 goto err;
972 }
973
974 return 0;
975
976 err:
977 free(errmsg);
978 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100979}
980
Frédéric Lécailledba97072017-03-17 15:33:50 +0100981
Willy Tarreau034c88c2017-01-23 23:36:45 +0100982/* parse the "tfo" server keyword */
983static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
984{
985 newsrv->flags |= SRV_F_FASTOPEN;
986 return 0;
987}
988
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200989/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200990 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200991 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200992 *
993 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200994 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200995void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200996{
Willy Tarreau87b09662015-04-03 00:22:06 +0200997 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200998
Willy Tarreau87b09662015-04-03 00:22:06 +0200999 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
1000 if (stream->srv_conn == srv)
1001 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001002}
1003
1004/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001005 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001006 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001007 *
1008 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001009 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001010void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001011{
1012 struct server *srv;
1013
1014 for (srv = px->srv; srv != NULL; srv = srv->next)
1015 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001016 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001017}
1018
Willy Tarreaubda92272014-05-20 21:55:30 +02001019/* Appends some information to a message string related to a server going UP or
1020 * DOWN. If both <forced> and <reason> are null and the server tracks another
1021 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001022 * If <check> is non-null, an entire string describing the check result will be
1023 * appended after a comma and a space (eg: to report some information from the
1024 * check that changed the state). In the other case, the string will be built
1025 * using the check results stored into the struct server if present.
1026 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001027 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001028 *
1029 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001030 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001031void srv_append_status(struct buffer *msg, struct server *s,
1032 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001033{
Emeric Brun5a133512017-10-19 14:42:30 +02001034 short status = s->op_st_chg.status;
1035 short code = s->op_st_chg.code;
1036 long duration = s->op_st_chg.duration;
1037 char *desc = s->op_st_chg.reason;
1038
1039 if (check) {
1040 status = check->status;
1041 code = check->code;
1042 duration = check->duration;
1043 desc = check->desc;
1044 }
1045
1046 if (status != -1) {
1047 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1048
1049 if (status >= HCHK_STATUS_L57DATA)
1050 chunk_appendf(msg, ", code: %d", code);
1051
1052 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001053 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001054
1055 chunk_appendf(msg, ", info: \"");
1056
1057 chunk_initlen(&src, desc, 0, strlen(desc));
1058 chunk_asciiencode(msg, &src, '"');
1059
1060 chunk_appendf(msg, "\"");
1061 }
1062
1063 if (duration >= 0)
1064 chunk_appendf(msg, ", check duration: %ldms", duration);
1065 }
1066 else if (desc && *desc) {
1067 chunk_appendf(msg, ", %s", desc);
1068 }
1069 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001070 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001071 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072
1073 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001074 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001075 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1076 " %d sessions active, %d requeued, %d remaining in queue",
1077 s->proxy->srv_act, s->proxy->srv_bck,
1078 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1079 s->cur_sess, xferred, s->nbpend);
1080 else
1081 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1082 " %d sessions requeued, %d total in queue",
1083 s->proxy->srv_act, s->proxy->srv_bck,
1084 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1085 xferred, s->nbpend);
1086 }
1087}
1088
Emeric Brun5a133512017-10-19 14:42:30 +02001089/* Marks server <s> down, regardless of its checks' statuses. The server is
1090 * registered in a list to postpone the counting of the remaining servers on
1091 * the proxy and transfers queued streams whenever possible to other servers at
1092 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1093 * non-null as the reason for going down or the available data from the check
1094 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001095 *
1096 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001097 */
Emeric Brun5a133512017-10-19 14:42:30 +02001098void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001099{
1100 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001101
Emeric Brun64cc49c2017-10-03 14:46:45 +02001102 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001103 return;
1104
Emeric Brun52a91d32017-08-31 14:41:55 +02001105 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001106 *s->op_st_chg.reason = 0;
1107 s->op_st_chg.status = -1;
1108 if (reason) {
1109 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1110 }
1111 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001112 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001113 s->op_st_chg.code = check->code;
1114 s->op_st_chg.status = check->status;
1115 s->op_st_chg.duration = check->duration;
1116 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001117
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001118 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001119 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001120
Emeric Brun9f0b4582017-10-23 14:39:51 +02001121 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001122 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001123 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001124 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001125 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001126}
1127
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001128/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001129 * in maintenance. The server is registered in a list to postpone the counting
1130 * of the remaining servers on the proxy and tries to grab requests from the
1131 * proxy at a sync point. Maintenance servers are ignored. It stores the
1132 * <reason> if non-null as the reason for going down or the available data
1133 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001134 *
1135 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001136 */
Emeric Brun5a133512017-10-19 14:42:30 +02001137void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001138{
1139 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001140
Emeric Brun64cc49c2017-10-03 14:46:45 +02001141 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001142 return;
1143
Emeric Brun52a91d32017-08-31 14:41:55 +02001144 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001145 return;
1146
Emeric Brun52a91d32017-08-31 14:41:55 +02001147 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001148 *s->op_st_chg.reason = 0;
1149 s->op_st_chg.status = -1;
1150 if (reason) {
1151 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1152 }
1153 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001154 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001155 s->op_st_chg.code = check->code;
1156 s->op_st_chg.status = check->status;
1157 s->op_st_chg.duration = check->duration;
1158 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001159
Emeric Brun64cc49c2017-10-03 14:46:45 +02001160 if (s->slowstart <= 0)
1161 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001162
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001163 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001164 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001165
Emeric Brun9f0b4582017-10-23 14:39:51 +02001166 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001167 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001168 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001169 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001170 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001171}
1172
Willy Tarreau8eb77842014-05-21 13:54:57 +02001173/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001174 * isn't in maintenance. The server is registered in a list to postpone the
1175 * counting of the remaining servers on the proxy and tries to grab requests
1176 * from the proxy. Maintenance servers are ignored. It stores the
1177 * <reason> if non-null as the reason for going down or the available data
1178 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001179 * up. Note that it makes use of the trash to build the log strings, so <reason>
1180 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001181 *
1182 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001183 */
Emeric Brun5a133512017-10-19 14:42:30 +02001184void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001185{
1186 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001187
Emeric Brun64cc49c2017-10-03 14:46:45 +02001188 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001189 return;
1190
Emeric Brun52a91d32017-08-31 14:41:55 +02001191 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001192 return;
1193
Emeric Brun52a91d32017-08-31 14:41:55 +02001194 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001195 *s->op_st_chg.reason = 0;
1196 s->op_st_chg.status = -1;
1197 if (reason) {
1198 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1199 }
1200 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001201 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001202 s->op_st_chg.code = check->code;
1203 s->op_st_chg.status = check->status;
1204 s->op_st_chg.duration = check->duration;
1205 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001206
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001207 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001208 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001209
Emeric Brun9f0b4582017-10-23 14:39:51 +02001210 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001211 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001212 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001213 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001214 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001215}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001216
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001217/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1218 * enforce either maint mode or drain mode. It is not allowed to set more than
1219 * one flag at once. The equivalent "inherited" flag is propagated to all
1220 * tracking servers. Maintenance mode disables health checks (but not agent
1221 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001222 * is done. If <cause> is non-null, it will be displayed at the end of the log
1223 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001224 *
1225 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001226 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001227void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001228{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001229 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001230
1231 if (!mode)
1232 return;
1233
1234 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001235 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001236 return;
1237
Emeric Brun52a91d32017-08-31 14:41:55 +02001238 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001239 if (cause)
1240 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1241
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001242 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001243 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001244
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001245 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001246 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1247 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001248 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001249
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001250 /* compute the inherited flag to propagate */
1251 if (mode & SRV_ADMF_MAINT)
1252 mode = SRV_ADMF_IMAINT;
1253 else if (mode & SRV_ADMF_DRAIN)
1254 mode = SRV_ADMF_IDRAIN;
1255
Emeric Brun9f0b4582017-10-23 14:39:51 +02001256 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001257 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001258 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001259 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001260 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001261}
1262
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001263/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1264 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1265 * than one flag at once. The equivalent "inherited" flag is propagated to all
1266 * tracking servers. Leaving maintenance mode re-enables health checks. When
1267 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001268 *
1269 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001270 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001271void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001272{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001273 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001274
1275 if (!mode)
1276 return;
1277
1278 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001279 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001280 return;
1281
Emeric Brun52a91d32017-08-31 14:41:55 +02001282 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001283
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001284 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001285 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001286
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001287 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001288 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1289 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001290 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001291
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001292 if (mode & SRV_ADMF_MAINT)
1293 mode = SRV_ADMF_IMAINT;
1294 else if (mode & SRV_ADMF_DRAIN)
1295 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001296
Emeric Brun9f0b4582017-10-23 14:39:51 +02001297 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001298 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001299 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001300 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001301 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001302}
1303
Willy Tarreau757478e2016-11-03 19:22:19 +01001304/* principle: propagate maint and drain to tracking servers. This is useful
1305 * upon startup so that inherited states are correct.
1306 */
1307static void srv_propagate_admin_state(struct server *srv)
1308{
1309 struct server *srv2;
1310
1311 if (!srv->trackers)
1312 return;
1313
1314 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001315 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001316 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001317 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001318
Emeric Brun52a91d32017-08-31 14:41:55 +02001319 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001320 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001321 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001322 }
1323}
1324
1325/* Compute and propagate the admin states for all servers in proxy <px>.
1326 * Only servers *not* tracking another one are considered, because other
1327 * ones will be handled when the server they track is visited.
1328 */
1329void srv_compute_all_admin_states(struct proxy *px)
1330{
1331 struct server *srv;
1332
1333 for (srv = px->srv; srv; srv = srv->next) {
1334 if (srv->track)
1335 continue;
1336 srv_propagate_admin_state(srv);
1337 }
1338}
1339
Willy Tarreaudff55432012-10-10 17:51:05 +02001340/* Note: must not be declared <const> as its list will be overwritten.
1341 * Please take care of keeping this list alphabetically sorted, doing so helps
1342 * all code contributors.
1343 * Optional keywords are also declared with a NULL ->parse() function so that
1344 * the config parser can report an appropriate error when a known keyword was
1345 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001346 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001347 */
1348static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001349 { "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 +01001350 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001351 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001352 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001353 { "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 +01001354 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001355 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1356 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001357 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001358 { "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 +01001359 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001360 { "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 +01001361 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001362 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001363 { "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 +01001364 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1365 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001366 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001367 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001368 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001369 { "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 +01001370 { "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 +02001371 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001372 { "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 +01001373 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001374 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1375 { "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 +02001376 { "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 +01001377 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001378 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001379 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001380 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1381 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001382 { NULL, NULL, 0 },
1383}};
1384
Willy Tarreau0108d902018-11-25 19:14:37 +01001385INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001386
Willy Tarreau004e0452013-11-21 11:22:01 +01001387/* Recomputes the server's eweight based on its state, uweight, the current time,
1388 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001389 * state is automatically disabled if the time is elapsed. If <must_update> is
1390 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001391 *
1392 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001393 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001394void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001395{
1396 struct proxy *px = sv->proxy;
1397 unsigned w;
1398
1399 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1400 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001401 if (sv->next_state == SRV_ST_STARTING)
1402 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001403 }
1404
1405 /* We must take care of not pushing the server to full throttle during slow starts.
1406 * It must also start immediately, at least at the minimal step when leaving maintenance.
1407 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001408 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001409 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1410 else
1411 w = px->lbprm.wdiv;
1412
Emeric Brun52a91d32017-08-31 14:41:55 +02001413 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001414
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001415 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001416 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001417 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001418}
1419
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001421 * Parses weight_str and configures sv accordingly.
1422 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001423 *
1424 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001425 */
1426const char *server_parse_weight_change_request(struct server *sv,
1427 const char *weight_str)
1428{
1429 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001430 long int w;
1431 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001432
1433 px = sv->proxy;
1434
1435 /* if the weight is terminated with '%', it is set relative to
1436 * the initial weight, otherwise it is absolute.
1437 */
1438 if (!*weight_str)
1439 return "Require <weight> or <weight%>.\n";
1440
Simon Hormanb796afa2013-02-12 10:45:53 +09001441 w = strtol(weight_str, &end, 10);
1442 if (end == weight_str)
1443 return "Empty weight string empty or preceded by garbage";
1444 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001445 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001446 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001447 /* Avoid integer overflow */
1448 if (w > 25600)
1449 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001450 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001451 if (w > 256)
1452 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001453 }
1454 else if (w < 0 || w > 256)
1455 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001456 else if (end[0] != '\0')
1457 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001458
1459 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1460 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1461
1462 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001463 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001464
1465 return NULL;
1466}
1467
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001468/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001469 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1470 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001471 * Returns:
1472 * - error string on error
1473 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001474 *
1475 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001476 */
1477const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001478 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001479{
1480 unsigned char ip[INET6_ADDRSTRLEN];
1481
1482 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001483 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001484 return NULL;
1485 }
1486 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001487 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001488 return NULL;
1489 }
1490
1491 return "Could not understand IP address format.\n";
1492}
1493
Willy Tarreau46b7f532018-08-21 11:54:26 +02001494/*
1495 * Must be called with the server lock held.
1496 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001497const char *server_parse_maxconn_change_request(struct server *sv,
1498 const char *maxconn_str)
1499{
1500 long int v;
1501 char *end;
1502
1503 if (!*maxconn_str)
1504 return "Require <maxconn>.\n";
1505
1506 v = strtol(maxconn_str, &end, 10);
1507 if (end == maxconn_str)
1508 return "maxconn string empty or preceded by garbage";
1509 else if (end[0] != '\0')
1510 return "Trailing garbage in maxconn string";
1511
1512 if (sv->maxconn == sv->minconn) { // static maxconn
1513 sv->maxconn = sv->minconn = v;
1514 } else { // dynamic maxconn
1515 sv->maxconn = v;
1516 }
1517
1518 if (may_dequeue_tasks(sv, sv->proxy))
1519 process_srv_queue(sv);
1520
1521 return NULL;
1522}
1523
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001524#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001525static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1526 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001527{
1528 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001529 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001530 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001531 NULL,
1532 };
1533
1534 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001535 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001536
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001537 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1538}
1539
1540static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1541{
1542 struct sample_expr *expr;
1543
1544 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 +01001545 if (!expr) {
1546 memprintf(err, "error detected while parsing sni expression : %s", *err);
1547 return ERR_ALERT | ERR_FATAL;
1548 }
1549
1550 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1551 memprintf(err, "error detected while parsing sni expression : "
1552 " fetch method '%s' extracts information from '%s', "
1553 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001554 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001555 return ERR_ALERT | ERR_FATAL;
1556 }
1557
1558 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1559 release_sample_expr(newsrv->ssl_ctx.sni);
1560 newsrv->ssl_ctx.sni = expr;
1561
1562 return 0;
1563}
1564#endif
1565
1566static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1567{
1568 if (err && *err) {
1569 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001570 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 +01001571 }
1572 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001573 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1574 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001575}
1576
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001577static void srv_conn_src_sport_range_cpy(struct server *srv,
1578 struct server *src)
1579{
1580 int range_sz;
1581
1582 range_sz = src->conn_src.sport_range->size;
1583 if (range_sz > 0) {
1584 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1585 if (srv->conn_src.sport_range != NULL) {
1586 int i;
1587
1588 for (i = 0; i < range_sz; i++) {
1589 srv->conn_src.sport_range->ports[i] =
1590 src->conn_src.sport_range->ports[i];
1591 }
1592 }
1593 }
1594}
1595
1596/*
1597 * Copy <src> server connection source settings to <srv> server everything needed.
1598 */
1599static void srv_conn_src_cpy(struct server *srv, struct server *src)
1600{
1601 srv->conn_src.opts = src->conn_src.opts;
1602 srv->conn_src.source_addr = src->conn_src.source_addr;
1603
1604 /* Source port range copy. */
1605 if (src->conn_src.sport_range != NULL)
1606 srv_conn_src_sport_range_cpy(srv, src);
1607
1608#ifdef CONFIG_HAP_TRANSPARENT
1609 if (src->conn_src.bind_hdr_name != NULL) {
1610 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1611 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1612 }
1613 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1614 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1615#endif
1616 if (src->conn_src.iface_name != NULL)
1617 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1618}
1619
1620/*
1621 * Copy <src> server SSL settings to <srv> server allocating
1622 * everything needed.
1623 */
1624#if defined(USE_OPENSSL)
1625static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1626{
1627 if (src->ssl_ctx.ca_file != NULL)
1628 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1629 if (src->ssl_ctx.crl_file != NULL)
1630 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1631 if (src->ssl_ctx.client_crt != NULL)
1632 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1633
1634 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1635
1636 if (src->ssl_ctx.verify_host != NULL)
1637 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1638 if (src->ssl_ctx.ciphers != NULL)
1639 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001640#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001641 if (src->ssl_ctx.ciphersuites != NULL)
1642 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1643#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001644 if (src->sni_expr != NULL)
1645 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001646
1647#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1648 if (src->ssl_ctx.alpn_str) {
1649 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1650 if (srv->ssl_ctx.alpn_str) {
1651 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1652 src->ssl_ctx.alpn_len);
1653 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1654 }
1655 }
1656#endif
1657#ifdef OPENSSL_NPN_NEGOTIATED
1658 if (src->ssl_ctx.npn_str) {
1659 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1660 if (srv->ssl_ctx.npn_str) {
1661 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1662 src->ssl_ctx.npn_len);
1663 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1664 }
1665 }
1666#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001667}
1668#endif
1669
1670/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001671 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001672 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001673 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001674 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001675static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001676{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001677 char *hostname_dn;
1678 int hostname_len, hostname_dn_len;
1679
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001680 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001681 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001682
Christopher Faulet67957bd2017-09-27 11:00:59 +02001683 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001684 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001685 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1686 hostname_dn, trash.size);
1687 if (hostname_dn_len == -1)
1688 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001689
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001690
Christopher Faulet67957bd2017-09-27 11:00:59 +02001691 free(srv->hostname);
1692 free(srv->hostname_dn);
1693 srv->hostname = strdup(hostname);
1694 srv->hostname_dn = strdup(hostname_dn);
1695 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001696 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001697 goto err;
1698
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001699 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001700
1701 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001702 free(srv->hostname); srv->hostname = NULL;
1703 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001704 return -1;
1705}
1706
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001707/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001708 * Copy <src> server settings to <srv> server allocating
1709 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001710 * This function is not supposed to be called at any time, but only
1711 * during server settings parsing or during server allocations from
1712 * a server template, and just after having calloc()'ed a new server.
1713 * So, <src> may only be a default server (when parsing server settings)
1714 * or a server template (during server allocations from a server template).
1715 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1716 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001717 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001718static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001719{
1720 /* Connection source settings copy */
1721 srv_conn_src_cpy(srv, src);
1722
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001723 if (srv_tmpl) {
1724 srv->addr = src->addr;
1725 srv->svc_port = src->svc_port;
1726 }
1727
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001728 srv->pp_opts = src->pp_opts;
1729 if (src->rdr_pfx != NULL) {
1730 srv->rdr_pfx = strdup(src->rdr_pfx);
1731 srv->rdr_len = src->rdr_len;
1732 }
1733 if (src->cookie != NULL) {
1734 srv->cookie = strdup(src->cookie);
1735 srv->cklen = src->cklen;
1736 }
1737 srv->use_ssl = src->use_ssl;
1738 srv->check.addr = srv->agent.addr = src->check.addr;
1739 srv->check.use_ssl = src->check.use_ssl;
1740 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001741 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001742 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001743 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001744 /* Note: 'flags' field has potentially been already initialized. */
1745 srv->flags |= src->flags;
1746 srv->do_check = src->do_check;
1747 srv->do_agent = src->do_agent;
1748 if (srv->check.port)
1749 srv->flags |= SRV_F_CHECKPORT;
1750 srv->check.inter = src->check.inter;
1751 srv->check.fastinter = src->check.fastinter;
1752 srv->check.downinter = src->check.downinter;
1753 srv->agent.use_ssl = src->agent.use_ssl;
1754 srv->agent.port = src->agent.port;
1755 if (src->agent.send_string != NULL)
1756 srv->agent.send_string = strdup(src->agent.send_string);
1757 srv->agent.send_string_len = src->agent.send_string_len;
1758 srv->agent.inter = src->agent.inter;
1759 srv->agent.fastinter = src->agent.fastinter;
1760 srv->agent.downinter = src->agent.downinter;
1761 srv->maxqueue = src->maxqueue;
1762 srv->minconn = src->minconn;
1763 srv->maxconn = src->maxconn;
1764 srv->slowstart = src->slowstart;
1765 srv->observe = src->observe;
1766 srv->onerror = src->onerror;
1767 srv->onmarkeddown = src->onmarkeddown;
1768 srv->onmarkedup = src->onmarkedup;
1769 if (src->trackit != NULL)
1770 srv->trackit = strdup(src->trackit);
1771 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1772 srv->uweight = srv->iweight = src->iweight;
1773
1774 srv->check.send_proxy = src->check.send_proxy;
1775 /* health: up, but will fall down at first failure */
1776 srv->check.rise = srv->check.health = src->check.rise;
1777 srv->check.fall = src->check.fall;
1778
1779 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001780 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1781 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1782 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001783 srv->check.state |= CHK_ST_PAUSED;
1784 srv->check.health = 0;
1785 }
1786
1787 /* health: up but will fall down at first failure */
1788 srv->agent.rise = srv->agent.health = src->agent.rise;
1789 srv->agent.fall = src->agent.fall;
1790
1791 if (src->resolvers_id != NULL)
1792 srv->resolvers_id = strdup(src->resolvers_id);
1793 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001794 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001795 if (srv->dns_opts.family_prio == AF_UNSPEC)
1796 srv->dns_opts.family_prio = AF_INET6;
1797 memcpy(srv->dns_opts.pref_net,
1798 src->dns_opts.pref_net,
1799 sizeof srv->dns_opts.pref_net);
1800 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1801
1802 srv->init_addr_methods = src->init_addr_methods;
1803 srv->init_addr = src->init_addr;
1804#if defined(USE_OPENSSL)
1805 srv_ssl_settings_cpy(srv, src);
1806#endif
1807#ifdef TCP_USER_TIMEOUT
1808 srv->tcp_ut = src->tcp_ut;
1809#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001810 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001811 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001812 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001813 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001814
Olivier Houchard8da5f982017-08-04 18:35:36 +02001815 if (srv_tmpl)
1816 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001817
1818 srv->check.via_socks4 = src->check.via_socks4;
1819 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001820}
1821
William Lallemand313bfd12018-10-26 14:47:32 +02001822struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001823{
1824 struct server *srv;
1825
1826 srv = calloc(1, sizeof *srv);
1827 if (!srv)
1828 return NULL;
1829
1830 srv->obj_type = OBJ_TYPE_SERVER;
1831 srv->proxy = proxy;
1832 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001833 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001834
Emeric Brun52a91d32017-08-31 14:41:55 +02001835 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001836 srv->last_change = now.tv_sec;
1837
1838 srv->check.status = HCHK_STATUS_INI;
1839 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001840 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001841 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1842
1843 srv->agent.status = HCHK_STATUS_INI;
1844 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001845 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001846 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1847
Willy Tarreau975b1552019-06-06 16:25:55 +02001848 /* please don't put default server settings here, they are set in
1849 * init_default_instance().
1850 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001851 return srv;
1852}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001853
1854/*
1855 * Validate <srv> server health-check settings.
1856 * Returns 0 if everything is OK, -1 if not.
1857 */
1858static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1859{
1860 struct tcpcheck_rule *r = NULL;
1861 struct list *l;
1862
1863 /*
1864 * We need at least a service port, a check port or the first tcp-check rule must
1865 * be a 'connect' one when checking an IPv4/IPv6 server.
1866 */
1867 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1868 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1869 return 0;
1870
1871 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1872 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001873 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1874 "Check has been disabled.\n",
1875 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001876 return -1;
1877 }
1878
1879 /* search the first action (connect / send / expect) in the list */
1880 l = &srv->proxy->tcpcheck_rules;
1881 list_for_each_entry(r, l, list) {
1882 if (r->action != TCPCHK_ACT_COMMENT)
1883 break;
1884 }
1885
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 "nor tcp_check rule 'connect' with 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 /* scan the tcp-check ruleset to ensure a port has been configured */
1894 l = &srv->proxy->tcpcheck_rules;
1895 list_for_each_entry(r, l, list) {
1896 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001897 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1898 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1899 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001900 return -1;
1901 }
1902 }
1903
1904 return 0;
1905}
1906
1907/*
1908 * Initialize <srv> health-check structure.
1909 * Returns the error string in case of memory allocation failure, NULL if not.
1910 */
1911static const char *do_health_check_init(struct server *srv, int check_type, int state)
1912{
1913 const char *ret;
1914
1915 if (!srv->do_check)
1916 return NULL;
1917
1918 ret = init_check(&srv->check, check_type);
1919 if (ret)
1920 return ret;
1921
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001922 srv->check.state |= state;
1923 global.maxsock++;
1924
1925 return NULL;
1926}
1927
1928static int server_health_check_init(const char *file, int linenum,
1929 struct server *srv, struct proxy *curproxy)
1930{
1931 const char *ret;
1932
1933 if (!srv->do_check)
1934 return 0;
1935
1936 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001937 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1938 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001939 return ERR_ALERT | ERR_FATAL;
1940 }
1941
1942 if (server_healthcheck_validate(file, linenum, srv) < 0)
1943 return ERR_ALERT | ERR_ABORT;
1944
1945 /* note: check type will be set during the config review phase */
1946 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1947 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001948 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001949 return ERR_ALERT | ERR_ABORT;
1950 }
1951
1952 return 0;
1953}
1954
1955/*
1956 * Initialize <srv> agent check structure.
1957 * Returns the error string in case of memory allocation failure, NULL if not.
1958 */
1959static const char *do_server_agent_check_init(struct server *srv, int state)
1960{
1961 const char *ret;
1962
1963 if (!srv->do_agent)
1964 return NULL;
1965
1966 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1967 if (ret)
1968 return ret;
1969
1970 if (!srv->agent.inter)
1971 srv->agent.inter = srv->check.inter;
1972
1973 srv->agent.state |= state;
1974 global.maxsock++;
1975
1976 return NULL;
1977}
1978
1979static int server_agent_check_init(const char *file, int linenum,
1980 struct server *srv, struct proxy *curproxy)
1981{
1982 const char *ret;
1983
1984 if (!srv->do_agent)
1985 return 0;
1986
1987 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001988 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 +02001989 file, linenum, srv->id);
1990 return ERR_ALERT | ERR_FATAL;
1991 }
1992
1993 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1994 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001995 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001996 return ERR_ALERT | ERR_ABORT;
1997 }
1998
1999 return 0;
2000}
2001
2002#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2003static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2004 struct server *srv, struct proxy *proxy)
2005{
2006 int ret;
2007 char *err = NULL;
2008
2009 if (!srv->sni_expr)
2010 return 0;
2011
2012 ret = server_parse_sni_expr(srv, proxy, &err);
2013 if (!ret)
2014 return 0;
2015
2016 display_parser_err(file, linenum, args, cur_arg, &err);
2017 free(err);
2018
2019 return ret;
2020}
2021#endif
2022
2023/*
2024 * Server initializations finalization.
2025 * Initialize health check, agent check and SNI expression if enabled.
2026 * Must not be called for a default server instance.
2027 */
2028static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2029 struct server *srv, struct proxy *px)
2030{
2031 int ret;
2032
2033 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2034 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2035 return ret;
2036 }
2037
2038#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2039 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2040 return ret;
2041#endif
2042
2043 if (srv->flags & SRV_F_BACKUP)
2044 px->srv_bck++;
2045 else
2046 px->srv_act++;
2047 srv_lb_commit_status(srv);
2048
2049 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002050err:
2051 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002052}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002053
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002054/*
2055 * Parse as much as possible such a range string argument: low[-high]
2056 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2057 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2058 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2059 * Returns 0 if succeeded, -1 if not.
2060 */
2061static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2062{
2063 char *nb_high_arg;
2064
2065 *nb_high = 0;
2066 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002067 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002068
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002069 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002070 *nb_high_arg++ = '\0';
2071 *nb_high = atoi(nb_high_arg);
2072 }
2073 else {
2074 *nb_high += *nb_low;
2075 *nb_low = 1;
2076 }
2077
2078 if (*nb_low < 0 || *nb_high < *nb_low)
2079 return -1;
2080
2081 return 0;
2082}
2083
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002084static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2085{
2086 chunk_printf(&trash, "%s%d", prefix, nb);
2087 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002088 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002089}
2090
2091/*
2092 * Initialize as much as possible servers from <srv> server template.
2093 * Note that a server template is a special server with
2094 * a few different parameters than a server which has
2095 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002096 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002097 * 'srv' template included.
2098 */
2099static int server_template_init(struct server *srv, struct proxy *px)
2100{
2101 int i;
2102 struct server *newsrv;
2103
2104 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2105 int check_init_state;
2106 int agent_init_state;
2107
2108 newsrv = new_server(px);
2109 if (!newsrv)
2110 goto err;
2111
2112 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002113 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002114#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2115 if (newsrv->sni_expr) {
2116 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2117 if (!newsrv->ssl_ctx.sni)
2118 goto err;
2119 }
2120#endif
2121 /* Set this new server ID. */
2122 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2123
2124 /* Initial checks states. */
2125 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2126 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2127
2128 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2129 do_server_agent_check_init(newsrv, agent_init_state))
2130 goto err;
2131
2132 /* Linked backwards first. This will be restablished after parsing. */
2133 newsrv->next = px->srv;
2134 px->srv = newsrv;
2135 }
2136 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2137
2138 return i - srv->tmpl_info.nb_low;
2139
2140 err:
2141 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2142 if (newsrv) {
2143#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2144 release_sample_expr(newsrv->ssl_ctx.sni);
2145#endif
2146 free_check(&newsrv->agent);
2147 free_check(&newsrv->check);
2148 }
2149 free(newsrv);
2150 return i - srv->tmpl_info.nb_low;
2151}
2152
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002153int 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 +02002154{
2155 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002156 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002157 char *errmsg = NULL;
2158 int err_code = 0;
2159 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002160 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002161
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002162 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002163 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002164 !strcmp(args[0], "default-server") ||
2165 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002167 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002168 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002169 int srv_tmpl = !defsrv && !srv;
2170 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002171
2172 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002173 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002174 err_code |= ERR_ALERT | ERR_FATAL;
2175 goto out;
2176 }
2177 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002178 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002179
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002180 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002181 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002182 if (!*args[2]) {
2183 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002184 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002185 file, linenum, args[0]);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189
2190 err = invalid_char(args[1]);
2191 }
2192 else if (srv_tmpl) {
2193 if (!*args[3]) {
2194 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002195 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 +02002196 file, linenum, args[0]);
2197 err_code |= ERR_ALERT | ERR_FATAL;
2198 goto out;
2199 }
2200
2201 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002202 }
2203
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002204 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002205 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 +02002206 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002207 err_code |= ERR_ALERT | ERR_FATAL;
2208 goto out;
2209 }
2210
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002211 cur_arg = 2;
2212 if (srv_tmpl) {
2213 /* Parse server-template <nb | range> arg. */
2214 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002215 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002216 file, linenum, args[0], args[cur_arg]);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220 cur_arg++;
2221 }
2222
Willy Tarreau272adea2014-03-31 10:39:59 +02002223 if (!defsrv) {
2224 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002225 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002226 struct protocol *proto;
2227
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002228 newsrv = new_server(curproxy);
2229 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002230 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002231 err_code |= ERR_ALERT | ERR_ABORT;
2232 goto out;
2233 }
2234
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002235 if (srv_tmpl) {
2236 newsrv->tmpl_info.nb_low = tmpl_range_low;
2237 newsrv->tmpl_info.nb_high = tmpl_range_high;
2238 }
2239
Willy Tarreau272adea2014-03-31 10:39:59 +02002240 /* the servers are linked backwards first */
2241 newsrv->next = curproxy->srv;
2242 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002243 newsrv->conf.file = strdup(file);
2244 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002245 /* Note: for a server template, its id is its prefix.
2246 * This is a temporary id which will be used for server allocations to come
2247 * after parsing.
2248 */
2249 if (srv)
2250 newsrv->id = strdup(args[1]);
2251 else
2252 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002253
2254 /* several ways to check the port component :
2255 * - IP => port=+0, relative (IPv4 only)
2256 * - IP: => port=+0, relative
2257 * - IP:N => port=N, absolute
2258 * - IP:+N => port=+N, relative
2259 * - IP:-N => port=-N, relative
2260 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002261 if (!parse_addr)
2262 goto skip_addr;
2263
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002264 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002265 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002266 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002267 err_code |= ERR_ALERT | ERR_FATAL;
2268 goto out;
2269 }
2270
2271 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002272 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002273 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002274 file, linenum, args[0], args[1]);
2275 err_code |= ERR_ALERT | ERR_FATAL;
2276 goto out;
2277 }
2278
2279 if (!port1 || !port2) {
2280 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002281 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002282 }
2283 else if (port1 != port2) {
2284 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002285 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002286 file, linenum, args[0], args[1], args[2]);
2287 err_code |= ERR_ALERT | ERR_FATAL;
2288 goto out;
2289 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002290
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002291 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002292 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002293 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002294 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002295 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2296 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2297 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002298 err_code |= ERR_ALERT | ERR_FATAL;
2299 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002300 }
2301 }
2302 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002303 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002304 file, linenum, newsrv->id);
2305 err_code |= ERR_ALERT | ERR_FATAL;
2306 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002307 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002308 }
2309
Willy Tarreau272adea2014-03-31 10:39:59 +02002310 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002311 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002312
Olivier Houchard8da5f982017-08-04 18:35:36 +02002313 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002314 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002315 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002316 err_code |= ERR_ALERT | ERR_FATAL;
2317 goto out;
2318 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002319
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002320 cur_arg++;
2321 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002322 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002323 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002324 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002325 } else {
2326 newsrv = &curproxy->defsrv;
2327 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002328 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002329 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 }
2331
2332 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002333 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002334 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002335
2336 if (err == PARSE_TIME_OVER) {
2337 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2338 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2339 err_code |= ERR_ALERT | ERR_FATAL;
2340 goto out;
2341 }
2342 else if (err == PARSE_TIME_UNDER) {
2343 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2344 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2345 err_code |= ERR_ALERT | ERR_FATAL;
2346 goto out;
2347 }
2348 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002349 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 file, linenum, *err, newsrv->id);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002355 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002356 file, linenum, val, args[cur_arg], newsrv->id);
2357 err_code |= ERR_ALERT | ERR_FATAL;
2358 goto out;
2359 }
2360 newsrv->agent.inter = val;
2361 cur_arg += 2;
2362 }
Misiekea849332017-01-09 09:39:51 +01002363 else if (!strcmp(args[cur_arg], "agent-addr")) {
2364 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002365 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002366 goto out;
2367 }
2368
2369 cur_arg += 2;
2370 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002371 else if (!strcmp(args[cur_arg], "agent-port")) {
2372 global.maxsock++;
2373 newsrv->agent.port = atol(args[cur_arg + 1]);
2374 cur_arg += 2;
2375 }
James Brown55f9ff12015-10-21 18:19:05 -07002376 else if (!strcmp(args[cur_arg], "agent-send")) {
2377 global.maxsock++;
2378 free(newsrv->agent.send_string);
2379 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2380 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2381 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2382 cur_arg += 2;
2383 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002384 else if (!strcmp(args[cur_arg], "init-addr")) {
2385 char *p, *end;
2386 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002387 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002388
2389 newsrv->init_addr_methods = 0;
2390 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2391
2392 for (p = args[cur_arg + 1]; *p; p = end) {
2393 /* cut on next comma */
2394 for (end = p; *end && *end != ','; end++);
2395 if (*end)
2396 *(end++) = 0;
2397
Willy Tarreau4310d362016-11-02 15:05:56 +01002398 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002399 if (!strcmp(p, "libc")) {
2400 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2401 }
2402 else if (!strcmp(p, "last")) {
2403 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2404 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002405 else if (!strcmp(p, "none")) {
2406 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2407 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002408 else if (str2ip2(p, &sa, 0)) {
2409 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002410 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002411 file, linenum, args[cur_arg], p);
2412 err_code |= ERR_ALERT | ERR_FATAL;
2413 goto out;
2414 }
2415 newsrv->init_addr = sa;
2416 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2417 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002418 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002419 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 +02002420 file, linenum, args[cur_arg], p);
2421 err_code |= ERR_ALERT | ERR_FATAL;
2422 goto out;
2423 }
2424 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002425 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002426 file, linenum, args[cur_arg], p);
2427 err_code |= ERR_ALERT | ERR_FATAL;
2428 goto out;
2429 }
2430 }
2431 cur_arg += 2;
2432 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002433 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002434 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002435 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2436 cur_arg += 2;
2437 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002438 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2439 char *p, *end;
2440
2441 for (p = args[cur_arg + 1]; *p; p = end) {
2442 /* cut on next comma */
2443 for (end = p; *end && *end != ','; end++);
2444 if (*end)
2445 *(end++) = 0;
2446
2447 if (!strcmp(p, "allow-dup-ip")) {
2448 newsrv->dns_opts.accept_duplicate_ip = 1;
2449 }
2450 else if (!strcmp(p, "prevent-dup-ip")) {
2451 newsrv->dns_opts.accept_duplicate_ip = 0;
2452 }
2453 else {
2454 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2455 file, linenum, args[cur_arg], p);
2456 err_code |= ERR_ALERT | ERR_FATAL;
2457 goto out;
2458 }
2459 }
2460
2461 cur_arg += 2;
2462 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002463 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2464 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002465 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002466 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002467 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002468 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002469 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002470 file, linenum, args[cur_arg]);
2471 err_code |= ERR_ALERT | ERR_FATAL;
2472 goto out;
2473 }
2474 cur_arg += 2;
2475 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002476 else if (!strcmp(args[cur_arg], "resolve-net")) {
2477 char *p, *e;
2478 unsigned char mask;
2479 struct dns_options *opt;
2480
2481 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002482 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002483 file, linenum, args[cur_arg]);
2484 err_code |= ERR_ALERT | ERR_FATAL;
2485 goto out;
2486 }
2487
2488 opt = &newsrv->dns_opts;
2489
2490 /* Split arguments by comma, and convert it from ipv4 or ipv6
2491 * string network in in_addr or in6_addr.
2492 */
2493 p = args[cur_arg + 1];
2494 e = p;
2495 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002496 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002497 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002498 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002499 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2500 err_code |= ERR_ALERT | ERR_FATAL;
2501 goto out;
2502 }
2503 /* look for end or comma. */
2504 while (*e != ',' && *e != '\0')
2505 e++;
2506 if (*e == ',') {
2507 *e = '\0';
2508 e++;
2509 }
2510 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2511 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2512 /* Try to convert input string from ipv4 or ipv6 network. */
2513 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2514 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2515 &mask)) {
2516 /* Try to convert input string from ipv6 network. */
2517 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2518 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2519 } else {
2520 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002521 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002522 file, linenum, args[cur_arg], p);
2523 err_code |= ERR_ALERT | ERR_FATAL;
2524 goto out;
2525 }
2526 opt->pref_net_nb++;
2527 p = e;
2528 }
2529
2530 cur_arg += 2;
2531 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002532 else if (!strcmp(args[cur_arg], "rise")) {
2533 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002534 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002535 file, linenum, args[cur_arg]);
2536 err_code |= ERR_ALERT | ERR_FATAL;
2537 goto out;
2538 }
2539
2540 newsrv->check.rise = atol(args[cur_arg + 1]);
2541 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002542 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002543 file, linenum, args[cur_arg]);
2544 err_code |= ERR_ALERT | ERR_FATAL;
2545 goto out;
2546 }
2547
2548 if (newsrv->check.health)
2549 newsrv->check.health = newsrv->check.rise;
2550 cur_arg += 2;
2551 }
2552 else if (!strcmp(args[cur_arg], "fall")) {
2553 newsrv->check.fall = atol(args[cur_arg + 1]);
2554
2555 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002556 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002557 file, linenum, args[cur_arg]);
2558 err_code |= ERR_ALERT | ERR_FATAL;
2559 goto out;
2560 }
2561
2562 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002563 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002564 file, linenum, args[cur_arg]);
2565 err_code |= ERR_ALERT | ERR_FATAL;
2566 goto out;
2567 }
2568
2569 cur_arg += 2;
2570 }
2571 else if (!strcmp(args[cur_arg], "inter")) {
2572 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002573
2574 if (err == PARSE_TIME_OVER) {
2575 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2576 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2577 err_code |= ERR_ALERT | ERR_FATAL;
2578 goto out;
2579 }
2580 else if (err == PARSE_TIME_UNDER) {
2581 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2582 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2583 err_code |= ERR_ALERT | ERR_FATAL;
2584 goto out;
2585 }
2586 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002587 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002588 file, linenum, *err, newsrv->id);
2589 err_code |= ERR_ALERT | ERR_FATAL;
2590 goto out;
2591 }
2592 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002593 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002594 file, linenum, val, args[cur_arg], newsrv->id);
2595 err_code |= ERR_ALERT | ERR_FATAL;
2596 goto out;
2597 }
2598 newsrv->check.inter = val;
2599 cur_arg += 2;
2600 }
2601 else if (!strcmp(args[cur_arg], "fastinter")) {
2602 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002603
2604 if (err == PARSE_TIME_OVER) {
2605 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2606 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2607 err_code |= ERR_ALERT | ERR_FATAL;
2608 goto out;
2609 }
2610 else if (err == PARSE_TIME_UNDER) {
2611 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2612 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2613 err_code |= ERR_ALERT | ERR_FATAL;
2614 goto out;
2615 }
2616 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002617 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002618 file, linenum, *err, newsrv->id);
2619 err_code |= ERR_ALERT | ERR_FATAL;
2620 goto out;
2621 }
2622 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002623 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002624 file, linenum, val, args[cur_arg], newsrv->id);
2625 err_code |= ERR_ALERT | ERR_FATAL;
2626 goto out;
2627 }
2628 newsrv->check.fastinter = val;
2629 cur_arg += 2;
2630 }
2631 else if (!strcmp(args[cur_arg], "downinter")) {
2632 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002633
2634 if (err == PARSE_TIME_OVER) {
2635 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2636 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2637 err_code |= ERR_ALERT | ERR_FATAL;
2638 goto out;
2639 }
2640 else if (err == PARSE_TIME_UNDER) {
2641 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2642 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2643 err_code |= ERR_ALERT | ERR_FATAL;
2644 goto out;
2645 }
2646 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002647 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002648 file, linenum, *err, newsrv->id);
2649 err_code |= ERR_ALERT | ERR_FATAL;
2650 goto out;
2651 }
2652 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002653 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002654 file, linenum, val, args[cur_arg], newsrv->id);
2655 err_code |= ERR_ALERT | ERR_FATAL;
2656 goto out;
2657 }
2658 newsrv->check.downinter = val;
2659 cur_arg += 2;
2660 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002661 else if (!strcmp(args[cur_arg], "port")) {
2662 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002663 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002664 cur_arg += 2;
2665 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002666 else if (!strcmp(args[cur_arg], "weight")) {
2667 int w;
2668 w = atol(args[cur_arg + 1]);
2669 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002670 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002671 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2672 err_code |= ERR_ALERT | ERR_FATAL;
2673 goto out;
2674 }
2675 newsrv->uweight = newsrv->iweight = w;
2676 cur_arg += 2;
2677 }
2678 else if (!strcmp(args[cur_arg], "minconn")) {
2679 newsrv->minconn = atol(args[cur_arg + 1]);
2680 cur_arg += 2;
2681 }
2682 else if (!strcmp(args[cur_arg], "maxconn")) {
2683 newsrv->maxconn = atol(args[cur_arg + 1]);
2684 cur_arg += 2;
2685 }
2686 else if (!strcmp(args[cur_arg], "maxqueue")) {
2687 newsrv->maxqueue = atol(args[cur_arg + 1]);
2688 cur_arg += 2;
2689 }
2690 else if (!strcmp(args[cur_arg], "slowstart")) {
2691 /* slowstart is stored in seconds */
2692 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002693
2694 if (err == PARSE_TIME_OVER) {
2695 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2696 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2697 err_code |= ERR_ALERT | ERR_FATAL;
2698 goto out;
2699 }
2700 else if (err == PARSE_TIME_UNDER) {
2701 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2702 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2703 err_code |= ERR_ALERT | ERR_FATAL;
2704 goto out;
2705 }
2706 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002707 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002708 file, linenum, *err, newsrv->id);
2709 err_code |= ERR_ALERT | ERR_FATAL;
2710 goto out;
2711 }
2712 newsrv->slowstart = (val + 999) / 1000;
2713 cur_arg += 2;
2714 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002715 else if (!strcmp(args[cur_arg], "on-error")) {
2716 if (!strcmp(args[cur_arg + 1], "fastinter"))
2717 newsrv->onerror = HANA_ONERR_FASTINTER;
2718 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2719 newsrv->onerror = HANA_ONERR_FAILCHK;
2720 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2721 newsrv->onerror = HANA_ONERR_SUDDTH;
2722 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2723 newsrv->onerror = HANA_ONERR_MARKDWN;
2724 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002725 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002726 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2727 file, linenum, args[cur_arg], args[cur_arg + 1]);
2728 err_code |= ERR_ALERT | ERR_FATAL;
2729 goto out;
2730 }
2731
2732 cur_arg += 2;
2733 }
2734 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2735 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2736 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2737 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002738 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002739 file, linenum, args[cur_arg], args[cur_arg + 1]);
2740 err_code |= ERR_ALERT | ERR_FATAL;
2741 goto out;
2742 }
2743
2744 cur_arg += 2;
2745 }
2746 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2747 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2748 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2749 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002750 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002751 file, linenum, args[cur_arg], args[cur_arg + 1]);
2752 err_code |= ERR_ALERT | ERR_FATAL;
2753 goto out;
2754 }
2755
2756 cur_arg += 2;
2757 }
2758 else if (!strcmp(args[cur_arg], "error-limit")) {
2759 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002760 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002761 file, linenum, args[cur_arg]);
2762 err_code |= ERR_ALERT | ERR_FATAL;
2763 goto out;
2764 }
2765
2766 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2767
2768 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002769 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002770 file, linenum, args[cur_arg]);
2771 err_code |= ERR_ALERT | ERR_FATAL;
2772 goto out;
2773 }
2774 cur_arg += 2;
2775 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002776 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002777 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002778 file, linenum, "usesrc", "source");
2779 err_code |= ERR_ALERT | ERR_FATAL;
2780 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002781 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002782 else {
2783 static int srv_dumped;
2784 struct srv_kw *kw;
2785 char *err;
2786
2787 kw = srv_find_kw(args[cur_arg]);
2788 if (kw) {
2789 char *err = NULL;
2790 int code;
2791
2792 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002793 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 +02002794 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002795 if (kw->skip != -1)
2796 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002797 err_code |= ERR_ALERT | ERR_FATAL;
2798 goto out;
2799 }
2800
2801 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002802 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002803 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002804 if (kw->skip != -1)
2805 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002806 err_code |= ERR_ALERT;
2807 continue;
2808 }
2809
2810 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2811 err_code |= code;
2812
2813 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002814 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002815 if (code & ERR_FATAL) {
2816 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002817 if (kw->skip != -1)
2818 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002819 goto out;
2820 }
2821 }
2822 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002823 if (kw->skip != -1)
2824 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002825 continue;
2826 }
2827
2828 err = NULL;
2829 if (!srv_dumped) {
2830 srv_dump_kws(&err);
2831 indent_msg(&err, 4);
2832 srv_dumped = 1;
2833 }
2834
Christopher Faulet767a84b2017-11-24 16:50:31 +01002835 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002836 file, linenum, args[0], args[1], args[cur_arg],
2837 err ? " Registered keywords :" : "", err ? err : "");
2838 free(err);
2839
2840 err_code |= ERR_ALERT | ERR_FATAL;
2841 goto out;
2842 }
2843 }
2844
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002845 if (!defsrv)
2846 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2847 if (err_code & ERR_FATAL)
2848 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002849 if (srv_tmpl)
2850 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002851 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002852 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002853 return 0;
2854
2855 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002856 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002857 free(errmsg);
2858 return err_code;
2859}
2860
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002861/* Returns a pointer to the first server matching either id <id>.
2862 * NULL is returned if no match is found.
2863 * the lookup is performed in the backend <bk>
2864 */
2865struct server *server_find_by_id(struct proxy *bk, int id)
2866{
2867 struct eb32_node *eb32;
2868 struct server *curserver;
2869
2870 if (!bk || (id ==0))
2871 return NULL;
2872
2873 /* <bk> has no backend capabilities, so it can't have a server */
2874 if (!(bk->cap & PR_CAP_BE))
2875 return NULL;
2876
2877 curserver = NULL;
2878
2879 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2880 if (eb32)
2881 curserver = container_of(eb32, struct server, conf.id);
2882
2883 return curserver;
2884}
2885
2886/* Returns a pointer to the first server matching either name <name>, or id
2887 * if <name> starts with a '#'. NULL is returned if no match is found.
2888 * the lookup is performed in the backend <bk>
2889 */
2890struct server *server_find_by_name(struct proxy *bk, const char *name)
2891{
2892 struct server *curserver;
2893
2894 if (!bk || !name)
2895 return NULL;
2896
2897 /* <bk> has no backend capabilities, so it can't have a server */
2898 if (!(bk->cap & PR_CAP_BE))
2899 return NULL;
2900
2901 curserver = NULL;
2902 if (*name == '#') {
2903 curserver = server_find_by_id(bk, atoi(name + 1));
2904 if (curserver)
2905 return curserver;
2906 }
2907 else {
2908 curserver = bk->srv;
2909
2910 while (curserver && (strcmp(curserver->id, name) != 0))
2911 curserver = curserver->next;
2912
2913 if (curserver)
2914 return curserver;
2915 }
2916
2917 return NULL;
2918}
2919
2920struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2921{
2922 struct server *byname;
2923 struct server *byid;
2924
2925 if (!name && !id)
2926 return NULL;
2927
2928 if (diff)
2929 *diff = 0;
2930
2931 byname = byid = NULL;
2932
2933 if (name) {
2934 byname = server_find_by_name(bk, name);
2935 if (byname && (!id || byname->puid == id))
2936 return byname;
2937 }
2938
2939 /* remaining possibilities :
2940 * - name not set
2941 * - name set but not found
2942 * - name found but ID doesn't match
2943 */
2944 if (id) {
2945 byid = server_find_by_id(bk, id);
2946 if (byid) {
2947 if (byname) {
2948 /* use id only if forced by configuration */
2949 if (byid->flags & SRV_F_FORCED_ID) {
2950 if (diff)
2951 *diff |= 2;
2952 return byid;
2953 }
2954 else {
2955 if (diff)
2956 *diff |= 1;
2957 return byname;
2958 }
2959 }
2960
2961 /* remaining possibilities:
2962 * - name not set
2963 * - name set but not found
2964 */
2965 if (name && diff)
2966 *diff |= 2;
2967 return byid;
2968 }
2969
2970 /* id bot found */
2971 if (byname) {
2972 if (diff)
2973 *diff |= 1;
2974 return byname;
2975 }
2976 }
2977
2978 return NULL;
2979}
2980
Willy Tarreau46b7f532018-08-21 11:54:26 +02002981/* Update a server state using the parameters available in the params list.
2982 *
2983 * Grabs the server lock during operation.
2984 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002985static void srv_update_state(struct server *srv, int version, char **params)
2986{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002987 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002988 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002989
2990 /* fields since version 1
2991 * and common to all other upcoming versions
2992 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002993 enum srv_state srv_op_state;
2994 enum srv_admin srv_admin_state;
2995 unsigned srv_uweight, srv_iweight;
2996 unsigned long srv_last_time_change;
2997 short srv_check_status;
2998 enum chk_result srv_check_result;
2999 int srv_check_health;
3000 int srv_check_state, srv_agent_state;
3001 int bk_f_forced_id;
3002 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003003 int fqdn_set_by_cli;
3004 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003005 const char *port_str;
3006 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003007 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003008
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003009 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003010 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003011 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003012 switch (version) {
3013 case 1:
3014 /*
3015 * now we can proceed with server's state update:
3016 * srv_addr: params[0]
3017 * srv_op_state: params[1]
3018 * srv_admin_state: params[2]
3019 * srv_uweight: params[3]
3020 * srv_iweight: params[4]
3021 * srv_last_time_change: params[5]
3022 * srv_check_status: params[6]
3023 * srv_check_result: params[7]
3024 * srv_check_health: params[8]
3025 * srv_check_state: params[9]
3026 * srv_agent_state: params[10]
3027 * bk_f_forced_id: params[11]
3028 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003029 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003030 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003031 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003032 */
3033
3034 /* validating srv_op_state */
3035 p = NULL;
3036 errno = 0;
3037 srv_op_state = strtol(params[1], &p, 10);
3038 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3039 (srv_op_state != SRV_ST_STOPPED &&
3040 srv_op_state != SRV_ST_STARTING &&
3041 srv_op_state != SRV_ST_RUNNING &&
3042 srv_op_state != SRV_ST_STOPPING)) {
3043 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3044 }
3045
3046 /* validating srv_admin_state */
3047 p = NULL;
3048 errno = 0;
3049 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003050 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003051
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003052 /* inherited statuses will be recomputed later.
3053 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3054 */
3055 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003056
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003057 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3058 (srv_admin_state != 0 &&
3059 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003060 srv_admin_state != SRV_ADMF_CMAINT &&
3061 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003062 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003063 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003064 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3065 }
3066
3067 /* validating srv_uweight */
3068 p = NULL;
3069 errno = 0;
3070 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003071 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003072 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3073
3074 /* validating srv_iweight */
3075 p = NULL;
3076 errno = 0;
3077 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003078 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003079 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3080
3081 /* validating srv_last_time_change */
3082 p = NULL;
3083 errno = 0;
3084 srv_last_time_change = strtol(params[5], &p, 10);
3085 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3086 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3087
3088 /* validating srv_check_status */
3089 p = NULL;
3090 errno = 0;
3091 srv_check_status = strtol(params[6], &p, 10);
3092 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3093 (srv_check_status >= HCHK_STATUS_SIZE))
3094 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3095
3096 /* validating srv_check_result */
3097 p = NULL;
3098 errno = 0;
3099 srv_check_result = strtol(params[7], &p, 10);
3100 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3101 (srv_check_result != CHK_RES_UNKNOWN &&
3102 srv_check_result != CHK_RES_NEUTRAL &&
3103 srv_check_result != CHK_RES_FAILED &&
3104 srv_check_result != CHK_RES_PASSED &&
3105 srv_check_result != CHK_RES_CONDPASS)) {
3106 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3107 }
3108
3109 /* validating srv_check_health */
3110 p = NULL;
3111 errno = 0;
3112 srv_check_health = strtol(params[8], &p, 10);
3113 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3114 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3115
3116 /* validating srv_check_state */
3117 p = NULL;
3118 errno = 0;
3119 srv_check_state = strtol(params[9], &p, 10);
3120 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3121 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3122 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3123
3124 /* validating srv_agent_state */
3125 p = NULL;
3126 errno = 0;
3127 srv_agent_state = strtol(params[10], &p, 10);
3128 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3129 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3130 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3131
3132 /* validating bk_f_forced_id */
3133 p = NULL;
3134 errno = 0;
3135 bk_f_forced_id = strtol(params[11], &p, 10);
3136 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3137 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3138
3139 /* validating srv_f_forced_id */
3140 p = NULL;
3141 errno = 0;
3142 srv_f_forced_id = strtol(params[12], &p, 10);
3143 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3144 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3145
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003146 /* validating srv_fqdn */
3147 fqdn = params[13];
3148 if (fqdn && *fqdn == '-')
3149 fqdn = NULL;
3150 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3151 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3152 fqdn = NULL;
3153 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003154
Frédéric Lécaille31694712017-08-01 08:47:19 +02003155 port_str = params[14];
3156 if (port_str) {
3157 port = strl2uic(port_str, strlen(port_str));
3158 if (port > USHRT_MAX) {
3159 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3160 port_str = NULL;
3161 }
3162 }
3163
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003164 /* SRV record
3165 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3166 */
3167 srvrecord = params[15];
3168 if (srvrecord && *srvrecord != '_')
3169 srvrecord = NULL;
3170
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003171 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003173 goto out;
3174
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003175 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003176 /* recover operational state and apply it to this server
3177 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003178 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003179 switch (srv_op_state) {
3180 case SRV_ST_STOPPED:
3181 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003182 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003183 break;
3184 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003185 /* If rise == 1 there is no STARTING state, let's switch to
3186 * RUNNING
3187 */
3188 if (srv->check.rise == 1) {
3189 srv->check.health = srv->check.rise + srv->check.fall - 1;
3190 srv_set_running(srv, "", NULL);
3191 break;
3192 }
3193 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3194 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003195 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003196 break;
3197 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003198 /* If fall == 1 there is no STOPPING state, let's switch to
3199 * STOPPED
3200 */
3201 if (srv->check.fall == 1) {
3202 srv->check.health = 0;
3203 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3204 break;
3205 }
3206 if (srv->check.health < srv->check.rise ||
3207 srv->check.health > srv->check.rise + srv->check.fall - 2)
3208 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003209 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003210 break;
3211 case SRV_ST_RUNNING:
3212 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003213 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003214 break;
3215 }
3216
3217 /* When applying server state, the following rules apply:
3218 * - in case of a configuration change, we apply the setting from the new
3219 * configuration, regardless of old running state
3220 * - if no configuration change, we apply old running state only if old running
3221 * state is different from new configuration state
3222 */
3223 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003224 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3225 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003226 srv_adm_set_maint(srv);
3227 else
3228 srv_adm_set_ready(srv);
3229 }
3230 /* configuration is the same, let's compate old running state and new conf state */
3231 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003232 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003233 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003234 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003235 srv_adm_set_ready(srv);
3236 }
3237 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003238 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003239 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003240 * (srv->iweight is the weight set up in configuration).
3241 * There are two possible reasons for FDRAIN to have been present :
3242 * - previous config weight was zero
3243 * - "set server b/s drain" was sent to the CLI
3244 *
3245 * In the first case, we simply want to drop this drain state
3246 * if the new weight is not zero anymore, meaning the administrator
3247 * has intentionally turned the weight back to a positive value to
3248 * enable the server again after an operation. In the second case,
3249 * the drain state was forced on the CLI regardless of the config's
3250 * weight so we don't want a change to the config weight to lose this
3251 * status. What this means is :
3252 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3253 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003254 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003255 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003256 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003257 }
3258
3259 srv->last_change = date.tv_sec - srv_last_time_change;
3260 srv->check.status = srv_check_status;
3261 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003262
3263 /* Only case we want to apply is removing ENABLED flag which could have been
3264 * done by the "disable health" command over the stats socket
3265 */
3266 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3267 (srv_check_state & CHK_ST_CONFIGURED) &&
3268 !(srv_check_state & CHK_ST_ENABLED))
3269 srv->check.state &= ~CHK_ST_ENABLED;
3270
3271 /* Only case we want to apply is removing ENABLED flag which could have been
3272 * done by the "disable agent" command over the stats socket
3273 */
3274 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3275 (srv_agent_state & CHK_ST_CONFIGURED) &&
3276 !(srv_agent_state & CHK_ST_ENABLED))
3277 srv->agent.state &= ~CHK_ST_ENABLED;
3278
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003279 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3280 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003281 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003282 * It means that a configuration file change has precedence over a unix socket change
3283 * for server's weight
3284 *
3285 * by default, HAProxy applies the following weight when parsing the configuration
3286 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003287 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003288 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003289 srv->uweight = srv_uweight;
3290 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003291 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003292
Willy Tarreaue5a60682016-11-09 14:54:53 +01003293 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003294 if (strcmp(params[0], "-"))
3295 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003296
3297 if (fqdn && srv->hostname) {
3298 if (!strcmp(srv->hostname, fqdn)) {
3299 /* Here we reset the 'set from stats socket FQDN' flag
3300 * to support such transitions:
3301 * Let's say initial FQDN value is foo1 (in configuration file).
3302 * - FQDN changed from stats socket, from foo1 to foo2 value,
3303 * - FQDN changed again from file configuration (with the same previous value
3304 set from stats socket, from foo1 to foo2 value),
3305 * - reload for any other reason than a FQDN modification,
3306 * the configuration file FQDN matches the fqdn server state file value.
3307 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003308 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003309 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003310 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003311 }
3312 else {
3313 /* If the FDQN has been changed from stats socket,
3314 * apply fqdn state file value (which is the value set
3315 * from stats socket).
3316 */
3317 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003318 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003319 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003320 }
3321 }
3322 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003323 /* If all the conditions below are validated, this means
3324 * we're evaluating a server managed by SRV resolution
3325 */
3326 else if (fqdn && !srv->hostname && srvrecord) {
3327 int res;
3328
3329 /* we can't apply previous state if SRV record has changed */
3330 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3331 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);
3332 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3333 goto out;
3334 }
3335
3336 /* create or find a SRV resolution for this srv record */
3337 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3338 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3339 if (srv->srvrq == NULL) {
3340 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3341 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3342 goto out;
3343 }
3344
3345 /* prepare DNS resolution for this server */
3346 res = srv_prepare_for_resolution(srv, fqdn);
3347 if (res == -1) {
3348 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3349 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3350 goto out;
3351 }
3352
3353 /* configure check.port accordingly */
3354 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3355 !(srv->flags & SRV_F_CHECKPORT))
3356 srv->check.port = port;
3357
3358 /* Unset SRV_F_MAPPORTS for SRV records.
3359 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3360 * because no ports are provided in the configuration file.
3361 * This is because HAProxy will use the port found into the SRV record.
3362 */
3363 srv->flags &= ~SRV_F_MAPPORTS;
3364 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003365
Frédéric Lécaille31694712017-08-01 08:47:19 +02003366 if (port_str)
3367 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003368 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003369
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003370 break;
3371 default:
3372 chunk_appendf(msg, ", version '%d' not supported", version);
3373 }
3374
3375 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003376 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003377 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003378 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003379 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003380 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003381}
3382
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003383
3384/*
3385 * read next line from file <f> and return the server state version if one found.
3386 * If no version is found, then 0 is returned
3387 * Note that this should be the first read on <f>
3388 */
3389static int srv_state_get_version(FILE *f) {
3390 char buf[2];
3391 int ret;
3392
3393 /* first character of first line of the file must contain the version of the export */
3394 if (fgets(buf, 2, f) == NULL) {
3395 return 0;
3396 }
3397
3398 ret = atoi(buf);
3399 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3400 (ret > SRV_STATE_FILE_VERSION_MAX))
3401 return 0;
3402
3403 return ret;
3404}
3405
3406
3407/*
3408 * parses server state line stored in <buf> and supposedly in version <version>.
3409 * Set <params> and <srv_params> accordingly.
3410 * In case of error, params[0] is set to NULL.
3411 */
3412static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3413{
3414 int buflen, arg, srv_arg;
3415 char *cur, *end;
3416
3417 buflen = strlen(buf);
3418 cur = buf;
3419 end = cur + buflen;
3420
3421 /* we need at least one character */
3422 if (buflen == 0) {
3423 params[0] = NULL;
3424 return;
3425 }
3426
3427 /* ignore blank characters at the beginning of the line */
3428 while (isspace(*cur))
3429 ++cur;
3430
3431 /* Ignore empty or commented lines */
3432 if (cur == end || *cur == '#') {
3433 params[0] = NULL;
3434 return;
3435 }
3436
3437 /* truncated lines */
3438 if (buf[buflen - 1] != '\n') {
3439 //ha_warning("server-state file '%s': truncated line\n", filepath);
3440 params[0] = NULL;
3441 return;
3442 }
3443
3444 /* Removes trailing '\n' */
3445 buf[buflen - 1] = '\0';
3446
3447 /* we're now ready to move the line into *srv_params[] */
3448 params[0] = cur;
3449 arg = 1;
3450 srv_arg = 0;
3451 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3452 if (isspace(*cur)) {
3453 *cur = '\0';
3454 ++cur;
3455 while (isspace(*cur))
3456 ++cur;
3457 switch (version) {
3458 case 1:
3459 /*
3460 * srv_addr: params[4] => srv_params[0]
3461 * srv_op_state: params[5] => srv_params[1]
3462 * srv_admin_state: params[6] => srv_params[2]
3463 * srv_uweight: params[7] => srv_params[3]
3464 * srv_iweight: params[8] => srv_params[4]
3465 * srv_last_time_change: params[9] => srv_params[5]
3466 * srv_check_status: params[10] => srv_params[6]
3467 * srv_check_result: params[11] => srv_params[7]
3468 * srv_check_health: params[12] => srv_params[8]
3469 * srv_check_state: params[13] => srv_params[9]
3470 * srv_agent_state: params[14] => srv_params[10]
3471 * bk_f_forced_id: params[15] => srv_params[11]
3472 * srv_f_forced_id: params[16] => srv_params[12]
3473 * srv_fqdn: params[17] => srv_params[13]
3474 * srv_port: params[18] => srv_params[14]
3475 * srvrecord: params[19] => srv_params[15]
3476 */
3477 if (arg >= 4) {
3478 srv_params[srv_arg] = cur;
3479 ++srv_arg;
3480 }
3481 break;
3482 }
3483
3484 params[arg] = cur;
3485 ++arg;
3486 }
3487 else {
3488 ++cur;
3489 }
3490 }
3491
3492 /* if line is incomplete line, then ignore it.
3493 * otherwise, update useful flags */
3494 switch (version) {
3495 case 1:
3496 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3497 params[0] = NULL;
3498 return;
3499 }
3500 break;
3501 }
3502
3503 return;
3504}
3505
3506
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003507/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3508 * For each proxy, it does the following:
3509 * - opens its server state file (either one or local one)
3510 * - read whole file, line by line
3511 * - analyse each line to check if it matches our current backend:
3512 * - backend name matches
3513 * - backend id matches if id is forced and name doesn't match
3514 * - if the server pointed by the line is found, then state is applied
3515 *
3516 * If the running backend uuid or id differs from the state file, then HAProxy reports
3517 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003518 *
3519 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003520 */
3521void apply_server_state(void)
3522{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003523 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003524 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3525 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003526 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003527 FILE *f;
3528 char *filepath;
3529 char globalfilepath[MAXPATHLEN + 1];
3530 char localfilepath[MAXPATHLEN + 1];
3531 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003532 struct proxy *curproxy, *bk;
3533 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003534 char *line;
3535 char *bkname, *srvname;
3536 struct state_line *st;
3537 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003538
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003539
3540 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003541 globalfilepathlen = 0;
3542 /* create the globalfilepath variable */
3543 if (global.server_state_file) {
3544 /* absolute path or no base directory provided */
3545 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3546 len = strlen(global.server_state_file);
3547 if (len > MAXPATHLEN) {
3548 globalfilepathlen = 0;
3549 goto globalfileerror;
3550 }
3551 memcpy(globalfilepath, global.server_state_file, len);
3552 globalfilepath[len] = '\0';
3553 globalfilepathlen = len;
3554 }
3555 else if (global.server_state_base) {
3556 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003557 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003558 globalfilepathlen = 0;
3559 goto globalfileerror;
3560 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003561 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003562 globalfilepath[len] = 0;
3563 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003564
3565 /* append a slash if needed */
3566 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3567 if (globalfilepathlen + 1 > MAXPATHLEN) {
3568 globalfilepathlen = 0;
3569 goto globalfileerror;
3570 }
3571 globalfilepath[globalfilepathlen++] = '/';
3572 }
3573
3574 len = strlen(global.server_state_file);
3575 if (globalfilepathlen + len > MAXPATHLEN) {
3576 globalfilepathlen = 0;
3577 goto globalfileerror;
3578 }
3579 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3580 globalfilepathlen += len;
3581 globalfilepath[globalfilepathlen++] = 0;
3582 }
3583 }
3584 globalfileerror:
3585 if (globalfilepathlen == 0)
3586 globalfilepath[0] = '\0';
3587
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003588 /* Load global server state in a tree */
3589 if (globalfilepathlen > 0) {
3590 errno = 0;
3591 f = fopen(globalfilepath, "r");
3592 if (errno)
3593 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
3594
3595 global_file_version = srv_state_get_version(f);
3596 if (global_file_version == 0)
3597 goto out_load_server_state_in_tree;
3598
3599 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3600 line = NULL;
3601
3602 line = strdup(mybuf);
3603 if (line == NULL)
3604 continue;
3605
3606 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3607 if (params[0] == NULL)
3608 continue;
3609
3610 /* bkname */
3611 bkname = params[1];
3612 /* srvname */
3613 srvname = params[3];
3614
3615 /* key */
3616 chunk_printf(&trash, "%s %s", bkname, srvname);
3617
3618 /* store line in tree */
3619 st = calloc(1, sizeof(*st) + trash.size);
3620 if (st == NULL) {
3621 goto nextline;
3622 }
3623 memcpy(st->name_name.key, trash.area, trash.size);
3624 ebst_insert(&state_file, &st->name_name);
3625
3626 /* save line */
3627 st->line = line;
3628
3629 continue;
3630
3631 nextline:
3632 /* free up memory in case of error during the processing of the line */
3633 free(line);
3634 }
3635 }
3636 out_load_server_state_in_tree:
3637
3638 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003639 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003640 /* servers are only in backends */
3641 if (!(curproxy->cap & PR_CAP_BE))
3642 continue;
3643 fileopenerr = 0;
3644 filepath = NULL;
3645
3646 /* search server state file path and name */
3647 switch (curproxy->load_server_state_from_file) {
3648 /* read servers state from global file */
3649 case PR_SRV_STATE_FILE_GLOBAL:
3650 /* there was an error while generating global server state file path */
3651 if (globalfilepathlen == 0)
3652 continue;
3653 filepath = globalfilepath;
3654 fileopenerr = 1;
3655 break;
3656 /* this backend has its own file */
3657 case PR_SRV_STATE_FILE_LOCAL:
3658 localfilepathlen = 0;
3659 localfilepath[0] = '\0';
3660 len = 0;
3661 /* create the localfilepath variable */
3662 /* absolute path or no base directory provided */
3663 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3664 len = strlen(curproxy->server_state_file_name);
3665 if (len > MAXPATHLEN) {
3666 localfilepathlen = 0;
3667 goto localfileerror;
3668 }
3669 memcpy(localfilepath, curproxy->server_state_file_name, len);
3670 localfilepath[len] = '\0';
3671 localfilepathlen = len;
3672 }
3673 else if (global.server_state_base) {
3674 len = strlen(global.server_state_base);
3675 localfilepathlen += len;
3676
3677 if (localfilepathlen > MAXPATHLEN) {
3678 localfilepathlen = 0;
3679 goto localfileerror;
3680 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003681 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003682 localfilepath[localfilepathlen] = 0;
3683
3684 /* append a slash if needed */
3685 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3686 if (localfilepathlen + 1 > MAXPATHLEN) {
3687 localfilepathlen = 0;
3688 goto localfileerror;
3689 }
3690 localfilepath[localfilepathlen++] = '/';
3691 }
3692
3693 len = strlen(curproxy->server_state_file_name);
3694 if (localfilepathlen + len > MAXPATHLEN) {
3695 localfilepathlen = 0;
3696 goto localfileerror;
3697 }
3698 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3699 localfilepathlen += len;
3700 localfilepath[localfilepathlen++] = 0;
3701 }
3702 filepath = localfilepath;
3703 localfileerror:
3704 if (localfilepathlen == 0)
3705 localfilepath[0] = '\0';
3706
3707 break;
3708 case PR_SRV_STATE_FILE_NONE:
3709 default:
3710 continue;
3711 }
3712
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003713 /* when global file is used, we get data from the tree
3714 * Note that in such case we don't check backend name neither uuid.
3715 * Backend name can't be wrong since it's used as a key to retrieve the server state
3716 * line from the tree.
3717 */
3718 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3719 struct server *srv = curproxy->srv;
3720 while (srv) {
3721 struct ebmb_node *node;
3722 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003723
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003724 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3725 node = ebst_lookup(&state_file, trash.area);
3726 if (!node)
3727 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003728
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003729 st = container_of(node, struct state_line, name_name);
3730 memcpy(mybuf, st->line, strlen(st->line));
3731 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003732
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003733 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3734 if (params[0] == NULL)
3735 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003736
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003737 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003738
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003739 next:
3740 srv = srv->next;
3741 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003742
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003743 continue; /* next proxy in list */
3744 }
3745 else {
3746 /* load 'local' state file */
3747 errno = 0;
3748 f = fopen(filepath, "r");
3749 if (errno && fileopenerr)
3750 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3751 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003752 continue;
3753
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003754 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003755
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003756 /* first character of first line of the file must contain the version of the export */
3757 version = srv_state_get_version(f);
3758 if (version == 0) {
3759 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3760 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003761 }
3762
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003763 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3764 int bk_f_forced_id = 0;
3765 int check_id = 0;
3766 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003767
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003768 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003769
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003770 if (params[0] == NULL) {
3771 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003772 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003773
3774 /* if line is incomplete line, then ignore it.
3775 * otherwise, update useful flags */
3776 switch (version) {
3777 case 1:
3778 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3779 check_id = (atoi(params[0]) == curproxy->uuid);
3780 check_name = (strcmp(curproxy->id, params[1]) == 0);
3781 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003782 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003783
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003784 bk = curproxy;
3785
3786 /* if backend can't be found, let's continue */
3787 if (!check_id && !check_name)
3788 continue;
3789 else if (!check_id && check_name) {
3790 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3791 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3792 }
3793 else if (check_id && !check_name) {
3794 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3795 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3796 /* if name doesn't match, we still want to update curproxy if the backend id
3797 * was forced in previous the previous configuration */
3798 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003799 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003800 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003801
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003802 /* look for the server by its name: param[3] */
3803 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003804
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003805 if (!srv) {
3806 /* if no server found, then warning and continue with next line */
3807 ha_warning("can't find server '%s' in backend '%s'\n",
3808 params[3], params[1]);
3809 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3810 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003811 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003812 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003813
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003814 /* now we can proceed with server's state update */
3815 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003816 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003817 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003818fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003819 fclose(f);
3820 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003821
3822 /* now free memory allocated for the tree */
3823 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3824 node;
3825 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3826 st = container_of(node, struct state_line, name_name);
3827 ebmb_delete(&st->name_name);
3828 free(st->line);
3829 free(st);
3830 }
3831
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003832}
3833
Simon Horman7d09b9a2013-02-12 10:45:51 +09003834/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003835 * update a server's current IP address.
3836 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3837 * ip is in network format.
3838 * updater is a string which contains an information about the requester of the update.
3839 * updater is used if not NULL.
3840 *
3841 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003842 *
3843 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003844 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003845int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003846{
3847 /* generates a log line and a warning on stderr */
3848 if (1) {
3849 /* book enough space for both IPv4 and IPv6 */
3850 char oldip[INET6_ADDRSTRLEN];
3851 char newip[INET6_ADDRSTRLEN];
3852
3853 memset(oldip, '\0', INET6_ADDRSTRLEN);
3854 memset(newip, '\0', INET6_ADDRSTRLEN);
3855
3856 /* copy old IP address in a string */
3857 switch (s->addr.ss_family) {
3858 case AF_INET:
3859 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3860 break;
3861 case AF_INET6:
3862 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3863 break;
3864 };
3865
3866 /* copy new IP address in a string */
3867 switch (ip_sin_family) {
3868 case AF_INET:
3869 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3870 break;
3871 case AF_INET6:
3872 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3873 break;
3874 };
3875
3876 /* save log line into a buffer */
3877 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3878 s->proxy->id, s->id, oldip, newip, updater);
3879
3880 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003881 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003882
3883 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003884 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003885 }
3886
3887 /* save the new IP family */
3888 s->addr.ss_family = ip_sin_family;
3889 /* save the new IP address */
3890 switch (ip_sin_family) {
3891 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003892 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003893 break;
3894 case AF_INET6:
3895 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3896 break;
3897 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003898 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003899
3900 return 0;
3901}
3902
3903/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003904 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3905 *
3906 * Caller can pass its name through <updater> to get it integrated in the response message
3907 * returned by the function.
3908 *
3909 * The function first does the following, in that order:
3910 * - validates the new addr and/or port
3911 * - checks if an update is required (new IP or port is different than current ones)
3912 * - checks the update is allowed:
3913 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3914 * - allow all changes if no CHECKS are configured
3915 * - if CHECK is configured:
3916 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3917 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3918 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003919 *
3920 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003921 */
3922const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3923{
3924 struct sockaddr_storage sa;
3925 int ret, port_change_required;
3926 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003927 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003928 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003929 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003930
3931 msg = get_trash_chunk();
3932 chunk_reset(msg);
3933
3934 if (addr) {
3935 memset(&sa, 0, sizeof(struct sockaddr_storage));
3936 if (str2ip2(addr, &sa, 0) == NULL) {
3937 chunk_printf(msg, "Invalid addr '%s'", addr);
3938 goto out;
3939 }
3940
3941 /* changes are allowed on AF_INET* families only */
3942 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3943 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3944 goto out;
3945 }
3946
3947 /* collecting data currently setup */
3948 memset(current_addr, '\0', sizeof(current_addr));
3949 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3950 /* changes are allowed on AF_INET* families only */
3951 if ((ret != AF_INET) && (ret != AF_INET6)) {
3952 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3953 goto out;
3954 }
3955
3956 /* applying ADDR changes if required and allowed
3957 * ipcmp returns 0 when both ADDR are the same
3958 */
3959 if (ipcmp(&s->addr, &sa) == 0) {
3960 chunk_appendf(msg, "no need to change the addr");
3961 goto port;
3962 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003963 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003964 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003965
3966 /* we also need to update check's ADDR only if it uses the server's one */
3967 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003968 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003969 }
3970
3971 /* we also need to update agent ADDR only if it use the server's one */
3972 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003973 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003974 }
3975
3976 /* update report for caller */
3977 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3978 }
3979
3980 port:
3981 if (port) {
3982 char sign = '\0';
3983 char *endptr;
3984
3985 if (addr)
3986 chunk_appendf(msg, ", ");
3987
3988 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003989 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003990
3991 /* check if PORT change is required */
3992 port_change_required = 0;
3993
3994 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003995 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003996 new_port = strtol(port, &endptr, 10);
3997 if ((errno != 0) || (port == endptr)) {
3998 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3999 goto out;
4000 }
4001
4002 /* check if caller triggers a port mapped or offset */
4003 if (sign == '-' || (sign == '+')) {
4004 /* check if server currently uses port map */
4005 if (!(s->flags & SRV_F_MAPPORTS)) {
4006 /* switch from fixed port to port map mandatorily triggers
4007 * a port change */
4008 port_change_required = 1;
4009 /* check is configured
4010 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
4011 * prevent PORT change if check doesn't have it's dedicated port while switching
4012 * to port mapping */
4013 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4014 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.");
4015 goto out;
4016 }
4017 }
4018 /* we're already using port maps */
4019 else {
4020 port_change_required = current_port != new_port;
4021 }
4022 }
4023 /* fixed port */
4024 else {
4025 port_change_required = current_port != new_port;
4026 }
4027
4028 /* applying PORT changes if required and update response message */
4029 if (port_change_required) {
4030 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01004031 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01004032 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004033
4034 /* prepare message */
4035 chunk_appendf(msg, "port changed from '");
4036 if (s->flags & SRV_F_MAPPORTS)
4037 chunk_appendf(msg, "+");
4038 chunk_appendf(msg, "%d' to '", current_port);
4039
4040 if (sign == '-') {
4041 s->flags |= SRV_F_MAPPORTS;
4042 chunk_appendf(msg, "%c", sign);
4043 /* just use for result output */
4044 new_port = -new_port;
4045 }
4046 else if (sign == '+') {
4047 s->flags |= SRV_F_MAPPORTS;
4048 chunk_appendf(msg, "%c", sign);
4049 }
4050 else {
4051 s->flags &= ~SRV_F_MAPPORTS;
4052 }
4053
4054 chunk_appendf(msg, "%d'", new_port);
4055
4056 /* we also need to update health checks port only if it uses server's realport */
4057 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4058 s->check.port = new_port;
4059 }
4060 }
4061 else {
4062 chunk_appendf(msg, "no need to change the port");
4063 }
4064 }
4065
4066out:
Olivier Houchard4e694042017-03-14 20:01:29 +01004067 if (changed)
4068 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004069 if (updater)
4070 chunk_appendf(msg, " by '%s'", updater);
4071 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004072 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004073}
4074
4075
4076/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004077 * update server status based on result of name resolution
4078 * returns:
4079 * 0 if server status is updated
4080 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02004081 *
4082 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004083 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004084int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004085{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004086 struct dns_resolvers *resolvers = s->resolvers;
4087 struct dns_resolution *resolution = s->dns_requester->resolution;
4088 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004089
4090 switch (resolution->status) {
4091 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004092 /* status when HAProxy has just (re)started.
4093 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004094 break;
4095
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004096 case RSLV_STATUS_VALID:
4097 /*
4098 * resume health checks
4099 * server will be turned back on if health check is safe
4100 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004101 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02004102 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004103 return 1;
4104 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
4105 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02004106 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004107 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02004108
Emeric Brun52a91d32017-08-31 14:41:55 +02004109 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004110 return 1;
4111 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
4112 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
4113 s->proxy->id, s->id);
4114
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004115 ha_warning("%s.\n", trash.area);
4116 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004117 return 0;
4118
4119 case RSLV_STATUS_NX:
4120 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004121 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
4122 if (!tick_is_expired(exp, now_ms))
4123 break;
4124
4125 if (s->next_admin & SRV_ADMF_RMAINT)
4126 return 1;
4127 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
4128 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004129
4130 case RSLV_STATUS_TIMEOUT:
4131 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004132 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
4133 if (!tick_is_expired(exp, now_ms))
4134 break;
4135
4136 if (s->next_admin & SRV_ADMF_RMAINT)
4137 return 1;
4138 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
4139 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004140
4141 case RSLV_STATUS_REFUSED:
4142 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004143 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
4144 if (!tick_is_expired(exp, now_ms))
4145 break;
4146
4147 if (s->next_admin & SRV_ADMF_RMAINT)
4148 return 1;
4149 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
4150 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004151
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004152 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004153 /* stop server if resolution failed for a long enough period */
4154 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4155 if (!tick_is_expired(exp, now_ms))
4156 break;
4157
4158 if (s->next_admin & SRV_ADMF_RMAINT)
4159 return 1;
4160 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4161 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004162 }
4163
4164 return 1;
4165}
4166
4167/*
4168 * Server Name Resolution valid response callback
4169 * It expects:
4170 * - <nameserver>: the name server which answered the valid response
4171 * - <response>: buffer containing a valid DNS response
4172 * - <response_len>: size of <response>
4173 * It performs the following actions:
4174 * - ignore response if current ip found and server family not met
4175 * - update with first new ip found if family is met and current IP is not found
4176 * returns:
4177 * 0 on error
4178 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004179 *
4180 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004181 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004182int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004183{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004184 struct server *s = NULL;
4185 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004186 void *serverip, *firstip;
4187 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004188 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004189 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004190 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004191
Christopher Faulet67957bd2017-09-27 11:00:59 +02004192 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004193 if (!s)
4194 return 1;
4195
Christopher Faulet67957bd2017-09-27 11:00:59 +02004196 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004197
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004198 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004199 firstip = NULL; /* pointer to the first valid response found */
4200 /* it will be used as the new IP if a change is required */
4201 firstip_sin_family = AF_UNSPEC;
4202 serverip = NULL; /* current server IP address */
4203
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004204 /* initializing server IP pointer */
4205 server_sin_family = s->addr.ss_family;
4206 switch (server_sin_family) {
4207 case AF_INET:
4208 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4209 break;
4210
4211 case AF_INET6:
4212 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4213 break;
4214
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004215 case AF_UNSPEC:
4216 break;
4217
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004218 default:
4219 goto invalid;
4220 }
4221
Baptiste Assmann729c9012017-05-22 15:13:10 +02004222 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004223 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004224 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004225
4226 switch (ret) {
4227 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004228 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004229
4230 case DNS_UPD_SRVIP_NOT_FOUND:
4231 goto save_ip;
4232
4233 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004234 goto invalid;
4235
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004236 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004237 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004238 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004239
Baptiste Assmannfad03182015-10-28 02:03:32 +01004240 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004241 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004242 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004243 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004244
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004245 default:
4246 goto invalid;
4247
4248 }
4249
4250 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004251 if (nameserver) {
4252 nameserver->counters.update++;
4253 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004254 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004255 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004256 else
4257 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004258 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004259
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004260 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004261 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004262 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004263
4264 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004265 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004266 nameserver->counters.invalid++;
4267 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004268 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004269 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004270 return 0;
4271}
4272
4273/*
4274 * Server Name Resolution error management callback
4275 * returns:
4276 * 0 on error
4277 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004278 *
4279 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004280 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004281int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004282{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004283 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004284
Christopher Faulet67957bd2017-09-27 11:00:59 +02004285 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004286 if (!s)
4287 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004288 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004289 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004290 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004291 return 1;
4292}
4293
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004294/*
4295 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004296 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004297 * It returns a pointer to the first server found or NULL if <ip> is not yet
4298 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004299 *
4300 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004301 */
4302struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4303{
4304 struct server *tmpsrv;
4305 struct proxy *be;
4306
4307 if (!srv)
4308 return NULL;
4309
4310 be = srv->proxy;
4311 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004312 /* we found the current server is the same, ignore it */
4313 if (srv == tmpsrv)
4314 continue;
4315
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004316 /* We want to compare the IP in the record with the IP of the servers in the
4317 * same backend, only if:
4318 * * DNS resolution is enabled on the server
4319 * * the hostname used for the resolution by our server is the same than the
4320 * one used for the server found in the backend
4321 * * the server found in the backend is not our current server
4322 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004323 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004324 if ((tmpsrv->hostname_dn == NULL) ||
4325 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4326 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004327 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004328 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004329 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004330 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004331
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004332 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004333 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004334 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004335 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004336 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004337
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004338 /* At this point, we have 2 different servers using the same DNS hostname
4339 * for their respective resolution.
4340 */
4341 if (*ip_family == tmpsrv->addr.ss_family &&
4342 ((tmpsrv->addr.ss_family == AF_INET &&
4343 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4344 (tmpsrv->addr.ss_family == AF_INET6 &&
4345 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004346 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004347 return tmpsrv;
4348 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004349 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004350 }
4351
Emeric Brune9fd6b52017-11-02 17:20:39 +01004352
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004353 return NULL;
4354}
4355
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004356/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4357 * resolver. This is suited for initial address configuration. Returns 0 on
4358 * success otherwise a non-zero error code. In case of error, *err_code, if
4359 * not NULL, is filled up.
4360 */
4361int srv_set_addr_via_libc(struct server *srv, int *err_code)
4362{
4363 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004364 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004365 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004366 return 1;
4367 }
4368 return 0;
4369}
4370
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004371/* Set the server's FDQN (->hostname) from <hostname>.
4372 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004373 *
4374 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004375 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004376int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004377{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004378 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004379 char *hostname_dn;
4380 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004381
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004382 /* Note that the server lock is already held. */
4383 if (!srv->resolvers)
4384 return -1;
4385
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004386 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004387 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004388 /* run time DNS resolution was not active for this server
4389 * and we can't enable it at run time for now.
4390 */
4391 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004392 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004393
4394 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004395 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004396 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004397 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4398 hostname_dn, trash.size);
4399 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004400 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004401
Christopher Faulet67957bd2017-09-27 11:00:59 +02004402 resolution = srv->dns_requester->resolution;
4403 if (resolution &&
4404 resolution->hostname_dn &&
4405 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004406 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004407
Christopher Faulet67957bd2017-09-27 11:00:59 +02004408 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004409
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004410 free(srv->hostname);
4411 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004412 srv->hostname = strdup(hostname);
4413 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004414 srv->hostname_dn_len = hostname_dn_len;
4415 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004416 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004417
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004418 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004419 goto err;
4420
4421 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004422 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004423 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004424 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004425
4426 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004427 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004428 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004429 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004430}
4431
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004432/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4433 * from the state file. This is suited for initial address configuration.
4434 * Returns 0 on success otherwise a non-zero error code. In case of error,
4435 * *err_code, if not NULL, is filled up.
4436 */
4437static int srv_apply_lastaddr(struct server *srv, int *err_code)
4438{
4439 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4440 if (err_code)
4441 *err_code |= ERR_WARN;
4442 return 1;
4443 }
4444 return 0;
4445}
4446
Willy Tarreau25e51522016-11-04 15:10:17 +01004447/* returns 0 if no error, otherwise a combination of ERR_* flags */
4448static int srv_iterate_initaddr(struct server *srv)
4449{
4450 int return_code = 0;
4451 int err_code;
4452 unsigned int methods;
4453
4454 methods = srv->init_addr_methods;
4455 if (!methods) { // default to "last,libc"
4456 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4457 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4458 }
4459
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004460 /* "-dr" : always append "none" so that server addresses resolution
4461 * failures are silently ignored, this is convenient to validate some
4462 * configs out of their environment.
4463 */
4464 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4465 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4466
Willy Tarreau25e51522016-11-04 15:10:17 +01004467 while (methods) {
4468 err_code = 0;
4469 switch (srv_get_next_initaddr(&methods)) {
4470 case SRV_IADDR_LAST:
4471 if (!srv->lastaddr)
4472 continue;
4473 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004474 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004475 return_code |= err_code;
4476 break;
4477
4478 case SRV_IADDR_LIBC:
4479 if (!srv->hostname)
4480 continue;
4481 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004482 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004483 return_code |= err_code;
4484 break;
4485
Willy Tarreau37ebe122016-11-04 15:17:58 +01004486 case SRV_IADDR_NONE:
4487 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004488 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004489 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4490 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004491 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004492 return return_code;
4493
Willy Tarreau4310d362016-11-02 15:05:56 +01004494 case SRV_IADDR_IP:
4495 ipcpy(&srv->init_addr, &srv->addr);
4496 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004497 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4498 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004499 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004500 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004501
Willy Tarreau25e51522016-11-04 15:10:17 +01004502 default: /* unhandled method */
4503 break;
4504 }
4505 }
4506
4507 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004508 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004509 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4510 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004511 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004512 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004513 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4514 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004515
4516 return_code |= ERR_ALERT | ERR_FATAL;
4517 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004518out:
4519 srv_set_dyncookie(srv);
4520 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004521}
4522
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004523/*
4524 * This function parses all backends and all servers within each backend
4525 * and performs servers' addr resolution based on information provided by:
4526 * - configuration file
4527 * - server-state file (states provided by an 'old' haproxy process)
4528 *
4529 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4530 */
4531int srv_init_addr(void)
4532{
4533 struct proxy *curproxy;
4534 int return_code = 0;
4535
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004536 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004537 while (curproxy) {
4538 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004539
4540 /* servers are in backend only */
4541 if (!(curproxy->cap & PR_CAP_BE))
4542 goto srv_init_addr_next;
4543
Willy Tarreau25e51522016-11-04 15:10:17 +01004544 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004545 if (srv->hostname)
4546 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004547
4548 srv_init_addr_next:
4549 curproxy = curproxy->next;
4550 }
4551
4552 return return_code;
4553}
4554
Willy Tarreau46b7f532018-08-21 11:54:26 +02004555/*
4556 * Must be called with the server lock held.
4557 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004558const 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 +02004559{
4560
Willy Tarreau83061a82018-07-13 11:56:34 +02004561 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004562
4563 msg = get_trash_chunk();
4564 chunk_reset(msg);
4565
Olivier Houchard8da5f982017-08-04 18:35:36 +02004566 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004567 chunk_appendf(msg, "no need to change the FDQN");
4568 goto out;
4569 }
4570
4571 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4572 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4573 goto out;
4574 }
4575
4576 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4577 server->proxy->id, server->id, server->hostname, fqdn);
4578
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004579 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004580 chunk_reset(msg);
4581 chunk_appendf(msg, "could not update %s/%s FQDN",
4582 server->proxy->id, server->id);
4583 goto out;
4584 }
4585
4586 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004587 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004588
4589 out:
4590 if (updater)
4591 chunk_appendf(msg, " by '%s'", updater);
4592 chunk_appendf(msg, "\n");
4593
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004594 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004595}
4596
4597
Willy Tarreau21b069d2016-11-23 17:15:08 +01004598/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4599 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004600 * 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 +01004601 * used for CLI commands requiring a server name.
4602 * Important: the <arg> is modified to remove the '/'.
4603 */
4604struct server *cli_find_server(struct appctx *appctx, char *arg)
4605{
4606 struct proxy *px;
4607 struct server *sv;
4608 char *line;
4609
4610 /* split "backend/server" and make <line> point to server */
4611 for (line = arg; *line; line++)
4612 if (*line == '/') {
4613 *line++ = '\0';
4614 break;
4615 }
4616
4617 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004618 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004619 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004620 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004621 return NULL;
4622 }
4623
4624 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004625 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004626 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004627 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004628 return NULL;
4629 }
4630
4631 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004632 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004633 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004634 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004635 return NULL;
4636 }
4637
4638 return sv;
4639}
4640
William Lallemand222baf22016-11-19 02:00:33 +01004641
Willy Tarreau46b7f532018-08-21 11:54:26 +02004642/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004643static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004644{
4645 struct server *sv;
4646 const char *warning;
4647
4648 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4649 return 1;
4650
4651 sv = cli_find_server(appctx, args[2]);
4652 if (!sv)
4653 return 1;
4654
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004655 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004656
William Lallemand222baf22016-11-19 02:00:33 +01004657 if (strcmp(args[3], "weight") == 0) {
4658 warning = server_parse_weight_change_request(sv, args[4]);
4659 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004660 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004661 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004662 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004663 }
4664 }
4665 else if (strcmp(args[3], "state") == 0) {
4666 if (strcmp(args[4], "ready") == 0)
4667 srv_adm_set_ready(sv);
4668 else if (strcmp(args[4], "drain") == 0)
4669 srv_adm_set_drain(sv);
4670 else if (strcmp(args[4], "maint") == 0)
4671 srv_adm_set_maint(sv);
4672 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004673 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004674 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004675 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004676 }
4677 }
4678 else if (strcmp(args[3], "health") == 0) {
4679 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004680 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004681 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004682 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004683 }
4684 else if (strcmp(args[4], "up") == 0) {
4685 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004686 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004687 }
4688 else if (strcmp(args[4], "stopping") == 0) {
4689 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004690 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004691 }
4692 else if (strcmp(args[4], "down") == 0) {
4693 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004694 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004695 }
4696 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004697 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004698 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004699 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004700 }
4701 }
4702 else if (strcmp(args[3], "agent") == 0) {
4703 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004704 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004705 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004706 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004707 }
4708 else if (strcmp(args[4], "up") == 0) {
4709 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004710 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004711 }
4712 else if (strcmp(args[4], "down") == 0) {
4713 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004714 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004715 }
4716 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004717 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004718 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004719 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004720 }
4721 }
Misiek2da082d2017-01-09 09:40:42 +01004722 else if (strcmp(args[3], "agent-addr") == 0) {
4723 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004724 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004725 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4726 appctx->st0 = CLI_ST_PRINT;
4727 } else {
4728 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004729 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004730 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4731 appctx->st0 = CLI_ST_PRINT;
4732 }
4733 }
4734 }
4735 else if (strcmp(args[3], "agent-send") == 0) {
4736 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004737 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004738 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4739 appctx->st0 = CLI_ST_PRINT;
4740 } else {
4741 char *nss = strdup(args[4]);
4742 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004743 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004744 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4745 appctx->st0 = CLI_ST_PRINT;
4746 } else {
4747 free(sv->agent.send_string);
4748 sv->agent.send_string = nss;
4749 sv->agent.send_string_len = strlen(args[4]);
4750 }
4751 }
4752 }
William Lallemand222baf22016-11-19 02:00:33 +01004753 else if (strcmp(args[3], "check-port") == 0) {
4754 int i = 0;
4755 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004756 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004757 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004758 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004759 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004760 }
4761 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004762 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004763 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004764 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004765 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004766 }
4767 /* prevent the update of port to 0 if MAPPORTS are in use */
4768 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004769 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004770 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004771 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004772 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004773 }
4774 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004775 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004776 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004777 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004778 }
4779 else if (strcmp(args[3], "addr") == 0) {
4780 char *addr = NULL;
4781 char *port = NULL;
4782 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004783 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004784 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004785 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004786 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004787 }
4788 else {
4789 addr = args[4];
4790 }
4791 if (strcmp(args[5], "port") == 0) {
4792 port = args[6];
4793 }
4794 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4795 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004796 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004797 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004798 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004799 }
4800 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4801 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004802 else if (strcmp(args[3], "fqdn") == 0) {
4803 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004804 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004805 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4806 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004807 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004808 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004809 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004810 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004811 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004812 appctx->ctx.cli.msg = warning;
4813 appctx->st0 = CLI_ST_PRINT;
4814 }
4815 }
William Lallemand222baf22016-11-19 02:00:33 +01004816 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004817 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004818 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 +01004819 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004820 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004821 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004822 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004823 return 1;
4824}
4825
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004826static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004827{
4828 struct stream_interface *si = appctx->owner;
4829 struct proxy *px;
4830 struct server *sv;
4831 char *line;
4832
4833
4834 /* split "backend/server" and make <line> point to server */
4835 for (line = args[2]; *line; line++)
4836 if (*line == '/') {
4837 *line++ = '\0';
4838 break;
4839 }
4840
4841 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004842 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004843 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004844 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004845 return 1;
4846 }
4847
4848 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004849 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004850 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004851 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004852 return 1;
4853 }
4854
4855 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004856 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4857 sv->iweight);
4858 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004859 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004860 return 0;
4861 }
William Lallemand6b160942016-11-22 12:34:35 +01004862 return 1;
4863}
4864
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004865/* Parse a "set weight" command.
4866 *
4867 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004868 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004869static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004870{
4871 struct server *sv;
4872 const char *warning;
4873
4874 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4875 return 1;
4876
4877 sv = cli_find_server(appctx, args[2]);
4878 if (!sv)
4879 return 1;
4880
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004881 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4882
William Lallemand6b160942016-11-22 12:34:35 +01004883 warning = server_parse_weight_change_request(sv, args[3]);
4884 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004885 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004886 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004887 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004888 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004889
4890 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4891
William Lallemand6b160942016-11-22 12:34:35 +01004892 return 1;
4893}
4894
Willy Tarreau46b7f532018-08-21 11:54:26 +02004895/* parse a "set maxconn server" command. It always returns 1.
4896 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004897 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004898 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004899static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004900{
4901 struct server *sv;
4902 const char *warning;
4903
4904 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4905 return 1;
4906
4907 sv = cli_find_server(appctx, args[3]);
4908 if (!sv)
4909 return 1;
4910
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004911 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4912
Willy Tarreaub8026272016-11-23 11:26:56 +01004913 warning = server_parse_maxconn_change_request(sv, args[4]);
4914 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004915 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004916 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004917 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004918 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004919
4920 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4921
Willy Tarreaub8026272016-11-23 11:26:56 +01004922 return 1;
4923}
William Lallemand6b160942016-11-22 12:34:35 +01004924
Willy Tarreau46b7f532018-08-21 11:54:26 +02004925/* parse a "disable agent" command. It always returns 1.
4926 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004927 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004928 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004929static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004930{
4931 struct server *sv;
4932
4933 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4934 return 1;
4935
4936 sv = cli_find_server(appctx, args[2]);
4937 if (!sv)
4938 return 1;
4939
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004940 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004941 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004942 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004943 return 1;
4944}
4945
Willy Tarreau46b7f532018-08-21 11:54:26 +02004946/* parse a "disable health" command. It always returns 1.
4947 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004948 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004949 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004950static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004951{
4952 struct server *sv;
4953
4954 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4955 return 1;
4956
4957 sv = cli_find_server(appctx, args[2]);
4958 if (!sv)
4959 return 1;
4960
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004961 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004962 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004963 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004964 return 1;
4965}
4966
Willy Tarreau46b7f532018-08-21 11:54:26 +02004967/* parse a "disable server" command. It always returns 1.
4968 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004969 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004970 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004971static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004972{
4973 struct server *sv;
4974
4975 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4976 return 1;
4977
4978 sv = cli_find_server(appctx, args[2]);
4979 if (!sv)
4980 return 1;
4981
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004982 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004983 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004984 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004985 return 1;
4986}
4987
Willy Tarreau46b7f532018-08-21 11:54:26 +02004988/* parse a "enable agent" command. It always returns 1.
4989 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004990 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004991 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004992static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004993{
4994 struct server *sv;
4995
4996 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4997 return 1;
4998
4999 sv = cli_find_server(appctx, args[2]);
5000 if (!sv)
5001 return 1;
5002
5003 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02005004 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005005 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01005006 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005007 return 1;
5008 }
5009
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005010 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005011 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005012 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005013 return 1;
5014}
5015
Willy Tarreau46b7f532018-08-21 11:54:26 +02005016/* parse a "enable health" command. It always returns 1.
5017 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005018 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02005019 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005020static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005021{
5022 struct server *sv;
5023
5024 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5025 return 1;
5026
5027 sv = cli_find_server(appctx, args[2]);
5028 if (!sv)
5029 return 1;
5030
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005031 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005032 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005033 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005034 return 1;
5035}
5036
Willy Tarreau46b7f532018-08-21 11:54:26 +02005037/* parse a "enable server" command. It always returns 1.
5038 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005039 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02005040 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005041static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01005042{
5043 struct server *sv;
5044
5045 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5046 return 1;
5047
5048 sv = cli_find_server(appctx, args[2]);
5049 if (!sv)
5050 return 1;
5051
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005052 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005053 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01005054 if (!(sv->flags & SRV_F_COOKIESET)
5055 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
5056 sv->cookie)
5057 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005058 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005059 return 1;
5060}
5061
William Lallemand222baf22016-11-19 02:00:33 +01005062/* register cli keywords */
5063static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005064 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005065 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005066 { { "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 +01005067 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005068 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005069 { { "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 +01005070 { { "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 +01005071 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01005072 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
5073 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
5074
William Lallemand222baf22016-11-19 02:00:33 +01005075 {{},}
5076}};
5077
Willy Tarreau0108d902018-11-25 19:14:37 +01005078INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005079
Emeric Brun64cc49c2017-10-03 14:46:45 +02005080/*
5081 * This function applies server's status changes, it is
5082 * is designed to be called asynchronously.
5083 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02005084 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005085 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005086static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005087{
5088 struct check *check = &s->check;
5089 int xferred;
5090 struct proxy *px = s->proxy;
5091 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5092 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5093 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005094 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005095
Emeric Brun64cc49c2017-10-03 14:46:45 +02005096 /* If currently main is not set we try to apply pending state changes */
5097 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5098 int next_admin;
5099
5100 /* Backup next admin */
5101 next_admin = s->next_admin;
5102
5103 /* restore current admin state */
5104 s->next_admin = s->cur_admin;
5105
5106 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5107 s->last_change = now.tv_sec;
5108 if (s->proxy->lbprm.set_server_status_down)
5109 s->proxy->lbprm.set_server_status_down(s);
5110
5111 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5112 srv_shutdown_streams(s, SF_ERR_DOWN);
5113
5114 /* we might have streams queued on this server and waiting for
5115 * a connection. Those which are redispatchable will be queued
5116 * to another server or to the proxy itself.
5117 */
5118 xferred = pendconn_redistribute(s);
5119
5120 tmptrash = alloc_trash_chunk();
5121 if (tmptrash) {
5122 chunk_printf(tmptrash,
5123 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5124 s->proxy->id, s->id);
5125
Emeric Brun5a133512017-10-19 14:42:30 +02005126 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005127 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005128
5129 /* we don't send an alert if the server was previously paused */
5130 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005131 send_log(s->proxy, log_level, "%s.\n",
5132 tmptrash->area);
5133 send_email_alert(s, log_level, "%s",
5134 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005135 free_trash_chunk(tmptrash);
5136 tmptrash = NULL;
5137 }
5138 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5139 set_backend_down(s->proxy);
5140
5141 s->counters.down_trans++;
5142 }
5143 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5144 s->last_change = now.tv_sec;
5145 if (s->proxy->lbprm.set_server_status_down)
5146 s->proxy->lbprm.set_server_status_down(s);
5147
5148 /* we might have streams queued on this server and waiting for
5149 * a connection. Those which are redispatchable will be queued
5150 * to another server or to the proxy itself.
5151 */
5152 xferred = pendconn_redistribute(s);
5153
5154 tmptrash = alloc_trash_chunk();
5155 if (tmptrash) {
5156 chunk_printf(tmptrash,
5157 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5158 s->proxy->id, s->id);
5159
Emeric Brun5a133512017-10-19 14:42:30 +02005160 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005161
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005162 ha_warning("%s.\n", tmptrash->area);
5163 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5164 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005165 free_trash_chunk(tmptrash);
5166 tmptrash = NULL;
5167 }
5168
5169 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5170 set_backend_down(s->proxy);
5171 }
5172 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5173 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5174 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5175 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5176 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5177 s->proxy->last_change = now.tv_sec;
5178 }
5179
5180 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5181 s->down_time += now.tv_sec - s->last_change;
5182
5183 s->last_change = now.tv_sec;
5184 if (s->next_state == SRV_ST_STARTING)
5185 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5186
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005187 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005188 /* now propagate the status change to any LB algorithms */
5189 if (px->lbprm.update_server_eweight)
5190 px->lbprm.update_server_eweight(s);
5191 else if (srv_willbe_usable(s)) {
5192 if (px->lbprm.set_server_status_up)
5193 px->lbprm.set_server_status_up(s);
5194 }
5195 else {
5196 if (px->lbprm.set_server_status_down)
5197 px->lbprm.set_server_status_down(s);
5198 }
5199
5200 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5201 * and it's not a backup server and its effective weight is > 0,
5202 * then it can accept new connections, so we shut down all streams
5203 * on all backup servers.
5204 */
5205 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5206 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5207 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5208
5209 /* check if we can handle some connections queued at the proxy. We
5210 * will take as many as we can handle.
5211 */
5212 xferred = pendconn_grab_from_px(s);
5213
5214 tmptrash = alloc_trash_chunk();
5215 if (tmptrash) {
5216 chunk_printf(tmptrash,
5217 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5218 s->proxy->id, s->id);
5219
Emeric Brun5a133512017-10-19 14:42:30 +02005220 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005221 ha_warning("%s.\n", tmptrash->area);
5222 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5223 tmptrash->area);
5224 send_email_alert(s, LOG_NOTICE, "%s",
5225 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005226 free_trash_chunk(tmptrash);
5227 tmptrash = NULL;
5228 }
5229
5230 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5231 set_backend_down(s->proxy);
5232 }
5233 else if (s->cur_eweight != s->next_eweight) {
5234 /* now propagate the status change to any LB algorithms */
5235 if (px->lbprm.update_server_eweight)
5236 px->lbprm.update_server_eweight(s);
5237 else if (srv_willbe_usable(s)) {
5238 if (px->lbprm.set_server_status_up)
5239 px->lbprm.set_server_status_up(s);
5240 }
5241 else {
5242 if (px->lbprm.set_server_status_down)
5243 px->lbprm.set_server_status_down(s);
5244 }
5245
5246 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5247 set_backend_down(s->proxy);
5248 }
5249
5250 s->next_admin = next_admin;
5251 }
5252
Emeric Brun5a133512017-10-19 14:42:30 +02005253 /* reset operational state change */
5254 *s->op_st_chg.reason = 0;
5255 s->op_st_chg.status = s->op_st_chg.code = -1;
5256 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005257
5258 /* Now we try to apply pending admin changes */
5259
5260 /* Maintenance must also disable health checks */
5261 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5262 if (s->check.state & CHK_ST_ENABLED) {
5263 s->check.state |= CHK_ST_PAUSED;
5264 check->health = 0;
5265 }
5266
5267 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005268 tmptrash = alloc_trash_chunk();
5269 if (tmptrash) {
5270 chunk_printf(tmptrash,
5271 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5272 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5273 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005274
Olivier Houchard796a2b32017-10-24 17:42:47 +02005275 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005276
Olivier Houchard796a2b32017-10-24 17:42:47 +02005277 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005278 ha_warning("%s.\n", tmptrash->area);
5279 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5280 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005281 }
5282 free_trash_chunk(tmptrash);
5283 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005284 }
Emeric Brun8f298292017-12-06 16:47:17 +01005285 /* commit new admin status */
5286
5287 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005288 }
5289 else { /* server was still running */
5290 check->health = 0; /* failure */
5291 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005292
5293 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005294 if (s->proxy->lbprm.set_server_status_down)
5295 s->proxy->lbprm.set_server_status_down(s);
5296
Emeric Brun64cc49c2017-10-03 14:46:45 +02005297 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5298 srv_shutdown_streams(s, SF_ERR_DOWN);
5299
5300 /* we might have streams queued on this server and waiting for
5301 * a connection. Those which are redispatchable will be queued
5302 * to another server or to the proxy itself.
5303 */
5304 xferred = pendconn_redistribute(s);
5305
5306 tmptrash = alloc_trash_chunk();
5307 if (tmptrash) {
5308 chunk_printf(tmptrash,
5309 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5310 s->flags & SRV_F_BACKUP ? "Backup " : "",
5311 s->proxy->id, s->id,
5312 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5313
5314 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5315
5316 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005317 ha_warning("%s.\n", tmptrash->area);
5318 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5319 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005320 }
5321 free_trash_chunk(tmptrash);
5322 tmptrash = NULL;
5323 }
5324 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5325 set_backend_down(s->proxy);
5326
5327 s->counters.down_trans++;
5328 }
5329 }
5330 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5331 /* OK here we're leaving maintenance, we have many things to check,
5332 * because the server might possibly be coming back up depending on
5333 * its state. In practice, leaving maintenance means that we should
5334 * immediately turn to UP (more or less the slowstart) under the
5335 * following conditions :
5336 * - server is neither checked nor tracked
5337 * - server tracks another server which is not checked
5338 * - server tracks another server which is already up
5339 * Which sums up as something simpler :
5340 * "either the tracking server is up or the server's checks are disabled
5341 * or up". Otherwise we only re-enable health checks. There's a special
5342 * case associated to the stopping state which can be inherited. Note
5343 * that the server might still be in drain mode, which is naturally dealt
5344 * with by the lower level functions.
5345 */
5346
5347 if (s->check.state & CHK_ST_ENABLED) {
5348 s->check.state &= ~CHK_ST_PAUSED;
5349 check->health = check->rise; /* start OK but check immediately */
5350 }
5351
5352 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5353 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5354 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5355 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5356 s->next_state = SRV_ST_STOPPING;
5357 }
5358 else {
5359 s->next_state = SRV_ST_STARTING;
5360 if (s->slowstart > 0)
5361 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5362 else
5363 s->next_state = SRV_ST_RUNNING;
5364 }
5365
5366 }
5367
5368 tmptrash = alloc_trash_chunk();
5369 if (tmptrash) {
5370 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5371 chunk_printf(tmptrash,
5372 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5373 s->flags & SRV_F_BACKUP ? "Backup " : "",
5374 s->proxy->id, s->id,
5375 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5376 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5377 }
5378 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5379 chunk_printf(tmptrash,
5380 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5381 s->flags & SRV_F_BACKUP ? "Backup " : "",
5382 s->proxy->id, s->id, s->hostname,
5383 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5384 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5385 }
5386 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5387 chunk_printf(tmptrash,
5388 "%sServer %s/%s is %s/%s (leaving maintenance)",
5389 s->flags & SRV_F_BACKUP ? "Backup " : "",
5390 s->proxy->id, s->id,
5391 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5392 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5393 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005394 ha_warning("%s.\n", tmptrash->area);
5395 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5396 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005397 free_trash_chunk(tmptrash);
5398 tmptrash = NULL;
5399 }
5400
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005401 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005402 /* now propagate the status change to any LB algorithms */
5403 if (px->lbprm.update_server_eweight)
5404 px->lbprm.update_server_eweight(s);
5405 else if (srv_willbe_usable(s)) {
5406 if (px->lbprm.set_server_status_up)
5407 px->lbprm.set_server_status_up(s);
5408 }
5409 else {
5410 if (px->lbprm.set_server_status_down)
5411 px->lbprm.set_server_status_down(s);
5412 }
5413
5414 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5415 set_backend_down(s->proxy);
5416
Willy Tarreau6a78e612018-08-07 10:14:53 +02005417 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5418 * and it's not a backup server and its effective weight is > 0,
5419 * then it can accept new connections, so we shut down all streams
5420 * on all backup servers.
5421 */
5422 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5423 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5424 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5425
5426 /* check if we can handle some connections queued at the proxy. We
5427 * will take as many as we can handle.
5428 */
5429 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005430 }
5431 else if (s->next_admin & SRV_ADMF_MAINT) {
5432 /* remaining in maintenance mode, let's inform precisely about the
5433 * situation.
5434 */
5435 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5436 tmptrash = alloc_trash_chunk();
5437 if (tmptrash) {
5438 chunk_printf(tmptrash,
5439 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5440 s->flags & SRV_F_BACKUP ? "Backup " : "",
5441 s->proxy->id, s->id);
5442
5443 if (s->track) /* normally it's mandatory here */
5444 chunk_appendf(tmptrash, " via %s/%s",
5445 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005446 ha_warning("%s.\n", tmptrash->area);
5447 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5448 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005449 free_trash_chunk(tmptrash);
5450 tmptrash = NULL;
5451 }
5452 }
5453 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5454 tmptrash = alloc_trash_chunk();
5455 if (tmptrash) {
5456 chunk_printf(tmptrash,
5457 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5458 s->flags & SRV_F_BACKUP ? "Backup " : "",
5459 s->proxy->id, s->id, s->hostname);
5460
5461 if (s->track) /* normally it's mandatory here */
5462 chunk_appendf(tmptrash, " via %s/%s",
5463 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005464 ha_warning("%s.\n", tmptrash->area);
5465 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5466 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005467 free_trash_chunk(tmptrash);
5468 tmptrash = NULL;
5469 }
5470 }
5471 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5472 tmptrash = alloc_trash_chunk();
5473 if (tmptrash) {
5474 chunk_printf(tmptrash,
5475 "%sServer %s/%s remains in forced maintenance",
5476 s->flags & SRV_F_BACKUP ? "Backup " : "",
5477 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005478 ha_warning("%s.\n", tmptrash->area);
5479 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5480 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005481 free_trash_chunk(tmptrash);
5482 tmptrash = NULL;
5483 }
5484 }
5485 /* don't report anything when leaving drain mode and remaining in maintenance */
5486
5487 s->cur_admin = s->next_admin;
5488 }
5489
5490 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5491 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5492 /* drain state is applied only if not yet in maint */
5493
5494 s->last_change = now.tv_sec;
5495 if (px->lbprm.set_server_status_down)
5496 px->lbprm.set_server_status_down(s);
5497
5498 /* we might have streams queued on this server and waiting for
5499 * a connection. Those which are redispatchable will be queued
5500 * to another server or to the proxy itself.
5501 */
5502 xferred = pendconn_redistribute(s);
5503
5504 tmptrash = alloc_trash_chunk();
5505 if (tmptrash) {
5506 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5507 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5508 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5509
5510 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5511
5512 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005513 ha_warning("%s.\n", tmptrash->area);
5514 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5515 tmptrash->area);
5516 send_email_alert(s, LOG_NOTICE, "%s",
5517 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005518 }
5519 free_trash_chunk(tmptrash);
5520 tmptrash = NULL;
5521 }
5522
5523 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5524 set_backend_down(s->proxy);
5525 }
5526 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5527 /* OK completely leaving drain mode */
5528 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5529 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5530 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5531 s->proxy->last_change = now.tv_sec;
5532 }
5533
5534 if (s->last_change < now.tv_sec) // ignore negative times
5535 s->down_time += now.tv_sec - s->last_change;
5536 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005537 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005538
5539 tmptrash = alloc_trash_chunk();
5540 if (tmptrash) {
5541 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5542 chunk_printf(tmptrash,
5543 "%sServer %s/%s is %s (leaving forced drain)",
5544 s->flags & SRV_F_BACKUP ? "Backup " : "",
5545 s->proxy->id, s->id,
5546 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5547 }
5548 else {
5549 chunk_printf(tmptrash,
5550 "%sServer %s/%s is %s (leaving drain)",
5551 s->flags & SRV_F_BACKUP ? "Backup " : "",
5552 s->proxy->id, s->id,
5553 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5554 if (s->track) /* normally it's mandatory here */
5555 chunk_appendf(tmptrash, " via %s/%s",
5556 s->track->proxy->id, s->track->id);
5557 }
5558
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005559 ha_warning("%s.\n", tmptrash->area);
5560 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5561 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005562 free_trash_chunk(tmptrash);
5563 tmptrash = NULL;
5564 }
5565
5566 /* now propagate the status change to any LB algorithms */
5567 if (px->lbprm.update_server_eweight)
5568 px->lbprm.update_server_eweight(s);
5569 else if (srv_willbe_usable(s)) {
5570 if (px->lbprm.set_server_status_up)
5571 px->lbprm.set_server_status_up(s);
5572 }
5573 else {
5574 if (px->lbprm.set_server_status_down)
5575 px->lbprm.set_server_status_down(s);
5576 }
5577 }
5578 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5579 /* remaining in drain mode after removing one of its flags */
5580
5581 tmptrash = alloc_trash_chunk();
5582 if (tmptrash) {
5583 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5584 chunk_printf(tmptrash,
5585 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5586 s->flags & SRV_F_BACKUP ? "Backup " : "",
5587 s->proxy->id, s->id);
5588
5589 if (s->track) /* normally it's mandatory here */
5590 chunk_appendf(tmptrash, " via %s/%s",
5591 s->track->proxy->id, s->track->id);
5592 }
5593 else {
5594 chunk_printf(tmptrash,
5595 "%sServer %s/%s remains in forced drain mode",
5596 s->flags & SRV_F_BACKUP ? "Backup " : "",
5597 s->proxy->id, s->id);
5598 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005599 ha_warning("%s.\n", tmptrash->area);
5600 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5601 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005602 free_trash_chunk(tmptrash);
5603 tmptrash = NULL;
5604 }
5605
5606 /* commit new admin status */
5607
5608 s->cur_admin = s->next_admin;
5609 }
5610 }
5611
5612 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005613 *s->adm_st_chg_cause = 0;
5614}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005615
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005616struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5617{
5618 struct connection *conn;
5619
5620 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5621 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005622 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005623 }
5624
5625 return task;
5626}
5627
Willy Tarreau980855b2019-02-07 14:59:29 +01005628struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005629{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005630 struct server *srv;
5631 struct eb32_node *eb;
5632 int i;
5633 unsigned int next_wakeup;
5634 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005635
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005636 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5637 while (1) {
5638 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005639
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005640 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5641 if (!eb) {
5642 /* we might have reached the end of the tree, typically because
5643 * <now_ms> is in the first half and we're first scanning the last
5644 * half. Let's loop back to the beginning of the tree now.
5645 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005646
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005647 eb = eb32_first(&idle_conn_srv);
5648 if (likely(!eb))
5649 break;
5650 }
5651 if (tick_is_lt(now_ms, eb->key)) {
5652 /* timer not expired yet, revisit it later */
5653 next_wakeup = eb->key;
5654 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005655 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005656 }
5657 srv = eb32_entry(eb, struct server, idle_node);
5658 for (i = 0; i < global.nbthread; i++) {
5659 int max_conn = (srv->curr_idle_thr[i] / 2) +
5660 (srv->curr_idle_thr[i] & 1);
5661 int j;
5662 int did_remove = 0;
5663
Olivier Houchard4be71902019-07-11 15:49:00 +02005664 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005665 for (j = 0; j < max_conn; j++) {
5666 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5667 if (!conn)
5668 break;
5669 did_remove = 1;
5670 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5671 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005672 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005673 if (did_remove && max_conn < srv->curr_idle_thr[i])
5674 srv_is_empty = 0;
5675 if (did_remove)
5676 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5677 }
5678 eb32_delete(&srv->idle_node);
5679 if (!srv_is_empty) {
5680 /* There are still more idle connections, add the
5681 * server back in the tree.
5682 */
5683 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5684 now_ms);
5685 eb32_insert(&idle_conn_srv, &srv->idle_node);
5686 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005687 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005688 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5689
5690 if (need_wakeup)
5691 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005692 else
5693 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005694
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005695 return task;
5696}
Olivier Houchard88698d92019-04-16 19:07:22 +02005697
5698/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5699static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5700 struct proxy *defpx, const char *file, int line,
5701 char **err)
5702{
5703 int arg = -1;
5704
5705 if (too_many_args(1, args, err, NULL))
5706 return -1;
5707
5708 if (*(args[1]) != 0)
5709 arg = atoi(args[1]);
5710
5711 if (arg < 0 || arg > 100) {
5712 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5713 return -1;
5714 }
5715
5716 if (args[0][10] == 'h')
5717 global.tune.pool_high_ratio = arg;
5718 else
5719 global.tune.pool_low_ratio = arg;
5720 return 0;
5721}
5722
5723/* config keyword parsers */
5724static struct cfg_kw_list cfg_kws = {ILH, {
5725 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5726 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5727 { 0, NULL, NULL }
5728}};
5729
5730INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5731
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005732/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005733 * Local variables:
5734 * c-indent-level: 8
5735 * c-basic-offset: 8
5736 * End:
5737 */