blob: b69d1d1d025ba21772caebb78997f3a819269831 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020035#include <proto/port_range.h>
36#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020037#include <proto/queue.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020038#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020039#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010040#include <proto/stream_interface.h>
41#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020042#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020043#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020044
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020045static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010046static int srv_apply_lastaddr(struct server *srv, int *err_code);
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau21faa912012-10-10 08:27:36 +020048/* List head of all known server keywords */
49static struct srv_kw_list srv_keywords = {
50 .list = LIST_HEAD_INIT(srv_keywords.list)
51};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020052
Simon Hormana3608442013-11-01 16:46:15 +090053int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020054{
Willy Tarreau892337c2014-05-13 23:41:20 +020055 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020056 return s->down_time;
57
58 return now.tv_sec - s->last_change + s->down_time;
59}
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050061int srv_lastsession(const struct server *s)
62{
63 if (s->counters.last_sess)
64 return now.tv_sec - s->counters.last_sess;
65
66 return -1;
67}
68
Simon Horman4a741432013-02-23 15:35:38 +090069int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020070{
Simon Horman4a741432013-02-23 15:35:38 +090071 const struct server *s = check->server;
72
Willy Tarreauff5ae352013-12-11 20:36:34 +010073 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090074 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010075
Willy Tarreau892337c2014-05-13 23:41:20 +020076 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090077 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010078
Simon Horman4a741432013-02-23 15:35:38 +090079 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080}
81
Olivier Houchard4e694042017-03-14 20:01:29 +010082void srv_set_dyncookie(struct server *s)
83{
84 struct proxy *p = s->proxy;
85 struct server *tmpserv;
86 char *tmpbuf;
87 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010088 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010089 size_t buffer_len;
90 int addr_len;
91 int port;
92
93 if ((s->flags & SRV_F_COOKIESET) ||
94 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
95 s->proxy->dyncookie_key == NULL)
96 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010097 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +010098
99 if (s->addr.ss_family != AF_INET &&
100 s->addr.ss_family != AF_INET6)
101 return;
102 /*
103 * Buffer to calculate the cookie value.
104 * The buffer contains the secret key + the server IP address
105 * + the TCP port.
106 */
107 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
108 /*
109 * The TCP port should use only 2 bytes, but is stored in
110 * an unsigned int in struct server, so let's use 4, to be
111 * on the safe side.
112 */
113 buffer_len = key_len + addr_len + 4;
114 tmpbuf = trash.str;
115 memcpy(tmpbuf, p->dyncookie_key, key_len);
116 memcpy(&(tmpbuf[key_len]),
117 s->addr.ss_family == AF_INET ?
118 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
119 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
120 addr_len);
121 /*
122 * Make sure it's the same across all the load balancers,
123 * no matter their endianness.
124 */
125 port = htonl(s->svc_port);
126 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
127 hash_value = XXH64(tmpbuf, buffer_len, 0);
128 memprintf(&s->cookie, "%016llx", hash_value);
129 if (!s->cookie)
130 return;
131 s->cklen = 16;
132 /*
133 * Check that we did not get a hash collision.
134 * Unlikely, but it can happen.
135 */
136 for (p = proxy; p != NULL; p = p->next)
137 for (tmpserv = proxy->srv; tmpserv != NULL;
138 tmpserv = tmpserv->next) {
139 if (tmpserv == s)
140 continue;
141 if (tmpserv->cookie &&
142 strcmp(tmpserv->cookie, s->cookie) == 0) {
143 Warning("We generated two equal cookies for two different servers.\n"
144 "Please change the secret key for '%s'.\n",
145 s->proxy->id);
146 }
147 }
148}
149
Willy Tarreau21faa912012-10-10 08:27:36 +0200150/*
151 * Registers the server keyword list <kwl> as a list of valid keywords for next
152 * parsing sessions.
153 */
154void srv_register_keywords(struct srv_kw_list *kwl)
155{
156 LIST_ADDQ(&srv_keywords.list, &kwl->list);
157}
158
159/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
160 * keyword is found with a NULL ->parse() function, then an attempt is made to
161 * find one with a valid ->parse() function. This way it is possible to declare
162 * platform-dependant, known keywords as NULL, then only declare them as valid
163 * if some options are met. Note that if the requested keyword contains an
164 * opening parenthesis, everything from this point is ignored.
165 */
166struct srv_kw *srv_find_kw(const char *kw)
167{
168 int index;
169 const char *kwend;
170 struct srv_kw_list *kwl;
171 struct srv_kw *ret = NULL;
172
173 kwend = strchr(kw, '(');
174 if (!kwend)
175 kwend = kw + strlen(kw);
176
177 list_for_each_entry(kwl, &srv_keywords.list, list) {
178 for (index = 0; kwl->kw[index].kw != NULL; index++) {
179 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
180 kwl->kw[index].kw[kwend-kw] == 0) {
181 if (kwl->kw[index].parse)
182 return &kwl->kw[index]; /* found it !*/
183 else
184 ret = &kwl->kw[index]; /* may be OK */
185 }
186 }
187 }
188 return ret;
189}
190
191/* Dumps all registered "server" keywords to the <out> string pointer. The
192 * unsupported keywords are only dumped if their supported form was not
193 * found.
194 */
195void srv_dump_kws(char **out)
196{
197 struct srv_kw_list *kwl;
198 int index;
199
200 *out = NULL;
201 list_for_each_entry(kwl, &srv_keywords.list, list) {
202 for (index = 0; kwl->kw[index].kw != NULL; index++) {
203 if (kwl->kw[index].parse ||
204 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
205 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
206 kwl->scope,
207 kwl->kw[index].kw,
208 kwl->kw[index].skip ? " <arg>" : "",
209 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
210 kwl->kw[index].parse ? "" : " (not supported)");
211 }
212 }
213 }
214}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100215
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100216/* Parse the "backup" server keyword */
217static int srv_parse_backup(char **args, int *cur_arg,
218 struct proxy *curproxy, struct server *newsrv, char **err)
219{
220 newsrv->flags |= SRV_F_BACKUP;
221 return 0;
222}
223
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100224/* Parse the "check-send-proxy" server keyword */
225static int srv_parse_check_send_proxy(char **args, int *cur_arg,
226 struct proxy *curproxy, struct server *newsrv, char **err)
227{
228 newsrv->check.send_proxy = 1;
229 return 0;
230}
231
Willy Tarreaudff55432012-10-10 17:51:05 +0200232/* parse the "id" server keyword */
233static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
234{
235 struct eb32_node *node;
236
237 if (!*args[*cur_arg + 1]) {
238 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
239 return ERR_ALERT | ERR_FATAL;
240 }
241
242 newsrv->puid = atol(args[*cur_arg + 1]);
243 newsrv->conf.id.key = newsrv->puid;
244
245 if (newsrv->puid <= 0) {
246 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
247 return ERR_ALERT | ERR_FATAL;
248 }
249
250 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
251 if (node) {
252 struct server *target = container_of(node, struct server, conf.id);
253 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
254 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
255 target->id);
256 return ERR_ALERT | ERR_FATAL;
257 }
258
259 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200260 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200261 return 0;
262}
263
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100264/* Parse the "no-backup" server keyword */
265static int srv_parse_no_backup(char **args, int *cur_arg,
266 struct proxy *curproxy, struct server *newsrv, char **err)
267{
268 newsrv->flags &= ~SRV_F_BACKUP;
269 return 0;
270}
271
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100272/* Parse the "no-check-send-proxy" server keyword */
273static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
274 struct proxy *curproxy, struct server *newsrv, char **err)
275{
276 newsrv->check.send_proxy = 0;
277 return 0;
278}
279
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100280/* Disable server PROXY protocol flags. */
281static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
282{
283 srv->pp_opts &= ~flags;
284 return 0;
285}
286
287/* Parse the "no-send-proxy" server keyword */
288static int srv_parse_no_send_proxy(char **args, int *cur_arg,
289 struct proxy *curproxy, struct server *newsrv, char **err)
290{
291 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
292}
293
294/* Parse the "no-send-proxy-v2" server keyword */
295static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
296 struct proxy *curproxy, struct server *newsrv, char **err)
297{
298 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
299}
300
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100301/* Parse the "non-stick" server keyword */
302static int srv_parse_non_stick(char **args, int *cur_arg,
303 struct proxy *curproxy, struct server *newsrv, char **err)
304{
305 newsrv->flags |= SRV_F_NON_STICK;
306 return 0;
307}
308
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100309/* Enable server PROXY protocol flags. */
310static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
311{
312 srv->pp_opts |= flags;
313 return 0;
314}
315
316/* Parse the "send-proxy" server keyword */
317static int srv_parse_send_proxy(char **args, int *cur_arg,
318 struct proxy *curproxy, struct server *newsrv, char **err)
319{
320 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
321}
322
323/* Parse the "send-proxy-v2" server keyword */
324static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
325 struct proxy *curproxy, struct server *newsrv, char **err)
326{
327 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
328}
329
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100330/* Parse the "stick" server keyword */
331static int srv_parse_stick(char **args, int *cur_arg,
332 struct proxy *curproxy, struct server *newsrv, char **err)
333{
334 newsrv->flags &= ~SRV_F_NON_STICK;
335 return 0;
336}
337
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200338/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200339 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200340 * shutdown.
341 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200342void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200343{
Willy Tarreau87b09662015-04-03 00:22:06 +0200344 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200345
Willy Tarreau87b09662015-04-03 00:22:06 +0200346 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
347 if (stream->srv_conn == srv)
348 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200349}
350
351/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200352 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200353 * the reason for the shutdown.
354 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200355void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200356{
357 struct server *srv;
358
359 for (srv = px->srv; srv != NULL; srv = srv->next)
360 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200361 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200362}
363
Willy Tarreaubda92272014-05-20 21:55:30 +0200364/* Appends some information to a message string related to a server going UP or
365 * DOWN. If both <forced> and <reason> are null and the server tracks another
366 * one, a "via" information will be provided to know where the status came from.
367 * If <reason> is non-null, the entire string will be appended after a comma and
368 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200369 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200370 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200371 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200372void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200373{
Willy Tarreaubda92272014-05-20 21:55:30 +0200374 if (reason)
375 chunk_appendf(msg, ", %s", reason);
376 else if (!forced && s->track)
377 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200378
379 if (xferred >= 0) {
380 if (s->state == SRV_ST_STOPPED)
381 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
382 " %d sessions active, %d requeued, %d remaining in queue",
383 s->proxy->srv_act, s->proxy->srv_bck,
384 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
385 s->cur_sess, xferred, s->nbpend);
386 else
387 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
388 " %d sessions requeued, %d total in queue",
389 s->proxy->srv_act, s->proxy->srv_bck,
390 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
391 xferred, s->nbpend);
392 }
393}
394
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200395/* Marks server <s> down, regardless of its checks' statuses, notifies by all
396 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200397 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200398 * recomputes the number of servers, but not the map. Maintenance servers are
399 * ignored. It reports <reason> if non-null as the reason for going down. Note
400 * that it makes use of the trash to build the log strings, so <reason> must
401 * not be placed there.
402 */
403void srv_set_stopped(struct server *s, const char *reason)
404{
405 struct server *srv;
406 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
407 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900408 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200409 int xferred;
410
411 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
412 return;
413
414 s->last_change = now.tv_sec;
415 s->state = SRV_ST_STOPPED;
416 if (s->proxy->lbprm.set_server_status_down)
417 s->proxy->lbprm.set_server_status_down(s);
418
419 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200420 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200421
Willy Tarreau87b09662015-04-03 00:22:06 +0200422 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200423 * a connection. Those which are redispatchable will be queued
424 * to another server or to the proxy itself.
425 */
426 xferred = pendconn_redistribute(s);
427
428 chunk_printf(&trash,
429 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
430 s->proxy->id, s->id);
431
432 srv_append_status(&trash, s, reason, xferred, 0);
433 Warning("%s.\n", trash.str);
434
435 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900436 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
437 send_log(s->proxy, log_level, "%s.\n", trash.str);
438 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200439
440 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
441 set_backend_down(s->proxy);
442
443 s->counters.down_trans++;
444
445 for (srv = s->trackers; srv; srv = srv->tracknext)
446 srv_set_stopped(srv, NULL);
447}
448
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200449/* Marks server <s> up regardless of its checks' statuses and provided it isn't
450 * in maintenance. Notifies by all available means, recounts the remaining
451 * servers on the proxy and tries to grab requests from the proxy. It
452 * automatically recomputes the number of servers, but not the map. Maintenance
453 * servers are ignored. It reports <reason> if non-null as the reason for going
454 * up. Note that it makes use of the trash to build the log strings, so <reason>
455 * must not be placed there.
456 */
457void srv_set_running(struct server *s, const char *reason)
458{
459 struct server *srv;
460 int xferred;
461
462 if (s->admin & SRV_ADMF_MAINT)
463 return;
464
465 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
466 return;
467
468 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
469 if (s->proxy->last_change < now.tv_sec) // ignore negative times
470 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
471 s->proxy->last_change = now.tv_sec;
472 }
473
474 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
475 s->down_time += now.tv_sec - s->last_change;
476
477 s->last_change = now.tv_sec;
478
479 s->state = SRV_ST_STARTING;
480 if (s->slowstart > 0)
481 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
482 else
483 s->state = SRV_ST_RUNNING;
484
485 server_recalc_eweight(s);
486
487 /* If the server is set with "on-marked-up shutdown-backup-sessions",
488 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200489 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200490 * on all backup servers.
491 */
492 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
493 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200494 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200495
496 /* check if we can handle some connections queued at the proxy. We
497 * will take as many as we can handle.
498 */
499 xferred = pendconn_grab_from_px(s);
500
501 chunk_printf(&trash,
502 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
503 s->proxy->id, s->id);
504
505 srv_append_status(&trash, s, reason, xferred, 0);
506 Warning("%s.\n", trash.str);
507 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900508 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200509
510 for (srv = s->trackers; srv; srv = srv->tracknext)
511 srv_set_running(srv, NULL);
512}
513
Willy Tarreau8eb77842014-05-21 13:54:57 +0200514/* Marks server <s> stopping regardless of its checks' statuses and provided it
515 * isn't in maintenance. Notifies by all available means, recounts the remaining
516 * servers on the proxy and tries to grab requests from the proxy. It
517 * automatically recomputes the number of servers, but not the map. Maintenance
518 * servers are ignored. It reports <reason> if non-null as the reason for going
519 * up. Note that it makes use of the trash to build the log strings, so <reason>
520 * must not be placed there.
521 */
522void srv_set_stopping(struct server *s, const char *reason)
523{
524 struct server *srv;
525 int xferred;
526
527 if (s->admin & SRV_ADMF_MAINT)
528 return;
529
530 if (s->state == SRV_ST_STOPPING)
531 return;
532
533 s->last_change = now.tv_sec;
534 s->state = SRV_ST_STOPPING;
535 if (s->proxy->lbprm.set_server_status_down)
536 s->proxy->lbprm.set_server_status_down(s);
537
Willy Tarreau87b09662015-04-03 00:22:06 +0200538 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200539 * a connection. Those which are redispatchable will be queued
540 * to another server or to the proxy itself.
541 */
542 xferred = pendconn_redistribute(s);
543
544 chunk_printf(&trash,
545 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
546 s->proxy->id, s->id);
547
548 srv_append_status(&trash, s, reason, xferred, 0);
549
550 Warning("%s.\n", trash.str);
551 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
552
553 if (!s->proxy->srv_bck && !s->proxy->srv_act)
554 set_backend_down(s->proxy);
555
556 for (srv = s->trackers; srv; srv = srv->tracknext)
557 srv_set_stopping(srv, NULL);
558}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200559
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200560/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
561 * enforce either maint mode or drain mode. It is not allowed to set more than
562 * one flag at once. The equivalent "inherited" flag is propagated to all
563 * tracking servers. Maintenance mode disables health checks (but not agent
564 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100565 * is done. If <cause> is non-null, it will be displayed at the end of the log
566 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200567 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100568void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200569{
570 struct check *check = &s->check;
571 struct server *srv;
572 int xferred;
573
574 if (!mode)
575 return;
576
577 /* stop going down as soon as we meet a server already in the same state */
578 if (s->admin & mode)
579 return;
580
581 s->admin |= mode;
582
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200583 /* stop going down if the equivalent flag was already present (forced or inherited) */
584 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
585 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
586 return;
587
588 /* Maintenance must also disable health checks */
589 if (mode & SRV_ADMF_MAINT) {
590 if (s->check.state & CHK_ST_ENABLED) {
591 s->check.state |= CHK_ST_PAUSED;
592 check->health = 0;
593 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200594
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200595 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200596 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100597 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
598 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
599 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200600
Willy Tarreaubda92272014-05-20 21:55:30 +0200601 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200602
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100603 if (!(global.mode & MODE_STARTING)) {
604 Warning("%s.\n", trash.str);
605 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
606 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200607 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200608 else { /* server was still running */
609 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
610 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
611
612 check->health = 0; /* failure */
613 s->last_change = now.tv_sec;
614 s->state = SRV_ST_STOPPED;
615 if (s->proxy->lbprm.set_server_status_down)
616 s->proxy->lbprm.set_server_status_down(s);
617
618 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200619 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200620
Willy Tarreau87b09662015-04-03 00:22:06 +0200621 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200622 * a connection. Those which are redispatchable will be queued
623 * to another server or to the proxy itself.
624 */
625 xferred = pendconn_redistribute(s);
626
627 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100628 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200629 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +0100630 s->proxy->id, s->id,
631 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200632
633 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
634
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100635 if (!(global.mode & MODE_STARTING)) {
636 Warning("%s.\n", trash.str);
637 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
638 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200639
640 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
641 set_backend_down(s->proxy);
642
643 s->counters.down_trans++;
644 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200645 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200646
647 /* drain state is applied only if not yet in maint */
648 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200649 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
650
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200651 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200652 if (s->proxy->lbprm.set_server_status_down)
653 s->proxy->lbprm.set_server_status_down(s);
654
Willy Tarreau87b09662015-04-03 00:22:06 +0200655 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200656 * a connection. Those which are redispatchable will be queued
657 * to another server or to the proxy itself.
658 */
659 xferred = pendconn_redistribute(s);
660
Willy Tarreau8b428482016-11-07 15:53:43 +0100661 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
662 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
663 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200664
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200665 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200666
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100667 if (!(global.mode & MODE_STARTING)) {
668 Warning("%s.\n", trash.str);
669 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
670 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
671 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200672 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
673 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200674 }
675
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200676 /* compute the inherited flag to propagate */
677 if (mode & SRV_ADMF_MAINT)
678 mode = SRV_ADMF_IMAINT;
679 else if (mode & SRV_ADMF_DRAIN)
680 mode = SRV_ADMF_IDRAIN;
681
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200682 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +0100683 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200684}
685
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200686/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
687 * stop enforcing either maint mode or drain mode. It is not allowed to set more
688 * than one flag at once. The equivalent "inherited" flag is propagated to all
689 * tracking servers. Leaving maintenance mode re-enables health checks. When
690 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200691 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200692void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200693{
694 struct check *check = &s->check;
695 struct server *srv;
696 int xferred = -1;
697
698 if (!mode)
699 return;
700
701 /* stop going down as soon as we see the flag is not there anymore */
702 if (!(s->admin & mode))
703 return;
704
705 s->admin &= ~mode;
706
707 if (s->admin & SRV_ADMF_MAINT) {
708 /* remaining in maintenance mode, let's inform precisely about the
709 * situation.
710 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200711 if (mode & SRV_ADMF_FMAINT) {
712 chunk_printf(&trash,
713 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
714 s->flags & SRV_F_BACKUP ? "Backup " : "",
715 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200716
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200717 if (s->track) /* normally it's mandatory here */
718 chunk_appendf(&trash, " via %s/%s",
719 s->track->proxy->id, s->track->id);
720 Warning("%s.\n", trash.str);
721 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
722 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100723 if (mode & SRV_ADMF_RMAINT) {
724 chunk_printf(&trash,
725 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
726 s->flags & SRV_F_BACKUP ? "Backup " : "",
727 s->proxy->id, s->id, s->hostname);
728
729 if (s->track) /* normally it's mandatory here */
730 chunk_appendf(&trash, " via %s/%s",
731 s->track->proxy->id, s->track->id);
732 Warning("%s.\n", trash.str);
733 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
734 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200735 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200736 chunk_printf(&trash,
737 "%sServer %s/%s remains in forced maintenance",
738 s->flags & SRV_F_BACKUP ? "Backup " : "",
739 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200740 Warning("%s.\n", trash.str);
741 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
742 }
743 /* don't report anything when leaving drain mode and remaining in maintenance */
744 }
745 else if (mode & SRV_ADMF_MAINT) {
746 /* OK here we're leaving maintenance, we have many things to check,
747 * because the server might possibly be coming back up depending on
748 * its state. In practice, leaving maintenance means that we should
749 * immediately turn to UP (more or less the slowstart) under the
750 * following conditions :
751 * - server is neither checked nor tracked
752 * - server tracks another server which is not checked
753 * - server tracks another server which is already up
754 * Which sums up as something simpler :
755 * "either the tracking server is up or the server's checks are disabled
756 * or up". Otherwise we only re-enable health checks. There's a special
757 * case associated to the stopping state which can be inherited. Note
758 * that the server might still be in drain mode, which is naturally dealt
759 * with by the lower level functions.
760 */
761
762 if (s->check.state & CHK_ST_ENABLED) {
763 s->check.state &= ~CHK_ST_PAUSED;
764 check->health = check->rise; /* start OK but check immediately */
765 }
766
767 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
768 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
769 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
770 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
771 if (s->proxy->last_change < now.tv_sec) // ignore negative times
772 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
773 s->proxy->last_change = now.tv_sec;
774 }
775
776 if (s->last_change < now.tv_sec) // ignore negative times
777 s->down_time += now.tv_sec - s->last_change;
778 s->last_change = now.tv_sec;
779
780 if (s->track && s->track->state == SRV_ST_STOPPING)
781 s->state = SRV_ST_STOPPING;
782 else {
783 s->state = SRV_ST_STARTING;
784 if (s->slowstart > 0)
785 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
786 else
787 s->state = SRV_ST_RUNNING;
788 }
789
790 server_recalc_eweight(s);
791
792 /* If the server is set with "on-marked-up shutdown-backup-sessions",
793 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200794 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200795 * on all backup servers.
796 */
797 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
798 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200799 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200800
801 /* check if we can handle some connections queued at the proxy. We
802 * will take as many as we can handle.
803 */
804 xferred = pendconn_grab_from_px(s);
805 }
806
807 if (mode & SRV_ADMF_FMAINT) {
808 chunk_printf(&trash,
809 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
810 s->flags & SRV_F_BACKUP ? "Backup " : "",
811 s->proxy->id, s->id,
812 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
813 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200814 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100815 else if (mode & SRV_ADMF_RMAINT) {
816 chunk_printf(&trash,
817 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
818 s->flags & SRV_F_BACKUP ? "Backup " : "",
819 s->proxy->id, s->id, s->hostname,
820 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
821 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
822 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200823 else {
824 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200825 "%sServer %s/%s is %s/%s (leaving maintenance)",
826 s->flags & SRV_F_BACKUP ? "Backup " : "",
827 s->proxy->id, s->id,
828 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
829 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
830 srv_append_status(&trash, s, NULL, xferred, 0);
831 }
832 Warning("%s.\n", trash.str);
833 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
834 }
835 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
836 /* remaining in drain mode after removing one of its flags */
837
838 if (mode & SRV_ADMF_FDRAIN) {
839 chunk_printf(&trash,
840 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200841 s->flags & SRV_F_BACKUP ? "Backup " : "",
842 s->proxy->id, s->id);
843
844 if (s->track) /* normally it's mandatory here */
845 chunk_appendf(&trash, " via %s/%s",
846 s->track->proxy->id, s->track->id);
847 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200848 else {
849 chunk_printf(&trash,
850 "%sServer %s/%s remains in forced drain mode",
851 s->flags & SRV_F_BACKUP ? "Backup " : "",
852 s->proxy->id, s->id);
853 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200854 Warning("%s.\n", trash.str);
855 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200856 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200857 else if (mode & SRV_ADMF_DRAIN) {
858 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200859 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
860 if (s->proxy->last_change < now.tv_sec) // ignore negative times
861 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
862 s->proxy->last_change = now.tv_sec;
863 }
864
865 if (s->last_change < now.tv_sec) // ignore negative times
866 s->down_time += now.tv_sec - s->last_change;
867 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200868 server_recalc_eweight(s);
869
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200870 if (mode & SRV_ADMF_FDRAIN) {
871 chunk_printf(&trash,
872 "%sServer %s/%s is %s (leaving forced drain)",
873 s->flags & SRV_F_BACKUP ? "Backup " : "",
874 s->proxy->id, s->id,
875 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
876 }
877 else {
878 chunk_printf(&trash,
879 "%sServer %s/%s is %s (leaving drain)",
880 s->flags & SRV_F_BACKUP ? "Backup " : "",
881 s->proxy->id, s->id,
882 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
883 if (s->track) /* normally it's mandatory here */
884 chunk_appendf(&trash, " via %s/%s",
885 s->track->proxy->id, s->track->id);
886 }
887 Warning("%s.\n", trash.str);
888 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200889 }
890
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200891 /* stop going down if the equivalent flag is still present (forced or inherited) */
892 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
893 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
894 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200895
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200896 if (mode & SRV_ADMF_MAINT)
897 mode = SRV_ADMF_IMAINT;
898 else if (mode & SRV_ADMF_DRAIN)
899 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200900
901 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200902 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200903}
904
Willy Tarreau757478e2016-11-03 19:22:19 +0100905/* principle: propagate maint and drain to tracking servers. This is useful
906 * upon startup so that inherited states are correct.
907 */
908static void srv_propagate_admin_state(struct server *srv)
909{
910 struct server *srv2;
911
912 if (!srv->trackers)
913 return;
914
915 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
916 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +0100917 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100918
919 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +0100920 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100921 }
922}
923
924/* Compute and propagate the admin states for all servers in proxy <px>.
925 * Only servers *not* tracking another one are considered, because other
926 * ones will be handled when the server they track is visited.
927 */
928void srv_compute_all_admin_states(struct proxy *px)
929{
930 struct server *srv;
931
932 for (srv = px->srv; srv; srv = srv->next) {
933 if (srv->track)
934 continue;
935 srv_propagate_admin_state(srv);
936 }
937}
938
Willy Tarreaudff55432012-10-10 17:51:05 +0200939/* Note: must not be declared <const> as its list will be overwritten.
940 * Please take care of keeping this list alphabetically sorted, doing so helps
941 * all code contributors.
942 * Optional keywords are also declared with a NULL ->parse() function so that
943 * the config parser can report an appropriate error when a known keyword was
944 * not enabled.
945 */
946static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +0100947 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100948 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* enable PROXY protocol for health checks */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +0100949 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
950 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100951 { "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 +0100952 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
953 { "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 +0100954 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100955 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
956 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100957 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Willy Tarreaudff55432012-10-10 17:51:05 +0200958 { NULL, NULL, 0 },
959}};
960
961__attribute__((constructor))
962static void __listener_init(void)
963{
964 srv_register_keywords(&srv_kws);
965}
966
Willy Tarreau004e0452013-11-21 11:22:01 +0100967/* Recomputes the server's eweight based on its state, uweight, the current time,
968 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
969 * state is automatically disabled if the time is elapsed.
970 */
971void server_recalc_eweight(struct server *sv)
972{
973 struct proxy *px = sv->proxy;
974 unsigned w;
975
976 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
977 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200978 if (sv->state == SRV_ST_STARTING)
979 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100980 }
981
982 /* We must take care of not pushing the server to full throttle during slow starts.
983 * It must also start immediately, at least at the minimal step when leaving maintenance.
984 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200985 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100986 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
987 else
988 w = px->lbprm.wdiv;
989
990 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
991
992 /* now propagate the status change to any LB algorithms */
993 if (px->lbprm.update_server_eweight)
994 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200995 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100996 if (px->lbprm.set_server_status_up)
997 px->lbprm.set_server_status_up(sv);
998 }
999 else {
1000 if (px->lbprm.set_server_status_down)
1001 px->lbprm.set_server_status_down(sv);
1002 }
1003}
1004
Willy Tarreaubaaee002006-06-26 02:48:02 +02001005/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001006 * Parses weight_str and configures sv accordingly.
1007 * Returns NULL on success, error message string otherwise.
1008 */
1009const char *server_parse_weight_change_request(struct server *sv,
1010 const char *weight_str)
1011{
1012 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001013 long int w;
1014 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001015
1016 px = sv->proxy;
1017
1018 /* if the weight is terminated with '%', it is set relative to
1019 * the initial weight, otherwise it is absolute.
1020 */
1021 if (!*weight_str)
1022 return "Require <weight> or <weight%>.\n";
1023
Simon Hormanb796afa2013-02-12 10:45:53 +09001024 w = strtol(weight_str, &end, 10);
1025 if (end == weight_str)
1026 return "Empty weight string empty or preceded by garbage";
1027 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001028 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001029 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001030 /* Avoid integer overflow */
1031 if (w > 25600)
1032 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001033 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001034 if (w > 256)
1035 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001036 }
1037 else if (w < 0 || w > 256)
1038 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001039 else if (end[0] != '\0')
1040 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001041
1042 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1043 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1044
1045 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001046 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001047
1048 return NULL;
1049}
1050
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001051/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001052 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1053 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001054 * Returns:
1055 * - error string on error
1056 * - NULL on success
1057 */
1058const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001059 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001060{
1061 unsigned char ip[INET6_ADDRSTRLEN];
1062
1063 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001064 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001065 return NULL;
1066 }
1067 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001068 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001069 return NULL;
1070 }
1071
1072 return "Could not understand IP address format.\n";
1073}
1074
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001075const char *server_parse_maxconn_change_request(struct server *sv,
1076 const char *maxconn_str)
1077{
1078 long int v;
1079 char *end;
1080
1081 if (!*maxconn_str)
1082 return "Require <maxconn>.\n";
1083
1084 v = strtol(maxconn_str, &end, 10);
1085 if (end == maxconn_str)
1086 return "maxconn string empty or preceded by garbage";
1087 else if (end[0] != '\0')
1088 return "Trailing garbage in maxconn string";
1089
1090 if (sv->maxconn == sv->minconn) { // static maxconn
1091 sv->maxconn = sv->minconn = v;
1092 } else { // dynamic maxconn
1093 sv->maxconn = v;
1094 }
1095
1096 if (may_dequeue_tasks(sv, sv->proxy))
1097 process_srv_queue(sv);
1098
1099 return NULL;
1100}
1101
Willy Tarreau272adea2014-03-31 10:39:59 +02001102int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1103{
1104 struct server *newsrv = NULL;
1105 const char *err;
1106 char *errmsg = NULL;
1107 int err_code = 0;
1108 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001109 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001110
1111 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
1112 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02001113 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
1114
1115 if (!defsrv && curproxy == defproxy) {
1116 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1117 err_code |= ERR_ALERT | ERR_FATAL;
1118 goto out;
1119 }
1120 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1121 err_code |= ERR_ALERT | ERR_FATAL;
1122
Frédéric Lécaille8065b6d2017-03-09 14:01:02 +01001123 if (!defsrv && !*args[2]) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001124 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1125 file, linenum, args[0]);
1126 err_code |= ERR_ALERT | ERR_FATAL;
1127 goto out;
1128 }
1129
1130 err = invalid_char(args[1]);
1131 if (err && !defsrv) {
1132 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1133 file, linenum, *err, args[1]);
1134 err_code |= ERR_ALERT | ERR_FATAL;
1135 goto out;
1136 }
1137
1138 if (!defsrv) {
1139 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001140 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001141 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001142 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02001143
Vincent Bernat02779b62016-04-03 13:48:43 +02001144 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001145 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1146 err_code |= ERR_ALERT | ERR_ABORT;
1147 goto out;
1148 }
1149
1150 /* the servers are linked backwards first */
1151 newsrv->next = curproxy->srv;
1152 curproxy->srv = newsrv;
1153 newsrv->proxy = curproxy;
1154 newsrv->conf.file = strdup(file);
1155 newsrv->conf.line = linenum;
1156
1157 newsrv->obj_type = OBJ_TYPE_SERVER;
1158 LIST_INIT(&newsrv->actconns);
1159 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02001160 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02001161 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02001162 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +02001163 do_check = 0;
1164 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +02001165 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +02001166 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +02001167 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +02001168 newsrv->last_change = now.tv_sec;
1169 newsrv->id = strdup(args[1]);
1170
1171 /* several ways to check the port component :
1172 * - IP => port=+0, relative (IPv4 only)
1173 * - IP: => port=+0, relative
1174 * - IP:N => port=N, absolute
1175 * - IP:+N => port=+N, relative
1176 * - IP:-N => port=-N, relative
1177 */
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001178 sk = str2sa_range(args[2], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001179 if (!sk) {
1180 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1181 err_code |= ERR_ALERT | ERR_FATAL;
1182 goto out;
1183 }
1184
1185 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001186 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001187 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1188 file, linenum, args[0], args[1]);
1189 err_code |= ERR_ALERT | ERR_FATAL;
1190 goto out;
1191 }
1192
1193 if (!port1 || !port2) {
1194 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001195 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001196 }
1197 else if (port1 != port2) {
1198 /* port range */
1199 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1200 file, linenum, args[0], args[1], args[2]);
1201 err_code |= ERR_ALERT | ERR_FATAL;
1202 goto out;
1203 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001204
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001205 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001206 newsrv->hostname = fqdn;
1207 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001208 goto skip_name_resolution;
1209
Willy Tarreau07101d52015-09-08 16:16:35 +02001210 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001211 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001212 goto skip_name_resolution;
1213
1214 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1215 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1216 goto skip_name_resolution;
1217 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1218 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1219 file, linenum, args[2]);
1220 err_code |= ERR_ALERT | ERR_FATAL;
1221 goto out;
1222 }
1223
1224 curr_resolution->requester = newsrv;
1225 curr_resolution->requester_cb = snr_resolution_cb;
1226 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1227 curr_resolution->status = RSLV_STATUS_NONE;
1228 curr_resolution->step = RSLV_STEP_NONE;
1229 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001230 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001231 newsrv->resolution = curr_resolution;
1232
1233 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001234 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001235 newsrv->svc_port = port;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001236 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = xprt_get(XPRT_RAW);
Willy Tarreau272adea2014-03-31 10:39:59 +02001237
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001238 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001239 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1240 file, linenum, newsrv->addr.ss_family, args[2]);
1241 err_code |= ERR_ALERT | ERR_FATAL;
1242 goto out;
1243 }
1244
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001245 newsrv->pp_opts = curproxy->defsrv.pp_opts;
Willy Tarreau141ad852016-12-22 18:38:00 +01001246 newsrv->use_ssl = curproxy->defsrv.use_ssl;
Willy Tarreau272adea2014-03-31 10:39:59 +02001247 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1248 newsrv->check.port = curproxy->defsrv.check.port;
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +01001249 /* Note: 'flags' field has potentially been already initialized. */
1250 newsrv->flags |= curproxy->defsrv.flags;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001251 if (newsrv->check.port)
1252 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001253 newsrv->check.inter = curproxy->defsrv.check.inter;
1254 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1255 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1256 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1257 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001258 if (curproxy->defsrv.agent.send_string != NULL)
1259 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1260 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001261 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1262 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1263 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1264 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1265 newsrv->minconn = curproxy->defsrv.minconn;
1266 newsrv->maxconn = curproxy->defsrv.maxconn;
1267 newsrv->slowstart = curproxy->defsrv.slowstart;
1268 newsrv->onerror = curproxy->defsrv.onerror;
1269 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1270 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1271 newsrv->consecutive_errors_limit
1272 = curproxy->defsrv.consecutive_errors_limit;
Willy Tarreau272adea2014-03-31 10:39:59 +02001273 newsrv->uweight = newsrv->iweight
1274 = curproxy->defsrv.iweight;
1275
1276 newsrv->check.status = HCHK_STATUS_INI;
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001277 newsrv->check.send_proxy = curproxy->defsrv.check.send_proxy;
Willy Tarreau272adea2014-03-31 10:39:59 +02001278 newsrv->check.rise = curproxy->defsrv.check.rise;
1279 newsrv->check.fall = curproxy->defsrv.check.fall;
1280 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1281 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001282 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001283
1284 newsrv->agent.status = HCHK_STATUS_INI;
1285 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1286 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1287 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1288 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001289 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1290 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1291 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001292 memcpy(newsrv->dns_opts.pref_net,
1293 curproxy->defsrv.dns_opts.pref_net,
1294 sizeof(newsrv->dns_opts.pref_net));
1295 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001296 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1297 newsrv->init_addr = curproxy->defsrv.init_addr;
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001298#if defined(USE_OPENSSL)
1299 /* SSL config. */
1300 newsrv->ssl_ctx.verify = curproxy->defsrv.ssl_ctx.verify;
1301#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02001302
1303 cur_arg = 3;
1304 } else {
1305 newsrv = &curproxy->defsrv;
1306 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001307 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001308 }
1309
1310 while (*args[cur_arg]) {
1311 if (!strcmp(args[cur_arg], "agent-check")) {
1312 global.maxsock++;
1313 do_agent = 1;
1314 cur_arg += 1;
1315 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1316 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1317 if (err) {
1318 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1319 file, linenum, *err, newsrv->id);
1320 err_code |= ERR_ALERT | ERR_FATAL;
1321 goto out;
1322 }
1323 if (val <= 0) {
1324 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1325 file, linenum, val, args[cur_arg], newsrv->id);
1326 err_code |= ERR_ALERT | ERR_FATAL;
1327 goto out;
1328 }
1329 newsrv->agent.inter = val;
1330 cur_arg += 2;
1331 }
Misiekea849332017-01-09 09:39:51 +01001332 else if (!strcmp(args[cur_arg], "agent-addr")) {
1333 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1334 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1335 goto out;
1336 }
1337
1338 cur_arg += 2;
1339 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001340 else if (!strcmp(args[cur_arg], "agent-port")) {
1341 global.maxsock++;
1342 newsrv->agent.port = atol(args[cur_arg + 1]);
1343 cur_arg += 2;
1344 }
James Brown55f9ff12015-10-21 18:19:05 -07001345 else if (!strcmp(args[cur_arg], "agent-send")) {
1346 global.maxsock++;
1347 free(newsrv->agent.send_string);
1348 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1349 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1350 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1351 cur_arg += 2;
1352 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001353 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1354 newsrv->cookie = strdup(args[cur_arg + 1]);
1355 newsrv->cklen = strlen(args[cur_arg + 1]);
Olivier Houchard4e694042017-03-14 20:01:29 +01001356 newsrv->flags |= SRV_F_COOKIESET;
Willy Tarreau272adea2014-03-31 10:39:59 +02001357 cur_arg += 2;
1358 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001359 else if (!strcmp(args[cur_arg], "init-addr")) {
1360 char *p, *end;
1361 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001362 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001363
1364 newsrv->init_addr_methods = 0;
1365 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1366
1367 for (p = args[cur_arg + 1]; *p; p = end) {
1368 /* cut on next comma */
1369 for (end = p; *end && *end != ','; end++);
1370 if (*end)
1371 *(end++) = 0;
1372
Willy Tarreau4310d362016-11-02 15:05:56 +01001373 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001374 if (!strcmp(p, "libc")) {
1375 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1376 }
1377 else if (!strcmp(p, "last")) {
1378 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1379 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001380 else if (!strcmp(p, "none")) {
1381 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1382 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001383 else if (str2ip2(p, &sa, 0)) {
1384 if (is_addr(&newsrv->init_addr)) {
1385 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1386 file, linenum, args[cur_arg], p);
1387 err_code |= ERR_ALERT | ERR_FATAL;
1388 goto out;
1389 }
1390 newsrv->init_addr = sa;
1391 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1392 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001393 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001394 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001395 file, linenum, args[cur_arg], p);
1396 err_code |= ERR_ALERT | ERR_FATAL;
1397 goto out;
1398 }
1399 if (!done) {
1400 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1401 file, linenum, args[cur_arg], p);
1402 err_code |= ERR_ALERT | ERR_FATAL;
1403 goto out;
1404 }
1405 }
1406 cur_arg += 2;
1407 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001408 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1409 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1410 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1411 cur_arg += 2;
1412 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001413 else if (!strcmp(args[cur_arg], "resolvers")) {
1414 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1415 cur_arg += 2;
1416 }
1417 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1418 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001419 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001420 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001421 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001422 else {
1423 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1424 file, linenum, args[cur_arg]);
1425 err_code |= ERR_ALERT | ERR_FATAL;
1426 goto out;
1427 }
1428 cur_arg += 2;
1429 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001430 else if (!strcmp(args[cur_arg], "resolve-net")) {
1431 char *p, *e;
1432 unsigned char mask;
1433 struct dns_options *opt;
1434
1435 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1436 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1437 file, linenum, args[cur_arg]);
1438 err_code |= ERR_ALERT | ERR_FATAL;
1439 goto out;
1440 }
1441
1442 opt = &newsrv->dns_opts;
1443
1444 /* Split arguments by comma, and convert it from ipv4 or ipv6
1445 * string network in in_addr or in6_addr.
1446 */
1447 p = args[cur_arg + 1];
1448 e = p;
1449 while (*p != '\0') {
1450 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001451 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001452 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1453 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1454 err_code |= ERR_ALERT | ERR_FATAL;
1455 goto out;
1456 }
1457 /* look for end or comma. */
1458 while (*e != ',' && *e != '\0')
1459 e++;
1460 if (*e == ',') {
1461 *e = '\0';
1462 e++;
1463 }
1464 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1465 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1466 /* Try to convert input string from ipv4 or ipv6 network. */
1467 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1468 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1469 &mask)) {
1470 /* Try to convert input string from ipv6 network. */
1471 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1472 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1473 } else {
1474 /* All network conversions fail, retrun error. */
1475 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1476 file, linenum, args[cur_arg], p);
1477 err_code |= ERR_ALERT | ERR_FATAL;
1478 goto out;
1479 }
1480 opt->pref_net_nb++;
1481 p = e;
1482 }
1483
1484 cur_arg += 2;
1485 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001486 else if (!strcmp(args[cur_arg], "rise")) {
1487 if (!*args[cur_arg + 1]) {
1488 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1489 file, linenum, args[cur_arg]);
1490 err_code |= ERR_ALERT | ERR_FATAL;
1491 goto out;
1492 }
1493
1494 newsrv->check.rise = atol(args[cur_arg + 1]);
1495 if (newsrv->check.rise <= 0) {
1496 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1497 file, linenum, args[cur_arg]);
1498 err_code |= ERR_ALERT | ERR_FATAL;
1499 goto out;
1500 }
1501
1502 if (newsrv->check.health)
1503 newsrv->check.health = newsrv->check.rise;
1504 cur_arg += 2;
1505 }
1506 else if (!strcmp(args[cur_arg], "fall")) {
1507 newsrv->check.fall = atol(args[cur_arg + 1]);
1508
1509 if (!*args[cur_arg + 1]) {
1510 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1511 file, linenum, args[cur_arg]);
1512 err_code |= ERR_ALERT | ERR_FATAL;
1513 goto out;
1514 }
1515
1516 if (newsrv->check.fall <= 0) {
1517 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1518 file, linenum, args[cur_arg]);
1519 err_code |= ERR_ALERT | ERR_FATAL;
1520 goto out;
1521 }
1522
1523 cur_arg += 2;
1524 }
1525 else if (!strcmp(args[cur_arg], "inter")) {
1526 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1527 if (err) {
1528 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1529 file, linenum, *err, newsrv->id);
1530 err_code |= ERR_ALERT | ERR_FATAL;
1531 goto out;
1532 }
1533 if (val <= 0) {
1534 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1535 file, linenum, val, args[cur_arg], newsrv->id);
1536 err_code |= ERR_ALERT | ERR_FATAL;
1537 goto out;
1538 }
1539 newsrv->check.inter = val;
1540 cur_arg += 2;
1541 }
1542 else if (!strcmp(args[cur_arg], "fastinter")) {
1543 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1544 if (err) {
1545 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1546 file, linenum, *err, newsrv->id);
1547 err_code |= ERR_ALERT | ERR_FATAL;
1548 goto out;
1549 }
1550 if (val <= 0) {
1551 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1552 file, linenum, val, args[cur_arg], newsrv->id);
1553 err_code |= ERR_ALERT | ERR_FATAL;
1554 goto out;
1555 }
1556 newsrv->check.fastinter = val;
1557 cur_arg += 2;
1558 }
1559 else if (!strcmp(args[cur_arg], "downinter")) {
1560 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1561 if (err) {
1562 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1563 file, linenum, *err, newsrv->id);
1564 err_code |= ERR_ALERT | ERR_FATAL;
1565 goto out;
1566 }
1567 if (val <= 0) {
1568 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1569 file, linenum, val, args[cur_arg], newsrv->id);
1570 err_code |= ERR_ALERT | ERR_FATAL;
1571 goto out;
1572 }
1573 newsrv->check.downinter = val;
1574 cur_arg += 2;
1575 }
1576 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1577 struct sockaddr_storage *sk;
1578 int port1, port2;
1579 struct protocol *proto;
1580
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001581 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001582 if (!sk) {
1583 Alert("parsing [%s:%d] : '%s' : %s\n",
1584 file, linenum, args[cur_arg], errmsg);
1585 err_code |= ERR_ALERT | ERR_FATAL;
1586 goto out;
1587 }
1588
1589 proto = protocol_by_family(sk->ss_family);
1590 if (!proto || !proto->connect) {
1591 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1592 file, linenum, args[cur_arg], args[cur_arg + 1]);
1593 err_code |= ERR_ALERT | ERR_FATAL;
1594 goto out;
1595 }
1596
1597 if (port1 != port2) {
1598 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1599 file, linenum, args[cur_arg], args[cur_arg + 1]);
1600 err_code |= ERR_ALERT | ERR_FATAL;
1601 goto out;
1602 }
1603
Simon Horman41f58762015-01-30 11:22:56 +09001604 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001605 newsrv->flags |= SRV_F_CHECKADDR;
1606 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001607 cur_arg += 2;
1608 }
1609 else if (!strcmp(args[cur_arg], "port")) {
1610 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001611 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001612 cur_arg += 2;
1613 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001614 else if (!strcmp(args[cur_arg], "weight")) {
1615 int w;
1616 w = atol(args[cur_arg + 1]);
1617 if (w < 0 || w > SRV_UWGHT_MAX) {
1618 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1619 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1620 err_code |= ERR_ALERT | ERR_FATAL;
1621 goto out;
1622 }
1623 newsrv->uweight = newsrv->iweight = w;
1624 cur_arg += 2;
1625 }
1626 else if (!strcmp(args[cur_arg], "minconn")) {
1627 newsrv->minconn = atol(args[cur_arg + 1]);
1628 cur_arg += 2;
1629 }
1630 else if (!strcmp(args[cur_arg], "maxconn")) {
1631 newsrv->maxconn = atol(args[cur_arg + 1]);
1632 cur_arg += 2;
1633 }
1634 else if (!strcmp(args[cur_arg], "maxqueue")) {
1635 newsrv->maxqueue = atol(args[cur_arg + 1]);
1636 cur_arg += 2;
1637 }
1638 else if (!strcmp(args[cur_arg], "slowstart")) {
1639 /* slowstart is stored in seconds */
1640 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1641 if (err) {
1642 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1643 file, linenum, *err, newsrv->id);
1644 err_code |= ERR_ALERT | ERR_FATAL;
1645 goto out;
1646 }
1647 newsrv->slowstart = (val + 999) / 1000;
1648 cur_arg += 2;
1649 }
1650 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1651
1652 if (!*args[cur_arg + 1]) {
1653 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1654 file, linenum);
1655 err_code |= ERR_ALERT | ERR_FATAL;
1656 goto out;
1657 }
1658
1659 newsrv->trackit = strdup(args[cur_arg + 1]);
1660
1661 cur_arg += 2;
1662 }
1663 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1664 global.maxsock++;
1665 do_check = 1;
1666 cur_arg += 1;
1667 }
1668 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001669 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001670 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001671 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001672 newsrv->check.state |= CHK_ST_PAUSED;
1673 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001674 cur_arg += 1;
1675 }
1676 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1677 if (!strcmp(args[cur_arg + 1], "none"))
1678 newsrv->observe = HANA_OBS_NONE;
1679 else if (!strcmp(args[cur_arg + 1], "layer4"))
1680 newsrv->observe = HANA_OBS_LAYER4;
1681 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1682 if (curproxy->mode != PR_MODE_HTTP) {
1683 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1684 file, linenum, args[cur_arg + 1]);
1685 err_code |= ERR_ALERT;
1686 }
1687 newsrv->observe = HANA_OBS_LAYER7;
1688 }
1689 else {
1690 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1691 "'layer4', 'layer7' but got '%s'\n",
1692 file, linenum, args[cur_arg], args[cur_arg + 1]);
1693 err_code |= ERR_ALERT | ERR_FATAL;
1694 goto out;
1695 }
1696
1697 cur_arg += 2;
1698 }
1699 else if (!strcmp(args[cur_arg], "on-error")) {
1700 if (!strcmp(args[cur_arg + 1], "fastinter"))
1701 newsrv->onerror = HANA_ONERR_FASTINTER;
1702 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1703 newsrv->onerror = HANA_ONERR_FAILCHK;
1704 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1705 newsrv->onerror = HANA_ONERR_SUDDTH;
1706 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1707 newsrv->onerror = HANA_ONERR_MARKDWN;
1708 else {
1709 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1710 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1711 file, linenum, args[cur_arg], args[cur_arg + 1]);
1712 err_code |= ERR_ALERT | ERR_FATAL;
1713 goto out;
1714 }
1715
1716 cur_arg += 2;
1717 }
1718 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1719 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1720 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1721 else {
1722 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1723 file, linenum, args[cur_arg], args[cur_arg + 1]);
1724 err_code |= ERR_ALERT | ERR_FATAL;
1725 goto out;
1726 }
1727
1728 cur_arg += 2;
1729 }
1730 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1731 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1732 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1733 else {
1734 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1735 file, linenum, args[cur_arg], args[cur_arg + 1]);
1736 err_code |= ERR_ALERT | ERR_FATAL;
1737 goto out;
1738 }
1739
1740 cur_arg += 2;
1741 }
1742 else if (!strcmp(args[cur_arg], "error-limit")) {
1743 if (!*args[cur_arg + 1]) {
1744 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1745 file, linenum, args[cur_arg]);
1746 err_code |= ERR_ALERT | ERR_FATAL;
1747 goto out;
1748 }
1749
1750 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1751
1752 if (newsrv->consecutive_errors_limit <= 0) {
1753 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1754 file, linenum, args[cur_arg]);
1755 err_code |= ERR_ALERT | ERR_FATAL;
1756 goto out;
1757 }
1758 cur_arg += 2;
1759 }
1760 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1761 int port_low, port_high;
1762 struct sockaddr_storage *sk;
1763 struct protocol *proto;
1764
1765 if (!*args[cur_arg + 1]) {
1766 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1767 file, linenum, "source", "usesrc", "interface");
1768 err_code |= ERR_ALERT | ERR_FATAL;
1769 goto out;
1770 }
1771
1772 newsrv->conn_src.opts |= CO_SRC_BIND;
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001773 sk = str2sa_range(args[cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001774 if (!sk) {
1775 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1776 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1777 err_code |= ERR_ALERT | ERR_FATAL;
1778 goto out;
1779 }
1780
1781 proto = protocol_by_family(sk->ss_family);
1782 if (!proto || !proto->connect) {
1783 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1784 file, linenum, args[cur_arg], args[cur_arg+1]);
1785 err_code |= ERR_ALERT | ERR_FATAL;
1786 goto out;
1787 }
1788
1789 newsrv->conn_src.source_addr = *sk;
1790
1791 if (port_low != port_high) {
1792 int i;
1793
1794 if (!port_low || !port_high) {
1795 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1796 file, linenum, args[cur_arg], args[cur_arg + 1]);
1797 err_code |= ERR_ALERT | ERR_FATAL;
1798 goto out;
1799 }
1800
1801 if (port_low <= 0 || port_low > 65535 ||
1802 port_high <= 0 || port_high > 65535 ||
1803 port_low > port_high) {
1804 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1805 file, linenum, port_low, port_high);
1806 err_code |= ERR_ALERT | ERR_FATAL;
1807 goto out;
1808 }
1809 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1810 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1811 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1812 }
1813
1814 cur_arg += 2;
1815 while (*(args[cur_arg])) {
1816 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001817#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001818 if (!*args[cur_arg + 1]) {
1819 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1820 file, linenum, "usesrc");
1821 err_code |= ERR_ALERT | ERR_FATAL;
1822 goto out;
1823 }
1824 if (!strcmp(args[cur_arg + 1], "client")) {
1825 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1826 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1827 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1828 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1829 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1830 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1831 char *name, *end;
1832
1833 name = args[cur_arg+1] + 7;
1834 while (isspace(*name))
1835 name++;
1836
1837 end = name;
1838 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1839 end++;
1840
1841 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1842 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1843 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1844 newsrv->conn_src.bind_hdr_len = end - name;
1845 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1846 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1847 newsrv->conn_src.bind_hdr_occ = -1;
1848
1849 /* now look for an occurrence number */
1850 while (isspace(*end))
1851 end++;
1852 if (*end == ',') {
1853 end++;
1854 name = end;
1855 if (*end == '-')
1856 end++;
1857 while (isdigit((int)*end))
1858 end++;
1859 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1860 }
1861
1862 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1863 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1864 " occurrences values smaller than %d.\n",
1865 file, linenum, MAX_HDR_HISTORY);
1866 err_code |= ERR_ALERT | ERR_FATAL;
1867 goto out;
1868 }
1869 } else {
1870 struct sockaddr_storage *sk;
1871 int port1, port2;
1872
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001873 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001874 if (!sk) {
1875 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1876 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1877 err_code |= ERR_ALERT | ERR_FATAL;
1878 goto out;
1879 }
1880
1881 proto = protocol_by_family(sk->ss_family);
1882 if (!proto || !proto->connect) {
1883 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1884 file, linenum, args[cur_arg], args[cur_arg+1]);
1885 err_code |= ERR_ALERT | ERR_FATAL;
1886 goto out;
1887 }
1888
1889 if (port1 != port2) {
1890 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1891 file, linenum, args[cur_arg], args[cur_arg + 1]);
1892 err_code |= ERR_ALERT | ERR_FATAL;
1893 goto out;
1894 }
1895 newsrv->conn_src.tproxy_addr = *sk;
1896 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1897 }
1898 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001899 cur_arg += 2;
1900 continue;
1901#else /* no TPROXY support */
1902 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1903 file, linenum, "usesrc");
1904 err_code |= ERR_ALERT | ERR_FATAL;
1905 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001906#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001907 } /* "usesrc" */
1908
1909 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1910#ifdef SO_BINDTODEVICE
1911 if (!*args[cur_arg + 1]) {
1912 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1913 file, linenum, args[0]);
1914 err_code |= ERR_ALERT | ERR_FATAL;
1915 goto out;
1916 }
1917 free(newsrv->conn_src.iface_name);
1918 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1919 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1920 global.last_checks |= LSTCHK_NETADM;
1921#else
1922 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1923 file, linenum, args[0], args[cur_arg]);
1924 err_code |= ERR_ALERT | ERR_FATAL;
1925 goto out;
1926#endif
1927 cur_arg += 2;
1928 continue;
1929 }
1930 /* this keyword in not an option of "source" */
1931 break;
1932 } /* while */
1933 }
1934 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1935 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1936 file, linenum, "usesrc", "source");
1937 err_code |= ERR_ALERT | ERR_FATAL;
1938 goto out;
1939 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001940 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1941#ifdef CONFIG_HAP_NS
1942 char *arg = args[cur_arg + 1];
1943 if (!strcmp(arg, "*")) {
1944 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1945 } else {
1946 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1947
1948 if (newsrv->netns == NULL)
1949 newsrv->netns = netns_store_insert(arg);
1950
1951 if (newsrv->netns == NULL) {
1952 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1953 err_code |= ERR_ALERT | ERR_FATAL;
1954 goto out;
1955 }
1956 }
1957#else
1958 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1959 file, linenum, args[0], args[cur_arg]);
1960 err_code |= ERR_ALERT | ERR_FATAL;
1961 goto out;
1962#endif
1963 cur_arg += 2;
1964 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001965 else {
1966 static int srv_dumped;
1967 struct srv_kw *kw;
1968 char *err;
1969
1970 kw = srv_find_kw(args[cur_arg]);
1971 if (kw) {
1972 char *err = NULL;
1973 int code;
1974
1975 if (!kw->parse) {
1976 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1977 file, linenum, args[0], args[1], args[cur_arg]);
1978 cur_arg += 1 + kw->skip ;
1979 err_code |= ERR_ALERT | ERR_FATAL;
1980 goto out;
1981 }
1982
1983 if (defsrv && !kw->default_ok) {
1984 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1985 file, linenum, args[0], args[1], args[cur_arg]);
1986 cur_arg += 1 + kw->skip ;
1987 err_code |= ERR_ALERT;
1988 continue;
1989 }
1990
1991 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1992 err_code |= code;
1993
1994 if (code) {
1995 if (err && *err) {
1996 indent_msg(&err, 2);
1997 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1998 }
1999 else
2000 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
2001 file, linenum, args[0], args[1], args[cur_arg]);
2002 if (code & ERR_FATAL) {
2003 free(err);
2004 cur_arg += 1 + kw->skip;
2005 goto out;
2006 }
2007 }
2008 free(err);
2009 cur_arg += 1 + kw->skip;
2010 continue;
2011 }
2012
2013 err = NULL;
2014 if (!srv_dumped) {
2015 srv_dump_kws(&err);
2016 indent_msg(&err, 4);
2017 srv_dumped = 1;
2018 }
2019
2020 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2021 file, linenum, args[0], args[1], args[cur_arg],
2022 err ? " Registered keywords :" : "", err ? err : "");
2023 free(err);
2024
2025 err_code |= ERR_ALERT | ERR_FATAL;
2026 goto out;
2027 }
2028 }
2029
Willy Tarreau272adea2014-03-31 10:39:59 +02002030 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002031 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002032
2033 if (newsrv->trackit) {
2034 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
2035 file, linenum);
2036 err_code |= ERR_ALERT | ERR_FATAL;
2037 goto out;
2038 }
2039
Willy Tarreau272adea2014-03-31 10:39:59 +02002040 /*
2041 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02002042 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02002043 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002044 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09002045 (is_inet_addr(&newsrv->check.addr) ||
2046 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01002047 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002048 struct list *l;
2049
2050 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
2051 if (!r) {
2052 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
2053 file, linenum, newsrv->id);
2054 err_code |= ERR_ALERT | ERR_FATAL;
2055 goto out;
2056 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002057 /* search the first action (connect / send / expect) in the list */
2058 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002059 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002060 if (r->action != TCPCHK_ACT_COMMENT)
2061 break;
2062 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002063 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
2064 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",
2065 file, linenum, newsrv->id);
2066 err_code |= ERR_ALERT | ERR_FATAL;
2067 goto out;
2068 }
2069 else {
2070 /* scan the tcp-check ruleset to ensure a port has been configured */
2071 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002072 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002073 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
2074 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",
2075 file, linenum, newsrv->id);
2076 err_code |= ERR_ALERT | ERR_FATAL;
2077 goto out;
2078 }
2079 }
2080 }
2081 }
2082
2083 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09002084 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002085 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002086 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2087 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002088 goto out;
2089 }
2090
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002091 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01002092 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002093
Willy Tarreau272adea2014-03-31 10:39:59 +02002094 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2095 }
2096
2097 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002098 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002099
2100 if (!newsrv->agent.port) {
2101 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2102 file, linenum, newsrv->id);
2103 err_code |= ERR_ALERT | ERR_FATAL;
2104 goto out;
2105 }
2106
2107 if (!newsrv->agent.inter)
2108 newsrv->agent.inter = newsrv->check.inter;
2109
Simon Hormanb1900d52015-01-30 11:22:54 +09002110 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02002111 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002112 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2113 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002114 goto out;
2115 }
2116
2117 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2118 }
2119
2120 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02002121 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02002122 curproxy->srv_bck++;
2123 else
2124 curproxy->srv_act++;
2125
Willy Tarreauc5150da2014-05-13 19:27:31 +02002126 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002127 }
2128 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002129 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 return 0;
2131
2132 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002133 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002134 free(errmsg);
2135 return err_code;
2136}
2137
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002138/* Returns a pointer to the first server matching either id <id>.
2139 * NULL is returned if no match is found.
2140 * the lookup is performed in the backend <bk>
2141 */
2142struct server *server_find_by_id(struct proxy *bk, int id)
2143{
2144 struct eb32_node *eb32;
2145 struct server *curserver;
2146
2147 if (!bk || (id ==0))
2148 return NULL;
2149
2150 /* <bk> has no backend capabilities, so it can't have a server */
2151 if (!(bk->cap & PR_CAP_BE))
2152 return NULL;
2153
2154 curserver = NULL;
2155
2156 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2157 if (eb32)
2158 curserver = container_of(eb32, struct server, conf.id);
2159
2160 return curserver;
2161}
2162
2163/* Returns a pointer to the first server matching either name <name>, or id
2164 * if <name> starts with a '#'. NULL is returned if no match is found.
2165 * the lookup is performed in the backend <bk>
2166 */
2167struct server *server_find_by_name(struct proxy *bk, const char *name)
2168{
2169 struct server *curserver;
2170
2171 if (!bk || !name)
2172 return NULL;
2173
2174 /* <bk> has no backend capabilities, so it can't have a server */
2175 if (!(bk->cap & PR_CAP_BE))
2176 return NULL;
2177
2178 curserver = NULL;
2179 if (*name == '#') {
2180 curserver = server_find_by_id(bk, atoi(name + 1));
2181 if (curserver)
2182 return curserver;
2183 }
2184 else {
2185 curserver = bk->srv;
2186
2187 while (curserver && (strcmp(curserver->id, name) != 0))
2188 curserver = curserver->next;
2189
2190 if (curserver)
2191 return curserver;
2192 }
2193
2194 return NULL;
2195}
2196
2197struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2198{
2199 struct server *byname;
2200 struct server *byid;
2201
2202 if (!name && !id)
2203 return NULL;
2204
2205 if (diff)
2206 *diff = 0;
2207
2208 byname = byid = NULL;
2209
2210 if (name) {
2211 byname = server_find_by_name(bk, name);
2212 if (byname && (!id || byname->puid == id))
2213 return byname;
2214 }
2215
2216 /* remaining possibilities :
2217 * - name not set
2218 * - name set but not found
2219 * - name found but ID doesn't match
2220 */
2221 if (id) {
2222 byid = server_find_by_id(bk, id);
2223 if (byid) {
2224 if (byname) {
2225 /* use id only if forced by configuration */
2226 if (byid->flags & SRV_F_FORCED_ID) {
2227 if (diff)
2228 *diff |= 2;
2229 return byid;
2230 }
2231 else {
2232 if (diff)
2233 *diff |= 1;
2234 return byname;
2235 }
2236 }
2237
2238 /* remaining possibilities:
2239 * - name not set
2240 * - name set but not found
2241 */
2242 if (name && diff)
2243 *diff |= 2;
2244 return byid;
2245 }
2246
2247 /* id bot found */
2248 if (byname) {
2249 if (diff)
2250 *diff |= 1;
2251 return byname;
2252 }
2253 }
2254
2255 return NULL;
2256}
2257
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002258/* Update a server state using the parameters available in the params list */
2259static void srv_update_state(struct server *srv, int version, char **params)
2260{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002261 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002262 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002263
2264 /* fields since version 1
2265 * and common to all other upcoming versions
2266 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002267 enum srv_state srv_op_state;
2268 enum srv_admin srv_admin_state;
2269 unsigned srv_uweight, srv_iweight;
2270 unsigned long srv_last_time_change;
2271 short srv_check_status;
2272 enum chk_result srv_check_result;
2273 int srv_check_health;
2274 int srv_check_state, srv_agent_state;
2275 int bk_f_forced_id;
2276 int srv_f_forced_id;
2277
Willy Tarreau31138fa2015-09-29 18:38:47 +02002278 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002279 switch (version) {
2280 case 1:
2281 /*
2282 * now we can proceed with server's state update:
2283 * srv_addr: params[0]
2284 * srv_op_state: params[1]
2285 * srv_admin_state: params[2]
2286 * srv_uweight: params[3]
2287 * srv_iweight: params[4]
2288 * srv_last_time_change: params[5]
2289 * srv_check_status: params[6]
2290 * srv_check_result: params[7]
2291 * srv_check_health: params[8]
2292 * srv_check_state: params[9]
2293 * srv_agent_state: params[10]
2294 * bk_f_forced_id: params[11]
2295 * srv_f_forced_id: params[12]
2296 */
2297
2298 /* validating srv_op_state */
2299 p = NULL;
2300 errno = 0;
2301 srv_op_state = strtol(params[1], &p, 10);
2302 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2303 (srv_op_state != SRV_ST_STOPPED &&
2304 srv_op_state != SRV_ST_STARTING &&
2305 srv_op_state != SRV_ST_RUNNING &&
2306 srv_op_state != SRV_ST_STOPPING)) {
2307 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2308 }
2309
2310 /* validating srv_admin_state */
2311 p = NULL;
2312 errno = 0;
2313 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002314
2315 /* inherited statuses will be recomputed later */
2316 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2317
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002318 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2319 (srv_admin_state != 0 &&
2320 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002321 srv_admin_state != SRV_ADMF_CMAINT &&
2322 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002323 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002324 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002325 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2326 }
2327
2328 /* validating srv_uweight */
2329 p = NULL;
2330 errno = 0;
2331 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002332 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002333 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2334
2335 /* validating srv_iweight */
2336 p = NULL;
2337 errno = 0;
2338 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002339 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002340 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2341
2342 /* validating srv_last_time_change */
2343 p = NULL;
2344 errno = 0;
2345 srv_last_time_change = strtol(params[5], &p, 10);
2346 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2347 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2348
2349 /* validating srv_check_status */
2350 p = NULL;
2351 errno = 0;
2352 srv_check_status = strtol(params[6], &p, 10);
2353 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2354 (srv_check_status >= HCHK_STATUS_SIZE))
2355 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2356
2357 /* validating srv_check_result */
2358 p = NULL;
2359 errno = 0;
2360 srv_check_result = strtol(params[7], &p, 10);
2361 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2362 (srv_check_result != CHK_RES_UNKNOWN &&
2363 srv_check_result != CHK_RES_NEUTRAL &&
2364 srv_check_result != CHK_RES_FAILED &&
2365 srv_check_result != CHK_RES_PASSED &&
2366 srv_check_result != CHK_RES_CONDPASS)) {
2367 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2368 }
2369
2370 /* validating srv_check_health */
2371 p = NULL;
2372 errno = 0;
2373 srv_check_health = strtol(params[8], &p, 10);
2374 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2375 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2376
2377 /* validating srv_check_state */
2378 p = NULL;
2379 errno = 0;
2380 srv_check_state = strtol(params[9], &p, 10);
2381 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2382 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2383 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2384
2385 /* validating srv_agent_state */
2386 p = NULL;
2387 errno = 0;
2388 srv_agent_state = strtol(params[10], &p, 10);
2389 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2390 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2391 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2392
2393 /* validating bk_f_forced_id */
2394 p = NULL;
2395 errno = 0;
2396 bk_f_forced_id = strtol(params[11], &p, 10);
2397 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2398 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2399
2400 /* validating srv_f_forced_id */
2401 p = NULL;
2402 errno = 0;
2403 srv_f_forced_id = strtol(params[12], &p, 10);
2404 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2405 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2406
2407
2408 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002409 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002410 goto out;
2411
2412 /* recover operational state and apply it to this server
2413 * and all servers tracking this one */
2414 switch (srv_op_state) {
2415 case SRV_ST_STOPPED:
2416 srv->check.health = 0;
2417 srv_set_stopped(srv, "changed from server-state after a reload");
2418 break;
2419 case SRV_ST_STARTING:
2420 srv->state = srv_op_state;
2421 break;
2422 case SRV_ST_STOPPING:
2423 srv->check.health = srv->check.rise + srv->check.fall - 1;
2424 srv_set_stopping(srv, "changed from server-state after a reload");
2425 break;
2426 case SRV_ST_RUNNING:
2427 srv->check.health = srv->check.rise + srv->check.fall - 1;
2428 srv_set_running(srv, "");
2429 break;
2430 }
2431
2432 /* When applying server state, the following rules apply:
2433 * - in case of a configuration change, we apply the setting from the new
2434 * configuration, regardless of old running state
2435 * - if no configuration change, we apply old running state only if old running
2436 * state is different from new configuration state
2437 */
2438 /* configuration has changed */
2439 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2440 if (srv->admin & SRV_ADMF_CMAINT)
2441 srv_adm_set_maint(srv);
2442 else
2443 srv_adm_set_ready(srv);
2444 }
2445 /* configuration is the same, let's compate old running state and new conf state */
2446 else {
2447 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2448 srv_adm_set_maint(srv);
2449 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2450 srv_adm_set_ready(srv);
2451 }
2452 /* apply drain mode if server is currently enabled */
2453 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2454 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002455 * (srv->iweight is the weight set up in configuration).
2456 * There are two possible reasons for FDRAIN to have been present :
2457 * - previous config weight was zero
2458 * - "set server b/s drain" was sent to the CLI
2459 *
2460 * In the first case, we simply want to drop this drain state
2461 * if the new weight is not zero anymore, meaning the administrator
2462 * has intentionally turned the weight back to a positive value to
2463 * enable the server again after an operation. In the second case,
2464 * the drain state was forced on the CLI regardless of the config's
2465 * weight so we don't want a change to the config weight to lose this
2466 * status. What this means is :
2467 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2468 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002469 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002470 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002471 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002472 }
2473
2474 srv->last_change = date.tv_sec - srv_last_time_change;
2475 srv->check.status = srv_check_status;
2476 srv->check.result = srv_check_result;
2477 srv->check.health = srv_check_health;
2478
2479 /* Only case we want to apply is removing ENABLED flag which could have been
2480 * done by the "disable health" command over the stats socket
2481 */
2482 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2483 (srv_check_state & CHK_ST_CONFIGURED) &&
2484 !(srv_check_state & CHK_ST_ENABLED))
2485 srv->check.state &= ~CHK_ST_ENABLED;
2486
2487 /* Only case we want to apply is removing ENABLED flag which could have been
2488 * done by the "disable agent" command over the stats socket
2489 */
2490 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2491 (srv_agent_state & CHK_ST_CONFIGURED) &&
2492 !(srv_agent_state & CHK_ST_ENABLED))
2493 srv->agent.state &= ~CHK_ST_ENABLED;
2494
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002495 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2496 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002497 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002498 * It means that a configuration file change has precedence over a unix socket change
2499 * for server's weight
2500 *
2501 * by default, HAProxy applies the following weight when parsing the configuration
2502 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002503 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002504 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002505 srv->uweight = srv_uweight;
2506 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002507 server_recalc_eweight(srv);
2508
Willy Tarreaue5a60682016-11-09 14:54:53 +01002509 /* load server IP address */
2510 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002511 break;
2512 default:
2513 chunk_appendf(msg, ", version '%d' not supported", version);
2514 }
2515
2516 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002517 if (msg->len) {
2518 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002519 Warning("server-state application failed for server '%s/%s'%s",
2520 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002521 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002522}
2523
2524/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2525 * For each proxy, it does the following:
2526 * - opens its server state file (either one or local one)
2527 * - read whole file, line by line
2528 * - analyse each line to check if it matches our current backend:
2529 * - backend name matches
2530 * - backend id matches if id is forced and name doesn't match
2531 * - if the server pointed by the line is found, then state is applied
2532 *
2533 * If the running backend uuid or id differs from the state file, then HAProxy reports
2534 * a warning.
2535 */
2536void apply_server_state(void)
2537{
2538 char *cur, *end;
2539 char mybuf[SRV_STATE_LINE_MAXLEN];
2540 int mybuflen;
2541 char *params[SRV_STATE_FILE_MAX_FIELDS];
2542 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2543 int arg, srv_arg, version, diff;
2544 FILE *f;
2545 char *filepath;
2546 char globalfilepath[MAXPATHLEN + 1];
2547 char localfilepath[MAXPATHLEN + 1];
2548 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2549 extern struct proxy *proxy;
2550 struct proxy *curproxy, *bk;
2551 struct server *srv;
2552
2553 globalfilepathlen = 0;
2554 /* create the globalfilepath variable */
2555 if (global.server_state_file) {
2556 /* absolute path or no base directory provided */
2557 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2558 len = strlen(global.server_state_file);
2559 if (len > MAXPATHLEN) {
2560 globalfilepathlen = 0;
2561 goto globalfileerror;
2562 }
2563 memcpy(globalfilepath, global.server_state_file, len);
2564 globalfilepath[len] = '\0';
2565 globalfilepathlen = len;
2566 }
2567 else if (global.server_state_base) {
2568 len = strlen(global.server_state_base);
2569 globalfilepathlen += len;
2570
2571 if (globalfilepathlen > MAXPATHLEN) {
2572 globalfilepathlen = 0;
2573 goto globalfileerror;
2574 }
2575 strncpy(globalfilepath, global.server_state_base, len);
2576 globalfilepath[globalfilepathlen] = 0;
2577
2578 /* append a slash if needed */
2579 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2580 if (globalfilepathlen + 1 > MAXPATHLEN) {
2581 globalfilepathlen = 0;
2582 goto globalfileerror;
2583 }
2584 globalfilepath[globalfilepathlen++] = '/';
2585 }
2586
2587 len = strlen(global.server_state_file);
2588 if (globalfilepathlen + len > MAXPATHLEN) {
2589 globalfilepathlen = 0;
2590 goto globalfileerror;
2591 }
2592 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2593 globalfilepathlen += len;
2594 globalfilepath[globalfilepathlen++] = 0;
2595 }
2596 }
2597 globalfileerror:
2598 if (globalfilepathlen == 0)
2599 globalfilepath[0] = '\0';
2600
2601 /* read servers state from local file */
2602 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2603 /* servers are only in backends */
2604 if (!(curproxy->cap & PR_CAP_BE))
2605 continue;
2606 fileopenerr = 0;
2607 filepath = NULL;
2608
2609 /* search server state file path and name */
2610 switch (curproxy->load_server_state_from_file) {
2611 /* read servers state from global file */
2612 case PR_SRV_STATE_FILE_GLOBAL:
2613 /* there was an error while generating global server state file path */
2614 if (globalfilepathlen == 0)
2615 continue;
2616 filepath = globalfilepath;
2617 fileopenerr = 1;
2618 break;
2619 /* this backend has its own file */
2620 case PR_SRV_STATE_FILE_LOCAL:
2621 localfilepathlen = 0;
2622 localfilepath[0] = '\0';
2623 len = 0;
2624 /* create the localfilepath variable */
2625 /* absolute path or no base directory provided */
2626 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2627 len = strlen(curproxy->server_state_file_name);
2628 if (len > MAXPATHLEN) {
2629 localfilepathlen = 0;
2630 goto localfileerror;
2631 }
2632 memcpy(localfilepath, curproxy->server_state_file_name, len);
2633 localfilepath[len] = '\0';
2634 localfilepathlen = len;
2635 }
2636 else if (global.server_state_base) {
2637 len = strlen(global.server_state_base);
2638 localfilepathlen += len;
2639
2640 if (localfilepathlen > MAXPATHLEN) {
2641 localfilepathlen = 0;
2642 goto localfileerror;
2643 }
2644 strncpy(localfilepath, global.server_state_base, len);
2645 localfilepath[localfilepathlen] = 0;
2646
2647 /* append a slash if needed */
2648 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2649 if (localfilepathlen + 1 > MAXPATHLEN) {
2650 localfilepathlen = 0;
2651 goto localfileerror;
2652 }
2653 localfilepath[localfilepathlen++] = '/';
2654 }
2655
2656 len = strlen(curproxy->server_state_file_name);
2657 if (localfilepathlen + len > MAXPATHLEN) {
2658 localfilepathlen = 0;
2659 goto localfileerror;
2660 }
2661 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2662 localfilepathlen += len;
2663 localfilepath[localfilepathlen++] = 0;
2664 }
2665 filepath = localfilepath;
2666 localfileerror:
2667 if (localfilepathlen == 0)
2668 localfilepath[0] = '\0';
2669
2670 break;
2671 case PR_SRV_STATE_FILE_NONE:
2672 default:
2673 continue;
2674 }
2675
2676 /* preload global state file */
2677 errno = 0;
2678 f = fopen(filepath, "r");
2679 if (errno && fileopenerr)
2680 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2681 if (!f)
2682 continue;
2683
2684 mybuf[0] = '\0';
2685 mybuflen = 0;
2686 version = 0;
2687
2688 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002689 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2690 Warning("Can't read first line of the server state file '%s'\n", filepath);
2691 goto fileclose;
2692 }
2693
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002694 cur = mybuf;
2695 version = atoi(cur);
2696 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2697 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002698 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002699
2700 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2701 int bk_f_forced_id = 0;
2702 int check_id = 0;
2703 int check_name = 0;
2704
2705 mybuflen = strlen(mybuf);
2706 cur = mybuf;
2707 end = cur + mybuflen;
2708
2709 bk = NULL;
2710 srv = NULL;
2711
2712 /* we need at least one character */
2713 if (mybuflen == 0)
2714 continue;
2715
2716 /* ignore blank characters at the beginning of the line */
2717 while (isspace(*cur))
2718 ++cur;
2719
2720 if (cur == end)
2721 continue;
2722
2723 /* ignore comment line */
2724 if (*cur == '#')
2725 continue;
2726
2727 /* we're now ready to move the line into *srv_params[] */
2728 params[0] = cur;
2729 arg = 1;
2730 srv_arg = 0;
2731 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2732 if (isspace(*cur)) {
2733 *cur = '\0';
2734 ++cur;
2735 while (isspace(*cur))
2736 ++cur;
2737 switch (version) {
2738 case 1:
2739 /*
2740 * srv_addr: params[4] => srv_params[0]
2741 * srv_op_state: params[5] => srv_params[1]
2742 * srv_admin_state: params[6] => srv_params[2]
2743 * srv_uweight: params[7] => srv_params[3]
2744 * srv_iweight: params[8] => srv_params[4]
2745 * srv_last_time_change: params[9] => srv_params[5]
2746 * srv_check_status: params[10] => srv_params[6]
2747 * srv_check_result: params[11] => srv_params[7]
2748 * srv_check_health: params[12] => srv_params[8]
2749 * srv_check_state: params[13] => srv_params[9]
2750 * srv_agent_state: params[14] => srv_params[10]
2751 * bk_f_forced_id: params[15] => srv_params[11]
2752 * srv_f_forced_id: params[16] => srv_params[12]
2753 */
2754 if (arg >= 4) {
2755 srv_params[srv_arg] = cur;
2756 ++srv_arg;
2757 }
2758 break;
2759 }
2760
2761 params[arg] = cur;
2762 ++arg;
2763 }
2764 else {
2765 ++cur;
2766 }
2767 }
2768
2769 /* if line is incomplete line, then ignore it.
2770 * otherwise, update useful flags */
2771 switch (version) {
2772 case 1:
2773 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2774 continue;
2775 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2776 check_id = (atoi(params[0]) == curproxy->uuid);
2777 check_name = (strcmp(curproxy->id, params[1]) == 0);
2778 break;
2779 }
2780
2781 diff = 0;
2782 bk = curproxy;
2783
2784 /* if backend can't be found, let's continue */
2785 if (!check_id && !check_name)
2786 continue;
2787 else if (!check_id && check_name) {
2788 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2789 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2790 }
2791 else if (check_id && !check_name) {
2792 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2793 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2794 /* if name doesn't match, we still want to update curproxy if the backend id
2795 * was forced in previous the previous configuration */
2796 if (!bk_f_forced_id)
2797 continue;
2798 }
2799
2800 /* look for the server by its id: param[2] */
2801 /* else look for the server by its name: param[3] */
2802 diff = 0;
2803 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2804
2805 if (!srv) {
2806 /* if no server found, then warning and continue with next line */
2807 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2808 params[3], params[2], params[0], params[1]);
2809 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2810 params[3], params[2], params[0], params[1]);
2811 continue;
2812 }
2813 else if (diff & PR_FBM_MISMATCH_ID) {
2814 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);
2815 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);
2816 }
2817 else if (diff & PR_FBM_MISMATCH_NAME) {
2818 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);
2819 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);
2820 }
2821
2822 /* now we can proceed with server's state update */
2823 srv_update_state(srv, version, srv_params);
2824 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002825fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002826 fclose(f);
2827 }
2828}
2829
Simon Horman7d09b9a2013-02-12 10:45:51 +09002830/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002831 * update a server's current IP address.
2832 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2833 * ip is in network format.
2834 * updater is a string which contains an information about the requester of the update.
2835 * updater is used if not NULL.
2836 *
2837 * A log line and a stderr warning message is generated based on server's backend options.
2838 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002839int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002840{
2841 /* generates a log line and a warning on stderr */
2842 if (1) {
2843 /* book enough space for both IPv4 and IPv6 */
2844 char oldip[INET6_ADDRSTRLEN];
2845 char newip[INET6_ADDRSTRLEN];
2846
2847 memset(oldip, '\0', INET6_ADDRSTRLEN);
2848 memset(newip, '\0', INET6_ADDRSTRLEN);
2849
2850 /* copy old IP address in a string */
2851 switch (s->addr.ss_family) {
2852 case AF_INET:
2853 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2854 break;
2855 case AF_INET6:
2856 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2857 break;
2858 };
2859
2860 /* copy new IP address in a string */
2861 switch (ip_sin_family) {
2862 case AF_INET:
2863 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2864 break;
2865 case AF_INET6:
2866 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2867 break;
2868 };
2869
2870 /* save log line into a buffer */
2871 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2872 s->proxy->id, s->id, oldip, newip, updater);
2873
2874 /* write the buffer on stderr */
2875 Warning("%s.\n", trash.str);
2876
2877 /* send a log */
2878 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2879 }
2880
2881 /* save the new IP family */
2882 s->addr.ss_family = ip_sin_family;
2883 /* save the new IP address */
2884 switch (ip_sin_family) {
2885 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002886 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002887 break;
2888 case AF_INET6:
2889 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2890 break;
2891 };
Olivier Houchard4e694042017-03-14 20:01:29 +01002892 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002893
2894 return 0;
2895}
2896
2897/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002898 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2899 *
2900 * Caller can pass its name through <updater> to get it integrated in the response message
2901 * returned by the function.
2902 *
2903 * The function first does the following, in that order:
2904 * - validates the new addr and/or port
2905 * - checks if an update is required (new IP or port is different than current ones)
2906 * - checks the update is allowed:
2907 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2908 * - allow all changes if no CHECKS are configured
2909 * - if CHECK is configured:
2910 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2911 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2912 * conditions are met
2913 */
2914const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2915{
2916 struct sockaddr_storage sa;
2917 int ret, port_change_required;
2918 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00002919 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002920 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01002921 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002922
2923 msg = get_trash_chunk();
2924 chunk_reset(msg);
2925
2926 if (addr) {
2927 memset(&sa, 0, sizeof(struct sockaddr_storage));
2928 if (str2ip2(addr, &sa, 0) == NULL) {
2929 chunk_printf(msg, "Invalid addr '%s'", addr);
2930 goto out;
2931 }
2932
2933 /* changes are allowed on AF_INET* families only */
2934 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2935 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2936 goto out;
2937 }
2938
2939 /* collecting data currently setup */
2940 memset(current_addr, '\0', sizeof(current_addr));
2941 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2942 /* changes are allowed on AF_INET* families only */
2943 if ((ret != AF_INET) && (ret != AF_INET6)) {
2944 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2945 goto out;
2946 }
2947
2948 /* applying ADDR changes if required and allowed
2949 * ipcmp returns 0 when both ADDR are the same
2950 */
2951 if (ipcmp(&s->addr, &sa) == 0) {
2952 chunk_appendf(msg, "no need to change the addr");
2953 goto port;
2954 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002955 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01002956 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002957
2958 /* we also need to update check's ADDR only if it uses the server's one */
2959 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002960 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002961 }
2962
2963 /* we also need to update agent ADDR only if it use the server's one */
2964 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002965 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002966 }
2967
2968 /* update report for caller */
2969 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2970 }
2971
2972 port:
2973 if (port) {
2974 char sign = '\0';
2975 char *endptr;
2976
2977 if (addr)
2978 chunk_appendf(msg, ", ");
2979
2980 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01002981 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002982
2983 /* check if PORT change is required */
2984 port_change_required = 0;
2985
2986 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04002987 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002988 new_port = strtol(port, &endptr, 10);
2989 if ((errno != 0) || (port == endptr)) {
2990 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2991 goto out;
2992 }
2993
2994 /* check if caller triggers a port mapped or offset */
2995 if (sign == '-' || (sign == '+')) {
2996 /* check if server currently uses port map */
2997 if (!(s->flags & SRV_F_MAPPORTS)) {
2998 /* switch from fixed port to port map mandatorily triggers
2999 * a port change */
3000 port_change_required = 1;
3001 /* check is configured
3002 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3003 * prevent PORT change if check doesn't have it's dedicated port while switching
3004 * to port mapping */
3005 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3006 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.");
3007 goto out;
3008 }
3009 }
3010 /* we're already using port maps */
3011 else {
3012 port_change_required = current_port != new_port;
3013 }
3014 }
3015 /* fixed port */
3016 else {
3017 port_change_required = current_port != new_port;
3018 }
3019
3020 /* applying PORT changes if required and update response message */
3021 if (port_change_required) {
3022 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003023 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003024 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003025
3026 /* prepare message */
3027 chunk_appendf(msg, "port changed from '");
3028 if (s->flags & SRV_F_MAPPORTS)
3029 chunk_appendf(msg, "+");
3030 chunk_appendf(msg, "%d' to '", current_port);
3031
3032 if (sign == '-') {
3033 s->flags |= SRV_F_MAPPORTS;
3034 chunk_appendf(msg, "%c", sign);
3035 /* just use for result output */
3036 new_port = -new_port;
3037 }
3038 else if (sign == '+') {
3039 s->flags |= SRV_F_MAPPORTS;
3040 chunk_appendf(msg, "%c", sign);
3041 }
3042 else {
3043 s->flags &= ~SRV_F_MAPPORTS;
3044 }
3045
3046 chunk_appendf(msg, "%d'", new_port);
3047
3048 /* we also need to update health checks port only if it uses server's realport */
3049 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3050 s->check.port = new_port;
3051 }
3052 }
3053 else {
3054 chunk_appendf(msg, "no need to change the port");
3055 }
3056 }
3057
3058out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003059 if (changed)
3060 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003061 if (updater)
3062 chunk_appendf(msg, " by '%s'", updater);
3063 chunk_appendf(msg, "\n");
3064 return msg->str;
3065}
3066
3067
3068/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003069 * update server status based on result of name resolution
3070 * returns:
3071 * 0 if server status is updated
3072 * 1 if server status has not changed
3073 */
3074int snr_update_srv_status(struct server *s)
3075{
3076 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003077 struct dns_resolvers *resolvers;
3078
3079 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003080
3081 switch (resolution->status) {
3082 case RSLV_STATUS_NONE:
3083 /* status when HAProxy has just (re)started */
3084 trigger_resolution(s);
3085 break;
3086
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003087 case RSLV_STATUS_VALID:
3088 /*
3089 * resume health checks
3090 * server will be turned back on if health check is safe
3091 */
3092 if (!(s->admin & SRV_ADMF_RMAINT))
3093 return 1;
3094 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3095 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3096 s->proxy->id, s->id);
3097
3098 Warning("%s.\n", trash.str);
3099 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3100 return 0;
3101
3102 case RSLV_STATUS_NX:
3103 /* stop server if resolution is NX for a long enough period */
3104 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3105 if (s->admin & SRV_ADMF_RMAINT)
3106 return 1;
3107 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3108 return 0;
3109 }
3110 break;
3111
3112 case RSLV_STATUS_TIMEOUT:
3113 /* stop server if resolution is TIMEOUT for a long enough period */
3114 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3115 if (s->admin & SRV_ADMF_RMAINT)
3116 return 1;
3117 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3118 return 0;
3119 }
3120 break;
3121
3122 case RSLV_STATUS_REFUSED:
3123 /* stop server if resolution is REFUSED for a long enough period */
3124 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3125 if (s->admin & SRV_ADMF_RMAINT)
3126 return 1;
3127 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3128 return 0;
3129 }
3130 break;
3131
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003132 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003133 /* stop server if resolution is in unmatched error for a long enough period */
3134 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3135 if (s->admin & SRV_ADMF_RMAINT)
3136 return 1;
3137 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3138 return 0;
3139 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003140 break;
3141 }
3142
3143 return 1;
3144}
3145
3146/*
3147 * Server Name Resolution valid response callback
3148 * It expects:
3149 * - <nameserver>: the name server which answered the valid response
3150 * - <response>: buffer containing a valid DNS response
3151 * - <response_len>: size of <response>
3152 * It performs the following actions:
3153 * - ignore response if current ip found and server family not met
3154 * - update with first new ip found if family is met and current IP is not found
3155 * returns:
3156 * 0 on error
3157 * 1 when no error or safe ignore
3158 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003159int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003160{
3161 struct server *s;
3162 void *serverip, *firstip;
3163 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003164 int ret;
3165 struct chunk *chk = get_trash_chunk();
3166
3167 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003168 firstip = NULL; /* pointer to the first valid response found */
3169 /* it will be used as the new IP if a change is required */
3170 firstip_sin_family = AF_UNSPEC;
3171 serverip = NULL; /* current server IP address */
3172
3173 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003174 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003175
3176 /* initializing server IP pointer */
3177 server_sin_family = s->addr.ss_family;
3178 switch (server_sin_family) {
3179 case AF_INET:
3180 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3181 break;
3182
3183 case AF_INET6:
3184 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3185 break;
3186
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003187 case AF_UNSPEC:
3188 break;
3189
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003190 default:
3191 goto invalid;
3192 }
3193
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003194 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003195 serverip, server_sin_family, &firstip,
3196 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003197
3198 switch (ret) {
3199 case DNS_UPD_NO:
3200 if (resolution->status != RSLV_STATUS_VALID) {
3201 resolution->status = RSLV_STATUS_VALID;
3202 resolution->last_status_change = now_ms;
3203 }
3204 goto stop_resolution;
3205
3206 case DNS_UPD_SRVIP_NOT_FOUND:
3207 goto save_ip;
3208
3209 case DNS_UPD_CNAME:
3210 if (resolution->status != RSLV_STATUS_VALID) {
3211 resolution->status = RSLV_STATUS_VALID;
3212 resolution->last_status_change = now_ms;
3213 }
3214 goto invalid;
3215
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003216 case DNS_UPD_NO_IP_FOUND:
3217 if (resolution->status != RSLV_STATUS_OTHER) {
3218 resolution->status = RSLV_STATUS_OTHER;
3219 resolution->last_status_change = now_ms;
3220 }
3221 goto stop_resolution;
3222
Baptiste Assmannfad03182015-10-28 02:03:32 +01003223 case DNS_UPD_NAME_ERROR:
3224 /* if this is not the last expected response, we ignore it */
3225 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3226 return 0;
3227 /* update resolution status to OTHER error type */
3228 if (resolution->status != RSLV_STATUS_OTHER) {
3229 resolution->status = RSLV_STATUS_OTHER;
3230 resolution->last_status_change = now_ms;
3231 }
3232 goto stop_resolution;
3233
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003234 default:
3235 goto invalid;
3236
3237 }
3238
3239 save_ip:
3240 nameserver->counters.update += 1;
3241 if (resolution->status != RSLV_STATUS_VALID) {
3242 resolution->status = RSLV_STATUS_VALID;
3243 resolution->last_status_change = now_ms;
3244 }
3245
3246 /* save the first ip we found */
3247 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3248 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3249
3250 stop_resolution:
3251 /* update last resolution date and time */
3252 resolution->last_resolution = now_ms;
3253 /* reset current status flag */
3254 resolution->step = RSLV_STEP_NONE;
3255 /* reset values */
3256 dns_reset_resolution(resolution);
3257
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003258 dns_update_resolvers_timeout(nameserver->resolvers);
3259
3260 snr_update_srv_status(s);
3261 return 0;
3262
3263 invalid:
3264 nameserver->counters.invalid += 1;
3265 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3266 goto stop_resolution;
3267
3268 snr_update_srv_status(s);
3269 return 0;
3270}
3271
3272/*
3273 * Server Name Resolution error management callback
3274 * returns:
3275 * 0 on error
3276 * 1 when no error or safe ignore
3277 */
3278int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3279{
3280 struct server *s;
3281 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003282 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003283
3284 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003285 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003286 resolvers = resolution->resolvers;
3287
3288 /* can be ignored if this is not the last response */
3289 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3290 return 1;
3291 }
3292
3293 switch (error_code) {
3294 case DNS_RESP_INVALID:
3295 case DNS_RESP_WRONG_NAME:
3296 if (resolution->status != RSLV_STATUS_INVALID) {
3297 resolution->status = RSLV_STATUS_INVALID;
3298 resolution->last_status_change = now_ms;
3299 }
3300 break;
3301
3302 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003303 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003304 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003305 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003306 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003307 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3308 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003309
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003310 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003311 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003312 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003313 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003314 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003315 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003316 }
3317 else if (res_preferred_afinet) {
3318 /* fallback from A to AAAA */
3319 resolution->query_type = DNS_RTYPE_AAAA;
3320 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003321 else {
3322 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003323 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003324 resolution->query_type = DNS_RTYPE_A;
3325 } else {
3326 resolution->query_type = DNS_RTYPE_AAAA;
3327 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003328 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003329
3330 dns_send_query(resolution);
3331
3332 /*
3333 * move the resolution to the last element of the FIFO queue
3334 * and update timeout wakeup based on the new first entry
3335 */
3336 if (dns_check_resolution_queue(resolvers) > 1) {
3337 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003338 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003339 /* ex first resolution goes to the end of the queue */
3340 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3341 }
3342 dns_update_resolvers_timeout(resolvers);
3343 goto leave;
3344 }
3345 else {
3346 if (resolution->status != RSLV_STATUS_OTHER) {
3347 resolution->status = RSLV_STATUS_OTHER;
3348 resolution->last_status_change = now_ms;
3349 }
3350 }
3351 break;
3352
3353 case DNS_RESP_NX_DOMAIN:
3354 if (resolution->status != RSLV_STATUS_NX) {
3355 resolution->status = RSLV_STATUS_NX;
3356 resolution->last_status_change = now_ms;
3357 }
3358 break;
3359
3360 case DNS_RESP_REFUSED:
3361 if (resolution->status != RSLV_STATUS_REFUSED) {
3362 resolution->status = RSLV_STATUS_REFUSED;
3363 resolution->last_status_change = now_ms;
3364 }
3365 break;
3366
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003367 case DNS_RESP_TIMEOUT:
3368 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3369 resolution->status = RSLV_STATUS_TIMEOUT;
3370 resolution->last_status_change = now_ms;
3371 }
3372 break;
3373 }
3374
3375 /* update last resolution date and time */
3376 resolution->last_resolution = now_ms;
3377 /* reset current status flag */
3378 resolution->step = RSLV_STEP_NONE;
3379 /* reset values */
3380 dns_reset_resolution(resolution);
3381
3382 LIST_DEL(&resolution->list);
3383 dns_update_resolvers_timeout(resolvers);
3384
3385 leave:
3386 snr_update_srv_status(s);
3387 return 1;
3388}
3389
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003390/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3391 * resolver. This is suited for initial address configuration. Returns 0 on
3392 * success otherwise a non-zero error code. In case of error, *err_code, if
3393 * not NULL, is filled up.
3394 */
3395int srv_set_addr_via_libc(struct server *srv, int *err_code)
3396{
3397 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003398 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003399 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003400 return 1;
3401 }
3402 return 0;
3403}
3404
3405/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3406 * from the state file. This is suited for initial address configuration.
3407 * Returns 0 on success otherwise a non-zero error code. In case of error,
3408 * *err_code, if not NULL, is filled up.
3409 */
3410static int srv_apply_lastaddr(struct server *srv, int *err_code)
3411{
3412 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3413 if (err_code)
3414 *err_code |= ERR_WARN;
3415 return 1;
3416 }
3417 return 0;
3418}
3419
Willy Tarreau25e51522016-11-04 15:10:17 +01003420/* returns 0 if no error, otherwise a combination of ERR_* flags */
3421static int srv_iterate_initaddr(struct server *srv)
3422{
3423 int return_code = 0;
3424 int err_code;
3425 unsigned int methods;
3426
3427 methods = srv->init_addr_methods;
3428 if (!methods) { // default to "last,libc"
3429 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3430 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3431 }
3432
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003433 /* "-dr" : always append "none" so that server addresses resolution
3434 * failures are silently ignored, this is convenient to validate some
3435 * configs out of their environment.
3436 */
3437 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3438 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3439
Willy Tarreau25e51522016-11-04 15:10:17 +01003440 while (methods) {
3441 err_code = 0;
3442 switch (srv_get_next_initaddr(&methods)) {
3443 case SRV_IADDR_LAST:
3444 if (!srv->lastaddr)
3445 continue;
3446 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003447 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003448 return_code |= err_code;
3449 break;
3450
3451 case SRV_IADDR_LIBC:
3452 if (!srv->hostname)
3453 continue;
3454 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003455 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003456 return_code |= err_code;
3457 break;
3458
Willy Tarreau37ebe122016-11-04 15:17:58 +01003459 case SRV_IADDR_NONE:
3460 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003461 if (return_code) {
3462 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3463 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3464 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003465 return return_code;
3466
Willy Tarreau4310d362016-11-02 15:05:56 +01003467 case SRV_IADDR_IP:
3468 ipcpy(&srv->init_addr, &srv->addr);
3469 if (return_code) {
3470 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3471 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3472 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003473 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003474
Willy Tarreau25e51522016-11-04 15:10:17 +01003475 default: /* unhandled method */
3476 break;
3477 }
3478 }
3479
3480 if (!return_code) {
3481 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3482 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3483 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003484 else {
3485 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3486 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3487 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003488
3489 return_code |= ERR_ALERT | ERR_FATAL;
3490 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003491out:
3492 srv_set_dyncookie(srv);
3493 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003494}
3495
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003496/*
3497 * This function parses all backends and all servers within each backend
3498 * and performs servers' addr resolution based on information provided by:
3499 * - configuration file
3500 * - server-state file (states provided by an 'old' haproxy process)
3501 *
3502 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3503 */
3504int srv_init_addr(void)
3505{
3506 struct proxy *curproxy;
3507 int return_code = 0;
3508
3509 curproxy = proxy;
3510 while (curproxy) {
3511 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003512
3513 /* servers are in backend only */
3514 if (!(curproxy->cap & PR_CAP_BE))
3515 goto srv_init_addr_next;
3516
Willy Tarreau25e51522016-11-04 15:10:17 +01003517 for (srv = curproxy->srv; srv; srv = srv->next)
3518 if (srv->hostname)
3519 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003520
3521 srv_init_addr_next:
3522 curproxy = curproxy->next;
3523 }
3524
3525 return return_code;
3526}
3527
Willy Tarreau21b069d2016-11-23 17:15:08 +01003528/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3529 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003530 * 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 +01003531 * used for CLI commands requiring a server name.
3532 * Important: the <arg> is modified to remove the '/'.
3533 */
3534struct server *cli_find_server(struct appctx *appctx, char *arg)
3535{
3536 struct proxy *px;
3537 struct server *sv;
3538 char *line;
3539
3540 /* split "backend/server" and make <line> point to server */
3541 for (line = arg; *line; line++)
3542 if (*line == '/') {
3543 *line++ = '\0';
3544 break;
3545 }
3546
3547 if (!*line || !*arg) {
3548 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003549 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003550 return NULL;
3551 }
3552
3553 if (!get_backend_server(arg, line, &px, &sv)) {
3554 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003555 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003556 return NULL;
3557 }
3558
3559 if (px->state == PR_STSTOPPED) {
3560 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003561 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003562 return NULL;
3563 }
3564
3565 return sv;
3566}
3567
William Lallemand222baf22016-11-19 02:00:33 +01003568
3569static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3570{
3571 struct server *sv;
3572 const char *warning;
3573
3574 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3575 return 1;
3576
3577 sv = cli_find_server(appctx, args[2]);
3578 if (!sv)
3579 return 1;
3580
3581 if (strcmp(args[3], "weight") == 0) {
3582 warning = server_parse_weight_change_request(sv, args[4]);
3583 if (warning) {
3584 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003585 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003586 }
3587 }
3588 else if (strcmp(args[3], "state") == 0) {
3589 if (strcmp(args[4], "ready") == 0)
3590 srv_adm_set_ready(sv);
3591 else if (strcmp(args[4], "drain") == 0)
3592 srv_adm_set_drain(sv);
3593 else if (strcmp(args[4], "maint") == 0)
3594 srv_adm_set_maint(sv);
3595 else {
3596 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003597 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003598 }
3599 }
3600 else if (strcmp(args[3], "health") == 0) {
3601 if (sv->track) {
3602 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003603 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003604 }
3605 else if (strcmp(args[4], "up") == 0) {
3606 sv->check.health = sv->check.rise + sv->check.fall - 1;
3607 srv_set_running(sv, "changed from CLI");
3608 }
3609 else if (strcmp(args[4], "stopping") == 0) {
3610 sv->check.health = sv->check.rise + sv->check.fall - 1;
3611 srv_set_stopping(sv, "changed from CLI");
3612 }
3613 else if (strcmp(args[4], "down") == 0) {
3614 sv->check.health = 0;
3615 srv_set_stopped(sv, "changed from CLI");
3616 }
3617 else {
3618 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003619 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003620 }
3621 }
3622 else if (strcmp(args[3], "agent") == 0) {
3623 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3624 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003625 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003626 }
3627 else if (strcmp(args[4], "up") == 0) {
3628 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3629 srv_set_running(sv, "changed from CLI");
3630 }
3631 else if (strcmp(args[4], "down") == 0) {
3632 sv->agent.health = 0;
3633 srv_set_stopped(sv, "changed from CLI");
3634 }
3635 else {
3636 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003637 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003638 }
3639 }
Misiek2da082d2017-01-09 09:40:42 +01003640 else if (strcmp(args[3], "agent-addr") == 0) {
3641 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3642 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3643 appctx->st0 = CLI_ST_PRINT;
3644 } else {
3645 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3646 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3647 appctx->st0 = CLI_ST_PRINT;
3648 }
3649 }
3650 }
3651 else if (strcmp(args[3], "agent-send") == 0) {
3652 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3653 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3654 appctx->st0 = CLI_ST_PRINT;
3655 } else {
3656 char *nss = strdup(args[4]);
3657 if (!nss) {
3658 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3659 appctx->st0 = CLI_ST_PRINT;
3660 } else {
3661 free(sv->agent.send_string);
3662 sv->agent.send_string = nss;
3663 sv->agent.send_string_len = strlen(args[4]);
3664 }
3665 }
3666 }
William Lallemand222baf22016-11-19 02:00:33 +01003667 else if (strcmp(args[3], "check-port") == 0) {
3668 int i = 0;
3669 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3670 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003671 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003672 }
3673 if ((i < 0) || (i > 65535)) {
3674 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003675 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003676 }
3677 /* prevent the update of port to 0 if MAPPORTS are in use */
3678 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3679 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003680 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003681 return 1;
3682 }
3683 sv->check.port = i;
3684 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003685 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003686 }
3687 else if (strcmp(args[3], "addr") == 0) {
3688 char *addr = NULL;
3689 char *port = NULL;
3690 if (strlen(args[4]) == 0) {
3691 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003692 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003693 return 1;
3694 }
3695 else {
3696 addr = args[4];
3697 }
3698 if (strcmp(args[5], "port") == 0) {
3699 port = args[6];
3700 }
3701 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3702 if (warning) {
3703 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003704 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003705 }
3706 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3707 }
3708 else {
3709 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 +01003710 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003711 }
3712 return 1;
3713}
3714
William Lallemand6b160942016-11-22 12:34:35 +01003715static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3716{
3717 struct stream_interface *si = appctx->owner;
3718 struct proxy *px;
3719 struct server *sv;
3720 char *line;
3721
3722
3723 /* split "backend/server" and make <line> point to server */
3724 for (line = args[2]; *line; line++)
3725 if (*line == '/') {
3726 *line++ = '\0';
3727 break;
3728 }
3729
3730 if (!*line) {
3731 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003732 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003733 return 1;
3734 }
3735
3736 if (!get_backend_server(args[2], line, &px, &sv)) {
3737 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003738 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003739 return 1;
3740 }
3741
3742 /* return server's effective weight at the moment */
3743 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003744 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01003745 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003746 return 0;
3747 }
William Lallemand6b160942016-11-22 12:34:35 +01003748 return 1;
3749}
3750
3751static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
3752{
3753 struct server *sv;
3754 const char *warning;
3755
3756 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3757 return 1;
3758
3759 sv = cli_find_server(appctx, args[2]);
3760 if (!sv)
3761 return 1;
3762
3763 warning = server_parse_weight_change_request(sv, args[3]);
3764 if (warning) {
3765 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003766 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003767 }
3768 return 1;
3769}
3770
Willy Tarreaub8026272016-11-23 11:26:56 +01003771/* parse a "set maxconn server" command. It always returns 1. */
3772static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
3773{
3774 struct server *sv;
3775 const char *warning;
3776
3777 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3778 return 1;
3779
3780 sv = cli_find_server(appctx, args[3]);
3781 if (!sv)
3782 return 1;
3783
3784 warning = server_parse_maxconn_change_request(sv, args[4]);
3785 if (warning) {
3786 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003787 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01003788 }
3789 return 1;
3790}
William Lallemand6b160942016-11-22 12:34:35 +01003791
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003792/* parse a "disable agent" command. It always returns 1. */
3793static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
3794{
3795 struct server *sv;
3796
3797 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3798 return 1;
3799
3800 sv = cli_find_server(appctx, args[2]);
3801 if (!sv)
3802 return 1;
3803
3804 sv->agent.state &= ~CHK_ST_ENABLED;
3805 return 1;
3806}
3807
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003808/* parse a "disable health" command. It always returns 1. */
3809static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
3810{
3811 struct server *sv;
3812
3813 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3814 return 1;
3815
3816 sv = cli_find_server(appctx, args[2]);
3817 if (!sv)
3818 return 1;
3819
3820 sv->check.state &= ~CHK_ST_ENABLED;
3821 return 1;
3822}
3823
Willy Tarreauffb4d582016-11-24 12:47:00 +01003824/* parse a "disable server" command. It always returns 1. */
3825static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
3826{
3827 struct server *sv;
3828
3829 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3830 return 1;
3831
3832 sv = cli_find_server(appctx, args[2]);
3833 if (!sv)
3834 return 1;
3835
3836 srv_adm_set_maint(sv);
3837 return 1;
3838}
3839
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003840/* parse a "enable agent" command. It always returns 1. */
3841static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
3842{
3843 struct server *sv;
3844
3845 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3846 return 1;
3847
3848 sv = cli_find_server(appctx, args[2]);
3849 if (!sv)
3850 return 1;
3851
3852 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
3853 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003854 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003855 return 1;
3856 }
3857
3858 sv->agent.state |= CHK_ST_ENABLED;
3859 return 1;
3860}
3861
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003862/* parse a "enable health" command. It always returns 1. */
3863static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
3864{
3865 struct server *sv;
3866
3867 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3868 return 1;
3869
3870 sv = cli_find_server(appctx, args[2]);
3871 if (!sv)
3872 return 1;
3873
3874 sv->check.state |= CHK_ST_ENABLED;
3875 return 1;
3876}
3877
Willy Tarreauffb4d582016-11-24 12:47:00 +01003878/* parse a "enable server" command. It always returns 1. */
3879static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
3880{
3881 struct server *sv;
3882
3883 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3884 return 1;
3885
3886 sv = cli_find_server(appctx, args[2]);
3887 if (!sv)
3888 return 1;
3889
3890 srv_adm_set_ready(sv);
3891 return 1;
3892}
3893
William Lallemand222baf22016-11-19 02:00:33 +01003894/* register cli keywords */
3895static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003896 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003897 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003898 { { "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 +01003899 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003900 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003901 { { "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 +01003902 { { "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 +01003903 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01003904 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
3905 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
3906
William Lallemand222baf22016-11-19 02:00:33 +01003907 {{},}
3908}};
3909
3910__attribute__((constructor))
3911static void __server_init(void)
3912{
3913 cli_register_kw(&cli_kws);
3914}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003915
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003916/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003917 * Local variables:
3918 * c-indent-level: 8
3919 * c-basic-offset: 8
3920 * End:
3921 */