blob: c6e42be6797689734229b25a228f1ae177ea33d1 [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);
Baptiste Assmann9d41fe72017-05-04 12:13:31 +020050static void srv_free_dns_resolution(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Willy Tarreau21faa912012-10-10 08:27:36 +020052/* List head of all known server keywords */
53static struct srv_kw_list srv_keywords = {
54 .list = LIST_HEAD_INIT(srv_keywords.list)
55};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020056
Simon Hormana3608442013-11-01 16:46:15 +090057int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020058{
Willy Tarreau892337c2014-05-13 23:41:20 +020059 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020060 return s->down_time;
61
62 return now.tv_sec - s->last_change + s->down_time;
63}
Willy Tarreaubaaee002006-06-26 02:48:02 +020064
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050065int srv_lastsession(const struct server *s)
66{
67 if (s->counters.last_sess)
68 return now.tv_sec - s->counters.last_sess;
69
70 return -1;
71}
72
Simon Horman4a741432013-02-23 15:35:38 +090073int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020074{
Simon Horman4a741432013-02-23 15:35:38 +090075 const struct server *s = check->server;
76
Willy Tarreauff5ae352013-12-11 20:36:34 +010077 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090078 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010079
Willy Tarreau892337c2014-05-13 23:41:20 +020080 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090081 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082
Simon Horman4a741432013-02-23 15:35:38 +090083 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010084}
85
Olivier Houchard4e694042017-03-14 20:01:29 +010086void srv_set_dyncookie(struct server *s)
87{
88 struct proxy *p = s->proxy;
89 struct server *tmpserv;
90 char *tmpbuf;
91 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010092 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010093 size_t buffer_len;
94 int addr_len;
95 int port;
96
97 if ((s->flags & SRV_F_COOKIESET) ||
98 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
99 s->proxy->dyncookie_key == NULL)
100 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100101 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100102
103 if (s->addr.ss_family != AF_INET &&
104 s->addr.ss_family != AF_INET6)
105 return;
106 /*
107 * Buffer to calculate the cookie value.
108 * The buffer contains the secret key + the server IP address
109 * + the TCP port.
110 */
111 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
112 /*
113 * The TCP port should use only 2 bytes, but is stored in
114 * an unsigned int in struct server, so let's use 4, to be
115 * on the safe side.
116 */
117 buffer_len = key_len + addr_len + 4;
118 tmpbuf = trash.str;
119 memcpy(tmpbuf, p->dyncookie_key, key_len);
120 memcpy(&(tmpbuf[key_len]),
121 s->addr.ss_family == AF_INET ?
122 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
123 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
124 addr_len);
125 /*
126 * Make sure it's the same across all the load balancers,
127 * no matter their endianness.
128 */
129 port = htonl(s->svc_port);
130 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
131 hash_value = XXH64(tmpbuf, buffer_len, 0);
132 memprintf(&s->cookie, "%016llx", hash_value);
133 if (!s->cookie)
134 return;
135 s->cklen = 16;
136 /*
137 * Check that we did not get a hash collision.
138 * Unlikely, but it can happen.
139 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200140 for (tmpserv = p->srv; tmpserv != NULL;
141 tmpserv = tmpserv->next) {
142 if (tmpserv == s)
143 continue;
144 if (tmpserv->cookie &&
145 strcmp(tmpserv->cookie, s->cookie) == 0) {
146 Warning("We generated two equal cookies for two different servers.\n"
147 "Please change the secret key for '%s'.\n",
148 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200150 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100151}
152
Willy Tarreau21faa912012-10-10 08:27:36 +0200153/*
154 * Registers the server keyword list <kwl> as a list of valid keywords for next
155 * parsing sessions.
156 */
157void srv_register_keywords(struct srv_kw_list *kwl)
158{
159 LIST_ADDQ(&srv_keywords.list, &kwl->list);
160}
161
162/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
163 * keyword is found with a NULL ->parse() function, then an attempt is made to
164 * find one with a valid ->parse() function. This way it is possible to declare
165 * platform-dependant, known keywords as NULL, then only declare them as valid
166 * if some options are met. Note that if the requested keyword contains an
167 * opening parenthesis, everything from this point is ignored.
168 */
169struct srv_kw *srv_find_kw(const char *kw)
170{
171 int index;
172 const char *kwend;
173 struct srv_kw_list *kwl;
174 struct srv_kw *ret = NULL;
175
176 kwend = strchr(kw, '(');
177 if (!kwend)
178 kwend = kw + strlen(kw);
179
180 list_for_each_entry(kwl, &srv_keywords.list, list) {
181 for (index = 0; kwl->kw[index].kw != NULL; index++) {
182 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
183 kwl->kw[index].kw[kwend-kw] == 0) {
184 if (kwl->kw[index].parse)
185 return &kwl->kw[index]; /* found it !*/
186 else
187 ret = &kwl->kw[index]; /* may be OK */
188 }
189 }
190 }
191 return ret;
192}
193
194/* Dumps all registered "server" keywords to the <out> string pointer. The
195 * unsupported keywords are only dumped if their supported form was not
196 * found.
197 */
198void srv_dump_kws(char **out)
199{
200 struct srv_kw_list *kwl;
201 int index;
202
203 *out = NULL;
204 list_for_each_entry(kwl, &srv_keywords.list, list) {
205 for (index = 0; kwl->kw[index].kw != NULL; index++) {
206 if (kwl->kw[index].parse ||
207 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
208 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
209 kwl->scope,
210 kwl->kw[index].kw,
211 kwl->kw[index].skip ? " <arg>" : "",
212 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
213 kwl->kw[index].parse ? "" : " (not supported)");
214 }
215 }
216 }
217}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100218
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100219/* Parse the "addr" server keyword */
220static int srv_parse_addr(char **args, int *cur_arg,
221 struct proxy *curproxy, struct server *newsrv, char **err)
222{
223 char *errmsg, *arg;
224 struct sockaddr_storage *sk;
225 int port1, port2;
226 struct protocol *proto;
227
228 errmsg = NULL;
229 arg = args[*cur_arg + 1];
230
231 if (!*arg) {
232 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
233 goto err;
234 }
235
236 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
237 if (!sk) {
238 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
239 goto err;
240 }
241
242 proto = protocol_by_family(sk->ss_family);
243 if (!proto || !proto->connect) {
244 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
245 args[*cur_arg], arg);
246 goto err;
247 }
248
249 if (port1 != port2) {
250 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
251 args[*cur_arg], arg);
252 goto err;
253 }
254
255 newsrv->check.addr = newsrv->agent.addr = *sk;
256 newsrv->flags |= SRV_F_CHECKADDR;
257 newsrv->flags |= SRV_F_AGENTADDR;
258
259 return 0;
260
261 err:
262 free(errmsg);
263 return ERR_ALERT | ERR_FATAL;
264}
265
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100266/* Parse the "agent-check" server keyword */
267static int srv_parse_agent_check(char **args, int *cur_arg,
268 struct proxy *curproxy, struct server *newsrv, char **err)
269{
270 newsrv->do_agent = 1;
271 return 0;
272}
273
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100274/* Parse the "backup" server keyword */
275static int srv_parse_backup(char **args, int *cur_arg,
276 struct proxy *curproxy, struct server *newsrv, char **err)
277{
278 newsrv->flags |= SRV_F_BACKUP;
279 return 0;
280}
281
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100282/* Parse the "check" server keyword */
283static int srv_parse_check(char **args, int *cur_arg,
284 struct proxy *curproxy, struct server *newsrv, char **err)
285{
286 newsrv->do_check = 1;
287 return 0;
288}
289
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100290/* Parse the "check-send-proxy" server keyword */
291static int srv_parse_check_send_proxy(char **args, int *cur_arg,
292 struct proxy *curproxy, struct server *newsrv, char **err)
293{
294 newsrv->check.send_proxy = 1;
295 return 0;
296}
297
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100298/* Parse the "cookie" server keyword */
299static int srv_parse_cookie(char **args, int *cur_arg,
300 struct proxy *curproxy, struct server *newsrv, char **err)
301{
302 char *arg;
303
304 arg = args[*cur_arg + 1];
305 if (!*arg) {
306 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
307 return ERR_ALERT | ERR_FATAL;
308 }
309
310 free(newsrv->cookie);
311 newsrv->cookie = strdup(arg);
312 newsrv->cklen = strlen(arg);
313 newsrv->flags |= SRV_F_COOKIESET;
314 return 0;
315}
316
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100317/* Parse the "disabled" server keyword */
318static int srv_parse_disabled(char **args, int *cur_arg,
319 struct proxy *curproxy, struct server *newsrv, char **err)
320{
321 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
322 newsrv->state = SRV_ST_STOPPED;
323 newsrv->check.state |= CHK_ST_PAUSED;
324 newsrv->check.health = 0;
325 return 0;
326}
327
328/* Parse the "enabled" server keyword */
329static int srv_parse_enabled(char **args, int *cur_arg,
330 struct proxy *curproxy, struct server *newsrv, char **err)
331{
332 newsrv->admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
333 newsrv->state = SRV_ST_RUNNING;
334 newsrv->check.state &= ~CHK_ST_PAUSED;
335 newsrv->check.health = newsrv->check.rise;
336 return 0;
337}
338
Willy Tarreaudff55432012-10-10 17:51:05 +0200339/* parse the "id" server keyword */
340static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
341{
342 struct eb32_node *node;
343
344 if (!*args[*cur_arg + 1]) {
345 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
346 return ERR_ALERT | ERR_FATAL;
347 }
348
349 newsrv->puid = atol(args[*cur_arg + 1]);
350 newsrv->conf.id.key = newsrv->puid;
351
352 if (newsrv->puid <= 0) {
353 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
354 return ERR_ALERT | ERR_FATAL;
355 }
356
357 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
358 if (node) {
359 struct server *target = container_of(node, struct server, conf.id);
360 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
361 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
362 target->id);
363 return ERR_ALERT | ERR_FATAL;
364 }
365
366 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200367 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200368 return 0;
369}
370
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100371/* Parse the "namespace" server keyword */
372static int srv_parse_namespace(char **args, int *cur_arg,
373 struct proxy *curproxy, struct server *newsrv, char **err)
374{
375#ifdef CONFIG_HAP_NS
376 char *arg;
377
378 arg = args[*cur_arg + 1];
379 if (!*arg) {
380 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
381 return ERR_ALERT | ERR_FATAL;
382 }
383
384 if (!strcmp(arg, "*")) {
385 /* Use the namespace associated with the connection (if present). */
386 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
387 return 0;
388 }
389
390 /*
391 * As this parser may be called several times for the same 'default-server'
392 * object, or for a new 'server' instance deriving from a 'default-server'
393 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
394 */
395 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
396
397 newsrv->netns = netns_store_lookup(arg, strlen(arg));
398 if (!newsrv->netns)
399 newsrv->netns = netns_store_insert(arg);
400
401 if (!newsrv->netns) {
402 memprintf(err, "Cannot open namespace '%s'", arg);
403 return ERR_ALERT | ERR_FATAL;
404 }
405
406 return 0;
407#else
408 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
409 return ERR_ALERT | ERR_FATAL;
410#endif
411}
412
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100413/* Parse the "no-agent-check" server keyword */
414static int srv_parse_no_agent_check(char **args, int *cur_arg,
415 struct proxy *curproxy, struct server *newsrv, char **err)
416{
417 free_check(&newsrv->agent);
418 newsrv->agent.inter = 0;
419 newsrv->agent.port = 0;
420 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
421 newsrv->do_agent = 0;
422 return 0;
423}
424
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100425/* Parse the "no-backup" server keyword */
426static int srv_parse_no_backup(char **args, int *cur_arg,
427 struct proxy *curproxy, struct server *newsrv, char **err)
428{
429 newsrv->flags &= ~SRV_F_BACKUP;
430 return 0;
431}
432
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100433/* Parse the "no-check" server keyword */
434static int srv_parse_no_check(char **args, int *cur_arg,
435 struct proxy *curproxy, struct server *newsrv, char **err)
436{
437 free_check(&newsrv->check);
438 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
439 newsrv->do_check = 0;
440 return 0;
441}
442
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100443/* Parse the "no-check-send-proxy" server keyword */
444static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
445 struct proxy *curproxy, struct server *newsrv, char **err)
446{
447 newsrv->check.send_proxy = 0;
448 return 0;
449}
450
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100451/* Disable server PROXY protocol flags. */
452static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
453{
454 srv->pp_opts &= ~flags;
455 return 0;
456}
457
458/* Parse the "no-send-proxy" server keyword */
459static int srv_parse_no_send_proxy(char **args, int *cur_arg,
460 struct proxy *curproxy, struct server *newsrv, char **err)
461{
462 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
463}
464
465/* Parse the "no-send-proxy-v2" server keyword */
466static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
467 struct proxy *curproxy, struct server *newsrv, char **err)
468{
469 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
470}
471
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100472/* Parse the "non-stick" server keyword */
473static int srv_parse_non_stick(char **args, int *cur_arg,
474 struct proxy *curproxy, struct server *newsrv, char **err)
475{
476 newsrv->flags |= SRV_F_NON_STICK;
477 return 0;
478}
479
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100480/* Enable server PROXY protocol flags. */
481static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
482{
483 srv->pp_opts |= flags;
484 return 0;
485}
486
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100487/* Parse the "observe" server keyword */
488static int srv_parse_observe(char **args, int *cur_arg,
489 struct proxy *curproxy, struct server *newsrv, char **err)
490{
491 char *arg;
492
493 arg = args[*cur_arg + 1];
494 if (!*arg) {
495 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
496 return ERR_ALERT | ERR_FATAL;
497 }
498
499 if (!strcmp(arg, "none")) {
500 newsrv->observe = HANA_OBS_NONE;
501 }
502 else if (!strcmp(arg, "layer4")) {
503 newsrv->observe = HANA_OBS_LAYER4;
504 }
505 else if (!strcmp(arg, "layer7")) {
506 if (curproxy->mode != PR_MODE_HTTP) {
507 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
508 return ERR_ALERT;
509 }
510 newsrv->observe = HANA_OBS_LAYER7;
511 }
512 else {
513 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
514 "but got '%s'\n", args[*cur_arg], arg);
515 return ERR_ALERT | ERR_FATAL;
516 }
517
518 return 0;
519}
520
Frédéric Lécaille16186232017-03-14 16:42:49 +0100521/* Parse the "redir" server keyword */
522static int srv_parse_redir(char **args, int *cur_arg,
523 struct proxy *curproxy, struct server *newsrv, char **err)
524{
525 char *arg;
526
527 arg = args[*cur_arg + 1];
528 if (!*arg) {
529 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
530 return ERR_ALERT | ERR_FATAL;
531 }
532
533 free(newsrv->rdr_pfx);
534 newsrv->rdr_pfx = strdup(arg);
535 newsrv->rdr_len = strlen(arg);
536
537 return 0;
538}
539
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100540/* Parse the "send-proxy" server keyword */
541static int srv_parse_send_proxy(char **args, int *cur_arg,
542 struct proxy *curproxy, struct server *newsrv, char **err)
543{
544 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
545}
546
547/* Parse the "send-proxy-v2" server keyword */
548static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
549 struct proxy *curproxy, struct server *newsrv, char **err)
550{
551 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
552}
553
Frédéric Lécailledba97072017-03-17 15:33:50 +0100554
555/* Parse the "source" server keyword */
556static int srv_parse_source(char **args, int *cur_arg,
557 struct proxy *curproxy, struct server *newsrv, char **err)
558{
559 char *errmsg;
560 int port_low, port_high;
561 struct sockaddr_storage *sk;
562 struct protocol *proto;
563
564 errmsg = NULL;
565
566 if (!*args[*cur_arg + 1]) {
567 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
568 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
569 goto err;
570 }
571
572 /* 'sk' is statically allocated (no need to be freed). */
573 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
574 if (!sk) {
575 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
576 goto err;
577 }
578
579 proto = protocol_by_family(sk->ss_family);
580 if (!proto || !proto->connect) {
581 Alert("'%s %s' : connect() not supported for this address family.\n",
582 args[*cur_arg], args[*cur_arg + 1]);
583 goto err;
584 }
585
586 newsrv->conn_src.opts |= CO_SRC_BIND;
587 newsrv->conn_src.source_addr = *sk;
588
589 if (port_low != port_high) {
590 int i;
591
592 if (!port_low || !port_high) {
593 Alert("'%s' does not support port offsets (found '%s').\n",
594 args[*cur_arg], args[*cur_arg + 1]);
595 goto err;
596 }
597
598 if (port_low <= 0 || port_low > 65535 ||
599 port_high <= 0 || port_high > 65535 ||
600 port_low > port_high) {
601 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
602 goto err;
603 }
604 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
605 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
606 newsrv->conn_src.sport_range->ports[i] = port_low + i;
607 }
608
609 *cur_arg += 2;
610 while (*(args[*cur_arg])) {
611 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
612#if defined(CONFIG_HAP_TRANSPARENT)
613 if (!*args[*cur_arg + 1]) {
614 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
615 "or 'hdr_ip(name,#)' as argument.\n");
616 goto err;
617 }
618 if (!strcmp(args[*cur_arg + 1], "client")) {
619 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
620 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
621 }
622 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
623 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
624 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
625 }
626 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
627 char *name, *end;
628
629 name = args[*cur_arg + 1] + 7;
630 while (isspace(*name))
631 name++;
632
633 end = name;
634 while (*end && !isspace(*end) && *end != ',' && *end != ')')
635 end++;
636
637 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
638 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
639 free(newsrv->conn_src.bind_hdr_name);
640 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
641 newsrv->conn_src.bind_hdr_len = end - name;
642 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
643 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
644 newsrv->conn_src.bind_hdr_occ = -1;
645
646 /* now look for an occurrence number */
647 while (isspace(*end))
648 end++;
649 if (*end == ',') {
650 end++;
651 name = end;
652 if (*end == '-')
653 end++;
654 while (isdigit((int)*end))
655 end++;
656 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
657 }
658
659 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
660 Alert("usesrc hdr_ip(name,num) does not support negative"
661 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
662 goto err;
663 }
664 }
665 else {
666 struct sockaddr_storage *sk;
667 int port1, port2;
668
669 /* 'sk' is statically allocated (no need to be freed). */
670 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
671 if (!sk) {
672 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
673 goto err;
674 }
675
676 proto = protocol_by_family(sk->ss_family);
677 if (!proto || !proto->connect) {
678 Alert("'%s %s' : connect() not supported for this address family.\n",
679 args[*cur_arg], args[*cur_arg + 1]);
680 goto err;
681 }
682
683 if (port1 != port2) {
684 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
685 args[*cur_arg], args[*cur_arg + 1]);
686 goto err;
687 }
688 newsrv->conn_src.tproxy_addr = *sk;
689 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
690 }
691 global.last_checks |= LSTCHK_NETADM;
692 *cur_arg += 2;
693 continue;
694#else /* no TPROXY support */
695 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
696 goto err;
697#endif /* defined(CONFIG_HAP_TRANSPARENT) */
698 } /* "usesrc" */
699
700 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
701#ifdef SO_BINDTODEVICE
702 if (!*args[*cur_arg + 1]) {
703 Alert("'%s' : missing interface name.\n", args[0]);
704 goto err;
705 }
706 free(newsrv->conn_src.iface_name);
707 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
708 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
709 global.last_checks |= LSTCHK_NETADM;
710#else
711 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
712 goto err;
713#endif
714 *cur_arg += 2;
715 continue;
716 }
717 /* this keyword in not an option of "source" */
718 break;
719 } /* while */
720
721 return 0;
722
723 err:
724 free(errmsg);
725 return ERR_ALERT | ERR_FATAL;
726}
727
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100728/* Parse the "stick" server keyword */
729static int srv_parse_stick(char **args, int *cur_arg,
730 struct proxy *curproxy, struct server *newsrv, char **err)
731{
732 newsrv->flags &= ~SRV_F_NON_STICK;
733 return 0;
734}
735
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100736/* Parse the "track" server keyword */
737static int srv_parse_track(char **args, int *cur_arg,
738 struct proxy *curproxy, struct server *newsrv, char **err)
739{
740 char *arg;
741
742 arg = args[*cur_arg + 1];
743 if (!*arg) {
744 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
745 return ERR_ALERT | ERR_FATAL;
746 }
747
748 free(newsrv->trackit);
749 newsrv->trackit = strdup(arg);
750
751 return 0;
752}
753
Frédéric Lécailledba97072017-03-17 15:33:50 +0100754
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200755/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200756 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200757 * shutdown.
758 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200759void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200760{
Willy Tarreau87b09662015-04-03 00:22:06 +0200761 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200762
Willy Tarreau87b09662015-04-03 00:22:06 +0200763 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
764 if (stream->srv_conn == srv)
765 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200766}
767
768/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200769 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200770 * the reason for the shutdown.
771 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200772void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200773{
774 struct server *srv;
775
776 for (srv = px->srv; srv != NULL; srv = srv->next)
777 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200778 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200779}
780
Willy Tarreaubda92272014-05-20 21:55:30 +0200781/* Appends some information to a message string related to a server going UP or
782 * DOWN. If both <forced> and <reason> are null and the server tracks another
783 * one, a "via" information will be provided to know where the status came from.
784 * If <reason> is non-null, the entire string will be appended after a comma and
785 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200786 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200787 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200788 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200789void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200790{
Willy Tarreaubda92272014-05-20 21:55:30 +0200791 if (reason)
792 chunk_appendf(msg, ", %s", reason);
793 else if (!forced && s->track)
794 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200795
796 if (xferred >= 0) {
797 if (s->state == SRV_ST_STOPPED)
798 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
799 " %d sessions active, %d requeued, %d remaining in queue",
800 s->proxy->srv_act, s->proxy->srv_bck,
801 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
802 s->cur_sess, xferred, s->nbpend);
803 else
804 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
805 " %d sessions requeued, %d total in queue",
806 s->proxy->srv_act, s->proxy->srv_bck,
807 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
808 xferred, s->nbpend);
809 }
810}
811
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200812/* Marks server <s> down, regardless of its checks' statuses, notifies by all
813 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200814 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200815 * recomputes the number of servers, but not the map. Maintenance servers are
816 * ignored. It reports <reason> if non-null as the reason for going down. Note
817 * that it makes use of the trash to build the log strings, so <reason> must
818 * not be placed there.
819 */
820void srv_set_stopped(struct server *s, const char *reason)
821{
822 struct server *srv;
823 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
824 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900825 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200826 int xferred;
827
828 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
829 return;
830
831 s->last_change = now.tv_sec;
832 s->state = SRV_ST_STOPPED;
833 if (s->proxy->lbprm.set_server_status_down)
834 s->proxy->lbprm.set_server_status_down(s);
835
836 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200837 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200838
Willy Tarreau87b09662015-04-03 00:22:06 +0200839 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200840 * a connection. Those which are redispatchable will be queued
841 * to another server or to the proxy itself.
842 */
843 xferred = pendconn_redistribute(s);
844
845 chunk_printf(&trash,
846 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
847 s->proxy->id, s->id);
848
849 srv_append_status(&trash, s, reason, xferred, 0);
850 Warning("%s.\n", trash.str);
851
852 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900853 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
854 send_log(s->proxy, log_level, "%s.\n", trash.str);
855 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200856
857 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
858 set_backend_down(s->proxy);
859
860 s->counters.down_trans++;
861
862 for (srv = s->trackers; srv; srv = srv->tracknext)
863 srv_set_stopped(srv, NULL);
864}
865
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200866/* Marks server <s> up regardless of its checks' statuses and provided it isn't
867 * in maintenance. Notifies by all available means, recounts the remaining
868 * servers on the proxy and tries to grab requests from the proxy. It
869 * automatically recomputes the number of servers, but not the map. Maintenance
870 * servers are ignored. It reports <reason> if non-null as the reason for going
871 * up. Note that it makes use of the trash to build the log strings, so <reason>
872 * must not be placed there.
873 */
874void srv_set_running(struct server *s, const char *reason)
875{
876 struct server *srv;
877 int xferred;
878
879 if (s->admin & SRV_ADMF_MAINT)
880 return;
881
882 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
883 return;
884
885 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
886 if (s->proxy->last_change < now.tv_sec) // ignore negative times
887 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
888 s->proxy->last_change = now.tv_sec;
889 }
890
891 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
892 s->down_time += now.tv_sec - s->last_change;
893
894 s->last_change = now.tv_sec;
895
896 s->state = SRV_ST_STARTING;
897 if (s->slowstart > 0)
898 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
899 else
900 s->state = SRV_ST_RUNNING;
901
902 server_recalc_eweight(s);
903
904 /* If the server is set with "on-marked-up shutdown-backup-sessions",
905 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200906 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200907 * on all backup servers.
908 */
909 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
910 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200911 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200912
913 /* check if we can handle some connections queued at the proxy. We
914 * will take as many as we can handle.
915 */
916 xferred = pendconn_grab_from_px(s);
917
918 chunk_printf(&trash,
919 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
920 s->proxy->id, s->id);
921
922 srv_append_status(&trash, s, reason, xferred, 0);
923 Warning("%s.\n", trash.str);
924 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900925 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200926
927 for (srv = s->trackers; srv; srv = srv->tracknext)
928 srv_set_running(srv, NULL);
929}
930
Willy Tarreau8eb77842014-05-21 13:54:57 +0200931/* Marks server <s> stopping regardless of its checks' statuses and provided it
932 * isn't in maintenance. Notifies by all available means, recounts the remaining
933 * servers on the proxy and tries to grab requests from the proxy. It
934 * automatically recomputes the number of servers, but not the map. Maintenance
935 * servers are ignored. It reports <reason> if non-null as the reason for going
936 * up. Note that it makes use of the trash to build the log strings, so <reason>
937 * must not be placed there.
938 */
939void srv_set_stopping(struct server *s, const char *reason)
940{
941 struct server *srv;
942 int xferred;
943
944 if (s->admin & SRV_ADMF_MAINT)
945 return;
946
947 if (s->state == SRV_ST_STOPPING)
948 return;
949
950 s->last_change = now.tv_sec;
951 s->state = SRV_ST_STOPPING;
952 if (s->proxy->lbprm.set_server_status_down)
953 s->proxy->lbprm.set_server_status_down(s);
954
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200956 * a connection. Those which are redispatchable will be queued
957 * to another server or to the proxy itself.
958 */
959 xferred = pendconn_redistribute(s);
960
961 chunk_printf(&trash,
962 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
963 s->proxy->id, s->id);
964
965 srv_append_status(&trash, s, reason, xferred, 0);
966
967 Warning("%s.\n", trash.str);
968 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
969
970 if (!s->proxy->srv_bck && !s->proxy->srv_act)
971 set_backend_down(s->proxy);
972
973 for (srv = s->trackers; srv; srv = srv->tracknext)
974 srv_set_stopping(srv, NULL);
975}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200976
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200977/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
978 * enforce either maint mode or drain mode. It is not allowed to set more than
979 * one flag at once. The equivalent "inherited" flag is propagated to all
980 * tracking servers. Maintenance mode disables health checks (but not agent
981 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100982 * is done. If <cause> is non-null, it will be displayed at the end of the log
983 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200984 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100985void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200986{
987 struct check *check = &s->check;
988 struct server *srv;
989 int xferred;
990
991 if (!mode)
992 return;
993
994 /* stop going down as soon as we meet a server already in the same state */
995 if (s->admin & mode)
996 return;
997
998 s->admin |= mode;
999
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001000 /* stop going down if the equivalent flag was already present (forced or inherited) */
1001 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
1002 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
1003 return;
1004
1005 /* Maintenance must also disable health checks */
1006 if (mode & SRV_ADMF_MAINT) {
1007 if (s->check.state & CHK_ST_ENABLED) {
1008 s->check.state |= CHK_ST_PAUSED;
1009 check->health = 0;
1010 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001011
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001012 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001013 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001014 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
1015 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1016 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017
Willy Tarreaubda92272014-05-20 21:55:30 +02001018 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001020 if (!(global.mode & MODE_STARTING)) {
1021 Warning("%s.\n", trash.str);
1022 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1023 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001024 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001025 else { /* server was still running */
1026 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
1027 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1028
1029 check->health = 0; /* failure */
1030 s->last_change = now.tv_sec;
1031 s->state = SRV_ST_STOPPED;
1032 if (s->proxy->lbprm.set_server_status_down)
1033 s->proxy->lbprm.set_server_status_down(s);
1034
1035 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001036 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001037
Willy Tarreau87b09662015-04-03 00:22:06 +02001038 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001039 * a connection. Those which are redispatchable will be queued
1040 * to another server or to the proxy itself.
1041 */
1042 xferred = pendconn_redistribute(s);
1043
1044 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001045 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001046 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001047 s->proxy->id, s->id,
1048 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001049
1050 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1051
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001052 if (!(global.mode & MODE_STARTING)) {
1053 Warning("%s.\n", trash.str);
1054 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1055 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001056
1057 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1058 set_backend_down(s->proxy);
1059
1060 s->counters.down_trans++;
1061 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001062 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001063
1064 /* drain state is applied only if not yet in maint */
1065 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001066 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1067
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001069 if (s->proxy->lbprm.set_server_status_down)
1070 s->proxy->lbprm.set_server_status_down(s);
1071
Willy Tarreau87b09662015-04-03 00:22:06 +02001072 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001073 * a connection. Those which are redispatchable will be queued
1074 * to another server or to the proxy itself.
1075 */
1076 xferred = pendconn_redistribute(s);
1077
Willy Tarreau8b428482016-11-07 15:53:43 +01001078 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1079 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1080 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001081
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001082 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001083
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001084 if (!(global.mode & MODE_STARTING)) {
1085 Warning("%s.\n", trash.str);
1086 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1087 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1088 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001089 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1090 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001091 }
1092
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001093 /* compute the inherited flag to propagate */
1094 if (mode & SRV_ADMF_MAINT)
1095 mode = SRV_ADMF_IMAINT;
1096 else if (mode & SRV_ADMF_DRAIN)
1097 mode = SRV_ADMF_IDRAIN;
1098
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001099 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001100 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001101}
1102
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001103/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1104 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1105 * than one flag at once. The equivalent "inherited" flag is propagated to all
1106 * tracking servers. Leaving maintenance mode re-enables health checks. When
1107 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001108 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001109void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001110{
1111 struct check *check = &s->check;
1112 struct server *srv;
1113 int xferred = -1;
1114
1115 if (!mode)
1116 return;
1117
1118 /* stop going down as soon as we see the flag is not there anymore */
1119 if (!(s->admin & mode))
1120 return;
1121
1122 s->admin &= ~mode;
1123
1124 if (s->admin & SRV_ADMF_MAINT) {
1125 /* remaining in maintenance mode, let's inform precisely about the
1126 * situation.
1127 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001128 if (mode & SRV_ADMF_FMAINT) {
1129 chunk_printf(&trash,
1130 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1131 s->flags & SRV_F_BACKUP ? "Backup " : "",
1132 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001133
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001134 if (s->track) /* normally it's mandatory here */
1135 chunk_appendf(&trash, " via %s/%s",
1136 s->track->proxy->id, s->track->id);
1137 Warning("%s.\n", trash.str);
1138 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1139 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001140 if (mode & SRV_ADMF_RMAINT) {
1141 chunk_printf(&trash,
1142 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1143 s->flags & SRV_F_BACKUP ? "Backup " : "",
1144 s->proxy->id, s->id, s->hostname);
1145
1146 if (s->track) /* normally it's mandatory here */
1147 chunk_appendf(&trash, " via %s/%s",
1148 s->track->proxy->id, s->track->id);
1149 Warning("%s.\n", trash.str);
1150 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1151 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001152 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001153 chunk_printf(&trash,
1154 "%sServer %s/%s remains in forced maintenance",
1155 s->flags & SRV_F_BACKUP ? "Backup " : "",
1156 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001157 Warning("%s.\n", trash.str);
1158 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1159 }
1160 /* don't report anything when leaving drain mode and remaining in maintenance */
1161 }
1162 else if (mode & SRV_ADMF_MAINT) {
1163 /* OK here we're leaving maintenance, we have many things to check,
1164 * because the server might possibly be coming back up depending on
1165 * its state. In practice, leaving maintenance means that we should
1166 * immediately turn to UP (more or less the slowstart) under the
1167 * following conditions :
1168 * - server is neither checked nor tracked
1169 * - server tracks another server which is not checked
1170 * - server tracks another server which is already up
1171 * Which sums up as something simpler :
1172 * "either the tracking server is up or the server's checks are disabled
1173 * or up". Otherwise we only re-enable health checks. There's a special
1174 * case associated to the stopping state which can be inherited. Note
1175 * that the server might still be in drain mode, which is naturally dealt
1176 * with by the lower level functions.
1177 */
1178
1179 if (s->check.state & CHK_ST_ENABLED) {
1180 s->check.state &= ~CHK_ST_PAUSED;
1181 check->health = check->rise; /* start OK but check immediately */
1182 }
1183
1184 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
1185 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1186 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
1187 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1188 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1189 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1190 s->proxy->last_change = now.tv_sec;
1191 }
1192
1193 if (s->last_change < now.tv_sec) // ignore negative times
1194 s->down_time += now.tv_sec - s->last_change;
1195 s->last_change = now.tv_sec;
1196
1197 if (s->track && s->track->state == SRV_ST_STOPPING)
1198 s->state = SRV_ST_STOPPING;
1199 else {
1200 s->state = SRV_ST_STARTING;
1201 if (s->slowstart > 0)
1202 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1203 else
1204 s->state = SRV_ST_RUNNING;
1205 }
1206
1207 server_recalc_eweight(s);
1208
1209 /* 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,
Willy Tarreau87b09662015-04-03 00:22:06 +02001211 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001212 * on all backup servers.
1213 */
1214 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1215 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001216 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001217
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 }
1223
1224 if (mode & SRV_ADMF_FMAINT) {
1225 chunk_printf(&trash,
1226 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1227 s->flags & SRV_F_BACKUP ? "Backup " : "",
1228 s->proxy->id, s->id,
1229 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1230 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001231 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001232 else if (mode & SRV_ADMF_RMAINT) {
1233 chunk_printf(&trash,
1234 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1235 s->flags & SRV_F_BACKUP ? "Backup " : "",
1236 s->proxy->id, s->id, s->hostname,
1237 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1238 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1239 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001240 else {
1241 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001242 "%sServer %s/%s is %s/%s (leaving maintenance)",
1243 s->flags & SRV_F_BACKUP ? "Backup " : "",
1244 s->proxy->id, s->id,
1245 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1246 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1247 srv_append_status(&trash, s, NULL, xferred, 0);
1248 }
1249 Warning("%s.\n", trash.str);
1250 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1251 }
1252 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
1253 /* remaining in drain mode after removing one of its flags */
1254
1255 if (mode & SRV_ADMF_FDRAIN) {
1256 chunk_printf(&trash,
1257 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001258 s->flags & SRV_F_BACKUP ? "Backup " : "",
1259 s->proxy->id, s->id);
1260
1261 if (s->track) /* normally it's mandatory here */
1262 chunk_appendf(&trash, " via %s/%s",
1263 s->track->proxy->id, s->track->id);
1264 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001265 else {
1266 chunk_printf(&trash,
1267 "%sServer %s/%s remains in forced drain mode",
1268 s->flags & SRV_F_BACKUP ? "Backup " : "",
1269 s->proxy->id, s->id);
1270 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001271 Warning("%s.\n", trash.str);
1272 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001273 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001274 else if (mode & SRV_ADMF_DRAIN) {
1275 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001276 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1277 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1278 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1279 s->proxy->last_change = now.tv_sec;
1280 }
1281
1282 if (s->last_change < now.tv_sec) // ignore negative times
1283 s->down_time += now.tv_sec - s->last_change;
1284 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001285 server_recalc_eweight(s);
1286
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001287 if (mode & SRV_ADMF_FDRAIN) {
1288 chunk_printf(&trash,
1289 "%sServer %s/%s is %s (leaving forced drain)",
1290 s->flags & SRV_F_BACKUP ? "Backup " : "",
1291 s->proxy->id, s->id,
1292 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1293 }
1294 else {
1295 chunk_printf(&trash,
1296 "%sServer %s/%s is %s (leaving drain)",
1297 s->flags & SRV_F_BACKUP ? "Backup " : "",
1298 s->proxy->id, s->id,
1299 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1300 if (s->track) /* normally it's mandatory here */
1301 chunk_appendf(&trash, " via %s/%s",
1302 s->track->proxy->id, s->track->id);
1303 }
1304 Warning("%s.\n", trash.str);
1305 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001306 }
1307
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001308 /* stop going down if the equivalent flag is still present (forced or inherited) */
1309 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
1310 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
1311 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001312
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001313 if (mode & SRV_ADMF_MAINT)
1314 mode = SRV_ADMF_IMAINT;
1315 else if (mode & SRV_ADMF_DRAIN)
1316 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001317
1318 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001319 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001320}
1321
Willy Tarreau757478e2016-11-03 19:22:19 +01001322/* principle: propagate maint and drain to tracking servers. This is useful
1323 * upon startup so that inherited states are correct.
1324 */
1325static void srv_propagate_admin_state(struct server *srv)
1326{
1327 struct server *srv2;
1328
1329 if (!srv->trackers)
1330 return;
1331
1332 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
1333 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001334 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001335
1336 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001337 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001338 }
1339}
1340
1341/* Compute and propagate the admin states for all servers in proxy <px>.
1342 * Only servers *not* tracking another one are considered, because other
1343 * ones will be handled when the server they track is visited.
1344 */
1345void srv_compute_all_admin_states(struct proxy *px)
1346{
1347 struct server *srv;
1348
1349 for (srv = px->srv; srv; srv = srv->next) {
1350 if (srv->track)
1351 continue;
1352 srv_propagate_admin_state(srv);
1353 }
1354}
1355
Willy Tarreaudff55432012-10-10 17:51:05 +02001356/* Note: must not be declared <const> as its list will be overwritten.
1357 * Please take care of keeping this list alphabetically sorted, doing so helps
1358 * all code contributors.
1359 * Optional keywords are also declared with a NULL ->parse() function so that
1360 * the config parser can report an appropriate error when a known keyword was
1361 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001362 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001363 */
1364static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001365 { "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 +01001366 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001367 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001368 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001369 { "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 +01001370 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001371 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1372 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001373 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001374 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001375 { "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 +01001376 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001377 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001378 { "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 +01001379 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1380 { "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 +01001381 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001382 { "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 +01001383 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001384 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1385 { "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 +02001386 { "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 +01001387 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001388 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001389 { NULL, NULL, 0 },
1390}};
1391
1392__attribute__((constructor))
1393static void __listener_init(void)
1394{
1395 srv_register_keywords(&srv_kws);
1396}
1397
Willy Tarreau004e0452013-11-21 11:22:01 +01001398/* Recomputes the server's eweight based on its state, uweight, the current time,
1399 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1400 * state is automatically disabled if the time is elapsed.
1401 */
1402void server_recalc_eweight(struct server *sv)
1403{
1404 struct proxy *px = sv->proxy;
1405 unsigned w;
1406
1407 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1408 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +02001409 if (sv->state == SRV_ST_STARTING)
1410 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001411 }
1412
1413 /* We must take care of not pushing the server to full throttle during slow starts.
1414 * It must also start immediately, at least at the minimal step when leaving maintenance.
1415 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001416 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001417 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1418 else
1419 w = px->lbprm.wdiv;
1420
1421 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
1422
1423 /* now propagate the status change to any LB algorithms */
1424 if (px->lbprm.update_server_eweight)
1425 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +02001426 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001427 if (px->lbprm.set_server_status_up)
1428 px->lbprm.set_server_status_up(sv);
1429 }
1430 else {
1431 if (px->lbprm.set_server_status_down)
1432 px->lbprm.set_server_status_down(sv);
1433 }
1434}
1435
Willy Tarreaubaaee002006-06-26 02:48:02 +02001436/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001437 * Parses weight_str and configures sv accordingly.
1438 * Returns NULL on success, error message string otherwise.
1439 */
1440const char *server_parse_weight_change_request(struct server *sv,
1441 const char *weight_str)
1442{
1443 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001444 long int w;
1445 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001446
1447 px = sv->proxy;
1448
1449 /* if the weight is terminated with '%', it is set relative to
1450 * the initial weight, otherwise it is absolute.
1451 */
1452 if (!*weight_str)
1453 return "Require <weight> or <weight%>.\n";
1454
Simon Hormanb796afa2013-02-12 10:45:53 +09001455 w = strtol(weight_str, &end, 10);
1456 if (end == weight_str)
1457 return "Empty weight string empty or preceded by garbage";
1458 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001459 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001460 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001461 /* Avoid integer overflow */
1462 if (w > 25600)
1463 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001464 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001465 if (w > 256)
1466 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001467 }
1468 else if (w < 0 || w > 256)
1469 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001470 else if (end[0] != '\0')
1471 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001472
1473 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1474 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1475
1476 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001477 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001478
1479 return NULL;
1480}
1481
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001482/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001483 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1484 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001485 * Returns:
1486 * - error string on error
1487 * - NULL on success
1488 */
1489const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001490 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001491{
1492 unsigned char ip[INET6_ADDRSTRLEN];
1493
1494 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001495 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001496 return NULL;
1497 }
1498 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001499 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001500 return NULL;
1501 }
1502
1503 return "Could not understand IP address format.\n";
1504}
1505
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001506const char *server_parse_maxconn_change_request(struct server *sv,
1507 const char *maxconn_str)
1508{
1509 long int v;
1510 char *end;
1511
1512 if (!*maxconn_str)
1513 return "Require <maxconn>.\n";
1514
1515 v = strtol(maxconn_str, &end, 10);
1516 if (end == maxconn_str)
1517 return "maxconn string empty or preceded by garbage";
1518 else if (end[0] != '\0')
1519 return "Trailing garbage in maxconn string";
1520
1521 if (sv->maxconn == sv->minconn) { // static maxconn
1522 sv->maxconn = sv->minconn = v;
1523 } else { // dynamic maxconn
1524 sv->maxconn = v;
1525 }
1526
1527 if (may_dequeue_tasks(sv, sv->proxy))
1528 process_srv_queue(sv);
1529
1530 return NULL;
1531}
1532
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001533#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001534static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1535 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001536{
1537 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001538 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001539 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001540 NULL,
1541 };
1542
1543 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001544 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001545
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001546 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1547}
1548
1549static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1550{
1551 struct sample_expr *expr;
1552
1553 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 +01001554 if (!expr) {
1555 memprintf(err, "error detected while parsing sni expression : %s", *err);
1556 return ERR_ALERT | ERR_FATAL;
1557 }
1558
1559 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1560 memprintf(err, "error detected while parsing sni expression : "
1561 " fetch method '%s' extracts information from '%s', "
1562 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001563 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001564 return ERR_ALERT | ERR_FATAL;
1565 }
1566
1567 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1568 release_sample_expr(newsrv->ssl_ctx.sni);
1569 newsrv->ssl_ctx.sni = expr;
1570
1571 return 0;
1572}
1573#endif
1574
1575static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1576{
1577 if (err && *err) {
1578 indent_msg(err, 2);
1579 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1580 }
1581 else
1582 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1583 file, linenum, args[0], args[1], args[cur_arg]);
1584}
1585
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001586static void srv_conn_src_sport_range_cpy(struct server *srv,
1587 struct server *src)
1588{
1589 int range_sz;
1590
1591 range_sz = src->conn_src.sport_range->size;
1592 if (range_sz > 0) {
1593 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1594 if (srv->conn_src.sport_range != NULL) {
1595 int i;
1596
1597 for (i = 0; i < range_sz; i++) {
1598 srv->conn_src.sport_range->ports[i] =
1599 src->conn_src.sport_range->ports[i];
1600 }
1601 }
1602 }
1603}
1604
1605/*
1606 * Copy <src> server connection source settings to <srv> server everything needed.
1607 */
1608static void srv_conn_src_cpy(struct server *srv, struct server *src)
1609{
1610 srv->conn_src.opts = src->conn_src.opts;
1611 srv->conn_src.source_addr = src->conn_src.source_addr;
1612
1613 /* Source port range copy. */
1614 if (src->conn_src.sport_range != NULL)
1615 srv_conn_src_sport_range_cpy(srv, src);
1616
1617#ifdef CONFIG_HAP_TRANSPARENT
1618 if (src->conn_src.bind_hdr_name != NULL) {
1619 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1620 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1621 }
1622 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1623 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1624#endif
1625 if (src->conn_src.iface_name != NULL)
1626 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1627}
1628
1629/*
1630 * Copy <src> server SSL settings to <srv> server allocating
1631 * everything needed.
1632 */
1633#if defined(USE_OPENSSL)
1634static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1635{
1636 if (src->ssl_ctx.ca_file != NULL)
1637 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1638 if (src->ssl_ctx.crl_file != NULL)
1639 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1640 if (src->ssl_ctx.client_crt != NULL)
1641 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1642
1643 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1644
1645 if (src->ssl_ctx.verify_host != NULL)
1646 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1647 if (src->ssl_ctx.ciphers != NULL)
1648 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1649 if (src->sni_expr != NULL)
1650 srv->sni_expr = strdup(src->sni_expr);
1651}
1652#endif
1653
1654/*
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001655 * Allocate <srv> server dns resolution.
1656 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001657 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001658 */
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001659static int srv_alloc_dns_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001660{
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001661 struct dns_resolution *dst_dns_rslt;
1662
1663 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 dst_dns_rslt = dns_alloc_resolution();
1669
Baptiste Assmann42746372017-05-03 12:12:02 +02001670 srv->hostname_dn_len = dns_str_to_dn_label_len(hostname);
1671 srv->hostname_dn = calloc(srv->hostname_dn_len + 1, sizeof(char));
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001672
Baptiste Assmann42746372017-05-03 12:12:02 +02001673 if (!srv->hostname || !dst_dns_rslt || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001674 goto err;
1675
1676 srv->resolution = dst_dns_rslt;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001677
1678 if (!dns_str_to_dn_label(srv->hostname,
Baptiste Assmann42746372017-05-03 12:12:02 +02001679 srv->hostname_dn,
1680 srv->hostname_dn_len + 1))
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001681 goto err;
1682
Baptiste Assmann42746372017-05-03 12:12:02 +02001683 srv->resolution->hostname_dn = srv->hostname_dn;
1684 srv->resolution->hostname_dn_len = srv->hostname_dn_len;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001685 srv->resolution->requester = srv;
1686 srv->resolution->requester_cb = snr_resolution_cb;
1687 srv->resolution->requester_error_cb = snr_resolution_error_cb;
1688 srv->resolution->status = RSLV_STATUS_NONE;
1689 srv->resolution->step = RSLV_STEP_NONE;
1690 /* a first resolution has been done by the configuration parser */
1691 srv->resolution->last_resolution = 0;
1692
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001693 if (srv->resolvers_id) {
1694 struct dns_resolvers *curr_resolvers;
1695 int found = 0;
1696
1697 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
1698 if (!strcmp(curr_resolvers->id, srv->resolvers_id)) {
1699 found = 1;
1700 break;
1701 }
1702 }
1703 if (!found)
1704 goto err;
Baptiste Assmann42746372017-05-03 12:12:02 +02001705 srv->resolvers = curr_resolvers;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001706 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001707
1708 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001709
1710 err:
1711 free(srv->hostname);
1712 srv->hostname = NULL;
Baptiste Assmann42746372017-05-03 12:12:02 +02001713 free(srv->hostname_dn);
1714 srv->hostname_dn = NULL;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001715 dns_free_resolution(dst_dns_rslt);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001716 return -1;
1717}
1718
1719static void srv_free_dns_resolution(struct server *srv)
1720{
1721 if (!srv->resolution)
1722 return;
1723
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001724 dns_free_resolution(srv->resolution);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001725 srv->resolution = NULL;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001726}
1727
1728/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001729 * Copy <src> server settings to <srv> server allocating
1730 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001731 * This function is not supposed to be called at any time, but only
1732 * during server settings parsing or during server allocations from
1733 * a server template, and just after having calloc()'ed a new server.
1734 * So, <src> may only be a default server (when parsing server settings)
1735 * or a server template (during server allocations from a server template).
1736 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1737 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001738 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001739static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001740{
1741 /* Connection source settings copy */
1742 srv_conn_src_cpy(srv, src);
1743
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001744 if (srv_tmpl) {
1745 srv->addr = src->addr;
1746 srv->svc_port = src->svc_port;
1747 }
1748
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749 srv->pp_opts = src->pp_opts;
1750 if (src->rdr_pfx != NULL) {
1751 srv->rdr_pfx = strdup(src->rdr_pfx);
1752 srv->rdr_len = src->rdr_len;
1753 }
1754 if (src->cookie != NULL) {
1755 srv->cookie = strdup(src->cookie);
1756 srv->cklen = src->cklen;
1757 }
1758 srv->use_ssl = src->use_ssl;
1759 srv->check.addr = srv->agent.addr = src->check.addr;
1760 srv->check.use_ssl = src->check.use_ssl;
1761 srv->check.port = src->check.port;
1762 /* Note: 'flags' field has potentially been already initialized. */
1763 srv->flags |= src->flags;
1764 srv->do_check = src->do_check;
1765 srv->do_agent = src->do_agent;
1766 if (srv->check.port)
1767 srv->flags |= SRV_F_CHECKPORT;
1768 srv->check.inter = src->check.inter;
1769 srv->check.fastinter = src->check.fastinter;
1770 srv->check.downinter = src->check.downinter;
1771 srv->agent.use_ssl = src->agent.use_ssl;
1772 srv->agent.port = src->agent.port;
1773 if (src->agent.send_string != NULL)
1774 srv->agent.send_string = strdup(src->agent.send_string);
1775 srv->agent.send_string_len = src->agent.send_string_len;
1776 srv->agent.inter = src->agent.inter;
1777 srv->agent.fastinter = src->agent.fastinter;
1778 srv->agent.downinter = src->agent.downinter;
1779 srv->maxqueue = src->maxqueue;
1780 srv->minconn = src->minconn;
1781 srv->maxconn = src->maxconn;
1782 srv->slowstart = src->slowstart;
1783 srv->observe = src->observe;
1784 srv->onerror = src->onerror;
1785 srv->onmarkeddown = src->onmarkeddown;
1786 srv->onmarkedup = src->onmarkedup;
1787 if (src->trackit != NULL)
1788 srv->trackit = strdup(src->trackit);
1789 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1790 srv->uweight = srv->iweight = src->iweight;
1791
1792 srv->check.send_proxy = src->check.send_proxy;
1793 /* health: up, but will fall down at first failure */
1794 srv->check.rise = srv->check.health = src->check.rise;
1795 srv->check.fall = src->check.fall;
1796
1797 /* Here we check if 'disabled' is the default server state */
1798 if (src->admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1799 srv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1800 srv->state = SRV_ST_STOPPED;
1801 srv->check.state |= CHK_ST_PAUSED;
1802 srv->check.health = 0;
1803 }
1804
1805 /* health: up but will fall down at first failure */
1806 srv->agent.rise = srv->agent.health = src->agent.rise;
1807 srv->agent.fall = src->agent.fall;
1808
1809 if (src->resolvers_id != NULL)
1810 srv->resolvers_id = strdup(src->resolvers_id);
1811 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1812 if (srv->dns_opts.family_prio == AF_UNSPEC)
1813 srv->dns_opts.family_prio = AF_INET6;
1814 memcpy(srv->dns_opts.pref_net,
1815 src->dns_opts.pref_net,
1816 sizeof srv->dns_opts.pref_net);
1817 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1818
1819 srv->init_addr_methods = src->init_addr_methods;
1820 srv->init_addr = src->init_addr;
1821#if defined(USE_OPENSSL)
1822 srv_ssl_settings_cpy(srv, src);
1823#endif
1824#ifdef TCP_USER_TIMEOUT
1825 srv->tcp_ut = src->tcp_ut;
1826#endif
1827}
1828
1829static struct server *new_server(struct proxy *proxy)
1830{
1831 struct server *srv;
1832
1833 srv = calloc(1, sizeof *srv);
1834 if (!srv)
1835 return NULL;
1836
1837 srv->obj_type = OBJ_TYPE_SERVER;
1838 srv->proxy = proxy;
1839 LIST_INIT(&srv->actconns);
1840 LIST_INIT(&srv->pendconns);
1841 LIST_INIT(&srv->priv_conns);
1842 LIST_INIT(&srv->idle_conns);
1843 LIST_INIT(&srv->safe_conns);
1844
1845 srv->state = SRV_ST_RUNNING; /* early server setup */
1846 srv->last_change = now.tv_sec;
1847
1848 srv->check.status = HCHK_STATUS_INI;
1849 srv->check.server = srv;
1850 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1851
1852 srv->agent.status = HCHK_STATUS_INI;
1853 srv->agent.server = srv;
1854 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1855
1856 return srv;
1857}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001858
1859/*
1860 * Validate <srv> server health-check settings.
1861 * Returns 0 if everything is OK, -1 if not.
1862 */
1863static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1864{
1865 struct tcpcheck_rule *r = NULL;
1866 struct list *l;
1867
1868 /*
1869 * We need at least a service port, a check port or the first tcp-check rule must
1870 * be a 'connect' one when checking an IPv4/IPv6 server.
1871 */
1872 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1873 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1874 return 0;
1875
1876 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1877 if (!r) {
1878 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1879 "Check has been disabled.\n",
1880 file, linenum, srv->id);
1881 return -1;
1882 }
1883
1884 /* search the first action (connect / send / expect) in the list */
1885 l = &srv->proxy->tcpcheck_rules;
1886 list_for_each_entry(r, l, list) {
1887 if (r->action != TCPCHK_ACT_COMMENT)
1888 break;
1889 }
1890
1891 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1892 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1893 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1894 file, linenum, srv->id);
1895 return -1;
1896 }
1897
1898 /* scan the tcp-check ruleset to ensure a port has been configured */
1899 l = &srv->proxy->tcpcheck_rules;
1900 list_for_each_entry(r, l, list) {
1901 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1902 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1903 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1904 file, linenum, srv->id);
1905 return -1;
1906 }
1907 }
1908
1909 return 0;
1910}
1911
1912/*
1913 * Initialize <srv> health-check structure.
1914 * Returns the error string in case of memory allocation failure, NULL if not.
1915 */
1916static const char *do_health_check_init(struct server *srv, int check_type, int state)
1917{
1918 const char *ret;
1919
1920 if (!srv->do_check)
1921 return NULL;
1922
1923 ret = init_check(&srv->check, check_type);
1924 if (ret)
1925 return ret;
1926
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001927 srv->check.state |= state;
1928 global.maxsock++;
1929
1930 return NULL;
1931}
1932
1933static int server_health_check_init(const char *file, int linenum,
1934 struct server *srv, struct proxy *curproxy)
1935{
1936 const char *ret;
1937
1938 if (!srv->do_check)
1939 return 0;
1940
1941 if (srv->trackit) {
1942 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1943 file, linenum);
1944 return ERR_ALERT | ERR_FATAL;
1945 }
1946
1947 if (server_healthcheck_validate(file, linenum, srv) < 0)
1948 return ERR_ALERT | ERR_ABORT;
1949
1950 /* note: check type will be set during the config review phase */
1951 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1952 if (ret) {
1953 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1954 return ERR_ALERT | ERR_ABORT;
1955 }
1956
1957 return 0;
1958}
1959
1960/*
1961 * Initialize <srv> agent check structure.
1962 * Returns the error string in case of memory allocation failure, NULL if not.
1963 */
1964static const char *do_server_agent_check_init(struct server *srv, int state)
1965{
1966 const char *ret;
1967
1968 if (!srv->do_agent)
1969 return NULL;
1970
1971 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1972 if (ret)
1973 return ret;
1974
1975 if (!srv->agent.inter)
1976 srv->agent.inter = srv->check.inter;
1977
1978 srv->agent.state |= state;
1979 global.maxsock++;
1980
1981 return NULL;
1982}
1983
1984static int server_agent_check_init(const char *file, int linenum,
1985 struct server *srv, struct proxy *curproxy)
1986{
1987 const char *ret;
1988
1989 if (!srv->do_agent)
1990 return 0;
1991
1992 if (!srv->agent.port) {
1993 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1994 file, linenum, srv->id);
1995 return ERR_ALERT | ERR_FATAL;
1996 }
1997
1998 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1999 if (ret) {
2000 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2001 return ERR_ALERT | ERR_ABORT;
2002 }
2003
2004 return 0;
2005}
2006
2007#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2008static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2009 struct server *srv, struct proxy *proxy)
2010{
2011 int ret;
2012 char *err = NULL;
2013
2014 if (!srv->sni_expr)
2015 return 0;
2016
2017 ret = server_parse_sni_expr(srv, proxy, &err);
2018 if (!ret)
2019 return 0;
2020
2021 display_parser_err(file, linenum, args, cur_arg, &err);
2022 free(err);
2023
2024 return ret;
2025}
2026#endif
2027
2028/*
2029 * Server initializations finalization.
2030 * Initialize health check, agent check and SNI expression if enabled.
2031 * Must not be called for a default server instance.
2032 */
2033static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2034 struct server *srv, struct proxy *px)
2035{
2036 int ret;
2037
2038 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2039 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2040 return ret;
2041 }
2042
2043#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2044 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2045 return ret;
2046#endif
2047
2048 if (srv->flags & SRV_F_BACKUP)
2049 px->srv_bck++;
2050 else
2051 px->srv_act++;
2052 srv_lb_commit_status(srv);
2053
2054 return 0;
2055}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002056
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002057/*
2058 * Parse as much as possible such a range string argument: low[-high]
2059 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2060 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2061 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2062 * Returns 0 if succeeded, -1 if not.
2063 */
2064static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2065{
2066 char *nb_high_arg;
2067
2068 *nb_high = 0;
2069 chunk_printf(&trash, "%s", arg);
2070 *nb_low = atoi(trash.str);
2071
2072 if ((nb_high_arg = strchr(trash.str, '-'))) {
2073 *nb_high_arg++ = '\0';
2074 *nb_high = atoi(nb_high_arg);
2075 }
2076 else {
2077 *nb_high += *nb_low;
2078 *nb_low = 1;
2079 }
2080
2081 if (*nb_low < 0 || *nb_high < *nb_low)
2082 return -1;
2083
2084 return 0;
2085}
2086
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002087static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2088{
2089 chunk_printf(&trash, "%s%d", prefix, nb);
2090 free(srv->id);
2091 srv->id = strdup(trash.str);
2092}
2093
2094/*
2095 * Initialize as much as possible servers from <srv> server template.
2096 * Note that a server template is a special server with
2097 * a few different parameters than a server which has
2098 * been parsed mostly the same way as a server.
2099 * Returns the number of servers succesfully allocated,
2100 * 'srv' template included.
2101 */
2102static int server_template_init(struct server *srv, struct proxy *px)
2103{
2104 int i;
2105 struct server *newsrv;
2106
2107 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2108 int check_init_state;
2109 int agent_init_state;
2110
2111 newsrv = new_server(px);
2112 if (!newsrv)
2113 goto err;
2114
2115 srv_settings_cpy(newsrv, srv, 1);
2116 srv_alloc_dns_resolution(newsrv, srv->hostname);
2117#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2118 if (newsrv->sni_expr) {
2119 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2120 if (!newsrv->ssl_ctx.sni)
2121 goto err;
2122 }
2123#endif
2124 /* Set this new server ID. */
2125 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2126
2127 /* Initial checks states. */
2128 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2129 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2130
2131 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2132 do_server_agent_check_init(newsrv, agent_init_state))
2133 goto err;
2134
2135 /* Linked backwards first. This will be restablished after parsing. */
2136 newsrv->next = px->srv;
2137 px->srv = newsrv;
2138 }
2139 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2140
2141 return i - srv->tmpl_info.nb_low;
2142
2143 err:
2144 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2145 if (newsrv) {
2146#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2147 release_sample_expr(newsrv->ssl_ctx.sni);
2148#endif
2149 free_check(&newsrv->agent);
2150 free_check(&newsrv->check);
2151 }
2152 free(newsrv);
2153 return i - srv->tmpl_info.nb_low;
2154}
2155
Willy Tarreau272adea2014-03-31 10:39:59 +02002156int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2157{
2158 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002159 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002160 char *errmsg = NULL;
2161 int err_code = 0;
2162 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002163 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002164
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002165 if (!strcmp(args[0], "server") ||
2166 !strcmp(args[0], "default-server") ||
2167 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002168 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002169 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002170 int srv = !defsrv && !strcmp(args[0], "server");
2171 int srv_tmpl = !defsrv && !srv;
2172 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002173
2174 if (!defsrv && curproxy == defproxy) {
2175 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2176 err_code |= ERR_ALERT | ERR_FATAL;
2177 goto out;
2178 }
2179 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
2180 err_code |= ERR_ALERT | ERR_FATAL;
2181
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002182 /* There is no mandatory first arguments for default server. */
2183 if (srv) {
2184 if (!*args[2]) {
2185 /* 'server' line number of argument check. */
2186 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2187 file, linenum, args[0]);
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
2191
2192 err = invalid_char(args[1]);
2193 }
2194 else if (srv_tmpl) {
2195 if (!*args[3]) {
2196 /* 'server-template' line number of argument check. */
2197 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2198 file, linenum, args[0]);
2199 err_code |= ERR_ALERT | ERR_FATAL;
2200 goto out;
2201 }
2202
2203 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002204 }
2205
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002206 if (err) {
2207 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
2208 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002209 err_code |= ERR_ALERT | ERR_FATAL;
2210 goto out;
2211 }
2212
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002213 cur_arg = 2;
2214 if (srv_tmpl) {
2215 /* Parse server-template <nb | range> arg. */
2216 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
2217 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
2218 file, linenum, args[0], args[cur_arg]);
2219 err_code |= ERR_ALERT | ERR_FATAL;
2220 goto out;
2221 }
2222 cur_arg++;
2223 }
2224
Willy Tarreau272adea2014-03-31 10:39:59 +02002225 if (!defsrv) {
2226 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002227 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002228 struct protocol *proto;
2229
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002230 newsrv = new_server(curproxy);
2231 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002232 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2233 err_code |= ERR_ALERT | ERR_ABORT;
2234 goto out;
2235 }
2236
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002237 if (srv_tmpl) {
2238 newsrv->tmpl_info.nb_low = tmpl_range_low;
2239 newsrv->tmpl_info.nb_high = tmpl_range_high;
2240 }
2241
Willy Tarreau272adea2014-03-31 10:39:59 +02002242 /* the servers are linked backwards first */
2243 newsrv->next = curproxy->srv;
2244 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002245 newsrv->conf.file = strdup(file);
2246 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002247 /* Note: for a server template, its id is its prefix.
2248 * This is a temporary id which will be used for server allocations to come
2249 * after parsing.
2250 */
2251 if (srv)
2252 newsrv->id = strdup(args[1]);
2253 else
2254 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002255
2256 /* several ways to check the port component :
2257 * - IP => port=+0, relative (IPv4 only)
2258 * - IP: => port=+0, relative
2259 * - IP:N => port=N, absolute
2260 * - IP:+N => port=+N, relative
2261 * - IP:-N => port=-N, relative
2262 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002263 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002264 if (!sk) {
2265 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
2266 err_code |= ERR_ALERT | ERR_FATAL;
2267 goto out;
2268 }
2269
2270 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002271 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002272 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
2273 file, linenum, args[0], args[1]);
2274 err_code |= ERR_ALERT | ERR_FATAL;
2275 goto out;
2276 }
2277
2278 if (!port1 || !port2) {
2279 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002280 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002281 }
2282 else if (port1 != port2) {
2283 /* port range */
2284 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
2285 file, linenum, args[0], args[1], args[2]);
2286 err_code |= ERR_ALERT | ERR_FATAL;
2287 goto out;
2288 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002289
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002290 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002291 if (fqdn) {
2292 if (srv_alloc_dns_resolution(newsrv, fqdn) == -1) {
2293 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2294 file, linenum, newsrv->id);
2295 err_code |= ERR_ALERT | ERR_FATAL;
2296 goto out;
2297 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002298 }
2299
Willy Tarreau272adea2014-03-31 10:39:59 +02002300 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002301 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002302
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002303 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002304 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002305 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002306 err_code |= ERR_ALERT | ERR_FATAL;
2307 goto out;
2308 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002309
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002310 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002311 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002312 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002313 } else {
2314 newsrv = &curproxy->defsrv;
2315 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002316 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002317 }
2318
2319 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002320 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002321 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2322 if (err) {
2323 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2324 file, linenum, *err, newsrv->id);
2325 err_code |= ERR_ALERT | ERR_FATAL;
2326 goto out;
2327 }
2328 if (val <= 0) {
2329 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2330 file, linenum, val, args[cur_arg], newsrv->id);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334 newsrv->agent.inter = val;
2335 cur_arg += 2;
2336 }
Misiekea849332017-01-09 09:39:51 +01002337 else if (!strcmp(args[cur_arg], "agent-addr")) {
2338 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2339 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2340 goto out;
2341 }
2342
2343 cur_arg += 2;
2344 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002345 else if (!strcmp(args[cur_arg], "agent-port")) {
2346 global.maxsock++;
2347 newsrv->agent.port = atol(args[cur_arg + 1]);
2348 cur_arg += 2;
2349 }
James Brown55f9ff12015-10-21 18:19:05 -07002350 else if (!strcmp(args[cur_arg], "agent-send")) {
2351 global.maxsock++;
2352 free(newsrv->agent.send_string);
2353 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2354 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2355 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2356 cur_arg += 2;
2357 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002358 else if (!strcmp(args[cur_arg], "init-addr")) {
2359 char *p, *end;
2360 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002361 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002362
2363 newsrv->init_addr_methods = 0;
2364 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2365
2366 for (p = args[cur_arg + 1]; *p; p = end) {
2367 /* cut on next comma */
2368 for (end = p; *end && *end != ','; end++);
2369 if (*end)
2370 *(end++) = 0;
2371
Willy Tarreau4310d362016-11-02 15:05:56 +01002372 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002373 if (!strcmp(p, "libc")) {
2374 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2375 }
2376 else if (!strcmp(p, "last")) {
2377 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2378 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002379 else if (!strcmp(p, "none")) {
2380 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2381 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002382 else if (str2ip2(p, &sa, 0)) {
2383 if (is_addr(&newsrv->init_addr)) {
2384 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2385 file, linenum, args[cur_arg], p);
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
2388 }
2389 newsrv->init_addr = sa;
2390 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2391 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002392 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002393 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002394 file, linenum, args[cur_arg], p);
2395 err_code |= ERR_ALERT | ERR_FATAL;
2396 goto out;
2397 }
2398 if (!done) {
2399 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2400 file, linenum, args[cur_arg], p);
2401 err_code |= ERR_ALERT | ERR_FATAL;
2402 goto out;
2403 }
2404 }
2405 cur_arg += 2;
2406 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002407 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002408 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002409 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2410 cur_arg += 2;
2411 }
2412 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2413 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002414 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002415 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002416 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002417 else {
2418 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2419 file, linenum, args[cur_arg]);
2420 err_code |= ERR_ALERT | ERR_FATAL;
2421 goto out;
2422 }
2423 cur_arg += 2;
2424 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002425 else if (!strcmp(args[cur_arg], "resolve-net")) {
2426 char *p, *e;
2427 unsigned char mask;
2428 struct dns_options *opt;
2429
2430 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2431 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2432 file, linenum, args[cur_arg]);
2433 err_code |= ERR_ALERT | ERR_FATAL;
2434 goto out;
2435 }
2436
2437 opt = &newsrv->dns_opts;
2438
2439 /* Split arguments by comma, and convert it from ipv4 or ipv6
2440 * string network in in_addr or in6_addr.
2441 */
2442 p = args[cur_arg + 1];
2443 e = p;
2444 while (*p != '\0') {
2445 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002446 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002447 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2448 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2449 err_code |= ERR_ALERT | ERR_FATAL;
2450 goto out;
2451 }
2452 /* look for end or comma. */
2453 while (*e != ',' && *e != '\0')
2454 e++;
2455 if (*e == ',') {
2456 *e = '\0';
2457 e++;
2458 }
2459 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2460 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2461 /* Try to convert input string from ipv4 or ipv6 network. */
2462 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2463 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2464 &mask)) {
2465 /* Try to convert input string from ipv6 network. */
2466 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2467 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2468 } else {
2469 /* All network conversions fail, retrun error. */
2470 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2471 file, linenum, args[cur_arg], p);
2472 err_code |= ERR_ALERT | ERR_FATAL;
2473 goto out;
2474 }
2475 opt->pref_net_nb++;
2476 p = e;
2477 }
2478
2479 cur_arg += 2;
2480 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002481 else if (!strcmp(args[cur_arg], "rise")) {
2482 if (!*args[cur_arg + 1]) {
2483 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2484 file, linenum, args[cur_arg]);
2485 err_code |= ERR_ALERT | ERR_FATAL;
2486 goto out;
2487 }
2488
2489 newsrv->check.rise = atol(args[cur_arg + 1]);
2490 if (newsrv->check.rise <= 0) {
2491 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2492 file, linenum, args[cur_arg]);
2493 err_code |= ERR_ALERT | ERR_FATAL;
2494 goto out;
2495 }
2496
2497 if (newsrv->check.health)
2498 newsrv->check.health = newsrv->check.rise;
2499 cur_arg += 2;
2500 }
2501 else if (!strcmp(args[cur_arg], "fall")) {
2502 newsrv->check.fall = atol(args[cur_arg + 1]);
2503
2504 if (!*args[cur_arg + 1]) {
2505 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2506 file, linenum, args[cur_arg]);
2507 err_code |= ERR_ALERT | ERR_FATAL;
2508 goto out;
2509 }
2510
2511 if (newsrv->check.fall <= 0) {
2512 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2513 file, linenum, args[cur_arg]);
2514 err_code |= ERR_ALERT | ERR_FATAL;
2515 goto out;
2516 }
2517
2518 cur_arg += 2;
2519 }
2520 else if (!strcmp(args[cur_arg], "inter")) {
2521 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2522 if (err) {
2523 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2524 file, linenum, *err, newsrv->id);
2525 err_code |= ERR_ALERT | ERR_FATAL;
2526 goto out;
2527 }
2528 if (val <= 0) {
2529 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2530 file, linenum, val, args[cur_arg], newsrv->id);
2531 err_code |= ERR_ALERT | ERR_FATAL;
2532 goto out;
2533 }
2534 newsrv->check.inter = val;
2535 cur_arg += 2;
2536 }
2537 else if (!strcmp(args[cur_arg], "fastinter")) {
2538 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2539 if (err) {
2540 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2541 file, linenum, *err, newsrv->id);
2542 err_code |= ERR_ALERT | ERR_FATAL;
2543 goto out;
2544 }
2545 if (val <= 0) {
2546 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2547 file, linenum, val, args[cur_arg], newsrv->id);
2548 err_code |= ERR_ALERT | ERR_FATAL;
2549 goto out;
2550 }
2551 newsrv->check.fastinter = val;
2552 cur_arg += 2;
2553 }
2554 else if (!strcmp(args[cur_arg], "downinter")) {
2555 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2556 if (err) {
2557 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2558 file, linenum, *err, newsrv->id);
2559 err_code |= ERR_ALERT | ERR_FATAL;
2560 goto out;
2561 }
2562 if (val <= 0) {
2563 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2564 file, linenum, val, args[cur_arg], newsrv->id);
2565 err_code |= ERR_ALERT | ERR_FATAL;
2566 goto out;
2567 }
2568 newsrv->check.downinter = val;
2569 cur_arg += 2;
2570 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002571 else if (!strcmp(args[cur_arg], "port")) {
2572 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002573 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002574 cur_arg += 2;
2575 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002576 else if (!strcmp(args[cur_arg], "weight")) {
2577 int w;
2578 w = atol(args[cur_arg + 1]);
2579 if (w < 0 || w > SRV_UWGHT_MAX) {
2580 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2581 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2582 err_code |= ERR_ALERT | ERR_FATAL;
2583 goto out;
2584 }
2585 newsrv->uweight = newsrv->iweight = w;
2586 cur_arg += 2;
2587 }
2588 else if (!strcmp(args[cur_arg], "minconn")) {
2589 newsrv->minconn = atol(args[cur_arg + 1]);
2590 cur_arg += 2;
2591 }
2592 else if (!strcmp(args[cur_arg], "maxconn")) {
2593 newsrv->maxconn = atol(args[cur_arg + 1]);
2594 cur_arg += 2;
2595 }
2596 else if (!strcmp(args[cur_arg], "maxqueue")) {
2597 newsrv->maxqueue = atol(args[cur_arg + 1]);
2598 cur_arg += 2;
2599 }
2600 else if (!strcmp(args[cur_arg], "slowstart")) {
2601 /* slowstart is stored in seconds */
2602 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2603 if (err) {
2604 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2605 file, linenum, *err, newsrv->id);
2606 err_code |= ERR_ALERT | ERR_FATAL;
2607 goto out;
2608 }
2609 newsrv->slowstart = (val + 999) / 1000;
2610 cur_arg += 2;
2611 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002612 else if (!strcmp(args[cur_arg], "on-error")) {
2613 if (!strcmp(args[cur_arg + 1], "fastinter"))
2614 newsrv->onerror = HANA_ONERR_FASTINTER;
2615 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2616 newsrv->onerror = HANA_ONERR_FAILCHK;
2617 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2618 newsrv->onerror = HANA_ONERR_SUDDTH;
2619 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2620 newsrv->onerror = HANA_ONERR_MARKDWN;
2621 else {
2622 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2623 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2624 file, linenum, args[cur_arg], args[cur_arg + 1]);
2625 err_code |= ERR_ALERT | ERR_FATAL;
2626 goto out;
2627 }
2628
2629 cur_arg += 2;
2630 }
2631 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2632 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2633 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2634 else {
2635 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2636 file, linenum, args[cur_arg], args[cur_arg + 1]);
2637 err_code |= ERR_ALERT | ERR_FATAL;
2638 goto out;
2639 }
2640
2641 cur_arg += 2;
2642 }
2643 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2644 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2645 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2646 else {
2647 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2648 file, linenum, args[cur_arg], args[cur_arg + 1]);
2649 err_code |= ERR_ALERT | ERR_FATAL;
2650 goto out;
2651 }
2652
2653 cur_arg += 2;
2654 }
2655 else if (!strcmp(args[cur_arg], "error-limit")) {
2656 if (!*args[cur_arg + 1]) {
2657 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2658 file, linenum, args[cur_arg]);
2659 err_code |= ERR_ALERT | ERR_FATAL;
2660 goto out;
2661 }
2662
2663 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2664
2665 if (newsrv->consecutive_errors_limit <= 0) {
2666 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2667 file, linenum, args[cur_arg]);
2668 err_code |= ERR_ALERT | ERR_FATAL;
2669 goto out;
2670 }
2671 cur_arg += 2;
2672 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002673 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002674 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2675 file, linenum, "usesrc", "source");
2676 err_code |= ERR_ALERT | ERR_FATAL;
2677 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002678 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002679 else {
2680 static int srv_dumped;
2681 struct srv_kw *kw;
2682 char *err;
2683
2684 kw = srv_find_kw(args[cur_arg]);
2685 if (kw) {
2686 char *err = NULL;
2687 int code;
2688
2689 if (!kw->parse) {
2690 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2691 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002692 if (kw->skip != -1)
2693 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002694 err_code |= ERR_ALERT | ERR_FATAL;
2695 goto out;
2696 }
2697
2698 if (defsrv && !kw->default_ok) {
2699 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2700 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002701 if (kw->skip != -1)
2702 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002703 err_code |= ERR_ALERT;
2704 continue;
2705 }
2706
2707 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2708 err_code |= code;
2709
2710 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002711 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002712 if (code & ERR_FATAL) {
2713 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002714 if (kw->skip != -1)
2715 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002716 goto out;
2717 }
2718 }
2719 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002720 if (kw->skip != -1)
2721 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002722 continue;
2723 }
2724
2725 err = NULL;
2726 if (!srv_dumped) {
2727 srv_dump_kws(&err);
2728 indent_msg(&err, 4);
2729 srv_dumped = 1;
2730 }
2731
2732 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2733 file, linenum, args[0], args[1], args[cur_arg],
2734 err ? " Registered keywords :" : "", err ? err : "");
2735 free(err);
2736
2737 err_code |= ERR_ALERT | ERR_FATAL;
2738 goto out;
2739 }
2740 }
2741
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002742 if (!defsrv)
2743 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2744 if (err_code & ERR_FATAL)
2745 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002746 if (srv_tmpl)
2747 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002748 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002749 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002750 return 0;
2751
2752 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002753 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002754 free(errmsg);
2755 return err_code;
2756}
2757
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002758/* Returns a pointer to the first server matching either id <id>.
2759 * NULL is returned if no match is found.
2760 * the lookup is performed in the backend <bk>
2761 */
2762struct server *server_find_by_id(struct proxy *bk, int id)
2763{
2764 struct eb32_node *eb32;
2765 struct server *curserver;
2766
2767 if (!bk || (id ==0))
2768 return NULL;
2769
2770 /* <bk> has no backend capabilities, so it can't have a server */
2771 if (!(bk->cap & PR_CAP_BE))
2772 return NULL;
2773
2774 curserver = NULL;
2775
2776 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2777 if (eb32)
2778 curserver = container_of(eb32, struct server, conf.id);
2779
2780 return curserver;
2781}
2782
2783/* Returns a pointer to the first server matching either name <name>, or id
2784 * if <name> starts with a '#'. NULL is returned if no match is found.
2785 * the lookup is performed in the backend <bk>
2786 */
2787struct server *server_find_by_name(struct proxy *bk, const char *name)
2788{
2789 struct server *curserver;
2790
2791 if (!bk || !name)
2792 return NULL;
2793
2794 /* <bk> has no backend capabilities, so it can't have a server */
2795 if (!(bk->cap & PR_CAP_BE))
2796 return NULL;
2797
2798 curserver = NULL;
2799 if (*name == '#') {
2800 curserver = server_find_by_id(bk, atoi(name + 1));
2801 if (curserver)
2802 return curserver;
2803 }
2804 else {
2805 curserver = bk->srv;
2806
2807 while (curserver && (strcmp(curserver->id, name) != 0))
2808 curserver = curserver->next;
2809
2810 if (curserver)
2811 return curserver;
2812 }
2813
2814 return NULL;
2815}
2816
2817struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2818{
2819 struct server *byname;
2820 struct server *byid;
2821
2822 if (!name && !id)
2823 return NULL;
2824
2825 if (diff)
2826 *diff = 0;
2827
2828 byname = byid = NULL;
2829
2830 if (name) {
2831 byname = server_find_by_name(bk, name);
2832 if (byname && (!id || byname->puid == id))
2833 return byname;
2834 }
2835
2836 /* remaining possibilities :
2837 * - name not set
2838 * - name set but not found
2839 * - name found but ID doesn't match
2840 */
2841 if (id) {
2842 byid = server_find_by_id(bk, id);
2843 if (byid) {
2844 if (byname) {
2845 /* use id only if forced by configuration */
2846 if (byid->flags & SRV_F_FORCED_ID) {
2847 if (diff)
2848 *diff |= 2;
2849 return byid;
2850 }
2851 else {
2852 if (diff)
2853 *diff |= 1;
2854 return byname;
2855 }
2856 }
2857
2858 /* remaining possibilities:
2859 * - name not set
2860 * - name set but not found
2861 */
2862 if (name && diff)
2863 *diff |= 2;
2864 return byid;
2865 }
2866
2867 /* id bot found */
2868 if (byname) {
2869 if (diff)
2870 *diff |= 1;
2871 return byname;
2872 }
2873 }
2874
2875 return NULL;
2876}
2877
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878/* Update a server state using the parameters available in the params list */
2879static void srv_update_state(struct server *srv, int version, char **params)
2880{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002881 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002882 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002883
2884 /* fields since version 1
2885 * and common to all other upcoming versions
2886 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002887 enum srv_state srv_op_state;
2888 enum srv_admin srv_admin_state;
2889 unsigned srv_uweight, srv_iweight;
2890 unsigned long srv_last_time_change;
2891 short srv_check_status;
2892 enum chk_result srv_check_result;
2893 int srv_check_health;
2894 int srv_check_state, srv_agent_state;
2895 int bk_f_forced_id;
2896 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002897 int fqdn_set_by_cli;
2898 const char *fqdn;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002899
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002900 fqdn = NULL;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002901 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002902 switch (version) {
2903 case 1:
2904 /*
2905 * now we can proceed with server's state update:
2906 * srv_addr: params[0]
2907 * srv_op_state: params[1]
2908 * srv_admin_state: params[2]
2909 * srv_uweight: params[3]
2910 * srv_iweight: params[4]
2911 * srv_last_time_change: params[5]
2912 * srv_check_status: params[6]
2913 * srv_check_result: params[7]
2914 * srv_check_health: params[8]
2915 * srv_check_state: params[9]
2916 * srv_agent_state: params[10]
2917 * bk_f_forced_id: params[11]
2918 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002919 * srv_fqdn: params[13]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002920 */
2921
2922 /* validating srv_op_state */
2923 p = NULL;
2924 errno = 0;
2925 srv_op_state = strtol(params[1], &p, 10);
2926 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2927 (srv_op_state != SRV_ST_STOPPED &&
2928 srv_op_state != SRV_ST_STARTING &&
2929 srv_op_state != SRV_ST_RUNNING &&
2930 srv_op_state != SRV_ST_STOPPING)) {
2931 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2932 }
2933
2934 /* validating srv_admin_state */
2935 p = NULL;
2936 errno = 0;
2937 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002938 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002939
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002940 /* inherited statuses will be recomputed later.
2941 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2942 */
2943 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002944
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002945 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2946 (srv_admin_state != 0 &&
2947 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002948 srv_admin_state != SRV_ADMF_CMAINT &&
2949 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002950 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002951 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002952 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2953 }
2954
2955 /* validating srv_uweight */
2956 p = NULL;
2957 errno = 0;
2958 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002959 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002960 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2961
2962 /* validating srv_iweight */
2963 p = NULL;
2964 errno = 0;
2965 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002966 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002967 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2968
2969 /* validating srv_last_time_change */
2970 p = NULL;
2971 errno = 0;
2972 srv_last_time_change = strtol(params[5], &p, 10);
2973 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2974 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2975
2976 /* validating srv_check_status */
2977 p = NULL;
2978 errno = 0;
2979 srv_check_status = strtol(params[6], &p, 10);
2980 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2981 (srv_check_status >= HCHK_STATUS_SIZE))
2982 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2983
2984 /* validating srv_check_result */
2985 p = NULL;
2986 errno = 0;
2987 srv_check_result = strtol(params[7], &p, 10);
2988 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2989 (srv_check_result != CHK_RES_UNKNOWN &&
2990 srv_check_result != CHK_RES_NEUTRAL &&
2991 srv_check_result != CHK_RES_FAILED &&
2992 srv_check_result != CHK_RES_PASSED &&
2993 srv_check_result != CHK_RES_CONDPASS)) {
2994 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2995 }
2996
2997 /* validating srv_check_health */
2998 p = NULL;
2999 errno = 0;
3000 srv_check_health = strtol(params[8], &p, 10);
3001 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3002 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3003
3004 /* validating srv_check_state */
3005 p = NULL;
3006 errno = 0;
3007 srv_check_state = strtol(params[9], &p, 10);
3008 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3009 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3010 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3011
3012 /* validating srv_agent_state */
3013 p = NULL;
3014 errno = 0;
3015 srv_agent_state = strtol(params[10], &p, 10);
3016 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3017 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3018 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3019
3020 /* validating bk_f_forced_id */
3021 p = NULL;
3022 errno = 0;
3023 bk_f_forced_id = strtol(params[11], &p, 10);
3024 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3025 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3026
3027 /* validating srv_f_forced_id */
3028 p = NULL;
3029 errno = 0;
3030 srv_f_forced_id = strtol(params[12], &p, 10);
3031 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3032 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3033
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003034 /* validating srv_fqdn */
3035 fqdn = params[13];
3036 if (fqdn && *fqdn == '-')
3037 fqdn = NULL;
3038 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3039 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3040 fqdn = NULL;
3041 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003042
3043 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02003044 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003045 goto out;
3046
3047 /* recover operational state and apply it to this server
3048 * and all servers tracking this one */
3049 switch (srv_op_state) {
3050 case SRV_ST_STOPPED:
3051 srv->check.health = 0;
3052 srv_set_stopped(srv, "changed from server-state after a reload");
3053 break;
3054 case SRV_ST_STARTING:
3055 srv->state = srv_op_state;
3056 break;
3057 case SRV_ST_STOPPING:
3058 srv->check.health = srv->check.rise + srv->check.fall - 1;
3059 srv_set_stopping(srv, "changed from server-state after a reload");
3060 break;
3061 case SRV_ST_RUNNING:
3062 srv->check.health = srv->check.rise + srv->check.fall - 1;
3063 srv_set_running(srv, "");
3064 break;
3065 }
3066
3067 /* When applying server state, the following rules apply:
3068 * - in case of a configuration change, we apply the setting from the new
3069 * configuration, regardless of old running state
3070 * - if no configuration change, we apply old running state only if old running
3071 * state is different from new configuration state
3072 */
3073 /* configuration has changed */
3074 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
3075 if (srv->admin & SRV_ADMF_CMAINT)
3076 srv_adm_set_maint(srv);
3077 else
3078 srv_adm_set_ready(srv);
3079 }
3080 /* configuration is the same, let's compate old running state and new conf state */
3081 else {
3082 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
3083 srv_adm_set_maint(srv);
3084 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
3085 srv_adm_set_ready(srv);
3086 }
3087 /* apply drain mode if server is currently enabled */
3088 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
3089 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003090 * (srv->iweight is the weight set up in configuration).
3091 * There are two possible reasons for FDRAIN to have been present :
3092 * - previous config weight was zero
3093 * - "set server b/s drain" was sent to the CLI
3094 *
3095 * In the first case, we simply want to drop this drain state
3096 * if the new weight is not zero anymore, meaning the administrator
3097 * has intentionally turned the weight back to a positive value to
3098 * enable the server again after an operation. In the second case,
3099 * the drain state was forced on the CLI regardless of the config's
3100 * weight so we don't want a change to the config weight to lose this
3101 * status. What this means is :
3102 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3103 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003104 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003105 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003106 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003107 }
3108
3109 srv->last_change = date.tv_sec - srv_last_time_change;
3110 srv->check.status = srv_check_status;
3111 srv->check.result = srv_check_result;
3112 srv->check.health = srv_check_health;
3113
3114 /* Only case we want to apply is removing ENABLED flag which could have been
3115 * done by the "disable health" command over the stats socket
3116 */
3117 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3118 (srv_check_state & CHK_ST_CONFIGURED) &&
3119 !(srv_check_state & CHK_ST_ENABLED))
3120 srv->check.state &= ~CHK_ST_ENABLED;
3121
3122 /* Only case we want to apply is removing ENABLED flag which could have been
3123 * done by the "disable agent" command over the stats socket
3124 */
3125 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3126 (srv_agent_state & CHK_ST_CONFIGURED) &&
3127 !(srv_agent_state & CHK_ST_ENABLED))
3128 srv->agent.state &= ~CHK_ST_ENABLED;
3129
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003130 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3131 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003132 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003133 * It means that a configuration file change has precedence over a unix socket change
3134 * for server's weight
3135 *
3136 * by default, HAProxy applies the following weight when parsing the configuration
3137 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003138 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003139 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003140 srv->uweight = srv_uweight;
3141 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142 server_recalc_eweight(srv);
3143
Willy Tarreaue5a60682016-11-09 14:54:53 +01003144 /* load server IP address */
3145 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003146
3147 if (fqdn && srv->hostname) {
3148 if (!strcmp(srv->hostname, fqdn)) {
3149 /* Here we reset the 'set from stats socket FQDN' flag
3150 * to support such transitions:
3151 * Let's say initial FQDN value is foo1 (in configuration file).
3152 * - FQDN changed from stats socket, from foo1 to foo2 value,
3153 * - FQDN changed again from file configuration (with the same previous value
3154 set from stats socket, from foo1 to foo2 value),
3155 * - reload for any other reason than a FQDN modification,
3156 * the configuration file FQDN matches the fqdn server state file value.
3157 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3158 * any futher FQDN modification.
3159 */
3160 srv->admin &= ~SRV_ADMF_HMAINT;
3161 }
3162 else {
3163 /* If the FDQN has been changed from stats socket,
3164 * apply fqdn state file value (which is the value set
3165 * from stats socket).
3166 */
3167 if (fqdn_set_by_cli) {
3168 srv_set_fqdn(srv, fqdn);
3169 srv->admin |= SRV_ADMF_HMAINT;
3170 }
3171 }
3172 }
3173
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003174 break;
3175 default:
3176 chunk_appendf(msg, ", version '%d' not supported", version);
3177 }
3178
3179 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003180 if (msg->len) {
3181 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02003182 Warning("server-state application failed for server '%s/%s'%s",
3183 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003184 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003185}
3186
3187/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3188 * For each proxy, it does the following:
3189 * - opens its server state file (either one or local one)
3190 * - read whole file, line by line
3191 * - analyse each line to check if it matches our current backend:
3192 * - backend name matches
3193 * - backend id matches if id is forced and name doesn't match
3194 * - if the server pointed by the line is found, then state is applied
3195 *
3196 * If the running backend uuid or id differs from the state file, then HAProxy reports
3197 * a warning.
3198 */
3199void apply_server_state(void)
3200{
3201 char *cur, *end;
3202 char mybuf[SRV_STATE_LINE_MAXLEN];
3203 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003204 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3205 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003206 int arg, srv_arg, version, diff;
3207 FILE *f;
3208 char *filepath;
3209 char globalfilepath[MAXPATHLEN + 1];
3210 char localfilepath[MAXPATHLEN + 1];
3211 int len, fileopenerr, globalfilepathlen, localfilepathlen;
3212 extern struct proxy *proxy;
3213 struct proxy *curproxy, *bk;
3214 struct server *srv;
3215
3216 globalfilepathlen = 0;
3217 /* create the globalfilepath variable */
3218 if (global.server_state_file) {
3219 /* absolute path or no base directory provided */
3220 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3221 len = strlen(global.server_state_file);
3222 if (len > MAXPATHLEN) {
3223 globalfilepathlen = 0;
3224 goto globalfileerror;
3225 }
3226 memcpy(globalfilepath, global.server_state_file, len);
3227 globalfilepath[len] = '\0';
3228 globalfilepathlen = len;
3229 }
3230 else if (global.server_state_base) {
3231 len = strlen(global.server_state_base);
3232 globalfilepathlen += len;
3233
3234 if (globalfilepathlen > MAXPATHLEN) {
3235 globalfilepathlen = 0;
3236 goto globalfileerror;
3237 }
3238 strncpy(globalfilepath, global.server_state_base, len);
3239 globalfilepath[globalfilepathlen] = 0;
3240
3241 /* append a slash if needed */
3242 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3243 if (globalfilepathlen + 1 > MAXPATHLEN) {
3244 globalfilepathlen = 0;
3245 goto globalfileerror;
3246 }
3247 globalfilepath[globalfilepathlen++] = '/';
3248 }
3249
3250 len = strlen(global.server_state_file);
3251 if (globalfilepathlen + len > MAXPATHLEN) {
3252 globalfilepathlen = 0;
3253 goto globalfileerror;
3254 }
3255 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3256 globalfilepathlen += len;
3257 globalfilepath[globalfilepathlen++] = 0;
3258 }
3259 }
3260 globalfileerror:
3261 if (globalfilepathlen == 0)
3262 globalfilepath[0] = '\0';
3263
3264 /* read servers state from local file */
3265 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3266 /* servers are only in backends */
3267 if (!(curproxy->cap & PR_CAP_BE))
3268 continue;
3269 fileopenerr = 0;
3270 filepath = NULL;
3271
3272 /* search server state file path and name */
3273 switch (curproxy->load_server_state_from_file) {
3274 /* read servers state from global file */
3275 case PR_SRV_STATE_FILE_GLOBAL:
3276 /* there was an error while generating global server state file path */
3277 if (globalfilepathlen == 0)
3278 continue;
3279 filepath = globalfilepath;
3280 fileopenerr = 1;
3281 break;
3282 /* this backend has its own file */
3283 case PR_SRV_STATE_FILE_LOCAL:
3284 localfilepathlen = 0;
3285 localfilepath[0] = '\0';
3286 len = 0;
3287 /* create the localfilepath variable */
3288 /* absolute path or no base directory provided */
3289 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3290 len = strlen(curproxy->server_state_file_name);
3291 if (len > MAXPATHLEN) {
3292 localfilepathlen = 0;
3293 goto localfileerror;
3294 }
3295 memcpy(localfilepath, curproxy->server_state_file_name, len);
3296 localfilepath[len] = '\0';
3297 localfilepathlen = len;
3298 }
3299 else if (global.server_state_base) {
3300 len = strlen(global.server_state_base);
3301 localfilepathlen += len;
3302
3303 if (localfilepathlen > MAXPATHLEN) {
3304 localfilepathlen = 0;
3305 goto localfileerror;
3306 }
3307 strncpy(localfilepath, global.server_state_base, len);
3308 localfilepath[localfilepathlen] = 0;
3309
3310 /* append a slash if needed */
3311 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3312 if (localfilepathlen + 1 > MAXPATHLEN) {
3313 localfilepathlen = 0;
3314 goto localfileerror;
3315 }
3316 localfilepath[localfilepathlen++] = '/';
3317 }
3318
3319 len = strlen(curproxy->server_state_file_name);
3320 if (localfilepathlen + len > MAXPATHLEN) {
3321 localfilepathlen = 0;
3322 goto localfileerror;
3323 }
3324 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3325 localfilepathlen += len;
3326 localfilepath[localfilepathlen++] = 0;
3327 }
3328 filepath = localfilepath;
3329 localfileerror:
3330 if (localfilepathlen == 0)
3331 localfilepath[0] = '\0';
3332
3333 break;
3334 case PR_SRV_STATE_FILE_NONE:
3335 default:
3336 continue;
3337 }
3338
3339 /* preload global state file */
3340 errno = 0;
3341 f = fopen(filepath, "r");
3342 if (errno && fileopenerr)
3343 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3344 if (!f)
3345 continue;
3346
3347 mybuf[0] = '\0';
3348 mybuflen = 0;
3349 version = 0;
3350
3351 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003352 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3353 Warning("Can't read first line of the server state file '%s'\n", filepath);
3354 goto fileclose;
3355 }
3356
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003357 cur = mybuf;
3358 version = atoi(cur);
3359 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3360 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003361 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003362
3363 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3364 int bk_f_forced_id = 0;
3365 int check_id = 0;
3366 int check_name = 0;
3367
3368 mybuflen = strlen(mybuf);
3369 cur = mybuf;
3370 end = cur + mybuflen;
3371
3372 bk = NULL;
3373 srv = NULL;
3374
3375 /* we need at least one character */
3376 if (mybuflen == 0)
3377 continue;
3378
3379 /* ignore blank characters at the beginning of the line */
3380 while (isspace(*cur))
3381 ++cur;
3382
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003383 /* Ignore empty or commented lines */
3384 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003385 continue;
3386
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003387 /* truncated lines */
3388 if (mybuf[mybuflen - 1] != '\n') {
3389 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003390 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003391 }
3392
3393 /* Removes trailing '\n' */
3394 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003395
3396 /* we're now ready to move the line into *srv_params[] */
3397 params[0] = cur;
3398 arg = 1;
3399 srv_arg = 0;
3400 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3401 if (isspace(*cur)) {
3402 *cur = '\0';
3403 ++cur;
3404 while (isspace(*cur))
3405 ++cur;
3406 switch (version) {
3407 case 1:
3408 /*
3409 * srv_addr: params[4] => srv_params[0]
3410 * srv_op_state: params[5] => srv_params[1]
3411 * srv_admin_state: params[6] => srv_params[2]
3412 * srv_uweight: params[7] => srv_params[3]
3413 * srv_iweight: params[8] => srv_params[4]
3414 * srv_last_time_change: params[9] => srv_params[5]
3415 * srv_check_status: params[10] => srv_params[6]
3416 * srv_check_result: params[11] => srv_params[7]
3417 * srv_check_health: params[12] => srv_params[8]
3418 * srv_check_state: params[13] => srv_params[9]
3419 * srv_agent_state: params[14] => srv_params[10]
3420 * bk_f_forced_id: params[15] => srv_params[11]
3421 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003422 * srv_fqdn: params[17] => srv_params[13]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423 */
3424 if (arg >= 4) {
3425 srv_params[srv_arg] = cur;
3426 ++srv_arg;
3427 }
3428 break;
3429 }
3430
3431 params[arg] = cur;
3432 ++arg;
3433 }
3434 else {
3435 ++cur;
3436 }
3437 }
3438
3439 /* if line is incomplete line, then ignore it.
3440 * otherwise, update useful flags */
3441 switch (version) {
3442 case 1:
3443 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3444 continue;
3445 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3446 check_id = (atoi(params[0]) == curproxy->uuid);
3447 check_name = (strcmp(curproxy->id, params[1]) == 0);
3448 break;
3449 }
3450
3451 diff = 0;
3452 bk = curproxy;
3453
3454 /* if backend can't be found, let's continue */
3455 if (!check_id && !check_name)
3456 continue;
3457 else if (!check_id && check_name) {
3458 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3459 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3460 }
3461 else if (check_id && !check_name) {
3462 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3463 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3464 /* if name doesn't match, we still want to update curproxy if the backend id
3465 * was forced in previous the previous configuration */
3466 if (!bk_f_forced_id)
3467 continue;
3468 }
3469
3470 /* look for the server by its id: param[2] */
3471 /* else look for the server by its name: param[3] */
3472 diff = 0;
3473 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3474
3475 if (!srv) {
3476 /* if no server found, then warning and continue with next line */
3477 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3478 params[3], params[2], params[0], params[1]);
3479 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3480 params[3], params[2], params[0], params[1]);
3481 continue;
3482 }
3483 else if (diff & PR_FBM_MISMATCH_ID) {
3484 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);
3485 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);
3486 }
3487 else if (diff & PR_FBM_MISMATCH_NAME) {
3488 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);
3489 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);
3490 }
3491
3492 /* now we can proceed with server's state update */
3493 srv_update_state(srv, version, srv_params);
3494 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003495fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003496 fclose(f);
3497 }
3498}
3499
Simon Horman7d09b9a2013-02-12 10:45:51 +09003500/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003501 * update a server's current IP address.
3502 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3503 * ip is in network format.
3504 * updater is a string which contains an information about the requester of the update.
3505 * updater is used if not NULL.
3506 *
3507 * A log line and a stderr warning message is generated based on server's backend options.
3508 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003509int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003510{
3511 /* generates a log line and a warning on stderr */
3512 if (1) {
3513 /* book enough space for both IPv4 and IPv6 */
3514 char oldip[INET6_ADDRSTRLEN];
3515 char newip[INET6_ADDRSTRLEN];
3516
3517 memset(oldip, '\0', INET6_ADDRSTRLEN);
3518 memset(newip, '\0', INET6_ADDRSTRLEN);
3519
3520 /* copy old IP address in a string */
3521 switch (s->addr.ss_family) {
3522 case AF_INET:
3523 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3524 break;
3525 case AF_INET6:
3526 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3527 break;
3528 };
3529
3530 /* copy new IP address in a string */
3531 switch (ip_sin_family) {
3532 case AF_INET:
3533 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3534 break;
3535 case AF_INET6:
3536 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3537 break;
3538 };
3539
3540 /* save log line into a buffer */
3541 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3542 s->proxy->id, s->id, oldip, newip, updater);
3543
3544 /* write the buffer on stderr */
3545 Warning("%s.\n", trash.str);
3546
3547 /* send a log */
3548 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3549 }
3550
3551 /* save the new IP family */
3552 s->addr.ss_family = ip_sin_family;
3553 /* save the new IP address */
3554 switch (ip_sin_family) {
3555 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003556 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003557 break;
3558 case AF_INET6:
3559 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3560 break;
3561 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003562 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003563
3564 return 0;
3565}
3566
3567/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003568 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3569 *
3570 * Caller can pass its name through <updater> to get it integrated in the response message
3571 * returned by the function.
3572 *
3573 * The function first does the following, in that order:
3574 * - validates the new addr and/or port
3575 * - checks if an update is required (new IP or port is different than current ones)
3576 * - checks the update is allowed:
3577 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3578 * - allow all changes if no CHECKS are configured
3579 * - if CHECK is configured:
3580 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3581 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3582 * conditions are met
3583 */
3584const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3585{
3586 struct sockaddr_storage sa;
3587 int ret, port_change_required;
3588 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003589 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003590 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003591 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003592
3593 msg = get_trash_chunk();
3594 chunk_reset(msg);
3595
3596 if (addr) {
3597 memset(&sa, 0, sizeof(struct sockaddr_storage));
3598 if (str2ip2(addr, &sa, 0) == NULL) {
3599 chunk_printf(msg, "Invalid addr '%s'", addr);
3600 goto out;
3601 }
3602
3603 /* changes are allowed on AF_INET* families only */
3604 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3605 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3606 goto out;
3607 }
3608
3609 /* collecting data currently setup */
3610 memset(current_addr, '\0', sizeof(current_addr));
3611 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3612 /* changes are allowed on AF_INET* families only */
3613 if ((ret != AF_INET) && (ret != AF_INET6)) {
3614 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3615 goto out;
3616 }
3617
3618 /* applying ADDR changes if required and allowed
3619 * ipcmp returns 0 when both ADDR are the same
3620 */
3621 if (ipcmp(&s->addr, &sa) == 0) {
3622 chunk_appendf(msg, "no need to change the addr");
3623 goto port;
3624 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003625 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003626 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003627
3628 /* we also need to update check's ADDR only if it uses the server's one */
3629 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003630 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003631 }
3632
3633 /* we also need to update agent ADDR only if it use the server's one */
3634 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003635 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003636 }
3637
3638 /* update report for caller */
3639 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3640 }
3641
3642 port:
3643 if (port) {
3644 char sign = '\0';
3645 char *endptr;
3646
3647 if (addr)
3648 chunk_appendf(msg, ", ");
3649
3650 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003651 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003652
3653 /* check if PORT change is required */
3654 port_change_required = 0;
3655
3656 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003657 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003658 new_port = strtol(port, &endptr, 10);
3659 if ((errno != 0) || (port == endptr)) {
3660 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3661 goto out;
3662 }
3663
3664 /* check if caller triggers a port mapped or offset */
3665 if (sign == '-' || (sign == '+')) {
3666 /* check if server currently uses port map */
3667 if (!(s->flags & SRV_F_MAPPORTS)) {
3668 /* switch from fixed port to port map mandatorily triggers
3669 * a port change */
3670 port_change_required = 1;
3671 /* check is configured
3672 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3673 * prevent PORT change if check doesn't have it's dedicated port while switching
3674 * to port mapping */
3675 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3676 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.");
3677 goto out;
3678 }
3679 }
3680 /* we're already using port maps */
3681 else {
3682 port_change_required = current_port != new_port;
3683 }
3684 }
3685 /* fixed port */
3686 else {
3687 port_change_required = current_port != new_port;
3688 }
3689
3690 /* applying PORT changes if required and update response message */
3691 if (port_change_required) {
3692 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003693 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003694 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003695
3696 /* prepare message */
3697 chunk_appendf(msg, "port changed from '");
3698 if (s->flags & SRV_F_MAPPORTS)
3699 chunk_appendf(msg, "+");
3700 chunk_appendf(msg, "%d' to '", current_port);
3701
3702 if (sign == '-') {
3703 s->flags |= SRV_F_MAPPORTS;
3704 chunk_appendf(msg, "%c", sign);
3705 /* just use for result output */
3706 new_port = -new_port;
3707 }
3708 else if (sign == '+') {
3709 s->flags |= SRV_F_MAPPORTS;
3710 chunk_appendf(msg, "%c", sign);
3711 }
3712 else {
3713 s->flags &= ~SRV_F_MAPPORTS;
3714 }
3715
3716 chunk_appendf(msg, "%d'", new_port);
3717
3718 /* we also need to update health checks port only if it uses server's realport */
3719 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3720 s->check.port = new_port;
3721 }
3722 }
3723 else {
3724 chunk_appendf(msg, "no need to change the port");
3725 }
3726 }
3727
3728out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003729 if (changed)
3730 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003731 if (updater)
3732 chunk_appendf(msg, " by '%s'", updater);
3733 chunk_appendf(msg, "\n");
3734 return msg->str;
3735}
3736
3737
3738/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003739 * update server status based on result of name resolution
3740 * returns:
3741 * 0 if server status is updated
3742 * 1 if server status has not changed
3743 */
3744int snr_update_srv_status(struct server *s)
3745{
3746 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003747 struct dns_resolvers *resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003748
3749 switch (resolution->status) {
3750 case RSLV_STATUS_NONE:
3751 /* status when HAProxy has just (re)started */
3752 trigger_resolution(s);
3753 break;
3754
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003755 case RSLV_STATUS_VALID:
3756 /*
3757 * resume health checks
3758 * server will be turned back on if health check is safe
3759 */
3760 if (!(s->admin & SRV_ADMF_RMAINT))
3761 return 1;
3762 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3763 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3764 s->proxy->id, s->id);
3765
3766 Warning("%s.\n", trash.str);
3767 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3768 return 0;
3769
3770 case RSLV_STATUS_NX:
3771 /* stop server if resolution is NX for a long enough period */
3772 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3773 if (s->admin & SRV_ADMF_RMAINT)
3774 return 1;
3775 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3776 return 0;
3777 }
3778 break;
3779
3780 case RSLV_STATUS_TIMEOUT:
3781 /* stop server if resolution is TIMEOUT for a long enough period */
3782 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3783 if (s->admin & SRV_ADMF_RMAINT)
3784 return 1;
3785 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3786 return 0;
3787 }
3788 break;
3789
3790 case RSLV_STATUS_REFUSED:
3791 /* stop server if resolution is REFUSED for a long enough period */
3792 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3793 if (s->admin & SRV_ADMF_RMAINT)
3794 return 1;
3795 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3796 return 0;
3797 }
3798 break;
3799
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003800 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003801 /* stop server if resolution is in unmatched error for a long enough period */
3802 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3803 if (s->admin & SRV_ADMF_RMAINT)
3804 return 1;
3805 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3806 return 0;
3807 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808 break;
3809 }
3810
3811 return 1;
3812}
3813
3814/*
3815 * Server Name Resolution valid response callback
3816 * It expects:
3817 * - <nameserver>: the name server which answered the valid response
3818 * - <response>: buffer containing a valid DNS response
3819 * - <response_len>: size of <response>
3820 * It performs the following actions:
3821 * - ignore response if current ip found and server family not met
3822 * - update with first new ip found if family is met and current IP is not found
3823 * returns:
3824 * 0 on error
3825 * 1 when no error or safe ignore
3826 */
Baptiste Assmann729c9012017-05-22 15:13:10 +02003827int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003828{
3829 struct server *s;
3830 void *serverip, *firstip;
3831 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003832 int ret;
3833 struct chunk *chk = get_trash_chunk();
3834
3835 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003836 firstip = NULL; /* pointer to the first valid response found */
3837 /* it will be used as the new IP if a change is required */
3838 firstip_sin_family = AF_UNSPEC;
3839 serverip = NULL; /* current server IP address */
3840
3841 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003842 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003843
3844 /* initializing server IP pointer */
3845 server_sin_family = s->addr.ss_family;
3846 switch (server_sin_family) {
3847 case AF_INET:
3848 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3849 break;
3850
3851 case AF_INET6:
3852 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3853 break;
3854
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003855 case AF_UNSPEC:
3856 break;
3857
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003858 default:
3859 goto invalid;
3860 }
3861
Baptiste Assmann729c9012017-05-22 15:13:10 +02003862 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003863 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003864 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003865
3866 switch (ret) {
3867 case DNS_UPD_NO:
3868 if (resolution->status != RSLV_STATUS_VALID) {
3869 resolution->status = RSLV_STATUS_VALID;
3870 resolution->last_status_change = now_ms;
3871 }
3872 goto stop_resolution;
3873
3874 case DNS_UPD_SRVIP_NOT_FOUND:
3875 goto save_ip;
3876
3877 case DNS_UPD_CNAME:
3878 if (resolution->status != RSLV_STATUS_VALID) {
3879 resolution->status = RSLV_STATUS_VALID;
3880 resolution->last_status_change = now_ms;
3881 }
3882 goto invalid;
3883
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003884 case DNS_UPD_NO_IP_FOUND:
3885 if (resolution->status != RSLV_STATUS_OTHER) {
3886 resolution->status = RSLV_STATUS_OTHER;
3887 resolution->last_status_change = now_ms;
3888 }
3889 goto stop_resolution;
3890
Baptiste Assmannfad03182015-10-28 02:03:32 +01003891 case DNS_UPD_NAME_ERROR:
3892 /* if this is not the last expected response, we ignore it */
3893 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3894 return 0;
3895 /* update resolution status to OTHER error type */
3896 if (resolution->status != RSLV_STATUS_OTHER) {
3897 resolution->status = RSLV_STATUS_OTHER;
3898 resolution->last_status_change = now_ms;
3899 }
3900 goto stop_resolution;
3901
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003902 default:
3903 goto invalid;
3904
3905 }
3906
3907 save_ip:
3908 nameserver->counters.update += 1;
3909 if (resolution->status != RSLV_STATUS_VALID) {
3910 resolution->status = RSLV_STATUS_VALID;
3911 resolution->last_status_change = now_ms;
3912 }
3913
3914 /* save the first ip we found */
3915 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3916 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3917
3918 stop_resolution:
3919 /* update last resolution date and time */
3920 resolution->last_resolution = now_ms;
3921 /* reset current status flag */
3922 resolution->step = RSLV_STEP_NONE;
3923 /* reset values */
3924 dns_reset_resolution(resolution);
3925
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926 dns_update_resolvers_timeout(nameserver->resolvers);
3927
3928 snr_update_srv_status(s);
3929 return 0;
3930
3931 invalid:
3932 nameserver->counters.invalid += 1;
3933 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3934 goto stop_resolution;
3935
3936 snr_update_srv_status(s);
3937 return 0;
3938}
3939
3940/*
3941 * Server Name Resolution error management callback
3942 * returns:
3943 * 0 on error
3944 * 1 when no error or safe ignore
3945 */
3946int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3947{
3948 struct server *s;
3949 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003950 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003951
3952 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003953 s = resolution->requester;
Baptiste Assmann42746372017-05-03 12:12:02 +02003954 resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003955
3956 /* can be ignored if this is not the last response */
3957 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3958 return 1;
3959 }
3960
3961 switch (error_code) {
3962 case DNS_RESP_INVALID:
3963 case DNS_RESP_WRONG_NAME:
3964 if (resolution->status != RSLV_STATUS_INVALID) {
3965 resolution->status = RSLV_STATUS_INVALID;
3966 resolution->last_status_change = now_ms;
3967 }
3968 break;
3969
3970 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003971 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003972 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003973 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003974 case DNS_RESP_CNAME_ERROR:
Baptiste Assmann42746372017-05-03 12:12:02 +02003975 res_preferred_afinet = s->dns_opts.family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3976 res_preferred_afinet6 = s->dns_opts.family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003977
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003978 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003979 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003980 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003981 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003982 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003983 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003984 }
3985 else if (res_preferred_afinet) {
3986 /* fallback from A to AAAA */
3987 resolution->query_type = DNS_RTYPE_AAAA;
3988 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003989 else {
3990 resolution->try -= 1;
Baptiste Assmann42746372017-05-03 12:12:02 +02003991 if (s->dns_opts.family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003992 resolution->query_type = DNS_RTYPE_A;
3993 } else {
3994 resolution->query_type = DNS_RTYPE_AAAA;
3995 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003996 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003997
3998 dns_send_query(resolution);
3999
4000 /*
4001 * move the resolution to the last element of the FIFO queue
4002 * and update timeout wakeup based on the new first entry
4003 */
4004 if (dns_check_resolution_queue(resolvers) > 1) {
4005 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02004006 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004007 /* ex first resolution goes to the end of the queue */
4008 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
4009 }
4010 dns_update_resolvers_timeout(resolvers);
4011 goto leave;
4012 }
4013 else {
4014 if (resolution->status != RSLV_STATUS_OTHER) {
4015 resolution->status = RSLV_STATUS_OTHER;
4016 resolution->last_status_change = now_ms;
4017 }
4018 }
4019 break;
4020
4021 case DNS_RESP_NX_DOMAIN:
4022 if (resolution->status != RSLV_STATUS_NX) {
4023 resolution->status = RSLV_STATUS_NX;
4024 resolution->last_status_change = now_ms;
4025 }
4026 break;
4027
4028 case DNS_RESP_REFUSED:
4029 if (resolution->status != RSLV_STATUS_REFUSED) {
4030 resolution->status = RSLV_STATUS_REFUSED;
4031 resolution->last_status_change = now_ms;
4032 }
4033 break;
4034
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004035 case DNS_RESP_TIMEOUT:
4036 if (resolution->status != RSLV_STATUS_TIMEOUT) {
4037 resolution->status = RSLV_STATUS_TIMEOUT;
4038 resolution->last_status_change = now_ms;
4039 }
4040 break;
4041 }
4042
4043 /* update last resolution date and time */
4044 resolution->last_resolution = now_ms;
4045 /* reset current status flag */
4046 resolution->step = RSLV_STEP_NONE;
4047 /* reset values */
4048 dns_reset_resolution(resolution);
4049
4050 LIST_DEL(&resolution->list);
4051 dns_update_resolvers_timeout(resolvers);
4052
4053 leave:
4054 snr_update_srv_status(s);
4055 return 1;
4056}
4057
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004058/*
4059 * Function to check if <ip> is already affected to a server in the backend
4060 * which owns <srv>.
4061 * It returns a pointer to the first server found or NULL if <ip> is not yet
4062 * assigned.
4063 * NOTE: <ip> and <ip_family> are provided by a 'struct rec' available in dns.c.
4064 */
4065struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4066{
4067 struct server *tmpsrv;
4068 struct proxy *be;
4069
4070 if (!srv)
4071 return NULL;
4072
4073 be = srv->proxy;
4074 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
4075 /* We want to compare the IP in the record with the IP of the servers in the
4076 * same backend, only if:
4077 * * DNS resolution is enabled on the server
4078 * * the hostname used for the resolution by our server is the same than the
4079 * one used for the server found in the backend
4080 * * the server found in the backend is not our current server
4081 */
4082 if ((tmpsrv->resolution == NULL) ||
4083 (srv->resolution->hostname_dn_len != tmpsrv->resolution->hostname_dn_len) ||
4084 (strcmp(srv->resolution->hostname_dn, tmpsrv->resolution->hostname_dn) != 0) ||
4085 (srv->puid == tmpsrv->puid))
4086 continue;
4087
4088 /* At this point, we have 2 different servers using the same DNS hostname
4089 * for their respective resolution.
4090 */
4091 if (*ip_family == tmpsrv->addr.ss_family &&
4092 ((tmpsrv->addr.ss_family == AF_INET &&
4093 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4094 (tmpsrv->addr.ss_family == AF_INET6 &&
4095 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
4096 return tmpsrv;
4097 }
4098 }
4099
4100 return NULL;
4101}
4102
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004103/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4104 * resolver. This is suited for initial address configuration. Returns 0 on
4105 * success otherwise a non-zero error code. In case of error, *err_code, if
4106 * not NULL, is filled up.
4107 */
4108int srv_set_addr_via_libc(struct server *srv, int *err_code)
4109{
4110 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004111 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004112 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004113 return 1;
4114 }
4115 return 0;
4116}
4117
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004118/* Set the server's FDQN (->hostname) from <hostname>.
4119 * Returns -1 if failed, 0 if not.
4120 */
4121int srv_set_fqdn(struct server *srv, const char *hostname)
4122{
4123 srv_free_dns_resolution(srv);
4124
4125 return srv_alloc_dns_resolution(srv, hostname);
4126}
4127
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004128/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4129 * from the state file. This is suited for initial address configuration.
4130 * Returns 0 on success otherwise a non-zero error code. In case of error,
4131 * *err_code, if not NULL, is filled up.
4132 */
4133static int srv_apply_lastaddr(struct server *srv, int *err_code)
4134{
4135 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4136 if (err_code)
4137 *err_code |= ERR_WARN;
4138 return 1;
4139 }
4140 return 0;
4141}
4142
Willy Tarreau25e51522016-11-04 15:10:17 +01004143/* returns 0 if no error, otherwise a combination of ERR_* flags */
4144static int srv_iterate_initaddr(struct server *srv)
4145{
4146 int return_code = 0;
4147 int err_code;
4148 unsigned int methods;
4149
4150 methods = srv->init_addr_methods;
4151 if (!methods) { // default to "last,libc"
4152 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4153 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4154 }
4155
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004156 /* "-dr" : always append "none" so that server addresses resolution
4157 * failures are silently ignored, this is convenient to validate some
4158 * configs out of their environment.
4159 */
4160 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4161 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4162
Willy Tarreau25e51522016-11-04 15:10:17 +01004163 while (methods) {
4164 err_code = 0;
4165 switch (srv_get_next_initaddr(&methods)) {
4166 case SRV_IADDR_LAST:
4167 if (!srv->lastaddr)
4168 continue;
4169 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004170 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004171 return_code |= err_code;
4172 break;
4173
4174 case SRV_IADDR_LIBC:
4175 if (!srv->hostname)
4176 continue;
4177 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004178 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004179 return_code |= err_code;
4180 break;
4181
Willy Tarreau37ebe122016-11-04 15:17:58 +01004182 case SRV_IADDR_NONE:
4183 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004184 if (return_code) {
4185 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4186 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4187 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004188 return return_code;
4189
Willy Tarreau4310d362016-11-02 15:05:56 +01004190 case SRV_IADDR_IP:
4191 ipcpy(&srv->init_addr, &srv->addr);
4192 if (return_code) {
4193 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4194 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4195 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004196 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004197
Willy Tarreau25e51522016-11-04 15:10:17 +01004198 default: /* unhandled method */
4199 break;
4200 }
4201 }
4202
4203 if (!return_code) {
4204 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
4205 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4206 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004207 else {
4208 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
4209 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4210 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004211
4212 return_code |= ERR_ALERT | ERR_FATAL;
4213 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004214out:
4215 srv_set_dyncookie(srv);
4216 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004217}
4218
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004219/*
4220 * This function parses all backends and all servers within each backend
4221 * and performs servers' addr resolution based on information provided by:
4222 * - configuration file
4223 * - server-state file (states provided by an 'old' haproxy process)
4224 *
4225 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4226 */
4227int srv_init_addr(void)
4228{
4229 struct proxy *curproxy;
4230 int return_code = 0;
4231
4232 curproxy = proxy;
4233 while (curproxy) {
4234 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004235
4236 /* servers are in backend only */
4237 if (!(curproxy->cap & PR_CAP_BE))
4238 goto srv_init_addr_next;
4239
Willy Tarreau25e51522016-11-04 15:10:17 +01004240 for (srv = curproxy->srv; srv; srv = srv->next)
4241 if (srv->hostname)
4242 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004243
4244 srv_init_addr_next:
4245 curproxy = curproxy->next;
4246 }
4247
4248 return return_code;
4249}
4250
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004251const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
4252{
4253
4254 struct chunk *msg;
4255
4256 msg = get_trash_chunk();
4257 chunk_reset(msg);
4258
4259 if (!strcmp(fqdn, server->hostname)) {
4260 chunk_appendf(msg, "no need to change the FDQN");
4261 goto out;
4262 }
4263
4264 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4265 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4266 goto out;
4267 }
4268
4269 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4270 server->proxy->id, server->id, server->hostname, fqdn);
4271
4272 if (srv_set_fqdn(server, fqdn) < 0) {
4273 chunk_reset(msg);
4274 chunk_appendf(msg, "could not update %s/%s FQDN",
4275 server->proxy->id, server->id);
4276 goto out;
4277 }
4278
4279 /* Flag as FQDN set from stats socket. */
4280 server->admin |= SRV_ADMF_HMAINT;
4281
4282 out:
4283 if (updater)
4284 chunk_appendf(msg, " by '%s'", updater);
4285 chunk_appendf(msg, "\n");
4286
4287 return msg->str;
4288}
4289
4290
Willy Tarreau21b069d2016-11-23 17:15:08 +01004291/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4292 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004293 * 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 +01004294 * used for CLI commands requiring a server name.
4295 * Important: the <arg> is modified to remove the '/'.
4296 */
4297struct server *cli_find_server(struct appctx *appctx, char *arg)
4298{
4299 struct proxy *px;
4300 struct server *sv;
4301 char *line;
4302
4303 /* split "backend/server" and make <line> point to server */
4304 for (line = arg; *line; line++)
4305 if (*line == '/') {
4306 *line++ = '\0';
4307 break;
4308 }
4309
4310 if (!*line || !*arg) {
4311 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004312 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004313 return NULL;
4314 }
4315
4316 if (!get_backend_server(arg, line, &px, &sv)) {
4317 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004318 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004319 return NULL;
4320 }
4321
4322 if (px->state == PR_STSTOPPED) {
4323 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004324 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004325 return NULL;
4326 }
4327
4328 return sv;
4329}
4330
William Lallemand222baf22016-11-19 02:00:33 +01004331
4332static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4333{
4334 struct server *sv;
4335 const char *warning;
4336
4337 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4338 return 1;
4339
4340 sv = cli_find_server(appctx, args[2]);
4341 if (!sv)
4342 return 1;
4343
4344 if (strcmp(args[3], "weight") == 0) {
4345 warning = server_parse_weight_change_request(sv, args[4]);
4346 if (warning) {
4347 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004348 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004349 }
4350 }
4351 else if (strcmp(args[3], "state") == 0) {
4352 if (strcmp(args[4], "ready") == 0)
4353 srv_adm_set_ready(sv);
4354 else if (strcmp(args[4], "drain") == 0)
4355 srv_adm_set_drain(sv);
4356 else if (strcmp(args[4], "maint") == 0)
4357 srv_adm_set_maint(sv);
4358 else {
4359 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004360 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004361 }
4362 }
4363 else if (strcmp(args[3], "health") == 0) {
4364 if (sv->track) {
4365 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004366 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004367 }
4368 else if (strcmp(args[4], "up") == 0) {
4369 sv->check.health = sv->check.rise + sv->check.fall - 1;
4370 srv_set_running(sv, "changed from CLI");
4371 }
4372 else if (strcmp(args[4], "stopping") == 0) {
4373 sv->check.health = sv->check.rise + sv->check.fall - 1;
4374 srv_set_stopping(sv, "changed from CLI");
4375 }
4376 else if (strcmp(args[4], "down") == 0) {
4377 sv->check.health = 0;
4378 srv_set_stopped(sv, "changed from CLI");
4379 }
4380 else {
4381 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004382 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004383 }
4384 }
4385 else if (strcmp(args[3], "agent") == 0) {
4386 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4387 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004388 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004389 }
4390 else if (strcmp(args[4], "up") == 0) {
4391 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4392 srv_set_running(sv, "changed from CLI");
4393 }
4394 else if (strcmp(args[4], "down") == 0) {
4395 sv->agent.health = 0;
4396 srv_set_stopped(sv, "changed from CLI");
4397 }
4398 else {
4399 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004400 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004401 }
4402 }
Misiek2da082d2017-01-09 09:40:42 +01004403 else if (strcmp(args[3], "agent-addr") == 0) {
4404 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4405 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4406 appctx->st0 = CLI_ST_PRINT;
4407 } else {
4408 if (str2ip(args[4], &sv->agent.addr) == NULL) {
4409 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4410 appctx->st0 = CLI_ST_PRINT;
4411 }
4412 }
4413 }
4414 else if (strcmp(args[3], "agent-send") == 0) {
4415 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4416 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4417 appctx->st0 = CLI_ST_PRINT;
4418 } else {
4419 char *nss = strdup(args[4]);
4420 if (!nss) {
4421 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4422 appctx->st0 = CLI_ST_PRINT;
4423 } else {
4424 free(sv->agent.send_string);
4425 sv->agent.send_string = nss;
4426 sv->agent.send_string_len = strlen(args[4]);
4427 }
4428 }
4429 }
William Lallemand222baf22016-11-19 02:00:33 +01004430 else if (strcmp(args[3], "check-port") == 0) {
4431 int i = 0;
4432 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
4433 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004434 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004435 }
4436 if ((i < 0) || (i > 65535)) {
4437 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004438 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004439 }
4440 /* prevent the update of port to 0 if MAPPORTS are in use */
4441 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
4442 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004443 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004444 return 1;
4445 }
4446 sv->check.port = i;
4447 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004448 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004449 }
4450 else if (strcmp(args[3], "addr") == 0) {
4451 char *addr = NULL;
4452 char *port = NULL;
4453 if (strlen(args[4]) == 0) {
4454 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004455 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004456 return 1;
4457 }
4458 else {
4459 addr = args[4];
4460 }
4461 if (strcmp(args[5], "port") == 0) {
4462 port = args[6];
4463 }
4464 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4465 if (warning) {
4466 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004467 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004468 }
4469 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4470 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004471 else if (strcmp(args[3], "fqdn") == 0) {
4472 if (!*args[4]) {
4473 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4474 appctx->st0 = CLI_ST_PRINT;
4475 return 1;
4476 }
4477 warning = update_server_fqdn(sv, args[4], "stats socket command");
4478 if (warning) {
4479 appctx->ctx.cli.msg = warning;
4480 appctx->st0 = CLI_ST_PRINT;
4481 }
4482 }
William Lallemand222baf22016-11-19 02:00:33 +01004483 else {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004484 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 +01004485 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004486 }
4487 return 1;
4488}
4489
William Lallemand6b160942016-11-22 12:34:35 +01004490static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4491{
4492 struct stream_interface *si = appctx->owner;
4493 struct proxy *px;
4494 struct server *sv;
4495 char *line;
4496
4497
4498 /* split "backend/server" and make <line> point to server */
4499 for (line = args[2]; *line; line++)
4500 if (*line == '/') {
4501 *line++ = '\0';
4502 break;
4503 }
4504
4505 if (!*line) {
4506 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004507 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004508 return 1;
4509 }
4510
4511 if (!get_backend_server(args[2], line, &px, &sv)) {
4512 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004513 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004514 return 1;
4515 }
4516
4517 /* return server's effective weight at the moment */
4518 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004519 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004520 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004521 return 0;
4522 }
William Lallemand6b160942016-11-22 12:34:35 +01004523 return 1;
4524}
4525
4526static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4527{
4528 struct server *sv;
4529 const char *warning;
4530
4531 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4532 return 1;
4533
4534 sv = cli_find_server(appctx, args[2]);
4535 if (!sv)
4536 return 1;
4537
4538 warning = server_parse_weight_change_request(sv, args[3]);
4539 if (warning) {
4540 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004541 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004542 }
4543 return 1;
4544}
4545
Willy Tarreaub8026272016-11-23 11:26:56 +01004546/* parse a "set maxconn server" command. It always returns 1. */
4547static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4548{
4549 struct server *sv;
4550 const char *warning;
4551
4552 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4553 return 1;
4554
4555 sv = cli_find_server(appctx, args[3]);
4556 if (!sv)
4557 return 1;
4558
4559 warning = server_parse_maxconn_change_request(sv, args[4]);
4560 if (warning) {
4561 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004562 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004563 }
4564 return 1;
4565}
William Lallemand6b160942016-11-22 12:34:35 +01004566
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004567/* parse a "disable agent" command. It always returns 1. */
4568static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4569{
4570 struct server *sv;
4571
4572 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4573 return 1;
4574
4575 sv = cli_find_server(appctx, args[2]);
4576 if (!sv)
4577 return 1;
4578
4579 sv->agent.state &= ~CHK_ST_ENABLED;
4580 return 1;
4581}
4582
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004583/* parse a "disable health" command. It always returns 1. */
4584static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4585{
4586 struct server *sv;
4587
4588 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4589 return 1;
4590
4591 sv = cli_find_server(appctx, args[2]);
4592 if (!sv)
4593 return 1;
4594
4595 sv->check.state &= ~CHK_ST_ENABLED;
4596 return 1;
4597}
4598
Willy Tarreauffb4d582016-11-24 12:47:00 +01004599/* parse a "disable server" command. It always returns 1. */
4600static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4601{
4602 struct server *sv;
4603
4604 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4605 return 1;
4606
4607 sv = cli_find_server(appctx, args[2]);
4608 if (!sv)
4609 return 1;
4610
4611 srv_adm_set_maint(sv);
4612 return 1;
4613}
4614
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004615/* parse a "enable agent" command. It always returns 1. */
4616static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4617{
4618 struct server *sv;
4619
4620 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4621 return 1;
4622
4623 sv = cli_find_server(appctx, args[2]);
4624 if (!sv)
4625 return 1;
4626
4627 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4628 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004629 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004630 return 1;
4631 }
4632
4633 sv->agent.state |= CHK_ST_ENABLED;
4634 return 1;
4635}
4636
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004637/* parse a "enable health" command. It always returns 1. */
4638static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4639{
4640 struct server *sv;
4641
4642 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4643 return 1;
4644
4645 sv = cli_find_server(appctx, args[2]);
4646 if (!sv)
4647 return 1;
4648
4649 sv->check.state |= CHK_ST_ENABLED;
4650 return 1;
4651}
4652
Willy Tarreauffb4d582016-11-24 12:47:00 +01004653/* parse a "enable server" command. It always returns 1. */
4654static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4655{
4656 struct server *sv;
4657
4658 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4659 return 1;
4660
4661 sv = cli_find_server(appctx, args[2]);
4662 if (!sv)
4663 return 1;
4664
4665 srv_adm_set_ready(sv);
4666 return 1;
4667}
4668
William Lallemand222baf22016-11-19 02:00:33 +01004669/* register cli keywords */
4670static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004671 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004672 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004673 { { "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 +01004674 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004675 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004676 { { "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 +01004677 { { "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 +01004678 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004679 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4680 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4681
William Lallemand222baf22016-11-19 02:00:33 +01004682 {{},}
4683}};
4684
4685__attribute__((constructor))
4686static void __server_init(void)
4687{
4688 cli_register_kw(&cli_kws);
4689}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004690
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004691/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004692 * Local variables:
4693 * c-indent-level: 8
4694 * c-basic-offset: 8
4695 * End:
4696 */