blob: ddb2842ee774c60bdf5c4bf8d2cc7f4e226bbfee [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;
Willy Tarreau272adea2014-03-31 10:39:59 +02001298
1299 cur_arg = 3;
1300 } else {
1301 newsrv = &curproxy->defsrv;
1302 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001303 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001304 }
1305
1306 while (*args[cur_arg]) {
1307 if (!strcmp(args[cur_arg], "agent-check")) {
1308 global.maxsock++;
1309 do_agent = 1;
1310 cur_arg += 1;
1311 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1312 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1313 if (err) {
1314 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1315 file, linenum, *err, newsrv->id);
1316 err_code |= ERR_ALERT | ERR_FATAL;
1317 goto out;
1318 }
1319 if (val <= 0) {
1320 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1321 file, linenum, val, args[cur_arg], newsrv->id);
1322 err_code |= ERR_ALERT | ERR_FATAL;
1323 goto out;
1324 }
1325 newsrv->agent.inter = val;
1326 cur_arg += 2;
1327 }
Misiekea849332017-01-09 09:39:51 +01001328 else if (!strcmp(args[cur_arg], "agent-addr")) {
1329 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1330 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1331 goto out;
1332 }
1333
1334 cur_arg += 2;
1335 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001336 else if (!strcmp(args[cur_arg], "agent-port")) {
1337 global.maxsock++;
1338 newsrv->agent.port = atol(args[cur_arg + 1]);
1339 cur_arg += 2;
1340 }
James Brown55f9ff12015-10-21 18:19:05 -07001341 else if (!strcmp(args[cur_arg], "agent-send")) {
1342 global.maxsock++;
1343 free(newsrv->agent.send_string);
1344 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1345 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1346 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1347 cur_arg += 2;
1348 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001349 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1350 newsrv->cookie = strdup(args[cur_arg + 1]);
1351 newsrv->cklen = strlen(args[cur_arg + 1]);
Olivier Houchard4e694042017-03-14 20:01:29 +01001352 newsrv->flags |= SRV_F_COOKIESET;
Willy Tarreau272adea2014-03-31 10:39:59 +02001353 cur_arg += 2;
1354 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001355 else if (!strcmp(args[cur_arg], "init-addr")) {
1356 char *p, *end;
1357 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001358 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001359
1360 newsrv->init_addr_methods = 0;
1361 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1362
1363 for (p = args[cur_arg + 1]; *p; p = end) {
1364 /* cut on next comma */
1365 for (end = p; *end && *end != ','; end++);
1366 if (*end)
1367 *(end++) = 0;
1368
Willy Tarreau4310d362016-11-02 15:05:56 +01001369 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001370 if (!strcmp(p, "libc")) {
1371 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1372 }
1373 else if (!strcmp(p, "last")) {
1374 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1375 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001376 else if (!strcmp(p, "none")) {
1377 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1378 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001379 else if (str2ip2(p, &sa, 0)) {
1380 if (is_addr(&newsrv->init_addr)) {
1381 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1382 file, linenum, args[cur_arg], p);
1383 err_code |= ERR_ALERT | ERR_FATAL;
1384 goto out;
1385 }
1386 newsrv->init_addr = sa;
1387 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1388 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001389 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001390 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001391 file, linenum, args[cur_arg], p);
1392 err_code |= ERR_ALERT | ERR_FATAL;
1393 goto out;
1394 }
1395 if (!done) {
1396 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1397 file, linenum, args[cur_arg], p);
1398 err_code |= ERR_ALERT | ERR_FATAL;
1399 goto out;
1400 }
1401 }
1402 cur_arg += 2;
1403 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001404 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1405 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1406 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1407 cur_arg += 2;
1408 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001409 else if (!strcmp(args[cur_arg], "resolvers")) {
1410 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1411 cur_arg += 2;
1412 }
1413 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1414 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001415 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001416 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001417 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001418 else {
1419 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1420 file, linenum, args[cur_arg]);
1421 err_code |= ERR_ALERT | ERR_FATAL;
1422 goto out;
1423 }
1424 cur_arg += 2;
1425 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001426 else if (!strcmp(args[cur_arg], "resolve-net")) {
1427 char *p, *e;
1428 unsigned char mask;
1429 struct dns_options *opt;
1430
1431 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1432 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1433 file, linenum, args[cur_arg]);
1434 err_code |= ERR_ALERT | ERR_FATAL;
1435 goto out;
1436 }
1437
1438 opt = &newsrv->dns_opts;
1439
1440 /* Split arguments by comma, and convert it from ipv4 or ipv6
1441 * string network in in_addr or in6_addr.
1442 */
1443 p = args[cur_arg + 1];
1444 e = p;
1445 while (*p != '\0') {
1446 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001447 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001448 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1449 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1450 err_code |= ERR_ALERT | ERR_FATAL;
1451 goto out;
1452 }
1453 /* look for end or comma. */
1454 while (*e != ',' && *e != '\0')
1455 e++;
1456 if (*e == ',') {
1457 *e = '\0';
1458 e++;
1459 }
1460 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1461 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1462 /* Try to convert input string from ipv4 or ipv6 network. */
1463 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1464 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1465 &mask)) {
1466 /* Try to convert input string from ipv6 network. */
1467 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1468 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1469 } else {
1470 /* All network conversions fail, retrun error. */
1471 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1472 file, linenum, args[cur_arg], p);
1473 err_code |= ERR_ALERT | ERR_FATAL;
1474 goto out;
1475 }
1476 opt->pref_net_nb++;
1477 p = e;
1478 }
1479
1480 cur_arg += 2;
1481 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001482 else if (!strcmp(args[cur_arg], "rise")) {
1483 if (!*args[cur_arg + 1]) {
1484 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1485 file, linenum, args[cur_arg]);
1486 err_code |= ERR_ALERT | ERR_FATAL;
1487 goto out;
1488 }
1489
1490 newsrv->check.rise = atol(args[cur_arg + 1]);
1491 if (newsrv->check.rise <= 0) {
1492 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1493 file, linenum, args[cur_arg]);
1494 err_code |= ERR_ALERT | ERR_FATAL;
1495 goto out;
1496 }
1497
1498 if (newsrv->check.health)
1499 newsrv->check.health = newsrv->check.rise;
1500 cur_arg += 2;
1501 }
1502 else if (!strcmp(args[cur_arg], "fall")) {
1503 newsrv->check.fall = atol(args[cur_arg + 1]);
1504
1505 if (!*args[cur_arg + 1]) {
1506 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1507 file, linenum, args[cur_arg]);
1508 err_code |= ERR_ALERT | ERR_FATAL;
1509 goto out;
1510 }
1511
1512 if (newsrv->check.fall <= 0) {
1513 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1514 file, linenum, args[cur_arg]);
1515 err_code |= ERR_ALERT | ERR_FATAL;
1516 goto out;
1517 }
1518
1519 cur_arg += 2;
1520 }
1521 else if (!strcmp(args[cur_arg], "inter")) {
1522 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1523 if (err) {
1524 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1525 file, linenum, *err, newsrv->id);
1526 err_code |= ERR_ALERT | ERR_FATAL;
1527 goto out;
1528 }
1529 if (val <= 0) {
1530 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1531 file, linenum, val, args[cur_arg], newsrv->id);
1532 err_code |= ERR_ALERT | ERR_FATAL;
1533 goto out;
1534 }
1535 newsrv->check.inter = val;
1536 cur_arg += 2;
1537 }
1538 else if (!strcmp(args[cur_arg], "fastinter")) {
1539 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1540 if (err) {
1541 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1542 file, linenum, *err, newsrv->id);
1543 err_code |= ERR_ALERT | ERR_FATAL;
1544 goto out;
1545 }
1546 if (val <= 0) {
1547 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1548 file, linenum, val, args[cur_arg], newsrv->id);
1549 err_code |= ERR_ALERT | ERR_FATAL;
1550 goto out;
1551 }
1552 newsrv->check.fastinter = val;
1553 cur_arg += 2;
1554 }
1555 else if (!strcmp(args[cur_arg], "downinter")) {
1556 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1557 if (err) {
1558 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1559 file, linenum, *err, newsrv->id);
1560 err_code |= ERR_ALERT | ERR_FATAL;
1561 goto out;
1562 }
1563 if (val <= 0) {
1564 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1565 file, linenum, val, args[cur_arg], newsrv->id);
1566 err_code |= ERR_ALERT | ERR_FATAL;
1567 goto out;
1568 }
1569 newsrv->check.downinter = val;
1570 cur_arg += 2;
1571 }
1572 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1573 struct sockaddr_storage *sk;
1574 int port1, port2;
1575 struct protocol *proto;
1576
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001577 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001578 if (!sk) {
1579 Alert("parsing [%s:%d] : '%s' : %s\n",
1580 file, linenum, args[cur_arg], errmsg);
1581 err_code |= ERR_ALERT | ERR_FATAL;
1582 goto out;
1583 }
1584
1585 proto = protocol_by_family(sk->ss_family);
1586 if (!proto || !proto->connect) {
1587 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1588 file, linenum, args[cur_arg], args[cur_arg + 1]);
1589 err_code |= ERR_ALERT | ERR_FATAL;
1590 goto out;
1591 }
1592
1593 if (port1 != port2) {
1594 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1595 file, linenum, args[cur_arg], args[cur_arg + 1]);
1596 err_code |= ERR_ALERT | ERR_FATAL;
1597 goto out;
1598 }
1599
Simon Horman41f58762015-01-30 11:22:56 +09001600 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001601 newsrv->flags |= SRV_F_CHECKADDR;
1602 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001603 cur_arg += 2;
1604 }
1605 else if (!strcmp(args[cur_arg], "port")) {
1606 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001607 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001608 cur_arg += 2;
1609 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001610 else if (!strcmp(args[cur_arg], "weight")) {
1611 int w;
1612 w = atol(args[cur_arg + 1]);
1613 if (w < 0 || w > SRV_UWGHT_MAX) {
1614 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1615 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1616 err_code |= ERR_ALERT | ERR_FATAL;
1617 goto out;
1618 }
1619 newsrv->uweight = newsrv->iweight = w;
1620 cur_arg += 2;
1621 }
1622 else if (!strcmp(args[cur_arg], "minconn")) {
1623 newsrv->minconn = atol(args[cur_arg + 1]);
1624 cur_arg += 2;
1625 }
1626 else if (!strcmp(args[cur_arg], "maxconn")) {
1627 newsrv->maxconn = atol(args[cur_arg + 1]);
1628 cur_arg += 2;
1629 }
1630 else if (!strcmp(args[cur_arg], "maxqueue")) {
1631 newsrv->maxqueue = atol(args[cur_arg + 1]);
1632 cur_arg += 2;
1633 }
1634 else if (!strcmp(args[cur_arg], "slowstart")) {
1635 /* slowstart is stored in seconds */
1636 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1637 if (err) {
1638 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1639 file, linenum, *err, newsrv->id);
1640 err_code |= ERR_ALERT | ERR_FATAL;
1641 goto out;
1642 }
1643 newsrv->slowstart = (val + 999) / 1000;
1644 cur_arg += 2;
1645 }
1646 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1647
1648 if (!*args[cur_arg + 1]) {
1649 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1650 file, linenum);
1651 err_code |= ERR_ALERT | ERR_FATAL;
1652 goto out;
1653 }
1654
1655 newsrv->trackit = strdup(args[cur_arg + 1]);
1656
1657 cur_arg += 2;
1658 }
1659 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1660 global.maxsock++;
1661 do_check = 1;
1662 cur_arg += 1;
1663 }
1664 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001665 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001666 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001667 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001668 newsrv->check.state |= CHK_ST_PAUSED;
1669 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001670 cur_arg += 1;
1671 }
1672 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1673 if (!strcmp(args[cur_arg + 1], "none"))
1674 newsrv->observe = HANA_OBS_NONE;
1675 else if (!strcmp(args[cur_arg + 1], "layer4"))
1676 newsrv->observe = HANA_OBS_LAYER4;
1677 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1678 if (curproxy->mode != PR_MODE_HTTP) {
1679 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1680 file, linenum, args[cur_arg + 1]);
1681 err_code |= ERR_ALERT;
1682 }
1683 newsrv->observe = HANA_OBS_LAYER7;
1684 }
1685 else {
1686 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1687 "'layer4', 'layer7' but got '%s'\n",
1688 file, linenum, args[cur_arg], args[cur_arg + 1]);
1689 err_code |= ERR_ALERT | ERR_FATAL;
1690 goto out;
1691 }
1692
1693 cur_arg += 2;
1694 }
1695 else if (!strcmp(args[cur_arg], "on-error")) {
1696 if (!strcmp(args[cur_arg + 1], "fastinter"))
1697 newsrv->onerror = HANA_ONERR_FASTINTER;
1698 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1699 newsrv->onerror = HANA_ONERR_FAILCHK;
1700 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1701 newsrv->onerror = HANA_ONERR_SUDDTH;
1702 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1703 newsrv->onerror = HANA_ONERR_MARKDWN;
1704 else {
1705 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1706 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1707 file, linenum, args[cur_arg], args[cur_arg + 1]);
1708 err_code |= ERR_ALERT | ERR_FATAL;
1709 goto out;
1710 }
1711
1712 cur_arg += 2;
1713 }
1714 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1715 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1716 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1717 else {
1718 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1719 file, linenum, args[cur_arg], args[cur_arg + 1]);
1720 err_code |= ERR_ALERT | ERR_FATAL;
1721 goto out;
1722 }
1723
1724 cur_arg += 2;
1725 }
1726 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1727 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1728 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1729 else {
1730 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1731 file, linenum, args[cur_arg], args[cur_arg + 1]);
1732 err_code |= ERR_ALERT | ERR_FATAL;
1733 goto out;
1734 }
1735
1736 cur_arg += 2;
1737 }
1738 else if (!strcmp(args[cur_arg], "error-limit")) {
1739 if (!*args[cur_arg + 1]) {
1740 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1741 file, linenum, args[cur_arg]);
1742 err_code |= ERR_ALERT | ERR_FATAL;
1743 goto out;
1744 }
1745
1746 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1747
1748 if (newsrv->consecutive_errors_limit <= 0) {
1749 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1750 file, linenum, args[cur_arg]);
1751 err_code |= ERR_ALERT | ERR_FATAL;
1752 goto out;
1753 }
1754 cur_arg += 2;
1755 }
1756 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1757 int port_low, port_high;
1758 struct sockaddr_storage *sk;
1759 struct protocol *proto;
1760
1761 if (!*args[cur_arg + 1]) {
1762 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1763 file, linenum, "source", "usesrc", "interface");
1764 err_code |= ERR_ALERT | ERR_FATAL;
1765 goto out;
1766 }
1767
1768 newsrv->conn_src.opts |= CO_SRC_BIND;
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001769 sk = str2sa_range(args[cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001770 if (!sk) {
1771 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1772 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1773 err_code |= ERR_ALERT | ERR_FATAL;
1774 goto out;
1775 }
1776
1777 proto = protocol_by_family(sk->ss_family);
1778 if (!proto || !proto->connect) {
1779 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1780 file, linenum, args[cur_arg], args[cur_arg+1]);
1781 err_code |= ERR_ALERT | ERR_FATAL;
1782 goto out;
1783 }
1784
1785 newsrv->conn_src.source_addr = *sk;
1786
1787 if (port_low != port_high) {
1788 int i;
1789
1790 if (!port_low || !port_high) {
1791 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1792 file, linenum, args[cur_arg], args[cur_arg + 1]);
1793 err_code |= ERR_ALERT | ERR_FATAL;
1794 goto out;
1795 }
1796
1797 if (port_low <= 0 || port_low > 65535 ||
1798 port_high <= 0 || port_high > 65535 ||
1799 port_low > port_high) {
1800 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1801 file, linenum, port_low, port_high);
1802 err_code |= ERR_ALERT | ERR_FATAL;
1803 goto out;
1804 }
1805 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1806 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1807 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1808 }
1809
1810 cur_arg += 2;
1811 while (*(args[cur_arg])) {
1812 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001813#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001814 if (!*args[cur_arg + 1]) {
1815 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1816 file, linenum, "usesrc");
1817 err_code |= ERR_ALERT | ERR_FATAL;
1818 goto out;
1819 }
1820 if (!strcmp(args[cur_arg + 1], "client")) {
1821 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1822 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1823 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1824 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1825 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1826 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1827 char *name, *end;
1828
1829 name = args[cur_arg+1] + 7;
1830 while (isspace(*name))
1831 name++;
1832
1833 end = name;
1834 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1835 end++;
1836
1837 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1838 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1839 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1840 newsrv->conn_src.bind_hdr_len = end - name;
1841 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1842 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1843 newsrv->conn_src.bind_hdr_occ = -1;
1844
1845 /* now look for an occurrence number */
1846 while (isspace(*end))
1847 end++;
1848 if (*end == ',') {
1849 end++;
1850 name = end;
1851 if (*end == '-')
1852 end++;
1853 while (isdigit((int)*end))
1854 end++;
1855 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1856 }
1857
1858 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1859 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1860 " occurrences values smaller than %d.\n",
1861 file, linenum, MAX_HDR_HISTORY);
1862 err_code |= ERR_ALERT | ERR_FATAL;
1863 goto out;
1864 }
1865 } else {
1866 struct sockaddr_storage *sk;
1867 int port1, port2;
1868
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001869 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001870 if (!sk) {
1871 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1872 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1873 err_code |= ERR_ALERT | ERR_FATAL;
1874 goto out;
1875 }
1876
1877 proto = protocol_by_family(sk->ss_family);
1878 if (!proto || !proto->connect) {
1879 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1880 file, linenum, args[cur_arg], args[cur_arg+1]);
1881 err_code |= ERR_ALERT | ERR_FATAL;
1882 goto out;
1883 }
1884
1885 if (port1 != port2) {
1886 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1887 file, linenum, args[cur_arg], args[cur_arg + 1]);
1888 err_code |= ERR_ALERT | ERR_FATAL;
1889 goto out;
1890 }
1891 newsrv->conn_src.tproxy_addr = *sk;
1892 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1893 }
1894 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001895 cur_arg += 2;
1896 continue;
1897#else /* no TPROXY support */
1898 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1899 file, linenum, "usesrc");
1900 err_code |= ERR_ALERT | ERR_FATAL;
1901 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001902#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001903 } /* "usesrc" */
1904
1905 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1906#ifdef SO_BINDTODEVICE
1907 if (!*args[cur_arg + 1]) {
1908 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1909 file, linenum, args[0]);
1910 err_code |= ERR_ALERT | ERR_FATAL;
1911 goto out;
1912 }
1913 free(newsrv->conn_src.iface_name);
1914 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1915 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1916 global.last_checks |= LSTCHK_NETADM;
1917#else
1918 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1919 file, linenum, args[0], args[cur_arg]);
1920 err_code |= ERR_ALERT | ERR_FATAL;
1921 goto out;
1922#endif
1923 cur_arg += 2;
1924 continue;
1925 }
1926 /* this keyword in not an option of "source" */
1927 break;
1928 } /* while */
1929 }
1930 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1931 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1932 file, linenum, "usesrc", "source");
1933 err_code |= ERR_ALERT | ERR_FATAL;
1934 goto out;
1935 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001936 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1937#ifdef CONFIG_HAP_NS
1938 char *arg = args[cur_arg + 1];
1939 if (!strcmp(arg, "*")) {
1940 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1941 } else {
1942 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1943
1944 if (newsrv->netns == NULL)
1945 newsrv->netns = netns_store_insert(arg);
1946
1947 if (newsrv->netns == NULL) {
1948 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1949 err_code |= ERR_ALERT | ERR_FATAL;
1950 goto out;
1951 }
1952 }
1953#else
1954 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1955 file, linenum, args[0], args[cur_arg]);
1956 err_code |= ERR_ALERT | ERR_FATAL;
1957 goto out;
1958#endif
1959 cur_arg += 2;
1960 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001961 else {
1962 static int srv_dumped;
1963 struct srv_kw *kw;
1964 char *err;
1965
1966 kw = srv_find_kw(args[cur_arg]);
1967 if (kw) {
1968 char *err = NULL;
1969 int code;
1970
1971 if (!kw->parse) {
1972 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1973 file, linenum, args[0], args[1], args[cur_arg]);
1974 cur_arg += 1 + kw->skip ;
1975 err_code |= ERR_ALERT | ERR_FATAL;
1976 goto out;
1977 }
1978
1979 if (defsrv && !kw->default_ok) {
1980 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1981 file, linenum, args[0], args[1], args[cur_arg]);
1982 cur_arg += 1 + kw->skip ;
1983 err_code |= ERR_ALERT;
1984 continue;
1985 }
1986
1987 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1988 err_code |= code;
1989
1990 if (code) {
1991 if (err && *err) {
1992 indent_msg(&err, 2);
1993 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1994 }
1995 else
1996 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1997 file, linenum, args[0], args[1], args[cur_arg]);
1998 if (code & ERR_FATAL) {
1999 free(err);
2000 cur_arg += 1 + kw->skip;
2001 goto out;
2002 }
2003 }
2004 free(err);
2005 cur_arg += 1 + kw->skip;
2006 continue;
2007 }
2008
2009 err = NULL;
2010 if (!srv_dumped) {
2011 srv_dump_kws(&err);
2012 indent_msg(&err, 4);
2013 srv_dumped = 1;
2014 }
2015
2016 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2017 file, linenum, args[0], args[1], args[cur_arg],
2018 err ? " Registered keywords :" : "", err ? err : "");
2019 free(err);
2020
2021 err_code |= ERR_ALERT | ERR_FATAL;
2022 goto out;
2023 }
2024 }
2025
Willy Tarreau272adea2014-03-31 10:39:59 +02002026 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002027 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002028
2029 if (newsrv->trackit) {
2030 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
2031 file, linenum);
2032 err_code |= ERR_ALERT | ERR_FATAL;
2033 goto out;
2034 }
2035
Willy Tarreau272adea2014-03-31 10:39:59 +02002036 /*
2037 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02002038 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02002039 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002040 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09002041 (is_inet_addr(&newsrv->check.addr) ||
2042 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01002043 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002044 struct list *l;
2045
2046 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
2047 if (!r) {
2048 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
2049 file, linenum, newsrv->id);
2050 err_code |= ERR_ALERT | ERR_FATAL;
2051 goto out;
2052 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002053 /* search the first action (connect / send / expect) in the list */
2054 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002055 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002056 if (r->action != TCPCHK_ACT_COMMENT)
2057 break;
2058 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002059 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
2060 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",
2061 file, linenum, newsrv->id);
2062 err_code |= ERR_ALERT | ERR_FATAL;
2063 goto out;
2064 }
2065 else {
2066 /* scan the tcp-check ruleset to ensure a port has been configured */
2067 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002068 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002069 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
2070 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",
2071 file, linenum, newsrv->id);
2072 err_code |= ERR_ALERT | ERR_FATAL;
2073 goto out;
2074 }
2075 }
2076 }
2077 }
2078
2079 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09002080 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002081 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002082 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2083 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002084 goto out;
2085 }
2086
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002087 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01002088 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002089
Willy Tarreau272adea2014-03-31 10:39:59 +02002090 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2091 }
2092
2093 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002094 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002095
2096 if (!newsrv->agent.port) {
2097 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2098 file, linenum, newsrv->id);
2099 err_code |= ERR_ALERT | ERR_FATAL;
2100 goto out;
2101 }
2102
2103 if (!newsrv->agent.inter)
2104 newsrv->agent.inter = newsrv->check.inter;
2105
Simon Hormanb1900d52015-01-30 11:22:54 +09002106 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02002107 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002108 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2109 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002110 goto out;
2111 }
2112
2113 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2114 }
2115
2116 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02002117 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02002118 curproxy->srv_bck++;
2119 else
2120 curproxy->srv_act++;
2121
Willy Tarreauc5150da2014-05-13 19:27:31 +02002122 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002123 }
2124 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002125 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002126 return 0;
2127
2128 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002129 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 free(errmsg);
2131 return err_code;
2132}
2133
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002134/* Returns a pointer to the first server matching either id <id>.
2135 * NULL is returned if no match is found.
2136 * the lookup is performed in the backend <bk>
2137 */
2138struct server *server_find_by_id(struct proxy *bk, int id)
2139{
2140 struct eb32_node *eb32;
2141 struct server *curserver;
2142
2143 if (!bk || (id ==0))
2144 return NULL;
2145
2146 /* <bk> has no backend capabilities, so it can't have a server */
2147 if (!(bk->cap & PR_CAP_BE))
2148 return NULL;
2149
2150 curserver = NULL;
2151
2152 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2153 if (eb32)
2154 curserver = container_of(eb32, struct server, conf.id);
2155
2156 return curserver;
2157}
2158
2159/* Returns a pointer to the first server matching either name <name>, or id
2160 * if <name> starts with a '#'. NULL is returned if no match is found.
2161 * the lookup is performed in the backend <bk>
2162 */
2163struct server *server_find_by_name(struct proxy *bk, const char *name)
2164{
2165 struct server *curserver;
2166
2167 if (!bk || !name)
2168 return NULL;
2169
2170 /* <bk> has no backend capabilities, so it can't have a server */
2171 if (!(bk->cap & PR_CAP_BE))
2172 return NULL;
2173
2174 curserver = NULL;
2175 if (*name == '#') {
2176 curserver = server_find_by_id(bk, atoi(name + 1));
2177 if (curserver)
2178 return curserver;
2179 }
2180 else {
2181 curserver = bk->srv;
2182
2183 while (curserver && (strcmp(curserver->id, name) != 0))
2184 curserver = curserver->next;
2185
2186 if (curserver)
2187 return curserver;
2188 }
2189
2190 return NULL;
2191}
2192
2193struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2194{
2195 struct server *byname;
2196 struct server *byid;
2197
2198 if (!name && !id)
2199 return NULL;
2200
2201 if (diff)
2202 *diff = 0;
2203
2204 byname = byid = NULL;
2205
2206 if (name) {
2207 byname = server_find_by_name(bk, name);
2208 if (byname && (!id || byname->puid == id))
2209 return byname;
2210 }
2211
2212 /* remaining possibilities :
2213 * - name not set
2214 * - name set but not found
2215 * - name found but ID doesn't match
2216 */
2217 if (id) {
2218 byid = server_find_by_id(bk, id);
2219 if (byid) {
2220 if (byname) {
2221 /* use id only if forced by configuration */
2222 if (byid->flags & SRV_F_FORCED_ID) {
2223 if (diff)
2224 *diff |= 2;
2225 return byid;
2226 }
2227 else {
2228 if (diff)
2229 *diff |= 1;
2230 return byname;
2231 }
2232 }
2233
2234 /* remaining possibilities:
2235 * - name not set
2236 * - name set but not found
2237 */
2238 if (name && diff)
2239 *diff |= 2;
2240 return byid;
2241 }
2242
2243 /* id bot found */
2244 if (byname) {
2245 if (diff)
2246 *diff |= 1;
2247 return byname;
2248 }
2249 }
2250
2251 return NULL;
2252}
2253
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002254/* Update a server state using the parameters available in the params list */
2255static void srv_update_state(struct server *srv, int version, char **params)
2256{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002257 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002258 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002259
2260 /* fields since version 1
2261 * and common to all other upcoming versions
2262 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002263 enum srv_state srv_op_state;
2264 enum srv_admin srv_admin_state;
2265 unsigned srv_uweight, srv_iweight;
2266 unsigned long srv_last_time_change;
2267 short srv_check_status;
2268 enum chk_result srv_check_result;
2269 int srv_check_health;
2270 int srv_check_state, srv_agent_state;
2271 int bk_f_forced_id;
2272 int srv_f_forced_id;
2273
Willy Tarreau31138fa2015-09-29 18:38:47 +02002274 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002275 switch (version) {
2276 case 1:
2277 /*
2278 * now we can proceed with server's state update:
2279 * srv_addr: params[0]
2280 * srv_op_state: params[1]
2281 * srv_admin_state: params[2]
2282 * srv_uweight: params[3]
2283 * srv_iweight: params[4]
2284 * srv_last_time_change: params[5]
2285 * srv_check_status: params[6]
2286 * srv_check_result: params[7]
2287 * srv_check_health: params[8]
2288 * srv_check_state: params[9]
2289 * srv_agent_state: params[10]
2290 * bk_f_forced_id: params[11]
2291 * srv_f_forced_id: params[12]
2292 */
2293
2294 /* validating srv_op_state */
2295 p = NULL;
2296 errno = 0;
2297 srv_op_state = strtol(params[1], &p, 10);
2298 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2299 (srv_op_state != SRV_ST_STOPPED &&
2300 srv_op_state != SRV_ST_STARTING &&
2301 srv_op_state != SRV_ST_RUNNING &&
2302 srv_op_state != SRV_ST_STOPPING)) {
2303 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2304 }
2305
2306 /* validating srv_admin_state */
2307 p = NULL;
2308 errno = 0;
2309 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002310
2311 /* inherited statuses will be recomputed later */
2312 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2313
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002314 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2315 (srv_admin_state != 0 &&
2316 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002317 srv_admin_state != SRV_ADMF_CMAINT &&
2318 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002319 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002320 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002321 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2322 }
2323
2324 /* validating srv_uweight */
2325 p = NULL;
2326 errno = 0;
2327 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002328 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002329 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2330
2331 /* validating srv_iweight */
2332 p = NULL;
2333 errno = 0;
2334 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002335 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002336 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2337
2338 /* validating srv_last_time_change */
2339 p = NULL;
2340 errno = 0;
2341 srv_last_time_change = strtol(params[5], &p, 10);
2342 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2343 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2344
2345 /* validating srv_check_status */
2346 p = NULL;
2347 errno = 0;
2348 srv_check_status = strtol(params[6], &p, 10);
2349 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2350 (srv_check_status >= HCHK_STATUS_SIZE))
2351 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2352
2353 /* validating srv_check_result */
2354 p = NULL;
2355 errno = 0;
2356 srv_check_result = strtol(params[7], &p, 10);
2357 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2358 (srv_check_result != CHK_RES_UNKNOWN &&
2359 srv_check_result != CHK_RES_NEUTRAL &&
2360 srv_check_result != CHK_RES_FAILED &&
2361 srv_check_result != CHK_RES_PASSED &&
2362 srv_check_result != CHK_RES_CONDPASS)) {
2363 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2364 }
2365
2366 /* validating srv_check_health */
2367 p = NULL;
2368 errno = 0;
2369 srv_check_health = strtol(params[8], &p, 10);
2370 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2371 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2372
2373 /* validating srv_check_state */
2374 p = NULL;
2375 errno = 0;
2376 srv_check_state = strtol(params[9], &p, 10);
2377 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2378 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2379 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2380
2381 /* validating srv_agent_state */
2382 p = NULL;
2383 errno = 0;
2384 srv_agent_state = strtol(params[10], &p, 10);
2385 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2386 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2387 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2388
2389 /* validating bk_f_forced_id */
2390 p = NULL;
2391 errno = 0;
2392 bk_f_forced_id = strtol(params[11], &p, 10);
2393 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2394 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2395
2396 /* validating srv_f_forced_id */
2397 p = NULL;
2398 errno = 0;
2399 srv_f_forced_id = strtol(params[12], &p, 10);
2400 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2401 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2402
2403
2404 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002405 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002406 goto out;
2407
2408 /* recover operational state and apply it to this server
2409 * and all servers tracking this one */
2410 switch (srv_op_state) {
2411 case SRV_ST_STOPPED:
2412 srv->check.health = 0;
2413 srv_set_stopped(srv, "changed from server-state after a reload");
2414 break;
2415 case SRV_ST_STARTING:
2416 srv->state = srv_op_state;
2417 break;
2418 case SRV_ST_STOPPING:
2419 srv->check.health = srv->check.rise + srv->check.fall - 1;
2420 srv_set_stopping(srv, "changed from server-state after a reload");
2421 break;
2422 case SRV_ST_RUNNING:
2423 srv->check.health = srv->check.rise + srv->check.fall - 1;
2424 srv_set_running(srv, "");
2425 break;
2426 }
2427
2428 /* When applying server state, the following rules apply:
2429 * - in case of a configuration change, we apply the setting from the new
2430 * configuration, regardless of old running state
2431 * - if no configuration change, we apply old running state only if old running
2432 * state is different from new configuration state
2433 */
2434 /* configuration has changed */
2435 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2436 if (srv->admin & SRV_ADMF_CMAINT)
2437 srv_adm_set_maint(srv);
2438 else
2439 srv_adm_set_ready(srv);
2440 }
2441 /* configuration is the same, let's compate old running state and new conf state */
2442 else {
2443 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2444 srv_adm_set_maint(srv);
2445 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2446 srv_adm_set_ready(srv);
2447 }
2448 /* apply drain mode if server is currently enabled */
2449 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2450 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002451 * (srv->iweight is the weight set up in configuration).
2452 * There are two possible reasons for FDRAIN to have been present :
2453 * - previous config weight was zero
2454 * - "set server b/s drain" was sent to the CLI
2455 *
2456 * In the first case, we simply want to drop this drain state
2457 * if the new weight is not zero anymore, meaning the administrator
2458 * has intentionally turned the weight back to a positive value to
2459 * enable the server again after an operation. In the second case,
2460 * the drain state was forced on the CLI regardless of the config's
2461 * weight so we don't want a change to the config weight to lose this
2462 * status. What this means is :
2463 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2464 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002465 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002466 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002467 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002468 }
2469
2470 srv->last_change = date.tv_sec - srv_last_time_change;
2471 srv->check.status = srv_check_status;
2472 srv->check.result = srv_check_result;
2473 srv->check.health = srv_check_health;
2474
2475 /* Only case we want to apply is removing ENABLED flag which could have been
2476 * done by the "disable health" command over the stats socket
2477 */
2478 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2479 (srv_check_state & CHK_ST_CONFIGURED) &&
2480 !(srv_check_state & CHK_ST_ENABLED))
2481 srv->check.state &= ~CHK_ST_ENABLED;
2482
2483 /* Only case we want to apply is removing ENABLED flag which could have been
2484 * done by the "disable agent" command over the stats socket
2485 */
2486 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2487 (srv_agent_state & CHK_ST_CONFIGURED) &&
2488 !(srv_agent_state & CHK_ST_ENABLED))
2489 srv->agent.state &= ~CHK_ST_ENABLED;
2490
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002491 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2492 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002493 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002494 * It means that a configuration file change has precedence over a unix socket change
2495 * for server's weight
2496 *
2497 * by default, HAProxy applies the following weight when parsing the configuration
2498 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002499 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002500 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002501 srv->uweight = srv_uweight;
2502 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002503 server_recalc_eweight(srv);
2504
Willy Tarreaue5a60682016-11-09 14:54:53 +01002505 /* load server IP address */
2506 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002507 break;
2508 default:
2509 chunk_appendf(msg, ", version '%d' not supported", version);
2510 }
2511
2512 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002513 if (msg->len) {
2514 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002515 Warning("server-state application failed for server '%s/%s'%s",
2516 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002517 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002518}
2519
2520/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2521 * For each proxy, it does the following:
2522 * - opens its server state file (either one or local one)
2523 * - read whole file, line by line
2524 * - analyse each line to check if it matches our current backend:
2525 * - backend name matches
2526 * - backend id matches if id is forced and name doesn't match
2527 * - if the server pointed by the line is found, then state is applied
2528 *
2529 * If the running backend uuid or id differs from the state file, then HAProxy reports
2530 * a warning.
2531 */
2532void apply_server_state(void)
2533{
2534 char *cur, *end;
2535 char mybuf[SRV_STATE_LINE_MAXLEN];
2536 int mybuflen;
2537 char *params[SRV_STATE_FILE_MAX_FIELDS];
2538 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2539 int arg, srv_arg, version, diff;
2540 FILE *f;
2541 char *filepath;
2542 char globalfilepath[MAXPATHLEN + 1];
2543 char localfilepath[MAXPATHLEN + 1];
2544 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2545 extern struct proxy *proxy;
2546 struct proxy *curproxy, *bk;
2547 struct server *srv;
2548
2549 globalfilepathlen = 0;
2550 /* create the globalfilepath variable */
2551 if (global.server_state_file) {
2552 /* absolute path or no base directory provided */
2553 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2554 len = strlen(global.server_state_file);
2555 if (len > MAXPATHLEN) {
2556 globalfilepathlen = 0;
2557 goto globalfileerror;
2558 }
2559 memcpy(globalfilepath, global.server_state_file, len);
2560 globalfilepath[len] = '\0';
2561 globalfilepathlen = len;
2562 }
2563 else if (global.server_state_base) {
2564 len = strlen(global.server_state_base);
2565 globalfilepathlen += len;
2566
2567 if (globalfilepathlen > MAXPATHLEN) {
2568 globalfilepathlen = 0;
2569 goto globalfileerror;
2570 }
2571 strncpy(globalfilepath, global.server_state_base, len);
2572 globalfilepath[globalfilepathlen] = 0;
2573
2574 /* append a slash if needed */
2575 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2576 if (globalfilepathlen + 1 > MAXPATHLEN) {
2577 globalfilepathlen = 0;
2578 goto globalfileerror;
2579 }
2580 globalfilepath[globalfilepathlen++] = '/';
2581 }
2582
2583 len = strlen(global.server_state_file);
2584 if (globalfilepathlen + len > MAXPATHLEN) {
2585 globalfilepathlen = 0;
2586 goto globalfileerror;
2587 }
2588 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2589 globalfilepathlen += len;
2590 globalfilepath[globalfilepathlen++] = 0;
2591 }
2592 }
2593 globalfileerror:
2594 if (globalfilepathlen == 0)
2595 globalfilepath[0] = '\0';
2596
2597 /* read servers state from local file */
2598 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2599 /* servers are only in backends */
2600 if (!(curproxy->cap & PR_CAP_BE))
2601 continue;
2602 fileopenerr = 0;
2603 filepath = NULL;
2604
2605 /* search server state file path and name */
2606 switch (curproxy->load_server_state_from_file) {
2607 /* read servers state from global file */
2608 case PR_SRV_STATE_FILE_GLOBAL:
2609 /* there was an error while generating global server state file path */
2610 if (globalfilepathlen == 0)
2611 continue;
2612 filepath = globalfilepath;
2613 fileopenerr = 1;
2614 break;
2615 /* this backend has its own file */
2616 case PR_SRV_STATE_FILE_LOCAL:
2617 localfilepathlen = 0;
2618 localfilepath[0] = '\0';
2619 len = 0;
2620 /* create the localfilepath variable */
2621 /* absolute path or no base directory provided */
2622 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2623 len = strlen(curproxy->server_state_file_name);
2624 if (len > MAXPATHLEN) {
2625 localfilepathlen = 0;
2626 goto localfileerror;
2627 }
2628 memcpy(localfilepath, curproxy->server_state_file_name, len);
2629 localfilepath[len] = '\0';
2630 localfilepathlen = len;
2631 }
2632 else if (global.server_state_base) {
2633 len = strlen(global.server_state_base);
2634 localfilepathlen += len;
2635
2636 if (localfilepathlen > MAXPATHLEN) {
2637 localfilepathlen = 0;
2638 goto localfileerror;
2639 }
2640 strncpy(localfilepath, global.server_state_base, len);
2641 localfilepath[localfilepathlen] = 0;
2642
2643 /* append a slash if needed */
2644 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2645 if (localfilepathlen + 1 > MAXPATHLEN) {
2646 localfilepathlen = 0;
2647 goto localfileerror;
2648 }
2649 localfilepath[localfilepathlen++] = '/';
2650 }
2651
2652 len = strlen(curproxy->server_state_file_name);
2653 if (localfilepathlen + len > MAXPATHLEN) {
2654 localfilepathlen = 0;
2655 goto localfileerror;
2656 }
2657 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2658 localfilepathlen += len;
2659 localfilepath[localfilepathlen++] = 0;
2660 }
2661 filepath = localfilepath;
2662 localfileerror:
2663 if (localfilepathlen == 0)
2664 localfilepath[0] = '\0';
2665
2666 break;
2667 case PR_SRV_STATE_FILE_NONE:
2668 default:
2669 continue;
2670 }
2671
2672 /* preload global state file */
2673 errno = 0;
2674 f = fopen(filepath, "r");
2675 if (errno && fileopenerr)
2676 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2677 if (!f)
2678 continue;
2679
2680 mybuf[0] = '\0';
2681 mybuflen = 0;
2682 version = 0;
2683
2684 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002685 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2686 Warning("Can't read first line of the server state file '%s'\n", filepath);
2687 goto fileclose;
2688 }
2689
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002690 cur = mybuf;
2691 version = atoi(cur);
2692 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2693 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002694 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002695
2696 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2697 int bk_f_forced_id = 0;
2698 int check_id = 0;
2699 int check_name = 0;
2700
2701 mybuflen = strlen(mybuf);
2702 cur = mybuf;
2703 end = cur + mybuflen;
2704
2705 bk = NULL;
2706 srv = NULL;
2707
2708 /* we need at least one character */
2709 if (mybuflen == 0)
2710 continue;
2711
2712 /* ignore blank characters at the beginning of the line */
2713 while (isspace(*cur))
2714 ++cur;
2715
2716 if (cur == end)
2717 continue;
2718
2719 /* ignore comment line */
2720 if (*cur == '#')
2721 continue;
2722
2723 /* we're now ready to move the line into *srv_params[] */
2724 params[0] = cur;
2725 arg = 1;
2726 srv_arg = 0;
2727 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2728 if (isspace(*cur)) {
2729 *cur = '\0';
2730 ++cur;
2731 while (isspace(*cur))
2732 ++cur;
2733 switch (version) {
2734 case 1:
2735 /*
2736 * srv_addr: params[4] => srv_params[0]
2737 * srv_op_state: params[5] => srv_params[1]
2738 * srv_admin_state: params[6] => srv_params[2]
2739 * srv_uweight: params[7] => srv_params[3]
2740 * srv_iweight: params[8] => srv_params[4]
2741 * srv_last_time_change: params[9] => srv_params[5]
2742 * srv_check_status: params[10] => srv_params[6]
2743 * srv_check_result: params[11] => srv_params[7]
2744 * srv_check_health: params[12] => srv_params[8]
2745 * srv_check_state: params[13] => srv_params[9]
2746 * srv_agent_state: params[14] => srv_params[10]
2747 * bk_f_forced_id: params[15] => srv_params[11]
2748 * srv_f_forced_id: params[16] => srv_params[12]
2749 */
2750 if (arg >= 4) {
2751 srv_params[srv_arg] = cur;
2752 ++srv_arg;
2753 }
2754 break;
2755 }
2756
2757 params[arg] = cur;
2758 ++arg;
2759 }
2760 else {
2761 ++cur;
2762 }
2763 }
2764
2765 /* if line is incomplete line, then ignore it.
2766 * otherwise, update useful flags */
2767 switch (version) {
2768 case 1:
2769 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2770 continue;
2771 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2772 check_id = (atoi(params[0]) == curproxy->uuid);
2773 check_name = (strcmp(curproxy->id, params[1]) == 0);
2774 break;
2775 }
2776
2777 diff = 0;
2778 bk = curproxy;
2779
2780 /* if backend can't be found, let's continue */
2781 if (!check_id && !check_name)
2782 continue;
2783 else if (!check_id && check_name) {
2784 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2785 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2786 }
2787 else if (check_id && !check_name) {
2788 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2789 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2790 /* if name doesn't match, we still want to update curproxy if the backend id
2791 * was forced in previous the previous configuration */
2792 if (!bk_f_forced_id)
2793 continue;
2794 }
2795
2796 /* look for the server by its id: param[2] */
2797 /* else look for the server by its name: param[3] */
2798 diff = 0;
2799 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2800
2801 if (!srv) {
2802 /* if no server found, then warning and continue with next line */
2803 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2804 params[3], params[2], params[0], params[1]);
2805 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2806 params[3], params[2], params[0], params[1]);
2807 continue;
2808 }
2809 else if (diff & PR_FBM_MISMATCH_ID) {
2810 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);
2811 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);
2812 }
2813 else if (diff & PR_FBM_MISMATCH_NAME) {
2814 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);
2815 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);
2816 }
2817
2818 /* now we can proceed with server's state update */
2819 srv_update_state(srv, version, srv_params);
2820 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002821fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002822 fclose(f);
2823 }
2824}
2825
Simon Horman7d09b9a2013-02-12 10:45:51 +09002826/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002827 * update a server's current IP address.
2828 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2829 * ip is in network format.
2830 * updater is a string which contains an information about the requester of the update.
2831 * updater is used if not NULL.
2832 *
2833 * A log line and a stderr warning message is generated based on server's backend options.
2834 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002835int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002836{
2837 /* generates a log line and a warning on stderr */
2838 if (1) {
2839 /* book enough space for both IPv4 and IPv6 */
2840 char oldip[INET6_ADDRSTRLEN];
2841 char newip[INET6_ADDRSTRLEN];
2842
2843 memset(oldip, '\0', INET6_ADDRSTRLEN);
2844 memset(newip, '\0', INET6_ADDRSTRLEN);
2845
2846 /* copy old IP address in a string */
2847 switch (s->addr.ss_family) {
2848 case AF_INET:
2849 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2850 break;
2851 case AF_INET6:
2852 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2853 break;
2854 };
2855
2856 /* copy new IP address in a string */
2857 switch (ip_sin_family) {
2858 case AF_INET:
2859 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2860 break;
2861 case AF_INET6:
2862 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2863 break;
2864 };
2865
2866 /* save log line into a buffer */
2867 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2868 s->proxy->id, s->id, oldip, newip, updater);
2869
2870 /* write the buffer on stderr */
2871 Warning("%s.\n", trash.str);
2872
2873 /* send a log */
2874 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2875 }
2876
2877 /* save the new IP family */
2878 s->addr.ss_family = ip_sin_family;
2879 /* save the new IP address */
2880 switch (ip_sin_family) {
2881 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002882 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002883 break;
2884 case AF_INET6:
2885 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2886 break;
2887 };
Olivier Houchard4e694042017-03-14 20:01:29 +01002888 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002889
2890 return 0;
2891}
2892
2893/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002894 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2895 *
2896 * Caller can pass its name through <updater> to get it integrated in the response message
2897 * returned by the function.
2898 *
2899 * The function first does the following, in that order:
2900 * - validates the new addr and/or port
2901 * - checks if an update is required (new IP or port is different than current ones)
2902 * - checks the update is allowed:
2903 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2904 * - allow all changes if no CHECKS are configured
2905 * - if CHECK is configured:
2906 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2907 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2908 * conditions are met
2909 */
2910const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2911{
2912 struct sockaddr_storage sa;
2913 int ret, port_change_required;
2914 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00002915 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002916 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01002917 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002918
2919 msg = get_trash_chunk();
2920 chunk_reset(msg);
2921
2922 if (addr) {
2923 memset(&sa, 0, sizeof(struct sockaddr_storage));
2924 if (str2ip2(addr, &sa, 0) == NULL) {
2925 chunk_printf(msg, "Invalid addr '%s'", addr);
2926 goto out;
2927 }
2928
2929 /* changes are allowed on AF_INET* families only */
2930 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2931 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2932 goto out;
2933 }
2934
2935 /* collecting data currently setup */
2936 memset(current_addr, '\0', sizeof(current_addr));
2937 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2938 /* changes are allowed on AF_INET* families only */
2939 if ((ret != AF_INET) && (ret != AF_INET6)) {
2940 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2941 goto out;
2942 }
2943
2944 /* applying ADDR changes if required and allowed
2945 * ipcmp returns 0 when both ADDR are the same
2946 */
2947 if (ipcmp(&s->addr, &sa) == 0) {
2948 chunk_appendf(msg, "no need to change the addr");
2949 goto port;
2950 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002951 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01002952 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002953
2954 /* we also need to update check's ADDR only if it uses the server's one */
2955 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002956 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002957 }
2958
2959 /* we also need to update agent ADDR only if it use the server's one */
2960 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002961 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002962 }
2963
2964 /* update report for caller */
2965 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2966 }
2967
2968 port:
2969 if (port) {
2970 char sign = '\0';
2971 char *endptr;
2972
2973 if (addr)
2974 chunk_appendf(msg, ", ");
2975
2976 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01002977 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002978
2979 /* check if PORT change is required */
2980 port_change_required = 0;
2981
2982 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04002983 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002984 new_port = strtol(port, &endptr, 10);
2985 if ((errno != 0) || (port == endptr)) {
2986 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2987 goto out;
2988 }
2989
2990 /* check if caller triggers a port mapped or offset */
2991 if (sign == '-' || (sign == '+')) {
2992 /* check if server currently uses port map */
2993 if (!(s->flags & SRV_F_MAPPORTS)) {
2994 /* switch from fixed port to port map mandatorily triggers
2995 * a port change */
2996 port_change_required = 1;
2997 /* check is configured
2998 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2999 * prevent PORT change if check doesn't have it's dedicated port while switching
3000 * to port mapping */
3001 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3002 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.");
3003 goto out;
3004 }
3005 }
3006 /* we're already using port maps */
3007 else {
3008 port_change_required = current_port != new_port;
3009 }
3010 }
3011 /* fixed port */
3012 else {
3013 port_change_required = current_port != new_port;
3014 }
3015
3016 /* applying PORT changes if required and update response message */
3017 if (port_change_required) {
3018 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003019 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003020 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003021
3022 /* prepare message */
3023 chunk_appendf(msg, "port changed from '");
3024 if (s->flags & SRV_F_MAPPORTS)
3025 chunk_appendf(msg, "+");
3026 chunk_appendf(msg, "%d' to '", current_port);
3027
3028 if (sign == '-') {
3029 s->flags |= SRV_F_MAPPORTS;
3030 chunk_appendf(msg, "%c", sign);
3031 /* just use for result output */
3032 new_port = -new_port;
3033 }
3034 else if (sign == '+') {
3035 s->flags |= SRV_F_MAPPORTS;
3036 chunk_appendf(msg, "%c", sign);
3037 }
3038 else {
3039 s->flags &= ~SRV_F_MAPPORTS;
3040 }
3041
3042 chunk_appendf(msg, "%d'", new_port);
3043
3044 /* we also need to update health checks port only if it uses server's realport */
3045 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3046 s->check.port = new_port;
3047 }
3048 }
3049 else {
3050 chunk_appendf(msg, "no need to change the port");
3051 }
3052 }
3053
3054out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003055 if (changed)
3056 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003057 if (updater)
3058 chunk_appendf(msg, " by '%s'", updater);
3059 chunk_appendf(msg, "\n");
3060 return msg->str;
3061}
3062
3063
3064/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003065 * update server status based on result of name resolution
3066 * returns:
3067 * 0 if server status is updated
3068 * 1 if server status has not changed
3069 */
3070int snr_update_srv_status(struct server *s)
3071{
3072 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003073 struct dns_resolvers *resolvers;
3074
3075 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003076
3077 switch (resolution->status) {
3078 case RSLV_STATUS_NONE:
3079 /* status when HAProxy has just (re)started */
3080 trigger_resolution(s);
3081 break;
3082
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003083 case RSLV_STATUS_VALID:
3084 /*
3085 * resume health checks
3086 * server will be turned back on if health check is safe
3087 */
3088 if (!(s->admin & SRV_ADMF_RMAINT))
3089 return 1;
3090 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3091 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3092 s->proxy->id, s->id);
3093
3094 Warning("%s.\n", trash.str);
3095 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3096 return 0;
3097
3098 case RSLV_STATUS_NX:
3099 /* stop server if resolution is NX for a long enough period */
3100 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3101 if (s->admin & SRV_ADMF_RMAINT)
3102 return 1;
3103 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3104 return 0;
3105 }
3106 break;
3107
3108 case RSLV_STATUS_TIMEOUT:
3109 /* stop server if resolution is TIMEOUT for a long enough period */
3110 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3111 if (s->admin & SRV_ADMF_RMAINT)
3112 return 1;
3113 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3114 return 0;
3115 }
3116 break;
3117
3118 case RSLV_STATUS_REFUSED:
3119 /* stop server if resolution is REFUSED for a long enough period */
3120 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3121 if (s->admin & SRV_ADMF_RMAINT)
3122 return 1;
3123 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3124 return 0;
3125 }
3126 break;
3127
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003128 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003129 /* stop server if resolution is in unmatched error for a long enough period */
3130 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3131 if (s->admin & SRV_ADMF_RMAINT)
3132 return 1;
3133 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3134 return 0;
3135 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003136 break;
3137 }
3138
3139 return 1;
3140}
3141
3142/*
3143 * Server Name Resolution valid response callback
3144 * It expects:
3145 * - <nameserver>: the name server which answered the valid response
3146 * - <response>: buffer containing a valid DNS response
3147 * - <response_len>: size of <response>
3148 * It performs the following actions:
3149 * - ignore response if current ip found and server family not met
3150 * - update with first new ip found if family is met and current IP is not found
3151 * returns:
3152 * 0 on error
3153 * 1 when no error or safe ignore
3154 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003155int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003156{
3157 struct server *s;
3158 void *serverip, *firstip;
3159 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003160 int ret;
3161 struct chunk *chk = get_trash_chunk();
3162
3163 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003164 firstip = NULL; /* pointer to the first valid response found */
3165 /* it will be used as the new IP if a change is required */
3166 firstip_sin_family = AF_UNSPEC;
3167 serverip = NULL; /* current server IP address */
3168
3169 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003170 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003171
3172 /* initializing server IP pointer */
3173 server_sin_family = s->addr.ss_family;
3174 switch (server_sin_family) {
3175 case AF_INET:
3176 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3177 break;
3178
3179 case AF_INET6:
3180 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3181 break;
3182
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003183 case AF_UNSPEC:
3184 break;
3185
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003186 default:
3187 goto invalid;
3188 }
3189
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003190 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003191 serverip, server_sin_family, &firstip,
3192 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003193
3194 switch (ret) {
3195 case DNS_UPD_NO:
3196 if (resolution->status != RSLV_STATUS_VALID) {
3197 resolution->status = RSLV_STATUS_VALID;
3198 resolution->last_status_change = now_ms;
3199 }
3200 goto stop_resolution;
3201
3202 case DNS_UPD_SRVIP_NOT_FOUND:
3203 goto save_ip;
3204
3205 case DNS_UPD_CNAME:
3206 if (resolution->status != RSLV_STATUS_VALID) {
3207 resolution->status = RSLV_STATUS_VALID;
3208 resolution->last_status_change = now_ms;
3209 }
3210 goto invalid;
3211
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003212 case DNS_UPD_NO_IP_FOUND:
3213 if (resolution->status != RSLV_STATUS_OTHER) {
3214 resolution->status = RSLV_STATUS_OTHER;
3215 resolution->last_status_change = now_ms;
3216 }
3217 goto stop_resolution;
3218
Baptiste Assmannfad03182015-10-28 02:03:32 +01003219 case DNS_UPD_NAME_ERROR:
3220 /* if this is not the last expected response, we ignore it */
3221 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3222 return 0;
3223 /* update resolution status to OTHER error type */
3224 if (resolution->status != RSLV_STATUS_OTHER) {
3225 resolution->status = RSLV_STATUS_OTHER;
3226 resolution->last_status_change = now_ms;
3227 }
3228 goto stop_resolution;
3229
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003230 default:
3231 goto invalid;
3232
3233 }
3234
3235 save_ip:
3236 nameserver->counters.update += 1;
3237 if (resolution->status != RSLV_STATUS_VALID) {
3238 resolution->status = RSLV_STATUS_VALID;
3239 resolution->last_status_change = now_ms;
3240 }
3241
3242 /* save the first ip we found */
3243 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3244 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3245
3246 stop_resolution:
3247 /* update last resolution date and time */
3248 resolution->last_resolution = now_ms;
3249 /* reset current status flag */
3250 resolution->step = RSLV_STEP_NONE;
3251 /* reset values */
3252 dns_reset_resolution(resolution);
3253
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003254 dns_update_resolvers_timeout(nameserver->resolvers);
3255
3256 snr_update_srv_status(s);
3257 return 0;
3258
3259 invalid:
3260 nameserver->counters.invalid += 1;
3261 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3262 goto stop_resolution;
3263
3264 snr_update_srv_status(s);
3265 return 0;
3266}
3267
3268/*
3269 * Server Name Resolution error management callback
3270 * returns:
3271 * 0 on error
3272 * 1 when no error or safe ignore
3273 */
3274int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3275{
3276 struct server *s;
3277 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003278 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003279
3280 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003281 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003282 resolvers = resolution->resolvers;
3283
3284 /* can be ignored if this is not the last response */
3285 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3286 return 1;
3287 }
3288
3289 switch (error_code) {
3290 case DNS_RESP_INVALID:
3291 case DNS_RESP_WRONG_NAME:
3292 if (resolution->status != RSLV_STATUS_INVALID) {
3293 resolution->status = RSLV_STATUS_INVALID;
3294 resolution->last_status_change = now_ms;
3295 }
3296 break;
3297
3298 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003299 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003300 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003301 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003302 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003303 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3304 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003305
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003306 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003307 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003308 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003309 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003310 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003311 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003312 }
3313 else if (res_preferred_afinet) {
3314 /* fallback from A to AAAA */
3315 resolution->query_type = DNS_RTYPE_AAAA;
3316 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003317 else {
3318 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003319 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003320 resolution->query_type = DNS_RTYPE_A;
3321 } else {
3322 resolution->query_type = DNS_RTYPE_AAAA;
3323 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003324 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003325
3326 dns_send_query(resolution);
3327
3328 /*
3329 * move the resolution to the last element of the FIFO queue
3330 * and update timeout wakeup based on the new first entry
3331 */
3332 if (dns_check_resolution_queue(resolvers) > 1) {
3333 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003334 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003335 /* ex first resolution goes to the end of the queue */
3336 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3337 }
3338 dns_update_resolvers_timeout(resolvers);
3339 goto leave;
3340 }
3341 else {
3342 if (resolution->status != RSLV_STATUS_OTHER) {
3343 resolution->status = RSLV_STATUS_OTHER;
3344 resolution->last_status_change = now_ms;
3345 }
3346 }
3347 break;
3348
3349 case DNS_RESP_NX_DOMAIN:
3350 if (resolution->status != RSLV_STATUS_NX) {
3351 resolution->status = RSLV_STATUS_NX;
3352 resolution->last_status_change = now_ms;
3353 }
3354 break;
3355
3356 case DNS_RESP_REFUSED:
3357 if (resolution->status != RSLV_STATUS_REFUSED) {
3358 resolution->status = RSLV_STATUS_REFUSED;
3359 resolution->last_status_change = now_ms;
3360 }
3361 break;
3362
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003363 case DNS_RESP_TIMEOUT:
3364 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3365 resolution->status = RSLV_STATUS_TIMEOUT;
3366 resolution->last_status_change = now_ms;
3367 }
3368 break;
3369 }
3370
3371 /* update last resolution date and time */
3372 resolution->last_resolution = now_ms;
3373 /* reset current status flag */
3374 resolution->step = RSLV_STEP_NONE;
3375 /* reset values */
3376 dns_reset_resolution(resolution);
3377
3378 LIST_DEL(&resolution->list);
3379 dns_update_resolvers_timeout(resolvers);
3380
3381 leave:
3382 snr_update_srv_status(s);
3383 return 1;
3384}
3385
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003386/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3387 * resolver. This is suited for initial address configuration. Returns 0 on
3388 * success otherwise a non-zero error code. In case of error, *err_code, if
3389 * not NULL, is filled up.
3390 */
3391int srv_set_addr_via_libc(struct server *srv, int *err_code)
3392{
3393 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003394 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003395 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003396 return 1;
3397 }
3398 return 0;
3399}
3400
3401/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3402 * from the state file. This is suited for initial address configuration.
3403 * Returns 0 on success otherwise a non-zero error code. In case of error,
3404 * *err_code, if not NULL, is filled up.
3405 */
3406static int srv_apply_lastaddr(struct server *srv, int *err_code)
3407{
3408 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3409 if (err_code)
3410 *err_code |= ERR_WARN;
3411 return 1;
3412 }
3413 return 0;
3414}
3415
Willy Tarreau25e51522016-11-04 15:10:17 +01003416/* returns 0 if no error, otherwise a combination of ERR_* flags */
3417static int srv_iterate_initaddr(struct server *srv)
3418{
3419 int return_code = 0;
3420 int err_code;
3421 unsigned int methods;
3422
3423 methods = srv->init_addr_methods;
3424 if (!methods) { // default to "last,libc"
3425 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3426 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3427 }
3428
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003429 /* "-dr" : always append "none" so that server addresses resolution
3430 * failures are silently ignored, this is convenient to validate some
3431 * configs out of their environment.
3432 */
3433 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3434 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3435
Willy Tarreau25e51522016-11-04 15:10:17 +01003436 while (methods) {
3437 err_code = 0;
3438 switch (srv_get_next_initaddr(&methods)) {
3439 case SRV_IADDR_LAST:
3440 if (!srv->lastaddr)
3441 continue;
3442 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003443 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003444 return_code |= err_code;
3445 break;
3446
3447 case SRV_IADDR_LIBC:
3448 if (!srv->hostname)
3449 continue;
3450 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003451 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003452 return_code |= err_code;
3453 break;
3454
Willy Tarreau37ebe122016-11-04 15:17:58 +01003455 case SRV_IADDR_NONE:
3456 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003457 if (return_code) {
3458 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3459 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3460 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003461 return return_code;
3462
Willy Tarreau4310d362016-11-02 15:05:56 +01003463 case SRV_IADDR_IP:
3464 ipcpy(&srv->init_addr, &srv->addr);
3465 if (return_code) {
3466 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3467 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3468 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003469 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003470
Willy Tarreau25e51522016-11-04 15:10:17 +01003471 default: /* unhandled method */
3472 break;
3473 }
3474 }
3475
3476 if (!return_code) {
3477 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3478 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3479 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003480 else {
3481 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3482 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3483 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003484
3485 return_code |= ERR_ALERT | ERR_FATAL;
3486 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003487out:
3488 srv_set_dyncookie(srv);
3489 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003490}
3491
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003492/*
3493 * This function parses all backends and all servers within each backend
3494 * and performs servers' addr resolution based on information provided by:
3495 * - configuration file
3496 * - server-state file (states provided by an 'old' haproxy process)
3497 *
3498 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3499 */
3500int srv_init_addr(void)
3501{
3502 struct proxy *curproxy;
3503 int return_code = 0;
3504
3505 curproxy = proxy;
3506 while (curproxy) {
3507 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003508
3509 /* servers are in backend only */
3510 if (!(curproxy->cap & PR_CAP_BE))
3511 goto srv_init_addr_next;
3512
Willy Tarreau25e51522016-11-04 15:10:17 +01003513 for (srv = curproxy->srv; srv; srv = srv->next)
3514 if (srv->hostname)
3515 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003516
3517 srv_init_addr_next:
3518 curproxy = curproxy->next;
3519 }
3520
3521 return return_code;
3522}
3523
Willy Tarreau21b069d2016-11-23 17:15:08 +01003524/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3525 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003526 * 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 +01003527 * used for CLI commands requiring a server name.
3528 * Important: the <arg> is modified to remove the '/'.
3529 */
3530struct server *cli_find_server(struct appctx *appctx, char *arg)
3531{
3532 struct proxy *px;
3533 struct server *sv;
3534 char *line;
3535
3536 /* split "backend/server" and make <line> point to server */
3537 for (line = arg; *line; line++)
3538 if (*line == '/') {
3539 *line++ = '\0';
3540 break;
3541 }
3542
3543 if (!*line || !*arg) {
3544 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003545 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003546 return NULL;
3547 }
3548
3549 if (!get_backend_server(arg, line, &px, &sv)) {
3550 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003551 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003552 return NULL;
3553 }
3554
3555 if (px->state == PR_STSTOPPED) {
3556 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003557 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003558 return NULL;
3559 }
3560
3561 return sv;
3562}
3563
William Lallemand222baf22016-11-19 02:00:33 +01003564
3565static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3566{
3567 struct server *sv;
3568 const char *warning;
3569
3570 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3571 return 1;
3572
3573 sv = cli_find_server(appctx, args[2]);
3574 if (!sv)
3575 return 1;
3576
3577 if (strcmp(args[3], "weight") == 0) {
3578 warning = server_parse_weight_change_request(sv, args[4]);
3579 if (warning) {
3580 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003581 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003582 }
3583 }
3584 else if (strcmp(args[3], "state") == 0) {
3585 if (strcmp(args[4], "ready") == 0)
3586 srv_adm_set_ready(sv);
3587 else if (strcmp(args[4], "drain") == 0)
3588 srv_adm_set_drain(sv);
3589 else if (strcmp(args[4], "maint") == 0)
3590 srv_adm_set_maint(sv);
3591 else {
3592 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003593 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003594 }
3595 }
3596 else if (strcmp(args[3], "health") == 0) {
3597 if (sv->track) {
3598 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003599 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003600 }
3601 else if (strcmp(args[4], "up") == 0) {
3602 sv->check.health = sv->check.rise + sv->check.fall - 1;
3603 srv_set_running(sv, "changed from CLI");
3604 }
3605 else if (strcmp(args[4], "stopping") == 0) {
3606 sv->check.health = sv->check.rise + sv->check.fall - 1;
3607 srv_set_stopping(sv, "changed from CLI");
3608 }
3609 else if (strcmp(args[4], "down") == 0) {
3610 sv->check.health = 0;
3611 srv_set_stopped(sv, "changed from CLI");
3612 }
3613 else {
3614 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003615 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003616 }
3617 }
3618 else if (strcmp(args[3], "agent") == 0) {
3619 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3620 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003621 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003622 }
3623 else if (strcmp(args[4], "up") == 0) {
3624 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3625 srv_set_running(sv, "changed from CLI");
3626 }
3627 else if (strcmp(args[4], "down") == 0) {
3628 sv->agent.health = 0;
3629 srv_set_stopped(sv, "changed from CLI");
3630 }
3631 else {
3632 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003633 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003634 }
3635 }
Misiek2da082d2017-01-09 09:40:42 +01003636 else if (strcmp(args[3], "agent-addr") == 0) {
3637 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3638 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3639 appctx->st0 = CLI_ST_PRINT;
3640 } else {
3641 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3642 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3643 appctx->st0 = CLI_ST_PRINT;
3644 }
3645 }
3646 }
3647 else if (strcmp(args[3], "agent-send") == 0) {
3648 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3649 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3650 appctx->st0 = CLI_ST_PRINT;
3651 } else {
3652 char *nss = strdup(args[4]);
3653 if (!nss) {
3654 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3655 appctx->st0 = CLI_ST_PRINT;
3656 } else {
3657 free(sv->agent.send_string);
3658 sv->agent.send_string = nss;
3659 sv->agent.send_string_len = strlen(args[4]);
3660 }
3661 }
3662 }
William Lallemand222baf22016-11-19 02:00:33 +01003663 else if (strcmp(args[3], "check-port") == 0) {
3664 int i = 0;
3665 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3666 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003667 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003668 }
3669 if ((i < 0) || (i > 65535)) {
3670 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003671 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003672 }
3673 /* prevent the update of port to 0 if MAPPORTS are in use */
3674 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3675 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003676 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003677 return 1;
3678 }
3679 sv->check.port = i;
3680 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003681 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003682 }
3683 else if (strcmp(args[3], "addr") == 0) {
3684 char *addr = NULL;
3685 char *port = NULL;
3686 if (strlen(args[4]) == 0) {
3687 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003688 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003689 return 1;
3690 }
3691 else {
3692 addr = args[4];
3693 }
3694 if (strcmp(args[5], "port") == 0) {
3695 port = args[6];
3696 }
3697 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3698 if (warning) {
3699 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003700 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003701 }
3702 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3703 }
3704 else {
3705 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 +01003706 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003707 }
3708 return 1;
3709}
3710
William Lallemand6b160942016-11-22 12:34:35 +01003711static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3712{
3713 struct stream_interface *si = appctx->owner;
3714 struct proxy *px;
3715 struct server *sv;
3716 char *line;
3717
3718
3719 /* split "backend/server" and make <line> point to server */
3720 for (line = args[2]; *line; line++)
3721 if (*line == '/') {
3722 *line++ = '\0';
3723 break;
3724 }
3725
3726 if (!*line) {
3727 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003728 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003729 return 1;
3730 }
3731
3732 if (!get_backend_server(args[2], line, &px, &sv)) {
3733 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003734 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003735 return 1;
3736 }
3737
3738 /* return server's effective weight at the moment */
3739 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003740 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01003741 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003742 return 0;
3743 }
William Lallemand6b160942016-11-22 12:34:35 +01003744 return 1;
3745}
3746
3747static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
3748{
3749 struct server *sv;
3750 const char *warning;
3751
3752 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3753 return 1;
3754
3755 sv = cli_find_server(appctx, args[2]);
3756 if (!sv)
3757 return 1;
3758
3759 warning = server_parse_weight_change_request(sv, args[3]);
3760 if (warning) {
3761 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003762 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003763 }
3764 return 1;
3765}
3766
Willy Tarreaub8026272016-11-23 11:26:56 +01003767/* parse a "set maxconn server" command. It always returns 1. */
3768static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
3769{
3770 struct server *sv;
3771 const char *warning;
3772
3773 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3774 return 1;
3775
3776 sv = cli_find_server(appctx, args[3]);
3777 if (!sv)
3778 return 1;
3779
3780 warning = server_parse_maxconn_change_request(sv, args[4]);
3781 if (warning) {
3782 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003783 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01003784 }
3785 return 1;
3786}
William Lallemand6b160942016-11-22 12:34:35 +01003787
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003788/* parse a "disable agent" command. It always returns 1. */
3789static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
3790{
3791 struct server *sv;
3792
3793 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3794 return 1;
3795
3796 sv = cli_find_server(appctx, args[2]);
3797 if (!sv)
3798 return 1;
3799
3800 sv->agent.state &= ~CHK_ST_ENABLED;
3801 return 1;
3802}
3803
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003804/* parse a "disable health" command. It always returns 1. */
3805static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
3806{
3807 struct server *sv;
3808
3809 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3810 return 1;
3811
3812 sv = cli_find_server(appctx, args[2]);
3813 if (!sv)
3814 return 1;
3815
3816 sv->check.state &= ~CHK_ST_ENABLED;
3817 return 1;
3818}
3819
Willy Tarreauffb4d582016-11-24 12:47:00 +01003820/* parse a "disable server" command. It always returns 1. */
3821static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
3822{
3823 struct server *sv;
3824
3825 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3826 return 1;
3827
3828 sv = cli_find_server(appctx, args[2]);
3829 if (!sv)
3830 return 1;
3831
3832 srv_adm_set_maint(sv);
3833 return 1;
3834}
3835
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003836/* parse a "enable agent" command. It always returns 1. */
3837static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
3838{
3839 struct server *sv;
3840
3841 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3842 return 1;
3843
3844 sv = cli_find_server(appctx, args[2]);
3845 if (!sv)
3846 return 1;
3847
3848 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
3849 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003850 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003851 return 1;
3852 }
3853
3854 sv->agent.state |= CHK_ST_ENABLED;
3855 return 1;
3856}
3857
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003858/* parse a "enable health" command. It always returns 1. */
3859static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
3860{
3861 struct server *sv;
3862
3863 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3864 return 1;
3865
3866 sv = cli_find_server(appctx, args[2]);
3867 if (!sv)
3868 return 1;
3869
3870 sv->check.state |= CHK_ST_ENABLED;
3871 return 1;
3872}
3873
Willy Tarreauffb4d582016-11-24 12:47:00 +01003874/* parse a "enable server" command. It always returns 1. */
3875static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
3876{
3877 struct server *sv;
3878
3879 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3880 return 1;
3881
3882 sv = cli_find_server(appctx, args[2]);
3883 if (!sv)
3884 return 1;
3885
3886 srv_adm_set_ready(sv);
3887 return 1;
3888}
3889
William Lallemand222baf22016-11-19 02:00:33 +01003890/* register cli keywords */
3891static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003892 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003893 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003894 { { "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 +01003895 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003896 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003897 { { "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 +01003898 { { "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 +01003899 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01003900 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
3901 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
3902
William Lallemand222baf22016-11-19 02:00:33 +01003903 {{},}
3904}};
3905
3906__attribute__((constructor))
3907static void __server_init(void)
3908{
3909 cli_register_kw(&cli_kws);
3910}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003911
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003912/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003913 * Local variables:
3914 * c-indent-level: 8
3915 * c-basic-offset: 8
3916 * End:
3917 */