blob: 8315097ac4dd0e8d23893de3d5ccb8881662575f [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>
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +010016#include <netinet/tcp.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020017
Olivier Houchard4e694042017-03-14 20:01:29 +010018#include <import/xxhash.h>
19
Willy Tarreau272adea2014-03-31 10:39:59 +020020#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020021#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020022#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010029#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020030#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020036#include <proto/port_range.h>
37#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020038#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010039#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010042#include <proto/stream_interface.h>
43#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020044#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020045#include <proto/dns.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 */
138 for (p = proxy; p != NULL; p = p->next)
139 for (tmpserv = proxy->srv; tmpserv != NULL;
140 tmpserv = tmpserv->next) {
141 if (tmpserv == s)
142 continue;
143 if (tmpserv->cookie &&
144 strcmp(tmpserv->cookie, s->cookie) == 0) {
145 Warning("We generated two equal cookies for two different servers.\n"
146 "Please change the secret key for '%s'.\n",
147 s->proxy->id);
148 }
149 }
150}
151
Willy Tarreau21faa912012-10-10 08:27:36 +0200152/*
153 * Registers the server keyword list <kwl> as a list of valid keywords for next
154 * parsing sessions.
155 */
156void srv_register_keywords(struct srv_kw_list *kwl)
157{
158 LIST_ADDQ(&srv_keywords.list, &kwl->list);
159}
160
161/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
162 * keyword is found with a NULL ->parse() function, then an attempt is made to
163 * find one with a valid ->parse() function. This way it is possible to declare
164 * platform-dependant, known keywords as NULL, then only declare them as valid
165 * if some options are met. Note that if the requested keyword contains an
166 * opening parenthesis, everything from this point is ignored.
167 */
168struct srv_kw *srv_find_kw(const char *kw)
169{
170 int index;
171 const char *kwend;
172 struct srv_kw_list *kwl;
173 struct srv_kw *ret = NULL;
174
175 kwend = strchr(kw, '(');
176 if (!kwend)
177 kwend = kw + strlen(kw);
178
179 list_for_each_entry(kwl, &srv_keywords.list, list) {
180 for (index = 0; kwl->kw[index].kw != NULL; index++) {
181 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
182 kwl->kw[index].kw[kwend-kw] == 0) {
183 if (kwl->kw[index].parse)
184 return &kwl->kw[index]; /* found it !*/
185 else
186 ret = &kwl->kw[index]; /* may be OK */
187 }
188 }
189 }
190 return ret;
191}
192
193/* Dumps all registered "server" keywords to the <out> string pointer. The
194 * unsupported keywords are only dumped if their supported form was not
195 * found.
196 */
197void srv_dump_kws(char **out)
198{
199 struct srv_kw_list *kwl;
200 int index;
201
202 *out = NULL;
203 list_for_each_entry(kwl, &srv_keywords.list, list) {
204 for (index = 0; kwl->kw[index].kw != NULL; index++) {
205 if (kwl->kw[index].parse ||
206 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
207 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
208 kwl->scope,
209 kwl->kw[index].kw,
210 kwl->kw[index].skip ? " <arg>" : "",
211 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
212 kwl->kw[index].parse ? "" : " (not supported)");
213 }
214 }
215 }
216}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100217
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100218/* Parse the "addr" server keyword */
219static int srv_parse_addr(char **args, int *cur_arg,
220 struct proxy *curproxy, struct server *newsrv, char **err)
221{
222 char *errmsg, *arg;
223 struct sockaddr_storage *sk;
224 int port1, port2;
225 struct protocol *proto;
226
227 errmsg = NULL;
228 arg = args[*cur_arg + 1];
229
230 if (!*arg) {
231 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
232 goto err;
233 }
234
235 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
236 if (!sk) {
237 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
238 goto err;
239 }
240
241 proto = protocol_by_family(sk->ss_family);
242 if (!proto || !proto->connect) {
243 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
244 args[*cur_arg], arg);
245 goto err;
246 }
247
248 if (port1 != port2) {
249 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
250 args[*cur_arg], arg);
251 goto err;
252 }
253
254 newsrv->check.addr = newsrv->agent.addr = *sk;
255 newsrv->flags |= SRV_F_CHECKADDR;
256 newsrv->flags |= SRV_F_AGENTADDR;
257
258 return 0;
259
260 err:
261 free(errmsg);
262 return ERR_ALERT | ERR_FATAL;
263}
264
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100265/* Parse the "backup" server keyword */
266static int srv_parse_backup(char **args, int *cur_arg,
267 struct proxy *curproxy, struct server *newsrv, char **err)
268{
269 newsrv->flags |= SRV_F_BACKUP;
270 return 0;
271}
272
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100273/* Parse the "check" server keyword */
274static int srv_parse_check(char **args, int *cur_arg,
275 struct proxy *curproxy, struct server *newsrv, char **err)
276{
277 newsrv->do_check = 1;
278 return 0;
279}
280
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100281/* Parse the "check-send-proxy" server keyword */
282static int srv_parse_check_send_proxy(char **args, int *cur_arg,
283 struct proxy *curproxy, struct server *newsrv, char **err)
284{
285 newsrv->check.send_proxy = 1;
286 return 0;
287}
288
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100289/* Parse the "cookie" server keyword */
290static int srv_parse_cookie(char **args, int *cur_arg,
291 struct proxy *curproxy, struct server *newsrv, char **err)
292{
293 char *arg;
294
295 arg = args[*cur_arg + 1];
296 if (!*arg) {
297 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
298 return ERR_ALERT | ERR_FATAL;
299 }
300
301 free(newsrv->cookie);
302 newsrv->cookie = strdup(arg);
303 newsrv->cklen = strlen(arg);
304 newsrv->flags |= SRV_F_COOKIESET;
305 return 0;
306}
307
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100308/* Parse the "disabled" server keyword */
309static int srv_parse_disabled(char **args, int *cur_arg,
310 struct proxy *curproxy, struct server *newsrv, char **err)
311{
312 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
313 newsrv->state = SRV_ST_STOPPED;
314 newsrv->check.state |= CHK_ST_PAUSED;
315 newsrv->check.health = 0;
316 return 0;
317}
318
319/* Parse the "enabled" server keyword */
320static int srv_parse_enabled(char **args, int *cur_arg,
321 struct proxy *curproxy, struct server *newsrv, char **err)
322{
323 newsrv->admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
324 newsrv->state = SRV_ST_RUNNING;
325 newsrv->check.state &= ~CHK_ST_PAUSED;
326 newsrv->check.health = newsrv->check.rise;
327 return 0;
328}
329
Willy Tarreaudff55432012-10-10 17:51:05 +0200330/* parse the "id" server keyword */
331static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
332{
333 struct eb32_node *node;
334
335 if (!*args[*cur_arg + 1]) {
336 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
337 return ERR_ALERT | ERR_FATAL;
338 }
339
340 newsrv->puid = atol(args[*cur_arg + 1]);
341 newsrv->conf.id.key = newsrv->puid;
342
343 if (newsrv->puid <= 0) {
344 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
345 return ERR_ALERT | ERR_FATAL;
346 }
347
348 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
349 if (node) {
350 struct server *target = container_of(node, struct server, conf.id);
351 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
352 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
353 target->id);
354 return ERR_ALERT | ERR_FATAL;
355 }
356
357 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200358 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200359 return 0;
360}
361
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100362/* Parse the "namespace" server keyword */
363static int srv_parse_namespace(char **args, int *cur_arg,
364 struct proxy *curproxy, struct server *newsrv, char **err)
365{
366#ifdef CONFIG_HAP_NS
367 char *arg;
368
369 arg = args[*cur_arg + 1];
370 if (!*arg) {
371 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
372 return ERR_ALERT | ERR_FATAL;
373 }
374
375 if (!strcmp(arg, "*")) {
376 /* Use the namespace associated with the connection (if present). */
377 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
378 return 0;
379 }
380
381 /*
382 * As this parser may be called several times for the same 'default-server'
383 * object, or for a new 'server' instance deriving from a 'default-server'
384 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
385 */
386 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
387
388 newsrv->netns = netns_store_lookup(arg, strlen(arg));
389 if (!newsrv->netns)
390 newsrv->netns = netns_store_insert(arg);
391
392 if (!newsrv->netns) {
393 memprintf(err, "Cannot open namespace '%s'", arg);
394 return ERR_ALERT | ERR_FATAL;
395 }
396
397 return 0;
398#else
399 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
400 return ERR_ALERT | ERR_FATAL;
401#endif
402}
403
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100404/* Parse the "no-backup" server keyword */
405static int srv_parse_no_backup(char **args, int *cur_arg,
406 struct proxy *curproxy, struct server *newsrv, char **err)
407{
408 newsrv->flags &= ~SRV_F_BACKUP;
409 return 0;
410}
411
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100412/* Parse the "no-check" server keyword */
413static int srv_parse_no_check(char **args, int *cur_arg,
414 struct proxy *curproxy, struct server *newsrv, char **err)
415{
416 free_check(&newsrv->check);
417 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
418 newsrv->do_check = 0;
419 return 0;
420}
421
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100422/* Parse the "no-check-send-proxy" server keyword */
423static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
424 struct proxy *curproxy, struct server *newsrv, char **err)
425{
426 newsrv->check.send_proxy = 0;
427 return 0;
428}
429
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100430/* Disable server PROXY protocol flags. */
431static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
432{
433 srv->pp_opts &= ~flags;
434 return 0;
435}
436
437/* Parse the "no-send-proxy" server keyword */
438static int srv_parse_no_send_proxy(char **args, int *cur_arg,
439 struct proxy *curproxy, struct server *newsrv, char **err)
440{
441 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
442}
443
444/* Parse the "no-send-proxy-v2" server keyword */
445static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
446 struct proxy *curproxy, struct server *newsrv, char **err)
447{
448 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
449}
450
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100451/* Parse the "non-stick" server keyword */
452static int srv_parse_non_stick(char **args, int *cur_arg,
453 struct proxy *curproxy, struct server *newsrv, char **err)
454{
455 newsrv->flags |= SRV_F_NON_STICK;
456 return 0;
457}
458
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100459/* Enable server PROXY protocol flags. */
460static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
461{
462 srv->pp_opts |= flags;
463 return 0;
464}
465
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100466/* Parse the "observe" server keyword */
467static int srv_parse_observe(char **args, int *cur_arg,
468 struct proxy *curproxy, struct server *newsrv, char **err)
469{
470 char *arg;
471
472 arg = args[*cur_arg + 1];
473 if (!*arg) {
474 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
475 return ERR_ALERT | ERR_FATAL;
476 }
477
478 if (!strcmp(arg, "none")) {
479 newsrv->observe = HANA_OBS_NONE;
480 }
481 else if (!strcmp(arg, "layer4")) {
482 newsrv->observe = HANA_OBS_LAYER4;
483 }
484 else if (!strcmp(arg, "layer7")) {
485 if (curproxy->mode != PR_MODE_HTTP) {
486 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
487 return ERR_ALERT;
488 }
489 newsrv->observe = HANA_OBS_LAYER7;
490 }
491 else {
492 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
493 "but got '%s'\n", args[*cur_arg], arg);
494 return ERR_ALERT | ERR_FATAL;
495 }
496
497 return 0;
498}
499
Frédéric Lécaille16186232017-03-14 16:42:49 +0100500/* Parse the "redir" server keyword */
501static int srv_parse_redir(char **args, int *cur_arg,
502 struct proxy *curproxy, struct server *newsrv, char **err)
503{
504 char *arg;
505
506 arg = args[*cur_arg + 1];
507 if (!*arg) {
508 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
509 return ERR_ALERT | ERR_FATAL;
510 }
511
512 free(newsrv->rdr_pfx);
513 newsrv->rdr_pfx = strdup(arg);
514 newsrv->rdr_len = strlen(arg);
515
516 return 0;
517}
518
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100519/* Parse the "send-proxy" server keyword */
520static int srv_parse_send_proxy(char **args, int *cur_arg,
521 struct proxy *curproxy, struct server *newsrv, char **err)
522{
523 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
524}
525
526/* Parse the "send-proxy-v2" server keyword */
527static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
528 struct proxy *curproxy, struct server *newsrv, char **err)
529{
530 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
531}
532
Frédéric Lécailledba97072017-03-17 15:33:50 +0100533
534/* Parse the "source" server keyword */
535static int srv_parse_source(char **args, int *cur_arg,
536 struct proxy *curproxy, struct server *newsrv, char **err)
537{
538 char *errmsg;
539 int port_low, port_high;
540 struct sockaddr_storage *sk;
541 struct protocol *proto;
542
543 errmsg = NULL;
544
545 if (!*args[*cur_arg + 1]) {
546 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
547 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
548 goto err;
549 }
550
551 /* 'sk' is statically allocated (no need to be freed). */
552 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
553 if (!sk) {
554 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
555 goto err;
556 }
557
558 proto = protocol_by_family(sk->ss_family);
559 if (!proto || !proto->connect) {
560 Alert("'%s %s' : connect() not supported for this address family.\n",
561 args[*cur_arg], args[*cur_arg + 1]);
562 goto err;
563 }
564
565 newsrv->conn_src.opts |= CO_SRC_BIND;
566 newsrv->conn_src.source_addr = *sk;
567
568 if (port_low != port_high) {
569 int i;
570
571 if (!port_low || !port_high) {
572 Alert("'%s' does not support port offsets (found '%s').\n",
573 args[*cur_arg], args[*cur_arg + 1]);
574 goto err;
575 }
576
577 if (port_low <= 0 || port_low > 65535 ||
578 port_high <= 0 || port_high > 65535 ||
579 port_low > port_high) {
580 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
581 goto err;
582 }
583 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
584 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
585 newsrv->conn_src.sport_range->ports[i] = port_low + i;
586 }
587
588 *cur_arg += 2;
589 while (*(args[*cur_arg])) {
590 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
591#if defined(CONFIG_HAP_TRANSPARENT)
592 if (!*args[*cur_arg + 1]) {
593 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
594 "or 'hdr_ip(name,#)' as argument.\n");
595 goto err;
596 }
597 if (!strcmp(args[*cur_arg + 1], "client")) {
598 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
599 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
600 }
601 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
602 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
603 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
604 }
605 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
606 char *name, *end;
607
608 name = args[*cur_arg + 1] + 7;
609 while (isspace(*name))
610 name++;
611
612 end = name;
613 while (*end && !isspace(*end) && *end != ',' && *end != ')')
614 end++;
615
616 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
617 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
618 free(newsrv->conn_src.bind_hdr_name);
619 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
620 newsrv->conn_src.bind_hdr_len = end - name;
621 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
622 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
623 newsrv->conn_src.bind_hdr_occ = -1;
624
625 /* now look for an occurrence number */
626 while (isspace(*end))
627 end++;
628 if (*end == ',') {
629 end++;
630 name = end;
631 if (*end == '-')
632 end++;
633 while (isdigit((int)*end))
634 end++;
635 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
636 }
637
638 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
639 Alert("usesrc hdr_ip(name,num) does not support negative"
640 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
641 goto err;
642 }
643 }
644 else {
645 struct sockaddr_storage *sk;
646 int port1, port2;
647
648 /* 'sk' is statically allocated (no need to be freed). */
649 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
650 if (!sk) {
651 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
652 goto err;
653 }
654
655 proto = protocol_by_family(sk->ss_family);
656 if (!proto || !proto->connect) {
657 Alert("'%s %s' : connect() not supported for this address family.\n",
658 args[*cur_arg], args[*cur_arg + 1]);
659 goto err;
660 }
661
662 if (port1 != port2) {
663 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
664 args[*cur_arg], args[*cur_arg + 1]);
665 goto err;
666 }
667 newsrv->conn_src.tproxy_addr = *sk;
668 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
669 }
670 global.last_checks |= LSTCHK_NETADM;
671 *cur_arg += 2;
672 continue;
673#else /* no TPROXY support */
674 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
675 goto err;
676#endif /* defined(CONFIG_HAP_TRANSPARENT) */
677 } /* "usesrc" */
678
679 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
680#ifdef SO_BINDTODEVICE
681 if (!*args[*cur_arg + 1]) {
682 Alert("'%s' : missing interface name.\n", args[0]);
683 goto err;
684 }
685 free(newsrv->conn_src.iface_name);
686 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
687 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
688 global.last_checks |= LSTCHK_NETADM;
689#else
690 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
691 goto err;
692#endif
693 *cur_arg += 2;
694 continue;
695 }
696 /* this keyword in not an option of "source" */
697 break;
698 } /* while */
699
700 return 0;
701
702 err:
703 free(errmsg);
704 return ERR_ALERT | ERR_FATAL;
705}
706
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100707/* Parse the "stick" server keyword */
708static int srv_parse_stick(char **args, int *cur_arg,
709 struct proxy *curproxy, struct server *newsrv, char **err)
710{
711 newsrv->flags &= ~SRV_F_NON_STICK;
712 return 0;
713}
714
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100715/* Parse the "track" server keyword */
716static int srv_parse_track(char **args, int *cur_arg,
717 struct proxy *curproxy, struct server *newsrv, char **err)
718{
719 char *arg;
720
721 arg = args[*cur_arg + 1];
722 if (!*arg) {
723 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
724 return ERR_ALERT | ERR_FATAL;
725 }
726
727 free(newsrv->trackit);
728 newsrv->trackit = strdup(arg);
729
730 return 0;
731}
732
Frédéric Lécailledba97072017-03-17 15:33:50 +0100733
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200734/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200735 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200736 * shutdown.
737 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200738void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200739{
Willy Tarreau87b09662015-04-03 00:22:06 +0200740 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200741
Willy Tarreau87b09662015-04-03 00:22:06 +0200742 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
743 if (stream->srv_conn == srv)
744 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200745}
746
747/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200748 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200749 * the reason for the shutdown.
750 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200751void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200752{
753 struct server *srv;
754
755 for (srv = px->srv; srv != NULL; srv = srv->next)
756 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200757 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200758}
759
Willy Tarreaubda92272014-05-20 21:55:30 +0200760/* Appends some information to a message string related to a server going UP or
761 * DOWN. If both <forced> and <reason> are null and the server tracks another
762 * one, a "via" information will be provided to know where the status came from.
763 * If <reason> is non-null, the entire string will be appended after a comma and
764 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200765 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200766 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200767 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200768void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200769{
Willy Tarreaubda92272014-05-20 21:55:30 +0200770 if (reason)
771 chunk_appendf(msg, ", %s", reason);
772 else if (!forced && s->track)
773 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200774
775 if (xferred >= 0) {
776 if (s->state == SRV_ST_STOPPED)
777 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
778 " %d sessions active, %d requeued, %d remaining in queue",
779 s->proxy->srv_act, s->proxy->srv_bck,
780 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
781 s->cur_sess, xferred, s->nbpend);
782 else
783 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
784 " %d sessions requeued, %d total in queue",
785 s->proxy->srv_act, s->proxy->srv_bck,
786 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
787 xferred, s->nbpend);
788 }
789}
790
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200791/* Marks server <s> down, regardless of its checks' statuses, notifies by all
792 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200793 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200794 * recomputes the number of servers, but not the map. Maintenance servers are
795 * ignored. It reports <reason> if non-null as the reason for going down. Note
796 * that it makes use of the trash to build the log strings, so <reason> must
797 * not be placed there.
798 */
799void srv_set_stopped(struct server *s, const char *reason)
800{
801 struct server *srv;
802 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
803 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900804 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200805 int xferred;
806
807 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
808 return;
809
810 s->last_change = now.tv_sec;
811 s->state = SRV_ST_STOPPED;
812 if (s->proxy->lbprm.set_server_status_down)
813 s->proxy->lbprm.set_server_status_down(s);
814
815 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200816 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200817
Willy Tarreau87b09662015-04-03 00:22:06 +0200818 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200819 * a connection. Those which are redispatchable will be queued
820 * to another server or to the proxy itself.
821 */
822 xferred = pendconn_redistribute(s);
823
824 chunk_printf(&trash,
825 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
826 s->proxy->id, s->id);
827
828 srv_append_status(&trash, s, reason, xferred, 0);
829 Warning("%s.\n", trash.str);
830
831 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900832 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
833 send_log(s->proxy, log_level, "%s.\n", trash.str);
834 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200835
836 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
837 set_backend_down(s->proxy);
838
839 s->counters.down_trans++;
840
841 for (srv = s->trackers; srv; srv = srv->tracknext)
842 srv_set_stopped(srv, NULL);
843}
844
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200845/* Marks server <s> up regardless of its checks' statuses and provided it isn't
846 * in maintenance. Notifies by all available means, recounts the remaining
847 * servers on the proxy and tries to grab requests from the proxy. It
848 * automatically recomputes the number of servers, but not the map. Maintenance
849 * servers are ignored. It reports <reason> if non-null as the reason for going
850 * up. Note that it makes use of the trash to build the log strings, so <reason>
851 * must not be placed there.
852 */
853void srv_set_running(struct server *s, const char *reason)
854{
855 struct server *srv;
856 int xferred;
857
858 if (s->admin & SRV_ADMF_MAINT)
859 return;
860
861 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
862 return;
863
864 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
865 if (s->proxy->last_change < now.tv_sec) // ignore negative times
866 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
867 s->proxy->last_change = now.tv_sec;
868 }
869
870 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
871 s->down_time += now.tv_sec - s->last_change;
872
873 s->last_change = now.tv_sec;
874
875 s->state = SRV_ST_STARTING;
876 if (s->slowstart > 0)
877 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
878 else
879 s->state = SRV_ST_RUNNING;
880
881 server_recalc_eweight(s);
882
883 /* If the server is set with "on-marked-up shutdown-backup-sessions",
884 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200885 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200886 * on all backup servers.
887 */
888 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
889 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200890 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200891
892 /* check if we can handle some connections queued at the proxy. We
893 * will take as many as we can handle.
894 */
895 xferred = pendconn_grab_from_px(s);
896
897 chunk_printf(&trash,
898 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
899 s->proxy->id, s->id);
900
901 srv_append_status(&trash, s, reason, xferred, 0);
902 Warning("%s.\n", trash.str);
903 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900904 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200905
906 for (srv = s->trackers; srv; srv = srv->tracknext)
907 srv_set_running(srv, NULL);
908}
909
Willy Tarreau8eb77842014-05-21 13:54:57 +0200910/* Marks server <s> stopping regardless of its checks' statuses and provided it
911 * isn't in maintenance. Notifies by all available means, recounts the remaining
912 * servers on the proxy and tries to grab requests from the proxy. It
913 * automatically recomputes the number of servers, but not the map. Maintenance
914 * servers are ignored. It reports <reason> if non-null as the reason for going
915 * up. Note that it makes use of the trash to build the log strings, so <reason>
916 * must not be placed there.
917 */
918void srv_set_stopping(struct server *s, const char *reason)
919{
920 struct server *srv;
921 int xferred;
922
923 if (s->admin & SRV_ADMF_MAINT)
924 return;
925
926 if (s->state == SRV_ST_STOPPING)
927 return;
928
929 s->last_change = now.tv_sec;
930 s->state = SRV_ST_STOPPING;
931 if (s->proxy->lbprm.set_server_status_down)
932 s->proxy->lbprm.set_server_status_down(s);
933
Willy Tarreau87b09662015-04-03 00:22:06 +0200934 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200935 * a connection. Those which are redispatchable will be queued
936 * to another server or to the proxy itself.
937 */
938 xferred = pendconn_redistribute(s);
939
940 chunk_printf(&trash,
941 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
942 s->proxy->id, s->id);
943
944 srv_append_status(&trash, s, reason, xferred, 0);
945
946 Warning("%s.\n", trash.str);
947 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
948
949 if (!s->proxy->srv_bck && !s->proxy->srv_act)
950 set_backend_down(s->proxy);
951
952 for (srv = s->trackers; srv; srv = srv->tracknext)
953 srv_set_stopping(srv, NULL);
954}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200955
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200956/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
957 * enforce either maint mode or drain mode. It is not allowed to set more than
958 * one flag at once. The equivalent "inherited" flag is propagated to all
959 * tracking servers. Maintenance mode disables health checks (but not agent
960 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100961 * is done. If <cause> is non-null, it will be displayed at the end of the log
962 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200963 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100964void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200965{
966 struct check *check = &s->check;
967 struct server *srv;
968 int xferred;
969
970 if (!mode)
971 return;
972
973 /* stop going down as soon as we meet a server already in the same state */
974 if (s->admin & mode)
975 return;
976
977 s->admin |= mode;
978
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200979 /* stop going down if the equivalent flag was already present (forced or inherited) */
980 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
981 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
982 return;
983
984 /* Maintenance must also disable health checks */
985 if (mode & SRV_ADMF_MAINT) {
986 if (s->check.state & CHK_ST_ENABLED) {
987 s->check.state |= CHK_ST_PAUSED;
988 check->health = 0;
989 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200990
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200991 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200992 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100993 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
994 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
995 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200996
Willy Tarreaubda92272014-05-20 21:55:30 +0200997 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200998
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100999 if (!(global.mode & MODE_STARTING)) {
1000 Warning("%s.\n", trash.str);
1001 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1002 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001003 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001004 else { /* server was still running */
1005 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
1006 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1007
1008 check->health = 0; /* failure */
1009 s->last_change = now.tv_sec;
1010 s->state = SRV_ST_STOPPED;
1011 if (s->proxy->lbprm.set_server_status_down)
1012 s->proxy->lbprm.set_server_status_down(s);
1013
1014 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001015 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001016
Willy Tarreau87b09662015-04-03 00:22:06 +02001017 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001018 * a connection. Those which are redispatchable will be queued
1019 * to another server or to the proxy itself.
1020 */
1021 xferred = pendconn_redistribute(s);
1022
1023 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001024 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001025 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001026 s->proxy->id, s->id,
1027 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001028
1029 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1030
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001031 if (!(global.mode & MODE_STARTING)) {
1032 Warning("%s.\n", trash.str);
1033 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1034 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001035
1036 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1037 set_backend_down(s->proxy);
1038
1039 s->counters.down_trans++;
1040 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001041 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001042
1043 /* drain state is applied only if not yet in maint */
1044 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001045 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1046
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001047 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001048 if (s->proxy->lbprm.set_server_status_down)
1049 s->proxy->lbprm.set_server_status_down(s);
1050
Willy Tarreau87b09662015-04-03 00:22:06 +02001051 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001052 * a connection. Those which are redispatchable will be queued
1053 * to another server or to the proxy itself.
1054 */
1055 xferred = pendconn_redistribute(s);
1056
Willy Tarreau8b428482016-11-07 15:53:43 +01001057 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1058 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1059 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001060
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001061 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001062
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001063 if (!(global.mode & MODE_STARTING)) {
1064 Warning("%s.\n", trash.str);
1065 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1066 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1067 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1069 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001070 }
1071
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001072 /* compute the inherited flag to propagate */
1073 if (mode & SRV_ADMF_MAINT)
1074 mode = SRV_ADMF_IMAINT;
1075 else if (mode & SRV_ADMF_DRAIN)
1076 mode = SRV_ADMF_IDRAIN;
1077
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001078 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001079 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001080}
1081
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001082/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1083 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1084 * than one flag at once. The equivalent "inherited" flag is propagated to all
1085 * tracking servers. Leaving maintenance mode re-enables health checks. When
1086 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001087 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001088void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001089{
1090 struct check *check = &s->check;
1091 struct server *srv;
1092 int xferred = -1;
1093
1094 if (!mode)
1095 return;
1096
1097 /* stop going down as soon as we see the flag is not there anymore */
1098 if (!(s->admin & mode))
1099 return;
1100
1101 s->admin &= ~mode;
1102
1103 if (s->admin & SRV_ADMF_MAINT) {
1104 /* remaining in maintenance mode, let's inform precisely about the
1105 * situation.
1106 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001107 if (mode & SRV_ADMF_FMAINT) {
1108 chunk_printf(&trash,
1109 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1110 s->flags & SRV_F_BACKUP ? "Backup " : "",
1111 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001112
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001113 if (s->track) /* normally it's mandatory here */
1114 chunk_appendf(&trash, " via %s/%s",
1115 s->track->proxy->id, s->track->id);
1116 Warning("%s.\n", trash.str);
1117 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1118 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001119 if (mode & SRV_ADMF_RMAINT) {
1120 chunk_printf(&trash,
1121 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1122 s->flags & SRV_F_BACKUP ? "Backup " : "",
1123 s->proxy->id, s->id, s->hostname);
1124
1125 if (s->track) /* normally it's mandatory here */
1126 chunk_appendf(&trash, " via %s/%s",
1127 s->track->proxy->id, s->track->id);
1128 Warning("%s.\n", trash.str);
1129 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1130 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001131 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132 chunk_printf(&trash,
1133 "%sServer %s/%s remains in forced maintenance",
1134 s->flags & SRV_F_BACKUP ? "Backup " : "",
1135 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001136 Warning("%s.\n", trash.str);
1137 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1138 }
1139 /* don't report anything when leaving drain mode and remaining in maintenance */
1140 }
1141 else if (mode & SRV_ADMF_MAINT) {
1142 /* OK here we're leaving maintenance, we have many things to check,
1143 * because the server might possibly be coming back up depending on
1144 * its state. In practice, leaving maintenance means that we should
1145 * immediately turn to UP (more or less the slowstart) under the
1146 * following conditions :
1147 * - server is neither checked nor tracked
1148 * - server tracks another server which is not checked
1149 * - server tracks another server which is already up
1150 * Which sums up as something simpler :
1151 * "either the tracking server is up or the server's checks are disabled
1152 * or up". Otherwise we only re-enable health checks. There's a special
1153 * case associated to the stopping state which can be inherited. Note
1154 * that the server might still be in drain mode, which is naturally dealt
1155 * with by the lower level functions.
1156 */
1157
1158 if (s->check.state & CHK_ST_ENABLED) {
1159 s->check.state &= ~CHK_ST_PAUSED;
1160 check->health = check->rise; /* start OK but check immediately */
1161 }
1162
1163 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
1164 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1165 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
1166 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1167 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1168 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1169 s->proxy->last_change = now.tv_sec;
1170 }
1171
1172 if (s->last_change < now.tv_sec) // ignore negative times
1173 s->down_time += now.tv_sec - s->last_change;
1174 s->last_change = now.tv_sec;
1175
1176 if (s->track && s->track->state == SRV_ST_STOPPING)
1177 s->state = SRV_ST_STOPPING;
1178 else {
1179 s->state = SRV_ST_STARTING;
1180 if (s->slowstart > 0)
1181 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1182 else
1183 s->state = SRV_ST_RUNNING;
1184 }
1185
1186 server_recalc_eweight(s);
1187
1188 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1189 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +02001190 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001191 * on all backup servers.
1192 */
1193 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1194 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001195 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001196
1197 /* check if we can handle some connections queued at the proxy. We
1198 * will take as many as we can handle.
1199 */
1200 xferred = pendconn_grab_from_px(s);
1201 }
1202
1203 if (mode & SRV_ADMF_FMAINT) {
1204 chunk_printf(&trash,
1205 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1206 s->flags & SRV_F_BACKUP ? "Backup " : "",
1207 s->proxy->id, s->id,
1208 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1209 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001210 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001211 else if (mode & SRV_ADMF_RMAINT) {
1212 chunk_printf(&trash,
1213 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1214 s->flags & SRV_F_BACKUP ? "Backup " : "",
1215 s->proxy->id, s->id, s->hostname,
1216 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1217 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1218 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001219 else {
1220 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001221 "%sServer %s/%s is %s/%s (leaving maintenance)",
1222 s->flags & SRV_F_BACKUP ? "Backup " : "",
1223 s->proxy->id, s->id,
1224 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1225 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1226 srv_append_status(&trash, s, NULL, xferred, 0);
1227 }
1228 Warning("%s.\n", trash.str);
1229 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1230 }
1231 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
1232 /* remaining in drain mode after removing one of its flags */
1233
1234 if (mode & SRV_ADMF_FDRAIN) {
1235 chunk_printf(&trash,
1236 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001237 s->flags & SRV_F_BACKUP ? "Backup " : "",
1238 s->proxy->id, s->id);
1239
1240 if (s->track) /* normally it's mandatory here */
1241 chunk_appendf(&trash, " via %s/%s",
1242 s->track->proxy->id, s->track->id);
1243 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001244 else {
1245 chunk_printf(&trash,
1246 "%sServer %s/%s remains in forced drain mode",
1247 s->flags & SRV_F_BACKUP ? "Backup " : "",
1248 s->proxy->id, s->id);
1249 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001250 Warning("%s.\n", trash.str);
1251 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001252 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001253 else if (mode & SRV_ADMF_DRAIN) {
1254 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001255 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1256 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1257 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1258 s->proxy->last_change = now.tv_sec;
1259 }
1260
1261 if (s->last_change < now.tv_sec) // ignore negative times
1262 s->down_time += now.tv_sec - s->last_change;
1263 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001264 server_recalc_eweight(s);
1265
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001266 if (mode & SRV_ADMF_FDRAIN) {
1267 chunk_printf(&trash,
1268 "%sServer %s/%s is %s (leaving forced drain)",
1269 s->flags & SRV_F_BACKUP ? "Backup " : "",
1270 s->proxy->id, s->id,
1271 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1272 }
1273 else {
1274 chunk_printf(&trash,
1275 "%sServer %s/%s is %s (leaving drain)",
1276 s->flags & SRV_F_BACKUP ? "Backup " : "",
1277 s->proxy->id, s->id,
1278 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1279 if (s->track) /* normally it's mandatory here */
1280 chunk_appendf(&trash, " via %s/%s",
1281 s->track->proxy->id, s->track->id);
1282 }
1283 Warning("%s.\n", trash.str);
1284 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001285 }
1286
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001287 /* stop going down if the equivalent flag is still present (forced or inherited) */
1288 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
1289 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
1290 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001291
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001292 if (mode & SRV_ADMF_MAINT)
1293 mode = SRV_ADMF_IMAINT;
1294 else if (mode & SRV_ADMF_DRAIN)
1295 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001296
1297 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001298 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001299}
1300
Willy Tarreau757478e2016-11-03 19:22:19 +01001301/* principle: propagate maint and drain to tracking servers. This is useful
1302 * upon startup so that inherited states are correct.
1303 */
1304static void srv_propagate_admin_state(struct server *srv)
1305{
1306 struct server *srv2;
1307
1308 if (!srv->trackers)
1309 return;
1310
1311 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
1312 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001313 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001314
1315 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001316 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001317 }
1318}
1319
1320/* Compute and propagate the admin states for all servers in proxy <px>.
1321 * Only servers *not* tracking another one are considered, because other
1322 * ones will be handled when the server they track is visited.
1323 */
1324void srv_compute_all_admin_states(struct proxy *px)
1325{
1326 struct server *srv;
1327
1328 for (srv = px->srv; srv; srv = srv->next) {
1329 if (srv->track)
1330 continue;
1331 srv_propagate_admin_state(srv);
1332 }
1333}
1334
Willy Tarreaudff55432012-10-10 17:51:05 +02001335/* Note: must not be declared <const> as its list will be overwritten.
1336 * Please take care of keeping this list alphabetically sorted, doing so helps
1337 * all code contributors.
1338 * Optional keywords are also declared with a NULL ->parse() function so that
1339 * the config parser can report an appropriate error when a known keyword was
1340 * not enabled.
1341 */
1342static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001343 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001344 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001345 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001346 { "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 +01001347 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001348 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1349 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001350 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001351 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001352 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001353 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001354 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* disable PROXY protol for health checks */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001355 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1356 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001357 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001358 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001359 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001360 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1361 { "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 +01001362 /*
1363 * Note: the following 'skip' field value is 0.
1364 * Here this does not mean that "source" setting does not need any argument.
1365 * This means that the number of argument is variable.
1366 */
1367 { "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 +01001368 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001369 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001370 { NULL, NULL, 0 },
1371}};
1372
1373__attribute__((constructor))
1374static void __listener_init(void)
1375{
1376 srv_register_keywords(&srv_kws);
1377}
1378
Willy Tarreau004e0452013-11-21 11:22:01 +01001379/* Recomputes the server's eweight based on its state, uweight, the current time,
1380 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1381 * state is automatically disabled if the time is elapsed.
1382 */
1383void server_recalc_eweight(struct server *sv)
1384{
1385 struct proxy *px = sv->proxy;
1386 unsigned w;
1387
1388 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1389 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +02001390 if (sv->state == SRV_ST_STARTING)
1391 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001392 }
1393
1394 /* We must take care of not pushing the server to full throttle during slow starts.
1395 * It must also start immediately, at least at the minimal step when leaving maintenance.
1396 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001397 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001398 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1399 else
1400 w = px->lbprm.wdiv;
1401
1402 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
1403
1404 /* now propagate the status change to any LB algorithms */
1405 if (px->lbprm.update_server_eweight)
1406 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +02001407 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001408 if (px->lbprm.set_server_status_up)
1409 px->lbprm.set_server_status_up(sv);
1410 }
1411 else {
1412 if (px->lbprm.set_server_status_down)
1413 px->lbprm.set_server_status_down(sv);
1414 }
1415}
1416
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001418 * Parses weight_str and configures sv accordingly.
1419 * Returns NULL on success, error message string otherwise.
1420 */
1421const char *server_parse_weight_change_request(struct server *sv,
1422 const char *weight_str)
1423{
1424 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001425 long int w;
1426 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001427
1428 px = sv->proxy;
1429
1430 /* if the weight is terminated with '%', it is set relative to
1431 * the initial weight, otherwise it is absolute.
1432 */
1433 if (!*weight_str)
1434 return "Require <weight> or <weight%>.\n";
1435
Simon Hormanb796afa2013-02-12 10:45:53 +09001436 w = strtol(weight_str, &end, 10);
1437 if (end == weight_str)
1438 return "Empty weight string empty or preceded by garbage";
1439 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001440 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001441 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001442 /* Avoid integer overflow */
1443 if (w > 25600)
1444 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001445 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001446 if (w > 256)
1447 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001448 }
1449 else if (w < 0 || w > 256)
1450 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001451 else if (end[0] != '\0')
1452 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001453
1454 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1455 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1456
1457 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001458 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001459
1460 return NULL;
1461}
1462
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001463/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001464 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1465 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001466 * Returns:
1467 * - error string on error
1468 * - NULL on success
1469 */
1470const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001471 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001472{
1473 unsigned char ip[INET6_ADDRSTRLEN];
1474
1475 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001476 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001477 return NULL;
1478 }
1479 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001480 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001481 return NULL;
1482 }
1483
1484 return "Could not understand IP address format.\n";
1485}
1486
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001487const char *server_parse_maxconn_change_request(struct server *sv,
1488 const char *maxconn_str)
1489{
1490 long int v;
1491 char *end;
1492
1493 if (!*maxconn_str)
1494 return "Require <maxconn>.\n";
1495
1496 v = strtol(maxconn_str, &end, 10);
1497 if (end == maxconn_str)
1498 return "maxconn string empty or preceded by garbage";
1499 else if (end[0] != '\0')
1500 return "Trailing garbage in maxconn string";
1501
1502 if (sv->maxconn == sv->minconn) { // static maxconn
1503 sv->maxconn = sv->minconn = v;
1504 } else { // dynamic maxconn
1505 sv->maxconn = v;
1506 }
1507
1508 if (may_dequeue_tasks(sv, sv->proxy))
1509 process_srv_queue(sv);
1510
1511 return NULL;
1512}
1513
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001514#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1515static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1516{
1517 int idx;
1518 struct sample_expr *expr;
1519 const char *args[] = {
1520 newsrv->sni_expr,
1521 NULL,
1522 };
1523
1524 idx = 0;
1525 proxy->conf.args.ctx = ARGC_SRV;
1526
1527 expr = sample_parse_expr((char **)args, &idx, px->conf.file, px->conf.line,
1528 err, &proxy->conf.args);
1529 if (!expr) {
1530 memprintf(err, "error detected while parsing sni expression : %s", *err);
1531 return ERR_ALERT | ERR_FATAL;
1532 }
1533
1534 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1535 memprintf(err, "error detected while parsing sni expression : "
1536 " fetch method '%s' extracts information from '%s', "
1537 "none of which is available here.\n",
1538 args[0], sample_src_names(expr->fetch->use));
1539 return ERR_ALERT | ERR_FATAL;
1540 }
1541
1542 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1543 release_sample_expr(newsrv->ssl_ctx.sni);
1544 newsrv->ssl_ctx.sni = expr;
1545
1546 return 0;
1547}
1548#endif
1549
1550static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1551{
1552 if (err && *err) {
1553 indent_msg(err, 2);
1554 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1555 }
1556 else
1557 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1558 file, linenum, args[0], args[1], args[cur_arg]);
1559}
1560
Willy Tarreau272adea2014-03-31 10:39:59 +02001561int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1562{
1563 struct server *newsrv = NULL;
1564 const char *err;
1565 char *errmsg = NULL;
1566 int err_code = 0;
1567 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001568 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001569
1570 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
1571 int cur_arg;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001572 int do_agent = 0, defsrv = (*args[0] == 'd');
Willy Tarreau272adea2014-03-31 10:39:59 +02001573
1574 if (!defsrv && curproxy == defproxy) {
1575 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1576 err_code |= ERR_ALERT | ERR_FATAL;
1577 goto out;
1578 }
1579 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1580 err_code |= ERR_ALERT | ERR_FATAL;
1581
Frédéric Lécaille8065b6d2017-03-09 14:01:02 +01001582 if (!defsrv && !*args[2]) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001583 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1584 file, linenum, args[0]);
1585 err_code |= ERR_ALERT | ERR_FATAL;
1586 goto out;
1587 }
1588
1589 err = invalid_char(args[1]);
1590 if (err && !defsrv) {
1591 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1592 file, linenum, *err, args[1]);
1593 err_code |= ERR_ALERT | ERR_FATAL;
1594 goto out;
1595 }
1596
1597 if (!defsrv) {
1598 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001599 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001600 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001601 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02001602
Vincent Bernat02779b62016-04-03 13:48:43 +02001603 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001604 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1605 err_code |= ERR_ALERT | ERR_ABORT;
1606 goto out;
1607 }
1608
1609 /* the servers are linked backwards first */
1610 newsrv->next = curproxy->srv;
1611 curproxy->srv = newsrv;
1612 newsrv->proxy = curproxy;
1613 newsrv->conf.file = strdup(file);
1614 newsrv->conf.line = linenum;
1615
1616 newsrv->obj_type = OBJ_TYPE_SERVER;
1617 LIST_INIT(&newsrv->actconns);
1618 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02001619 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02001620 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02001621 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +02001622 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +02001623 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +02001624 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +02001625 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +02001626 newsrv->last_change = now.tv_sec;
1627 newsrv->id = strdup(args[1]);
1628
1629 /* several ways to check the port component :
1630 * - IP => port=+0, relative (IPv4 only)
1631 * - IP: => port=+0, relative
1632 * - IP:N => port=N, absolute
1633 * - IP:+N => port=+N, relative
1634 * - IP:-N => port=-N, relative
1635 */
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001636 sk = str2sa_range(args[2], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001637 if (!sk) {
1638 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1639 err_code |= ERR_ALERT | ERR_FATAL;
1640 goto out;
1641 }
1642
1643 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001644 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001645 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1646 file, linenum, args[0], args[1]);
1647 err_code |= ERR_ALERT | ERR_FATAL;
1648 goto out;
1649 }
1650
1651 if (!port1 || !port2) {
1652 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001653 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001654 }
1655 else if (port1 != port2) {
1656 /* port range */
1657 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1658 file, linenum, args[0], args[1], args[2]);
1659 err_code |= ERR_ALERT | ERR_FATAL;
1660 goto out;
1661 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001662
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001663 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001664 newsrv->hostname = fqdn;
1665 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001666 goto skip_name_resolution;
1667
Willy Tarreau07101d52015-09-08 16:16:35 +02001668 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001669 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001670 goto skip_name_resolution;
1671
1672 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1673 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1674 goto skip_name_resolution;
1675 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1676 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1677 file, linenum, args[2]);
1678 err_code |= ERR_ALERT | ERR_FATAL;
1679 goto out;
1680 }
1681
1682 curr_resolution->requester = newsrv;
1683 curr_resolution->requester_cb = snr_resolution_cb;
1684 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1685 curr_resolution->status = RSLV_STATUS_NONE;
1686 curr_resolution->step = RSLV_STEP_NONE;
1687 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001688 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001689 newsrv->resolution = curr_resolution;
1690
1691 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001692 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001693 newsrv->svc_port = port;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001694 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = xprt_get(XPRT_RAW);
Willy Tarreau272adea2014-03-31 10:39:59 +02001695
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001696 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001697 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1698 file, linenum, newsrv->addr.ss_family, args[2]);
1699 err_code |= ERR_ALERT | ERR_FATAL;
1700 goto out;
1701 }
1702
Frédéric Lécailledba97072017-03-17 15:33:50 +01001703 /*
1704 * In this section we copy default-server connection source settings to
1705 * the new server object connection source
1706 * (newsrv->conn_src <- curproxy->defsrv.conn_src).
1707 */
1708 newsrv->conn_src.opts = curproxy->defsrv.conn_src.opts;
1709 newsrv->conn_src.source_addr = curproxy->defsrv.conn_src.source_addr;
1710 if (curproxy->defsrv.conn_src.sport_range != NULL) {
1711 int i, def_sport_range_sz;
1712 struct server *default_srv;
1713
1714 default_srv = &curproxy->defsrv;
1715 def_sport_range_sz = default_srv->conn_src.sport_range->size;
1716 if (def_sport_range_sz > 0) {
1717 newsrv->conn_src.sport_range = port_range_alloc_range(def_sport_range_sz);
1718 if (newsrv->conn_src.sport_range) {
1719 for (i = 0; i < def_sport_range_sz; i++)
1720 newsrv->conn_src.sport_range->ports[i] = default_srv->conn_src.sport_range->ports[i];
1721 }
1722 }
1723 }
1724 if (curproxy->defsrv.conn_src.bind_hdr_name != NULL) {
1725 newsrv->conn_src.bind_hdr_name = strdup(curproxy->defsrv.conn_src.bind_hdr_name);
1726 newsrv->conn_src.bind_hdr_len = strlen(curproxy->defsrv.conn_src.bind_hdr_name);
1727 }
1728 newsrv->conn_src.bind_hdr_occ = curproxy->defsrv.conn_src.bind_hdr_occ;
1729 newsrv->conn_src.tproxy_addr = curproxy->defsrv.conn_src.tproxy_addr;
1730 if (curproxy->defsrv.conn_src.iface_name != NULL)
1731 newsrv->conn_src.iface_name = strdup(curproxy->defsrv.conn_src.iface_name);
1732
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001733 newsrv->pp_opts = curproxy->defsrv.pp_opts;
Frédéric Lécaille16186232017-03-14 16:42:49 +01001734 if (curproxy->defsrv.rdr_pfx != NULL) {
1735 newsrv->rdr_pfx = strdup(curproxy->defsrv.rdr_pfx);
1736 newsrv->rdr_len = curproxy->defsrv.rdr_len;
1737 }
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001738 if (curproxy->defsrv.cookie != NULL) {
1739 newsrv->cookie = strdup(curproxy->defsrv.cookie);
1740 newsrv->cklen = curproxy->defsrv.cklen;
1741 }
Willy Tarreau141ad852016-12-22 18:38:00 +01001742 newsrv->use_ssl = curproxy->defsrv.use_ssl;
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001743 newsrv->check.addr = newsrv->agent.addr = curproxy->defsrv.check.addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001744 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1745 newsrv->check.port = curproxy->defsrv.check.port;
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +01001746 /* Note: 'flags' field has potentially been already initialized. */
1747 newsrv->flags |= curproxy->defsrv.flags;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001748 newsrv->do_check = curproxy->defsrv.do_check;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001749 if (newsrv->check.port)
1750 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001751 newsrv->check.inter = curproxy->defsrv.check.inter;
1752 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1753 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1754 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1755 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001756 if (curproxy->defsrv.agent.send_string != NULL)
1757 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1758 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001759 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1760 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1761 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1762 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1763 newsrv->minconn = curproxy->defsrv.minconn;
1764 newsrv->maxconn = curproxy->defsrv.maxconn;
1765 newsrv->slowstart = curproxy->defsrv.slowstart;
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001766 newsrv->observe = curproxy->defsrv.observe;
Willy Tarreau272adea2014-03-31 10:39:59 +02001767 newsrv->onerror = curproxy->defsrv.onerror;
1768 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1769 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001770 if (curproxy->defsrv.trackit != NULL)
1771 newsrv->trackit = strdup(curproxy->defsrv.trackit);
Willy Tarreau272adea2014-03-31 10:39:59 +02001772 newsrv->consecutive_errors_limit
1773 = curproxy->defsrv.consecutive_errors_limit;
Willy Tarreau272adea2014-03-31 10:39:59 +02001774 newsrv->uweight = newsrv->iweight
1775 = curproxy->defsrv.iweight;
1776
1777 newsrv->check.status = HCHK_STATUS_INI;
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001778 newsrv->check.send_proxy = curproxy->defsrv.check.send_proxy;
Willy Tarreau272adea2014-03-31 10:39:59 +02001779 newsrv->check.rise = curproxy->defsrv.check.rise;
1780 newsrv->check.fall = curproxy->defsrv.check.fall;
1781 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001782 /* Here we check if 'disabled' is the default server state */
1783 if (curproxy->defsrv.admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1784 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1785 newsrv->state = SRV_ST_STOPPED;
1786 newsrv->check.state |= CHK_ST_PAUSED;
1787 newsrv->check.health = 0;
1788 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001789 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001790 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001791
1792 newsrv->agent.status = HCHK_STATUS_INI;
1793 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1794 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1795 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1796 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001797 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1798 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1799 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001800 memcpy(newsrv->dns_opts.pref_net,
1801 curproxy->defsrv.dns_opts.pref_net,
1802 sizeof(newsrv->dns_opts.pref_net));
1803 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001804 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1805 newsrv->init_addr = curproxy->defsrv.init_addr;
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001806#if defined(USE_OPENSSL)
1807 /* SSL config. */
Frédéric Lécaille5e576432017-03-14 15:52:04 +01001808 if (curproxy->defsrv.ssl_ctx.ca_file != NULL)
1809 newsrv->ssl_ctx.ca_file = strdup(curproxy->defsrv.ssl_ctx.ca_file);
1810 if (curproxy->defsrv.ssl_ctx.crl_file != NULL)
1811 newsrv->ssl_ctx.crl_file = strdup(curproxy->defsrv.ssl_ctx.crl_file);
1812 if (curproxy->defsrv.ssl_ctx.client_crt != NULL)
1813 newsrv->ssl_ctx.client_crt = strdup(curproxy->defsrv.ssl_ctx.crl_file);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001814 newsrv->ssl_ctx.verify = curproxy->defsrv.ssl_ctx.verify;
Frédéric Lécaille273f3212017-03-13 15:52:01 +01001815 if (curproxy->defsrv.ssl_ctx.verify_host != NULL)
1816 newsrv->ssl_ctx.verify_host = strdup(curproxy->defsrv.ssl_ctx.verify_host);
Frédéric Lécaillebcaf1d72017-03-15 16:20:02 +01001817 if (curproxy->defsrv.ssl_ctx.ciphers != NULL)
1818 newsrv->ssl_ctx.ciphers = strdup(curproxy->defsrv.ssl_ctx.ciphers);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001819 if (curproxy->defsrv.sni_expr != NULL)
1820 newsrv->sni_expr = strdup(curproxy->defsrv.sni_expr);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001821#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02001822
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001823#ifdef TCP_USER_TIMEOUT
1824 newsrv->tcp_ut = curproxy->defsrv.tcp_ut;
1825#endif
1826
Willy Tarreau272adea2014-03-31 10:39:59 +02001827 cur_arg = 3;
1828 } else {
1829 newsrv = &curproxy->defsrv;
1830 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001831 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001832 }
1833
1834 while (*args[cur_arg]) {
1835 if (!strcmp(args[cur_arg], "agent-check")) {
1836 global.maxsock++;
1837 do_agent = 1;
1838 cur_arg += 1;
1839 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1840 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1841 if (err) {
1842 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1843 file, linenum, *err, newsrv->id);
1844 err_code |= ERR_ALERT | ERR_FATAL;
1845 goto out;
1846 }
1847 if (val <= 0) {
1848 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1849 file, linenum, val, args[cur_arg], newsrv->id);
1850 err_code |= ERR_ALERT | ERR_FATAL;
1851 goto out;
1852 }
1853 newsrv->agent.inter = val;
1854 cur_arg += 2;
1855 }
Misiekea849332017-01-09 09:39:51 +01001856 else if (!strcmp(args[cur_arg], "agent-addr")) {
1857 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1858 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1859 goto out;
1860 }
1861
1862 cur_arg += 2;
1863 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001864 else if (!strcmp(args[cur_arg], "agent-port")) {
1865 global.maxsock++;
1866 newsrv->agent.port = atol(args[cur_arg + 1]);
1867 cur_arg += 2;
1868 }
James Brown55f9ff12015-10-21 18:19:05 -07001869 else if (!strcmp(args[cur_arg], "agent-send")) {
1870 global.maxsock++;
1871 free(newsrv->agent.send_string);
1872 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1873 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1874 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1875 cur_arg += 2;
1876 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001877 else if (!strcmp(args[cur_arg], "init-addr")) {
1878 char *p, *end;
1879 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001880 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001881
1882 newsrv->init_addr_methods = 0;
1883 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1884
1885 for (p = args[cur_arg + 1]; *p; p = end) {
1886 /* cut on next comma */
1887 for (end = p; *end && *end != ','; end++);
1888 if (*end)
1889 *(end++) = 0;
1890
Willy Tarreau4310d362016-11-02 15:05:56 +01001891 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001892 if (!strcmp(p, "libc")) {
1893 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1894 }
1895 else if (!strcmp(p, "last")) {
1896 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1897 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001898 else if (!strcmp(p, "none")) {
1899 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1900 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001901 else if (str2ip2(p, &sa, 0)) {
1902 if (is_addr(&newsrv->init_addr)) {
1903 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1904 file, linenum, args[cur_arg], p);
1905 err_code |= ERR_ALERT | ERR_FATAL;
1906 goto out;
1907 }
1908 newsrv->init_addr = sa;
1909 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1910 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001911 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001912 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001913 file, linenum, args[cur_arg], p);
1914 err_code |= ERR_ALERT | ERR_FATAL;
1915 goto out;
1916 }
1917 if (!done) {
1918 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1919 file, linenum, args[cur_arg], p);
1920 err_code |= ERR_ALERT | ERR_FATAL;
1921 goto out;
1922 }
1923 }
1924 cur_arg += 2;
1925 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001926 else if (!strcmp(args[cur_arg], "resolvers")) {
1927 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1928 cur_arg += 2;
1929 }
1930 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1931 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001932 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001933 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001934 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001935 else {
1936 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1937 file, linenum, args[cur_arg]);
1938 err_code |= ERR_ALERT | ERR_FATAL;
1939 goto out;
1940 }
1941 cur_arg += 2;
1942 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001943 else if (!strcmp(args[cur_arg], "resolve-net")) {
1944 char *p, *e;
1945 unsigned char mask;
1946 struct dns_options *opt;
1947
1948 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1949 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1950 file, linenum, args[cur_arg]);
1951 err_code |= ERR_ALERT | ERR_FATAL;
1952 goto out;
1953 }
1954
1955 opt = &newsrv->dns_opts;
1956
1957 /* Split arguments by comma, and convert it from ipv4 or ipv6
1958 * string network in in_addr or in6_addr.
1959 */
1960 p = args[cur_arg + 1];
1961 e = p;
1962 while (*p != '\0') {
1963 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001964 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001965 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1966 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1967 err_code |= ERR_ALERT | ERR_FATAL;
1968 goto out;
1969 }
1970 /* look for end or comma. */
1971 while (*e != ',' && *e != '\0')
1972 e++;
1973 if (*e == ',') {
1974 *e = '\0';
1975 e++;
1976 }
1977 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1978 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1979 /* Try to convert input string from ipv4 or ipv6 network. */
1980 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1981 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1982 &mask)) {
1983 /* Try to convert input string from ipv6 network. */
1984 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1985 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1986 } else {
1987 /* All network conversions fail, retrun error. */
1988 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1989 file, linenum, args[cur_arg], p);
1990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992 }
1993 opt->pref_net_nb++;
1994 p = e;
1995 }
1996
1997 cur_arg += 2;
1998 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001999 else if (!strcmp(args[cur_arg], "rise")) {
2000 if (!*args[cur_arg + 1]) {
2001 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2002 file, linenum, args[cur_arg]);
2003 err_code |= ERR_ALERT | ERR_FATAL;
2004 goto out;
2005 }
2006
2007 newsrv->check.rise = atol(args[cur_arg + 1]);
2008 if (newsrv->check.rise <= 0) {
2009 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2010 file, linenum, args[cur_arg]);
2011 err_code |= ERR_ALERT | ERR_FATAL;
2012 goto out;
2013 }
2014
2015 if (newsrv->check.health)
2016 newsrv->check.health = newsrv->check.rise;
2017 cur_arg += 2;
2018 }
2019 else if (!strcmp(args[cur_arg], "fall")) {
2020 newsrv->check.fall = atol(args[cur_arg + 1]);
2021
2022 if (!*args[cur_arg + 1]) {
2023 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2024 file, linenum, args[cur_arg]);
2025 err_code |= ERR_ALERT | ERR_FATAL;
2026 goto out;
2027 }
2028
2029 if (newsrv->check.fall <= 0) {
2030 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2031 file, linenum, args[cur_arg]);
2032 err_code |= ERR_ALERT | ERR_FATAL;
2033 goto out;
2034 }
2035
2036 cur_arg += 2;
2037 }
2038 else if (!strcmp(args[cur_arg], "inter")) {
2039 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2040 if (err) {
2041 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2042 file, linenum, *err, newsrv->id);
2043 err_code |= ERR_ALERT | ERR_FATAL;
2044 goto out;
2045 }
2046 if (val <= 0) {
2047 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2048 file, linenum, val, args[cur_arg], newsrv->id);
2049 err_code |= ERR_ALERT | ERR_FATAL;
2050 goto out;
2051 }
2052 newsrv->check.inter = val;
2053 cur_arg += 2;
2054 }
2055 else if (!strcmp(args[cur_arg], "fastinter")) {
2056 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2057 if (err) {
2058 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2059 file, linenum, *err, newsrv->id);
2060 err_code |= ERR_ALERT | ERR_FATAL;
2061 goto out;
2062 }
2063 if (val <= 0) {
2064 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2065 file, linenum, val, args[cur_arg], newsrv->id);
2066 err_code |= ERR_ALERT | ERR_FATAL;
2067 goto out;
2068 }
2069 newsrv->check.fastinter = val;
2070 cur_arg += 2;
2071 }
2072 else if (!strcmp(args[cur_arg], "downinter")) {
2073 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2074 if (err) {
2075 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2076 file, linenum, *err, newsrv->id);
2077 err_code |= ERR_ALERT | ERR_FATAL;
2078 goto out;
2079 }
2080 if (val <= 0) {
2081 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2082 file, linenum, val, args[cur_arg], newsrv->id);
2083 err_code |= ERR_ALERT | ERR_FATAL;
2084 goto out;
2085 }
2086 newsrv->check.downinter = val;
2087 cur_arg += 2;
2088 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 else if (!strcmp(args[cur_arg], "port")) {
2090 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002091 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002092 cur_arg += 2;
2093 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002094 else if (!strcmp(args[cur_arg], "weight")) {
2095 int w;
2096 w = atol(args[cur_arg + 1]);
2097 if (w < 0 || w > SRV_UWGHT_MAX) {
2098 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2099 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2100 err_code |= ERR_ALERT | ERR_FATAL;
2101 goto out;
2102 }
2103 newsrv->uweight = newsrv->iweight = w;
2104 cur_arg += 2;
2105 }
2106 else if (!strcmp(args[cur_arg], "minconn")) {
2107 newsrv->minconn = atol(args[cur_arg + 1]);
2108 cur_arg += 2;
2109 }
2110 else if (!strcmp(args[cur_arg], "maxconn")) {
2111 newsrv->maxconn = atol(args[cur_arg + 1]);
2112 cur_arg += 2;
2113 }
2114 else if (!strcmp(args[cur_arg], "maxqueue")) {
2115 newsrv->maxqueue = atol(args[cur_arg + 1]);
2116 cur_arg += 2;
2117 }
2118 else if (!strcmp(args[cur_arg], "slowstart")) {
2119 /* slowstart is stored in seconds */
2120 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2121 if (err) {
2122 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2123 file, linenum, *err, newsrv->id);
2124 err_code |= ERR_ALERT | ERR_FATAL;
2125 goto out;
2126 }
2127 newsrv->slowstart = (val + 999) / 1000;
2128 cur_arg += 2;
2129 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 else if (!strcmp(args[cur_arg], "on-error")) {
2131 if (!strcmp(args[cur_arg + 1], "fastinter"))
2132 newsrv->onerror = HANA_ONERR_FASTINTER;
2133 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2134 newsrv->onerror = HANA_ONERR_FAILCHK;
2135 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2136 newsrv->onerror = HANA_ONERR_SUDDTH;
2137 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2138 newsrv->onerror = HANA_ONERR_MARKDWN;
2139 else {
2140 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2141 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2142 file, linenum, args[cur_arg], args[cur_arg + 1]);
2143 err_code |= ERR_ALERT | ERR_FATAL;
2144 goto out;
2145 }
2146
2147 cur_arg += 2;
2148 }
2149 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2150 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2151 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2152 else {
2153 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2154 file, linenum, args[cur_arg], args[cur_arg + 1]);
2155 err_code |= ERR_ALERT | ERR_FATAL;
2156 goto out;
2157 }
2158
2159 cur_arg += 2;
2160 }
2161 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2162 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2163 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2164 else {
2165 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2166 file, linenum, args[cur_arg], args[cur_arg + 1]);
2167 err_code |= ERR_ALERT | ERR_FATAL;
2168 goto out;
2169 }
2170
2171 cur_arg += 2;
2172 }
2173 else if (!strcmp(args[cur_arg], "error-limit")) {
2174 if (!*args[cur_arg + 1]) {
2175 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2176 file, linenum, args[cur_arg]);
2177 err_code |= ERR_ALERT | ERR_FATAL;
2178 goto out;
2179 }
2180
2181 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2182
2183 if (newsrv->consecutive_errors_limit <= 0) {
2184 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2185 file, linenum, args[cur_arg]);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189 cur_arg += 2;
2190 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002191 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
2192 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2193 file, linenum, "usesrc", "source");
2194 err_code |= ERR_ALERT | ERR_FATAL;
2195 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002196 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002197 else {
2198 static int srv_dumped;
2199 struct srv_kw *kw;
2200 char *err;
2201
2202 kw = srv_find_kw(args[cur_arg]);
2203 if (kw) {
2204 char *err = NULL;
2205 int code;
2206
2207 if (!kw->parse) {
2208 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2209 file, linenum, args[0], args[1], args[cur_arg]);
2210 cur_arg += 1 + kw->skip ;
2211 err_code |= ERR_ALERT | ERR_FATAL;
2212 goto out;
2213 }
2214
2215 if (defsrv && !kw->default_ok) {
2216 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2217 file, linenum, args[0], args[1], args[cur_arg]);
2218 cur_arg += 1 + kw->skip ;
2219 err_code |= ERR_ALERT;
2220 continue;
2221 }
2222
2223 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2224 err_code |= code;
2225
2226 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002227 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002228 if (code & ERR_FATAL) {
2229 free(err);
2230 cur_arg += 1 + kw->skip;
2231 goto out;
2232 }
2233 }
2234 free(err);
2235 cur_arg += 1 + kw->skip;
2236 continue;
2237 }
2238
2239 err = NULL;
2240 if (!srv_dumped) {
2241 srv_dump_kws(&err);
2242 indent_msg(&err, 4);
2243 srv_dumped = 1;
2244 }
2245
2246 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2247 file, linenum, args[0], args[1], args[cur_arg],
2248 err ? " Registered keywords :" : "", err ? err : "");
2249 free(err);
2250
2251 err_code |= ERR_ALERT | ERR_FATAL;
2252 goto out;
2253 }
2254 }
2255
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002256 /* This check is done only for 'server' instances. */
2257 if (!defsrv && newsrv->do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002258 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002259
2260 if (newsrv->trackit) {
2261 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
2262 file, linenum);
2263 err_code |= ERR_ALERT | ERR_FATAL;
2264 goto out;
2265 }
2266
Willy Tarreau272adea2014-03-31 10:39:59 +02002267 /*
2268 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02002269 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02002270 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002271 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09002272 (is_inet_addr(&newsrv->check.addr) ||
2273 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01002274 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002275 struct list *l;
2276
2277 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
2278 if (!r) {
2279 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
2280 file, linenum, newsrv->id);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002284 /* search the first action (connect / send / expect) in the list */
2285 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002286 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002287 if (r->action != TCPCHK_ACT_COMMENT)
2288 break;
2289 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002290 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
2291 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",
2292 file, linenum, newsrv->id);
2293 err_code |= ERR_ALERT | ERR_FATAL;
2294 goto out;
2295 }
2296 else {
2297 /* scan the tcp-check ruleset to ensure a port has been configured */
2298 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002299 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002300 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
2301 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",
2302 file, linenum, newsrv->id);
2303 err_code |= ERR_ALERT | ERR_FATAL;
2304 goto out;
2305 }
2306 }
2307 }
2308 }
2309
2310 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09002311 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002312 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002313 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2314 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002315 goto out;
2316 }
2317
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002318 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01002319 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002320
Willy Tarreau272adea2014-03-31 10:39:59 +02002321 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002322 global.maxsock++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002323 }
2324
2325 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002326 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002327
2328 if (!newsrv->agent.port) {
2329 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2330 file, linenum, newsrv->id);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334
2335 if (!newsrv->agent.inter)
2336 newsrv->agent.inter = newsrv->check.inter;
2337
Simon Hormanb1900d52015-01-30 11:22:54 +09002338 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02002339 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002340 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2341 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002342 goto out;
2343 }
2344
2345 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2346 }
2347
2348 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02002349 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 curproxy->srv_bck++;
2351 else
2352 curproxy->srv_act++;
2353
Willy Tarreauc5150da2014-05-13 19:27:31 +02002354 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002355 }
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002356#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2357 if (!defsrv && newsrv->sni_expr) {
2358 int code;
2359 char *err;
2360
2361 err = NULL;
2362
2363 code = server_parse_sni_expr(newsrv, curproxy, &err);
2364 err_code |= code;
2365 if (code) {
2366 display_parser_err(file, linenum, args, cur_arg, &err);
2367 free(err);
2368 if (code & ERR_FATAL)
2369 goto out;
2370 }
2371 }
2372#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02002373 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002374 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002375 return 0;
2376
2377 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002378 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002379 free(errmsg);
2380 return err_code;
2381}
2382
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002383/* Returns a pointer to the first server matching either id <id>.
2384 * NULL is returned if no match is found.
2385 * the lookup is performed in the backend <bk>
2386 */
2387struct server *server_find_by_id(struct proxy *bk, int id)
2388{
2389 struct eb32_node *eb32;
2390 struct server *curserver;
2391
2392 if (!bk || (id ==0))
2393 return NULL;
2394
2395 /* <bk> has no backend capabilities, so it can't have a server */
2396 if (!(bk->cap & PR_CAP_BE))
2397 return NULL;
2398
2399 curserver = NULL;
2400
2401 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2402 if (eb32)
2403 curserver = container_of(eb32, struct server, conf.id);
2404
2405 return curserver;
2406}
2407
2408/* Returns a pointer to the first server matching either name <name>, or id
2409 * if <name> starts with a '#'. NULL is returned if no match is found.
2410 * the lookup is performed in the backend <bk>
2411 */
2412struct server *server_find_by_name(struct proxy *bk, const char *name)
2413{
2414 struct server *curserver;
2415
2416 if (!bk || !name)
2417 return NULL;
2418
2419 /* <bk> has no backend capabilities, so it can't have a server */
2420 if (!(bk->cap & PR_CAP_BE))
2421 return NULL;
2422
2423 curserver = NULL;
2424 if (*name == '#') {
2425 curserver = server_find_by_id(bk, atoi(name + 1));
2426 if (curserver)
2427 return curserver;
2428 }
2429 else {
2430 curserver = bk->srv;
2431
2432 while (curserver && (strcmp(curserver->id, name) != 0))
2433 curserver = curserver->next;
2434
2435 if (curserver)
2436 return curserver;
2437 }
2438
2439 return NULL;
2440}
2441
2442struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2443{
2444 struct server *byname;
2445 struct server *byid;
2446
2447 if (!name && !id)
2448 return NULL;
2449
2450 if (diff)
2451 *diff = 0;
2452
2453 byname = byid = NULL;
2454
2455 if (name) {
2456 byname = server_find_by_name(bk, name);
2457 if (byname && (!id || byname->puid == id))
2458 return byname;
2459 }
2460
2461 /* remaining possibilities :
2462 * - name not set
2463 * - name set but not found
2464 * - name found but ID doesn't match
2465 */
2466 if (id) {
2467 byid = server_find_by_id(bk, id);
2468 if (byid) {
2469 if (byname) {
2470 /* use id only if forced by configuration */
2471 if (byid->flags & SRV_F_FORCED_ID) {
2472 if (diff)
2473 *diff |= 2;
2474 return byid;
2475 }
2476 else {
2477 if (diff)
2478 *diff |= 1;
2479 return byname;
2480 }
2481 }
2482
2483 /* remaining possibilities:
2484 * - name not set
2485 * - name set but not found
2486 */
2487 if (name && diff)
2488 *diff |= 2;
2489 return byid;
2490 }
2491
2492 /* id bot found */
2493 if (byname) {
2494 if (diff)
2495 *diff |= 1;
2496 return byname;
2497 }
2498 }
2499
2500 return NULL;
2501}
2502
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002503/* Update a server state using the parameters available in the params list */
2504static void srv_update_state(struct server *srv, int version, char **params)
2505{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002506 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002507 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002508
2509 /* fields since version 1
2510 * and common to all other upcoming versions
2511 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002512 enum srv_state srv_op_state;
2513 enum srv_admin srv_admin_state;
2514 unsigned srv_uweight, srv_iweight;
2515 unsigned long srv_last_time_change;
2516 short srv_check_status;
2517 enum chk_result srv_check_result;
2518 int srv_check_health;
2519 int srv_check_state, srv_agent_state;
2520 int bk_f_forced_id;
2521 int srv_f_forced_id;
2522
Willy Tarreau31138fa2015-09-29 18:38:47 +02002523 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002524 switch (version) {
2525 case 1:
2526 /*
2527 * now we can proceed with server's state update:
2528 * srv_addr: params[0]
2529 * srv_op_state: params[1]
2530 * srv_admin_state: params[2]
2531 * srv_uweight: params[3]
2532 * srv_iweight: params[4]
2533 * srv_last_time_change: params[5]
2534 * srv_check_status: params[6]
2535 * srv_check_result: params[7]
2536 * srv_check_health: params[8]
2537 * srv_check_state: params[9]
2538 * srv_agent_state: params[10]
2539 * bk_f_forced_id: params[11]
2540 * srv_f_forced_id: params[12]
2541 */
2542
2543 /* validating srv_op_state */
2544 p = NULL;
2545 errno = 0;
2546 srv_op_state = strtol(params[1], &p, 10);
2547 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2548 (srv_op_state != SRV_ST_STOPPED &&
2549 srv_op_state != SRV_ST_STARTING &&
2550 srv_op_state != SRV_ST_RUNNING &&
2551 srv_op_state != SRV_ST_STOPPING)) {
2552 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2553 }
2554
2555 /* validating srv_admin_state */
2556 p = NULL;
2557 errno = 0;
2558 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002559
2560 /* inherited statuses will be recomputed later */
2561 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2562
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002563 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2564 (srv_admin_state != 0 &&
2565 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002566 srv_admin_state != SRV_ADMF_CMAINT &&
2567 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002568 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002569 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002570 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2571 }
2572
2573 /* validating srv_uweight */
2574 p = NULL;
2575 errno = 0;
2576 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002577 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002578 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2579
2580 /* validating srv_iweight */
2581 p = NULL;
2582 errno = 0;
2583 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002584 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002585 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2586
2587 /* validating srv_last_time_change */
2588 p = NULL;
2589 errno = 0;
2590 srv_last_time_change = strtol(params[5], &p, 10);
2591 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2592 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2593
2594 /* validating srv_check_status */
2595 p = NULL;
2596 errno = 0;
2597 srv_check_status = strtol(params[6], &p, 10);
2598 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2599 (srv_check_status >= HCHK_STATUS_SIZE))
2600 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2601
2602 /* validating srv_check_result */
2603 p = NULL;
2604 errno = 0;
2605 srv_check_result = strtol(params[7], &p, 10);
2606 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2607 (srv_check_result != CHK_RES_UNKNOWN &&
2608 srv_check_result != CHK_RES_NEUTRAL &&
2609 srv_check_result != CHK_RES_FAILED &&
2610 srv_check_result != CHK_RES_PASSED &&
2611 srv_check_result != CHK_RES_CONDPASS)) {
2612 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2613 }
2614
2615 /* validating srv_check_health */
2616 p = NULL;
2617 errno = 0;
2618 srv_check_health = strtol(params[8], &p, 10);
2619 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2620 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2621
2622 /* validating srv_check_state */
2623 p = NULL;
2624 errno = 0;
2625 srv_check_state = strtol(params[9], &p, 10);
2626 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2627 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2628 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2629
2630 /* validating srv_agent_state */
2631 p = NULL;
2632 errno = 0;
2633 srv_agent_state = strtol(params[10], &p, 10);
2634 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2635 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2636 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2637
2638 /* validating bk_f_forced_id */
2639 p = NULL;
2640 errno = 0;
2641 bk_f_forced_id = strtol(params[11], &p, 10);
2642 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2643 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2644
2645 /* validating srv_f_forced_id */
2646 p = NULL;
2647 errno = 0;
2648 srv_f_forced_id = strtol(params[12], &p, 10);
2649 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2650 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2651
2652
2653 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002654 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002655 goto out;
2656
2657 /* recover operational state and apply it to this server
2658 * and all servers tracking this one */
2659 switch (srv_op_state) {
2660 case SRV_ST_STOPPED:
2661 srv->check.health = 0;
2662 srv_set_stopped(srv, "changed from server-state after a reload");
2663 break;
2664 case SRV_ST_STARTING:
2665 srv->state = srv_op_state;
2666 break;
2667 case SRV_ST_STOPPING:
2668 srv->check.health = srv->check.rise + srv->check.fall - 1;
2669 srv_set_stopping(srv, "changed from server-state after a reload");
2670 break;
2671 case SRV_ST_RUNNING:
2672 srv->check.health = srv->check.rise + srv->check.fall - 1;
2673 srv_set_running(srv, "");
2674 break;
2675 }
2676
2677 /* When applying server state, the following rules apply:
2678 * - in case of a configuration change, we apply the setting from the new
2679 * configuration, regardless of old running state
2680 * - if no configuration change, we apply old running state only if old running
2681 * state is different from new configuration state
2682 */
2683 /* configuration has changed */
2684 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2685 if (srv->admin & SRV_ADMF_CMAINT)
2686 srv_adm_set_maint(srv);
2687 else
2688 srv_adm_set_ready(srv);
2689 }
2690 /* configuration is the same, let's compate old running state and new conf state */
2691 else {
2692 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2693 srv_adm_set_maint(srv);
2694 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2695 srv_adm_set_ready(srv);
2696 }
2697 /* apply drain mode if server is currently enabled */
2698 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2699 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002700 * (srv->iweight is the weight set up in configuration).
2701 * There are two possible reasons for FDRAIN to have been present :
2702 * - previous config weight was zero
2703 * - "set server b/s drain" was sent to the CLI
2704 *
2705 * In the first case, we simply want to drop this drain state
2706 * if the new weight is not zero anymore, meaning the administrator
2707 * has intentionally turned the weight back to a positive value to
2708 * enable the server again after an operation. In the second case,
2709 * the drain state was forced on the CLI regardless of the config's
2710 * weight so we don't want a change to the config weight to lose this
2711 * status. What this means is :
2712 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2713 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002714 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002715 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002716 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002717 }
2718
2719 srv->last_change = date.tv_sec - srv_last_time_change;
2720 srv->check.status = srv_check_status;
2721 srv->check.result = srv_check_result;
2722 srv->check.health = srv_check_health;
2723
2724 /* Only case we want to apply is removing ENABLED flag which could have been
2725 * done by the "disable health" command over the stats socket
2726 */
2727 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2728 (srv_check_state & CHK_ST_CONFIGURED) &&
2729 !(srv_check_state & CHK_ST_ENABLED))
2730 srv->check.state &= ~CHK_ST_ENABLED;
2731
2732 /* Only case we want to apply is removing ENABLED flag which could have been
2733 * done by the "disable agent" command over the stats socket
2734 */
2735 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2736 (srv_agent_state & CHK_ST_CONFIGURED) &&
2737 !(srv_agent_state & CHK_ST_ENABLED))
2738 srv->agent.state &= ~CHK_ST_ENABLED;
2739
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002740 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2741 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002742 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002743 * It means that a configuration file change has precedence over a unix socket change
2744 * for server's weight
2745 *
2746 * by default, HAProxy applies the following weight when parsing the configuration
2747 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002748 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002749 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002750 srv->uweight = srv_uweight;
2751 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002752 server_recalc_eweight(srv);
2753
Willy Tarreaue5a60682016-11-09 14:54:53 +01002754 /* load server IP address */
2755 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002756 break;
2757 default:
2758 chunk_appendf(msg, ", version '%d' not supported", version);
2759 }
2760
2761 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002762 if (msg->len) {
2763 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002764 Warning("server-state application failed for server '%s/%s'%s",
2765 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002766 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002767}
2768
2769/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2770 * For each proxy, it does the following:
2771 * - opens its server state file (either one or local one)
2772 * - read whole file, line by line
2773 * - analyse each line to check if it matches our current backend:
2774 * - backend name matches
2775 * - backend id matches if id is forced and name doesn't match
2776 * - if the server pointed by the line is found, then state is applied
2777 *
2778 * If the running backend uuid or id differs from the state file, then HAProxy reports
2779 * a warning.
2780 */
2781void apply_server_state(void)
2782{
2783 char *cur, *end;
2784 char mybuf[SRV_STATE_LINE_MAXLEN];
2785 int mybuflen;
2786 char *params[SRV_STATE_FILE_MAX_FIELDS];
2787 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2788 int arg, srv_arg, version, diff;
2789 FILE *f;
2790 char *filepath;
2791 char globalfilepath[MAXPATHLEN + 1];
2792 char localfilepath[MAXPATHLEN + 1];
2793 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2794 extern struct proxy *proxy;
2795 struct proxy *curproxy, *bk;
2796 struct server *srv;
2797
2798 globalfilepathlen = 0;
2799 /* create the globalfilepath variable */
2800 if (global.server_state_file) {
2801 /* absolute path or no base directory provided */
2802 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2803 len = strlen(global.server_state_file);
2804 if (len > MAXPATHLEN) {
2805 globalfilepathlen = 0;
2806 goto globalfileerror;
2807 }
2808 memcpy(globalfilepath, global.server_state_file, len);
2809 globalfilepath[len] = '\0';
2810 globalfilepathlen = len;
2811 }
2812 else if (global.server_state_base) {
2813 len = strlen(global.server_state_base);
2814 globalfilepathlen += len;
2815
2816 if (globalfilepathlen > MAXPATHLEN) {
2817 globalfilepathlen = 0;
2818 goto globalfileerror;
2819 }
2820 strncpy(globalfilepath, global.server_state_base, len);
2821 globalfilepath[globalfilepathlen] = 0;
2822
2823 /* append a slash if needed */
2824 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2825 if (globalfilepathlen + 1 > MAXPATHLEN) {
2826 globalfilepathlen = 0;
2827 goto globalfileerror;
2828 }
2829 globalfilepath[globalfilepathlen++] = '/';
2830 }
2831
2832 len = strlen(global.server_state_file);
2833 if (globalfilepathlen + len > MAXPATHLEN) {
2834 globalfilepathlen = 0;
2835 goto globalfileerror;
2836 }
2837 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2838 globalfilepathlen += len;
2839 globalfilepath[globalfilepathlen++] = 0;
2840 }
2841 }
2842 globalfileerror:
2843 if (globalfilepathlen == 0)
2844 globalfilepath[0] = '\0';
2845
2846 /* read servers state from local file */
2847 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2848 /* servers are only in backends */
2849 if (!(curproxy->cap & PR_CAP_BE))
2850 continue;
2851 fileopenerr = 0;
2852 filepath = NULL;
2853
2854 /* search server state file path and name */
2855 switch (curproxy->load_server_state_from_file) {
2856 /* read servers state from global file */
2857 case PR_SRV_STATE_FILE_GLOBAL:
2858 /* there was an error while generating global server state file path */
2859 if (globalfilepathlen == 0)
2860 continue;
2861 filepath = globalfilepath;
2862 fileopenerr = 1;
2863 break;
2864 /* this backend has its own file */
2865 case PR_SRV_STATE_FILE_LOCAL:
2866 localfilepathlen = 0;
2867 localfilepath[0] = '\0';
2868 len = 0;
2869 /* create the localfilepath variable */
2870 /* absolute path or no base directory provided */
2871 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2872 len = strlen(curproxy->server_state_file_name);
2873 if (len > MAXPATHLEN) {
2874 localfilepathlen = 0;
2875 goto localfileerror;
2876 }
2877 memcpy(localfilepath, curproxy->server_state_file_name, len);
2878 localfilepath[len] = '\0';
2879 localfilepathlen = len;
2880 }
2881 else if (global.server_state_base) {
2882 len = strlen(global.server_state_base);
2883 localfilepathlen += len;
2884
2885 if (localfilepathlen > MAXPATHLEN) {
2886 localfilepathlen = 0;
2887 goto localfileerror;
2888 }
2889 strncpy(localfilepath, global.server_state_base, len);
2890 localfilepath[localfilepathlen] = 0;
2891
2892 /* append a slash if needed */
2893 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2894 if (localfilepathlen + 1 > MAXPATHLEN) {
2895 localfilepathlen = 0;
2896 goto localfileerror;
2897 }
2898 localfilepath[localfilepathlen++] = '/';
2899 }
2900
2901 len = strlen(curproxy->server_state_file_name);
2902 if (localfilepathlen + len > MAXPATHLEN) {
2903 localfilepathlen = 0;
2904 goto localfileerror;
2905 }
2906 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2907 localfilepathlen += len;
2908 localfilepath[localfilepathlen++] = 0;
2909 }
2910 filepath = localfilepath;
2911 localfileerror:
2912 if (localfilepathlen == 0)
2913 localfilepath[0] = '\0';
2914
2915 break;
2916 case PR_SRV_STATE_FILE_NONE:
2917 default:
2918 continue;
2919 }
2920
2921 /* preload global state file */
2922 errno = 0;
2923 f = fopen(filepath, "r");
2924 if (errno && fileopenerr)
2925 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2926 if (!f)
2927 continue;
2928
2929 mybuf[0] = '\0';
2930 mybuflen = 0;
2931 version = 0;
2932
2933 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002934 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2935 Warning("Can't read first line of the server state file '%s'\n", filepath);
2936 goto fileclose;
2937 }
2938
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002939 cur = mybuf;
2940 version = atoi(cur);
2941 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2942 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002943 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002944
2945 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2946 int bk_f_forced_id = 0;
2947 int check_id = 0;
2948 int check_name = 0;
2949
2950 mybuflen = strlen(mybuf);
2951 cur = mybuf;
2952 end = cur + mybuflen;
2953
2954 bk = NULL;
2955 srv = NULL;
2956
2957 /* we need at least one character */
2958 if (mybuflen == 0)
2959 continue;
2960
2961 /* ignore blank characters at the beginning of the line */
2962 while (isspace(*cur))
2963 ++cur;
2964
2965 if (cur == end)
2966 continue;
2967
2968 /* ignore comment line */
2969 if (*cur == '#')
2970 continue;
2971
2972 /* we're now ready to move the line into *srv_params[] */
2973 params[0] = cur;
2974 arg = 1;
2975 srv_arg = 0;
2976 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2977 if (isspace(*cur)) {
2978 *cur = '\0';
2979 ++cur;
2980 while (isspace(*cur))
2981 ++cur;
2982 switch (version) {
2983 case 1:
2984 /*
2985 * srv_addr: params[4] => srv_params[0]
2986 * srv_op_state: params[5] => srv_params[1]
2987 * srv_admin_state: params[6] => srv_params[2]
2988 * srv_uweight: params[7] => srv_params[3]
2989 * srv_iweight: params[8] => srv_params[4]
2990 * srv_last_time_change: params[9] => srv_params[5]
2991 * srv_check_status: params[10] => srv_params[6]
2992 * srv_check_result: params[11] => srv_params[7]
2993 * srv_check_health: params[12] => srv_params[8]
2994 * srv_check_state: params[13] => srv_params[9]
2995 * srv_agent_state: params[14] => srv_params[10]
2996 * bk_f_forced_id: params[15] => srv_params[11]
2997 * srv_f_forced_id: params[16] => srv_params[12]
2998 */
2999 if (arg >= 4) {
3000 srv_params[srv_arg] = cur;
3001 ++srv_arg;
3002 }
3003 break;
3004 }
3005
3006 params[arg] = cur;
3007 ++arg;
3008 }
3009 else {
3010 ++cur;
3011 }
3012 }
3013
3014 /* if line is incomplete line, then ignore it.
3015 * otherwise, update useful flags */
3016 switch (version) {
3017 case 1:
3018 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3019 continue;
3020 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3021 check_id = (atoi(params[0]) == curproxy->uuid);
3022 check_name = (strcmp(curproxy->id, params[1]) == 0);
3023 break;
3024 }
3025
3026 diff = 0;
3027 bk = curproxy;
3028
3029 /* if backend can't be found, let's continue */
3030 if (!check_id && !check_name)
3031 continue;
3032 else if (!check_id && check_name) {
3033 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3034 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3035 }
3036 else if (check_id && !check_name) {
3037 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3038 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3039 /* if name doesn't match, we still want to update curproxy if the backend id
3040 * was forced in previous the previous configuration */
3041 if (!bk_f_forced_id)
3042 continue;
3043 }
3044
3045 /* look for the server by its id: param[2] */
3046 /* else look for the server by its name: param[3] */
3047 diff = 0;
3048 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3049
3050 if (!srv) {
3051 /* if no server found, then warning and continue with next line */
3052 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3053 params[3], params[2], params[0], params[1]);
3054 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3055 params[3], params[2], params[0], params[1]);
3056 continue;
3057 }
3058 else if (diff & PR_FBM_MISMATCH_ID) {
3059 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);
3060 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);
3061 }
3062 else if (diff & PR_FBM_MISMATCH_NAME) {
3063 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);
3064 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);
3065 }
3066
3067 /* now we can proceed with server's state update */
3068 srv_update_state(srv, version, srv_params);
3069 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003070fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003071 fclose(f);
3072 }
3073}
3074
Simon Horman7d09b9a2013-02-12 10:45:51 +09003075/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003076 * update a server's current IP address.
3077 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3078 * ip is in network format.
3079 * updater is a string which contains an information about the requester of the update.
3080 * updater is used if not NULL.
3081 *
3082 * A log line and a stderr warning message is generated based on server's backend options.
3083 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003084int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003085{
3086 /* generates a log line and a warning on stderr */
3087 if (1) {
3088 /* book enough space for both IPv4 and IPv6 */
3089 char oldip[INET6_ADDRSTRLEN];
3090 char newip[INET6_ADDRSTRLEN];
3091
3092 memset(oldip, '\0', INET6_ADDRSTRLEN);
3093 memset(newip, '\0', INET6_ADDRSTRLEN);
3094
3095 /* copy old IP address in a string */
3096 switch (s->addr.ss_family) {
3097 case AF_INET:
3098 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3099 break;
3100 case AF_INET6:
3101 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3102 break;
3103 };
3104
3105 /* copy new IP address in a string */
3106 switch (ip_sin_family) {
3107 case AF_INET:
3108 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3109 break;
3110 case AF_INET6:
3111 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3112 break;
3113 };
3114
3115 /* save log line into a buffer */
3116 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3117 s->proxy->id, s->id, oldip, newip, updater);
3118
3119 /* write the buffer on stderr */
3120 Warning("%s.\n", trash.str);
3121
3122 /* send a log */
3123 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3124 }
3125
3126 /* save the new IP family */
3127 s->addr.ss_family = ip_sin_family;
3128 /* save the new IP address */
3129 switch (ip_sin_family) {
3130 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003131 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003132 break;
3133 case AF_INET6:
3134 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3135 break;
3136 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003137 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003138
3139 return 0;
3140}
3141
3142/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003143 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3144 *
3145 * Caller can pass its name through <updater> to get it integrated in the response message
3146 * returned by the function.
3147 *
3148 * The function first does the following, in that order:
3149 * - validates the new addr and/or port
3150 * - checks if an update is required (new IP or port is different than current ones)
3151 * - checks the update is allowed:
3152 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3153 * - allow all changes if no CHECKS are configured
3154 * - if CHECK is configured:
3155 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3156 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3157 * conditions are met
3158 */
3159const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3160{
3161 struct sockaddr_storage sa;
3162 int ret, port_change_required;
3163 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003164 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003165 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003166 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003167
3168 msg = get_trash_chunk();
3169 chunk_reset(msg);
3170
3171 if (addr) {
3172 memset(&sa, 0, sizeof(struct sockaddr_storage));
3173 if (str2ip2(addr, &sa, 0) == NULL) {
3174 chunk_printf(msg, "Invalid addr '%s'", addr);
3175 goto out;
3176 }
3177
3178 /* changes are allowed on AF_INET* families only */
3179 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3180 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3181 goto out;
3182 }
3183
3184 /* collecting data currently setup */
3185 memset(current_addr, '\0', sizeof(current_addr));
3186 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3187 /* changes are allowed on AF_INET* families only */
3188 if ((ret != AF_INET) && (ret != AF_INET6)) {
3189 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3190 goto out;
3191 }
3192
3193 /* applying ADDR changes if required and allowed
3194 * ipcmp returns 0 when both ADDR are the same
3195 */
3196 if (ipcmp(&s->addr, &sa) == 0) {
3197 chunk_appendf(msg, "no need to change the addr");
3198 goto port;
3199 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003200 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003201 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003202
3203 /* we also need to update check's ADDR only if it uses the server's one */
3204 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003205 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003206 }
3207
3208 /* we also need to update agent ADDR only if it use the server's one */
3209 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003210 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003211 }
3212
3213 /* update report for caller */
3214 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3215 }
3216
3217 port:
3218 if (port) {
3219 char sign = '\0';
3220 char *endptr;
3221
3222 if (addr)
3223 chunk_appendf(msg, ", ");
3224
3225 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003226 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003227
3228 /* check if PORT change is required */
3229 port_change_required = 0;
3230
3231 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003232 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003233 new_port = strtol(port, &endptr, 10);
3234 if ((errno != 0) || (port == endptr)) {
3235 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3236 goto out;
3237 }
3238
3239 /* check if caller triggers a port mapped or offset */
3240 if (sign == '-' || (sign == '+')) {
3241 /* check if server currently uses port map */
3242 if (!(s->flags & SRV_F_MAPPORTS)) {
3243 /* switch from fixed port to port map mandatorily triggers
3244 * a port change */
3245 port_change_required = 1;
3246 /* check is configured
3247 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3248 * prevent PORT change if check doesn't have it's dedicated port while switching
3249 * to port mapping */
3250 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3251 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.");
3252 goto out;
3253 }
3254 }
3255 /* we're already using port maps */
3256 else {
3257 port_change_required = current_port != new_port;
3258 }
3259 }
3260 /* fixed port */
3261 else {
3262 port_change_required = current_port != new_port;
3263 }
3264
3265 /* applying PORT changes if required and update response message */
3266 if (port_change_required) {
3267 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003268 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003269 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003270
3271 /* prepare message */
3272 chunk_appendf(msg, "port changed from '");
3273 if (s->flags & SRV_F_MAPPORTS)
3274 chunk_appendf(msg, "+");
3275 chunk_appendf(msg, "%d' to '", current_port);
3276
3277 if (sign == '-') {
3278 s->flags |= SRV_F_MAPPORTS;
3279 chunk_appendf(msg, "%c", sign);
3280 /* just use for result output */
3281 new_port = -new_port;
3282 }
3283 else if (sign == '+') {
3284 s->flags |= SRV_F_MAPPORTS;
3285 chunk_appendf(msg, "%c", sign);
3286 }
3287 else {
3288 s->flags &= ~SRV_F_MAPPORTS;
3289 }
3290
3291 chunk_appendf(msg, "%d'", new_port);
3292
3293 /* we also need to update health checks port only if it uses server's realport */
3294 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3295 s->check.port = new_port;
3296 }
3297 }
3298 else {
3299 chunk_appendf(msg, "no need to change the port");
3300 }
3301 }
3302
3303out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003304 if (changed)
3305 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003306 if (updater)
3307 chunk_appendf(msg, " by '%s'", updater);
3308 chunk_appendf(msg, "\n");
3309 return msg->str;
3310}
3311
3312
3313/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003314 * update server status based on result of name resolution
3315 * returns:
3316 * 0 if server status is updated
3317 * 1 if server status has not changed
3318 */
3319int snr_update_srv_status(struct server *s)
3320{
3321 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003322 struct dns_resolvers *resolvers;
3323
3324 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003325
3326 switch (resolution->status) {
3327 case RSLV_STATUS_NONE:
3328 /* status when HAProxy has just (re)started */
3329 trigger_resolution(s);
3330 break;
3331
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003332 case RSLV_STATUS_VALID:
3333 /*
3334 * resume health checks
3335 * server will be turned back on if health check is safe
3336 */
3337 if (!(s->admin & SRV_ADMF_RMAINT))
3338 return 1;
3339 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3340 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3341 s->proxy->id, s->id);
3342
3343 Warning("%s.\n", trash.str);
3344 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3345 return 0;
3346
3347 case RSLV_STATUS_NX:
3348 /* stop server if resolution is NX for a long enough period */
3349 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3350 if (s->admin & SRV_ADMF_RMAINT)
3351 return 1;
3352 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3353 return 0;
3354 }
3355 break;
3356
3357 case RSLV_STATUS_TIMEOUT:
3358 /* stop server if resolution is TIMEOUT for a long enough period */
3359 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3360 if (s->admin & SRV_ADMF_RMAINT)
3361 return 1;
3362 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3363 return 0;
3364 }
3365 break;
3366
3367 case RSLV_STATUS_REFUSED:
3368 /* stop server if resolution is REFUSED for a long enough period */
3369 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3370 if (s->admin & SRV_ADMF_RMAINT)
3371 return 1;
3372 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3373 return 0;
3374 }
3375 break;
3376
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003377 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003378 /* stop server if resolution is in unmatched error for a long enough period */
3379 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3380 if (s->admin & SRV_ADMF_RMAINT)
3381 return 1;
3382 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3383 return 0;
3384 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003385 break;
3386 }
3387
3388 return 1;
3389}
3390
3391/*
3392 * Server Name Resolution valid response callback
3393 * It expects:
3394 * - <nameserver>: the name server which answered the valid response
3395 * - <response>: buffer containing a valid DNS response
3396 * - <response_len>: size of <response>
3397 * It performs the following actions:
3398 * - ignore response if current ip found and server family not met
3399 * - update with first new ip found if family is met and current IP is not found
3400 * returns:
3401 * 0 on error
3402 * 1 when no error or safe ignore
3403 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003404int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003405{
3406 struct server *s;
3407 void *serverip, *firstip;
3408 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003409 int ret;
3410 struct chunk *chk = get_trash_chunk();
3411
3412 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003413 firstip = NULL; /* pointer to the first valid response found */
3414 /* it will be used as the new IP if a change is required */
3415 firstip_sin_family = AF_UNSPEC;
3416 serverip = NULL; /* current server IP address */
3417
3418 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003419 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003420
3421 /* initializing server IP pointer */
3422 server_sin_family = s->addr.ss_family;
3423 switch (server_sin_family) {
3424 case AF_INET:
3425 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3426 break;
3427
3428 case AF_INET6:
3429 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3430 break;
3431
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003432 case AF_UNSPEC:
3433 break;
3434
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003435 default:
3436 goto invalid;
3437 }
3438
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003439 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003440 serverip, server_sin_family, &firstip,
3441 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003442
3443 switch (ret) {
3444 case DNS_UPD_NO:
3445 if (resolution->status != RSLV_STATUS_VALID) {
3446 resolution->status = RSLV_STATUS_VALID;
3447 resolution->last_status_change = now_ms;
3448 }
3449 goto stop_resolution;
3450
3451 case DNS_UPD_SRVIP_NOT_FOUND:
3452 goto save_ip;
3453
3454 case DNS_UPD_CNAME:
3455 if (resolution->status != RSLV_STATUS_VALID) {
3456 resolution->status = RSLV_STATUS_VALID;
3457 resolution->last_status_change = now_ms;
3458 }
3459 goto invalid;
3460
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003461 case DNS_UPD_NO_IP_FOUND:
3462 if (resolution->status != RSLV_STATUS_OTHER) {
3463 resolution->status = RSLV_STATUS_OTHER;
3464 resolution->last_status_change = now_ms;
3465 }
3466 goto stop_resolution;
3467
Baptiste Assmannfad03182015-10-28 02:03:32 +01003468 case DNS_UPD_NAME_ERROR:
3469 /* if this is not the last expected response, we ignore it */
3470 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3471 return 0;
3472 /* update resolution status to OTHER error type */
3473 if (resolution->status != RSLV_STATUS_OTHER) {
3474 resolution->status = RSLV_STATUS_OTHER;
3475 resolution->last_status_change = now_ms;
3476 }
3477 goto stop_resolution;
3478
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003479 default:
3480 goto invalid;
3481
3482 }
3483
3484 save_ip:
3485 nameserver->counters.update += 1;
3486 if (resolution->status != RSLV_STATUS_VALID) {
3487 resolution->status = RSLV_STATUS_VALID;
3488 resolution->last_status_change = now_ms;
3489 }
3490
3491 /* save the first ip we found */
3492 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3493 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3494
3495 stop_resolution:
3496 /* update last resolution date and time */
3497 resolution->last_resolution = now_ms;
3498 /* reset current status flag */
3499 resolution->step = RSLV_STEP_NONE;
3500 /* reset values */
3501 dns_reset_resolution(resolution);
3502
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003503 dns_update_resolvers_timeout(nameserver->resolvers);
3504
3505 snr_update_srv_status(s);
3506 return 0;
3507
3508 invalid:
3509 nameserver->counters.invalid += 1;
3510 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3511 goto stop_resolution;
3512
3513 snr_update_srv_status(s);
3514 return 0;
3515}
3516
3517/*
3518 * Server Name Resolution error management callback
3519 * returns:
3520 * 0 on error
3521 * 1 when no error or safe ignore
3522 */
3523int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3524{
3525 struct server *s;
3526 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003527 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003528
3529 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003530 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003531 resolvers = resolution->resolvers;
3532
3533 /* can be ignored if this is not the last response */
3534 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3535 return 1;
3536 }
3537
3538 switch (error_code) {
3539 case DNS_RESP_INVALID:
3540 case DNS_RESP_WRONG_NAME:
3541 if (resolution->status != RSLV_STATUS_INVALID) {
3542 resolution->status = RSLV_STATUS_INVALID;
3543 resolution->last_status_change = now_ms;
3544 }
3545 break;
3546
3547 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003548 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003549 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003550 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003551 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003552 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3553 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003554
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003555 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003556 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003557 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003558 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003559 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003560 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003561 }
3562 else if (res_preferred_afinet) {
3563 /* fallback from A to AAAA */
3564 resolution->query_type = DNS_RTYPE_AAAA;
3565 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003566 else {
3567 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003568 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003569 resolution->query_type = DNS_RTYPE_A;
3570 } else {
3571 resolution->query_type = DNS_RTYPE_AAAA;
3572 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003573 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003574
3575 dns_send_query(resolution);
3576
3577 /*
3578 * move the resolution to the last element of the FIFO queue
3579 * and update timeout wakeup based on the new first entry
3580 */
3581 if (dns_check_resolution_queue(resolvers) > 1) {
3582 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003583 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003584 /* ex first resolution goes to the end of the queue */
3585 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3586 }
3587 dns_update_resolvers_timeout(resolvers);
3588 goto leave;
3589 }
3590 else {
3591 if (resolution->status != RSLV_STATUS_OTHER) {
3592 resolution->status = RSLV_STATUS_OTHER;
3593 resolution->last_status_change = now_ms;
3594 }
3595 }
3596 break;
3597
3598 case DNS_RESP_NX_DOMAIN:
3599 if (resolution->status != RSLV_STATUS_NX) {
3600 resolution->status = RSLV_STATUS_NX;
3601 resolution->last_status_change = now_ms;
3602 }
3603 break;
3604
3605 case DNS_RESP_REFUSED:
3606 if (resolution->status != RSLV_STATUS_REFUSED) {
3607 resolution->status = RSLV_STATUS_REFUSED;
3608 resolution->last_status_change = now_ms;
3609 }
3610 break;
3611
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003612 case DNS_RESP_TIMEOUT:
3613 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3614 resolution->status = RSLV_STATUS_TIMEOUT;
3615 resolution->last_status_change = now_ms;
3616 }
3617 break;
3618 }
3619
3620 /* update last resolution date and time */
3621 resolution->last_resolution = now_ms;
3622 /* reset current status flag */
3623 resolution->step = RSLV_STEP_NONE;
3624 /* reset values */
3625 dns_reset_resolution(resolution);
3626
3627 LIST_DEL(&resolution->list);
3628 dns_update_resolvers_timeout(resolvers);
3629
3630 leave:
3631 snr_update_srv_status(s);
3632 return 1;
3633}
3634
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003635/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3636 * resolver. This is suited for initial address configuration. Returns 0 on
3637 * success otherwise a non-zero error code. In case of error, *err_code, if
3638 * not NULL, is filled up.
3639 */
3640int srv_set_addr_via_libc(struct server *srv, int *err_code)
3641{
3642 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003643 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003644 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003645 return 1;
3646 }
3647 return 0;
3648}
3649
3650/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3651 * from the state file. This is suited for initial address configuration.
3652 * Returns 0 on success otherwise a non-zero error code. In case of error,
3653 * *err_code, if not NULL, is filled up.
3654 */
3655static int srv_apply_lastaddr(struct server *srv, int *err_code)
3656{
3657 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3658 if (err_code)
3659 *err_code |= ERR_WARN;
3660 return 1;
3661 }
3662 return 0;
3663}
3664
Willy Tarreau25e51522016-11-04 15:10:17 +01003665/* returns 0 if no error, otherwise a combination of ERR_* flags */
3666static int srv_iterate_initaddr(struct server *srv)
3667{
3668 int return_code = 0;
3669 int err_code;
3670 unsigned int methods;
3671
3672 methods = srv->init_addr_methods;
3673 if (!methods) { // default to "last,libc"
3674 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3675 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3676 }
3677
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003678 /* "-dr" : always append "none" so that server addresses resolution
3679 * failures are silently ignored, this is convenient to validate some
3680 * configs out of their environment.
3681 */
3682 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3683 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3684
Willy Tarreau25e51522016-11-04 15:10:17 +01003685 while (methods) {
3686 err_code = 0;
3687 switch (srv_get_next_initaddr(&methods)) {
3688 case SRV_IADDR_LAST:
3689 if (!srv->lastaddr)
3690 continue;
3691 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003692 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003693 return_code |= err_code;
3694 break;
3695
3696 case SRV_IADDR_LIBC:
3697 if (!srv->hostname)
3698 continue;
3699 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003700 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003701 return_code |= err_code;
3702 break;
3703
Willy Tarreau37ebe122016-11-04 15:17:58 +01003704 case SRV_IADDR_NONE:
3705 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003706 if (return_code) {
3707 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3708 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3709 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003710 return return_code;
3711
Willy Tarreau4310d362016-11-02 15:05:56 +01003712 case SRV_IADDR_IP:
3713 ipcpy(&srv->init_addr, &srv->addr);
3714 if (return_code) {
3715 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3716 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3717 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003718 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003719
Willy Tarreau25e51522016-11-04 15:10:17 +01003720 default: /* unhandled method */
3721 break;
3722 }
3723 }
3724
3725 if (!return_code) {
3726 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3727 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3728 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003729 else {
3730 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3731 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3732 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003733
3734 return_code |= ERR_ALERT | ERR_FATAL;
3735 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003736out:
3737 srv_set_dyncookie(srv);
3738 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003739}
3740
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003741/*
3742 * This function parses all backends and all servers within each backend
3743 * and performs servers' addr resolution based on information provided by:
3744 * - configuration file
3745 * - server-state file (states provided by an 'old' haproxy process)
3746 *
3747 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3748 */
3749int srv_init_addr(void)
3750{
3751 struct proxy *curproxy;
3752 int return_code = 0;
3753
3754 curproxy = proxy;
3755 while (curproxy) {
3756 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003757
3758 /* servers are in backend only */
3759 if (!(curproxy->cap & PR_CAP_BE))
3760 goto srv_init_addr_next;
3761
Willy Tarreau25e51522016-11-04 15:10:17 +01003762 for (srv = curproxy->srv; srv; srv = srv->next)
3763 if (srv->hostname)
3764 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003765
3766 srv_init_addr_next:
3767 curproxy = curproxy->next;
3768 }
3769
3770 return return_code;
3771}
3772
Willy Tarreau21b069d2016-11-23 17:15:08 +01003773/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3774 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003775 * 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 +01003776 * used for CLI commands requiring a server name.
3777 * Important: the <arg> is modified to remove the '/'.
3778 */
3779struct server *cli_find_server(struct appctx *appctx, char *arg)
3780{
3781 struct proxy *px;
3782 struct server *sv;
3783 char *line;
3784
3785 /* split "backend/server" and make <line> point to server */
3786 for (line = arg; *line; line++)
3787 if (*line == '/') {
3788 *line++ = '\0';
3789 break;
3790 }
3791
3792 if (!*line || !*arg) {
3793 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003794 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003795 return NULL;
3796 }
3797
3798 if (!get_backend_server(arg, line, &px, &sv)) {
3799 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003800 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003801 return NULL;
3802 }
3803
3804 if (px->state == PR_STSTOPPED) {
3805 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003806 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003807 return NULL;
3808 }
3809
3810 return sv;
3811}
3812
William Lallemand222baf22016-11-19 02:00:33 +01003813
3814static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3815{
3816 struct server *sv;
3817 const char *warning;
3818
3819 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3820 return 1;
3821
3822 sv = cli_find_server(appctx, args[2]);
3823 if (!sv)
3824 return 1;
3825
3826 if (strcmp(args[3], "weight") == 0) {
3827 warning = server_parse_weight_change_request(sv, args[4]);
3828 if (warning) {
3829 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003830 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003831 }
3832 }
3833 else if (strcmp(args[3], "state") == 0) {
3834 if (strcmp(args[4], "ready") == 0)
3835 srv_adm_set_ready(sv);
3836 else if (strcmp(args[4], "drain") == 0)
3837 srv_adm_set_drain(sv);
3838 else if (strcmp(args[4], "maint") == 0)
3839 srv_adm_set_maint(sv);
3840 else {
3841 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003842 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003843 }
3844 }
3845 else if (strcmp(args[3], "health") == 0) {
3846 if (sv->track) {
3847 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003848 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003849 }
3850 else if (strcmp(args[4], "up") == 0) {
3851 sv->check.health = sv->check.rise + sv->check.fall - 1;
3852 srv_set_running(sv, "changed from CLI");
3853 }
3854 else if (strcmp(args[4], "stopping") == 0) {
3855 sv->check.health = sv->check.rise + sv->check.fall - 1;
3856 srv_set_stopping(sv, "changed from CLI");
3857 }
3858 else if (strcmp(args[4], "down") == 0) {
3859 sv->check.health = 0;
3860 srv_set_stopped(sv, "changed from CLI");
3861 }
3862 else {
3863 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003864 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003865 }
3866 }
3867 else if (strcmp(args[3], "agent") == 0) {
3868 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3869 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003870 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003871 }
3872 else if (strcmp(args[4], "up") == 0) {
3873 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3874 srv_set_running(sv, "changed from CLI");
3875 }
3876 else if (strcmp(args[4], "down") == 0) {
3877 sv->agent.health = 0;
3878 srv_set_stopped(sv, "changed from CLI");
3879 }
3880 else {
3881 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003882 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003883 }
3884 }
Misiek2da082d2017-01-09 09:40:42 +01003885 else if (strcmp(args[3], "agent-addr") == 0) {
3886 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3887 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3888 appctx->st0 = CLI_ST_PRINT;
3889 } else {
3890 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3891 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3892 appctx->st0 = CLI_ST_PRINT;
3893 }
3894 }
3895 }
3896 else if (strcmp(args[3], "agent-send") == 0) {
3897 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3898 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3899 appctx->st0 = CLI_ST_PRINT;
3900 } else {
3901 char *nss = strdup(args[4]);
3902 if (!nss) {
3903 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3904 appctx->st0 = CLI_ST_PRINT;
3905 } else {
3906 free(sv->agent.send_string);
3907 sv->agent.send_string = nss;
3908 sv->agent.send_string_len = strlen(args[4]);
3909 }
3910 }
3911 }
William Lallemand222baf22016-11-19 02:00:33 +01003912 else if (strcmp(args[3], "check-port") == 0) {
3913 int i = 0;
3914 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3915 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003916 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003917 }
3918 if ((i < 0) || (i > 65535)) {
3919 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003920 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003921 }
3922 /* prevent the update of port to 0 if MAPPORTS are in use */
3923 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3924 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003925 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003926 return 1;
3927 }
3928 sv->check.port = i;
3929 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003930 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003931 }
3932 else if (strcmp(args[3], "addr") == 0) {
3933 char *addr = NULL;
3934 char *port = NULL;
3935 if (strlen(args[4]) == 0) {
3936 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003937 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003938 return 1;
3939 }
3940 else {
3941 addr = args[4];
3942 }
3943 if (strcmp(args[5], "port") == 0) {
3944 port = args[6];
3945 }
3946 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3947 if (warning) {
3948 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003949 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003950 }
3951 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3952 }
3953 else {
3954 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 +01003955 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003956 }
3957 return 1;
3958}
3959
William Lallemand6b160942016-11-22 12:34:35 +01003960static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3961{
3962 struct stream_interface *si = appctx->owner;
3963 struct proxy *px;
3964 struct server *sv;
3965 char *line;
3966
3967
3968 /* split "backend/server" and make <line> point to server */
3969 for (line = args[2]; *line; line++)
3970 if (*line == '/') {
3971 *line++ = '\0';
3972 break;
3973 }
3974
3975 if (!*line) {
3976 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003977 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003978 return 1;
3979 }
3980
3981 if (!get_backend_server(args[2], line, &px, &sv)) {
3982 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003983 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003984 return 1;
3985 }
3986
3987 /* return server's effective weight at the moment */
3988 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003989 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01003990 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003991 return 0;
3992 }
William Lallemand6b160942016-11-22 12:34:35 +01003993 return 1;
3994}
3995
3996static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
3997{
3998 struct server *sv;
3999 const char *warning;
4000
4001 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4002 return 1;
4003
4004 sv = cli_find_server(appctx, args[2]);
4005 if (!sv)
4006 return 1;
4007
4008 warning = server_parse_weight_change_request(sv, args[3]);
4009 if (warning) {
4010 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004011 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004012 }
4013 return 1;
4014}
4015
Willy Tarreaub8026272016-11-23 11:26:56 +01004016/* parse a "set maxconn server" command. It always returns 1. */
4017static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4018{
4019 struct server *sv;
4020 const char *warning;
4021
4022 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4023 return 1;
4024
4025 sv = cli_find_server(appctx, args[3]);
4026 if (!sv)
4027 return 1;
4028
4029 warning = server_parse_maxconn_change_request(sv, args[4]);
4030 if (warning) {
4031 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004032 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004033 }
4034 return 1;
4035}
William Lallemand6b160942016-11-22 12:34:35 +01004036
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004037/* parse a "disable agent" command. It always returns 1. */
4038static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4039{
4040 struct server *sv;
4041
4042 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4043 return 1;
4044
4045 sv = cli_find_server(appctx, args[2]);
4046 if (!sv)
4047 return 1;
4048
4049 sv->agent.state &= ~CHK_ST_ENABLED;
4050 return 1;
4051}
4052
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004053/* parse a "disable health" command. It always returns 1. */
4054static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4055{
4056 struct server *sv;
4057
4058 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4059 return 1;
4060
4061 sv = cli_find_server(appctx, args[2]);
4062 if (!sv)
4063 return 1;
4064
4065 sv->check.state &= ~CHK_ST_ENABLED;
4066 return 1;
4067}
4068
Willy Tarreauffb4d582016-11-24 12:47:00 +01004069/* parse a "disable server" command. It always returns 1. */
4070static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4071{
4072 struct server *sv;
4073
4074 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4075 return 1;
4076
4077 sv = cli_find_server(appctx, args[2]);
4078 if (!sv)
4079 return 1;
4080
4081 srv_adm_set_maint(sv);
4082 return 1;
4083}
4084
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004085/* parse a "enable agent" command. It always returns 1. */
4086static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4087{
4088 struct server *sv;
4089
4090 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4091 return 1;
4092
4093 sv = cli_find_server(appctx, args[2]);
4094 if (!sv)
4095 return 1;
4096
4097 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4098 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004099 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004100 return 1;
4101 }
4102
4103 sv->agent.state |= CHK_ST_ENABLED;
4104 return 1;
4105}
4106
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004107/* parse a "enable health" command. It always returns 1. */
4108static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4109{
4110 struct server *sv;
4111
4112 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4113 return 1;
4114
4115 sv = cli_find_server(appctx, args[2]);
4116 if (!sv)
4117 return 1;
4118
4119 sv->check.state |= CHK_ST_ENABLED;
4120 return 1;
4121}
4122
Willy Tarreauffb4d582016-11-24 12:47:00 +01004123/* parse a "enable server" command. It always returns 1. */
4124static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4125{
4126 struct server *sv;
4127
4128 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4129 return 1;
4130
4131 sv = cli_find_server(appctx, args[2]);
4132 if (!sv)
4133 return 1;
4134
4135 srv_adm_set_ready(sv);
4136 return 1;
4137}
4138
William Lallemand222baf22016-11-19 02:00:33 +01004139/* register cli keywords */
4140static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004141 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004142 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004143 { { "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 +01004144 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004145 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004146 { { "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 +01004147 { { "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 +01004148 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004149 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4150 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4151
William Lallemand222baf22016-11-19 02:00:33 +01004152 {{},}
4153}};
4154
4155__attribute__((constructor))
4156static void __server_init(void)
4157{
4158 cli_register_kw(&cli_kws);
4159}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004160
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004161/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004162 * Local variables:
4163 * c-indent-level: 8
4164 * c-basic-offset: 8
4165 * End:
4166 */