blob: b5d889005e2d8cf255764553db52e4e3983bddf0 [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);
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Willy Tarreau21faa912012-10-10 08:27:36 +020050/* List head of all known server keywords */
51static struct srv_kw_list srv_keywords = {
52 .list = LIST_HEAD_INIT(srv_keywords.list)
53};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054
Simon Hormana3608442013-11-01 16:46:15 +090055int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020056{
Willy Tarreau892337c2014-05-13 23:41:20 +020057 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058 return s->down_time;
59
60 return now.tv_sec - s->last_change + s->down_time;
61}
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050063int srv_lastsession(const struct server *s)
64{
65 if (s->counters.last_sess)
66 return now.tv_sec - s->counters.last_sess;
67
68 return -1;
69}
70
Simon Horman4a741432013-02-23 15:35:38 +090071int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020072{
Simon Horman4a741432013-02-23 15:35:38 +090073 const struct server *s = check->server;
74
Willy Tarreauff5ae352013-12-11 20:36:34 +010075 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090076 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010077
Willy Tarreau892337c2014-05-13 23:41:20 +020078 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090079 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080
Simon Horman4a741432013-02-23 15:35:38 +090081 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082}
83
Olivier Houchard4e694042017-03-14 20:01:29 +010084void srv_set_dyncookie(struct server *s)
85{
86 struct proxy *p = s->proxy;
87 struct server *tmpserv;
88 char *tmpbuf;
89 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010090 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010091 size_t buffer_len;
92 int addr_len;
93 int port;
94
95 if ((s->flags & SRV_F_COOKIESET) ||
96 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
97 s->proxy->dyncookie_key == NULL)
98 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010099 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100100
101 if (s->addr.ss_family != AF_INET &&
102 s->addr.ss_family != AF_INET6)
103 return;
104 /*
105 * Buffer to calculate the cookie value.
106 * The buffer contains the secret key + the server IP address
107 * + the TCP port.
108 */
109 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
110 /*
111 * The TCP port should use only 2 bytes, but is stored in
112 * an unsigned int in struct server, so let's use 4, to be
113 * on the safe side.
114 */
115 buffer_len = key_len + addr_len + 4;
116 tmpbuf = trash.str;
117 memcpy(tmpbuf, p->dyncookie_key, key_len);
118 memcpy(&(tmpbuf[key_len]),
119 s->addr.ss_family == AF_INET ?
120 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
121 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
122 addr_len);
123 /*
124 * Make sure it's the same across all the load balancers,
125 * no matter their endianness.
126 */
127 port = htonl(s->svc_port);
128 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
129 hash_value = XXH64(tmpbuf, buffer_len, 0);
130 memprintf(&s->cookie, "%016llx", hash_value);
131 if (!s->cookie)
132 return;
133 s->cklen = 16;
134 /*
135 * Check that we did not get a hash collision.
136 * Unlikely, but it can happen.
137 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200138 for (tmpserv = p->srv; tmpserv != NULL;
139 tmpserv = tmpserv->next) {
140 if (tmpserv == s)
141 continue;
142 if (tmpserv->cookie &&
143 strcmp(tmpserv->cookie, s->cookie) == 0) {
144 Warning("We generated two equal cookies for two different servers.\n"
145 "Please change the secret key for '%s'.\n",
146 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100147 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200148 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100149}
150
Willy Tarreau21faa912012-10-10 08:27:36 +0200151/*
152 * Registers the server keyword list <kwl> as a list of valid keywords for next
153 * parsing sessions.
154 */
155void srv_register_keywords(struct srv_kw_list *kwl)
156{
157 LIST_ADDQ(&srv_keywords.list, &kwl->list);
158}
159
160/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
161 * keyword is found with a NULL ->parse() function, then an attempt is made to
162 * find one with a valid ->parse() function. This way it is possible to declare
163 * platform-dependant, known keywords as NULL, then only declare them as valid
164 * if some options are met. Note that if the requested keyword contains an
165 * opening parenthesis, everything from this point is ignored.
166 */
167struct srv_kw *srv_find_kw(const char *kw)
168{
169 int index;
170 const char *kwend;
171 struct srv_kw_list *kwl;
172 struct srv_kw *ret = NULL;
173
174 kwend = strchr(kw, '(');
175 if (!kwend)
176 kwend = kw + strlen(kw);
177
178 list_for_each_entry(kwl, &srv_keywords.list, list) {
179 for (index = 0; kwl->kw[index].kw != NULL; index++) {
180 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
181 kwl->kw[index].kw[kwend-kw] == 0) {
182 if (kwl->kw[index].parse)
183 return &kwl->kw[index]; /* found it !*/
184 else
185 ret = &kwl->kw[index]; /* may be OK */
186 }
187 }
188 }
189 return ret;
190}
191
192/* Dumps all registered "server" keywords to the <out> string pointer. The
193 * unsupported keywords are only dumped if their supported form was not
194 * found.
195 */
196void srv_dump_kws(char **out)
197{
198 struct srv_kw_list *kwl;
199 int index;
200
201 *out = NULL;
202 list_for_each_entry(kwl, &srv_keywords.list, list) {
203 for (index = 0; kwl->kw[index].kw != NULL; index++) {
204 if (kwl->kw[index].parse ||
205 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
206 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
207 kwl->scope,
208 kwl->kw[index].kw,
209 kwl->kw[index].skip ? " <arg>" : "",
210 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
211 kwl->kw[index].parse ? "" : " (not supported)");
212 }
213 }
214 }
215}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100216
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100217/* Parse the "addr" server keyword */
218static int srv_parse_addr(char **args, int *cur_arg,
219 struct proxy *curproxy, struct server *newsrv, char **err)
220{
221 char *errmsg, *arg;
222 struct sockaddr_storage *sk;
223 int port1, port2;
224 struct protocol *proto;
225
226 errmsg = NULL;
227 arg = args[*cur_arg + 1];
228
229 if (!*arg) {
230 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
231 goto err;
232 }
233
234 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
235 if (!sk) {
236 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
237 goto err;
238 }
239
240 proto = protocol_by_family(sk->ss_family);
241 if (!proto || !proto->connect) {
242 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
243 args[*cur_arg], arg);
244 goto err;
245 }
246
247 if (port1 != port2) {
248 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
249 args[*cur_arg], arg);
250 goto err;
251 }
252
253 newsrv->check.addr = newsrv->agent.addr = *sk;
254 newsrv->flags |= SRV_F_CHECKADDR;
255 newsrv->flags |= SRV_F_AGENTADDR;
256
257 return 0;
258
259 err:
260 free(errmsg);
261 return ERR_ALERT | ERR_FATAL;
262}
263
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100264/* Parse the "agent-check" server keyword */
265static int srv_parse_agent_check(char **args, int *cur_arg,
266 struct proxy *curproxy, struct server *newsrv, char **err)
267{
268 newsrv->do_agent = 1;
269 return 0;
270}
271
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100272/* Parse the "backup" server keyword */
273static int srv_parse_backup(char **args, int *cur_arg,
274 struct proxy *curproxy, struct server *newsrv, char **err)
275{
276 newsrv->flags |= SRV_F_BACKUP;
277 return 0;
278}
279
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100280/* Parse the "check" server keyword */
281static int srv_parse_check(char **args, int *cur_arg,
282 struct proxy *curproxy, struct server *newsrv, char **err)
283{
284 newsrv->do_check = 1;
285 return 0;
286}
287
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100288/* Parse the "check-send-proxy" server keyword */
289static int srv_parse_check_send_proxy(char **args, int *cur_arg,
290 struct proxy *curproxy, struct server *newsrv, char **err)
291{
292 newsrv->check.send_proxy = 1;
293 return 0;
294}
295
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100296/* Parse the "cookie" server keyword */
297static int srv_parse_cookie(char **args, int *cur_arg,
298 struct proxy *curproxy, struct server *newsrv, char **err)
299{
300 char *arg;
301
302 arg = args[*cur_arg + 1];
303 if (!*arg) {
304 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
305 return ERR_ALERT | ERR_FATAL;
306 }
307
308 free(newsrv->cookie);
309 newsrv->cookie = strdup(arg);
310 newsrv->cklen = strlen(arg);
311 newsrv->flags |= SRV_F_COOKIESET;
312 return 0;
313}
314
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100315/* Parse the "disabled" server keyword */
316static int srv_parse_disabled(char **args, int *cur_arg,
317 struct proxy *curproxy, struct server *newsrv, char **err)
318{
319 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
320 newsrv->state = SRV_ST_STOPPED;
321 newsrv->check.state |= CHK_ST_PAUSED;
322 newsrv->check.health = 0;
323 return 0;
324}
325
326/* Parse the "enabled" server keyword */
327static int srv_parse_enabled(char **args, int *cur_arg,
328 struct proxy *curproxy, struct server *newsrv, char **err)
329{
330 newsrv->admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
331 newsrv->state = SRV_ST_RUNNING;
332 newsrv->check.state &= ~CHK_ST_PAUSED;
333 newsrv->check.health = newsrv->check.rise;
334 return 0;
335}
336
Willy Tarreaudff55432012-10-10 17:51:05 +0200337/* parse the "id" server keyword */
338static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
339{
340 struct eb32_node *node;
341
342 if (!*args[*cur_arg + 1]) {
343 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
344 return ERR_ALERT | ERR_FATAL;
345 }
346
347 newsrv->puid = atol(args[*cur_arg + 1]);
348 newsrv->conf.id.key = newsrv->puid;
349
350 if (newsrv->puid <= 0) {
351 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
352 return ERR_ALERT | ERR_FATAL;
353 }
354
355 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
356 if (node) {
357 struct server *target = container_of(node, struct server, conf.id);
358 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
359 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
360 target->id);
361 return ERR_ALERT | ERR_FATAL;
362 }
363
364 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200365 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200366 return 0;
367}
368
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100369/* Parse the "namespace" server keyword */
370static int srv_parse_namespace(char **args, int *cur_arg,
371 struct proxy *curproxy, struct server *newsrv, char **err)
372{
373#ifdef CONFIG_HAP_NS
374 char *arg;
375
376 arg = args[*cur_arg + 1];
377 if (!*arg) {
378 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
379 return ERR_ALERT | ERR_FATAL;
380 }
381
382 if (!strcmp(arg, "*")) {
383 /* Use the namespace associated with the connection (if present). */
384 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
385 return 0;
386 }
387
388 /*
389 * As this parser may be called several times for the same 'default-server'
390 * object, or for a new 'server' instance deriving from a 'default-server'
391 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
392 */
393 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
394
395 newsrv->netns = netns_store_lookup(arg, strlen(arg));
396 if (!newsrv->netns)
397 newsrv->netns = netns_store_insert(arg);
398
399 if (!newsrv->netns) {
400 memprintf(err, "Cannot open namespace '%s'", arg);
401 return ERR_ALERT | ERR_FATAL;
402 }
403
404 return 0;
405#else
406 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
407 return ERR_ALERT | ERR_FATAL;
408#endif
409}
410
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100411/* Parse the "no-agent-check" server keyword */
412static int srv_parse_no_agent_check(char **args, int *cur_arg,
413 struct proxy *curproxy, struct server *newsrv, char **err)
414{
415 free_check(&newsrv->agent);
416 newsrv->agent.inter = 0;
417 newsrv->agent.port = 0;
418 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
419 newsrv->do_agent = 0;
420 return 0;
421}
422
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100423/* Parse the "no-backup" server keyword */
424static int srv_parse_no_backup(char **args, int *cur_arg,
425 struct proxy *curproxy, struct server *newsrv, char **err)
426{
427 newsrv->flags &= ~SRV_F_BACKUP;
428 return 0;
429}
430
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100431/* Parse the "no-check" server keyword */
432static int srv_parse_no_check(char **args, int *cur_arg,
433 struct proxy *curproxy, struct server *newsrv, char **err)
434{
435 free_check(&newsrv->check);
436 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
437 newsrv->do_check = 0;
438 return 0;
439}
440
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100441/* Parse the "no-check-send-proxy" server keyword */
442static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
443 struct proxy *curproxy, struct server *newsrv, char **err)
444{
445 newsrv->check.send_proxy = 0;
446 return 0;
447}
448
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100449/* Disable server PROXY protocol flags. */
450static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
451{
452 srv->pp_opts &= ~flags;
453 return 0;
454}
455
456/* Parse the "no-send-proxy" server keyword */
457static int srv_parse_no_send_proxy(char **args, int *cur_arg,
458 struct proxy *curproxy, struct server *newsrv, char **err)
459{
460 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
461}
462
463/* Parse the "no-send-proxy-v2" server keyword */
464static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
465 struct proxy *curproxy, struct server *newsrv, char **err)
466{
467 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
468}
469
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100470/* Parse the "non-stick" server keyword */
471static int srv_parse_non_stick(char **args, int *cur_arg,
472 struct proxy *curproxy, struct server *newsrv, char **err)
473{
474 newsrv->flags |= SRV_F_NON_STICK;
475 return 0;
476}
477
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100478/* Enable server PROXY protocol flags. */
479static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
480{
481 srv->pp_opts |= flags;
482 return 0;
483}
484
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100485/* Parse the "observe" server keyword */
486static int srv_parse_observe(char **args, int *cur_arg,
487 struct proxy *curproxy, struct server *newsrv, char **err)
488{
489 char *arg;
490
491 arg = args[*cur_arg + 1];
492 if (!*arg) {
493 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
494 return ERR_ALERT | ERR_FATAL;
495 }
496
497 if (!strcmp(arg, "none")) {
498 newsrv->observe = HANA_OBS_NONE;
499 }
500 else if (!strcmp(arg, "layer4")) {
501 newsrv->observe = HANA_OBS_LAYER4;
502 }
503 else if (!strcmp(arg, "layer7")) {
504 if (curproxy->mode != PR_MODE_HTTP) {
505 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
506 return ERR_ALERT;
507 }
508 newsrv->observe = HANA_OBS_LAYER7;
509 }
510 else {
511 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
512 "but got '%s'\n", args[*cur_arg], arg);
513 return ERR_ALERT | ERR_FATAL;
514 }
515
516 return 0;
517}
518
Frédéric Lécaille16186232017-03-14 16:42:49 +0100519/* Parse the "redir" server keyword */
520static int srv_parse_redir(char **args, int *cur_arg,
521 struct proxy *curproxy, struct server *newsrv, char **err)
522{
523 char *arg;
524
525 arg = args[*cur_arg + 1];
526 if (!*arg) {
527 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
528 return ERR_ALERT | ERR_FATAL;
529 }
530
531 free(newsrv->rdr_pfx);
532 newsrv->rdr_pfx = strdup(arg);
533 newsrv->rdr_len = strlen(arg);
534
535 return 0;
536}
537
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100538/* Parse the "send-proxy" server keyword */
539static int srv_parse_send_proxy(char **args, int *cur_arg,
540 struct proxy *curproxy, struct server *newsrv, char **err)
541{
542 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
543}
544
545/* Parse the "send-proxy-v2" server keyword */
546static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
547 struct proxy *curproxy, struct server *newsrv, char **err)
548{
549 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
550}
551
Frédéric Lécailledba97072017-03-17 15:33:50 +0100552
553/* Parse the "source" server keyword */
554static int srv_parse_source(char **args, int *cur_arg,
555 struct proxy *curproxy, struct server *newsrv, char **err)
556{
557 char *errmsg;
558 int port_low, port_high;
559 struct sockaddr_storage *sk;
560 struct protocol *proto;
561
562 errmsg = NULL;
563
564 if (!*args[*cur_arg + 1]) {
565 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
566 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
567 goto err;
568 }
569
570 /* 'sk' is statically allocated (no need to be freed). */
571 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
572 if (!sk) {
573 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
574 goto err;
575 }
576
577 proto = protocol_by_family(sk->ss_family);
578 if (!proto || !proto->connect) {
579 Alert("'%s %s' : connect() not supported for this address family.\n",
580 args[*cur_arg], args[*cur_arg + 1]);
581 goto err;
582 }
583
584 newsrv->conn_src.opts |= CO_SRC_BIND;
585 newsrv->conn_src.source_addr = *sk;
586
587 if (port_low != port_high) {
588 int i;
589
590 if (!port_low || !port_high) {
591 Alert("'%s' does not support port offsets (found '%s').\n",
592 args[*cur_arg], args[*cur_arg + 1]);
593 goto err;
594 }
595
596 if (port_low <= 0 || port_low > 65535 ||
597 port_high <= 0 || port_high > 65535 ||
598 port_low > port_high) {
599 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
600 goto err;
601 }
602 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
603 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
604 newsrv->conn_src.sport_range->ports[i] = port_low + i;
605 }
606
607 *cur_arg += 2;
608 while (*(args[*cur_arg])) {
609 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
610#if defined(CONFIG_HAP_TRANSPARENT)
611 if (!*args[*cur_arg + 1]) {
612 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
613 "or 'hdr_ip(name,#)' as argument.\n");
614 goto err;
615 }
616 if (!strcmp(args[*cur_arg + 1], "client")) {
617 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
618 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
619 }
620 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
621 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
622 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
623 }
624 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
625 char *name, *end;
626
627 name = args[*cur_arg + 1] + 7;
628 while (isspace(*name))
629 name++;
630
631 end = name;
632 while (*end && !isspace(*end) && *end != ',' && *end != ')')
633 end++;
634
635 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
636 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
637 free(newsrv->conn_src.bind_hdr_name);
638 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
639 newsrv->conn_src.bind_hdr_len = end - name;
640 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
641 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
642 newsrv->conn_src.bind_hdr_occ = -1;
643
644 /* now look for an occurrence number */
645 while (isspace(*end))
646 end++;
647 if (*end == ',') {
648 end++;
649 name = end;
650 if (*end == '-')
651 end++;
652 while (isdigit((int)*end))
653 end++;
654 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
655 }
656
657 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
658 Alert("usesrc hdr_ip(name,num) does not support negative"
659 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
660 goto err;
661 }
662 }
663 else {
664 struct sockaddr_storage *sk;
665 int port1, port2;
666
667 /* 'sk' is statically allocated (no need to be freed). */
668 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
669 if (!sk) {
670 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
671 goto err;
672 }
673
674 proto = protocol_by_family(sk->ss_family);
675 if (!proto || !proto->connect) {
676 Alert("'%s %s' : connect() not supported for this address family.\n",
677 args[*cur_arg], args[*cur_arg + 1]);
678 goto err;
679 }
680
681 if (port1 != port2) {
682 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
683 args[*cur_arg], args[*cur_arg + 1]);
684 goto err;
685 }
686 newsrv->conn_src.tproxy_addr = *sk;
687 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
688 }
689 global.last_checks |= LSTCHK_NETADM;
690 *cur_arg += 2;
691 continue;
692#else /* no TPROXY support */
693 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
694 goto err;
695#endif /* defined(CONFIG_HAP_TRANSPARENT) */
696 } /* "usesrc" */
697
698 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
699#ifdef SO_BINDTODEVICE
700 if (!*args[*cur_arg + 1]) {
701 Alert("'%s' : missing interface name.\n", args[0]);
702 goto err;
703 }
704 free(newsrv->conn_src.iface_name);
705 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
706 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
707 global.last_checks |= LSTCHK_NETADM;
708#else
709 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
710 goto err;
711#endif
712 *cur_arg += 2;
713 continue;
714 }
715 /* this keyword in not an option of "source" */
716 break;
717 } /* while */
718
719 return 0;
720
721 err:
722 free(errmsg);
723 return ERR_ALERT | ERR_FATAL;
724}
725
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100726/* Parse the "stick" server keyword */
727static int srv_parse_stick(char **args, int *cur_arg,
728 struct proxy *curproxy, struct server *newsrv, char **err)
729{
730 newsrv->flags &= ~SRV_F_NON_STICK;
731 return 0;
732}
733
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100734/* Parse the "track" server keyword */
735static int srv_parse_track(char **args, int *cur_arg,
736 struct proxy *curproxy, struct server *newsrv, char **err)
737{
738 char *arg;
739
740 arg = args[*cur_arg + 1];
741 if (!*arg) {
742 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
743 return ERR_ALERT | ERR_FATAL;
744 }
745
746 free(newsrv->trackit);
747 newsrv->trackit = strdup(arg);
748
749 return 0;
750}
751
Frédéric Lécailledba97072017-03-17 15:33:50 +0100752
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200753/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200754 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200755 * shutdown.
756 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200757void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200758{
Willy Tarreau87b09662015-04-03 00:22:06 +0200759 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200760
Willy Tarreau87b09662015-04-03 00:22:06 +0200761 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
762 if (stream->srv_conn == srv)
763 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200764}
765
766/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200767 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200768 * the reason for the shutdown.
769 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200770void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200771{
772 struct server *srv;
773
774 for (srv = px->srv; srv != NULL; srv = srv->next)
775 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200776 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200777}
778
Willy Tarreaubda92272014-05-20 21:55:30 +0200779/* Appends some information to a message string related to a server going UP or
780 * DOWN. If both <forced> and <reason> are null and the server tracks another
781 * one, a "via" information will be provided to know where the status came from.
782 * If <reason> is non-null, the entire string will be appended after a comma and
783 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200784 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200785 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200786 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200787void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200788{
Willy Tarreaubda92272014-05-20 21:55:30 +0200789 if (reason)
790 chunk_appendf(msg, ", %s", reason);
791 else if (!forced && s->track)
792 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200793
794 if (xferred >= 0) {
795 if (s->state == SRV_ST_STOPPED)
796 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
797 " %d sessions active, %d requeued, %d remaining in queue",
798 s->proxy->srv_act, s->proxy->srv_bck,
799 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
800 s->cur_sess, xferred, s->nbpend);
801 else
802 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
803 " %d sessions requeued, %d total in queue",
804 s->proxy->srv_act, s->proxy->srv_bck,
805 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
806 xferred, s->nbpend);
807 }
808}
809
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200810/* Marks server <s> down, regardless of its checks' statuses, notifies by all
811 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200812 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200813 * recomputes the number of servers, but not the map. Maintenance servers are
814 * ignored. It reports <reason> if non-null as the reason for going down. Note
815 * that it makes use of the trash to build the log strings, so <reason> must
816 * not be placed there.
817 */
818void srv_set_stopped(struct server *s, const char *reason)
819{
820 struct server *srv;
821 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
822 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900823 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200824 int xferred;
825
826 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
827 return;
828
829 s->last_change = now.tv_sec;
830 s->state = SRV_ST_STOPPED;
831 if (s->proxy->lbprm.set_server_status_down)
832 s->proxy->lbprm.set_server_status_down(s);
833
834 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200835 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200836
Willy Tarreau87b09662015-04-03 00:22:06 +0200837 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200838 * a connection. Those which are redispatchable will be queued
839 * to another server or to the proxy itself.
840 */
841 xferred = pendconn_redistribute(s);
842
843 chunk_printf(&trash,
844 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
845 s->proxy->id, s->id);
846
847 srv_append_status(&trash, s, reason, xferred, 0);
848 Warning("%s.\n", trash.str);
849
850 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900851 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
852 send_log(s->proxy, log_level, "%s.\n", trash.str);
853 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200854
855 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
856 set_backend_down(s->proxy);
857
858 s->counters.down_trans++;
859
860 for (srv = s->trackers; srv; srv = srv->tracknext)
861 srv_set_stopped(srv, NULL);
862}
863
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200864/* Marks server <s> up regardless of its checks' statuses and provided it isn't
865 * in maintenance. Notifies by all available means, recounts the remaining
866 * servers on the proxy and tries to grab requests from the proxy. It
867 * automatically recomputes the number of servers, but not the map. Maintenance
868 * servers are ignored. It reports <reason> if non-null as the reason for going
869 * up. Note that it makes use of the trash to build the log strings, so <reason>
870 * must not be placed there.
871 */
872void srv_set_running(struct server *s, const char *reason)
873{
874 struct server *srv;
875 int xferred;
876
877 if (s->admin & SRV_ADMF_MAINT)
878 return;
879
880 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
881 return;
882
883 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
884 if (s->proxy->last_change < now.tv_sec) // ignore negative times
885 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
886 s->proxy->last_change = now.tv_sec;
887 }
888
889 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
890 s->down_time += now.tv_sec - s->last_change;
891
892 s->last_change = now.tv_sec;
893
894 s->state = SRV_ST_STARTING;
895 if (s->slowstart > 0)
896 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
897 else
898 s->state = SRV_ST_RUNNING;
899
900 server_recalc_eweight(s);
901
902 /* If the server is set with "on-marked-up shutdown-backup-sessions",
903 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200904 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200905 * on all backup servers.
906 */
907 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
908 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200909 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200910
911 /* check if we can handle some connections queued at the proxy. We
912 * will take as many as we can handle.
913 */
914 xferred = pendconn_grab_from_px(s);
915
916 chunk_printf(&trash,
917 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
918 s->proxy->id, s->id);
919
920 srv_append_status(&trash, s, reason, xferred, 0);
921 Warning("%s.\n", trash.str);
922 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900923 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200924
925 for (srv = s->trackers; srv; srv = srv->tracknext)
926 srv_set_running(srv, NULL);
927}
928
Willy Tarreau8eb77842014-05-21 13:54:57 +0200929/* Marks server <s> stopping regardless of its checks' statuses and provided it
930 * isn't in maintenance. Notifies by all available means, recounts the remaining
931 * servers on the proxy and tries to grab requests from the proxy. It
932 * automatically recomputes the number of servers, but not the map. Maintenance
933 * servers are ignored. It reports <reason> if non-null as the reason for going
934 * up. Note that it makes use of the trash to build the log strings, so <reason>
935 * must not be placed there.
936 */
937void srv_set_stopping(struct server *s, const char *reason)
938{
939 struct server *srv;
940 int xferred;
941
942 if (s->admin & SRV_ADMF_MAINT)
943 return;
944
945 if (s->state == SRV_ST_STOPPING)
946 return;
947
948 s->last_change = now.tv_sec;
949 s->state = SRV_ST_STOPPING;
950 if (s->proxy->lbprm.set_server_status_down)
951 s->proxy->lbprm.set_server_status_down(s);
952
Willy Tarreau87b09662015-04-03 00:22:06 +0200953 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200954 * a connection. Those which are redispatchable will be queued
955 * to another server or to the proxy itself.
956 */
957 xferred = pendconn_redistribute(s);
958
959 chunk_printf(&trash,
960 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
961 s->proxy->id, s->id);
962
963 srv_append_status(&trash, s, reason, xferred, 0);
964
965 Warning("%s.\n", trash.str);
966 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
967
968 if (!s->proxy->srv_bck && !s->proxy->srv_act)
969 set_backend_down(s->proxy);
970
971 for (srv = s->trackers; srv; srv = srv->tracknext)
972 srv_set_stopping(srv, NULL);
973}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200974
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200975/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
976 * enforce either maint mode or drain mode. It is not allowed to set more than
977 * one flag at once. The equivalent "inherited" flag is propagated to all
978 * tracking servers. Maintenance mode disables health checks (but not agent
979 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100980 * is done. If <cause> is non-null, it will be displayed at the end of the log
981 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200982 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100983void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200984{
985 struct check *check = &s->check;
986 struct server *srv;
987 int xferred;
988
989 if (!mode)
990 return;
991
992 /* stop going down as soon as we meet a server already in the same state */
993 if (s->admin & mode)
994 return;
995
996 s->admin |= mode;
997
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200998 /* stop going down if the equivalent flag was already present (forced or inherited) */
999 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
1000 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
1001 return;
1002
1003 /* Maintenance must also disable health checks */
1004 if (mode & SRV_ADMF_MAINT) {
1005 if (s->check.state & CHK_ST_ENABLED) {
1006 s->check.state |= CHK_ST_PAUSED;
1007 check->health = 0;
1008 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001009
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001010 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001011 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001012 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
1013 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1014 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001015
Willy Tarreaubda92272014-05-20 21:55:30 +02001016 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001018 if (!(global.mode & MODE_STARTING)) {
1019 Warning("%s.\n", trash.str);
1020 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1021 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001022 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001023 else { /* server was still running */
1024 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
1025 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1026
1027 check->health = 0; /* failure */
1028 s->last_change = now.tv_sec;
1029 s->state = SRV_ST_STOPPED;
1030 if (s->proxy->lbprm.set_server_status_down)
1031 s->proxy->lbprm.set_server_status_down(s);
1032
1033 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001034 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001035
Willy Tarreau87b09662015-04-03 00:22:06 +02001036 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001037 * a connection. Those which are redispatchable will be queued
1038 * to another server or to the proxy itself.
1039 */
1040 xferred = pendconn_redistribute(s);
1041
1042 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001043 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001044 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001045 s->proxy->id, s->id,
1046 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001047
1048 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1049
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001050 if (!(global.mode & MODE_STARTING)) {
1051 Warning("%s.\n", trash.str);
1052 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1053 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001054
1055 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1056 set_backend_down(s->proxy);
1057
1058 s->counters.down_trans++;
1059 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001060 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001061
1062 /* drain state is applied only if not yet in maint */
1063 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001064 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1065
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001066 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001067 if (s->proxy->lbprm.set_server_status_down)
1068 s->proxy->lbprm.set_server_status_down(s);
1069
Willy Tarreau87b09662015-04-03 00:22:06 +02001070 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001071 * a connection. Those which are redispatchable will be queued
1072 * to another server or to the proxy itself.
1073 */
1074 xferred = pendconn_redistribute(s);
1075
Willy Tarreau8b428482016-11-07 15:53:43 +01001076 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1077 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1078 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001079
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001080 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001081
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001082 if (!(global.mode & MODE_STARTING)) {
1083 Warning("%s.\n", trash.str);
1084 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1085 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1086 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001087 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1088 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001089 }
1090
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001091 /* compute the inherited flag to propagate */
1092 if (mode & SRV_ADMF_MAINT)
1093 mode = SRV_ADMF_IMAINT;
1094 else if (mode & SRV_ADMF_DRAIN)
1095 mode = SRV_ADMF_IDRAIN;
1096
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001097 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001098 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001099}
1100
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001101/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1102 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1103 * than one flag at once. The equivalent "inherited" flag is propagated to all
1104 * tracking servers. Leaving maintenance mode re-enables health checks. When
1105 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001106 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001107void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001108{
1109 struct check *check = &s->check;
1110 struct server *srv;
1111 int xferred = -1;
1112
1113 if (!mode)
1114 return;
1115
1116 /* stop going down as soon as we see the flag is not there anymore */
1117 if (!(s->admin & mode))
1118 return;
1119
1120 s->admin &= ~mode;
1121
1122 if (s->admin & SRV_ADMF_MAINT) {
1123 /* remaining in maintenance mode, let's inform precisely about the
1124 * situation.
1125 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001126 if (mode & SRV_ADMF_FMAINT) {
1127 chunk_printf(&trash,
1128 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1129 s->flags & SRV_F_BACKUP ? "Backup " : "",
1130 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001131
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001132 if (s->track) /* normally it's mandatory here */
1133 chunk_appendf(&trash, " via %s/%s",
1134 s->track->proxy->id, s->track->id);
1135 Warning("%s.\n", trash.str);
1136 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1137 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001138 if (mode & SRV_ADMF_RMAINT) {
1139 chunk_printf(&trash,
1140 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1141 s->flags & SRV_F_BACKUP ? "Backup " : "",
1142 s->proxy->id, s->id, s->hostname);
1143
1144 if (s->track) /* normally it's mandatory here */
1145 chunk_appendf(&trash, " via %s/%s",
1146 s->track->proxy->id, s->track->id);
1147 Warning("%s.\n", trash.str);
1148 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1149 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001150 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001151 chunk_printf(&trash,
1152 "%sServer %s/%s remains in forced maintenance",
1153 s->flags & SRV_F_BACKUP ? "Backup " : "",
1154 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001155 Warning("%s.\n", trash.str);
1156 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1157 }
1158 /* don't report anything when leaving drain mode and remaining in maintenance */
1159 }
1160 else if (mode & SRV_ADMF_MAINT) {
1161 /* OK here we're leaving maintenance, we have many things to check,
1162 * because the server might possibly be coming back up depending on
1163 * its state. In practice, leaving maintenance means that we should
1164 * immediately turn to UP (more or less the slowstart) under the
1165 * following conditions :
1166 * - server is neither checked nor tracked
1167 * - server tracks another server which is not checked
1168 * - server tracks another server which is already up
1169 * Which sums up as something simpler :
1170 * "either the tracking server is up or the server's checks are disabled
1171 * or up". Otherwise we only re-enable health checks. There's a special
1172 * case associated to the stopping state which can be inherited. Note
1173 * that the server might still be in drain mode, which is naturally dealt
1174 * with by the lower level functions.
1175 */
1176
1177 if (s->check.state & CHK_ST_ENABLED) {
1178 s->check.state &= ~CHK_ST_PAUSED;
1179 check->health = check->rise; /* start OK but check immediately */
1180 }
1181
1182 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
1183 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1184 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
1185 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1186 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1187 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1188 s->proxy->last_change = now.tv_sec;
1189 }
1190
1191 if (s->last_change < now.tv_sec) // ignore negative times
1192 s->down_time += now.tv_sec - s->last_change;
1193 s->last_change = now.tv_sec;
1194
1195 if (s->track && s->track->state == SRV_ST_STOPPING)
1196 s->state = SRV_ST_STOPPING;
1197 else {
1198 s->state = SRV_ST_STARTING;
1199 if (s->slowstart > 0)
1200 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1201 else
1202 s->state = SRV_ST_RUNNING;
1203 }
1204
1205 server_recalc_eweight(s);
1206
1207 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1208 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +02001209 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001210 * on all backup servers.
1211 */
1212 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1213 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001214 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001215
1216 /* check if we can handle some connections queued at the proxy. We
1217 * will take as many as we can handle.
1218 */
1219 xferred = pendconn_grab_from_px(s);
1220 }
1221
1222 if (mode & SRV_ADMF_FMAINT) {
1223 chunk_printf(&trash,
1224 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1225 s->flags & SRV_F_BACKUP ? "Backup " : "",
1226 s->proxy->id, s->id,
1227 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1228 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001229 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001230 else if (mode & SRV_ADMF_RMAINT) {
1231 chunk_printf(&trash,
1232 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1233 s->flags & SRV_F_BACKUP ? "Backup " : "",
1234 s->proxy->id, s->id, s->hostname,
1235 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1236 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1237 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001238 else {
1239 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001240 "%sServer %s/%s is %s/%s (leaving maintenance)",
1241 s->flags & SRV_F_BACKUP ? "Backup " : "",
1242 s->proxy->id, s->id,
1243 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1244 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1245 srv_append_status(&trash, s, NULL, xferred, 0);
1246 }
1247 Warning("%s.\n", trash.str);
1248 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1249 }
1250 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
1251 /* remaining in drain mode after removing one of its flags */
1252
1253 if (mode & SRV_ADMF_FDRAIN) {
1254 chunk_printf(&trash,
1255 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001256 s->flags & SRV_F_BACKUP ? "Backup " : "",
1257 s->proxy->id, s->id);
1258
1259 if (s->track) /* normally it's mandatory here */
1260 chunk_appendf(&trash, " via %s/%s",
1261 s->track->proxy->id, s->track->id);
1262 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001263 else {
1264 chunk_printf(&trash,
1265 "%sServer %s/%s remains in forced drain mode",
1266 s->flags & SRV_F_BACKUP ? "Backup " : "",
1267 s->proxy->id, s->id);
1268 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001269 Warning("%s.\n", trash.str);
1270 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001271 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001272 else if (mode & SRV_ADMF_DRAIN) {
1273 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001274 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1275 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1276 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1277 s->proxy->last_change = now.tv_sec;
1278 }
1279
1280 if (s->last_change < now.tv_sec) // ignore negative times
1281 s->down_time += now.tv_sec - s->last_change;
1282 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001283 server_recalc_eweight(s);
1284
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001285 if (mode & SRV_ADMF_FDRAIN) {
1286 chunk_printf(&trash,
1287 "%sServer %s/%s is %s (leaving forced drain)",
1288 s->flags & SRV_F_BACKUP ? "Backup " : "",
1289 s->proxy->id, s->id,
1290 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1291 }
1292 else {
1293 chunk_printf(&trash,
1294 "%sServer %s/%s is %s (leaving drain)",
1295 s->flags & SRV_F_BACKUP ? "Backup " : "",
1296 s->proxy->id, s->id,
1297 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1298 if (s->track) /* normally it's mandatory here */
1299 chunk_appendf(&trash, " via %s/%s",
1300 s->track->proxy->id, s->track->id);
1301 }
1302 Warning("%s.\n", trash.str);
1303 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001304 }
1305
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001306 /* stop going down if the equivalent flag is still present (forced or inherited) */
1307 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
1308 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
1309 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001310
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001311 if (mode & SRV_ADMF_MAINT)
1312 mode = SRV_ADMF_IMAINT;
1313 else if (mode & SRV_ADMF_DRAIN)
1314 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001315
1316 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001317 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001318}
1319
Willy Tarreau757478e2016-11-03 19:22:19 +01001320/* principle: propagate maint and drain to tracking servers. This is useful
1321 * upon startup so that inherited states are correct.
1322 */
1323static void srv_propagate_admin_state(struct server *srv)
1324{
1325 struct server *srv2;
1326
1327 if (!srv->trackers)
1328 return;
1329
1330 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
1331 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001332 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001333
1334 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001335 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001336 }
1337}
1338
1339/* Compute and propagate the admin states for all servers in proxy <px>.
1340 * Only servers *not* tracking another one are considered, because other
1341 * ones will be handled when the server they track is visited.
1342 */
1343void srv_compute_all_admin_states(struct proxy *px)
1344{
1345 struct server *srv;
1346
1347 for (srv = px->srv; srv; srv = srv->next) {
1348 if (srv->track)
1349 continue;
1350 srv_propagate_admin_state(srv);
1351 }
1352}
1353
Willy Tarreaudff55432012-10-10 17:51:05 +02001354/* Note: must not be declared <const> as its list will be overwritten.
1355 * Please take care of keeping this list alphabetically sorted, doing so helps
1356 * all code contributors.
1357 * Optional keywords are also declared with a NULL ->parse() function so that
1358 * the config parser can report an appropriate error when a known keyword was
1359 * not enabled.
1360 */
1361static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001362 { "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 +01001363 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001364 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001365 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001366 { "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 +01001367 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001368 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1369 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001370 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001371 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001372 { "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 +01001373 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001374 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001375 { "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 +01001376 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1377 { "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 +01001378 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001379 { "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 +01001380 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001381 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1382 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001383 /*
1384 * Note: the following 'skip' field value is 0.
1385 * Here this does not mean that "source" setting does not need any argument.
1386 * This means that the number of argument is variable.
1387 */
1388 { "source", srv_parse_source, 0, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001389 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001390 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001391 { NULL, NULL, 0 },
1392}};
1393
1394__attribute__((constructor))
1395static void __listener_init(void)
1396{
1397 srv_register_keywords(&srv_kws);
1398}
1399
Willy Tarreau004e0452013-11-21 11:22:01 +01001400/* Recomputes the server's eweight based on its state, uweight, the current time,
1401 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1402 * state is automatically disabled if the time is elapsed.
1403 */
1404void server_recalc_eweight(struct server *sv)
1405{
1406 struct proxy *px = sv->proxy;
1407 unsigned w;
1408
1409 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1410 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +02001411 if (sv->state == SRV_ST_STARTING)
1412 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001413 }
1414
1415 /* We must take care of not pushing the server to full throttle during slow starts.
1416 * It must also start immediately, at least at the minimal step when leaving maintenance.
1417 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001418 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001419 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1420 else
1421 w = px->lbprm.wdiv;
1422
1423 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
1424
1425 /* now propagate the status change to any LB algorithms */
1426 if (px->lbprm.update_server_eweight)
1427 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +02001428 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001429 if (px->lbprm.set_server_status_up)
1430 px->lbprm.set_server_status_up(sv);
1431 }
1432 else {
1433 if (px->lbprm.set_server_status_down)
1434 px->lbprm.set_server_status_down(sv);
1435 }
1436}
1437
Willy Tarreaubaaee002006-06-26 02:48:02 +02001438/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001439 * Parses weight_str and configures sv accordingly.
1440 * Returns NULL on success, error message string otherwise.
1441 */
1442const char *server_parse_weight_change_request(struct server *sv,
1443 const char *weight_str)
1444{
1445 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001446 long int w;
1447 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001448
1449 px = sv->proxy;
1450
1451 /* if the weight is terminated with '%', it is set relative to
1452 * the initial weight, otherwise it is absolute.
1453 */
1454 if (!*weight_str)
1455 return "Require <weight> or <weight%>.\n";
1456
Simon Hormanb796afa2013-02-12 10:45:53 +09001457 w = strtol(weight_str, &end, 10);
1458 if (end == weight_str)
1459 return "Empty weight string empty or preceded by garbage";
1460 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001461 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001462 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001463 /* Avoid integer overflow */
1464 if (w > 25600)
1465 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001466 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001467 if (w > 256)
1468 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001469 }
1470 else if (w < 0 || w > 256)
1471 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001472 else if (end[0] != '\0')
1473 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001474
1475 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1476 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1477
1478 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001479 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001480
1481 return NULL;
1482}
1483
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001484/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001485 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1486 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001487 * Returns:
1488 * - error string on error
1489 * - NULL on success
1490 */
1491const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001492 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001493{
1494 unsigned char ip[INET6_ADDRSTRLEN];
1495
1496 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001497 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001498 return NULL;
1499 }
1500 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001501 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001502 return NULL;
1503 }
1504
1505 return "Could not understand IP address format.\n";
1506}
1507
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001508const char *server_parse_maxconn_change_request(struct server *sv,
1509 const char *maxconn_str)
1510{
1511 long int v;
1512 char *end;
1513
1514 if (!*maxconn_str)
1515 return "Require <maxconn>.\n";
1516
1517 v = strtol(maxconn_str, &end, 10);
1518 if (end == maxconn_str)
1519 return "maxconn string empty or preceded by garbage";
1520 else if (end[0] != '\0')
1521 return "Trailing garbage in maxconn string";
1522
1523 if (sv->maxconn == sv->minconn) { // static maxconn
1524 sv->maxconn = sv->minconn = v;
1525 } else { // dynamic maxconn
1526 sv->maxconn = v;
1527 }
1528
1529 if (may_dequeue_tasks(sv, sv->proxy))
1530 process_srv_queue(sv);
1531
1532 return NULL;
1533}
1534
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001535#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1536static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1537{
1538 int idx;
1539 struct sample_expr *expr;
1540 const char *args[] = {
1541 newsrv->sni_expr,
1542 NULL,
1543 };
1544
1545 idx = 0;
1546 proxy->conf.args.ctx = ARGC_SRV;
1547
1548 expr = sample_parse_expr((char **)args, &idx, px->conf.file, px->conf.line,
1549 err, &proxy->conf.args);
1550 if (!expr) {
1551 memprintf(err, "error detected while parsing sni expression : %s", *err);
1552 return ERR_ALERT | ERR_FATAL;
1553 }
1554
1555 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1556 memprintf(err, "error detected while parsing sni expression : "
1557 " fetch method '%s' extracts information from '%s', "
1558 "none of which is available here.\n",
1559 args[0], sample_src_names(expr->fetch->use));
1560 return ERR_ALERT | ERR_FATAL;
1561 }
1562
1563 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1564 release_sample_expr(newsrv->ssl_ctx.sni);
1565 newsrv->ssl_ctx.sni = expr;
1566
1567 return 0;
1568}
1569#endif
1570
1571static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1572{
1573 if (err && *err) {
1574 indent_msg(err, 2);
1575 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1576 }
1577 else
1578 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1579 file, linenum, args[0], args[1], args[cur_arg]);
1580}
1581
Willy Tarreau272adea2014-03-31 10:39:59 +02001582int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1583{
1584 struct server *newsrv = NULL;
1585 const char *err;
1586 char *errmsg = NULL;
1587 int err_code = 0;
1588 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001589 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001590
1591 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
1592 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001593 int defsrv = (*args[0] == 'd');
Willy Tarreau272adea2014-03-31 10:39:59 +02001594
1595 if (!defsrv && curproxy == defproxy) {
1596 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1597 err_code |= ERR_ALERT | ERR_FATAL;
1598 goto out;
1599 }
1600 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1601 err_code |= ERR_ALERT | ERR_FATAL;
1602
Frédéric Lécaille8065b6d2017-03-09 14:01:02 +01001603 if (!defsrv && !*args[2]) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001604 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1605 file, linenum, args[0]);
1606 err_code |= ERR_ALERT | ERR_FATAL;
1607 goto out;
1608 }
1609
1610 err = invalid_char(args[1]);
1611 if (err && !defsrv) {
1612 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1613 file, linenum, *err, args[1]);
1614 err_code |= ERR_ALERT | ERR_FATAL;
1615 goto out;
1616 }
1617
1618 if (!defsrv) {
1619 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001620 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001621 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001622 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02001623
Vincent Bernat02779b62016-04-03 13:48:43 +02001624 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001625 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1626 err_code |= ERR_ALERT | ERR_ABORT;
1627 goto out;
1628 }
1629
1630 /* the servers are linked backwards first */
1631 newsrv->next = curproxy->srv;
1632 curproxy->srv = newsrv;
1633 newsrv->proxy = curproxy;
1634 newsrv->conf.file = strdup(file);
1635 newsrv->conf.line = linenum;
1636
1637 newsrv->obj_type = OBJ_TYPE_SERVER;
1638 LIST_INIT(&newsrv->actconns);
1639 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02001640 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02001641 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02001642 LIST_INIT(&newsrv->safe_conns);
Willy Tarreauc93cd162014-05-13 15:54:22 +02001643 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +02001644 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +02001645 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +02001646 newsrv->last_change = now.tv_sec;
1647 newsrv->id = strdup(args[1]);
1648
1649 /* several ways to check the port component :
1650 * - IP => port=+0, relative (IPv4 only)
1651 * - IP: => port=+0, relative
1652 * - IP:N => port=N, absolute
1653 * - IP:+N => port=+N, relative
1654 * - IP:-N => port=-N, relative
1655 */
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001656 sk = str2sa_range(args[2], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001657 if (!sk) {
1658 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1659 err_code |= ERR_ALERT | ERR_FATAL;
1660 goto out;
1661 }
1662
1663 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001664 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001665 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1666 file, linenum, args[0], args[1]);
1667 err_code |= ERR_ALERT | ERR_FATAL;
1668 goto out;
1669 }
1670
1671 if (!port1 || !port2) {
1672 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001673 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001674 }
1675 else if (port1 != port2) {
1676 /* port range */
1677 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1678 file, linenum, args[0], args[1], args[2]);
1679 err_code |= ERR_ALERT | ERR_FATAL;
1680 goto out;
1681 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001682
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001683 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001684 newsrv->hostname = fqdn;
1685 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001686 goto skip_name_resolution;
1687
Willy Tarreau07101d52015-09-08 16:16:35 +02001688 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001689 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001690 goto skip_name_resolution;
1691
1692 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1693 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1694 goto skip_name_resolution;
1695 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1696 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1697 file, linenum, args[2]);
1698 err_code |= ERR_ALERT | ERR_FATAL;
1699 goto out;
1700 }
1701
1702 curr_resolution->requester = newsrv;
1703 curr_resolution->requester_cb = snr_resolution_cb;
1704 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1705 curr_resolution->status = RSLV_STATUS_NONE;
1706 curr_resolution->step = RSLV_STEP_NONE;
1707 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001708 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001709 newsrv->resolution = curr_resolution;
1710
1711 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001712 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001713 newsrv->svc_port = port;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001714 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = xprt_get(XPRT_RAW);
Willy Tarreau272adea2014-03-31 10:39:59 +02001715
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001716 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001717 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1718 file, linenum, newsrv->addr.ss_family, args[2]);
1719 err_code |= ERR_ALERT | ERR_FATAL;
1720 goto out;
1721 }
1722
Frédéric Lécailledba97072017-03-17 15:33:50 +01001723 /*
1724 * In this section we copy default-server connection source settings to
1725 * the new server object connection source
1726 * (newsrv->conn_src <- curproxy->defsrv.conn_src).
1727 */
1728 newsrv->conn_src.opts = curproxy->defsrv.conn_src.opts;
1729 newsrv->conn_src.source_addr = curproxy->defsrv.conn_src.source_addr;
1730 if (curproxy->defsrv.conn_src.sport_range != NULL) {
1731 int i, def_sport_range_sz;
1732 struct server *default_srv;
1733
1734 default_srv = &curproxy->defsrv;
1735 def_sport_range_sz = default_srv->conn_src.sport_range->size;
1736 if (def_sport_range_sz > 0) {
1737 newsrv->conn_src.sport_range = port_range_alloc_range(def_sport_range_sz);
1738 if (newsrv->conn_src.sport_range) {
1739 for (i = 0; i < def_sport_range_sz; i++)
1740 newsrv->conn_src.sport_range->ports[i] = default_srv->conn_src.sport_range->ports[i];
1741 }
1742 }
1743 }
David Carlier3a471932017-04-07 20:48:00 +01001744#ifdef CONFIG_HAP_TRANSPARENT
Frédéric Lécailledba97072017-03-17 15:33:50 +01001745 if (curproxy->defsrv.conn_src.bind_hdr_name != NULL) {
1746 newsrv->conn_src.bind_hdr_name = strdup(curproxy->defsrv.conn_src.bind_hdr_name);
1747 newsrv->conn_src.bind_hdr_len = strlen(curproxy->defsrv.conn_src.bind_hdr_name);
1748 }
1749 newsrv->conn_src.bind_hdr_occ = curproxy->defsrv.conn_src.bind_hdr_occ;
1750 newsrv->conn_src.tproxy_addr = curproxy->defsrv.conn_src.tproxy_addr;
David Carlier3a471932017-04-07 20:48:00 +01001751#endif
Frédéric Lécailledba97072017-03-17 15:33:50 +01001752 if (curproxy->defsrv.conn_src.iface_name != NULL)
1753 newsrv->conn_src.iface_name = strdup(curproxy->defsrv.conn_src.iface_name);
1754
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001755 newsrv->pp_opts = curproxy->defsrv.pp_opts;
Frédéric Lécaille16186232017-03-14 16:42:49 +01001756 if (curproxy->defsrv.rdr_pfx != NULL) {
1757 newsrv->rdr_pfx = strdup(curproxy->defsrv.rdr_pfx);
1758 newsrv->rdr_len = curproxy->defsrv.rdr_len;
1759 }
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001760 if (curproxy->defsrv.cookie != NULL) {
1761 newsrv->cookie = strdup(curproxy->defsrv.cookie);
1762 newsrv->cklen = curproxy->defsrv.cklen;
1763 }
Willy Tarreau141ad852016-12-22 18:38:00 +01001764 newsrv->use_ssl = curproxy->defsrv.use_ssl;
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001765 newsrv->check.addr = newsrv->agent.addr = curproxy->defsrv.check.addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001766 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1767 newsrv->check.port = curproxy->defsrv.check.port;
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +01001768 /* Note: 'flags' field has potentially been already initialized. */
1769 newsrv->flags |= curproxy->defsrv.flags;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001770 newsrv->do_check = curproxy->defsrv.do_check;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001771 newsrv->do_agent = curproxy->defsrv.do_agent;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001772 if (newsrv->check.port)
1773 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001774 newsrv->check.inter = curproxy->defsrv.check.inter;
1775 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1776 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1777 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1778 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001779 if (curproxy->defsrv.agent.send_string != NULL)
1780 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1781 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001782 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1783 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1784 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1785 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1786 newsrv->minconn = curproxy->defsrv.minconn;
1787 newsrv->maxconn = curproxy->defsrv.maxconn;
1788 newsrv->slowstart = curproxy->defsrv.slowstart;
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001789 newsrv->observe = curproxy->defsrv.observe;
Willy Tarreau272adea2014-03-31 10:39:59 +02001790 newsrv->onerror = curproxy->defsrv.onerror;
1791 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1792 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001793 if (curproxy->defsrv.trackit != NULL)
1794 newsrv->trackit = strdup(curproxy->defsrv.trackit);
Willy Tarreau272adea2014-03-31 10:39:59 +02001795 newsrv->consecutive_errors_limit
1796 = curproxy->defsrv.consecutive_errors_limit;
Willy Tarreau272adea2014-03-31 10:39:59 +02001797 newsrv->uweight = newsrv->iweight
1798 = curproxy->defsrv.iweight;
1799
1800 newsrv->check.status = HCHK_STATUS_INI;
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001801 newsrv->check.send_proxy = curproxy->defsrv.check.send_proxy;
Willy Tarreau272adea2014-03-31 10:39:59 +02001802 newsrv->check.rise = curproxy->defsrv.check.rise;
1803 newsrv->check.fall = curproxy->defsrv.check.fall;
1804 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001805 /* Here we check if 'disabled' is the default server state */
1806 if (curproxy->defsrv.admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1807 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1808 newsrv->state = SRV_ST_STOPPED;
1809 newsrv->check.state |= CHK_ST_PAUSED;
1810 newsrv->check.health = 0;
1811 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001812 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001813 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001814
1815 newsrv->agent.status = HCHK_STATUS_INI;
1816 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1817 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1818 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1819 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001820 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1821 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1822 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001823 memcpy(newsrv->dns_opts.pref_net,
1824 curproxy->defsrv.dns_opts.pref_net,
1825 sizeof(newsrv->dns_opts.pref_net));
1826 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001827 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1828 newsrv->init_addr = curproxy->defsrv.init_addr;
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001829#if defined(USE_OPENSSL)
1830 /* SSL config. */
Frédéric Lécaille5e576432017-03-14 15:52:04 +01001831 if (curproxy->defsrv.ssl_ctx.ca_file != NULL)
1832 newsrv->ssl_ctx.ca_file = strdup(curproxy->defsrv.ssl_ctx.ca_file);
1833 if (curproxy->defsrv.ssl_ctx.crl_file != NULL)
1834 newsrv->ssl_ctx.crl_file = strdup(curproxy->defsrv.ssl_ctx.crl_file);
1835 if (curproxy->defsrv.ssl_ctx.client_crt != NULL)
Frédéric Lécailleacd48272017-03-29 14:58:09 +02001836 newsrv->ssl_ctx.client_crt = strdup(curproxy->defsrv.ssl_ctx.client_crt);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001837 newsrv->ssl_ctx.verify = curproxy->defsrv.ssl_ctx.verify;
Frédéric Lécaille273f3212017-03-13 15:52:01 +01001838 if (curproxy->defsrv.ssl_ctx.verify_host != NULL)
1839 newsrv->ssl_ctx.verify_host = strdup(curproxy->defsrv.ssl_ctx.verify_host);
Frédéric Lécaillebcaf1d72017-03-15 16:20:02 +01001840 if (curproxy->defsrv.ssl_ctx.ciphers != NULL)
1841 newsrv->ssl_ctx.ciphers = strdup(curproxy->defsrv.ssl_ctx.ciphers);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001842 if (curproxy->defsrv.sni_expr != NULL)
1843 newsrv->sni_expr = strdup(curproxy->defsrv.sni_expr);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001844#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02001845
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001846#ifdef TCP_USER_TIMEOUT
1847 newsrv->tcp_ut = curproxy->defsrv.tcp_ut;
1848#endif
1849
Willy Tarreau272adea2014-03-31 10:39:59 +02001850 cur_arg = 3;
1851 } else {
1852 newsrv = &curproxy->defsrv;
1853 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001854 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001855 }
1856
1857 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001858 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001859 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1860 if (err) {
1861 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1862 file, linenum, *err, newsrv->id);
1863 err_code |= ERR_ALERT | ERR_FATAL;
1864 goto out;
1865 }
1866 if (val <= 0) {
1867 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1868 file, linenum, val, args[cur_arg], newsrv->id);
1869 err_code |= ERR_ALERT | ERR_FATAL;
1870 goto out;
1871 }
1872 newsrv->agent.inter = val;
1873 cur_arg += 2;
1874 }
Misiekea849332017-01-09 09:39:51 +01001875 else if (!strcmp(args[cur_arg], "agent-addr")) {
1876 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1877 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1878 goto out;
1879 }
1880
1881 cur_arg += 2;
1882 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001883 else if (!strcmp(args[cur_arg], "agent-port")) {
1884 global.maxsock++;
1885 newsrv->agent.port = atol(args[cur_arg + 1]);
1886 cur_arg += 2;
1887 }
James Brown55f9ff12015-10-21 18:19:05 -07001888 else if (!strcmp(args[cur_arg], "agent-send")) {
1889 global.maxsock++;
1890 free(newsrv->agent.send_string);
1891 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1892 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1893 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1894 cur_arg += 2;
1895 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001896 else if (!strcmp(args[cur_arg], "init-addr")) {
1897 char *p, *end;
1898 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001899 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001900
1901 newsrv->init_addr_methods = 0;
1902 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1903
1904 for (p = args[cur_arg + 1]; *p; p = end) {
1905 /* cut on next comma */
1906 for (end = p; *end && *end != ','; end++);
1907 if (*end)
1908 *(end++) = 0;
1909
Willy Tarreau4310d362016-11-02 15:05:56 +01001910 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001911 if (!strcmp(p, "libc")) {
1912 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1913 }
1914 else if (!strcmp(p, "last")) {
1915 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1916 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001917 else if (!strcmp(p, "none")) {
1918 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1919 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001920 else if (str2ip2(p, &sa, 0)) {
1921 if (is_addr(&newsrv->init_addr)) {
1922 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1923 file, linenum, args[cur_arg], p);
1924 err_code |= ERR_ALERT | ERR_FATAL;
1925 goto out;
1926 }
1927 newsrv->init_addr = sa;
1928 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1929 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001930 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001931 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001932 file, linenum, args[cur_arg], p);
1933 err_code |= ERR_ALERT | ERR_FATAL;
1934 goto out;
1935 }
1936 if (!done) {
1937 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1938 file, linenum, args[cur_arg], p);
1939 err_code |= ERR_ALERT | ERR_FATAL;
1940 goto out;
1941 }
1942 }
1943 cur_arg += 2;
1944 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001945 else if (!strcmp(args[cur_arg], "resolvers")) {
1946 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1947 cur_arg += 2;
1948 }
1949 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1950 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001951 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001952 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001953 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001954 else {
1955 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1956 file, linenum, args[cur_arg]);
1957 err_code |= ERR_ALERT | ERR_FATAL;
1958 goto out;
1959 }
1960 cur_arg += 2;
1961 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001962 else if (!strcmp(args[cur_arg], "resolve-net")) {
1963 char *p, *e;
1964 unsigned char mask;
1965 struct dns_options *opt;
1966
1967 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1968 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1969 file, linenum, args[cur_arg]);
1970 err_code |= ERR_ALERT | ERR_FATAL;
1971 goto out;
1972 }
1973
1974 opt = &newsrv->dns_opts;
1975
1976 /* Split arguments by comma, and convert it from ipv4 or ipv6
1977 * string network in in_addr or in6_addr.
1978 */
1979 p = args[cur_arg + 1];
1980 e = p;
1981 while (*p != '\0') {
1982 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001983 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001984 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1985 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1986 err_code |= ERR_ALERT | ERR_FATAL;
1987 goto out;
1988 }
1989 /* look for end or comma. */
1990 while (*e != ',' && *e != '\0')
1991 e++;
1992 if (*e == ',') {
1993 *e = '\0';
1994 e++;
1995 }
1996 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1997 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1998 /* Try to convert input string from ipv4 or ipv6 network. */
1999 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2000 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2001 &mask)) {
2002 /* Try to convert input string from ipv6 network. */
2003 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2004 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2005 } else {
2006 /* All network conversions fail, retrun error. */
2007 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2008 file, linenum, args[cur_arg], p);
2009 err_code |= ERR_ALERT | ERR_FATAL;
2010 goto out;
2011 }
2012 opt->pref_net_nb++;
2013 p = e;
2014 }
2015
2016 cur_arg += 2;
2017 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002018 else if (!strcmp(args[cur_arg], "rise")) {
2019 if (!*args[cur_arg + 1]) {
2020 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2021 file, linenum, args[cur_arg]);
2022 err_code |= ERR_ALERT | ERR_FATAL;
2023 goto out;
2024 }
2025
2026 newsrv->check.rise = atol(args[cur_arg + 1]);
2027 if (newsrv->check.rise <= 0) {
2028 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2029 file, linenum, args[cur_arg]);
2030 err_code |= ERR_ALERT | ERR_FATAL;
2031 goto out;
2032 }
2033
2034 if (newsrv->check.health)
2035 newsrv->check.health = newsrv->check.rise;
2036 cur_arg += 2;
2037 }
2038 else if (!strcmp(args[cur_arg], "fall")) {
2039 newsrv->check.fall = atol(args[cur_arg + 1]);
2040
2041 if (!*args[cur_arg + 1]) {
2042 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2043 file, linenum, args[cur_arg]);
2044 err_code |= ERR_ALERT | ERR_FATAL;
2045 goto out;
2046 }
2047
2048 if (newsrv->check.fall <= 0) {
2049 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2050 file, linenum, args[cur_arg]);
2051 err_code |= ERR_ALERT | ERR_FATAL;
2052 goto out;
2053 }
2054
2055 cur_arg += 2;
2056 }
2057 else if (!strcmp(args[cur_arg], "inter")) {
2058 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2059 if (err) {
2060 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2061 file, linenum, *err, newsrv->id);
2062 err_code |= ERR_ALERT | ERR_FATAL;
2063 goto out;
2064 }
2065 if (val <= 0) {
2066 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2067 file, linenum, val, args[cur_arg], newsrv->id);
2068 err_code |= ERR_ALERT | ERR_FATAL;
2069 goto out;
2070 }
2071 newsrv->check.inter = val;
2072 cur_arg += 2;
2073 }
2074 else if (!strcmp(args[cur_arg], "fastinter")) {
2075 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2076 if (err) {
2077 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2078 file, linenum, *err, newsrv->id);
2079 err_code |= ERR_ALERT | ERR_FATAL;
2080 goto out;
2081 }
2082 if (val <= 0) {
2083 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2084 file, linenum, val, args[cur_arg], newsrv->id);
2085 err_code |= ERR_ALERT | ERR_FATAL;
2086 goto out;
2087 }
2088 newsrv->check.fastinter = val;
2089 cur_arg += 2;
2090 }
2091 else if (!strcmp(args[cur_arg], "downinter")) {
2092 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2093 if (err) {
2094 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2095 file, linenum, *err, newsrv->id);
2096 err_code |= ERR_ALERT | ERR_FATAL;
2097 goto out;
2098 }
2099 if (val <= 0) {
2100 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2101 file, linenum, val, args[cur_arg], newsrv->id);
2102 err_code |= ERR_ALERT | ERR_FATAL;
2103 goto out;
2104 }
2105 newsrv->check.downinter = val;
2106 cur_arg += 2;
2107 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002108 else if (!strcmp(args[cur_arg], "port")) {
2109 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002110 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002111 cur_arg += 2;
2112 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002113 else if (!strcmp(args[cur_arg], "weight")) {
2114 int w;
2115 w = atol(args[cur_arg + 1]);
2116 if (w < 0 || w > SRV_UWGHT_MAX) {
2117 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2118 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2119 err_code |= ERR_ALERT | ERR_FATAL;
2120 goto out;
2121 }
2122 newsrv->uweight = newsrv->iweight = w;
2123 cur_arg += 2;
2124 }
2125 else if (!strcmp(args[cur_arg], "minconn")) {
2126 newsrv->minconn = atol(args[cur_arg + 1]);
2127 cur_arg += 2;
2128 }
2129 else if (!strcmp(args[cur_arg], "maxconn")) {
2130 newsrv->maxconn = atol(args[cur_arg + 1]);
2131 cur_arg += 2;
2132 }
2133 else if (!strcmp(args[cur_arg], "maxqueue")) {
2134 newsrv->maxqueue = atol(args[cur_arg + 1]);
2135 cur_arg += 2;
2136 }
2137 else if (!strcmp(args[cur_arg], "slowstart")) {
2138 /* slowstart is stored in seconds */
2139 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2140 if (err) {
2141 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2142 file, linenum, *err, newsrv->id);
2143 err_code |= ERR_ALERT | ERR_FATAL;
2144 goto out;
2145 }
2146 newsrv->slowstart = (val + 999) / 1000;
2147 cur_arg += 2;
2148 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002149 else if (!strcmp(args[cur_arg], "on-error")) {
2150 if (!strcmp(args[cur_arg + 1], "fastinter"))
2151 newsrv->onerror = HANA_ONERR_FASTINTER;
2152 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2153 newsrv->onerror = HANA_ONERR_FAILCHK;
2154 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2155 newsrv->onerror = HANA_ONERR_SUDDTH;
2156 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2157 newsrv->onerror = HANA_ONERR_MARKDWN;
2158 else {
2159 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2160 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2161 file, linenum, args[cur_arg], args[cur_arg + 1]);
2162 err_code |= ERR_ALERT | ERR_FATAL;
2163 goto out;
2164 }
2165
2166 cur_arg += 2;
2167 }
2168 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2169 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2170 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2171 else {
2172 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2173 file, linenum, args[cur_arg], args[cur_arg + 1]);
2174 err_code |= ERR_ALERT | ERR_FATAL;
2175 goto out;
2176 }
2177
2178 cur_arg += 2;
2179 }
2180 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2181 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2182 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2183 else {
2184 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2185 file, linenum, args[cur_arg], args[cur_arg + 1]);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189
2190 cur_arg += 2;
2191 }
2192 else if (!strcmp(args[cur_arg], "error-limit")) {
2193 if (!*args[cur_arg + 1]) {
2194 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2195 file, linenum, args[cur_arg]);
2196 err_code |= ERR_ALERT | ERR_FATAL;
2197 goto out;
2198 }
2199
2200 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2201
2202 if (newsrv->consecutive_errors_limit <= 0) {
2203 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2204 file, linenum, args[cur_arg]);
2205 err_code |= ERR_ALERT | ERR_FATAL;
2206 goto out;
2207 }
2208 cur_arg += 2;
2209 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002210 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
2211 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2212 file, linenum, "usesrc", "source");
2213 err_code |= ERR_ALERT | ERR_FATAL;
2214 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002215 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 else {
2217 static int srv_dumped;
2218 struct srv_kw *kw;
2219 char *err;
2220
2221 kw = srv_find_kw(args[cur_arg]);
2222 if (kw) {
2223 char *err = NULL;
2224 int code;
2225
2226 if (!kw->parse) {
2227 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2228 file, linenum, args[0], args[1], args[cur_arg]);
2229 cur_arg += 1 + kw->skip ;
2230 err_code |= ERR_ALERT | ERR_FATAL;
2231 goto out;
2232 }
2233
2234 if (defsrv && !kw->default_ok) {
2235 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2236 file, linenum, args[0], args[1], args[cur_arg]);
2237 cur_arg += 1 + kw->skip ;
2238 err_code |= ERR_ALERT;
2239 continue;
2240 }
2241
2242 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2243 err_code |= code;
2244
2245 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002246 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002247 if (code & ERR_FATAL) {
2248 free(err);
2249 cur_arg += 1 + kw->skip;
2250 goto out;
2251 }
2252 }
2253 free(err);
2254 cur_arg += 1 + kw->skip;
2255 continue;
2256 }
2257
2258 err = NULL;
2259 if (!srv_dumped) {
2260 srv_dump_kws(&err);
2261 indent_msg(&err, 4);
2262 srv_dumped = 1;
2263 }
2264
2265 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2266 file, linenum, args[0], args[1], args[cur_arg],
2267 err ? " Registered keywords :" : "", err ? err : "");
2268 free(err);
2269
2270 err_code |= ERR_ALERT | ERR_FATAL;
2271 goto out;
2272 }
2273 }
2274
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002275 /* This check is done only for 'server' instances. */
2276 if (!defsrv && newsrv->do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002277 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002278
2279 if (newsrv->trackit) {
2280 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
2281 file, linenum);
2282 err_code |= ERR_ALERT | ERR_FATAL;
2283 goto out;
2284 }
2285
Willy Tarreau272adea2014-03-31 10:39:59 +02002286 /*
2287 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02002288 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02002289 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002290 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09002291 (is_inet_addr(&newsrv->check.addr) ||
2292 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01002293 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002294 struct list *l;
2295
2296 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
2297 if (!r) {
2298 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
2299 file, linenum, newsrv->id);
2300 err_code |= ERR_ALERT | ERR_FATAL;
2301 goto out;
2302 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002303 /* search the first action (connect / send / expect) in the list */
2304 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002305 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002306 if (r->action != TCPCHK_ACT_COMMENT)
2307 break;
2308 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002309 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
2310 Alert("parsing [%s:%d] : server %s has neither service port nor check port nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
2311 file, linenum, newsrv->id);
2312 err_code |= ERR_ALERT | ERR_FATAL;
2313 goto out;
2314 }
2315 else {
2316 /* scan the tcp-check ruleset to ensure a port has been configured */
2317 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002318 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002319 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
2320 Alert("parsing [%s:%d] : server %s has neither service port nor check port, and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
2321 file, linenum, newsrv->id);
2322 err_code |= ERR_ALERT | ERR_FATAL;
2323 goto out;
2324 }
2325 }
2326 }
2327 }
2328
2329 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09002330 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002331 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002332 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2333 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002334 goto out;
2335 }
2336
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002337 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01002338 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002339
Willy Tarreau272adea2014-03-31 10:39:59 +02002340 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002341 global.maxsock++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002342 }
2343
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002344 if (!defsrv && newsrv->do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002345 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002346
2347 if (!newsrv->agent.port) {
2348 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2349 file, linenum, newsrv->id);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
2352 }
2353
2354 if (!newsrv->agent.inter)
2355 newsrv->agent.inter = newsrv->check.inter;
2356
Simon Hormanb1900d52015-01-30 11:22:54 +09002357 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02002358 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002359 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2360 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002361 goto out;
2362 }
2363
2364 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002365 global.maxsock++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002366 }
2367
2368 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02002369 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02002370 curproxy->srv_bck++;
2371 else
2372 curproxy->srv_act++;
2373
Willy Tarreauc5150da2014-05-13 19:27:31 +02002374 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002375 }
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002376#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2377 if (!defsrv && newsrv->sni_expr) {
2378 int code;
2379 char *err;
2380
2381 err = NULL;
2382
2383 code = server_parse_sni_expr(newsrv, curproxy, &err);
2384 err_code |= code;
2385 if (code) {
2386 display_parser_err(file, linenum, args, cur_arg, &err);
2387 free(err);
2388 if (code & ERR_FATAL)
2389 goto out;
2390 }
2391 }
2392#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002394 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002395 return 0;
2396
2397 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002398 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002399 free(errmsg);
2400 return err_code;
2401}
2402
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002403/* Returns a pointer to the first server matching either id <id>.
2404 * NULL is returned if no match is found.
2405 * the lookup is performed in the backend <bk>
2406 */
2407struct server *server_find_by_id(struct proxy *bk, int id)
2408{
2409 struct eb32_node *eb32;
2410 struct server *curserver;
2411
2412 if (!bk || (id ==0))
2413 return NULL;
2414
2415 /* <bk> has no backend capabilities, so it can't have a server */
2416 if (!(bk->cap & PR_CAP_BE))
2417 return NULL;
2418
2419 curserver = NULL;
2420
2421 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2422 if (eb32)
2423 curserver = container_of(eb32, struct server, conf.id);
2424
2425 return curserver;
2426}
2427
2428/* Returns a pointer to the first server matching either name <name>, or id
2429 * if <name> starts with a '#'. NULL is returned if no match is found.
2430 * the lookup is performed in the backend <bk>
2431 */
2432struct server *server_find_by_name(struct proxy *bk, const char *name)
2433{
2434 struct server *curserver;
2435
2436 if (!bk || !name)
2437 return NULL;
2438
2439 /* <bk> has no backend capabilities, so it can't have a server */
2440 if (!(bk->cap & PR_CAP_BE))
2441 return NULL;
2442
2443 curserver = NULL;
2444 if (*name == '#') {
2445 curserver = server_find_by_id(bk, atoi(name + 1));
2446 if (curserver)
2447 return curserver;
2448 }
2449 else {
2450 curserver = bk->srv;
2451
2452 while (curserver && (strcmp(curserver->id, name) != 0))
2453 curserver = curserver->next;
2454
2455 if (curserver)
2456 return curserver;
2457 }
2458
2459 return NULL;
2460}
2461
2462struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2463{
2464 struct server *byname;
2465 struct server *byid;
2466
2467 if (!name && !id)
2468 return NULL;
2469
2470 if (diff)
2471 *diff = 0;
2472
2473 byname = byid = NULL;
2474
2475 if (name) {
2476 byname = server_find_by_name(bk, name);
2477 if (byname && (!id || byname->puid == id))
2478 return byname;
2479 }
2480
2481 /* remaining possibilities :
2482 * - name not set
2483 * - name set but not found
2484 * - name found but ID doesn't match
2485 */
2486 if (id) {
2487 byid = server_find_by_id(bk, id);
2488 if (byid) {
2489 if (byname) {
2490 /* use id only if forced by configuration */
2491 if (byid->flags & SRV_F_FORCED_ID) {
2492 if (diff)
2493 *diff |= 2;
2494 return byid;
2495 }
2496 else {
2497 if (diff)
2498 *diff |= 1;
2499 return byname;
2500 }
2501 }
2502
2503 /* remaining possibilities:
2504 * - name not set
2505 * - name set but not found
2506 */
2507 if (name && diff)
2508 *diff |= 2;
2509 return byid;
2510 }
2511
2512 /* id bot found */
2513 if (byname) {
2514 if (diff)
2515 *diff |= 1;
2516 return byname;
2517 }
2518 }
2519
2520 return NULL;
2521}
2522
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002523/* Update a server state using the parameters available in the params list */
2524static void srv_update_state(struct server *srv, int version, char **params)
2525{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002526 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002527 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002528
2529 /* fields since version 1
2530 * and common to all other upcoming versions
2531 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002532 enum srv_state srv_op_state;
2533 enum srv_admin srv_admin_state;
2534 unsigned srv_uweight, srv_iweight;
2535 unsigned long srv_last_time_change;
2536 short srv_check_status;
2537 enum chk_result srv_check_result;
2538 int srv_check_health;
2539 int srv_check_state, srv_agent_state;
2540 int bk_f_forced_id;
2541 int srv_f_forced_id;
2542
Willy Tarreau31138fa2015-09-29 18:38:47 +02002543 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002544 switch (version) {
2545 case 1:
2546 /*
2547 * now we can proceed with server's state update:
2548 * srv_addr: params[0]
2549 * srv_op_state: params[1]
2550 * srv_admin_state: params[2]
2551 * srv_uweight: params[3]
2552 * srv_iweight: params[4]
2553 * srv_last_time_change: params[5]
2554 * srv_check_status: params[6]
2555 * srv_check_result: params[7]
2556 * srv_check_health: params[8]
2557 * srv_check_state: params[9]
2558 * srv_agent_state: params[10]
2559 * bk_f_forced_id: params[11]
2560 * srv_f_forced_id: params[12]
2561 */
2562
2563 /* validating srv_op_state */
2564 p = NULL;
2565 errno = 0;
2566 srv_op_state = strtol(params[1], &p, 10);
2567 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2568 (srv_op_state != SRV_ST_STOPPED &&
2569 srv_op_state != SRV_ST_STARTING &&
2570 srv_op_state != SRV_ST_RUNNING &&
2571 srv_op_state != SRV_ST_STOPPING)) {
2572 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2573 }
2574
2575 /* validating srv_admin_state */
2576 p = NULL;
2577 errno = 0;
2578 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002579
2580 /* inherited statuses will be recomputed later */
2581 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2582
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002583 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2584 (srv_admin_state != 0 &&
2585 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002586 srv_admin_state != SRV_ADMF_CMAINT &&
2587 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002588 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002589 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002590 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2591 }
2592
2593 /* validating srv_uweight */
2594 p = NULL;
2595 errno = 0;
2596 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002597 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002598 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2599
2600 /* validating srv_iweight */
2601 p = NULL;
2602 errno = 0;
2603 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002604 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002605 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2606
2607 /* validating srv_last_time_change */
2608 p = NULL;
2609 errno = 0;
2610 srv_last_time_change = strtol(params[5], &p, 10);
2611 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2612 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2613
2614 /* validating srv_check_status */
2615 p = NULL;
2616 errno = 0;
2617 srv_check_status = strtol(params[6], &p, 10);
2618 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2619 (srv_check_status >= HCHK_STATUS_SIZE))
2620 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2621
2622 /* validating srv_check_result */
2623 p = NULL;
2624 errno = 0;
2625 srv_check_result = strtol(params[7], &p, 10);
2626 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2627 (srv_check_result != CHK_RES_UNKNOWN &&
2628 srv_check_result != CHK_RES_NEUTRAL &&
2629 srv_check_result != CHK_RES_FAILED &&
2630 srv_check_result != CHK_RES_PASSED &&
2631 srv_check_result != CHK_RES_CONDPASS)) {
2632 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2633 }
2634
2635 /* validating srv_check_health */
2636 p = NULL;
2637 errno = 0;
2638 srv_check_health = strtol(params[8], &p, 10);
2639 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2640 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2641
2642 /* validating srv_check_state */
2643 p = NULL;
2644 errno = 0;
2645 srv_check_state = strtol(params[9], &p, 10);
2646 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2647 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2648 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2649
2650 /* validating srv_agent_state */
2651 p = NULL;
2652 errno = 0;
2653 srv_agent_state = strtol(params[10], &p, 10);
2654 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2655 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2656 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2657
2658 /* validating bk_f_forced_id */
2659 p = NULL;
2660 errno = 0;
2661 bk_f_forced_id = strtol(params[11], &p, 10);
2662 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2663 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2664
2665 /* validating srv_f_forced_id */
2666 p = NULL;
2667 errno = 0;
2668 srv_f_forced_id = strtol(params[12], &p, 10);
2669 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2670 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2671
2672
2673 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002674 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002675 goto out;
2676
2677 /* recover operational state and apply it to this server
2678 * and all servers tracking this one */
2679 switch (srv_op_state) {
2680 case SRV_ST_STOPPED:
2681 srv->check.health = 0;
2682 srv_set_stopped(srv, "changed from server-state after a reload");
2683 break;
2684 case SRV_ST_STARTING:
2685 srv->state = srv_op_state;
2686 break;
2687 case SRV_ST_STOPPING:
2688 srv->check.health = srv->check.rise + srv->check.fall - 1;
2689 srv_set_stopping(srv, "changed from server-state after a reload");
2690 break;
2691 case SRV_ST_RUNNING:
2692 srv->check.health = srv->check.rise + srv->check.fall - 1;
2693 srv_set_running(srv, "");
2694 break;
2695 }
2696
2697 /* When applying server state, the following rules apply:
2698 * - in case of a configuration change, we apply the setting from the new
2699 * configuration, regardless of old running state
2700 * - if no configuration change, we apply old running state only if old running
2701 * state is different from new configuration state
2702 */
2703 /* configuration has changed */
2704 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2705 if (srv->admin & SRV_ADMF_CMAINT)
2706 srv_adm_set_maint(srv);
2707 else
2708 srv_adm_set_ready(srv);
2709 }
2710 /* configuration is the same, let's compate old running state and new conf state */
2711 else {
2712 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2713 srv_adm_set_maint(srv);
2714 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2715 srv_adm_set_ready(srv);
2716 }
2717 /* apply drain mode if server is currently enabled */
2718 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2719 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002720 * (srv->iweight is the weight set up in configuration).
2721 * There are two possible reasons for FDRAIN to have been present :
2722 * - previous config weight was zero
2723 * - "set server b/s drain" was sent to the CLI
2724 *
2725 * In the first case, we simply want to drop this drain state
2726 * if the new weight is not zero anymore, meaning the administrator
2727 * has intentionally turned the weight back to a positive value to
2728 * enable the server again after an operation. In the second case,
2729 * the drain state was forced on the CLI regardless of the config's
2730 * weight so we don't want a change to the config weight to lose this
2731 * status. What this means is :
2732 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2733 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002734 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002735 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002736 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002737 }
2738
2739 srv->last_change = date.tv_sec - srv_last_time_change;
2740 srv->check.status = srv_check_status;
2741 srv->check.result = srv_check_result;
2742 srv->check.health = srv_check_health;
2743
2744 /* Only case we want to apply is removing ENABLED flag which could have been
2745 * done by the "disable health" command over the stats socket
2746 */
2747 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2748 (srv_check_state & CHK_ST_CONFIGURED) &&
2749 !(srv_check_state & CHK_ST_ENABLED))
2750 srv->check.state &= ~CHK_ST_ENABLED;
2751
2752 /* Only case we want to apply is removing ENABLED flag which could have been
2753 * done by the "disable agent" command over the stats socket
2754 */
2755 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2756 (srv_agent_state & CHK_ST_CONFIGURED) &&
2757 !(srv_agent_state & CHK_ST_ENABLED))
2758 srv->agent.state &= ~CHK_ST_ENABLED;
2759
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002760 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2761 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002762 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002763 * It means that a configuration file change has precedence over a unix socket change
2764 * for server's weight
2765 *
2766 * by default, HAProxy applies the following weight when parsing the configuration
2767 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002768 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002769 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002770 srv->uweight = srv_uweight;
2771 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002772 server_recalc_eweight(srv);
2773
Willy Tarreaue5a60682016-11-09 14:54:53 +01002774 /* load server IP address */
2775 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002776 break;
2777 default:
2778 chunk_appendf(msg, ", version '%d' not supported", version);
2779 }
2780
2781 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002782 if (msg->len) {
2783 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002784 Warning("server-state application failed for server '%s/%s'%s",
2785 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002786 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787}
2788
2789/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2790 * For each proxy, it does the following:
2791 * - opens its server state file (either one or local one)
2792 * - read whole file, line by line
2793 * - analyse each line to check if it matches our current backend:
2794 * - backend name matches
2795 * - backend id matches if id is forced and name doesn't match
2796 * - if the server pointed by the line is found, then state is applied
2797 *
2798 * If the running backend uuid or id differs from the state file, then HAProxy reports
2799 * a warning.
2800 */
2801void apply_server_state(void)
2802{
2803 char *cur, *end;
2804 char mybuf[SRV_STATE_LINE_MAXLEN];
2805 int mybuflen;
2806 char *params[SRV_STATE_FILE_MAX_FIELDS];
2807 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2808 int arg, srv_arg, version, diff;
2809 FILE *f;
2810 char *filepath;
2811 char globalfilepath[MAXPATHLEN + 1];
2812 char localfilepath[MAXPATHLEN + 1];
2813 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2814 extern struct proxy *proxy;
2815 struct proxy *curproxy, *bk;
2816 struct server *srv;
2817
2818 globalfilepathlen = 0;
2819 /* create the globalfilepath variable */
2820 if (global.server_state_file) {
2821 /* absolute path or no base directory provided */
2822 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2823 len = strlen(global.server_state_file);
2824 if (len > MAXPATHLEN) {
2825 globalfilepathlen = 0;
2826 goto globalfileerror;
2827 }
2828 memcpy(globalfilepath, global.server_state_file, len);
2829 globalfilepath[len] = '\0';
2830 globalfilepathlen = len;
2831 }
2832 else if (global.server_state_base) {
2833 len = strlen(global.server_state_base);
2834 globalfilepathlen += len;
2835
2836 if (globalfilepathlen > MAXPATHLEN) {
2837 globalfilepathlen = 0;
2838 goto globalfileerror;
2839 }
2840 strncpy(globalfilepath, global.server_state_base, len);
2841 globalfilepath[globalfilepathlen] = 0;
2842
2843 /* append a slash if needed */
2844 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2845 if (globalfilepathlen + 1 > MAXPATHLEN) {
2846 globalfilepathlen = 0;
2847 goto globalfileerror;
2848 }
2849 globalfilepath[globalfilepathlen++] = '/';
2850 }
2851
2852 len = strlen(global.server_state_file);
2853 if (globalfilepathlen + len > MAXPATHLEN) {
2854 globalfilepathlen = 0;
2855 goto globalfileerror;
2856 }
2857 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2858 globalfilepathlen += len;
2859 globalfilepath[globalfilepathlen++] = 0;
2860 }
2861 }
2862 globalfileerror:
2863 if (globalfilepathlen == 0)
2864 globalfilepath[0] = '\0';
2865
2866 /* read servers state from local file */
2867 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2868 /* servers are only in backends */
2869 if (!(curproxy->cap & PR_CAP_BE))
2870 continue;
2871 fileopenerr = 0;
2872 filepath = NULL;
2873
2874 /* search server state file path and name */
2875 switch (curproxy->load_server_state_from_file) {
2876 /* read servers state from global file */
2877 case PR_SRV_STATE_FILE_GLOBAL:
2878 /* there was an error while generating global server state file path */
2879 if (globalfilepathlen == 0)
2880 continue;
2881 filepath = globalfilepath;
2882 fileopenerr = 1;
2883 break;
2884 /* this backend has its own file */
2885 case PR_SRV_STATE_FILE_LOCAL:
2886 localfilepathlen = 0;
2887 localfilepath[0] = '\0';
2888 len = 0;
2889 /* create the localfilepath variable */
2890 /* absolute path or no base directory provided */
2891 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2892 len = strlen(curproxy->server_state_file_name);
2893 if (len > MAXPATHLEN) {
2894 localfilepathlen = 0;
2895 goto localfileerror;
2896 }
2897 memcpy(localfilepath, curproxy->server_state_file_name, len);
2898 localfilepath[len] = '\0';
2899 localfilepathlen = len;
2900 }
2901 else if (global.server_state_base) {
2902 len = strlen(global.server_state_base);
2903 localfilepathlen += len;
2904
2905 if (localfilepathlen > MAXPATHLEN) {
2906 localfilepathlen = 0;
2907 goto localfileerror;
2908 }
2909 strncpy(localfilepath, global.server_state_base, len);
2910 localfilepath[localfilepathlen] = 0;
2911
2912 /* append a slash if needed */
2913 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2914 if (localfilepathlen + 1 > MAXPATHLEN) {
2915 localfilepathlen = 0;
2916 goto localfileerror;
2917 }
2918 localfilepath[localfilepathlen++] = '/';
2919 }
2920
2921 len = strlen(curproxy->server_state_file_name);
2922 if (localfilepathlen + len > MAXPATHLEN) {
2923 localfilepathlen = 0;
2924 goto localfileerror;
2925 }
2926 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2927 localfilepathlen += len;
2928 localfilepath[localfilepathlen++] = 0;
2929 }
2930 filepath = localfilepath;
2931 localfileerror:
2932 if (localfilepathlen == 0)
2933 localfilepath[0] = '\0';
2934
2935 break;
2936 case PR_SRV_STATE_FILE_NONE:
2937 default:
2938 continue;
2939 }
2940
2941 /* preload global state file */
2942 errno = 0;
2943 f = fopen(filepath, "r");
2944 if (errno && fileopenerr)
2945 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2946 if (!f)
2947 continue;
2948
2949 mybuf[0] = '\0';
2950 mybuflen = 0;
2951 version = 0;
2952
2953 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002954 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2955 Warning("Can't read first line of the server state file '%s'\n", filepath);
2956 goto fileclose;
2957 }
2958
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002959 cur = mybuf;
2960 version = atoi(cur);
2961 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2962 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002963 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002964
2965 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2966 int bk_f_forced_id = 0;
2967 int check_id = 0;
2968 int check_name = 0;
2969
2970 mybuflen = strlen(mybuf);
2971 cur = mybuf;
2972 end = cur + mybuflen;
2973
2974 bk = NULL;
2975 srv = NULL;
2976
2977 /* we need at least one character */
2978 if (mybuflen == 0)
2979 continue;
2980
2981 /* ignore blank characters at the beginning of the line */
2982 while (isspace(*cur))
2983 ++cur;
2984
2985 if (cur == end)
2986 continue;
2987
2988 /* ignore comment line */
2989 if (*cur == '#')
2990 continue;
2991
2992 /* we're now ready to move the line into *srv_params[] */
2993 params[0] = cur;
2994 arg = 1;
2995 srv_arg = 0;
2996 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2997 if (isspace(*cur)) {
2998 *cur = '\0';
2999 ++cur;
3000 while (isspace(*cur))
3001 ++cur;
3002 switch (version) {
3003 case 1:
3004 /*
3005 * srv_addr: params[4] => srv_params[0]
3006 * srv_op_state: params[5] => srv_params[1]
3007 * srv_admin_state: params[6] => srv_params[2]
3008 * srv_uweight: params[7] => srv_params[3]
3009 * srv_iweight: params[8] => srv_params[4]
3010 * srv_last_time_change: params[9] => srv_params[5]
3011 * srv_check_status: params[10] => srv_params[6]
3012 * srv_check_result: params[11] => srv_params[7]
3013 * srv_check_health: params[12] => srv_params[8]
3014 * srv_check_state: params[13] => srv_params[9]
3015 * srv_agent_state: params[14] => srv_params[10]
3016 * bk_f_forced_id: params[15] => srv_params[11]
3017 * srv_f_forced_id: params[16] => srv_params[12]
3018 */
3019 if (arg >= 4) {
3020 srv_params[srv_arg] = cur;
3021 ++srv_arg;
3022 }
3023 break;
3024 }
3025
3026 params[arg] = cur;
3027 ++arg;
3028 }
3029 else {
3030 ++cur;
3031 }
3032 }
3033
3034 /* if line is incomplete line, then ignore it.
3035 * otherwise, update useful flags */
3036 switch (version) {
3037 case 1:
3038 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3039 continue;
3040 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3041 check_id = (atoi(params[0]) == curproxy->uuid);
3042 check_name = (strcmp(curproxy->id, params[1]) == 0);
3043 break;
3044 }
3045
3046 diff = 0;
3047 bk = curproxy;
3048
3049 /* if backend can't be found, let's continue */
3050 if (!check_id && !check_name)
3051 continue;
3052 else if (!check_id && check_name) {
3053 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3054 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3055 }
3056 else if (check_id && !check_name) {
3057 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3058 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3059 /* if name doesn't match, we still want to update curproxy if the backend id
3060 * was forced in previous the previous configuration */
3061 if (!bk_f_forced_id)
3062 continue;
3063 }
3064
3065 /* look for the server by its id: param[2] */
3066 /* else look for the server by its name: param[3] */
3067 diff = 0;
3068 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3069
3070 if (!srv) {
3071 /* if no server found, then warning and continue with next line */
3072 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3073 params[3], params[2], params[0], params[1]);
3074 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3075 params[3], params[2], params[0], params[1]);
3076 continue;
3077 }
3078 else if (diff & PR_FBM_MISMATCH_ID) {
3079 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);
3080 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);
3081 }
3082 else if (diff & PR_FBM_MISMATCH_NAME) {
3083 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);
3084 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);
3085 }
3086
3087 /* now we can proceed with server's state update */
3088 srv_update_state(srv, version, srv_params);
3089 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003090fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003091 fclose(f);
3092 }
3093}
3094
Simon Horman7d09b9a2013-02-12 10:45:51 +09003095/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003096 * update a server's current IP address.
3097 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3098 * ip is in network format.
3099 * updater is a string which contains an information about the requester of the update.
3100 * updater is used if not NULL.
3101 *
3102 * A log line and a stderr warning message is generated based on server's backend options.
3103 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003104int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003105{
3106 /* generates a log line and a warning on stderr */
3107 if (1) {
3108 /* book enough space for both IPv4 and IPv6 */
3109 char oldip[INET6_ADDRSTRLEN];
3110 char newip[INET6_ADDRSTRLEN];
3111
3112 memset(oldip, '\0', INET6_ADDRSTRLEN);
3113 memset(newip, '\0', INET6_ADDRSTRLEN);
3114
3115 /* copy old IP address in a string */
3116 switch (s->addr.ss_family) {
3117 case AF_INET:
3118 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3119 break;
3120 case AF_INET6:
3121 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3122 break;
3123 };
3124
3125 /* copy new IP address in a string */
3126 switch (ip_sin_family) {
3127 case AF_INET:
3128 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3129 break;
3130 case AF_INET6:
3131 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3132 break;
3133 };
3134
3135 /* save log line into a buffer */
3136 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3137 s->proxy->id, s->id, oldip, newip, updater);
3138
3139 /* write the buffer on stderr */
3140 Warning("%s.\n", trash.str);
3141
3142 /* send a log */
3143 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3144 }
3145
3146 /* save the new IP family */
3147 s->addr.ss_family = ip_sin_family;
3148 /* save the new IP address */
3149 switch (ip_sin_family) {
3150 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003151 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003152 break;
3153 case AF_INET6:
3154 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3155 break;
3156 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003157 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003158
3159 return 0;
3160}
3161
3162/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003163 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3164 *
3165 * Caller can pass its name through <updater> to get it integrated in the response message
3166 * returned by the function.
3167 *
3168 * The function first does the following, in that order:
3169 * - validates the new addr and/or port
3170 * - checks if an update is required (new IP or port is different than current ones)
3171 * - checks the update is allowed:
3172 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3173 * - allow all changes if no CHECKS are configured
3174 * - if CHECK is configured:
3175 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3176 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3177 * conditions are met
3178 */
3179const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3180{
3181 struct sockaddr_storage sa;
3182 int ret, port_change_required;
3183 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003184 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003185 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003186 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003187
3188 msg = get_trash_chunk();
3189 chunk_reset(msg);
3190
3191 if (addr) {
3192 memset(&sa, 0, sizeof(struct sockaddr_storage));
3193 if (str2ip2(addr, &sa, 0) == NULL) {
3194 chunk_printf(msg, "Invalid addr '%s'", addr);
3195 goto out;
3196 }
3197
3198 /* changes are allowed on AF_INET* families only */
3199 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3200 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3201 goto out;
3202 }
3203
3204 /* collecting data currently setup */
3205 memset(current_addr, '\0', sizeof(current_addr));
3206 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3207 /* changes are allowed on AF_INET* families only */
3208 if ((ret != AF_INET) && (ret != AF_INET6)) {
3209 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3210 goto out;
3211 }
3212
3213 /* applying ADDR changes if required and allowed
3214 * ipcmp returns 0 when both ADDR are the same
3215 */
3216 if (ipcmp(&s->addr, &sa) == 0) {
3217 chunk_appendf(msg, "no need to change the addr");
3218 goto port;
3219 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003220 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003221 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003222
3223 /* we also need to update check's ADDR only if it uses the server's one */
3224 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003225 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003226 }
3227
3228 /* we also need to update agent ADDR only if it use the server's one */
3229 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003230 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003231 }
3232
3233 /* update report for caller */
3234 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3235 }
3236
3237 port:
3238 if (port) {
3239 char sign = '\0';
3240 char *endptr;
3241
3242 if (addr)
3243 chunk_appendf(msg, ", ");
3244
3245 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003246 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003247
3248 /* check if PORT change is required */
3249 port_change_required = 0;
3250
3251 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003252 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003253 new_port = strtol(port, &endptr, 10);
3254 if ((errno != 0) || (port == endptr)) {
3255 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3256 goto out;
3257 }
3258
3259 /* check if caller triggers a port mapped or offset */
3260 if (sign == '-' || (sign == '+')) {
3261 /* check if server currently uses port map */
3262 if (!(s->flags & SRV_F_MAPPORTS)) {
3263 /* switch from fixed port to port map mandatorily triggers
3264 * a port change */
3265 port_change_required = 1;
3266 /* check is configured
3267 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3268 * prevent PORT change if check doesn't have it's dedicated port while switching
3269 * to port mapping */
3270 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3271 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.");
3272 goto out;
3273 }
3274 }
3275 /* we're already using port maps */
3276 else {
3277 port_change_required = current_port != new_port;
3278 }
3279 }
3280 /* fixed port */
3281 else {
3282 port_change_required = current_port != new_port;
3283 }
3284
3285 /* applying PORT changes if required and update response message */
3286 if (port_change_required) {
3287 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003288 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003289 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003290
3291 /* prepare message */
3292 chunk_appendf(msg, "port changed from '");
3293 if (s->flags & SRV_F_MAPPORTS)
3294 chunk_appendf(msg, "+");
3295 chunk_appendf(msg, "%d' to '", current_port);
3296
3297 if (sign == '-') {
3298 s->flags |= SRV_F_MAPPORTS;
3299 chunk_appendf(msg, "%c", sign);
3300 /* just use for result output */
3301 new_port = -new_port;
3302 }
3303 else if (sign == '+') {
3304 s->flags |= SRV_F_MAPPORTS;
3305 chunk_appendf(msg, "%c", sign);
3306 }
3307 else {
3308 s->flags &= ~SRV_F_MAPPORTS;
3309 }
3310
3311 chunk_appendf(msg, "%d'", new_port);
3312
3313 /* we also need to update health checks port only if it uses server's realport */
3314 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3315 s->check.port = new_port;
3316 }
3317 }
3318 else {
3319 chunk_appendf(msg, "no need to change the port");
3320 }
3321 }
3322
3323out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003324 if (changed)
3325 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003326 if (updater)
3327 chunk_appendf(msg, " by '%s'", updater);
3328 chunk_appendf(msg, "\n");
3329 return msg->str;
3330}
3331
3332
3333/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003334 * update server status based on result of name resolution
3335 * returns:
3336 * 0 if server status is updated
3337 * 1 if server status has not changed
3338 */
3339int snr_update_srv_status(struct server *s)
3340{
3341 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003342 struct dns_resolvers *resolvers;
3343
3344 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003345
3346 switch (resolution->status) {
3347 case RSLV_STATUS_NONE:
3348 /* status when HAProxy has just (re)started */
3349 trigger_resolution(s);
3350 break;
3351
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003352 case RSLV_STATUS_VALID:
3353 /*
3354 * resume health checks
3355 * server will be turned back on if health check is safe
3356 */
3357 if (!(s->admin & SRV_ADMF_RMAINT))
3358 return 1;
3359 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3360 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3361 s->proxy->id, s->id);
3362
3363 Warning("%s.\n", trash.str);
3364 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3365 return 0;
3366
3367 case RSLV_STATUS_NX:
3368 /* stop server if resolution is NX for a long enough period */
3369 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3370 if (s->admin & SRV_ADMF_RMAINT)
3371 return 1;
3372 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3373 return 0;
3374 }
3375 break;
3376
3377 case RSLV_STATUS_TIMEOUT:
3378 /* stop server if resolution is TIMEOUT for a long enough period */
3379 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3380 if (s->admin & SRV_ADMF_RMAINT)
3381 return 1;
3382 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3383 return 0;
3384 }
3385 break;
3386
3387 case RSLV_STATUS_REFUSED:
3388 /* stop server if resolution is REFUSED for a long enough period */
3389 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3390 if (s->admin & SRV_ADMF_RMAINT)
3391 return 1;
3392 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3393 return 0;
3394 }
3395 break;
3396
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003397 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003398 /* stop server if resolution is in unmatched error for a long enough period */
3399 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3400 if (s->admin & SRV_ADMF_RMAINT)
3401 return 1;
3402 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3403 return 0;
3404 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003405 break;
3406 }
3407
3408 return 1;
3409}
3410
3411/*
3412 * Server Name Resolution valid response callback
3413 * It expects:
3414 * - <nameserver>: the name server which answered the valid response
3415 * - <response>: buffer containing a valid DNS response
3416 * - <response_len>: size of <response>
3417 * It performs the following actions:
3418 * - ignore response if current ip found and server family not met
3419 * - update with first new ip found if family is met and current IP is not found
3420 * returns:
3421 * 0 on error
3422 * 1 when no error or safe ignore
3423 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003424int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003425{
3426 struct server *s;
3427 void *serverip, *firstip;
3428 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003429 int ret;
3430 struct chunk *chk = get_trash_chunk();
3431
3432 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003433 firstip = NULL; /* pointer to the first valid response found */
3434 /* it will be used as the new IP if a change is required */
3435 firstip_sin_family = AF_UNSPEC;
3436 serverip = NULL; /* current server IP address */
3437
3438 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003439 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003440
3441 /* initializing server IP pointer */
3442 server_sin_family = s->addr.ss_family;
3443 switch (server_sin_family) {
3444 case AF_INET:
3445 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3446 break;
3447
3448 case AF_INET6:
3449 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3450 break;
3451
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003452 case AF_UNSPEC:
3453 break;
3454
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003455 default:
3456 goto invalid;
3457 }
3458
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003459 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003460 serverip, server_sin_family, &firstip,
3461 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003462
3463 switch (ret) {
3464 case DNS_UPD_NO:
3465 if (resolution->status != RSLV_STATUS_VALID) {
3466 resolution->status = RSLV_STATUS_VALID;
3467 resolution->last_status_change = now_ms;
3468 }
3469 goto stop_resolution;
3470
3471 case DNS_UPD_SRVIP_NOT_FOUND:
3472 goto save_ip;
3473
3474 case DNS_UPD_CNAME:
3475 if (resolution->status != RSLV_STATUS_VALID) {
3476 resolution->status = RSLV_STATUS_VALID;
3477 resolution->last_status_change = now_ms;
3478 }
3479 goto invalid;
3480
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003481 case DNS_UPD_NO_IP_FOUND:
3482 if (resolution->status != RSLV_STATUS_OTHER) {
3483 resolution->status = RSLV_STATUS_OTHER;
3484 resolution->last_status_change = now_ms;
3485 }
3486 goto stop_resolution;
3487
Baptiste Assmannfad03182015-10-28 02:03:32 +01003488 case DNS_UPD_NAME_ERROR:
3489 /* if this is not the last expected response, we ignore it */
3490 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3491 return 0;
3492 /* update resolution status to OTHER error type */
3493 if (resolution->status != RSLV_STATUS_OTHER) {
3494 resolution->status = RSLV_STATUS_OTHER;
3495 resolution->last_status_change = now_ms;
3496 }
3497 goto stop_resolution;
3498
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003499 default:
3500 goto invalid;
3501
3502 }
3503
3504 save_ip:
3505 nameserver->counters.update += 1;
3506 if (resolution->status != RSLV_STATUS_VALID) {
3507 resolution->status = RSLV_STATUS_VALID;
3508 resolution->last_status_change = now_ms;
3509 }
3510
3511 /* save the first ip we found */
3512 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3513 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3514
3515 stop_resolution:
3516 /* update last resolution date and time */
3517 resolution->last_resolution = now_ms;
3518 /* reset current status flag */
3519 resolution->step = RSLV_STEP_NONE;
3520 /* reset values */
3521 dns_reset_resolution(resolution);
3522
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003523 dns_update_resolvers_timeout(nameserver->resolvers);
3524
3525 snr_update_srv_status(s);
3526 return 0;
3527
3528 invalid:
3529 nameserver->counters.invalid += 1;
3530 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3531 goto stop_resolution;
3532
3533 snr_update_srv_status(s);
3534 return 0;
3535}
3536
3537/*
3538 * Server Name Resolution error management callback
3539 * returns:
3540 * 0 on error
3541 * 1 when no error or safe ignore
3542 */
3543int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3544{
3545 struct server *s;
3546 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003547 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003548
3549 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003550 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003551 resolvers = resolution->resolvers;
3552
3553 /* can be ignored if this is not the last response */
3554 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3555 return 1;
3556 }
3557
3558 switch (error_code) {
3559 case DNS_RESP_INVALID:
3560 case DNS_RESP_WRONG_NAME:
3561 if (resolution->status != RSLV_STATUS_INVALID) {
3562 resolution->status = RSLV_STATUS_INVALID;
3563 resolution->last_status_change = now_ms;
3564 }
3565 break;
3566
3567 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003568 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003569 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003570 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003571 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003572 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3573 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003574
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003575 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003576 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003577 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003578 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003579 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003580 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003581 }
3582 else if (res_preferred_afinet) {
3583 /* fallback from A to AAAA */
3584 resolution->query_type = DNS_RTYPE_AAAA;
3585 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003586 else {
3587 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003588 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003589 resolution->query_type = DNS_RTYPE_A;
3590 } else {
3591 resolution->query_type = DNS_RTYPE_AAAA;
3592 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003593 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003594
3595 dns_send_query(resolution);
3596
3597 /*
3598 * move the resolution to the last element of the FIFO queue
3599 * and update timeout wakeup based on the new first entry
3600 */
3601 if (dns_check_resolution_queue(resolvers) > 1) {
3602 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003603 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003604 /* ex first resolution goes to the end of the queue */
3605 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3606 }
3607 dns_update_resolvers_timeout(resolvers);
3608 goto leave;
3609 }
3610 else {
3611 if (resolution->status != RSLV_STATUS_OTHER) {
3612 resolution->status = RSLV_STATUS_OTHER;
3613 resolution->last_status_change = now_ms;
3614 }
3615 }
3616 break;
3617
3618 case DNS_RESP_NX_DOMAIN:
3619 if (resolution->status != RSLV_STATUS_NX) {
3620 resolution->status = RSLV_STATUS_NX;
3621 resolution->last_status_change = now_ms;
3622 }
3623 break;
3624
3625 case DNS_RESP_REFUSED:
3626 if (resolution->status != RSLV_STATUS_REFUSED) {
3627 resolution->status = RSLV_STATUS_REFUSED;
3628 resolution->last_status_change = now_ms;
3629 }
3630 break;
3631
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003632 case DNS_RESP_TIMEOUT:
3633 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3634 resolution->status = RSLV_STATUS_TIMEOUT;
3635 resolution->last_status_change = now_ms;
3636 }
3637 break;
3638 }
3639
3640 /* update last resolution date and time */
3641 resolution->last_resolution = now_ms;
3642 /* reset current status flag */
3643 resolution->step = RSLV_STEP_NONE;
3644 /* reset values */
3645 dns_reset_resolution(resolution);
3646
3647 LIST_DEL(&resolution->list);
3648 dns_update_resolvers_timeout(resolvers);
3649
3650 leave:
3651 snr_update_srv_status(s);
3652 return 1;
3653}
3654
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003655/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3656 * resolver. This is suited for initial address configuration. Returns 0 on
3657 * success otherwise a non-zero error code. In case of error, *err_code, if
3658 * not NULL, is filled up.
3659 */
3660int srv_set_addr_via_libc(struct server *srv, int *err_code)
3661{
3662 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003663 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003664 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003665 return 1;
3666 }
3667 return 0;
3668}
3669
3670/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3671 * from the state file. This is suited for initial address configuration.
3672 * Returns 0 on success otherwise a non-zero error code. In case of error,
3673 * *err_code, if not NULL, is filled up.
3674 */
3675static int srv_apply_lastaddr(struct server *srv, int *err_code)
3676{
3677 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3678 if (err_code)
3679 *err_code |= ERR_WARN;
3680 return 1;
3681 }
3682 return 0;
3683}
3684
Willy Tarreau25e51522016-11-04 15:10:17 +01003685/* returns 0 if no error, otherwise a combination of ERR_* flags */
3686static int srv_iterate_initaddr(struct server *srv)
3687{
3688 int return_code = 0;
3689 int err_code;
3690 unsigned int methods;
3691
3692 methods = srv->init_addr_methods;
3693 if (!methods) { // default to "last,libc"
3694 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3695 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3696 }
3697
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003698 /* "-dr" : always append "none" so that server addresses resolution
3699 * failures are silently ignored, this is convenient to validate some
3700 * configs out of their environment.
3701 */
3702 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3703 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3704
Willy Tarreau25e51522016-11-04 15:10:17 +01003705 while (methods) {
3706 err_code = 0;
3707 switch (srv_get_next_initaddr(&methods)) {
3708 case SRV_IADDR_LAST:
3709 if (!srv->lastaddr)
3710 continue;
3711 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003712 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003713 return_code |= err_code;
3714 break;
3715
3716 case SRV_IADDR_LIBC:
3717 if (!srv->hostname)
3718 continue;
3719 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003720 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003721 return_code |= err_code;
3722 break;
3723
Willy Tarreau37ebe122016-11-04 15:17:58 +01003724 case SRV_IADDR_NONE:
3725 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003726 if (return_code) {
3727 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3728 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3729 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003730 return return_code;
3731
Willy Tarreau4310d362016-11-02 15:05:56 +01003732 case SRV_IADDR_IP:
3733 ipcpy(&srv->init_addr, &srv->addr);
3734 if (return_code) {
3735 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3736 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3737 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003738 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003739
Willy Tarreau25e51522016-11-04 15:10:17 +01003740 default: /* unhandled method */
3741 break;
3742 }
3743 }
3744
3745 if (!return_code) {
3746 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3747 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3748 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003749 else {
3750 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3751 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3752 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003753
3754 return_code |= ERR_ALERT | ERR_FATAL;
3755 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003756out:
3757 srv_set_dyncookie(srv);
3758 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003759}
3760
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003761/*
3762 * This function parses all backends and all servers within each backend
3763 * and performs servers' addr resolution based on information provided by:
3764 * - configuration file
3765 * - server-state file (states provided by an 'old' haproxy process)
3766 *
3767 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3768 */
3769int srv_init_addr(void)
3770{
3771 struct proxy *curproxy;
3772 int return_code = 0;
3773
3774 curproxy = proxy;
3775 while (curproxy) {
3776 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003777
3778 /* servers are in backend only */
3779 if (!(curproxy->cap & PR_CAP_BE))
3780 goto srv_init_addr_next;
3781
Willy Tarreau25e51522016-11-04 15:10:17 +01003782 for (srv = curproxy->srv; srv; srv = srv->next)
3783 if (srv->hostname)
3784 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003785
3786 srv_init_addr_next:
3787 curproxy = curproxy->next;
3788 }
3789
3790 return return_code;
3791}
3792
Willy Tarreau21b069d2016-11-23 17:15:08 +01003793/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3794 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003795 * 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 +01003796 * used for CLI commands requiring a server name.
3797 * Important: the <arg> is modified to remove the '/'.
3798 */
3799struct server *cli_find_server(struct appctx *appctx, char *arg)
3800{
3801 struct proxy *px;
3802 struct server *sv;
3803 char *line;
3804
3805 /* split "backend/server" and make <line> point to server */
3806 for (line = arg; *line; line++)
3807 if (*line == '/') {
3808 *line++ = '\0';
3809 break;
3810 }
3811
3812 if (!*line || !*arg) {
3813 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003814 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003815 return NULL;
3816 }
3817
3818 if (!get_backend_server(arg, line, &px, &sv)) {
3819 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003820 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003821 return NULL;
3822 }
3823
3824 if (px->state == PR_STSTOPPED) {
3825 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003826 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003827 return NULL;
3828 }
3829
3830 return sv;
3831}
3832
William Lallemand222baf22016-11-19 02:00:33 +01003833
3834static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3835{
3836 struct server *sv;
3837 const char *warning;
3838
3839 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3840 return 1;
3841
3842 sv = cli_find_server(appctx, args[2]);
3843 if (!sv)
3844 return 1;
3845
3846 if (strcmp(args[3], "weight") == 0) {
3847 warning = server_parse_weight_change_request(sv, args[4]);
3848 if (warning) {
3849 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003850 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003851 }
3852 }
3853 else if (strcmp(args[3], "state") == 0) {
3854 if (strcmp(args[4], "ready") == 0)
3855 srv_adm_set_ready(sv);
3856 else if (strcmp(args[4], "drain") == 0)
3857 srv_adm_set_drain(sv);
3858 else if (strcmp(args[4], "maint") == 0)
3859 srv_adm_set_maint(sv);
3860 else {
3861 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003862 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003863 }
3864 }
3865 else if (strcmp(args[3], "health") == 0) {
3866 if (sv->track) {
3867 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003868 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003869 }
3870 else if (strcmp(args[4], "up") == 0) {
3871 sv->check.health = sv->check.rise + sv->check.fall - 1;
3872 srv_set_running(sv, "changed from CLI");
3873 }
3874 else if (strcmp(args[4], "stopping") == 0) {
3875 sv->check.health = sv->check.rise + sv->check.fall - 1;
3876 srv_set_stopping(sv, "changed from CLI");
3877 }
3878 else if (strcmp(args[4], "down") == 0) {
3879 sv->check.health = 0;
3880 srv_set_stopped(sv, "changed from CLI");
3881 }
3882 else {
3883 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003884 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003885 }
3886 }
3887 else if (strcmp(args[3], "agent") == 0) {
3888 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3889 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003890 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003891 }
3892 else if (strcmp(args[4], "up") == 0) {
3893 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3894 srv_set_running(sv, "changed from CLI");
3895 }
3896 else if (strcmp(args[4], "down") == 0) {
3897 sv->agent.health = 0;
3898 srv_set_stopped(sv, "changed from CLI");
3899 }
3900 else {
3901 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003902 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003903 }
3904 }
Misiek2da082d2017-01-09 09:40:42 +01003905 else if (strcmp(args[3], "agent-addr") == 0) {
3906 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3907 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3908 appctx->st0 = CLI_ST_PRINT;
3909 } else {
3910 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3911 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3912 appctx->st0 = CLI_ST_PRINT;
3913 }
3914 }
3915 }
3916 else if (strcmp(args[3], "agent-send") == 0) {
3917 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3918 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3919 appctx->st0 = CLI_ST_PRINT;
3920 } else {
3921 char *nss = strdup(args[4]);
3922 if (!nss) {
3923 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3924 appctx->st0 = CLI_ST_PRINT;
3925 } else {
3926 free(sv->agent.send_string);
3927 sv->agent.send_string = nss;
3928 sv->agent.send_string_len = strlen(args[4]);
3929 }
3930 }
3931 }
William Lallemand222baf22016-11-19 02:00:33 +01003932 else if (strcmp(args[3], "check-port") == 0) {
3933 int i = 0;
3934 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3935 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003936 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003937 }
3938 if ((i < 0) || (i > 65535)) {
3939 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003940 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003941 }
3942 /* prevent the update of port to 0 if MAPPORTS are in use */
3943 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3944 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003945 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003946 return 1;
3947 }
3948 sv->check.port = i;
3949 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003950 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003951 }
3952 else if (strcmp(args[3], "addr") == 0) {
3953 char *addr = NULL;
3954 char *port = NULL;
3955 if (strlen(args[4]) == 0) {
3956 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003957 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003958 return 1;
3959 }
3960 else {
3961 addr = args[4];
3962 }
3963 if (strcmp(args[5], "port") == 0) {
3964 port = args[6];
3965 }
3966 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3967 if (warning) {
3968 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003969 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003970 }
3971 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3972 }
3973 else {
3974 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr' and 'check-port'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003975 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003976 }
3977 return 1;
3978}
3979
William Lallemand6b160942016-11-22 12:34:35 +01003980static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3981{
3982 struct stream_interface *si = appctx->owner;
3983 struct proxy *px;
3984 struct server *sv;
3985 char *line;
3986
3987
3988 /* split "backend/server" and make <line> point to server */
3989 for (line = args[2]; *line; line++)
3990 if (*line == '/') {
3991 *line++ = '\0';
3992 break;
3993 }
3994
3995 if (!*line) {
3996 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003997 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003998 return 1;
3999 }
4000
4001 if (!get_backend_server(args[2], line, &px, &sv)) {
4002 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004003 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004004 return 1;
4005 }
4006
4007 /* return server's effective weight at the moment */
4008 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004009 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004010 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004011 return 0;
4012 }
William Lallemand6b160942016-11-22 12:34:35 +01004013 return 1;
4014}
4015
4016static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4017{
4018 struct server *sv;
4019 const char *warning;
4020
4021 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4022 return 1;
4023
4024 sv = cli_find_server(appctx, args[2]);
4025 if (!sv)
4026 return 1;
4027
4028 warning = server_parse_weight_change_request(sv, args[3]);
4029 if (warning) {
4030 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004031 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004032 }
4033 return 1;
4034}
4035
Willy Tarreaub8026272016-11-23 11:26:56 +01004036/* parse a "set maxconn server" command. It always returns 1. */
4037static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4038{
4039 struct server *sv;
4040 const char *warning;
4041
4042 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4043 return 1;
4044
4045 sv = cli_find_server(appctx, args[3]);
4046 if (!sv)
4047 return 1;
4048
4049 warning = server_parse_maxconn_change_request(sv, args[4]);
4050 if (warning) {
4051 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004052 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004053 }
4054 return 1;
4055}
William Lallemand6b160942016-11-22 12:34:35 +01004056
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004057/* parse a "disable agent" command. It always returns 1. */
4058static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4059{
4060 struct server *sv;
4061
4062 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4063 return 1;
4064
4065 sv = cli_find_server(appctx, args[2]);
4066 if (!sv)
4067 return 1;
4068
4069 sv->agent.state &= ~CHK_ST_ENABLED;
4070 return 1;
4071}
4072
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004073/* parse a "disable health" command. It always returns 1. */
4074static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4075{
4076 struct server *sv;
4077
4078 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4079 return 1;
4080
4081 sv = cli_find_server(appctx, args[2]);
4082 if (!sv)
4083 return 1;
4084
4085 sv->check.state &= ~CHK_ST_ENABLED;
4086 return 1;
4087}
4088
Willy Tarreauffb4d582016-11-24 12:47:00 +01004089/* parse a "disable server" command. It always returns 1. */
4090static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4091{
4092 struct server *sv;
4093
4094 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4095 return 1;
4096
4097 sv = cli_find_server(appctx, args[2]);
4098 if (!sv)
4099 return 1;
4100
4101 srv_adm_set_maint(sv);
4102 return 1;
4103}
4104
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004105/* parse a "enable agent" command. It always returns 1. */
4106static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4107{
4108 struct server *sv;
4109
4110 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4111 return 1;
4112
4113 sv = cli_find_server(appctx, args[2]);
4114 if (!sv)
4115 return 1;
4116
4117 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4118 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004119 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004120 return 1;
4121 }
4122
4123 sv->agent.state |= CHK_ST_ENABLED;
4124 return 1;
4125}
4126
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004127/* parse a "enable health" command. It always returns 1. */
4128static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4129{
4130 struct server *sv;
4131
4132 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4133 return 1;
4134
4135 sv = cli_find_server(appctx, args[2]);
4136 if (!sv)
4137 return 1;
4138
4139 sv->check.state |= CHK_ST_ENABLED;
4140 return 1;
4141}
4142
Willy Tarreauffb4d582016-11-24 12:47:00 +01004143/* parse a "enable server" command. It always returns 1. */
4144static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4145{
4146 struct server *sv;
4147
4148 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4149 return 1;
4150
4151 sv = cli_find_server(appctx, args[2]);
4152 if (!sv)
4153 return 1;
4154
4155 srv_adm_set_ready(sv);
4156 return 1;
4157}
4158
William Lallemand222baf22016-11-19 02:00:33 +01004159/* register cli keywords */
4160static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004161 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004162 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004163 { { "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 +01004164 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004165 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004166 { { "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 +01004167 { { "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 +01004168 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004169 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4170 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4171
William Lallemand222baf22016-11-19 02:00:33 +01004172 {{},}
4173}};
4174
4175__attribute__((constructor))
4176static void __server_init(void)
4177{
4178 cli_register_kw(&cli_kws);
4179}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004180
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004181/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004182 * Local variables:
4183 * c-indent-level: 8
4184 * c-basic-offset: 8
4185 * End:
4186 */