blob: 03fca296394e076f286b52516052e0c3e38adc67 [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>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020035#include <proto/port_range.h>
36#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020037#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010038#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020039#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010041#include <proto/stream_interface.h>
42#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020043#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020044#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010045#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020047static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010048static int srv_apply_lastaddr(struct server *srv, int *err_code);
Frédéric Lécailleb418c122017-04-26 11:24:02 +020049static int srv_set_fqdn(struct server *srv, const char *fqdn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Willy Tarreau21faa912012-10-10 08:27:36 +020051/* List head of all known server keywords */
52static struct srv_kw_list srv_keywords = {
53 .list = LIST_HEAD_INIT(srv_keywords.list)
54};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055
Simon Hormana3608442013-11-01 16:46:15 +090056int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020057{
Emeric Brun52a91d32017-08-31 14:41:55 +020058 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059 return s->down_time;
60
61 return now.tv_sec - s->last_change + s->down_time;
62}
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050064int srv_lastsession(const struct server *s)
65{
66 if (s->counters.last_sess)
67 return now.tv_sec - s->counters.last_sess;
68
69 return -1;
70}
71
Simon Horman4a741432013-02-23 15:35:38 +090072int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020073{
Simon Horman4a741432013-02-23 15:35:38 +090074 const struct server *s = check->server;
75
Willy Tarreauff5ae352013-12-11 20:36:34 +010076 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090077 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010078
Emeric Brun52a91d32017-08-31 14:41:55 +020079 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090080 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010081
Simon Horman4a741432013-02-23 15:35:38 +090082 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010083}
84
Olivier Houchard4e694042017-03-14 20:01:29 +010085void srv_set_dyncookie(struct server *s)
86{
87 struct proxy *p = s->proxy;
88 struct server *tmpserv;
89 char *tmpbuf;
90 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010091 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010092 size_t buffer_len;
93 int addr_len;
94 int port;
95
96 if ((s->flags & SRV_F_COOKIESET) ||
97 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
98 s->proxy->dyncookie_key == NULL)
99 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100100 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100101
102 if (s->addr.ss_family != AF_INET &&
103 s->addr.ss_family != AF_INET6)
104 return;
105 /*
106 * Buffer to calculate the cookie value.
107 * The buffer contains the secret key + the server IP address
108 * + the TCP port.
109 */
110 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
111 /*
112 * The TCP port should use only 2 bytes, but is stored in
113 * an unsigned int in struct server, so let's use 4, to be
114 * on the safe side.
115 */
116 buffer_len = key_len + addr_len + 4;
117 tmpbuf = trash.str;
118 memcpy(tmpbuf, p->dyncookie_key, key_len);
119 memcpy(&(tmpbuf[key_len]),
120 s->addr.ss_family == AF_INET ?
121 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
122 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
123 addr_len);
124 /*
125 * Make sure it's the same across all the load balancers,
126 * no matter their endianness.
127 */
128 port = htonl(s->svc_port);
129 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
130 hash_value = XXH64(tmpbuf, buffer_len, 0);
131 memprintf(&s->cookie, "%016llx", hash_value);
132 if (!s->cookie)
133 return;
134 s->cklen = 16;
135 /*
136 * Check that we did not get a hash collision.
137 * Unlikely, but it can happen.
138 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200139 for (tmpserv = p->srv; tmpserv != NULL;
140 tmpserv = tmpserv->next) {
141 if (tmpserv == s)
142 continue;
143 if (tmpserv->cookie &&
144 strcmp(tmpserv->cookie, s->cookie) == 0) {
145 Warning("We generated two equal cookies for two different servers.\n"
146 "Please change the secret key for '%s'.\n",
147 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100148 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200149 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100150}
151
Willy Tarreau21faa912012-10-10 08:27:36 +0200152/*
153 * Registers the server keyword list <kwl> as a list of valid keywords for next
154 * parsing sessions.
155 */
156void srv_register_keywords(struct srv_kw_list *kwl)
157{
158 LIST_ADDQ(&srv_keywords.list, &kwl->list);
159}
160
161/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
162 * keyword is found with a NULL ->parse() function, then an attempt is made to
163 * find one with a valid ->parse() function. This way it is possible to declare
164 * platform-dependant, known keywords as NULL, then only declare them as valid
165 * if some options are met. Note that if the requested keyword contains an
166 * opening parenthesis, everything from this point is ignored.
167 */
168struct srv_kw *srv_find_kw(const char *kw)
169{
170 int index;
171 const char *kwend;
172 struct srv_kw_list *kwl;
173 struct srv_kw *ret = NULL;
174
175 kwend = strchr(kw, '(');
176 if (!kwend)
177 kwend = kw + strlen(kw);
178
179 list_for_each_entry(kwl, &srv_keywords.list, list) {
180 for (index = 0; kwl->kw[index].kw != NULL; index++) {
181 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
182 kwl->kw[index].kw[kwend-kw] == 0) {
183 if (kwl->kw[index].parse)
184 return &kwl->kw[index]; /* found it !*/
185 else
186 ret = &kwl->kw[index]; /* may be OK */
187 }
188 }
189 }
190 return ret;
191}
192
193/* Dumps all registered "server" keywords to the <out> string pointer. The
194 * unsupported keywords are only dumped if their supported form was not
195 * found.
196 */
197void srv_dump_kws(char **out)
198{
199 struct srv_kw_list *kwl;
200 int index;
201
202 *out = NULL;
203 list_for_each_entry(kwl, &srv_keywords.list, list) {
204 for (index = 0; kwl->kw[index].kw != NULL; index++) {
205 if (kwl->kw[index].parse ||
206 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
207 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
208 kwl->scope,
209 kwl->kw[index].kw,
210 kwl->kw[index].skip ? " <arg>" : "",
211 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
212 kwl->kw[index].parse ? "" : " (not supported)");
213 }
214 }
215 }
216}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100217
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100218/* Parse the "addr" server keyword */
219static int srv_parse_addr(char **args, int *cur_arg,
220 struct proxy *curproxy, struct server *newsrv, char **err)
221{
222 char *errmsg, *arg;
223 struct sockaddr_storage *sk;
224 int port1, port2;
225 struct protocol *proto;
226
227 errmsg = NULL;
228 arg = args[*cur_arg + 1];
229
230 if (!*arg) {
231 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
232 goto err;
233 }
234
235 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
236 if (!sk) {
237 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
238 goto err;
239 }
240
241 proto = protocol_by_family(sk->ss_family);
242 if (!proto || !proto->connect) {
243 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
244 args[*cur_arg], arg);
245 goto err;
246 }
247
248 if (port1 != port2) {
249 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
250 args[*cur_arg], arg);
251 goto err;
252 }
253
254 newsrv->check.addr = newsrv->agent.addr = *sk;
255 newsrv->flags |= SRV_F_CHECKADDR;
256 newsrv->flags |= SRV_F_AGENTADDR;
257
258 return 0;
259
260 err:
261 free(errmsg);
262 return ERR_ALERT | ERR_FATAL;
263}
264
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100265/* Parse the "agent-check" server keyword */
266static int srv_parse_agent_check(char **args, int *cur_arg,
267 struct proxy *curproxy, struct server *newsrv, char **err)
268{
269 newsrv->do_agent = 1;
270 return 0;
271}
272
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100273/* Parse the "backup" server keyword */
274static int srv_parse_backup(char **args, int *cur_arg,
275 struct proxy *curproxy, struct server *newsrv, char **err)
276{
277 newsrv->flags |= SRV_F_BACKUP;
278 return 0;
279}
280
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100281/* Parse the "check" server keyword */
282static int srv_parse_check(char **args, int *cur_arg,
283 struct proxy *curproxy, struct server *newsrv, char **err)
284{
285 newsrv->do_check = 1;
286 return 0;
287}
288
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100289/* Parse the "check-send-proxy" server keyword */
290static int srv_parse_check_send_proxy(char **args, int *cur_arg,
291 struct proxy *curproxy, struct server *newsrv, char **err)
292{
293 newsrv->check.send_proxy = 1;
294 return 0;
295}
296
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100297/* Parse the "cookie" server keyword */
298static int srv_parse_cookie(char **args, int *cur_arg,
299 struct proxy *curproxy, struct server *newsrv, char **err)
300{
301 char *arg;
302
303 arg = args[*cur_arg + 1];
304 if (!*arg) {
305 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
306 return ERR_ALERT | ERR_FATAL;
307 }
308
309 free(newsrv->cookie);
310 newsrv->cookie = strdup(arg);
311 newsrv->cklen = strlen(arg);
312 newsrv->flags |= SRV_F_COOKIESET;
313 return 0;
314}
315
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100316/* Parse the "disabled" server keyword */
317static int srv_parse_disabled(char **args, int *cur_arg,
318 struct proxy *curproxy, struct server *newsrv, char **err)
319{
Emeric Brun52a91d32017-08-31 14:41:55 +0200320 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
321 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100322 newsrv->check.state |= CHK_ST_PAUSED;
323 newsrv->check.health = 0;
324 return 0;
325}
326
327/* Parse the "enabled" server keyword */
328static int srv_parse_enabled(char **args, int *cur_arg,
329 struct proxy *curproxy, struct server *newsrv, char **err)
330{
Emeric Brun52a91d32017-08-31 14:41:55 +0200331 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
332 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100333 newsrv->check.state &= ~CHK_ST_PAUSED;
334 newsrv->check.health = newsrv->check.rise;
335 return 0;
336}
337
Willy Tarreaudff55432012-10-10 17:51:05 +0200338/* parse the "id" server keyword */
339static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
340{
341 struct eb32_node *node;
342
343 if (!*args[*cur_arg + 1]) {
344 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
345 return ERR_ALERT | ERR_FATAL;
346 }
347
348 newsrv->puid = atol(args[*cur_arg + 1]);
349 newsrv->conf.id.key = newsrv->puid;
350
351 if (newsrv->puid <= 0) {
352 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
353 return ERR_ALERT | ERR_FATAL;
354 }
355
356 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
357 if (node) {
358 struct server *target = container_of(node, struct server, conf.id);
359 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
360 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
361 target->id);
362 return ERR_ALERT | ERR_FATAL;
363 }
364
365 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200366 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200367 return 0;
368}
369
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100370/* Parse the "namespace" server keyword */
371static int srv_parse_namespace(char **args, int *cur_arg,
372 struct proxy *curproxy, struct server *newsrv, char **err)
373{
374#ifdef CONFIG_HAP_NS
375 char *arg;
376
377 arg = args[*cur_arg + 1];
378 if (!*arg) {
379 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
380 return ERR_ALERT | ERR_FATAL;
381 }
382
383 if (!strcmp(arg, "*")) {
384 /* Use the namespace associated with the connection (if present). */
385 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
386 return 0;
387 }
388
389 /*
390 * As this parser may be called several times for the same 'default-server'
391 * object, or for a new 'server' instance deriving from a 'default-server'
392 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
393 */
394 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
395
396 newsrv->netns = netns_store_lookup(arg, strlen(arg));
397 if (!newsrv->netns)
398 newsrv->netns = netns_store_insert(arg);
399
400 if (!newsrv->netns) {
401 memprintf(err, "Cannot open namespace '%s'", arg);
402 return ERR_ALERT | ERR_FATAL;
403 }
404
405 return 0;
406#else
407 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
408 return ERR_ALERT | ERR_FATAL;
409#endif
410}
411
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100412/* Parse the "no-agent-check" server keyword */
413static int srv_parse_no_agent_check(char **args, int *cur_arg,
414 struct proxy *curproxy, struct server *newsrv, char **err)
415{
416 free_check(&newsrv->agent);
417 newsrv->agent.inter = 0;
418 newsrv->agent.port = 0;
419 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
420 newsrv->do_agent = 0;
421 return 0;
422}
423
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100424/* Parse the "no-backup" server keyword */
425static int srv_parse_no_backup(char **args, int *cur_arg,
426 struct proxy *curproxy, struct server *newsrv, char **err)
427{
428 newsrv->flags &= ~SRV_F_BACKUP;
429 return 0;
430}
431
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100432/* Parse the "no-check" server keyword */
433static int srv_parse_no_check(char **args, int *cur_arg,
434 struct proxy *curproxy, struct server *newsrv, char **err)
435{
436 free_check(&newsrv->check);
437 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
438 newsrv->do_check = 0;
439 return 0;
440}
441
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100442/* Parse the "no-check-send-proxy" server keyword */
443static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
444 struct proxy *curproxy, struct server *newsrv, char **err)
445{
446 newsrv->check.send_proxy = 0;
447 return 0;
448}
449
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100450/* Disable server PROXY protocol flags. */
451static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
452{
453 srv->pp_opts &= ~flags;
454 return 0;
455}
456
457/* Parse the "no-send-proxy" server keyword */
458static int srv_parse_no_send_proxy(char **args, int *cur_arg,
459 struct proxy *curproxy, struct server *newsrv, char **err)
460{
461 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
462}
463
464/* Parse the "no-send-proxy-v2" server keyword */
465static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
466 struct proxy *curproxy, struct server *newsrv, char **err)
467{
468 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
469}
470
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100471/* Parse the "non-stick" server keyword */
472static int srv_parse_non_stick(char **args, int *cur_arg,
473 struct proxy *curproxy, struct server *newsrv, char **err)
474{
475 newsrv->flags |= SRV_F_NON_STICK;
476 return 0;
477}
478
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100479/* Enable server PROXY protocol flags. */
480static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
481{
482 srv->pp_opts |= flags;
483 return 0;
484}
485
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100486/* Parse the "observe" server keyword */
487static int srv_parse_observe(char **args, int *cur_arg,
488 struct proxy *curproxy, struct server *newsrv, char **err)
489{
490 char *arg;
491
492 arg = args[*cur_arg + 1];
493 if (!*arg) {
494 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
495 return ERR_ALERT | ERR_FATAL;
496 }
497
498 if (!strcmp(arg, "none")) {
499 newsrv->observe = HANA_OBS_NONE;
500 }
501 else if (!strcmp(arg, "layer4")) {
502 newsrv->observe = HANA_OBS_LAYER4;
503 }
504 else if (!strcmp(arg, "layer7")) {
505 if (curproxy->mode != PR_MODE_HTTP) {
506 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
507 return ERR_ALERT;
508 }
509 newsrv->observe = HANA_OBS_LAYER7;
510 }
511 else {
512 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
513 "but got '%s'\n", args[*cur_arg], arg);
514 return ERR_ALERT | ERR_FATAL;
515 }
516
517 return 0;
518}
519
Frédéric Lécaille16186232017-03-14 16:42:49 +0100520/* Parse the "redir" server keyword */
521static int srv_parse_redir(char **args, int *cur_arg,
522 struct proxy *curproxy, struct server *newsrv, char **err)
523{
524 char *arg;
525
526 arg = args[*cur_arg + 1];
527 if (!*arg) {
528 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
529 return ERR_ALERT | ERR_FATAL;
530 }
531
532 free(newsrv->rdr_pfx);
533 newsrv->rdr_pfx = strdup(arg);
534 newsrv->rdr_len = strlen(arg);
535
536 return 0;
537}
538
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100539/* Parse the "send-proxy" server keyword */
540static int srv_parse_send_proxy(char **args, int *cur_arg,
541 struct proxy *curproxy, struct server *newsrv, char **err)
542{
543 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
544}
545
546/* Parse the "send-proxy-v2" server keyword */
547static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
548 struct proxy *curproxy, struct server *newsrv, char **err)
549{
550 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
551}
552
Frédéric Lécailledba97072017-03-17 15:33:50 +0100553
554/* Parse the "source" server keyword */
555static int srv_parse_source(char **args, int *cur_arg,
556 struct proxy *curproxy, struct server *newsrv, char **err)
557{
558 char *errmsg;
559 int port_low, port_high;
560 struct sockaddr_storage *sk;
561 struct protocol *proto;
562
563 errmsg = NULL;
564
565 if (!*args[*cur_arg + 1]) {
566 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
567 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
568 goto err;
569 }
570
571 /* 'sk' is statically allocated (no need to be freed). */
572 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
573 if (!sk) {
574 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
575 goto err;
576 }
577
578 proto = protocol_by_family(sk->ss_family);
579 if (!proto || !proto->connect) {
580 Alert("'%s %s' : connect() not supported for this address family.\n",
581 args[*cur_arg], args[*cur_arg + 1]);
582 goto err;
583 }
584
585 newsrv->conn_src.opts |= CO_SRC_BIND;
586 newsrv->conn_src.source_addr = *sk;
587
588 if (port_low != port_high) {
589 int i;
590
591 if (!port_low || !port_high) {
592 Alert("'%s' does not support port offsets (found '%s').\n",
593 args[*cur_arg], args[*cur_arg + 1]);
594 goto err;
595 }
596
597 if (port_low <= 0 || port_low > 65535 ||
598 port_high <= 0 || port_high > 65535 ||
599 port_low > port_high) {
600 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
601 goto err;
602 }
603 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
604 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
605 newsrv->conn_src.sport_range->ports[i] = port_low + i;
606 }
607
608 *cur_arg += 2;
609 while (*(args[*cur_arg])) {
610 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
611#if defined(CONFIG_HAP_TRANSPARENT)
612 if (!*args[*cur_arg + 1]) {
613 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
614 "or 'hdr_ip(name,#)' as argument.\n");
615 goto err;
616 }
617 if (!strcmp(args[*cur_arg + 1], "client")) {
618 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
619 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
620 }
621 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
622 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
623 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
624 }
625 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
626 char *name, *end;
627
628 name = args[*cur_arg + 1] + 7;
629 while (isspace(*name))
630 name++;
631
632 end = name;
633 while (*end && !isspace(*end) && *end != ',' && *end != ')')
634 end++;
635
636 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
637 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
638 free(newsrv->conn_src.bind_hdr_name);
639 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
640 newsrv->conn_src.bind_hdr_len = end - name;
641 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
642 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
643 newsrv->conn_src.bind_hdr_occ = -1;
644
645 /* now look for an occurrence number */
646 while (isspace(*end))
647 end++;
648 if (*end == ',') {
649 end++;
650 name = end;
651 if (*end == '-')
652 end++;
653 while (isdigit((int)*end))
654 end++;
655 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
656 }
657
658 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
659 Alert("usesrc hdr_ip(name,num) does not support negative"
660 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
661 goto err;
662 }
663 }
664 else {
665 struct sockaddr_storage *sk;
666 int port1, port2;
667
668 /* 'sk' is statically allocated (no need to be freed). */
669 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
670 if (!sk) {
671 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
672 goto err;
673 }
674
675 proto = protocol_by_family(sk->ss_family);
676 if (!proto || !proto->connect) {
677 Alert("'%s %s' : connect() not supported for this address family.\n",
678 args[*cur_arg], args[*cur_arg + 1]);
679 goto err;
680 }
681
682 if (port1 != port2) {
683 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
684 args[*cur_arg], args[*cur_arg + 1]);
685 goto err;
686 }
687 newsrv->conn_src.tproxy_addr = *sk;
688 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
689 }
690 global.last_checks |= LSTCHK_NETADM;
691 *cur_arg += 2;
692 continue;
693#else /* no TPROXY support */
694 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
695 goto err;
696#endif /* defined(CONFIG_HAP_TRANSPARENT) */
697 } /* "usesrc" */
698
699 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
700#ifdef SO_BINDTODEVICE
701 if (!*args[*cur_arg + 1]) {
702 Alert("'%s' : missing interface name.\n", args[0]);
703 goto err;
704 }
705 free(newsrv->conn_src.iface_name);
706 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
707 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
708 global.last_checks |= LSTCHK_NETADM;
709#else
710 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
711 goto err;
712#endif
713 *cur_arg += 2;
714 continue;
715 }
716 /* this keyword in not an option of "source" */
717 break;
718 } /* while */
719
720 return 0;
721
722 err:
723 free(errmsg);
724 return ERR_ALERT | ERR_FATAL;
725}
726
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100727/* Parse the "stick" server keyword */
728static int srv_parse_stick(char **args, int *cur_arg,
729 struct proxy *curproxy, struct server *newsrv, char **err)
730{
731 newsrv->flags &= ~SRV_F_NON_STICK;
732 return 0;
733}
734
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100735/* Parse the "track" server keyword */
736static int srv_parse_track(char **args, int *cur_arg,
737 struct proxy *curproxy, struct server *newsrv, char **err)
738{
739 char *arg;
740
741 arg = args[*cur_arg + 1];
742 if (!*arg) {
743 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
744 return ERR_ALERT | ERR_FATAL;
745 }
746
747 free(newsrv->trackit);
748 newsrv->trackit = strdup(arg);
749
750 return 0;
751}
752
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200754/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200755 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200756 * shutdown.
757 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200758void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200759{
Willy Tarreau87b09662015-04-03 00:22:06 +0200760 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200761
Willy Tarreau87b09662015-04-03 00:22:06 +0200762 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
763 if (stream->srv_conn == srv)
764 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200765}
766
767/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200768 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200769 * the reason for the shutdown.
770 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200771void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200772{
773 struct server *srv;
774
775 for (srv = px->srv; srv != NULL; srv = srv->next)
776 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200777 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200778}
779
Willy Tarreaubda92272014-05-20 21:55:30 +0200780/* Appends some information to a message string related to a server going UP or
781 * DOWN. If both <forced> and <reason> are null and the server tracks another
782 * one, a "via" information will be provided to know where the status came from.
783 * If <reason> is non-null, the entire string will be appended after a comma and
784 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200785 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200786 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200787 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200788void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200789{
Willy Tarreaubda92272014-05-20 21:55:30 +0200790 if (reason)
791 chunk_appendf(msg, ", %s", reason);
792 else if (!forced && s->track)
793 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200794
795 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200796 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200797 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
798 " %d sessions active, %d requeued, %d remaining in queue",
799 s->proxy->srv_act, s->proxy->srv_bck,
800 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
801 s->cur_sess, xferred, s->nbpend);
802 else
803 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
804 " %d sessions requeued, %d total in queue",
805 s->proxy->srv_act, s->proxy->srv_bck,
806 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
807 xferred, s->nbpend);
808 }
809}
810
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200811/* Marks server <s> down, regardless of its checks' statuses, notifies by all
812 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200813 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200814 * recomputes the number of servers, but not the map. Maintenance servers are
815 * ignored. It reports <reason> if non-null as the reason for going down. Note
816 * that it makes use of the trash to build the log strings, so <reason> must
817 * not be placed there.
818 */
819void srv_set_stopped(struct server *s, const char *reason)
820{
821 struct server *srv;
822 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200823 int srv_was_stopping = (s->next_state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900824 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200825 int xferred;
826
Emeric Brun52a91d32017-08-31 14:41:55 +0200827 if ((s->next_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200828 return;
829
830 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +0200831 s->next_state = SRV_ST_STOPPED;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200832 if (s->proxy->lbprm.set_server_status_down)
833 s->proxy->lbprm.set_server_status_down(s);
834
835 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200836 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200837
Willy Tarreau87b09662015-04-03 00:22:06 +0200838 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200839 * a connection. Those which are redispatchable will be queued
840 * to another server or to the proxy itself.
841 */
842 xferred = pendconn_redistribute(s);
843
844 chunk_printf(&trash,
845 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
846 s->proxy->id, s->id);
847
848 srv_append_status(&trash, s, reason, xferred, 0);
849 Warning("%s.\n", trash.str);
850
851 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900852 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
853 send_log(s->proxy, log_level, "%s.\n", trash.str);
854 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200855
856 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
857 set_backend_down(s->proxy);
858
859 s->counters.down_trans++;
860
861 for (srv = s->trackers; srv; srv = srv->tracknext)
862 srv_set_stopped(srv, NULL);
863}
864
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200865/* Marks server <s> up regardless of its checks' statuses and provided it isn't
866 * in maintenance. Notifies by all available means, recounts the remaining
867 * servers on the proxy and tries to grab requests from the proxy. It
868 * automatically recomputes the number of servers, but not the map. Maintenance
869 * servers are ignored. It reports <reason> if non-null as the reason for going
870 * up. Note that it makes use of the trash to build the log strings, so <reason>
871 * must not be placed there.
872 */
873void srv_set_running(struct server *s, const char *reason)
874{
875 struct server *srv;
876 int xferred;
877
Emeric Brun52a91d32017-08-31 14:41:55 +0200878 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200879 return;
880
Emeric Brun52a91d32017-08-31 14:41:55 +0200881 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200882 return;
883
884 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
885 if (s->proxy->last_change < now.tv_sec) // ignore negative times
886 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
887 s->proxy->last_change = now.tv_sec;
888 }
889
Emeric Brun52a91d32017-08-31 14:41:55 +0200890 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200891 s->down_time += now.tv_sec - s->last_change;
892
893 s->last_change = now.tv_sec;
894
Emeric Brun52a91d32017-08-31 14:41:55 +0200895 s->next_state = SRV_ST_STARTING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200896 if (s->slowstart > 0)
897 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
898 else
Emeric Brun52a91d32017-08-31 14:41:55 +0200899 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200900
901 server_recalc_eweight(s);
902
903 /* If the server is set with "on-marked-up shutdown-backup-sessions",
904 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200905 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200906 * on all backup servers.
907 */
908 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200909 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200910 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200911
912 /* check if we can handle some connections queued at the proxy. We
913 * will take as many as we can handle.
914 */
915 xferred = pendconn_grab_from_px(s);
916
917 chunk_printf(&trash,
918 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
919 s->proxy->id, s->id);
920
921 srv_append_status(&trash, s, reason, xferred, 0);
922 Warning("%s.\n", trash.str);
923 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900924 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200925
926 for (srv = s->trackers; srv; srv = srv->tracknext)
927 srv_set_running(srv, NULL);
928}
929
Willy Tarreau8eb77842014-05-21 13:54:57 +0200930/* Marks server <s> stopping regardless of its checks' statuses and provided it
931 * isn't in maintenance. Notifies by all available means, recounts the remaining
932 * servers on the proxy and tries to grab requests from the proxy. It
933 * automatically recomputes the number of servers, but not the map. Maintenance
934 * servers are ignored. It reports <reason> if non-null as the reason for going
935 * up. Note that it makes use of the trash to build the log strings, so <reason>
936 * must not be placed there.
937 */
938void srv_set_stopping(struct server *s, const char *reason)
939{
940 struct server *srv;
941 int xferred;
942
Emeric Brun52a91d32017-08-31 14:41:55 +0200943 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200944 return;
945
Emeric Brun52a91d32017-08-31 14:41:55 +0200946 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200947 return;
948
949 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +0200950 s->next_state = SRV_ST_STOPPING;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200951 if (s->proxy->lbprm.set_server_status_down)
952 s->proxy->lbprm.set_server_status_down(s);
953
Willy Tarreau87b09662015-04-03 00:22:06 +0200954 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200955 * a connection. Those which are redispatchable will be queued
956 * to another server or to the proxy itself.
957 */
958 xferred = pendconn_redistribute(s);
959
960 chunk_printf(&trash,
961 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
962 s->proxy->id, s->id);
963
964 srv_append_status(&trash, s, reason, xferred, 0);
965
966 Warning("%s.\n", trash.str);
967 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
968
969 if (!s->proxy->srv_bck && !s->proxy->srv_act)
970 set_backend_down(s->proxy);
971
972 for (srv = s->trackers; srv; srv = srv->tracknext)
973 srv_set_stopping(srv, NULL);
974}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200975
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200976/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
977 * enforce either maint mode or drain mode. It is not allowed to set more than
978 * one flag at once. The equivalent "inherited" flag is propagated to all
979 * tracking servers. Maintenance mode disables health checks (but not agent
980 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100981 * is done. If <cause> is non-null, it will be displayed at the end of the log
982 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200983 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100984void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200985{
986 struct check *check = &s->check;
987 struct server *srv;
988 int xferred;
989
990 if (!mode)
991 return;
992
993 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200994 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200995 return;
996
Emeric Brun52a91d32017-08-31 14:41:55 +0200997 s->next_admin |= mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200998
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200999 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001000 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1001 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001002 return;
1003
1004 /* Maintenance must also disable health checks */
1005 if (mode & SRV_ADMF_MAINT) {
1006 if (s->check.state & CHK_ST_ENABLED) {
1007 s->check.state |= CHK_ST_PAUSED;
1008 check->health = 0;
1009 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001010
Emeric Brun52a91d32017-08-31 14:41:55 +02001011 if (s->next_state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001012 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001013 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
1014 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1015 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001016
Willy Tarreaubda92272014-05-20 21:55:30 +02001017 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001019 if (!(global.mode & MODE_STARTING)) {
1020 Warning("%s.\n", trash.str);
1021 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1022 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001023 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001024 else { /* server was still running */
Emeric Brun52a91d32017-08-31 14:41:55 +02001025 int srv_was_stopping = (s->next_state == SRV_ST_STOPPING) || (s->next_admin & SRV_ADMF_DRAIN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001026 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1027
1028 check->health = 0; /* failure */
1029 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +02001030 s->next_state = SRV_ST_STOPPED;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001031 if (s->proxy->lbprm.set_server_status_down)
1032 s->proxy->lbprm.set_server_status_down(s);
1033
1034 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001035 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036
Willy Tarreau87b09662015-04-03 00:22:06 +02001037 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001038 * a connection. Those which are redispatchable will be queued
1039 * to another server or to the proxy itself.
1040 */
1041 xferred = pendconn_redistribute(s);
1042
1043 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001044 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001045 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001046 s->proxy->id, s->id,
1047 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001048
1049 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1050
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001051 if (!(global.mode & MODE_STARTING)) {
1052 Warning("%s.\n", trash.str);
1053 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1054 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001055
1056 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1057 set_backend_down(s->proxy);
1058
1059 s->counters.down_trans++;
1060 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001061 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001062
1063 /* drain state is applied only if not yet in maint */
Emeric Brun52a91d32017-08-31 14:41:55 +02001064 if ((mode & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001065 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1066
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001067 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068 if (s->proxy->lbprm.set_server_status_down)
1069 s->proxy->lbprm.set_server_status_down(s);
1070
Willy Tarreau87b09662015-04-03 00:22:06 +02001071 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072 * a connection. Those which are redispatchable will be queued
1073 * to another server or to the proxy itself.
1074 */
1075 xferred = pendconn_redistribute(s);
1076
Willy Tarreau8b428482016-11-07 15:53:43 +01001077 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1078 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1079 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001080
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001081 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001082
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001083 if (!(global.mode & MODE_STARTING)) {
1084 Warning("%s.\n", trash.str);
1085 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1086 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1087 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001088 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1089 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001090 }
1091
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001092 /* compute the inherited flag to propagate */
1093 if (mode & SRV_ADMF_MAINT)
1094 mode = SRV_ADMF_IMAINT;
1095 else if (mode & SRV_ADMF_DRAIN)
1096 mode = SRV_ADMF_IDRAIN;
1097
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001098 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001099 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001100}
1101
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001102/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1103 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1104 * than one flag at once. The equivalent "inherited" flag is propagated to all
1105 * tracking servers. Leaving maintenance mode re-enables health checks. When
1106 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001107 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001108void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001109{
1110 struct check *check = &s->check;
1111 struct server *srv;
1112 int xferred = -1;
1113
1114 if (!mode)
1115 return;
1116
1117 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001118 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001119 return;
1120
Emeric Brun52a91d32017-08-31 14:41:55 +02001121 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001122
Emeric Brun52a91d32017-08-31 14:41:55 +02001123 if (s->next_admin & SRV_ADMF_MAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001124 /* remaining in maintenance mode, let's inform precisely about the
1125 * situation.
1126 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001127 if (mode & SRV_ADMF_FMAINT) {
1128 chunk_printf(&trash,
1129 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1130 s->flags & SRV_F_BACKUP ? "Backup " : "",
1131 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001133 if (s->track) /* normally it's mandatory here */
1134 chunk_appendf(&trash, " via %s/%s",
1135 s->track->proxy->id, s->track->id);
1136 Warning("%s.\n", trash.str);
1137 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1138 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001139 if (mode & SRV_ADMF_RMAINT) {
1140 chunk_printf(&trash,
1141 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1142 s->flags & SRV_F_BACKUP ? "Backup " : "",
1143 s->proxy->id, s->id, s->hostname);
1144
1145 if (s->track) /* normally it's mandatory here */
1146 chunk_appendf(&trash, " via %s/%s",
1147 s->track->proxy->id, s->track->id);
1148 Warning("%s.\n", trash.str);
1149 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1150 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001151 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001152 chunk_printf(&trash,
1153 "%sServer %s/%s remains in forced maintenance",
1154 s->flags & SRV_F_BACKUP ? "Backup " : "",
1155 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001156 Warning("%s.\n", trash.str);
1157 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1158 }
1159 /* don't report anything when leaving drain mode and remaining in maintenance */
1160 }
1161 else if (mode & SRV_ADMF_MAINT) {
1162 /* OK here we're leaving maintenance, we have many things to check,
1163 * because the server might possibly be coming back up depending on
1164 * its state. In practice, leaving maintenance means that we should
1165 * immediately turn to UP (more or less the slowstart) under the
1166 * following conditions :
1167 * - server is neither checked nor tracked
1168 * - server tracks another server which is not checked
1169 * - server tracks another server which is already up
1170 * Which sums up as something simpler :
1171 * "either the tracking server is up or the server's checks are disabled
1172 * or up". Otherwise we only re-enable health checks. There's a special
1173 * case associated to the stopping state which can be inherited. Note
1174 * that the server might still be in drain mode, which is naturally dealt
1175 * with by the lower level functions.
1176 */
1177
1178 if (s->check.state & CHK_ST_ENABLED) {
1179 s->check.state &= ~CHK_ST_PAUSED;
1180 check->health = check->rise; /* start OK but check immediately */
1181 }
1182
Emeric Brun52a91d32017-08-31 14:41:55 +02001183 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001184 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1185 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001186
Emeric Brun52a91d32017-08-31 14:41:55 +02001187 if (s->track && s->track->next_state == SRV_ST_STOPPING)
Emeric Brune1e39472017-09-21 15:45:44 +02001188 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +02001189 s->next_state = SRV_ST_STOPPING;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001190 else {
Emeric Brune1e39472017-09-21 15:45:44 +02001191 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1192 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1193 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1194 s->proxy->last_change = now.tv_sec;
1195 }
1196
1197 if (s->last_change < now.tv_sec) // ignore negative times
1198 s->down_time += now.tv_sec - s->last_change;
1199
1200 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +02001201 s->next_state = SRV_ST_STARTING;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001202 if (s->slowstart > 0)
1203 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1204 else
Emeric Brun52a91d32017-08-31 14:41:55 +02001205 s->next_state = SRV_ST_RUNNING;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001206
Emeric Brune1e39472017-09-21 15:45:44 +02001207 server_recalc_eweight(s);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001208
Emeric Brune1e39472017-09-21 15:45:44 +02001209 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1210 * and it's not a backup server and its effective weight is > 0,
1211 * then it can accept new connections, so we shut down all streams
1212 * on all backup servers.
1213 */
1214 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1215 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
1216 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
1217
1218 /* check if we can handle some connections queued at the proxy. We
1219 * will take as many as we can handle.
1220 */
1221 xferred = pendconn_grab_from_px(s);
1222 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001223
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001224 }
1225
1226 if (mode & SRV_ADMF_FMAINT) {
1227 chunk_printf(&trash,
1228 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1229 s->flags & SRV_F_BACKUP ? "Backup " : "",
1230 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001231 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1232 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001233 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001234 else if (mode & SRV_ADMF_RMAINT) {
1235 chunk_printf(&trash,
1236 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1237 s->flags & SRV_F_BACKUP ? "Backup " : "",
1238 s->proxy->id, s->id, s->hostname,
Emeric Brun52a91d32017-08-31 14:41:55 +02001239 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1240 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaue6599732016-11-07 15:42:33 +01001241 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001242 else {
1243 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001244 "%sServer %s/%s is %s/%s (leaving maintenance)",
1245 s->flags & SRV_F_BACKUP ? "Backup " : "",
1246 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001247 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1248 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001249 srv_append_status(&trash, s, NULL, xferred, 0);
1250 }
1251 Warning("%s.\n", trash.str);
1252 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1253 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001254 else if ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001255 /* remaining in drain mode after removing one of its flags */
1256
1257 if (mode & SRV_ADMF_FDRAIN) {
1258 chunk_printf(&trash,
1259 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001260 s->flags & SRV_F_BACKUP ? "Backup " : "",
1261 s->proxy->id, s->id);
1262
1263 if (s->track) /* normally it's mandatory here */
1264 chunk_appendf(&trash, " via %s/%s",
1265 s->track->proxy->id, s->track->id);
1266 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001267 else {
1268 chunk_printf(&trash,
1269 "%sServer %s/%s remains in forced drain mode",
1270 s->flags & SRV_F_BACKUP ? "Backup " : "",
1271 s->proxy->id, s->id);
1272 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001273 Warning("%s.\n", trash.str);
1274 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001275 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001276 else if (mode & SRV_ADMF_DRAIN) {
1277 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001278 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1279 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1280 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1281 s->proxy->last_change = now.tv_sec;
1282 }
1283
1284 if (s->last_change < now.tv_sec) // ignore negative times
1285 s->down_time += now.tv_sec - s->last_change;
1286 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001287 server_recalc_eweight(s);
1288
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001289 if (mode & SRV_ADMF_FDRAIN) {
1290 chunk_printf(&trash,
1291 "%sServer %s/%s is %s (leaving forced drain)",
1292 s->flags & SRV_F_BACKUP ? "Backup " : "",
1293 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001294 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001295 }
1296 else {
1297 chunk_printf(&trash,
1298 "%sServer %s/%s is %s (leaving drain)",
1299 s->flags & SRV_F_BACKUP ? "Backup " : "",
1300 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001301 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001302 if (s->track) /* normally it's mandatory here */
1303 chunk_appendf(&trash, " via %s/%s",
1304 s->track->proxy->id, s->track->id);
1305 }
1306 Warning("%s.\n", trash.str);
1307 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001308 }
1309
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001310 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001311 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1312 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001313 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001314
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001315 if (mode & SRV_ADMF_MAINT)
1316 mode = SRV_ADMF_IMAINT;
1317 else if (mode & SRV_ADMF_DRAIN)
1318 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001319
1320 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001321 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001322}
1323
Willy Tarreau757478e2016-11-03 19:22:19 +01001324/* principle: propagate maint and drain to tracking servers. This is useful
1325 * upon startup so that inherited states are correct.
1326 */
1327static void srv_propagate_admin_state(struct server *srv)
1328{
1329 struct server *srv2;
1330
1331 if (!srv->trackers)
1332 return;
1333
1334 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001335 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001336 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001337
Emeric Brun52a91d32017-08-31 14:41:55 +02001338 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001339 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001340 }
1341}
1342
1343/* Compute and propagate the admin states for all servers in proxy <px>.
1344 * Only servers *not* tracking another one are considered, because other
1345 * ones will be handled when the server they track is visited.
1346 */
1347void srv_compute_all_admin_states(struct proxy *px)
1348{
1349 struct server *srv;
1350
1351 for (srv = px->srv; srv; srv = srv->next) {
1352 if (srv->track)
1353 continue;
1354 srv_propagate_admin_state(srv);
1355 }
1356}
1357
Willy Tarreaudff55432012-10-10 17:51:05 +02001358/* Note: must not be declared <const> as its list will be overwritten.
1359 * Please take care of keeping this list alphabetically sorted, doing so helps
1360 * all code contributors.
1361 * Optional keywords are also declared with a NULL ->parse() function so that
1362 * the config parser can report an appropriate error when a known keyword was
1363 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001364 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001365 */
1366static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001367 { "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 +01001368 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001369 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001370 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001371 { "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 +01001372 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001373 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1374 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001375 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001376 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001377 { "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 +01001378 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001379 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001380 { "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 +01001381 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1382 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001383 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001384 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001385 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001386 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1387 { "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 +02001388 { "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 +01001389 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001390 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001391 { NULL, NULL, 0 },
1392}};
1393
1394__attribute__((constructor))
1395static void __listener_init(void)
1396{
1397 srv_register_keywords(&srv_kws);
1398}
1399
Willy Tarreau004e0452013-11-21 11:22:01 +01001400/* Recomputes the server's eweight based on its state, uweight, the current time,
1401 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1402 * state is automatically disabled if the time is elapsed.
1403 */
1404void server_recalc_eweight(struct server *sv)
1405{
1406 struct proxy *px = sv->proxy;
1407 unsigned w;
1408
1409 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1410 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001411 if (sv->next_state == SRV_ST_STARTING)
1412 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001413 }
1414
1415 /* We must take care of not pushing the server to full throttle during slow starts.
1416 * It must also start immediately, at least at the minimal step when leaving maintenance.
1417 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001418 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001419 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1420 else
1421 w = px->lbprm.wdiv;
1422
Emeric Brun52a91d32017-08-31 14:41:55 +02001423 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001424
1425 /* now propagate the status change to any LB algorithms */
1426 if (px->lbprm.update_server_eweight)
1427 px->lbprm.update_server_eweight(sv);
Emeric Brun52a91d32017-08-31 14:41:55 +02001428 else if (srv_willbe_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001429 if (px->lbprm.set_server_status_up)
1430 px->lbprm.set_server_status_up(sv);
1431 }
1432 else {
1433 if (px->lbprm.set_server_status_down)
1434 px->lbprm.set_server_status_down(sv);
1435 }
1436}
1437
Willy Tarreaubaaee002006-06-26 02:48:02 +02001438/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001439 * Parses weight_str and configures sv accordingly.
1440 * Returns NULL on success, error message string otherwise.
1441 */
1442const char *server_parse_weight_change_request(struct server *sv,
1443 const char *weight_str)
1444{
1445 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001446 long int w;
1447 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001448
1449 px = sv->proxy;
1450
1451 /* if the weight is terminated with '%', it is set relative to
1452 * the initial weight, otherwise it is absolute.
1453 */
1454 if (!*weight_str)
1455 return "Require <weight> or <weight%>.\n";
1456
Simon Hormanb796afa2013-02-12 10:45:53 +09001457 w = strtol(weight_str, &end, 10);
1458 if (end == weight_str)
1459 return "Empty weight string empty or preceded by garbage";
1460 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001461 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001462 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001463 /* Avoid integer overflow */
1464 if (w > 25600)
1465 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001466 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001467 if (w > 256)
1468 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001469 }
1470 else if (w < 0 || w > 256)
1471 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001472 else if (end[0] != '\0')
1473 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001474
1475 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1476 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1477
1478 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001479 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001480
1481 return NULL;
1482}
1483
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001484/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001485 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1486 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001487 * Returns:
1488 * - error string on error
1489 * - NULL on success
1490 */
1491const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001492 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001493{
1494 unsigned char ip[INET6_ADDRSTRLEN];
1495
1496 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001497 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001498 return NULL;
1499 }
1500 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001501 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001502 return NULL;
1503 }
1504
1505 return "Could not understand IP address format.\n";
1506}
1507
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001508const char *server_parse_maxconn_change_request(struct server *sv,
1509 const char *maxconn_str)
1510{
1511 long int v;
1512 char *end;
1513
1514 if (!*maxconn_str)
1515 return "Require <maxconn>.\n";
1516
1517 v = strtol(maxconn_str, &end, 10);
1518 if (end == maxconn_str)
1519 return "maxconn string empty or preceded by garbage";
1520 else if (end[0] != '\0')
1521 return "Trailing garbage in maxconn string";
1522
1523 if (sv->maxconn == sv->minconn) { // static maxconn
1524 sv->maxconn = sv->minconn = v;
1525 } else { // dynamic maxconn
1526 sv->maxconn = v;
1527 }
1528
1529 if (may_dequeue_tasks(sv, sv->proxy))
1530 process_srv_queue(sv);
1531
1532 return NULL;
1533}
1534
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001535#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001536static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1537 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001538{
1539 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001540 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001541 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001542 NULL,
1543 };
1544
1545 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001546 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001547
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001548 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1549}
1550
1551static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1552{
1553 struct sample_expr *expr;
1554
1555 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 +01001556 if (!expr) {
1557 memprintf(err, "error detected while parsing sni expression : %s", *err);
1558 return ERR_ALERT | ERR_FATAL;
1559 }
1560
1561 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1562 memprintf(err, "error detected while parsing sni expression : "
1563 " fetch method '%s' extracts information from '%s', "
1564 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001565 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001566 return ERR_ALERT | ERR_FATAL;
1567 }
1568
1569 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1570 release_sample_expr(newsrv->ssl_ctx.sni);
1571 newsrv->ssl_ctx.sni = expr;
1572
1573 return 0;
1574}
1575#endif
1576
1577static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1578{
1579 if (err && *err) {
1580 indent_msg(err, 2);
1581 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1582 }
1583 else
1584 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1585 file, linenum, args[0], args[1], args[cur_arg]);
1586}
1587
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001588static void srv_conn_src_sport_range_cpy(struct server *srv,
1589 struct server *src)
1590{
1591 int range_sz;
1592
1593 range_sz = src->conn_src.sport_range->size;
1594 if (range_sz > 0) {
1595 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1596 if (srv->conn_src.sport_range != NULL) {
1597 int i;
1598
1599 for (i = 0; i < range_sz; i++) {
1600 srv->conn_src.sport_range->ports[i] =
1601 src->conn_src.sport_range->ports[i];
1602 }
1603 }
1604 }
1605}
1606
1607/*
1608 * Copy <src> server connection source settings to <srv> server everything needed.
1609 */
1610static void srv_conn_src_cpy(struct server *srv, struct server *src)
1611{
1612 srv->conn_src.opts = src->conn_src.opts;
1613 srv->conn_src.source_addr = src->conn_src.source_addr;
1614
1615 /* Source port range copy. */
1616 if (src->conn_src.sport_range != NULL)
1617 srv_conn_src_sport_range_cpy(srv, src);
1618
1619#ifdef CONFIG_HAP_TRANSPARENT
1620 if (src->conn_src.bind_hdr_name != NULL) {
1621 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1622 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1623 }
1624 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1625 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1626#endif
1627 if (src->conn_src.iface_name != NULL)
1628 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1629}
1630
1631/*
1632 * Copy <src> server SSL settings to <srv> server allocating
1633 * everything needed.
1634 */
1635#if defined(USE_OPENSSL)
1636static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1637{
1638 if (src->ssl_ctx.ca_file != NULL)
1639 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1640 if (src->ssl_ctx.crl_file != NULL)
1641 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1642 if (src->ssl_ctx.client_crt != NULL)
1643 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1644
1645 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1646
1647 if (src->ssl_ctx.verify_host != NULL)
1648 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1649 if (src->ssl_ctx.ciphers != NULL)
1650 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1651 if (src->sni_expr != NULL)
1652 srv->sni_expr = strdup(src->sni_expr);
1653}
1654#endif
1655
1656/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001657 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001658 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001659 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001660 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001661static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001662{
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001663 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001664 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001665
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001666 free(srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001667 srv->hostname = strdup(hostname);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001668
Baptiste Assmann42746372017-05-03 12:12:02 +02001669 srv->hostname_dn_len = dns_str_to_dn_label_len(hostname);
1670 srv->hostname_dn = calloc(srv->hostname_dn_len + 1, sizeof(char));
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001671
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001672 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001673 goto err;
1674
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001675 if (!dns_str_to_dn_label(srv->hostname,
Baptiste Assmann42746372017-05-03 12:12:02 +02001676 srv->hostname_dn,
1677 srv->hostname_dn_len + 1))
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001678 goto err;
1679
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001680 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001681
1682 err:
1683 free(srv->hostname);
1684 srv->hostname = NULL;
Baptiste Assmann42746372017-05-03 12:12:02 +02001685 free(srv->hostname_dn);
1686 srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001687 return -1;
1688}
1689
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001690/*
1691 * Free the link between a server and its resolution.
1692 * It also performs the following tasks:
1693 * - check if resolution can be moved back in the resolvers' pool
1694 * (and do it)
1695 * - move resolution's hostname_dn and hostname_dn_len to the next requester
1696 * available (when applied)
1697 */
Baptiste Assmann747359e2017-08-14 10:37:46 +02001698void srv_free_from_resolution(struct server *srv)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001699{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001700 struct dns_requester *requester;
1701 int count;
1702
1703 /* check if we can move the resolution back to the pool.
1704 * if <count> is greater than 1, then we can't */
1705 count = 0;
1706 list_for_each_entry(requester, &srv->resolution->requester.wait, list) {
1707 ++count;
1708 if (count > 1)
1709 break;
1710 }
1711 list_for_each_entry(requester, &srv->resolution->requester.curr, list) {
1712 ++count;
1713 if (count > 1)
1714 break;
1715 }
1716 if (count <= 1) {
1717 /* move the resolution back to the pool */
1718 dns_resolution_free(srv->resolvers, srv->resolution);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001719 return;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001720 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001721
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001722 dns_rm_requester_from_resolution(srv->dns_requester, srv->resolution);
1723
1724 return;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001725}
1726
1727/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001728 * Copy <src> server settings to <srv> server allocating
1729 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001730 * This function is not supposed to be called at any time, but only
1731 * during server settings parsing or during server allocations from
1732 * a server template, and just after having calloc()'ed a new server.
1733 * So, <src> may only be a default server (when parsing server settings)
1734 * or a server template (during server allocations from a server template).
1735 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1736 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001737 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001738static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001739{
1740 /* Connection source settings copy */
1741 srv_conn_src_cpy(srv, src);
1742
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001743 if (srv_tmpl) {
1744 srv->addr = src->addr;
1745 srv->svc_port = src->svc_port;
1746 }
1747
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001748 srv->pp_opts = src->pp_opts;
1749 if (src->rdr_pfx != NULL) {
1750 srv->rdr_pfx = strdup(src->rdr_pfx);
1751 srv->rdr_len = src->rdr_len;
1752 }
1753 if (src->cookie != NULL) {
1754 srv->cookie = strdup(src->cookie);
1755 srv->cklen = src->cklen;
1756 }
1757 srv->use_ssl = src->use_ssl;
1758 srv->check.addr = srv->agent.addr = src->check.addr;
1759 srv->check.use_ssl = src->check.use_ssl;
1760 srv->check.port = src->check.port;
1761 /* Note: 'flags' field has potentially been already initialized. */
1762 srv->flags |= src->flags;
1763 srv->do_check = src->do_check;
1764 srv->do_agent = src->do_agent;
1765 if (srv->check.port)
1766 srv->flags |= SRV_F_CHECKPORT;
1767 srv->check.inter = src->check.inter;
1768 srv->check.fastinter = src->check.fastinter;
1769 srv->check.downinter = src->check.downinter;
1770 srv->agent.use_ssl = src->agent.use_ssl;
1771 srv->agent.port = src->agent.port;
1772 if (src->agent.send_string != NULL)
1773 srv->agent.send_string = strdup(src->agent.send_string);
1774 srv->agent.send_string_len = src->agent.send_string_len;
1775 srv->agent.inter = src->agent.inter;
1776 srv->agent.fastinter = src->agent.fastinter;
1777 srv->agent.downinter = src->agent.downinter;
1778 srv->maxqueue = src->maxqueue;
1779 srv->minconn = src->minconn;
1780 srv->maxconn = src->maxconn;
1781 srv->slowstart = src->slowstart;
1782 srv->observe = src->observe;
1783 srv->onerror = src->onerror;
1784 srv->onmarkeddown = src->onmarkeddown;
1785 srv->onmarkedup = src->onmarkedup;
1786 if (src->trackit != NULL)
1787 srv->trackit = strdup(src->trackit);
1788 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1789 srv->uweight = srv->iweight = src->iweight;
1790
1791 srv->check.send_proxy = src->check.send_proxy;
1792 /* health: up, but will fall down at first failure */
1793 srv->check.rise = srv->check.health = src->check.rise;
1794 srv->check.fall = src->check.fall;
1795
1796 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001797 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1798 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1799 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001800 srv->check.state |= CHK_ST_PAUSED;
1801 srv->check.health = 0;
1802 }
1803
1804 /* health: up but will fall down at first failure */
1805 srv->agent.rise = srv->agent.health = src->agent.rise;
1806 srv->agent.fall = src->agent.fall;
1807
1808 if (src->resolvers_id != NULL)
1809 srv->resolvers_id = strdup(src->resolvers_id);
1810 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1811 if (srv->dns_opts.family_prio == AF_UNSPEC)
1812 srv->dns_opts.family_prio = AF_INET6;
1813 memcpy(srv->dns_opts.pref_net,
1814 src->dns_opts.pref_net,
1815 sizeof srv->dns_opts.pref_net);
1816 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1817
1818 srv->init_addr_methods = src->init_addr_methods;
1819 srv->init_addr = src->init_addr;
1820#if defined(USE_OPENSSL)
1821 srv_ssl_settings_cpy(srv, src);
1822#endif
1823#ifdef TCP_USER_TIMEOUT
1824 srv->tcp_ut = src->tcp_ut;
1825#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001826 if (srv_tmpl)
1827 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001828}
1829
1830static struct server *new_server(struct proxy *proxy)
1831{
1832 struct server *srv;
1833
1834 srv = calloc(1, sizeof *srv);
1835 if (!srv)
1836 return NULL;
1837
1838 srv->obj_type = OBJ_TYPE_SERVER;
1839 srv->proxy = proxy;
1840 LIST_INIT(&srv->actconns);
1841 LIST_INIT(&srv->pendconns);
1842 LIST_INIT(&srv->priv_conns);
1843 LIST_INIT(&srv->idle_conns);
1844 LIST_INIT(&srv->safe_conns);
1845
Emeric Brun52a91d32017-08-31 14:41:55 +02001846 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001847 srv->last_change = now.tv_sec;
1848
1849 srv->check.status = HCHK_STATUS_INI;
1850 srv->check.server = srv;
1851 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1852
1853 srv->agent.status = HCHK_STATUS_INI;
1854 srv->agent.server = srv;
1855 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1856
1857 return srv;
1858}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001859
1860/*
1861 * Validate <srv> server health-check settings.
1862 * Returns 0 if everything is OK, -1 if not.
1863 */
1864static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1865{
1866 struct tcpcheck_rule *r = NULL;
1867 struct list *l;
1868
1869 /*
1870 * We need at least a service port, a check port or the first tcp-check rule must
1871 * be a 'connect' one when checking an IPv4/IPv6 server.
1872 */
1873 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1874 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1875 return 0;
1876
1877 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1878 if (!r) {
1879 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1880 "Check has been disabled.\n",
1881 file, linenum, srv->id);
1882 return -1;
1883 }
1884
1885 /* search the first action (connect / send / expect) in the list */
1886 l = &srv->proxy->tcpcheck_rules;
1887 list_for_each_entry(r, l, list) {
1888 if (r->action != TCPCHK_ACT_COMMENT)
1889 break;
1890 }
1891
1892 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1893 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1894 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1895 file, linenum, srv->id);
1896 return -1;
1897 }
1898
1899 /* scan the tcp-check ruleset to ensure a port has been configured */
1900 l = &srv->proxy->tcpcheck_rules;
1901 list_for_each_entry(r, l, list) {
1902 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1903 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1904 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1905 file, linenum, srv->id);
1906 return -1;
1907 }
1908 }
1909
1910 return 0;
1911}
1912
1913/*
1914 * Initialize <srv> health-check structure.
1915 * Returns the error string in case of memory allocation failure, NULL if not.
1916 */
1917static const char *do_health_check_init(struct server *srv, int check_type, int state)
1918{
1919 const char *ret;
1920
1921 if (!srv->do_check)
1922 return NULL;
1923
1924 ret = init_check(&srv->check, check_type);
1925 if (ret)
1926 return ret;
1927
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001928 srv->check.state |= state;
1929 global.maxsock++;
1930
1931 return NULL;
1932}
1933
1934static int server_health_check_init(const char *file, int linenum,
1935 struct server *srv, struct proxy *curproxy)
1936{
1937 const char *ret;
1938
1939 if (!srv->do_check)
1940 return 0;
1941
1942 if (srv->trackit) {
1943 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1944 file, linenum);
1945 return ERR_ALERT | ERR_FATAL;
1946 }
1947
1948 if (server_healthcheck_validate(file, linenum, srv) < 0)
1949 return ERR_ALERT | ERR_ABORT;
1950
1951 /* note: check type will be set during the config review phase */
1952 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1953 if (ret) {
1954 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1955 return ERR_ALERT | ERR_ABORT;
1956 }
1957
1958 return 0;
1959}
1960
1961/*
1962 * Initialize <srv> agent check structure.
1963 * Returns the error string in case of memory allocation failure, NULL if not.
1964 */
1965static const char *do_server_agent_check_init(struct server *srv, int state)
1966{
1967 const char *ret;
1968
1969 if (!srv->do_agent)
1970 return NULL;
1971
1972 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1973 if (ret)
1974 return ret;
1975
1976 if (!srv->agent.inter)
1977 srv->agent.inter = srv->check.inter;
1978
1979 srv->agent.state |= state;
1980 global.maxsock++;
1981
1982 return NULL;
1983}
1984
1985static int server_agent_check_init(const char *file, int linenum,
1986 struct server *srv, struct proxy *curproxy)
1987{
1988 const char *ret;
1989
1990 if (!srv->do_agent)
1991 return 0;
1992
1993 if (!srv->agent.port) {
1994 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1995 file, linenum, srv->id);
1996 return ERR_ALERT | ERR_FATAL;
1997 }
1998
1999 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
2000 if (ret) {
2001 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2002 return ERR_ALERT | ERR_ABORT;
2003 }
2004
2005 return 0;
2006}
2007
2008#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2009static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2010 struct server *srv, struct proxy *proxy)
2011{
2012 int ret;
2013 char *err = NULL;
2014
2015 if (!srv->sni_expr)
2016 return 0;
2017
2018 ret = server_parse_sni_expr(srv, proxy, &err);
2019 if (!ret)
2020 return 0;
2021
2022 display_parser_err(file, linenum, args, cur_arg, &err);
2023 free(err);
2024
2025 return ret;
2026}
2027#endif
2028
2029/*
2030 * Server initializations finalization.
2031 * Initialize health check, agent check and SNI expression if enabled.
2032 * Must not be called for a default server instance.
2033 */
2034static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2035 struct server *srv, struct proxy *px)
2036{
2037 int ret;
2038
2039 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2040 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2041 return ret;
2042 }
2043
2044#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2045 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2046 return ret;
2047#endif
2048
2049 if (srv->flags & SRV_F_BACKUP)
2050 px->srv_bck++;
2051 else
2052 px->srv_act++;
2053 srv_lb_commit_status(srv);
2054
2055 return 0;
2056}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002057
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002058/*
2059 * Parse as much as possible such a range string argument: low[-high]
2060 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2061 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2062 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2063 * Returns 0 if succeeded, -1 if not.
2064 */
2065static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2066{
2067 char *nb_high_arg;
2068
2069 *nb_high = 0;
2070 chunk_printf(&trash, "%s", arg);
2071 *nb_low = atoi(trash.str);
2072
2073 if ((nb_high_arg = strchr(trash.str, '-'))) {
2074 *nb_high_arg++ = '\0';
2075 *nb_high = atoi(nb_high_arg);
2076 }
2077 else {
2078 *nb_high += *nb_low;
2079 *nb_low = 1;
2080 }
2081
2082 if (*nb_low < 0 || *nb_high < *nb_low)
2083 return -1;
2084
2085 return 0;
2086}
2087
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002088static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2089{
2090 chunk_printf(&trash, "%s%d", prefix, nb);
2091 free(srv->id);
2092 srv->id = strdup(trash.str);
2093}
2094
2095/*
2096 * Initialize as much as possible servers from <srv> server template.
2097 * Note that a server template is a special server with
2098 * a few different parameters than a server which has
2099 * been parsed mostly the same way as a server.
2100 * Returns the number of servers succesfully allocated,
2101 * 'srv' template included.
2102 */
2103static int server_template_init(struct server *srv, struct proxy *px)
2104{
2105 int i;
2106 struct server *newsrv;
2107
2108 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2109 int check_init_state;
2110 int agent_init_state;
2111
2112 newsrv = new_server(px);
2113 if (!newsrv)
2114 goto err;
2115
2116 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002117 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002118#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2119 if (newsrv->sni_expr) {
2120 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2121 if (!newsrv->ssl_ctx.sni)
2122 goto err;
2123 }
2124#endif
2125 /* Set this new server ID. */
2126 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2127
2128 /* Initial checks states. */
2129 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2130 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2131
2132 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2133 do_server_agent_check_init(newsrv, agent_init_state))
2134 goto err;
2135
2136 /* Linked backwards first. This will be restablished after parsing. */
2137 newsrv->next = px->srv;
2138 px->srv = newsrv;
2139 }
2140 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2141
2142 return i - srv->tmpl_info.nb_low;
2143
2144 err:
2145 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2146 if (newsrv) {
2147#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2148 release_sample_expr(newsrv->ssl_ctx.sni);
2149#endif
2150 free_check(&newsrv->agent);
2151 free_check(&newsrv->check);
2152 }
2153 free(newsrv);
2154 return i - srv->tmpl_info.nb_low;
2155}
2156
Willy Tarreau272adea2014-03-31 10:39:59 +02002157int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2158{
2159 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002160 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002161 char *errmsg = NULL;
2162 int err_code = 0;
2163 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002164 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002165
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002166 if (!strcmp(args[0], "server") ||
2167 !strcmp(args[0], "default-server") ||
2168 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002169 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002170 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002171 int srv = !defsrv && !strcmp(args[0], "server");
2172 int srv_tmpl = !defsrv && !srv;
2173 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002174
2175 if (!defsrv && curproxy == defproxy) {
2176 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2177 err_code |= ERR_ALERT | ERR_FATAL;
2178 goto out;
2179 }
2180 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
2181 err_code |= ERR_ALERT | ERR_FATAL;
2182
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002183 /* There is no mandatory first arguments for default server. */
2184 if (srv) {
2185 if (!*args[2]) {
2186 /* 'server' line number of argument check. */
2187 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2188 file, linenum, args[0]);
2189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
2191 }
2192
2193 err = invalid_char(args[1]);
2194 }
2195 else if (srv_tmpl) {
2196 if (!*args[3]) {
2197 /* 'server-template' line number of argument check. */
2198 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2199 file, linenum, args[0]);
2200 err_code |= ERR_ALERT | ERR_FATAL;
2201 goto out;
2202 }
2203
2204 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002205 }
2206
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002207 if (err) {
2208 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
2209 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002210 err_code |= ERR_ALERT | ERR_FATAL;
2211 goto out;
2212 }
2213
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002214 cur_arg = 2;
2215 if (srv_tmpl) {
2216 /* Parse server-template <nb | range> arg. */
2217 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
2218 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
2219 file, linenum, args[0], args[cur_arg]);
2220 err_code |= ERR_ALERT | ERR_FATAL;
2221 goto out;
2222 }
2223 cur_arg++;
2224 }
2225
Willy Tarreau272adea2014-03-31 10:39:59 +02002226 if (!defsrv) {
2227 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002228 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002229 struct protocol *proto;
2230
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002231 newsrv = new_server(curproxy);
2232 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002233 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2234 err_code |= ERR_ALERT | ERR_ABORT;
2235 goto out;
2236 }
2237
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002238 if (srv_tmpl) {
2239 newsrv->tmpl_info.nb_low = tmpl_range_low;
2240 newsrv->tmpl_info.nb_high = tmpl_range_high;
2241 }
2242
Willy Tarreau272adea2014-03-31 10:39:59 +02002243 /* the servers are linked backwards first */
2244 newsrv->next = curproxy->srv;
2245 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002246 newsrv->conf.file = strdup(file);
2247 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002248 /* Note: for a server template, its id is its prefix.
2249 * This is a temporary id which will be used for server allocations to come
2250 * after parsing.
2251 */
2252 if (srv)
2253 newsrv->id = strdup(args[1]);
2254 else
2255 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002256
2257 /* several ways to check the port component :
2258 * - IP => port=+0, relative (IPv4 only)
2259 * - IP: => port=+0, relative
2260 * - IP:N => port=N, absolute
2261 * - IP:+N => port=+N, relative
2262 * - IP:-N => port=-N, relative
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) {
2266 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
2267 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)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002273 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
2274 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 */
2285 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
2286 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) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002293 if (fqdn[0] == '_') {
2294 struct dns_srvrq *srvrq = NULL;
2295 int found = 0;
2296 /* SRV record */
2297 /* Check if a SRV request already exists, and if not, create it */
2298 list_for_each_entry(srvrq, &curproxy->srvrq_list, list) {
2299 if (!strcmp(srvrq->name, fqdn)) {
2300 found = 1;
2301 break;
2302 }
2303 }
2304 if (found == 0) {
2305 int hostname_dn_len;
2306
2307 srvrq = calloc(1, sizeof(*srvrq));
2308 if (!srvrq) {
2309 Alert("Failed to allocate memory");
2310 err_code = ERR_ALERT | ERR_FATAL;
2311 goto out;
2312 }
2313 srvrq->obj_type = OBJ_TYPE_SRVRQ;
2314 srvrq->proxy = proxy;
2315 srvrq->name = strdup(fqdn);
2316 srvrq->inter = 2000;
2317 hostname_dn_len = dns_str_to_dn_label_len(fqdn);
2318 if (hostname_dn_len == -1) {
2319 Alert("Failed to parse domaine name '%s'", fqdn);
2320 err_code = ERR_ALERT | ERR_FATAL;
2321 goto out;
2322 }
2323 srvrq->hostname_dn = malloc(hostname_dn_len + 1);
2324 srvrq->hostname_dn_len = hostname_dn_len;
2325 if (!srvrq->hostname_dn) {
2326 Alert("Failed to alloc memory");
2327 err_code = ERR_ALERT | ERR_FATAL;
2328 goto out;
2329 }
2330 if (!dns_str_to_dn_label(fqdn,
2331 srvrq->hostname_dn,
2332 hostname_dn_len + 1)) {
2333 Alert("Failed to parse domain name '%s'", fqdn);
2334 err_code = ERR_ALERT | ERR_FATAL;
2335 goto out;
2336 }
2337 LIST_ADDQ(&proxy->srvrq_list, &srvrq->list);
2338
2339 }
2340 newsrv->srvrq = srvrq;
2341
2342
2343 } else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2344 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2345 file, linenum, newsrv->id);
2346 err_code |= ERR_ALERT | ERR_FATAL;
2347 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002348 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002349 }
2350
Willy Tarreau272adea2014-03-31 10:39:59 +02002351 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002352 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002353
Olivier Houchard8da5f982017-08-04 18:35:36 +02002354 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002355 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002356 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002357 err_code |= ERR_ALERT | ERR_FATAL;
2358 goto out;
2359 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002360
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002361 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002362 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002363 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002364 } else {
2365 newsrv = &curproxy->defsrv;
2366 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002367 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002368 }
2369
2370 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002371 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002372 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2373 if (err) {
2374 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2375 file, linenum, *err, newsrv->id);
2376 err_code |= ERR_ALERT | ERR_FATAL;
2377 goto out;
2378 }
2379 if (val <= 0) {
2380 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2381 file, linenum, val, args[cur_arg], newsrv->id);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 newsrv->agent.inter = val;
2386 cur_arg += 2;
2387 }
Misiekea849332017-01-09 09:39:51 +01002388 else if (!strcmp(args[cur_arg], "agent-addr")) {
2389 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2390 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2391 goto out;
2392 }
2393
2394 cur_arg += 2;
2395 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002396 else if (!strcmp(args[cur_arg], "agent-port")) {
2397 global.maxsock++;
2398 newsrv->agent.port = atol(args[cur_arg + 1]);
2399 cur_arg += 2;
2400 }
James Brown55f9ff12015-10-21 18:19:05 -07002401 else if (!strcmp(args[cur_arg], "agent-send")) {
2402 global.maxsock++;
2403 free(newsrv->agent.send_string);
2404 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2405 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2406 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2407 cur_arg += 2;
2408 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002409 else if (!strcmp(args[cur_arg], "init-addr")) {
2410 char *p, *end;
2411 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002412 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002413
2414 newsrv->init_addr_methods = 0;
2415 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2416
2417 for (p = args[cur_arg + 1]; *p; p = end) {
2418 /* cut on next comma */
2419 for (end = p; *end && *end != ','; end++);
2420 if (*end)
2421 *(end++) = 0;
2422
Willy Tarreau4310d362016-11-02 15:05:56 +01002423 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002424 if (!strcmp(p, "libc")) {
2425 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2426 }
2427 else if (!strcmp(p, "last")) {
2428 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2429 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002430 else if (!strcmp(p, "none")) {
2431 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2432 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002433 else if (str2ip2(p, &sa, 0)) {
2434 if (is_addr(&newsrv->init_addr)) {
2435 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2436 file, linenum, args[cur_arg], p);
2437 err_code |= ERR_ALERT | ERR_FATAL;
2438 goto out;
2439 }
2440 newsrv->init_addr = sa;
2441 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2442 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002443 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002444 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002445 file, linenum, args[cur_arg], p);
2446 err_code |= ERR_ALERT | ERR_FATAL;
2447 goto out;
2448 }
2449 if (!done) {
2450 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2451 file, linenum, args[cur_arg], p);
2452 err_code |= ERR_ALERT | ERR_FATAL;
2453 goto out;
2454 }
2455 }
2456 cur_arg += 2;
2457 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002458 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002459 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002460 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2461 cur_arg += 2;
2462 }
2463 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 {
2469 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2470 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') {
2482 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2483 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') {
2496 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002497 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002498 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2499 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. */
2521 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2522 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]) {
2534 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2535 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) {
2542 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2543 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]) {
2556 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2557 file, linenum, args[cur_arg]);
2558 err_code |= ERR_ALERT | ERR_FATAL;
2559 goto out;
2560 }
2561
2562 if (newsrv->check.fall <= 0) {
2563 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2564 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);
2573 if (err) {
2574 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2575 file, linenum, *err, newsrv->id);
2576 err_code |= ERR_ALERT | ERR_FATAL;
2577 goto out;
2578 }
2579 if (val <= 0) {
2580 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2581 file, linenum, val, args[cur_arg], newsrv->id);
2582 err_code |= ERR_ALERT | ERR_FATAL;
2583 goto out;
2584 }
2585 newsrv->check.inter = val;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002586 if (newsrv->srvrq)
2587 newsrv->srvrq->inter = val;
Willy Tarreau272adea2014-03-31 10:39:59 +02002588 cur_arg += 2;
2589 }
2590 else if (!strcmp(args[cur_arg], "fastinter")) {
2591 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2592 if (err) {
2593 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2594 file, linenum, *err, newsrv->id);
2595 err_code |= ERR_ALERT | ERR_FATAL;
2596 goto out;
2597 }
2598 if (val <= 0) {
2599 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2600 file, linenum, val, args[cur_arg], newsrv->id);
2601 err_code |= ERR_ALERT | ERR_FATAL;
2602 goto out;
2603 }
2604 newsrv->check.fastinter = val;
2605 cur_arg += 2;
2606 }
2607 else if (!strcmp(args[cur_arg], "downinter")) {
2608 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2609 if (err) {
2610 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2611 file, linenum, *err, newsrv->id);
2612 err_code |= ERR_ALERT | ERR_FATAL;
2613 goto out;
2614 }
2615 if (val <= 0) {
2616 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2617 file, linenum, val, args[cur_arg], newsrv->id);
2618 err_code |= ERR_ALERT | ERR_FATAL;
2619 goto out;
2620 }
2621 newsrv->check.downinter = val;
2622 cur_arg += 2;
2623 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002624 else if (!strcmp(args[cur_arg], "port")) {
2625 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002626 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002627 cur_arg += 2;
2628 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002629 else if (!strcmp(args[cur_arg], "weight")) {
2630 int w;
2631 w = atol(args[cur_arg + 1]);
2632 if (w < 0 || w > SRV_UWGHT_MAX) {
2633 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2634 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2635 err_code |= ERR_ALERT | ERR_FATAL;
2636 goto out;
2637 }
2638 newsrv->uweight = newsrv->iweight = w;
2639 cur_arg += 2;
2640 }
2641 else if (!strcmp(args[cur_arg], "minconn")) {
2642 newsrv->minconn = atol(args[cur_arg + 1]);
2643 cur_arg += 2;
2644 }
2645 else if (!strcmp(args[cur_arg], "maxconn")) {
2646 newsrv->maxconn = atol(args[cur_arg + 1]);
2647 cur_arg += 2;
2648 }
2649 else if (!strcmp(args[cur_arg], "maxqueue")) {
2650 newsrv->maxqueue = atol(args[cur_arg + 1]);
2651 cur_arg += 2;
2652 }
2653 else if (!strcmp(args[cur_arg], "slowstart")) {
2654 /* slowstart is stored in seconds */
2655 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2656 if (err) {
2657 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2658 file, linenum, *err, newsrv->id);
2659 err_code |= ERR_ALERT | ERR_FATAL;
2660 goto out;
2661 }
2662 newsrv->slowstart = (val + 999) / 1000;
2663 cur_arg += 2;
2664 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002665 else if (!strcmp(args[cur_arg], "on-error")) {
2666 if (!strcmp(args[cur_arg + 1], "fastinter"))
2667 newsrv->onerror = HANA_ONERR_FASTINTER;
2668 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2669 newsrv->onerror = HANA_ONERR_FAILCHK;
2670 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2671 newsrv->onerror = HANA_ONERR_SUDDTH;
2672 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2673 newsrv->onerror = HANA_ONERR_MARKDWN;
2674 else {
2675 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2676 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2677 file, linenum, args[cur_arg], args[cur_arg + 1]);
2678 err_code |= ERR_ALERT | ERR_FATAL;
2679 goto out;
2680 }
2681
2682 cur_arg += 2;
2683 }
2684 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2685 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2686 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2687 else {
2688 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2689 file, linenum, args[cur_arg], args[cur_arg + 1]);
2690 err_code |= ERR_ALERT | ERR_FATAL;
2691 goto out;
2692 }
2693
2694 cur_arg += 2;
2695 }
2696 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2697 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2698 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2699 else {
2700 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2701 file, linenum, args[cur_arg], args[cur_arg + 1]);
2702 err_code |= ERR_ALERT | ERR_FATAL;
2703 goto out;
2704 }
2705
2706 cur_arg += 2;
2707 }
2708 else if (!strcmp(args[cur_arg], "error-limit")) {
2709 if (!*args[cur_arg + 1]) {
2710 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2711 file, linenum, args[cur_arg]);
2712 err_code |= ERR_ALERT | ERR_FATAL;
2713 goto out;
2714 }
2715
2716 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2717
2718 if (newsrv->consecutive_errors_limit <= 0) {
2719 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2720 file, linenum, args[cur_arg]);
2721 err_code |= ERR_ALERT | ERR_FATAL;
2722 goto out;
2723 }
2724 cur_arg += 2;
2725 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002726 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002727 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2728 file, linenum, "usesrc", "source");
2729 err_code |= ERR_ALERT | ERR_FATAL;
2730 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002731 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002732 else {
2733 static int srv_dumped;
2734 struct srv_kw *kw;
2735 char *err;
2736
2737 kw = srv_find_kw(args[cur_arg]);
2738 if (kw) {
2739 char *err = NULL;
2740 int code;
2741
2742 if (!kw->parse) {
2743 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2744 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002745 if (kw->skip != -1)
2746 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002747 err_code |= ERR_ALERT | ERR_FATAL;
2748 goto out;
2749 }
2750
2751 if (defsrv && !kw->default_ok) {
2752 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2753 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002754 if (kw->skip != -1)
2755 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002756 err_code |= ERR_ALERT;
2757 continue;
2758 }
2759
2760 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2761 err_code |= code;
2762
2763 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002764 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002765 if (code & ERR_FATAL) {
2766 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002767 if (kw->skip != -1)
2768 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002769 goto out;
2770 }
2771 }
2772 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002773 if (kw->skip != -1)
2774 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002775 continue;
2776 }
2777
2778 err = NULL;
2779 if (!srv_dumped) {
2780 srv_dump_kws(&err);
2781 indent_msg(&err, 4);
2782 srv_dumped = 1;
2783 }
2784
2785 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2786 file, linenum, args[0], args[1], args[cur_arg],
2787 err ? " Registered keywords :" : "", err ? err : "");
2788 free(err);
2789
2790 err_code |= ERR_ALERT | ERR_FATAL;
2791 goto out;
2792 }
2793 }
2794
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002795 if (!defsrv)
2796 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2797 if (err_code & ERR_FATAL)
2798 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002799 if (srv_tmpl)
2800 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002801 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002802 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002803 return 0;
2804
2805 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002806 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002807 free(errmsg);
2808 return err_code;
2809}
2810
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002811/* Returns a pointer to the first server matching either id <id>.
2812 * NULL is returned if no match is found.
2813 * the lookup is performed in the backend <bk>
2814 */
2815struct server *server_find_by_id(struct proxy *bk, int id)
2816{
2817 struct eb32_node *eb32;
2818 struct server *curserver;
2819
2820 if (!bk || (id ==0))
2821 return NULL;
2822
2823 /* <bk> has no backend capabilities, so it can't have a server */
2824 if (!(bk->cap & PR_CAP_BE))
2825 return NULL;
2826
2827 curserver = NULL;
2828
2829 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2830 if (eb32)
2831 curserver = container_of(eb32, struct server, conf.id);
2832
2833 return curserver;
2834}
2835
2836/* Returns a pointer to the first server matching either name <name>, or id
2837 * if <name> starts with a '#'. NULL is returned if no match is found.
2838 * the lookup is performed in the backend <bk>
2839 */
2840struct server *server_find_by_name(struct proxy *bk, const char *name)
2841{
2842 struct server *curserver;
2843
2844 if (!bk || !name)
2845 return NULL;
2846
2847 /* <bk> has no backend capabilities, so it can't have a server */
2848 if (!(bk->cap & PR_CAP_BE))
2849 return NULL;
2850
2851 curserver = NULL;
2852 if (*name == '#') {
2853 curserver = server_find_by_id(bk, atoi(name + 1));
2854 if (curserver)
2855 return curserver;
2856 }
2857 else {
2858 curserver = bk->srv;
2859
2860 while (curserver && (strcmp(curserver->id, name) != 0))
2861 curserver = curserver->next;
2862
2863 if (curserver)
2864 return curserver;
2865 }
2866
2867 return NULL;
2868}
2869
2870struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2871{
2872 struct server *byname;
2873 struct server *byid;
2874
2875 if (!name && !id)
2876 return NULL;
2877
2878 if (diff)
2879 *diff = 0;
2880
2881 byname = byid = NULL;
2882
2883 if (name) {
2884 byname = server_find_by_name(bk, name);
2885 if (byname && (!id || byname->puid == id))
2886 return byname;
2887 }
2888
2889 /* remaining possibilities :
2890 * - name not set
2891 * - name set but not found
2892 * - name found but ID doesn't match
2893 */
2894 if (id) {
2895 byid = server_find_by_id(bk, id);
2896 if (byid) {
2897 if (byname) {
2898 /* use id only if forced by configuration */
2899 if (byid->flags & SRV_F_FORCED_ID) {
2900 if (diff)
2901 *diff |= 2;
2902 return byid;
2903 }
2904 else {
2905 if (diff)
2906 *diff |= 1;
2907 return byname;
2908 }
2909 }
2910
2911 /* remaining possibilities:
2912 * - name not set
2913 * - name set but not found
2914 */
2915 if (name && diff)
2916 *diff |= 2;
2917 return byid;
2918 }
2919
2920 /* id bot found */
2921 if (byname) {
2922 if (diff)
2923 *diff |= 1;
2924 return byname;
2925 }
2926 }
2927
2928 return NULL;
2929}
2930
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002931/* Update a server state using the parameters available in the params list */
2932static void srv_update_state(struct server *srv, int version, char **params)
2933{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002934 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002935 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002936
2937 /* fields since version 1
2938 * and common to all other upcoming versions
2939 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002940 enum srv_state srv_op_state;
2941 enum srv_admin srv_admin_state;
2942 unsigned srv_uweight, srv_iweight;
2943 unsigned long srv_last_time_change;
2944 short srv_check_status;
2945 enum chk_result srv_check_result;
2946 int srv_check_health;
2947 int srv_check_state, srv_agent_state;
2948 int bk_f_forced_id;
2949 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002950 int fqdn_set_by_cli;
2951 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002952 const char *port_str;
2953 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002954
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002955 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002956 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002957 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002958 switch (version) {
2959 case 1:
2960 /*
2961 * now we can proceed with server's state update:
2962 * srv_addr: params[0]
2963 * srv_op_state: params[1]
2964 * srv_admin_state: params[2]
2965 * srv_uweight: params[3]
2966 * srv_iweight: params[4]
2967 * srv_last_time_change: params[5]
2968 * srv_check_status: params[6]
2969 * srv_check_result: params[7]
2970 * srv_check_health: params[8]
2971 * srv_check_state: params[9]
2972 * srv_agent_state: params[10]
2973 * bk_f_forced_id: params[11]
2974 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002975 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002976 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002977 */
2978
2979 /* validating srv_op_state */
2980 p = NULL;
2981 errno = 0;
2982 srv_op_state = strtol(params[1], &p, 10);
2983 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2984 (srv_op_state != SRV_ST_STOPPED &&
2985 srv_op_state != SRV_ST_STARTING &&
2986 srv_op_state != SRV_ST_RUNNING &&
2987 srv_op_state != SRV_ST_STOPPING)) {
2988 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2989 }
2990
2991 /* validating srv_admin_state */
2992 p = NULL;
2993 errno = 0;
2994 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002995 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002996
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002997 /* inherited statuses will be recomputed later.
2998 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2999 */
3000 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003001
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003002 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3003 (srv_admin_state != 0 &&
3004 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003005 srv_admin_state != SRV_ADMF_CMAINT &&
3006 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003007 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003008 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003009 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3010 }
3011
3012 /* validating srv_uweight */
3013 p = NULL;
3014 errno = 0;
3015 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003016 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003017 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3018
3019 /* validating srv_iweight */
3020 p = NULL;
3021 errno = 0;
3022 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003023 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003024 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3025
3026 /* validating srv_last_time_change */
3027 p = NULL;
3028 errno = 0;
3029 srv_last_time_change = strtol(params[5], &p, 10);
3030 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3031 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3032
3033 /* validating srv_check_status */
3034 p = NULL;
3035 errno = 0;
3036 srv_check_status = strtol(params[6], &p, 10);
3037 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3038 (srv_check_status >= HCHK_STATUS_SIZE))
3039 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3040
3041 /* validating srv_check_result */
3042 p = NULL;
3043 errno = 0;
3044 srv_check_result = strtol(params[7], &p, 10);
3045 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3046 (srv_check_result != CHK_RES_UNKNOWN &&
3047 srv_check_result != CHK_RES_NEUTRAL &&
3048 srv_check_result != CHK_RES_FAILED &&
3049 srv_check_result != CHK_RES_PASSED &&
3050 srv_check_result != CHK_RES_CONDPASS)) {
3051 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3052 }
3053
3054 /* validating srv_check_health */
3055 p = NULL;
3056 errno = 0;
3057 srv_check_health = strtol(params[8], &p, 10);
3058 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3059 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3060
3061 /* validating srv_check_state */
3062 p = NULL;
3063 errno = 0;
3064 srv_check_state = strtol(params[9], &p, 10);
3065 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3066 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3067 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3068
3069 /* validating srv_agent_state */
3070 p = NULL;
3071 errno = 0;
3072 srv_agent_state = strtol(params[10], &p, 10);
3073 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3074 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3075 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3076
3077 /* validating bk_f_forced_id */
3078 p = NULL;
3079 errno = 0;
3080 bk_f_forced_id = strtol(params[11], &p, 10);
3081 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3082 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3083
3084 /* validating srv_f_forced_id */
3085 p = NULL;
3086 errno = 0;
3087 srv_f_forced_id = strtol(params[12], &p, 10);
3088 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3089 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3090
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003091 /* validating srv_fqdn */
3092 fqdn = params[13];
3093 if (fqdn && *fqdn == '-')
3094 fqdn = NULL;
3095 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3096 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3097 fqdn = NULL;
3098 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003099
Frédéric Lécaille31694712017-08-01 08:47:19 +02003100 port_str = params[14];
3101 if (port_str) {
3102 port = strl2uic(port_str, strlen(port_str));
3103 if (port > USHRT_MAX) {
3104 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3105 port_str = NULL;
3106 }
3107 }
3108
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003109 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02003110 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003111 goto out;
3112
3113 /* recover operational state and apply it to this server
3114 * and all servers tracking this one */
3115 switch (srv_op_state) {
3116 case SRV_ST_STOPPED:
3117 srv->check.health = 0;
3118 srv_set_stopped(srv, "changed from server-state after a reload");
3119 break;
3120 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02003121 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003122 break;
3123 case SRV_ST_STOPPING:
3124 srv->check.health = srv->check.rise + srv->check.fall - 1;
3125 srv_set_stopping(srv, "changed from server-state after a reload");
3126 break;
3127 case SRV_ST_RUNNING:
3128 srv->check.health = srv->check.rise + srv->check.fall - 1;
3129 srv_set_running(srv, "");
3130 break;
3131 }
3132
3133 /* When applying server state, the following rules apply:
3134 * - in case of a configuration change, we apply the setting from the new
3135 * configuration, regardless of old running state
3136 * - if no configuration change, we apply old running state only if old running
3137 * state is different from new configuration state
3138 */
3139 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003140 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3141 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142 srv_adm_set_maint(srv);
3143 else
3144 srv_adm_set_ready(srv);
3145 }
3146 /* configuration is the same, let's compate old running state and new conf state */
3147 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003148 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003149 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003150 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003151 srv_adm_set_ready(srv);
3152 }
3153 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003154 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003155 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003156 * (srv->iweight is the weight set up in configuration).
3157 * There are two possible reasons for FDRAIN to have been present :
3158 * - previous config weight was zero
3159 * - "set server b/s drain" was sent to the CLI
3160 *
3161 * In the first case, we simply want to drop this drain state
3162 * if the new weight is not zero anymore, meaning the administrator
3163 * has intentionally turned the weight back to a positive value to
3164 * enable the server again after an operation. In the second case,
3165 * the drain state was forced on the CLI regardless of the config's
3166 * weight so we don't want a change to the config weight to lose this
3167 * status. What this means is :
3168 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3169 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003171 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003172 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003173 }
3174
3175 srv->last_change = date.tv_sec - srv_last_time_change;
3176 srv->check.status = srv_check_status;
3177 srv->check.result = srv_check_result;
3178 srv->check.health = srv_check_health;
3179
3180 /* Only case we want to apply is removing ENABLED flag which could have been
3181 * done by the "disable health" command over the stats socket
3182 */
3183 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3184 (srv_check_state & CHK_ST_CONFIGURED) &&
3185 !(srv_check_state & CHK_ST_ENABLED))
3186 srv->check.state &= ~CHK_ST_ENABLED;
3187
3188 /* Only case we want to apply is removing ENABLED flag which could have been
3189 * done by the "disable agent" command over the stats socket
3190 */
3191 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3192 (srv_agent_state & CHK_ST_CONFIGURED) &&
3193 !(srv_agent_state & CHK_ST_ENABLED))
3194 srv->agent.state &= ~CHK_ST_ENABLED;
3195
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003196 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3197 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003198 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003199 * It means that a configuration file change has precedence over a unix socket change
3200 * for server's weight
3201 *
3202 * by default, HAProxy applies the following weight when parsing the configuration
3203 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003204 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003205 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003206 srv->uweight = srv_uweight;
3207 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003208 server_recalc_eweight(srv);
3209
Willy Tarreaue5a60682016-11-09 14:54:53 +01003210 /* load server IP address */
3211 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003212
3213 if (fqdn && srv->hostname) {
3214 if (!strcmp(srv->hostname, fqdn)) {
3215 /* Here we reset the 'set from stats socket FQDN' flag
3216 * to support such transitions:
3217 * Let's say initial FQDN value is foo1 (in configuration file).
3218 * - FQDN changed from stats socket, from foo1 to foo2 value,
3219 * - FQDN changed again from file configuration (with the same previous value
3220 set from stats socket, from foo1 to foo2 value),
3221 * - reload for any other reason than a FQDN modification,
3222 * the configuration file FQDN matches the fqdn server state file value.
3223 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3224 * any futher FQDN modification.
3225 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003226 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003227 }
3228 else {
3229 /* If the FDQN has been changed from stats socket,
3230 * apply fqdn state file value (which is the value set
3231 * from stats socket).
3232 */
3233 if (fqdn_set_by_cli) {
3234 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02003235 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003236 }
3237 }
3238 }
3239
Frédéric Lécaille31694712017-08-01 08:47:19 +02003240 if (port_str)
3241 srv->svc_port = port;
3242
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003243 break;
3244 default:
3245 chunk_appendf(msg, ", version '%d' not supported", version);
3246 }
3247
3248 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003249 if (msg->len) {
3250 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02003251 Warning("server-state application failed for server '%s/%s'%s",
3252 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003253 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003254}
3255
3256/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3257 * For each proxy, it does the following:
3258 * - opens its server state file (either one or local one)
3259 * - read whole file, line by line
3260 * - analyse each line to check if it matches our current backend:
3261 * - backend name matches
3262 * - backend id matches if id is forced and name doesn't match
3263 * - if the server pointed by the line is found, then state is applied
3264 *
3265 * If the running backend uuid or id differs from the state file, then HAProxy reports
3266 * a warning.
3267 */
3268void apply_server_state(void)
3269{
3270 char *cur, *end;
3271 char mybuf[SRV_STATE_LINE_MAXLEN];
3272 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003273 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3274 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003275 int arg, srv_arg, version, diff;
3276 FILE *f;
3277 char *filepath;
3278 char globalfilepath[MAXPATHLEN + 1];
3279 char localfilepath[MAXPATHLEN + 1];
3280 int len, fileopenerr, globalfilepathlen, localfilepathlen;
3281 extern struct proxy *proxy;
3282 struct proxy *curproxy, *bk;
3283 struct server *srv;
3284
3285 globalfilepathlen = 0;
3286 /* create the globalfilepath variable */
3287 if (global.server_state_file) {
3288 /* absolute path or no base directory provided */
3289 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3290 len = strlen(global.server_state_file);
3291 if (len > MAXPATHLEN) {
3292 globalfilepathlen = 0;
3293 goto globalfileerror;
3294 }
3295 memcpy(globalfilepath, global.server_state_file, len);
3296 globalfilepath[len] = '\0';
3297 globalfilepathlen = len;
3298 }
3299 else if (global.server_state_base) {
3300 len = strlen(global.server_state_base);
3301 globalfilepathlen += len;
3302
3303 if (globalfilepathlen > MAXPATHLEN) {
3304 globalfilepathlen = 0;
3305 goto globalfileerror;
3306 }
3307 strncpy(globalfilepath, global.server_state_base, len);
3308 globalfilepath[globalfilepathlen] = 0;
3309
3310 /* append a slash if needed */
3311 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3312 if (globalfilepathlen + 1 > MAXPATHLEN) {
3313 globalfilepathlen = 0;
3314 goto globalfileerror;
3315 }
3316 globalfilepath[globalfilepathlen++] = '/';
3317 }
3318
3319 len = strlen(global.server_state_file);
3320 if (globalfilepathlen + len > MAXPATHLEN) {
3321 globalfilepathlen = 0;
3322 goto globalfileerror;
3323 }
3324 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3325 globalfilepathlen += len;
3326 globalfilepath[globalfilepathlen++] = 0;
3327 }
3328 }
3329 globalfileerror:
3330 if (globalfilepathlen == 0)
3331 globalfilepath[0] = '\0';
3332
3333 /* read servers state from local file */
3334 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3335 /* servers are only in backends */
3336 if (!(curproxy->cap & PR_CAP_BE))
3337 continue;
3338 fileopenerr = 0;
3339 filepath = NULL;
3340
3341 /* search server state file path and name */
3342 switch (curproxy->load_server_state_from_file) {
3343 /* read servers state from global file */
3344 case PR_SRV_STATE_FILE_GLOBAL:
3345 /* there was an error while generating global server state file path */
3346 if (globalfilepathlen == 0)
3347 continue;
3348 filepath = globalfilepath;
3349 fileopenerr = 1;
3350 break;
3351 /* this backend has its own file */
3352 case PR_SRV_STATE_FILE_LOCAL:
3353 localfilepathlen = 0;
3354 localfilepath[0] = '\0';
3355 len = 0;
3356 /* create the localfilepath variable */
3357 /* absolute path or no base directory provided */
3358 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3359 len = strlen(curproxy->server_state_file_name);
3360 if (len > MAXPATHLEN) {
3361 localfilepathlen = 0;
3362 goto localfileerror;
3363 }
3364 memcpy(localfilepath, curproxy->server_state_file_name, len);
3365 localfilepath[len] = '\0';
3366 localfilepathlen = len;
3367 }
3368 else if (global.server_state_base) {
3369 len = strlen(global.server_state_base);
3370 localfilepathlen += len;
3371
3372 if (localfilepathlen > MAXPATHLEN) {
3373 localfilepathlen = 0;
3374 goto localfileerror;
3375 }
3376 strncpy(localfilepath, global.server_state_base, len);
3377 localfilepath[localfilepathlen] = 0;
3378
3379 /* append a slash if needed */
3380 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3381 if (localfilepathlen + 1 > MAXPATHLEN) {
3382 localfilepathlen = 0;
3383 goto localfileerror;
3384 }
3385 localfilepath[localfilepathlen++] = '/';
3386 }
3387
3388 len = strlen(curproxy->server_state_file_name);
3389 if (localfilepathlen + len > MAXPATHLEN) {
3390 localfilepathlen = 0;
3391 goto localfileerror;
3392 }
3393 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3394 localfilepathlen += len;
3395 localfilepath[localfilepathlen++] = 0;
3396 }
3397 filepath = localfilepath;
3398 localfileerror:
3399 if (localfilepathlen == 0)
3400 localfilepath[0] = '\0';
3401
3402 break;
3403 case PR_SRV_STATE_FILE_NONE:
3404 default:
3405 continue;
3406 }
3407
3408 /* preload global state file */
3409 errno = 0;
3410 f = fopen(filepath, "r");
3411 if (errno && fileopenerr)
3412 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3413 if (!f)
3414 continue;
3415
3416 mybuf[0] = '\0';
3417 mybuflen = 0;
3418 version = 0;
3419
3420 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003421 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3422 Warning("Can't read first line of the server state file '%s'\n", filepath);
3423 goto fileclose;
3424 }
3425
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003426 cur = mybuf;
3427 version = atoi(cur);
3428 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3429 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003430 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003431
3432 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3433 int bk_f_forced_id = 0;
3434 int check_id = 0;
3435 int check_name = 0;
3436
3437 mybuflen = strlen(mybuf);
3438 cur = mybuf;
3439 end = cur + mybuflen;
3440
3441 bk = NULL;
3442 srv = NULL;
3443
3444 /* we need at least one character */
3445 if (mybuflen == 0)
3446 continue;
3447
3448 /* ignore blank characters at the beginning of the line */
3449 while (isspace(*cur))
3450 ++cur;
3451
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003452 /* Ignore empty or commented lines */
3453 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003454 continue;
3455
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003456 /* truncated lines */
3457 if (mybuf[mybuflen - 1] != '\n') {
3458 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003459 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003460 }
3461
3462 /* Removes trailing '\n' */
3463 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003464
3465 /* we're now ready to move the line into *srv_params[] */
3466 params[0] = cur;
3467 arg = 1;
3468 srv_arg = 0;
3469 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3470 if (isspace(*cur)) {
3471 *cur = '\0';
3472 ++cur;
3473 while (isspace(*cur))
3474 ++cur;
3475 switch (version) {
3476 case 1:
3477 /*
3478 * srv_addr: params[4] => srv_params[0]
3479 * srv_op_state: params[5] => srv_params[1]
3480 * srv_admin_state: params[6] => srv_params[2]
3481 * srv_uweight: params[7] => srv_params[3]
3482 * srv_iweight: params[8] => srv_params[4]
3483 * srv_last_time_change: params[9] => srv_params[5]
3484 * srv_check_status: params[10] => srv_params[6]
3485 * srv_check_result: params[11] => srv_params[7]
3486 * srv_check_health: params[12] => srv_params[8]
3487 * srv_check_state: params[13] => srv_params[9]
3488 * srv_agent_state: params[14] => srv_params[10]
3489 * bk_f_forced_id: params[15] => srv_params[11]
3490 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003491 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003492 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003493 */
3494 if (arg >= 4) {
3495 srv_params[srv_arg] = cur;
3496 ++srv_arg;
3497 }
3498 break;
3499 }
3500
3501 params[arg] = cur;
3502 ++arg;
3503 }
3504 else {
3505 ++cur;
3506 }
3507 }
3508
3509 /* if line is incomplete line, then ignore it.
3510 * otherwise, update useful flags */
3511 switch (version) {
3512 case 1:
3513 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3514 continue;
3515 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3516 check_id = (atoi(params[0]) == curproxy->uuid);
3517 check_name = (strcmp(curproxy->id, params[1]) == 0);
3518 break;
3519 }
3520
3521 diff = 0;
3522 bk = curproxy;
3523
3524 /* if backend can't be found, let's continue */
3525 if (!check_id && !check_name)
3526 continue;
3527 else if (!check_id && check_name) {
3528 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3529 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3530 }
3531 else if (check_id && !check_name) {
3532 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3533 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3534 /* if name doesn't match, we still want to update curproxy if the backend id
3535 * was forced in previous the previous configuration */
3536 if (!bk_f_forced_id)
3537 continue;
3538 }
3539
3540 /* look for the server by its id: param[2] */
3541 /* else look for the server by its name: param[3] */
3542 diff = 0;
3543 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3544
3545 if (!srv) {
3546 /* if no server found, then warning and continue with next line */
3547 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3548 params[3], params[2], params[0], params[1]);
3549 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3550 params[3], params[2], params[0], params[1]);
3551 continue;
3552 }
3553 else if (diff & PR_FBM_MISMATCH_ID) {
3554 Warning("In backend '%s' (id: '%d'): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
3555 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003556 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003557 }
3558 else if (diff & PR_FBM_MISMATCH_NAME) {
3559 Warning("In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
3560 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003561 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003562 }
3563
3564 /* now we can proceed with server's state update */
3565 srv_update_state(srv, version, srv_params);
3566 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003567fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003568 fclose(f);
3569 }
3570}
3571
Simon Horman7d09b9a2013-02-12 10:45:51 +09003572/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003573 * update a server's current IP address.
3574 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3575 * ip is in network format.
3576 * updater is a string which contains an information about the requester of the update.
3577 * updater is used if not NULL.
3578 *
3579 * A log line and a stderr warning message is generated based on server's backend options.
3580 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003581int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003582{
3583 /* generates a log line and a warning on stderr */
3584 if (1) {
3585 /* book enough space for both IPv4 and IPv6 */
3586 char oldip[INET6_ADDRSTRLEN];
3587 char newip[INET6_ADDRSTRLEN];
3588
3589 memset(oldip, '\0', INET6_ADDRSTRLEN);
3590 memset(newip, '\0', INET6_ADDRSTRLEN);
3591
3592 /* copy old IP address in a string */
3593 switch (s->addr.ss_family) {
3594 case AF_INET:
3595 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3596 break;
3597 case AF_INET6:
3598 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3599 break;
3600 };
3601
3602 /* copy new IP address in a string */
3603 switch (ip_sin_family) {
3604 case AF_INET:
3605 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3606 break;
3607 case AF_INET6:
3608 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3609 break;
3610 };
3611
3612 /* save log line into a buffer */
3613 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3614 s->proxy->id, s->id, oldip, newip, updater);
3615
3616 /* write the buffer on stderr */
3617 Warning("%s.\n", trash.str);
3618
3619 /* send a log */
3620 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3621 }
3622
3623 /* save the new IP family */
3624 s->addr.ss_family = ip_sin_family;
3625 /* save the new IP address */
3626 switch (ip_sin_family) {
3627 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003628 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003629 break;
3630 case AF_INET6:
3631 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3632 break;
3633 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003634 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003635
3636 return 0;
3637}
3638
3639/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003640 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3641 *
3642 * Caller can pass its name through <updater> to get it integrated in the response message
3643 * returned by the function.
3644 *
3645 * The function first does the following, in that order:
3646 * - validates the new addr and/or port
3647 * - checks if an update is required (new IP or port is different than current ones)
3648 * - checks the update is allowed:
3649 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3650 * - allow all changes if no CHECKS are configured
3651 * - if CHECK is configured:
3652 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3653 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3654 * conditions are met
3655 */
3656const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3657{
3658 struct sockaddr_storage sa;
3659 int ret, port_change_required;
3660 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003661 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003662 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003663 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003664
3665 msg = get_trash_chunk();
3666 chunk_reset(msg);
3667
3668 if (addr) {
3669 memset(&sa, 0, sizeof(struct sockaddr_storage));
3670 if (str2ip2(addr, &sa, 0) == NULL) {
3671 chunk_printf(msg, "Invalid addr '%s'", addr);
3672 goto out;
3673 }
3674
3675 /* changes are allowed on AF_INET* families only */
3676 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3677 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3678 goto out;
3679 }
3680
3681 /* collecting data currently setup */
3682 memset(current_addr, '\0', sizeof(current_addr));
3683 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3684 /* changes are allowed on AF_INET* families only */
3685 if ((ret != AF_INET) && (ret != AF_INET6)) {
3686 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3687 goto out;
3688 }
3689
3690 /* applying ADDR changes if required and allowed
3691 * ipcmp returns 0 when both ADDR are the same
3692 */
3693 if (ipcmp(&s->addr, &sa) == 0) {
3694 chunk_appendf(msg, "no need to change the addr");
3695 goto port;
3696 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003697 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003698 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003699
3700 /* we also need to update check's ADDR only if it uses the server's one */
3701 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003702 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003703 }
3704
3705 /* we also need to update agent ADDR only if it use the server's one */
3706 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003707 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003708 }
3709
3710 /* update report for caller */
3711 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3712 }
3713
3714 port:
3715 if (port) {
3716 char sign = '\0';
3717 char *endptr;
3718
3719 if (addr)
3720 chunk_appendf(msg, ", ");
3721
3722 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003723 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003724
3725 /* check if PORT change is required */
3726 port_change_required = 0;
3727
3728 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003729 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003730 new_port = strtol(port, &endptr, 10);
3731 if ((errno != 0) || (port == endptr)) {
3732 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3733 goto out;
3734 }
3735
3736 /* check if caller triggers a port mapped or offset */
3737 if (sign == '-' || (sign == '+')) {
3738 /* check if server currently uses port map */
3739 if (!(s->flags & SRV_F_MAPPORTS)) {
3740 /* switch from fixed port to port map mandatorily triggers
3741 * a port change */
3742 port_change_required = 1;
3743 /* check is configured
3744 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3745 * prevent PORT change if check doesn't have it's dedicated port while switching
3746 * to port mapping */
3747 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3748 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.");
3749 goto out;
3750 }
3751 }
3752 /* we're already using port maps */
3753 else {
3754 port_change_required = current_port != new_port;
3755 }
3756 }
3757 /* fixed port */
3758 else {
3759 port_change_required = current_port != new_port;
3760 }
3761
3762 /* applying PORT changes if required and update response message */
3763 if (port_change_required) {
3764 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003765 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003766 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003767
3768 /* prepare message */
3769 chunk_appendf(msg, "port changed from '");
3770 if (s->flags & SRV_F_MAPPORTS)
3771 chunk_appendf(msg, "+");
3772 chunk_appendf(msg, "%d' to '", current_port);
3773
3774 if (sign == '-') {
3775 s->flags |= SRV_F_MAPPORTS;
3776 chunk_appendf(msg, "%c", sign);
3777 /* just use for result output */
3778 new_port = -new_port;
3779 }
3780 else if (sign == '+') {
3781 s->flags |= SRV_F_MAPPORTS;
3782 chunk_appendf(msg, "%c", sign);
3783 }
3784 else {
3785 s->flags &= ~SRV_F_MAPPORTS;
3786 }
3787
3788 chunk_appendf(msg, "%d'", new_port);
3789
3790 /* we also need to update health checks port only if it uses server's realport */
3791 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3792 s->check.port = new_port;
3793 }
3794 }
3795 else {
3796 chunk_appendf(msg, "no need to change the port");
3797 }
3798 }
3799
3800out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003801 if (changed)
3802 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003803 if (updater)
3804 chunk_appendf(msg, " by '%s'", updater);
3805 chunk_appendf(msg, "\n");
3806 return msg->str;
3807}
3808
3809
3810/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003811 * update server status based on result of name resolution
3812 * returns:
3813 * 0 if server status is updated
3814 * 1 if server status has not changed
3815 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003816int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003817{
3818 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003819 struct dns_resolvers *resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003820
3821 switch (resolution->status) {
3822 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003823 /* status when HAProxy has just (re)started.
3824 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003825 break;
3826
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003827 case RSLV_STATUS_VALID:
3828 /*
3829 * resume health checks
3830 * server will be turned back on if health check is safe
3831 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003832 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003833 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003834 return 1;
3835 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3836 "No IP for server ");
3837 return (0);
3838 }
Emeric Brun52a91d32017-08-31 14:41:55 +02003839 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003840 return 1;
3841 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3842 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3843 s->proxy->id, s->id);
3844
3845 Warning("%s.\n", trash.str);
3846 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3847 return 0;
3848
3849 case RSLV_STATUS_NX:
3850 /* stop server if resolution is NX for a long enough period */
3851 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003852 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003853 return 1;
3854 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3855 return 0;
3856 }
3857 break;
3858
3859 case RSLV_STATUS_TIMEOUT:
3860 /* stop server if resolution is TIMEOUT for a long enough period */
3861 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003862 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003863 return 1;
3864 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3865 return 0;
3866 }
3867 break;
3868
3869 case RSLV_STATUS_REFUSED:
3870 /* stop server if resolution is REFUSED for a long enough period */
3871 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003872 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003873 return 1;
3874 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3875 return 0;
3876 }
3877 break;
3878
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003879 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003880 /* stop server if resolution is in unmatched error for a long enough period */
3881 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003882 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003883 return 1;
3884 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3885 return 0;
3886 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003887 break;
3888 }
3889
3890 return 1;
3891}
3892
3893/*
3894 * Server Name Resolution valid response callback
3895 * It expects:
3896 * - <nameserver>: the name server which answered the valid response
3897 * - <response>: buffer containing a valid DNS response
3898 * - <response_len>: size of <response>
3899 * It performs the following actions:
3900 * - ignore response if current ip found and server family not met
3901 * - update with first new ip found if family is met and current IP is not found
3902 * returns:
3903 * 0 on error
3904 * 1 when no error or safe ignore
3905 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003906int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003908 struct server *s = NULL;
3909 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003910 void *serverip, *firstip;
3911 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003912 int ret;
3913 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003914 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003915
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003916 s = objt_server(requester->requester);
3917 if (!s)
3918 return 1;
3919
3920 resolution = s->resolution;
3921
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003922 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003923 firstip = NULL; /* pointer to the first valid response found */
3924 /* it will be used as the new IP if a change is required */
3925 firstip_sin_family = AF_UNSPEC;
3926 serverip = NULL; /* current server IP address */
3927
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003928 /* initializing server IP pointer */
3929 server_sin_family = s->addr.ss_family;
3930 switch (server_sin_family) {
3931 case AF_INET:
3932 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3933 break;
3934
3935 case AF_INET6:
3936 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3937 break;
3938
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003939 case AF_UNSPEC:
3940 break;
3941
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003942 default:
3943 goto invalid;
3944 }
3945
Baptiste Assmann729c9012017-05-22 15:13:10 +02003946 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003947 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003948 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003949
3950 switch (ret) {
3951 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003952 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003953
3954 case DNS_UPD_SRVIP_NOT_FOUND:
3955 goto save_ip;
3956
3957 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003958 goto invalid;
3959
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003960 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003961 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003962 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003963
Baptiste Assmannfad03182015-10-28 02:03:32 +01003964 case DNS_UPD_NAME_ERROR:
3965 /* if this is not the last expected response, we ignore it */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003966 if (nameserver && (resolution->nb_responses < nameserver->resolvers->count_nameservers))
Baptiste Assmannfad03182015-10-28 02:03:32 +01003967 return 0;
3968 /* update resolution status to OTHER error type */
3969 if (resolution->status != RSLV_STATUS_OTHER) {
3970 resolution->status = RSLV_STATUS_OTHER;
3971 resolution->last_status_change = now_ms;
3972 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003973 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003974
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003975 default:
3976 goto invalid;
3977
3978 }
3979
3980 save_ip:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003981 if (nameserver)
3982 nameserver->counters.update += 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003983
3984 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003985 if (nameserver)
3986 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3987 else
3988 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003989 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3990
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003991 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003992 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003993 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003994
3995 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003996 if (nameserver) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003997 nameserver->counters.invalid += 1;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003998 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3999 goto update_status;
4000 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004001
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004002 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004003 return 0;
4004}
4005
4006/*
4007 * Server Name Resolution error management callback
4008 * returns:
4009 * 0 on error
4010 * 1 when no error or safe ignore
4011 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004012int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004013{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004014 struct server *s = NULL;
4015 struct dns_resolution *resolution = NULL;
4016 struct dns_resolvers *resolvers = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004017
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018 s = objt_server(requester->requester);
4019 if (!s)
4020 return 1;
4021
4022 resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02004023 resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004024
4025 /* can be ignored if this is not the last response */
4026 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
4027 return 1;
4028 }
4029
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004030 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004031 return 1;
4032}
4033
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004034/*
4035 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004036 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004037 * It returns a pointer to the first server found or NULL if <ip> is not yet
4038 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004039 */
4040struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4041{
4042 struct server *tmpsrv;
4043 struct proxy *be;
4044
4045 if (!srv)
4046 return NULL;
4047
4048 be = srv->proxy;
4049 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
4050 /* We want to compare the IP in the record with the IP of the servers in the
4051 * same backend, only if:
4052 * * DNS resolution is enabled on the server
4053 * * the hostname used for the resolution by our server is the same than the
4054 * one used for the server found in the backend
4055 * * the server found in the backend is not our current server
4056 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004057 if ((tmpsrv->hostname_dn == NULL) ||
4058 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4059 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004060 (srv->puid == tmpsrv->puid))
4061 continue;
4062
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004063 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02004064 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004065 continue;
4066
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004067 /* At this point, we have 2 different servers using the same DNS hostname
4068 * for their respective resolution.
4069 */
4070 if (*ip_family == tmpsrv->addr.ss_family &&
4071 ((tmpsrv->addr.ss_family == AF_INET &&
4072 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4073 (tmpsrv->addr.ss_family == AF_INET6 &&
4074 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
4075 return tmpsrv;
4076 }
4077 }
4078
4079 return NULL;
4080}
4081
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004082/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4083 * resolver. This is suited for initial address configuration. Returns 0 on
4084 * success otherwise a non-zero error code. In case of error, *err_code, if
4085 * not NULL, is filled up.
4086 */
4087int srv_set_addr_via_libc(struct server *srv, int *err_code)
4088{
4089 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004090 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004091 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004092 return 1;
4093 }
4094 return 0;
4095}
4096
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004097/* Set the server's FDQN (->hostname) from <hostname>.
4098 * Returns -1 if failed, 0 if not.
4099 */
4100int srv_set_fqdn(struct server *srv, const char *hostname)
4101{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004102 struct dns_resolution *resolution;
4103 int hostname_dn_len;
Olivier Houchard8da5f982017-08-04 18:35:36 +02004104 int did_set_reso = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004105
4106 /* run time DNS resolution was not active for this server
4107 * and we can't enable it at run time for now.
4108 */
4109 if (!srv->dns_requester)
4110 return -1;
4111
4112 chunk_reset(&trash);
4113
4114 /* check if hostname is really a hostname and if we have enough
4115 * room to save it in its domain name format
4116 */
4117 hostname_dn_len = dns_str_to_dn_label_len(hostname);
4118 if (hostname_dn_len == -1 || hostname_dn_len + 1 > trash.size)
4119 return -1;
4120
4121 if (!dns_str_to_dn_label(hostname,
4122 trash.str,
4123 hostname_dn_len + 1))
4124 return -1;
4125
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004126
Olivier Houchard8da5f982017-08-04 18:35:36 +02004127 if (srv->resolution->hostname_dn) {
4128 /* get a resolution from the curr or wait queues, or a brand new one from the pool */
4129 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
4130 if (!resolution)
4131 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004132
Olivier Houchard8da5f982017-08-04 18:35:36 +02004133 /* in this case, the new hostanme is the same than the old one */
4134 if (srv->resolution == resolution && srv->hostname)
4135 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004136
Olivier Houchard8da5f982017-08-04 18:35:36 +02004137 /* first, we need to unlink our server from its current resolution */
4138 srv_free_from_resolution(srv);
4139 } else {
Baptiste Assmann6fb81922017-08-14 16:35:45 +02004140 /* this server's fqdn has been set by a SRV record */
4141 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
4142 srv_free_from_resolution(srv);
4143 srv->resolution = resolution;
4144 if (resolution->hostname_dn == NULL) {
4145 resolution->last_resolution = now_ms;
4146 did_set_reso = 1;
4147 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02004148 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004149
4150 /* now we update server's parameters */
4151 free(srv->hostname);
4152 free(srv->hostname_dn);
4153 srv->hostname = strdup(hostname);
4154 srv->hostname_dn = strdup(trash.str);
4155 srv->hostname_dn_len = hostname_dn_len;
4156 if (!srv->hostname || !srv->hostname_dn)
4157 return -1;
Olivier Houchard8da5f982017-08-04 18:35:36 +02004158 if (did_set_reso) {
4159 resolution->query_type = srv->dns_requester->prefered_query_type;
4160 resolution->hostname_dn = srv->hostname_dn;
4161 resolution->hostname_dn_len = hostname_dn_len;
4162 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004163
4164 /* then we can link srv to its new resolution */
4165 dns_link_resolution(srv, OBJ_TYPE_SERVER, resolution);
4166
4167 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004168}
4169
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004170/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4171 * from the state file. This is suited for initial address configuration.
4172 * Returns 0 on success otherwise a non-zero error code. In case of error,
4173 * *err_code, if not NULL, is filled up.
4174 */
4175static int srv_apply_lastaddr(struct server *srv, int *err_code)
4176{
4177 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4178 if (err_code)
4179 *err_code |= ERR_WARN;
4180 return 1;
4181 }
4182 return 0;
4183}
4184
Willy Tarreau25e51522016-11-04 15:10:17 +01004185/* returns 0 if no error, otherwise a combination of ERR_* flags */
4186static int srv_iterate_initaddr(struct server *srv)
4187{
4188 int return_code = 0;
4189 int err_code;
4190 unsigned int methods;
4191
4192 methods = srv->init_addr_methods;
4193 if (!methods) { // default to "last,libc"
4194 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4195 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4196 }
4197
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004198 /* "-dr" : always append "none" so that server addresses resolution
4199 * failures are silently ignored, this is convenient to validate some
4200 * configs out of their environment.
4201 */
4202 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4203 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4204
Willy Tarreau25e51522016-11-04 15:10:17 +01004205 while (methods) {
4206 err_code = 0;
4207 switch (srv_get_next_initaddr(&methods)) {
4208 case SRV_IADDR_LAST:
4209 if (!srv->lastaddr)
4210 continue;
4211 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004212 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004213 return_code |= err_code;
4214 break;
4215
4216 case SRV_IADDR_LIBC:
4217 if (!srv->hostname)
4218 continue;
4219 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004220 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004221 return_code |= err_code;
4222 break;
4223
Willy Tarreau37ebe122016-11-04 15:17:58 +01004224 case SRV_IADDR_NONE:
4225 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004226 if (return_code) {
4227 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4228 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4229 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004230 return return_code;
4231
Willy Tarreau4310d362016-11-02 15:05:56 +01004232 case SRV_IADDR_IP:
4233 ipcpy(&srv->init_addr, &srv->addr);
4234 if (return_code) {
4235 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4236 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4237 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004238 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004239
Willy Tarreau25e51522016-11-04 15:10:17 +01004240 default: /* unhandled method */
4241 break;
4242 }
4243 }
4244
4245 if (!return_code) {
4246 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
4247 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4248 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004249 else {
4250 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
4251 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4252 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004253
4254 return_code |= ERR_ALERT | ERR_FATAL;
4255 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004256out:
4257 srv_set_dyncookie(srv);
4258 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004259}
4260
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004261/*
4262 * This function parses all backends and all servers within each backend
4263 * and performs servers' addr resolution based on information provided by:
4264 * - configuration file
4265 * - server-state file (states provided by an 'old' haproxy process)
4266 *
4267 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4268 */
4269int srv_init_addr(void)
4270{
4271 struct proxy *curproxy;
4272 int return_code = 0;
4273
4274 curproxy = proxy;
4275 while (curproxy) {
4276 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004277
4278 /* servers are in backend only */
4279 if (!(curproxy->cap & PR_CAP_BE))
4280 goto srv_init_addr_next;
4281
Willy Tarreau25e51522016-11-04 15:10:17 +01004282 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004283 if (srv->hostname)
4284 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004285
4286 srv_init_addr_next:
4287 curproxy = curproxy->next;
4288 }
4289
4290 return return_code;
4291}
4292
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004293const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
4294{
4295
4296 struct chunk *msg;
4297
4298 msg = get_trash_chunk();
4299 chunk_reset(msg);
4300
Olivier Houchard8da5f982017-08-04 18:35:36 +02004301 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004302 chunk_appendf(msg, "no need to change the FDQN");
4303 goto out;
4304 }
4305
4306 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4307 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4308 goto out;
4309 }
4310
4311 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4312 server->proxy->id, server->id, server->hostname, fqdn);
4313
4314 if (srv_set_fqdn(server, fqdn) < 0) {
4315 chunk_reset(msg);
4316 chunk_appendf(msg, "could not update %s/%s FQDN",
4317 server->proxy->id, server->id);
4318 goto out;
4319 }
4320
4321 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004322 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004323
4324 out:
4325 if (updater)
4326 chunk_appendf(msg, " by '%s'", updater);
4327 chunk_appendf(msg, "\n");
4328
4329 return msg->str;
4330}
4331
4332
Willy Tarreau21b069d2016-11-23 17:15:08 +01004333/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4334 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004335 * 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 +01004336 * used for CLI commands requiring a server name.
4337 * Important: the <arg> is modified to remove the '/'.
4338 */
4339struct server *cli_find_server(struct appctx *appctx, char *arg)
4340{
4341 struct proxy *px;
4342 struct server *sv;
4343 char *line;
4344
4345 /* split "backend/server" and make <line> point to server */
4346 for (line = arg; *line; line++)
4347 if (*line == '/') {
4348 *line++ = '\0';
4349 break;
4350 }
4351
4352 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004353 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004354 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004355 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004356 return NULL;
4357 }
4358
4359 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004360 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004361 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004362 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004363 return NULL;
4364 }
4365
4366 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004367 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004368 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004369 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004370 return NULL;
4371 }
4372
4373 return sv;
4374}
4375
William Lallemand222baf22016-11-19 02:00:33 +01004376
4377static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4378{
4379 struct server *sv;
4380 const char *warning;
4381
4382 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4383 return 1;
4384
4385 sv = cli_find_server(appctx, args[2]);
4386 if (!sv)
4387 return 1;
4388
4389 if (strcmp(args[3], "weight") == 0) {
4390 warning = server_parse_weight_change_request(sv, args[4]);
4391 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004392 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004393 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004394 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004395 }
4396 }
4397 else if (strcmp(args[3], "state") == 0) {
4398 if (strcmp(args[4], "ready") == 0)
4399 srv_adm_set_ready(sv);
4400 else if (strcmp(args[4], "drain") == 0)
4401 srv_adm_set_drain(sv);
4402 else if (strcmp(args[4], "maint") == 0)
4403 srv_adm_set_maint(sv);
4404 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004405 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004406 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004407 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004408 }
4409 }
4410 else if (strcmp(args[3], "health") == 0) {
4411 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004412 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004413 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004414 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004415 }
4416 else if (strcmp(args[4], "up") == 0) {
4417 sv->check.health = sv->check.rise + sv->check.fall - 1;
4418 srv_set_running(sv, "changed from CLI");
4419 }
4420 else if (strcmp(args[4], "stopping") == 0) {
4421 sv->check.health = sv->check.rise + sv->check.fall - 1;
4422 srv_set_stopping(sv, "changed from CLI");
4423 }
4424 else if (strcmp(args[4], "down") == 0) {
4425 sv->check.health = 0;
4426 srv_set_stopped(sv, "changed from CLI");
4427 }
4428 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004429 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004430 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004431 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004432 }
4433 }
4434 else if (strcmp(args[3], "agent") == 0) {
4435 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004436 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004437 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004438 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004439 }
4440 else if (strcmp(args[4], "up") == 0) {
4441 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4442 srv_set_running(sv, "changed from CLI");
4443 }
4444 else if (strcmp(args[4], "down") == 0) {
4445 sv->agent.health = 0;
4446 srv_set_stopped(sv, "changed from CLI");
4447 }
4448 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004449 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004450 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004451 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004452 }
4453 }
Misiek2da082d2017-01-09 09:40:42 +01004454 else if (strcmp(args[3], "agent-addr") == 0) {
4455 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004456 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004457 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4458 appctx->st0 = CLI_ST_PRINT;
4459 } else {
4460 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004461 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004462 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4463 appctx->st0 = CLI_ST_PRINT;
4464 }
4465 }
4466 }
4467 else if (strcmp(args[3], "agent-send") == 0) {
4468 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004469 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004470 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4471 appctx->st0 = CLI_ST_PRINT;
4472 } else {
4473 char *nss = strdup(args[4]);
4474 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004475 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004476 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4477 appctx->st0 = CLI_ST_PRINT;
4478 } else {
4479 free(sv->agent.send_string);
4480 sv->agent.send_string = nss;
4481 sv->agent.send_string_len = strlen(args[4]);
4482 }
4483 }
4484 }
William Lallemand222baf22016-11-19 02:00:33 +01004485 else if (strcmp(args[3], "check-port") == 0) {
4486 int i = 0;
4487 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004488 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004489 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004490 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004491 }
4492 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004493 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004494 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004495 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004496 }
4497 /* prevent the update of port to 0 if MAPPORTS are in use */
4498 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004499 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004500 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004501 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004502 return 1;
4503 }
4504 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004505 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004506 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004507 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004508 }
4509 else if (strcmp(args[3], "addr") == 0) {
4510 char *addr = NULL;
4511 char *port = NULL;
4512 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004513 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004514 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004515 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004516 return 1;
4517 }
4518 else {
4519 addr = args[4];
4520 }
4521 if (strcmp(args[5], "port") == 0) {
4522 port = args[6];
4523 }
4524 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4525 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004526 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004527 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004528 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004529 }
4530 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4531 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004532 else if (strcmp(args[3], "fqdn") == 0) {
4533 if (!*args[4]) {
4534 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4535 appctx->st0 = CLI_ST_PRINT;
4536 return 1;
4537 }
4538 warning = update_server_fqdn(sv, args[4], "stats socket command");
4539 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004540 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004541 appctx->ctx.cli.msg = warning;
4542 appctx->st0 = CLI_ST_PRINT;
4543 }
4544 }
William Lallemand222baf22016-11-19 02:00:33 +01004545 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004546 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004547 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 +01004548 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004549 }
4550 return 1;
4551}
4552
William Lallemand6b160942016-11-22 12:34:35 +01004553static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4554{
4555 struct stream_interface *si = appctx->owner;
4556 struct proxy *px;
4557 struct server *sv;
4558 char *line;
4559
4560
4561 /* split "backend/server" and make <line> point to server */
4562 for (line = args[2]; *line; line++)
4563 if (*line == '/') {
4564 *line++ = '\0';
4565 break;
4566 }
4567
4568 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004569 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004570 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004571 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004572 return 1;
4573 }
4574
4575 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004576 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004577 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004578 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004579 return 1;
4580 }
4581
4582 /* return server's effective weight at the moment */
4583 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004584 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004585 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004586 return 0;
4587 }
William Lallemand6b160942016-11-22 12:34:35 +01004588 return 1;
4589}
4590
4591static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4592{
4593 struct server *sv;
4594 const char *warning;
4595
4596 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4597 return 1;
4598
4599 sv = cli_find_server(appctx, args[2]);
4600 if (!sv)
4601 return 1;
4602
4603 warning = server_parse_weight_change_request(sv, args[3]);
4604 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004605 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004606 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004607 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004608 }
4609 return 1;
4610}
4611
Willy Tarreaub8026272016-11-23 11:26:56 +01004612/* parse a "set maxconn server" command. It always returns 1. */
4613static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4614{
4615 struct server *sv;
4616 const char *warning;
4617
4618 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4619 return 1;
4620
4621 sv = cli_find_server(appctx, args[3]);
4622 if (!sv)
4623 return 1;
4624
4625 warning = server_parse_maxconn_change_request(sv, args[4]);
4626 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004627 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004628 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004629 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004630 }
4631 return 1;
4632}
William Lallemand6b160942016-11-22 12:34:35 +01004633
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004634/* parse a "disable agent" command. It always returns 1. */
4635static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4636{
4637 struct server *sv;
4638
4639 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4640 return 1;
4641
4642 sv = cli_find_server(appctx, args[2]);
4643 if (!sv)
4644 return 1;
4645
4646 sv->agent.state &= ~CHK_ST_ENABLED;
4647 return 1;
4648}
4649
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004650/* parse a "disable health" command. It always returns 1. */
4651static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4652{
4653 struct server *sv;
4654
4655 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4656 return 1;
4657
4658 sv = cli_find_server(appctx, args[2]);
4659 if (!sv)
4660 return 1;
4661
4662 sv->check.state &= ~CHK_ST_ENABLED;
4663 return 1;
4664}
4665
Willy Tarreauffb4d582016-11-24 12:47:00 +01004666/* parse a "disable server" command. It always returns 1. */
4667static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4668{
4669 struct server *sv;
4670
4671 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4672 return 1;
4673
4674 sv = cli_find_server(appctx, args[2]);
4675 if (!sv)
4676 return 1;
4677
4678 srv_adm_set_maint(sv);
4679 return 1;
4680}
4681
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004682/* parse a "enable agent" command. It always returns 1. */
4683static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4684{
4685 struct server *sv;
4686
4687 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4688 return 1;
4689
4690 sv = cli_find_server(appctx, args[2]);
4691 if (!sv)
4692 return 1;
4693
4694 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004695 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004696 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004697 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004698 return 1;
4699 }
4700
4701 sv->agent.state |= CHK_ST_ENABLED;
4702 return 1;
4703}
4704
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004705/* parse a "enable health" command. It always returns 1. */
4706static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4707{
4708 struct server *sv;
4709
4710 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4711 return 1;
4712
4713 sv = cli_find_server(appctx, args[2]);
4714 if (!sv)
4715 return 1;
4716
4717 sv->check.state |= CHK_ST_ENABLED;
4718 return 1;
4719}
4720
Willy Tarreauffb4d582016-11-24 12:47:00 +01004721/* parse a "enable server" command. It always returns 1. */
4722static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4723{
4724 struct server *sv;
4725
4726 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4727 return 1;
4728
4729 sv = cli_find_server(appctx, args[2]);
4730 if (!sv)
4731 return 1;
4732
4733 srv_adm_set_ready(sv);
4734 return 1;
4735}
4736
William Lallemand222baf22016-11-19 02:00:33 +01004737/* register cli keywords */
4738static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004739 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004740 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004741 { { "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 +01004742 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004743 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004744 { { "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 +01004745 { { "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 +01004746 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004747 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4748 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4749
William Lallemand222baf22016-11-19 02:00:33 +01004750 {{},}
4751}};
4752
4753__attribute__((constructor))
4754static void __server_init(void)
4755{
4756 cli_register_kw(&cli_kws);
4757}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004758
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004759/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004760 * Local variables:
4761 * c-indent-level: 8
4762 * c-basic-offset: 8
4763 * End:
4764 */