blob: 8aaf2184233856987d50e04524784e187790671b [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
17#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020018#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020019#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010020#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020021#include <common/time.h>
22
William Lallemand222baf22016-11-19 02:00:33 +010023#include <types/applet.h>
24#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020025#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010026#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020027#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010028#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029
William Lallemand222baf22016-11-19 02:00:33 +010030#include <proto/applet.h>
31#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090032#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020033#include <proto/port_range.h>
34#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020035#include <proto/queue.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020037#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010038#include <proto/stream_interface.h>
39#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020040#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020041#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020042
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020043static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010044static int srv_apply_lastaddr(struct server *srv, int *err_code);
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreau21faa912012-10-10 08:27:36 +020046/* List head of all known server keywords */
47static struct srv_kw_list srv_keywords = {
48 .list = LIST_HEAD_INIT(srv_keywords.list)
49};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020050
Simon Hormana3608442013-11-01 16:46:15 +090051int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020052{
Willy Tarreau892337c2014-05-13 23:41:20 +020053 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054 return s->down_time;
55
56 return now.tv_sec - s->last_change + s->down_time;
57}
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050059int srv_lastsession(const struct server *s)
60{
61 if (s->counters.last_sess)
62 return now.tv_sec - s->counters.last_sess;
63
64 return -1;
65}
66
Simon Horman4a741432013-02-23 15:35:38 +090067int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020068{
Simon Horman4a741432013-02-23 15:35:38 +090069 const struct server *s = check->server;
70
Willy Tarreauff5ae352013-12-11 20:36:34 +010071 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090072 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010073
Willy Tarreau892337c2014-05-13 23:41:20 +020074 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090075 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010076
Simon Horman4a741432013-02-23 15:35:38 +090077 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010078}
79
Willy Tarreau21faa912012-10-10 08:27:36 +020080/*
81 * Registers the server keyword list <kwl> as a list of valid keywords for next
82 * parsing sessions.
83 */
84void srv_register_keywords(struct srv_kw_list *kwl)
85{
86 LIST_ADDQ(&srv_keywords.list, &kwl->list);
87}
88
89/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
90 * keyword is found with a NULL ->parse() function, then an attempt is made to
91 * find one with a valid ->parse() function. This way it is possible to declare
92 * platform-dependant, known keywords as NULL, then only declare them as valid
93 * if some options are met. Note that if the requested keyword contains an
94 * opening parenthesis, everything from this point is ignored.
95 */
96struct srv_kw *srv_find_kw(const char *kw)
97{
98 int index;
99 const char *kwend;
100 struct srv_kw_list *kwl;
101 struct srv_kw *ret = NULL;
102
103 kwend = strchr(kw, '(');
104 if (!kwend)
105 kwend = kw + strlen(kw);
106
107 list_for_each_entry(kwl, &srv_keywords.list, list) {
108 for (index = 0; kwl->kw[index].kw != NULL; index++) {
109 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
110 kwl->kw[index].kw[kwend-kw] == 0) {
111 if (kwl->kw[index].parse)
112 return &kwl->kw[index]; /* found it !*/
113 else
114 ret = &kwl->kw[index]; /* may be OK */
115 }
116 }
117 }
118 return ret;
119}
120
121/* Dumps all registered "server" keywords to the <out> string pointer. The
122 * unsupported keywords are only dumped if their supported form was not
123 * found.
124 */
125void srv_dump_kws(char **out)
126{
127 struct srv_kw_list *kwl;
128 int index;
129
130 *out = NULL;
131 list_for_each_entry(kwl, &srv_keywords.list, list) {
132 for (index = 0; kwl->kw[index].kw != NULL; index++) {
133 if (kwl->kw[index].parse ||
134 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
135 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
136 kwl->scope,
137 kwl->kw[index].kw,
138 kwl->kw[index].skip ? " <arg>" : "",
139 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
140 kwl->kw[index].parse ? "" : " (not supported)");
141 }
142 }
143 }
144}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100145
Willy Tarreaudff55432012-10-10 17:51:05 +0200146/* parse the "id" server keyword */
147static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
148{
149 struct eb32_node *node;
150
151 if (!*args[*cur_arg + 1]) {
152 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
153 return ERR_ALERT | ERR_FATAL;
154 }
155
156 newsrv->puid = atol(args[*cur_arg + 1]);
157 newsrv->conf.id.key = newsrv->puid;
158
159 if (newsrv->puid <= 0) {
160 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
161 return ERR_ALERT | ERR_FATAL;
162 }
163
164 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
165 if (node) {
166 struct server *target = container_of(node, struct server, conf.id);
167 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
168 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
169 target->id);
170 return ERR_ALERT | ERR_FATAL;
171 }
172
173 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200174 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200175 return 0;
176}
177
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200178/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200179 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200180 * shutdown.
181 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200182void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200183{
Willy Tarreau87b09662015-04-03 00:22:06 +0200184 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200185
Willy Tarreau87b09662015-04-03 00:22:06 +0200186 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
187 if (stream->srv_conn == srv)
188 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200189}
190
191/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200192 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200193 * the reason for the shutdown.
194 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200195void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200196{
197 struct server *srv;
198
199 for (srv = px->srv; srv != NULL; srv = srv->next)
200 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200201 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200202}
203
Willy Tarreaubda92272014-05-20 21:55:30 +0200204/* Appends some information to a message string related to a server going UP or
205 * DOWN. If both <forced> and <reason> are null and the server tracks another
206 * one, a "via" information will be provided to know where the status came from.
207 * If <reason> is non-null, the entire string will be appended after a comma and
208 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200209 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200210 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200211 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200212void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200213{
Willy Tarreaubda92272014-05-20 21:55:30 +0200214 if (reason)
215 chunk_appendf(msg, ", %s", reason);
216 else if (!forced && s->track)
217 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200218
219 if (xferred >= 0) {
220 if (s->state == SRV_ST_STOPPED)
221 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
222 " %d sessions active, %d requeued, %d remaining in queue",
223 s->proxy->srv_act, s->proxy->srv_bck,
224 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
225 s->cur_sess, xferred, s->nbpend);
226 else
227 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
228 " %d sessions requeued, %d total in queue",
229 s->proxy->srv_act, s->proxy->srv_bck,
230 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
231 xferred, s->nbpend);
232 }
233}
234
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200235/* Marks server <s> down, regardless of its checks' statuses, notifies by all
236 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200237 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200238 * recomputes the number of servers, but not the map. Maintenance servers are
239 * ignored. It reports <reason> if non-null as the reason for going down. Note
240 * that it makes use of the trash to build the log strings, so <reason> must
241 * not be placed there.
242 */
243void srv_set_stopped(struct server *s, const char *reason)
244{
245 struct server *srv;
246 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
247 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900248 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200249 int xferred;
250
251 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
252 return;
253
254 s->last_change = now.tv_sec;
255 s->state = SRV_ST_STOPPED;
256 if (s->proxy->lbprm.set_server_status_down)
257 s->proxy->lbprm.set_server_status_down(s);
258
259 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200260 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200261
Willy Tarreau87b09662015-04-03 00:22:06 +0200262 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200263 * a connection. Those which are redispatchable will be queued
264 * to another server or to the proxy itself.
265 */
266 xferred = pendconn_redistribute(s);
267
268 chunk_printf(&trash,
269 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
270 s->proxy->id, s->id);
271
272 srv_append_status(&trash, s, reason, xferred, 0);
273 Warning("%s.\n", trash.str);
274
275 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900276 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
277 send_log(s->proxy, log_level, "%s.\n", trash.str);
278 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200279
280 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
281 set_backend_down(s->proxy);
282
283 s->counters.down_trans++;
284
285 for (srv = s->trackers; srv; srv = srv->tracknext)
286 srv_set_stopped(srv, NULL);
287}
288
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200289/* Marks server <s> up regardless of its checks' statuses and provided it isn't
290 * in maintenance. Notifies by all available means, recounts the remaining
291 * servers on the proxy and tries to grab requests from the proxy. It
292 * automatically recomputes the number of servers, but not the map. Maintenance
293 * servers are ignored. It reports <reason> if non-null as the reason for going
294 * up. Note that it makes use of the trash to build the log strings, so <reason>
295 * must not be placed there.
296 */
297void srv_set_running(struct server *s, const char *reason)
298{
299 struct server *srv;
300 int xferred;
301
302 if (s->admin & SRV_ADMF_MAINT)
303 return;
304
305 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
306 return;
307
308 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
309 if (s->proxy->last_change < now.tv_sec) // ignore negative times
310 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
311 s->proxy->last_change = now.tv_sec;
312 }
313
314 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
315 s->down_time += now.tv_sec - s->last_change;
316
317 s->last_change = now.tv_sec;
318
319 s->state = SRV_ST_STARTING;
320 if (s->slowstart > 0)
321 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
322 else
323 s->state = SRV_ST_RUNNING;
324
325 server_recalc_eweight(s);
326
327 /* If the server is set with "on-marked-up shutdown-backup-sessions",
328 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200329 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200330 * on all backup servers.
331 */
332 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
333 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200334 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200335
336 /* check if we can handle some connections queued at the proxy. We
337 * will take as many as we can handle.
338 */
339 xferred = pendconn_grab_from_px(s);
340
341 chunk_printf(&trash,
342 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
343 s->proxy->id, s->id);
344
345 srv_append_status(&trash, s, reason, xferred, 0);
346 Warning("%s.\n", trash.str);
347 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900348 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200349
350 for (srv = s->trackers; srv; srv = srv->tracknext)
351 srv_set_running(srv, NULL);
352}
353
Willy Tarreau8eb77842014-05-21 13:54:57 +0200354/* Marks server <s> stopping regardless of its checks' statuses and provided it
355 * isn't in maintenance. Notifies by all available means, recounts the remaining
356 * servers on the proxy and tries to grab requests from the proxy. It
357 * automatically recomputes the number of servers, but not the map. Maintenance
358 * servers are ignored. It reports <reason> if non-null as the reason for going
359 * up. Note that it makes use of the trash to build the log strings, so <reason>
360 * must not be placed there.
361 */
362void srv_set_stopping(struct server *s, const char *reason)
363{
364 struct server *srv;
365 int xferred;
366
367 if (s->admin & SRV_ADMF_MAINT)
368 return;
369
370 if (s->state == SRV_ST_STOPPING)
371 return;
372
373 s->last_change = now.tv_sec;
374 s->state = SRV_ST_STOPPING;
375 if (s->proxy->lbprm.set_server_status_down)
376 s->proxy->lbprm.set_server_status_down(s);
377
Willy Tarreau87b09662015-04-03 00:22:06 +0200378 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200379 * a connection. Those which are redispatchable will be queued
380 * to another server or to the proxy itself.
381 */
382 xferred = pendconn_redistribute(s);
383
384 chunk_printf(&trash,
385 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
386 s->proxy->id, s->id);
387
388 srv_append_status(&trash, s, reason, xferred, 0);
389
390 Warning("%s.\n", trash.str);
391 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
392
393 if (!s->proxy->srv_bck && !s->proxy->srv_act)
394 set_backend_down(s->proxy);
395
396 for (srv = s->trackers; srv; srv = srv->tracknext)
397 srv_set_stopping(srv, NULL);
398}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200399
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200400/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
401 * enforce either maint mode or drain mode. It is not allowed to set more than
402 * one flag at once. The equivalent "inherited" flag is propagated to all
403 * tracking servers. Maintenance mode disables health checks (but not agent
404 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100405 * is done. If <cause> is non-null, it will be displayed at the end of the log
406 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200407 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100408void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200409{
410 struct check *check = &s->check;
411 struct server *srv;
412 int xferred;
413
414 if (!mode)
415 return;
416
417 /* stop going down as soon as we meet a server already in the same state */
418 if (s->admin & mode)
419 return;
420
421 s->admin |= mode;
422
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200423 /* stop going down if the equivalent flag was already present (forced or inherited) */
424 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
425 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
426 return;
427
428 /* Maintenance must also disable health checks */
429 if (mode & SRV_ADMF_MAINT) {
430 if (s->check.state & CHK_ST_ENABLED) {
431 s->check.state |= CHK_ST_PAUSED;
432 check->health = 0;
433 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200434
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200435 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200436 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100437 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
438 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
439 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200440
Willy Tarreaubda92272014-05-20 21:55:30 +0200441 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200442
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100443 if (!(global.mode & MODE_STARTING)) {
444 Warning("%s.\n", trash.str);
445 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
446 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200447 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200448 else { /* server was still running */
449 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
450 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
451
452 check->health = 0; /* failure */
453 s->last_change = now.tv_sec;
454 s->state = SRV_ST_STOPPED;
455 if (s->proxy->lbprm.set_server_status_down)
456 s->proxy->lbprm.set_server_status_down(s);
457
458 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200459 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200460
Willy Tarreau87b09662015-04-03 00:22:06 +0200461 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200462 * a connection. Those which are redispatchable will be queued
463 * to another server or to the proxy itself.
464 */
465 xferred = pendconn_redistribute(s);
466
467 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100468 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200469 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +0100470 s->proxy->id, s->id,
471 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200472
473 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
474
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100475 if (!(global.mode & MODE_STARTING)) {
476 Warning("%s.\n", trash.str);
477 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
478 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200479
480 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
481 set_backend_down(s->proxy);
482
483 s->counters.down_trans++;
484 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200485 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200486
487 /* drain state is applied only if not yet in maint */
488 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200489 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
490
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200491 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200492 if (s->proxy->lbprm.set_server_status_down)
493 s->proxy->lbprm.set_server_status_down(s);
494
Willy Tarreau87b09662015-04-03 00:22:06 +0200495 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200496 * a connection. Those which are redispatchable will be queued
497 * to another server or to the proxy itself.
498 */
499 xferred = pendconn_redistribute(s);
500
Willy Tarreau8b428482016-11-07 15:53:43 +0100501 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
502 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
503 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200504
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200505 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200506
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100507 if (!(global.mode & MODE_STARTING)) {
508 Warning("%s.\n", trash.str);
509 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
510 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
511 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200512 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
513 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200514 }
515
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200516 /* compute the inherited flag to propagate */
517 if (mode & SRV_ADMF_MAINT)
518 mode = SRV_ADMF_IMAINT;
519 else if (mode & SRV_ADMF_DRAIN)
520 mode = SRV_ADMF_IDRAIN;
521
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200522 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +0100523 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200524}
525
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200526/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
527 * stop enforcing either maint mode or drain mode. It is not allowed to set more
528 * than one flag at once. The equivalent "inherited" flag is propagated to all
529 * tracking servers. Leaving maintenance mode re-enables health checks. When
530 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200531 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200532void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200533{
534 struct check *check = &s->check;
535 struct server *srv;
536 int xferred = -1;
537
538 if (!mode)
539 return;
540
541 /* stop going down as soon as we see the flag is not there anymore */
542 if (!(s->admin & mode))
543 return;
544
545 s->admin &= ~mode;
546
547 if (s->admin & SRV_ADMF_MAINT) {
548 /* remaining in maintenance mode, let's inform precisely about the
549 * situation.
550 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200551 if (mode & SRV_ADMF_FMAINT) {
552 chunk_printf(&trash,
553 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
554 s->flags & SRV_F_BACKUP ? "Backup " : "",
555 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200556
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200557 if (s->track) /* normally it's mandatory here */
558 chunk_appendf(&trash, " via %s/%s",
559 s->track->proxy->id, s->track->id);
560 Warning("%s.\n", trash.str);
561 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
562 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100563 if (mode & SRV_ADMF_RMAINT) {
564 chunk_printf(&trash,
565 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
566 s->flags & SRV_F_BACKUP ? "Backup " : "",
567 s->proxy->id, s->id, s->hostname);
568
569 if (s->track) /* normally it's mandatory here */
570 chunk_appendf(&trash, " via %s/%s",
571 s->track->proxy->id, s->track->id);
572 Warning("%s.\n", trash.str);
573 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
574 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200575 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200576 chunk_printf(&trash,
577 "%sServer %s/%s remains in forced maintenance",
578 s->flags & SRV_F_BACKUP ? "Backup " : "",
579 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200580 Warning("%s.\n", trash.str);
581 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
582 }
583 /* don't report anything when leaving drain mode and remaining in maintenance */
584 }
585 else if (mode & SRV_ADMF_MAINT) {
586 /* OK here we're leaving maintenance, we have many things to check,
587 * because the server might possibly be coming back up depending on
588 * its state. In practice, leaving maintenance means that we should
589 * immediately turn to UP (more or less the slowstart) under the
590 * following conditions :
591 * - server is neither checked nor tracked
592 * - server tracks another server which is not checked
593 * - server tracks another server which is already up
594 * Which sums up as something simpler :
595 * "either the tracking server is up or the server's checks are disabled
596 * or up". Otherwise we only re-enable health checks. There's a special
597 * case associated to the stopping state which can be inherited. Note
598 * that the server might still be in drain mode, which is naturally dealt
599 * with by the lower level functions.
600 */
601
602 if (s->check.state & CHK_ST_ENABLED) {
603 s->check.state &= ~CHK_ST_PAUSED;
604 check->health = check->rise; /* start OK but check immediately */
605 }
606
607 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
608 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
609 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
610 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
611 if (s->proxy->last_change < now.tv_sec) // ignore negative times
612 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
613 s->proxy->last_change = now.tv_sec;
614 }
615
616 if (s->last_change < now.tv_sec) // ignore negative times
617 s->down_time += now.tv_sec - s->last_change;
618 s->last_change = now.tv_sec;
619
620 if (s->track && s->track->state == SRV_ST_STOPPING)
621 s->state = SRV_ST_STOPPING;
622 else {
623 s->state = SRV_ST_STARTING;
624 if (s->slowstart > 0)
625 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
626 else
627 s->state = SRV_ST_RUNNING;
628 }
629
630 server_recalc_eweight(s);
631
632 /* If the server is set with "on-marked-up shutdown-backup-sessions",
633 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200634 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200635 * on all backup servers.
636 */
637 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
638 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200639 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200640
641 /* check if we can handle some connections queued at the proxy. We
642 * will take as many as we can handle.
643 */
644 xferred = pendconn_grab_from_px(s);
645 }
646
647 if (mode & SRV_ADMF_FMAINT) {
648 chunk_printf(&trash,
649 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
650 s->flags & SRV_F_BACKUP ? "Backup " : "",
651 s->proxy->id, s->id,
652 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
653 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200654 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100655 else if (mode & SRV_ADMF_RMAINT) {
656 chunk_printf(&trash,
657 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
658 s->flags & SRV_F_BACKUP ? "Backup " : "",
659 s->proxy->id, s->id, s->hostname,
660 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
661 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
662 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200663 else {
664 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200665 "%sServer %s/%s is %s/%s (leaving maintenance)",
666 s->flags & SRV_F_BACKUP ? "Backup " : "",
667 s->proxy->id, s->id,
668 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
669 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
670 srv_append_status(&trash, s, NULL, xferred, 0);
671 }
672 Warning("%s.\n", trash.str);
673 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
674 }
675 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
676 /* remaining in drain mode after removing one of its flags */
677
678 if (mode & SRV_ADMF_FDRAIN) {
679 chunk_printf(&trash,
680 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200681 s->flags & SRV_F_BACKUP ? "Backup " : "",
682 s->proxy->id, s->id);
683
684 if (s->track) /* normally it's mandatory here */
685 chunk_appendf(&trash, " via %s/%s",
686 s->track->proxy->id, s->track->id);
687 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200688 else {
689 chunk_printf(&trash,
690 "%sServer %s/%s remains in forced drain mode",
691 s->flags & SRV_F_BACKUP ? "Backup " : "",
692 s->proxy->id, s->id);
693 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200694 Warning("%s.\n", trash.str);
695 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200696 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200697 else if (mode & SRV_ADMF_DRAIN) {
698 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200699 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
700 if (s->proxy->last_change < now.tv_sec) // ignore negative times
701 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
702 s->proxy->last_change = now.tv_sec;
703 }
704
705 if (s->last_change < now.tv_sec) // ignore negative times
706 s->down_time += now.tv_sec - s->last_change;
707 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200708 server_recalc_eweight(s);
709
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200710 if (mode & SRV_ADMF_FDRAIN) {
711 chunk_printf(&trash,
712 "%sServer %s/%s is %s (leaving forced drain)",
713 s->flags & SRV_F_BACKUP ? "Backup " : "",
714 s->proxy->id, s->id,
715 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
716 }
717 else {
718 chunk_printf(&trash,
719 "%sServer %s/%s is %s (leaving drain)",
720 s->flags & SRV_F_BACKUP ? "Backup " : "",
721 s->proxy->id, s->id,
722 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
723 if (s->track) /* normally it's mandatory here */
724 chunk_appendf(&trash, " via %s/%s",
725 s->track->proxy->id, s->track->id);
726 }
727 Warning("%s.\n", trash.str);
728 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200729 }
730
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200731 /* stop going down if the equivalent flag is still present (forced or inherited) */
732 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
733 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
734 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200735
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200736 if (mode & SRV_ADMF_MAINT)
737 mode = SRV_ADMF_IMAINT;
738 else if (mode & SRV_ADMF_DRAIN)
739 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200740
741 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200742 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200743}
744
Willy Tarreau757478e2016-11-03 19:22:19 +0100745/* principle: propagate maint and drain to tracking servers. This is useful
746 * upon startup so that inherited states are correct.
747 */
748static void srv_propagate_admin_state(struct server *srv)
749{
750 struct server *srv2;
751
752 if (!srv->trackers)
753 return;
754
755 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
756 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +0100757 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100758
759 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +0100760 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100761 }
762}
763
764/* Compute and propagate the admin states for all servers in proxy <px>.
765 * Only servers *not* tracking another one are considered, because other
766 * ones will be handled when the server they track is visited.
767 */
768void srv_compute_all_admin_states(struct proxy *px)
769{
770 struct server *srv;
771
772 for (srv = px->srv; srv; srv = srv->next) {
773 if (srv->track)
774 continue;
775 srv_propagate_admin_state(srv);
776 }
777}
778
Willy Tarreaudff55432012-10-10 17:51:05 +0200779/* Note: must not be declared <const> as its list will be overwritten.
780 * Please take care of keeping this list alphabetically sorted, doing so helps
781 * all code contributors.
782 * Optional keywords are also declared with a NULL ->parse() function so that
783 * the config parser can report an appropriate error when a known keyword was
784 * not enabled.
785 */
786static struct srv_kw_list srv_kws = { "ALL", { }, {
787 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
788 { NULL, NULL, 0 },
789}};
790
791__attribute__((constructor))
792static void __listener_init(void)
793{
794 srv_register_keywords(&srv_kws);
795}
796
Willy Tarreau004e0452013-11-21 11:22:01 +0100797/* Recomputes the server's eweight based on its state, uweight, the current time,
798 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
799 * state is automatically disabled if the time is elapsed.
800 */
801void server_recalc_eweight(struct server *sv)
802{
803 struct proxy *px = sv->proxy;
804 unsigned w;
805
806 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
807 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200808 if (sv->state == SRV_ST_STARTING)
809 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100810 }
811
812 /* We must take care of not pushing the server to full throttle during slow starts.
813 * It must also start immediately, at least at the minimal step when leaving maintenance.
814 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200815 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100816 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
817 else
818 w = px->lbprm.wdiv;
819
820 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
821
822 /* now propagate the status change to any LB algorithms */
823 if (px->lbprm.update_server_eweight)
824 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200825 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100826 if (px->lbprm.set_server_status_up)
827 px->lbprm.set_server_status_up(sv);
828 }
829 else {
830 if (px->lbprm.set_server_status_down)
831 px->lbprm.set_server_status_down(sv);
832 }
833}
834
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900836 * Parses weight_str and configures sv accordingly.
837 * Returns NULL on success, error message string otherwise.
838 */
839const char *server_parse_weight_change_request(struct server *sv,
840 const char *weight_str)
841{
842 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900843 long int w;
844 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900845
846 px = sv->proxy;
847
848 /* if the weight is terminated with '%', it is set relative to
849 * the initial weight, otherwise it is absolute.
850 */
851 if (!*weight_str)
852 return "Require <weight> or <weight%>.\n";
853
Simon Hormanb796afa2013-02-12 10:45:53 +0900854 w = strtol(weight_str, &end, 10);
855 if (end == weight_str)
856 return "Empty weight string empty or preceded by garbage";
857 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900858 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900859 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900860 /* Avoid integer overflow */
861 if (w > 25600)
862 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900863 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900864 if (w > 256)
865 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900866 }
867 else if (w < 0 || w > 256)
868 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900869 else if (end[0] != '\0')
870 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900871
872 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
873 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
874
875 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100876 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900877
878 return NULL;
879}
880
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200881/*
Thierry Fournier09a91782016-02-24 08:25:39 +0100882 * Parses <addr_str> and configures <sv> accordingly. <from> precise
883 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200884 * Returns:
885 * - error string on error
886 * - NULL on success
887 */
888const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +0100889 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200890{
891 unsigned char ip[INET6_ADDRSTRLEN];
892
893 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100894 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200895 return NULL;
896 }
897 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100898 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200899 return NULL;
900 }
901
902 return "Could not understand IP address format.\n";
903}
904
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200905const char *server_parse_maxconn_change_request(struct server *sv,
906 const char *maxconn_str)
907{
908 long int v;
909 char *end;
910
911 if (!*maxconn_str)
912 return "Require <maxconn>.\n";
913
914 v = strtol(maxconn_str, &end, 10);
915 if (end == maxconn_str)
916 return "maxconn string empty or preceded by garbage";
917 else if (end[0] != '\0')
918 return "Trailing garbage in maxconn string";
919
920 if (sv->maxconn == sv->minconn) { // static maxconn
921 sv->maxconn = sv->minconn = v;
922 } else { // dynamic maxconn
923 sv->maxconn = v;
924 }
925
926 if (may_dequeue_tasks(sv, sv->proxy))
927 process_srv_queue(sv);
928
929 return NULL;
930}
931
Willy Tarreau272adea2014-03-31 10:39:59 +0200932int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
933{
934 struct server *newsrv = NULL;
935 const char *err;
936 char *errmsg = NULL;
937 int err_code = 0;
938 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +0200939 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +0200940
941 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
942 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +0200943 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
944
945 if (!defsrv && curproxy == defproxy) {
946 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
947 err_code |= ERR_ALERT | ERR_FATAL;
948 goto out;
949 }
950 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
951 err_code |= ERR_ALERT | ERR_FATAL;
952
953 if (!*args[2]) {
954 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
955 file, linenum, args[0]);
956 err_code |= ERR_ALERT | ERR_FATAL;
957 goto out;
958 }
959
960 err = invalid_char(args[1]);
961 if (err && !defsrv) {
962 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
963 file, linenum, *err, args[1]);
964 err_code |= ERR_ALERT | ERR_FATAL;
965 goto out;
966 }
967
968 if (!defsrv) {
969 struct sockaddr_storage *sk;
970 int port1, port2;
971 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200972 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200973
Vincent Bernat02779b62016-04-03 13:48:43 +0200974 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200975 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
976 err_code |= ERR_ALERT | ERR_ABORT;
977 goto out;
978 }
979
980 /* the servers are linked backwards first */
981 newsrv->next = curproxy->srv;
982 curproxy->srv = newsrv;
983 newsrv->proxy = curproxy;
984 newsrv->conf.file = strdup(file);
985 newsrv->conf.line = linenum;
986
987 newsrv->obj_type = OBJ_TYPE_SERVER;
988 LIST_INIT(&newsrv->actconns);
989 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +0200990 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +0200991 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +0200992 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +0200993 do_check = 0;
994 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200995 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200996 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200997 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200998 newsrv->last_change = now.tv_sec;
999 newsrv->id = strdup(args[1]);
1000
1001 /* several ways to check the port component :
1002 * - IP => port=+0, relative (IPv4 only)
1003 * - IP: => port=+0, relative
1004 * - IP:N => port=N, absolute
1005 * - IP:+N => port=+N, relative
1006 * - IP:-N => port=-N, relative
1007 */
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001008 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001009 if (!sk) {
1010 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1011 err_code |= ERR_ALERT | ERR_FATAL;
1012 goto out;
1013 }
1014
1015 proto = protocol_by_family(sk->ss_family);
1016 if (!proto || !proto->connect) {
1017 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1018 file, linenum, args[0], args[1]);
1019 err_code |= ERR_ALERT | ERR_FATAL;
1020 goto out;
1021 }
1022
1023 if (!port1 || !port2) {
1024 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001025 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001026 }
1027 else if (port1 != port2) {
1028 /* port range */
1029 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1030 file, linenum, args[0], args[1], args[2]);
1031 err_code |= ERR_ALERT | ERR_FATAL;
1032 goto out;
1033 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001034
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001035 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001036 newsrv->hostname = fqdn;
1037 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001038 goto skip_name_resolution;
1039
Willy Tarreau07101d52015-09-08 16:16:35 +02001040 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001041 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001042 goto skip_name_resolution;
1043
1044 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1045 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1046 goto skip_name_resolution;
1047 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1048 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1049 file, linenum, args[2]);
1050 err_code |= ERR_ALERT | ERR_FATAL;
1051 goto out;
1052 }
1053
1054 curr_resolution->requester = newsrv;
1055 curr_resolution->requester_cb = snr_resolution_cb;
1056 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1057 curr_resolution->status = RSLV_STATUS_NONE;
1058 curr_resolution->step = RSLV_STEP_NONE;
1059 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001060 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001061 newsrv->resolution = curr_resolution;
1062
1063 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001064 newsrv->addr = *sk;
Willy Tarreau04276f32017-01-06 17:41:29 +01001065 newsrv->svc_port = get_host_port(sk);
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001066 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = xprt_get(XPRT_RAW);
Willy Tarreau272adea2014-03-31 10:39:59 +02001067
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001068 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001069 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1070 file, linenum, newsrv->addr.ss_family, args[2]);
1071 err_code |= ERR_ALERT | ERR_FATAL;
1072 goto out;
1073 }
1074
Willy Tarreau141ad852016-12-22 18:38:00 +01001075 newsrv->use_ssl = curproxy->defsrv.use_ssl;
Willy Tarreau272adea2014-03-31 10:39:59 +02001076 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1077 newsrv->check.port = curproxy->defsrv.check.port;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001078 if (newsrv->check.port)
1079 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001080 newsrv->check.inter = curproxy->defsrv.check.inter;
1081 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1082 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1083 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1084 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001085 if (curproxy->defsrv.agent.send_string != NULL)
1086 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1087 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001088 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1089 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1090 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1091 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1092 newsrv->minconn = curproxy->defsrv.minconn;
1093 newsrv->maxconn = curproxy->defsrv.maxconn;
1094 newsrv->slowstart = curproxy->defsrv.slowstart;
1095 newsrv->onerror = curproxy->defsrv.onerror;
1096 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1097 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1098 newsrv->consecutive_errors_limit
1099 = curproxy->defsrv.consecutive_errors_limit;
Willy Tarreau272adea2014-03-31 10:39:59 +02001100 newsrv->uweight = newsrv->iweight
1101 = curproxy->defsrv.iweight;
1102
1103 newsrv->check.status = HCHK_STATUS_INI;
1104 newsrv->check.rise = curproxy->defsrv.check.rise;
1105 newsrv->check.fall = curproxy->defsrv.check.fall;
1106 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1107 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001108 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001109
1110 newsrv->agent.status = HCHK_STATUS_INI;
1111 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1112 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1113 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1114 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001115 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1116 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1117 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001118 memcpy(newsrv->dns_opts.pref_net,
1119 curproxy->defsrv.dns_opts.pref_net,
1120 sizeof(newsrv->dns_opts.pref_net));
1121 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001122 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1123 newsrv->init_addr = curproxy->defsrv.init_addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001124
1125 cur_arg = 3;
1126 } else {
1127 newsrv = &curproxy->defsrv;
1128 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001129 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001130 }
1131
1132 while (*args[cur_arg]) {
1133 if (!strcmp(args[cur_arg], "agent-check")) {
1134 global.maxsock++;
1135 do_agent = 1;
1136 cur_arg += 1;
1137 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1138 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1139 if (err) {
1140 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1141 file, linenum, *err, newsrv->id);
1142 err_code |= ERR_ALERT | ERR_FATAL;
1143 goto out;
1144 }
1145 if (val <= 0) {
1146 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1147 file, linenum, val, args[cur_arg], newsrv->id);
1148 err_code |= ERR_ALERT | ERR_FATAL;
1149 goto out;
1150 }
1151 newsrv->agent.inter = val;
1152 cur_arg += 2;
1153 }
1154 else if (!strcmp(args[cur_arg], "agent-port")) {
1155 global.maxsock++;
1156 newsrv->agent.port = atol(args[cur_arg + 1]);
1157 cur_arg += 2;
1158 }
James Brown55f9ff12015-10-21 18:19:05 -07001159 else if (!strcmp(args[cur_arg], "agent-send")) {
1160 global.maxsock++;
1161 free(newsrv->agent.send_string);
1162 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1163 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1164 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1165 cur_arg += 2;
1166 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001167 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1168 newsrv->cookie = strdup(args[cur_arg + 1]);
1169 newsrv->cklen = strlen(args[cur_arg + 1]);
1170 cur_arg += 2;
1171 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001172 else if (!strcmp(args[cur_arg], "init-addr")) {
1173 char *p, *end;
1174 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001175 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001176
1177 newsrv->init_addr_methods = 0;
1178 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1179
1180 for (p = args[cur_arg + 1]; *p; p = end) {
1181 /* cut on next comma */
1182 for (end = p; *end && *end != ','; end++);
1183 if (*end)
1184 *(end++) = 0;
1185
Willy Tarreau4310d362016-11-02 15:05:56 +01001186 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001187 if (!strcmp(p, "libc")) {
1188 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1189 }
1190 else if (!strcmp(p, "last")) {
1191 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1192 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001193 else if (!strcmp(p, "none")) {
1194 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1195 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001196 else if (str2ip2(p, &sa, 0)) {
1197 if (is_addr(&newsrv->init_addr)) {
1198 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1199 file, linenum, args[cur_arg], p);
1200 err_code |= ERR_ALERT | ERR_FATAL;
1201 goto out;
1202 }
1203 newsrv->init_addr = sa;
1204 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1205 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001206 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001207 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001208 file, linenum, args[cur_arg], p);
1209 err_code |= ERR_ALERT | ERR_FATAL;
1210 goto out;
1211 }
1212 if (!done) {
1213 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1214 file, linenum, args[cur_arg], p);
1215 err_code |= ERR_ALERT | ERR_FATAL;
1216 goto out;
1217 }
1218 }
1219 cur_arg += 2;
1220 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001221 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1222 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1223 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1224 cur_arg += 2;
1225 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001226 else if (!strcmp(args[cur_arg], "resolvers")) {
1227 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1228 cur_arg += 2;
1229 }
1230 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1231 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001232 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001233 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001234 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001235 else {
1236 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1237 file, linenum, args[cur_arg]);
1238 err_code |= ERR_ALERT | ERR_FATAL;
1239 goto out;
1240 }
1241 cur_arg += 2;
1242 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001243 else if (!strcmp(args[cur_arg], "resolve-net")) {
1244 char *p, *e;
1245 unsigned char mask;
1246 struct dns_options *opt;
1247
1248 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1249 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1250 file, linenum, args[cur_arg]);
1251 err_code |= ERR_ALERT | ERR_FATAL;
1252 goto out;
1253 }
1254
1255 opt = &newsrv->dns_opts;
1256
1257 /* Split arguments by comma, and convert it from ipv4 or ipv6
1258 * string network in in_addr or in6_addr.
1259 */
1260 p = args[cur_arg + 1];
1261 e = p;
1262 while (*p != '\0') {
1263 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001264 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001265 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1266 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1267 err_code |= ERR_ALERT | ERR_FATAL;
1268 goto out;
1269 }
1270 /* look for end or comma. */
1271 while (*e != ',' && *e != '\0')
1272 e++;
1273 if (*e == ',') {
1274 *e = '\0';
1275 e++;
1276 }
1277 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1278 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1279 /* Try to convert input string from ipv4 or ipv6 network. */
1280 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1281 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1282 &mask)) {
1283 /* Try to convert input string from ipv6 network. */
1284 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1285 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1286 } else {
1287 /* All network conversions fail, retrun error. */
1288 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1289 file, linenum, args[cur_arg], p);
1290 err_code |= ERR_ALERT | ERR_FATAL;
1291 goto out;
1292 }
1293 opt->pref_net_nb++;
1294 p = e;
1295 }
1296
1297 cur_arg += 2;
1298 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001299 else if (!strcmp(args[cur_arg], "rise")) {
1300 if (!*args[cur_arg + 1]) {
1301 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1302 file, linenum, args[cur_arg]);
1303 err_code |= ERR_ALERT | ERR_FATAL;
1304 goto out;
1305 }
1306
1307 newsrv->check.rise = atol(args[cur_arg + 1]);
1308 if (newsrv->check.rise <= 0) {
1309 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1310 file, linenum, args[cur_arg]);
1311 err_code |= ERR_ALERT | ERR_FATAL;
1312 goto out;
1313 }
1314
1315 if (newsrv->check.health)
1316 newsrv->check.health = newsrv->check.rise;
1317 cur_arg += 2;
1318 }
1319 else if (!strcmp(args[cur_arg], "fall")) {
1320 newsrv->check.fall = atol(args[cur_arg + 1]);
1321
1322 if (!*args[cur_arg + 1]) {
1323 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1324 file, linenum, args[cur_arg]);
1325 err_code |= ERR_ALERT | ERR_FATAL;
1326 goto out;
1327 }
1328
1329 if (newsrv->check.fall <= 0) {
1330 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1331 file, linenum, args[cur_arg]);
1332 err_code |= ERR_ALERT | ERR_FATAL;
1333 goto out;
1334 }
1335
1336 cur_arg += 2;
1337 }
1338 else if (!strcmp(args[cur_arg], "inter")) {
1339 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1340 if (err) {
1341 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1342 file, linenum, *err, newsrv->id);
1343 err_code |= ERR_ALERT | ERR_FATAL;
1344 goto out;
1345 }
1346 if (val <= 0) {
1347 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1348 file, linenum, val, args[cur_arg], newsrv->id);
1349 err_code |= ERR_ALERT | ERR_FATAL;
1350 goto out;
1351 }
1352 newsrv->check.inter = val;
1353 cur_arg += 2;
1354 }
1355 else if (!strcmp(args[cur_arg], "fastinter")) {
1356 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1357 if (err) {
1358 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1359 file, linenum, *err, newsrv->id);
1360 err_code |= ERR_ALERT | ERR_FATAL;
1361 goto out;
1362 }
1363 if (val <= 0) {
1364 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1365 file, linenum, val, args[cur_arg], newsrv->id);
1366 err_code |= ERR_ALERT | ERR_FATAL;
1367 goto out;
1368 }
1369 newsrv->check.fastinter = val;
1370 cur_arg += 2;
1371 }
1372 else if (!strcmp(args[cur_arg], "downinter")) {
1373 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1374 if (err) {
1375 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1376 file, linenum, *err, newsrv->id);
1377 err_code |= ERR_ALERT | ERR_FATAL;
1378 goto out;
1379 }
1380 if (val <= 0) {
1381 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1382 file, linenum, val, args[cur_arg], newsrv->id);
1383 err_code |= ERR_ALERT | ERR_FATAL;
1384 goto out;
1385 }
1386 newsrv->check.downinter = val;
1387 cur_arg += 2;
1388 }
1389 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1390 struct sockaddr_storage *sk;
1391 int port1, port2;
1392 struct protocol *proto;
1393
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001394 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001395 if (!sk) {
1396 Alert("parsing [%s:%d] : '%s' : %s\n",
1397 file, linenum, args[cur_arg], errmsg);
1398 err_code |= ERR_ALERT | ERR_FATAL;
1399 goto out;
1400 }
1401
1402 proto = protocol_by_family(sk->ss_family);
1403 if (!proto || !proto->connect) {
1404 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1405 file, linenum, args[cur_arg], args[cur_arg + 1]);
1406 err_code |= ERR_ALERT | ERR_FATAL;
1407 goto out;
1408 }
1409
1410 if (port1 != port2) {
1411 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1412 file, linenum, args[cur_arg], args[cur_arg + 1]);
1413 err_code |= ERR_ALERT | ERR_FATAL;
1414 goto out;
1415 }
1416
Simon Horman41f58762015-01-30 11:22:56 +09001417 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001418 newsrv->flags |= SRV_F_CHECKADDR;
1419 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001420 cur_arg += 2;
1421 }
1422 else if (!strcmp(args[cur_arg], "port")) {
1423 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001424 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001425 cur_arg += 2;
1426 }
1427 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001428 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001429 cur_arg ++;
1430 }
1431 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001432 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001433 cur_arg ++;
1434 }
1435 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001436 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001437 cur_arg ++;
1438 }
David Safb76832014-05-08 23:42:08 -04001439 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1440 newsrv->pp_opts |= SRV_PP_V2;
1441 cur_arg ++;
1442 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001443 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1444 newsrv->check.send_proxy = 1;
1445 cur_arg ++;
1446 }
1447 else if (!strcmp(args[cur_arg], "weight")) {
1448 int w;
1449 w = atol(args[cur_arg + 1]);
1450 if (w < 0 || w > SRV_UWGHT_MAX) {
1451 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1452 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1453 err_code |= ERR_ALERT | ERR_FATAL;
1454 goto out;
1455 }
1456 newsrv->uweight = newsrv->iweight = w;
1457 cur_arg += 2;
1458 }
1459 else if (!strcmp(args[cur_arg], "minconn")) {
1460 newsrv->minconn = atol(args[cur_arg + 1]);
1461 cur_arg += 2;
1462 }
1463 else if (!strcmp(args[cur_arg], "maxconn")) {
1464 newsrv->maxconn = atol(args[cur_arg + 1]);
1465 cur_arg += 2;
1466 }
1467 else if (!strcmp(args[cur_arg], "maxqueue")) {
1468 newsrv->maxqueue = atol(args[cur_arg + 1]);
1469 cur_arg += 2;
1470 }
1471 else if (!strcmp(args[cur_arg], "slowstart")) {
1472 /* slowstart is stored in seconds */
1473 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1474 if (err) {
1475 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1476 file, linenum, *err, newsrv->id);
1477 err_code |= ERR_ALERT | ERR_FATAL;
1478 goto out;
1479 }
1480 newsrv->slowstart = (val + 999) / 1000;
1481 cur_arg += 2;
1482 }
1483 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1484
1485 if (!*args[cur_arg + 1]) {
1486 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1487 file, linenum);
1488 err_code |= ERR_ALERT | ERR_FATAL;
1489 goto out;
1490 }
1491
1492 newsrv->trackit = strdup(args[cur_arg + 1]);
1493
1494 cur_arg += 2;
1495 }
1496 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1497 global.maxsock++;
1498 do_check = 1;
1499 cur_arg += 1;
1500 }
1501 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001502 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001503 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001504 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001505 newsrv->check.state |= CHK_ST_PAUSED;
1506 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001507 cur_arg += 1;
1508 }
1509 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1510 if (!strcmp(args[cur_arg + 1], "none"))
1511 newsrv->observe = HANA_OBS_NONE;
1512 else if (!strcmp(args[cur_arg + 1], "layer4"))
1513 newsrv->observe = HANA_OBS_LAYER4;
1514 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1515 if (curproxy->mode != PR_MODE_HTTP) {
1516 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1517 file, linenum, args[cur_arg + 1]);
1518 err_code |= ERR_ALERT;
1519 }
1520 newsrv->observe = HANA_OBS_LAYER7;
1521 }
1522 else {
1523 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1524 "'layer4', 'layer7' but got '%s'\n",
1525 file, linenum, args[cur_arg], args[cur_arg + 1]);
1526 err_code |= ERR_ALERT | ERR_FATAL;
1527 goto out;
1528 }
1529
1530 cur_arg += 2;
1531 }
1532 else if (!strcmp(args[cur_arg], "on-error")) {
1533 if (!strcmp(args[cur_arg + 1], "fastinter"))
1534 newsrv->onerror = HANA_ONERR_FASTINTER;
1535 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1536 newsrv->onerror = HANA_ONERR_FAILCHK;
1537 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1538 newsrv->onerror = HANA_ONERR_SUDDTH;
1539 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1540 newsrv->onerror = HANA_ONERR_MARKDWN;
1541 else {
1542 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1543 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1544 file, linenum, args[cur_arg], args[cur_arg + 1]);
1545 err_code |= ERR_ALERT | ERR_FATAL;
1546 goto out;
1547 }
1548
1549 cur_arg += 2;
1550 }
1551 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1552 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1553 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1554 else {
1555 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1556 file, linenum, args[cur_arg], args[cur_arg + 1]);
1557 err_code |= ERR_ALERT | ERR_FATAL;
1558 goto out;
1559 }
1560
1561 cur_arg += 2;
1562 }
1563 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1564 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1565 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1566 else {
1567 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1568 file, linenum, args[cur_arg], args[cur_arg + 1]);
1569 err_code |= ERR_ALERT | ERR_FATAL;
1570 goto out;
1571 }
1572
1573 cur_arg += 2;
1574 }
1575 else if (!strcmp(args[cur_arg], "error-limit")) {
1576 if (!*args[cur_arg + 1]) {
1577 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1578 file, linenum, args[cur_arg]);
1579 err_code |= ERR_ALERT | ERR_FATAL;
1580 goto out;
1581 }
1582
1583 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1584
1585 if (newsrv->consecutive_errors_limit <= 0) {
1586 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1587 file, linenum, args[cur_arg]);
1588 err_code |= ERR_ALERT | ERR_FATAL;
1589 goto out;
1590 }
1591 cur_arg += 2;
1592 }
1593 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1594 int port_low, port_high;
1595 struct sockaddr_storage *sk;
1596 struct protocol *proto;
1597
1598 if (!*args[cur_arg + 1]) {
1599 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1600 file, linenum, "source", "usesrc", "interface");
1601 err_code |= ERR_ALERT | ERR_FATAL;
1602 goto out;
1603 }
1604
1605 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001606 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001607 if (!sk) {
1608 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1609 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1610 err_code |= ERR_ALERT | ERR_FATAL;
1611 goto out;
1612 }
1613
1614 proto = protocol_by_family(sk->ss_family);
1615 if (!proto || !proto->connect) {
1616 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1617 file, linenum, args[cur_arg], args[cur_arg+1]);
1618 err_code |= ERR_ALERT | ERR_FATAL;
1619 goto out;
1620 }
1621
1622 newsrv->conn_src.source_addr = *sk;
1623
1624 if (port_low != port_high) {
1625 int i;
1626
1627 if (!port_low || !port_high) {
1628 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1629 file, linenum, args[cur_arg], args[cur_arg + 1]);
1630 err_code |= ERR_ALERT | ERR_FATAL;
1631 goto out;
1632 }
1633
1634 if (port_low <= 0 || port_low > 65535 ||
1635 port_high <= 0 || port_high > 65535 ||
1636 port_low > port_high) {
1637 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1638 file, linenum, port_low, port_high);
1639 err_code |= ERR_ALERT | ERR_FATAL;
1640 goto out;
1641 }
1642 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1643 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1644 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1645 }
1646
1647 cur_arg += 2;
1648 while (*(args[cur_arg])) {
1649 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001650#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001651 if (!*args[cur_arg + 1]) {
1652 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1653 file, linenum, "usesrc");
1654 err_code |= ERR_ALERT | ERR_FATAL;
1655 goto out;
1656 }
1657 if (!strcmp(args[cur_arg + 1], "client")) {
1658 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1659 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1660 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1661 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1662 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1663 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1664 char *name, *end;
1665
1666 name = args[cur_arg+1] + 7;
1667 while (isspace(*name))
1668 name++;
1669
1670 end = name;
1671 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1672 end++;
1673
1674 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1675 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1676 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1677 newsrv->conn_src.bind_hdr_len = end - name;
1678 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1679 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1680 newsrv->conn_src.bind_hdr_occ = -1;
1681
1682 /* now look for an occurrence number */
1683 while (isspace(*end))
1684 end++;
1685 if (*end == ',') {
1686 end++;
1687 name = end;
1688 if (*end == '-')
1689 end++;
1690 while (isdigit((int)*end))
1691 end++;
1692 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1693 }
1694
1695 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1696 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1697 " occurrences values smaller than %d.\n",
1698 file, linenum, MAX_HDR_HISTORY);
1699 err_code |= ERR_ALERT | ERR_FATAL;
1700 goto out;
1701 }
1702 } else {
1703 struct sockaddr_storage *sk;
1704 int port1, port2;
1705
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001706 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001707 if (!sk) {
1708 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1709 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1710 err_code |= ERR_ALERT | ERR_FATAL;
1711 goto out;
1712 }
1713
1714 proto = protocol_by_family(sk->ss_family);
1715 if (!proto || !proto->connect) {
1716 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1717 file, linenum, args[cur_arg], args[cur_arg+1]);
1718 err_code |= ERR_ALERT | ERR_FATAL;
1719 goto out;
1720 }
1721
1722 if (port1 != port2) {
1723 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1724 file, linenum, args[cur_arg], args[cur_arg + 1]);
1725 err_code |= ERR_ALERT | ERR_FATAL;
1726 goto out;
1727 }
1728 newsrv->conn_src.tproxy_addr = *sk;
1729 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1730 }
1731 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001732 cur_arg += 2;
1733 continue;
1734#else /* no TPROXY support */
1735 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1736 file, linenum, "usesrc");
1737 err_code |= ERR_ALERT | ERR_FATAL;
1738 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001739#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001740 } /* "usesrc" */
1741
1742 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1743#ifdef SO_BINDTODEVICE
1744 if (!*args[cur_arg + 1]) {
1745 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1746 file, linenum, args[0]);
1747 err_code |= ERR_ALERT | ERR_FATAL;
1748 goto out;
1749 }
1750 free(newsrv->conn_src.iface_name);
1751 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1752 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1753 global.last_checks |= LSTCHK_NETADM;
1754#else
1755 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1756 file, linenum, args[0], args[cur_arg]);
1757 err_code |= ERR_ALERT | ERR_FATAL;
1758 goto out;
1759#endif
1760 cur_arg += 2;
1761 continue;
1762 }
1763 /* this keyword in not an option of "source" */
1764 break;
1765 } /* while */
1766 }
1767 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1768 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1769 file, linenum, "usesrc", "source");
1770 err_code |= ERR_ALERT | ERR_FATAL;
1771 goto out;
1772 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001773 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1774#ifdef CONFIG_HAP_NS
1775 char *arg = args[cur_arg + 1];
1776 if (!strcmp(arg, "*")) {
1777 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1778 } else {
1779 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1780
1781 if (newsrv->netns == NULL)
1782 newsrv->netns = netns_store_insert(arg);
1783
1784 if (newsrv->netns == NULL) {
1785 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1786 err_code |= ERR_ALERT | ERR_FATAL;
1787 goto out;
1788 }
1789 }
1790#else
1791 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1792 file, linenum, args[0], args[cur_arg]);
1793 err_code |= ERR_ALERT | ERR_FATAL;
1794 goto out;
1795#endif
1796 cur_arg += 2;
1797 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001798 else {
1799 static int srv_dumped;
1800 struct srv_kw *kw;
1801 char *err;
1802
1803 kw = srv_find_kw(args[cur_arg]);
1804 if (kw) {
1805 char *err = NULL;
1806 int code;
1807
1808 if (!kw->parse) {
1809 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1810 file, linenum, args[0], args[1], args[cur_arg]);
1811 cur_arg += 1 + kw->skip ;
1812 err_code |= ERR_ALERT | ERR_FATAL;
1813 goto out;
1814 }
1815
1816 if (defsrv && !kw->default_ok) {
1817 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1818 file, linenum, args[0], args[1], args[cur_arg]);
1819 cur_arg += 1 + kw->skip ;
1820 err_code |= ERR_ALERT;
1821 continue;
1822 }
1823
1824 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1825 err_code |= code;
1826
1827 if (code) {
1828 if (err && *err) {
1829 indent_msg(&err, 2);
1830 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1831 }
1832 else
1833 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1834 file, linenum, args[0], args[1], args[cur_arg]);
1835 if (code & ERR_FATAL) {
1836 free(err);
1837 cur_arg += 1 + kw->skip;
1838 goto out;
1839 }
1840 }
1841 free(err);
1842 cur_arg += 1 + kw->skip;
1843 continue;
1844 }
1845
1846 err = NULL;
1847 if (!srv_dumped) {
1848 srv_dump_kws(&err);
1849 indent_msg(&err, 4);
1850 srv_dumped = 1;
1851 }
1852
1853 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1854 file, linenum, args[0], args[1], args[cur_arg],
1855 err ? " Registered keywords :" : "", err ? err : "");
1856 free(err);
1857
1858 err_code |= ERR_ALERT | ERR_FATAL;
1859 goto out;
1860 }
1861 }
1862
Willy Tarreau272adea2014-03-31 10:39:59 +02001863 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001864 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001865
1866 if (newsrv->trackit) {
1867 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1868 file, linenum);
1869 err_code |= ERR_ALERT | ERR_FATAL;
1870 goto out;
1871 }
1872
Willy Tarreau272adea2014-03-31 10:39:59 +02001873 /*
1874 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001875 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001876 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001877 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001878 (is_inet_addr(&newsrv->check.addr) ||
1879 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001880 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001881 struct list *l;
1882
1883 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1884 if (!r) {
1885 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1886 file, linenum, newsrv->id);
1887 err_code |= ERR_ALERT | ERR_FATAL;
1888 goto out;
1889 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001890 /* search the first action (connect / send / expect) in the list */
1891 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001892 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001893 if (r->action != TCPCHK_ACT_COMMENT)
1894 break;
1895 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001896 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1897 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",
1898 file, linenum, newsrv->id);
1899 err_code |= ERR_ALERT | ERR_FATAL;
1900 goto out;
1901 }
1902 else {
1903 /* scan the tcp-check ruleset to ensure a port has been configured */
1904 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001905 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001906 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1907 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",
1908 file, linenum, newsrv->id);
1909 err_code |= ERR_ALERT | ERR_FATAL;
1910 goto out;
1911 }
1912 }
1913 }
1914 }
1915
1916 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001917 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001918 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001919 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1920 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001921 goto out;
1922 }
1923
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001924 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001925 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001926
Willy Tarreau272adea2014-03-31 10:39:59 +02001927 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1928 }
1929
1930 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001931 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001932
1933 if (!newsrv->agent.port) {
1934 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1935 file, linenum, newsrv->id);
1936 err_code |= ERR_ALERT | ERR_FATAL;
1937 goto out;
1938 }
1939
1940 if (!newsrv->agent.inter)
1941 newsrv->agent.inter = newsrv->check.inter;
1942
Simon Hormanb1900d52015-01-30 11:22:54 +09001943 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001944 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001945 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1946 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001947 goto out;
1948 }
1949
1950 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1951 }
1952
1953 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001954 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001955 curproxy->srv_bck++;
1956 else
1957 curproxy->srv_act++;
1958
Willy Tarreauc5150da2014-05-13 19:27:31 +02001959 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001960 }
1961 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001962 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001963 return 0;
1964
1965 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001966 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001967 free(errmsg);
1968 return err_code;
1969}
1970
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001971/* Returns a pointer to the first server matching either id <id>.
1972 * NULL is returned if no match is found.
1973 * the lookup is performed in the backend <bk>
1974 */
1975struct server *server_find_by_id(struct proxy *bk, int id)
1976{
1977 struct eb32_node *eb32;
1978 struct server *curserver;
1979
1980 if (!bk || (id ==0))
1981 return NULL;
1982
1983 /* <bk> has no backend capabilities, so it can't have a server */
1984 if (!(bk->cap & PR_CAP_BE))
1985 return NULL;
1986
1987 curserver = NULL;
1988
1989 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1990 if (eb32)
1991 curserver = container_of(eb32, struct server, conf.id);
1992
1993 return curserver;
1994}
1995
1996/* Returns a pointer to the first server matching either name <name>, or id
1997 * if <name> starts with a '#'. NULL is returned if no match is found.
1998 * the lookup is performed in the backend <bk>
1999 */
2000struct server *server_find_by_name(struct proxy *bk, const char *name)
2001{
2002 struct server *curserver;
2003
2004 if (!bk || !name)
2005 return NULL;
2006
2007 /* <bk> has no backend capabilities, so it can't have a server */
2008 if (!(bk->cap & PR_CAP_BE))
2009 return NULL;
2010
2011 curserver = NULL;
2012 if (*name == '#') {
2013 curserver = server_find_by_id(bk, atoi(name + 1));
2014 if (curserver)
2015 return curserver;
2016 }
2017 else {
2018 curserver = bk->srv;
2019
2020 while (curserver && (strcmp(curserver->id, name) != 0))
2021 curserver = curserver->next;
2022
2023 if (curserver)
2024 return curserver;
2025 }
2026
2027 return NULL;
2028}
2029
2030struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2031{
2032 struct server *byname;
2033 struct server *byid;
2034
2035 if (!name && !id)
2036 return NULL;
2037
2038 if (diff)
2039 *diff = 0;
2040
2041 byname = byid = NULL;
2042
2043 if (name) {
2044 byname = server_find_by_name(bk, name);
2045 if (byname && (!id || byname->puid == id))
2046 return byname;
2047 }
2048
2049 /* remaining possibilities :
2050 * - name not set
2051 * - name set but not found
2052 * - name found but ID doesn't match
2053 */
2054 if (id) {
2055 byid = server_find_by_id(bk, id);
2056 if (byid) {
2057 if (byname) {
2058 /* use id only if forced by configuration */
2059 if (byid->flags & SRV_F_FORCED_ID) {
2060 if (diff)
2061 *diff |= 2;
2062 return byid;
2063 }
2064 else {
2065 if (diff)
2066 *diff |= 1;
2067 return byname;
2068 }
2069 }
2070
2071 /* remaining possibilities:
2072 * - name not set
2073 * - name set but not found
2074 */
2075 if (name && diff)
2076 *diff |= 2;
2077 return byid;
2078 }
2079
2080 /* id bot found */
2081 if (byname) {
2082 if (diff)
2083 *diff |= 1;
2084 return byname;
2085 }
2086 }
2087
2088 return NULL;
2089}
2090
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002091/* Update a server state using the parameters available in the params list */
2092static void srv_update_state(struct server *srv, int version, char **params)
2093{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002094 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002095 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002096
2097 /* fields since version 1
2098 * and common to all other upcoming versions
2099 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002100 enum srv_state srv_op_state;
2101 enum srv_admin srv_admin_state;
2102 unsigned srv_uweight, srv_iweight;
2103 unsigned long srv_last_time_change;
2104 short srv_check_status;
2105 enum chk_result srv_check_result;
2106 int srv_check_health;
2107 int srv_check_state, srv_agent_state;
2108 int bk_f_forced_id;
2109 int srv_f_forced_id;
2110
Willy Tarreau31138fa2015-09-29 18:38:47 +02002111 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002112 switch (version) {
2113 case 1:
2114 /*
2115 * now we can proceed with server's state update:
2116 * srv_addr: params[0]
2117 * srv_op_state: params[1]
2118 * srv_admin_state: params[2]
2119 * srv_uweight: params[3]
2120 * srv_iweight: params[4]
2121 * srv_last_time_change: params[5]
2122 * srv_check_status: params[6]
2123 * srv_check_result: params[7]
2124 * srv_check_health: params[8]
2125 * srv_check_state: params[9]
2126 * srv_agent_state: params[10]
2127 * bk_f_forced_id: params[11]
2128 * srv_f_forced_id: params[12]
2129 */
2130
2131 /* validating srv_op_state */
2132 p = NULL;
2133 errno = 0;
2134 srv_op_state = strtol(params[1], &p, 10);
2135 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2136 (srv_op_state != SRV_ST_STOPPED &&
2137 srv_op_state != SRV_ST_STARTING &&
2138 srv_op_state != SRV_ST_RUNNING &&
2139 srv_op_state != SRV_ST_STOPPING)) {
2140 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2141 }
2142
2143 /* validating srv_admin_state */
2144 p = NULL;
2145 errno = 0;
2146 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002147
2148 /* inherited statuses will be recomputed later */
2149 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2150
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002151 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2152 (srv_admin_state != 0 &&
2153 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002154 srv_admin_state != SRV_ADMF_CMAINT &&
2155 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002156 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002157 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002158 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2159 }
2160
2161 /* validating srv_uweight */
2162 p = NULL;
2163 errno = 0;
2164 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002165 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002166 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2167
2168 /* validating srv_iweight */
2169 p = NULL;
2170 errno = 0;
2171 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002172 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002173 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2174
2175 /* validating srv_last_time_change */
2176 p = NULL;
2177 errno = 0;
2178 srv_last_time_change = strtol(params[5], &p, 10);
2179 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2180 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2181
2182 /* validating srv_check_status */
2183 p = NULL;
2184 errno = 0;
2185 srv_check_status = strtol(params[6], &p, 10);
2186 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2187 (srv_check_status >= HCHK_STATUS_SIZE))
2188 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2189
2190 /* validating srv_check_result */
2191 p = NULL;
2192 errno = 0;
2193 srv_check_result = strtol(params[7], &p, 10);
2194 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2195 (srv_check_result != CHK_RES_UNKNOWN &&
2196 srv_check_result != CHK_RES_NEUTRAL &&
2197 srv_check_result != CHK_RES_FAILED &&
2198 srv_check_result != CHK_RES_PASSED &&
2199 srv_check_result != CHK_RES_CONDPASS)) {
2200 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2201 }
2202
2203 /* validating srv_check_health */
2204 p = NULL;
2205 errno = 0;
2206 srv_check_health = strtol(params[8], &p, 10);
2207 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2208 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2209
2210 /* validating srv_check_state */
2211 p = NULL;
2212 errno = 0;
2213 srv_check_state = strtol(params[9], &p, 10);
2214 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2215 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2216 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2217
2218 /* validating srv_agent_state */
2219 p = NULL;
2220 errno = 0;
2221 srv_agent_state = strtol(params[10], &p, 10);
2222 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2223 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2224 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2225
2226 /* validating bk_f_forced_id */
2227 p = NULL;
2228 errno = 0;
2229 bk_f_forced_id = strtol(params[11], &p, 10);
2230 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2231 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2232
2233 /* validating srv_f_forced_id */
2234 p = NULL;
2235 errno = 0;
2236 srv_f_forced_id = strtol(params[12], &p, 10);
2237 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2238 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2239
2240
2241 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002242 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002243 goto out;
2244
2245 /* recover operational state and apply it to this server
2246 * and all servers tracking this one */
2247 switch (srv_op_state) {
2248 case SRV_ST_STOPPED:
2249 srv->check.health = 0;
2250 srv_set_stopped(srv, "changed from server-state after a reload");
2251 break;
2252 case SRV_ST_STARTING:
2253 srv->state = srv_op_state;
2254 break;
2255 case SRV_ST_STOPPING:
2256 srv->check.health = srv->check.rise + srv->check.fall - 1;
2257 srv_set_stopping(srv, "changed from server-state after a reload");
2258 break;
2259 case SRV_ST_RUNNING:
2260 srv->check.health = srv->check.rise + srv->check.fall - 1;
2261 srv_set_running(srv, "");
2262 break;
2263 }
2264
2265 /* When applying server state, the following rules apply:
2266 * - in case of a configuration change, we apply the setting from the new
2267 * configuration, regardless of old running state
2268 * - if no configuration change, we apply old running state only if old running
2269 * state is different from new configuration state
2270 */
2271 /* configuration has changed */
2272 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2273 if (srv->admin & SRV_ADMF_CMAINT)
2274 srv_adm_set_maint(srv);
2275 else
2276 srv_adm_set_ready(srv);
2277 }
2278 /* configuration is the same, let's compate old running state and new conf state */
2279 else {
2280 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2281 srv_adm_set_maint(srv);
2282 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2283 srv_adm_set_ready(srv);
2284 }
2285 /* apply drain mode if server is currently enabled */
2286 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2287 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002288 * (srv->iweight is the weight set up in configuration).
2289 * There are two possible reasons for FDRAIN to have been present :
2290 * - previous config weight was zero
2291 * - "set server b/s drain" was sent to the CLI
2292 *
2293 * In the first case, we simply want to drop this drain state
2294 * if the new weight is not zero anymore, meaning the administrator
2295 * has intentionally turned the weight back to a positive value to
2296 * enable the server again after an operation. In the second case,
2297 * the drain state was forced on the CLI regardless of the config's
2298 * weight so we don't want a change to the config weight to lose this
2299 * status. What this means is :
2300 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2301 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002302 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002303 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002304 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002305 }
2306
2307 srv->last_change = date.tv_sec - srv_last_time_change;
2308 srv->check.status = srv_check_status;
2309 srv->check.result = srv_check_result;
2310 srv->check.health = srv_check_health;
2311
2312 /* Only case we want to apply is removing ENABLED flag which could have been
2313 * done by the "disable health" command over the stats socket
2314 */
2315 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2316 (srv_check_state & CHK_ST_CONFIGURED) &&
2317 !(srv_check_state & CHK_ST_ENABLED))
2318 srv->check.state &= ~CHK_ST_ENABLED;
2319
2320 /* Only case we want to apply is removing ENABLED flag which could have been
2321 * done by the "disable agent" command over the stats socket
2322 */
2323 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2324 (srv_agent_state & CHK_ST_CONFIGURED) &&
2325 !(srv_agent_state & CHK_ST_ENABLED))
2326 srv->agent.state &= ~CHK_ST_ENABLED;
2327
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002328 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2329 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002330 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002331 * It means that a configuration file change has precedence over a unix socket change
2332 * for server's weight
2333 *
2334 * by default, HAProxy applies the following weight when parsing the configuration
2335 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002336 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002337 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002338 srv->uweight = srv_uweight;
2339 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002340 server_recalc_eweight(srv);
2341
Willy Tarreaue5a60682016-11-09 14:54:53 +01002342 /* load server IP address */
2343 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002344 break;
2345 default:
2346 chunk_appendf(msg, ", version '%d' not supported", version);
2347 }
2348
2349 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002350 if (msg->len) {
2351 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002352 Warning("server-state application failed for server '%s/%s'%s",
2353 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002354 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002355}
2356
2357/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2358 * For each proxy, it does the following:
2359 * - opens its server state file (either one or local one)
2360 * - read whole file, line by line
2361 * - analyse each line to check if it matches our current backend:
2362 * - backend name matches
2363 * - backend id matches if id is forced and name doesn't match
2364 * - if the server pointed by the line is found, then state is applied
2365 *
2366 * If the running backend uuid or id differs from the state file, then HAProxy reports
2367 * a warning.
2368 */
2369void apply_server_state(void)
2370{
2371 char *cur, *end;
2372 char mybuf[SRV_STATE_LINE_MAXLEN];
2373 int mybuflen;
2374 char *params[SRV_STATE_FILE_MAX_FIELDS];
2375 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2376 int arg, srv_arg, version, diff;
2377 FILE *f;
2378 char *filepath;
2379 char globalfilepath[MAXPATHLEN + 1];
2380 char localfilepath[MAXPATHLEN + 1];
2381 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2382 extern struct proxy *proxy;
2383 struct proxy *curproxy, *bk;
2384 struct server *srv;
2385
2386 globalfilepathlen = 0;
2387 /* create the globalfilepath variable */
2388 if (global.server_state_file) {
2389 /* absolute path or no base directory provided */
2390 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2391 len = strlen(global.server_state_file);
2392 if (len > MAXPATHLEN) {
2393 globalfilepathlen = 0;
2394 goto globalfileerror;
2395 }
2396 memcpy(globalfilepath, global.server_state_file, len);
2397 globalfilepath[len] = '\0';
2398 globalfilepathlen = len;
2399 }
2400 else if (global.server_state_base) {
2401 len = strlen(global.server_state_base);
2402 globalfilepathlen += len;
2403
2404 if (globalfilepathlen > MAXPATHLEN) {
2405 globalfilepathlen = 0;
2406 goto globalfileerror;
2407 }
2408 strncpy(globalfilepath, global.server_state_base, len);
2409 globalfilepath[globalfilepathlen] = 0;
2410
2411 /* append a slash if needed */
2412 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2413 if (globalfilepathlen + 1 > MAXPATHLEN) {
2414 globalfilepathlen = 0;
2415 goto globalfileerror;
2416 }
2417 globalfilepath[globalfilepathlen++] = '/';
2418 }
2419
2420 len = strlen(global.server_state_file);
2421 if (globalfilepathlen + len > MAXPATHLEN) {
2422 globalfilepathlen = 0;
2423 goto globalfileerror;
2424 }
2425 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2426 globalfilepathlen += len;
2427 globalfilepath[globalfilepathlen++] = 0;
2428 }
2429 }
2430 globalfileerror:
2431 if (globalfilepathlen == 0)
2432 globalfilepath[0] = '\0';
2433
2434 /* read servers state from local file */
2435 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2436 /* servers are only in backends */
2437 if (!(curproxy->cap & PR_CAP_BE))
2438 continue;
2439 fileopenerr = 0;
2440 filepath = NULL;
2441
2442 /* search server state file path and name */
2443 switch (curproxy->load_server_state_from_file) {
2444 /* read servers state from global file */
2445 case PR_SRV_STATE_FILE_GLOBAL:
2446 /* there was an error while generating global server state file path */
2447 if (globalfilepathlen == 0)
2448 continue;
2449 filepath = globalfilepath;
2450 fileopenerr = 1;
2451 break;
2452 /* this backend has its own file */
2453 case PR_SRV_STATE_FILE_LOCAL:
2454 localfilepathlen = 0;
2455 localfilepath[0] = '\0';
2456 len = 0;
2457 /* create the localfilepath variable */
2458 /* absolute path or no base directory provided */
2459 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2460 len = strlen(curproxy->server_state_file_name);
2461 if (len > MAXPATHLEN) {
2462 localfilepathlen = 0;
2463 goto localfileerror;
2464 }
2465 memcpy(localfilepath, curproxy->server_state_file_name, len);
2466 localfilepath[len] = '\0';
2467 localfilepathlen = len;
2468 }
2469 else if (global.server_state_base) {
2470 len = strlen(global.server_state_base);
2471 localfilepathlen += len;
2472
2473 if (localfilepathlen > MAXPATHLEN) {
2474 localfilepathlen = 0;
2475 goto localfileerror;
2476 }
2477 strncpy(localfilepath, global.server_state_base, len);
2478 localfilepath[localfilepathlen] = 0;
2479
2480 /* append a slash if needed */
2481 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2482 if (localfilepathlen + 1 > MAXPATHLEN) {
2483 localfilepathlen = 0;
2484 goto localfileerror;
2485 }
2486 localfilepath[localfilepathlen++] = '/';
2487 }
2488
2489 len = strlen(curproxy->server_state_file_name);
2490 if (localfilepathlen + len > MAXPATHLEN) {
2491 localfilepathlen = 0;
2492 goto localfileerror;
2493 }
2494 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2495 localfilepathlen += len;
2496 localfilepath[localfilepathlen++] = 0;
2497 }
2498 filepath = localfilepath;
2499 localfileerror:
2500 if (localfilepathlen == 0)
2501 localfilepath[0] = '\0';
2502
2503 break;
2504 case PR_SRV_STATE_FILE_NONE:
2505 default:
2506 continue;
2507 }
2508
2509 /* preload global state file */
2510 errno = 0;
2511 f = fopen(filepath, "r");
2512 if (errno && fileopenerr)
2513 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2514 if (!f)
2515 continue;
2516
2517 mybuf[0] = '\0';
2518 mybuflen = 0;
2519 version = 0;
2520
2521 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002522 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2523 Warning("Can't read first line of the server state file '%s'\n", filepath);
2524 goto fileclose;
2525 }
2526
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002527 cur = mybuf;
2528 version = atoi(cur);
2529 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2530 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002531 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002532
2533 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2534 int bk_f_forced_id = 0;
2535 int check_id = 0;
2536 int check_name = 0;
2537
2538 mybuflen = strlen(mybuf);
2539 cur = mybuf;
2540 end = cur + mybuflen;
2541
2542 bk = NULL;
2543 srv = NULL;
2544
2545 /* we need at least one character */
2546 if (mybuflen == 0)
2547 continue;
2548
2549 /* ignore blank characters at the beginning of the line */
2550 while (isspace(*cur))
2551 ++cur;
2552
2553 if (cur == end)
2554 continue;
2555
2556 /* ignore comment line */
2557 if (*cur == '#')
2558 continue;
2559
2560 /* we're now ready to move the line into *srv_params[] */
2561 params[0] = cur;
2562 arg = 1;
2563 srv_arg = 0;
2564 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2565 if (isspace(*cur)) {
2566 *cur = '\0';
2567 ++cur;
2568 while (isspace(*cur))
2569 ++cur;
2570 switch (version) {
2571 case 1:
2572 /*
2573 * srv_addr: params[4] => srv_params[0]
2574 * srv_op_state: params[5] => srv_params[1]
2575 * srv_admin_state: params[6] => srv_params[2]
2576 * srv_uweight: params[7] => srv_params[3]
2577 * srv_iweight: params[8] => srv_params[4]
2578 * srv_last_time_change: params[9] => srv_params[5]
2579 * srv_check_status: params[10] => srv_params[6]
2580 * srv_check_result: params[11] => srv_params[7]
2581 * srv_check_health: params[12] => srv_params[8]
2582 * srv_check_state: params[13] => srv_params[9]
2583 * srv_agent_state: params[14] => srv_params[10]
2584 * bk_f_forced_id: params[15] => srv_params[11]
2585 * srv_f_forced_id: params[16] => srv_params[12]
2586 */
2587 if (arg >= 4) {
2588 srv_params[srv_arg] = cur;
2589 ++srv_arg;
2590 }
2591 break;
2592 }
2593
2594 params[arg] = cur;
2595 ++arg;
2596 }
2597 else {
2598 ++cur;
2599 }
2600 }
2601
2602 /* if line is incomplete line, then ignore it.
2603 * otherwise, update useful flags */
2604 switch (version) {
2605 case 1:
2606 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2607 continue;
2608 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2609 check_id = (atoi(params[0]) == curproxy->uuid);
2610 check_name = (strcmp(curproxy->id, params[1]) == 0);
2611 break;
2612 }
2613
2614 diff = 0;
2615 bk = curproxy;
2616
2617 /* if backend can't be found, let's continue */
2618 if (!check_id && !check_name)
2619 continue;
2620 else if (!check_id && check_name) {
2621 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2622 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2623 }
2624 else if (check_id && !check_name) {
2625 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2626 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2627 /* if name doesn't match, we still want to update curproxy if the backend id
2628 * was forced in previous the previous configuration */
2629 if (!bk_f_forced_id)
2630 continue;
2631 }
2632
2633 /* look for the server by its id: param[2] */
2634 /* else look for the server by its name: param[3] */
2635 diff = 0;
2636 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2637
2638 if (!srv) {
2639 /* if no server found, then warning and continue with next line */
2640 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2641 params[3], params[2], params[0], params[1]);
2642 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2643 params[3], params[2], params[0], params[1]);
2644 continue;
2645 }
2646 else if (diff & PR_FBM_MISMATCH_ID) {
2647 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);
2648 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);
2649 }
2650 else if (diff & PR_FBM_MISMATCH_NAME) {
2651 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);
2652 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);
2653 }
2654
2655 /* now we can proceed with server's state update */
2656 srv_update_state(srv, version, srv_params);
2657 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002658fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002659 fclose(f);
2660 }
2661}
2662
Simon Horman7d09b9a2013-02-12 10:45:51 +09002663/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002664 * update a server's current IP address.
2665 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2666 * ip is in network format.
2667 * updater is a string which contains an information about the requester of the update.
2668 * updater is used if not NULL.
2669 *
2670 * A log line and a stderr warning message is generated based on server's backend options.
2671 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002672int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002673{
2674 /* generates a log line and a warning on stderr */
2675 if (1) {
2676 /* book enough space for both IPv4 and IPv6 */
2677 char oldip[INET6_ADDRSTRLEN];
2678 char newip[INET6_ADDRSTRLEN];
2679
2680 memset(oldip, '\0', INET6_ADDRSTRLEN);
2681 memset(newip, '\0', INET6_ADDRSTRLEN);
2682
2683 /* copy old IP address in a string */
2684 switch (s->addr.ss_family) {
2685 case AF_INET:
2686 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2687 break;
2688 case AF_INET6:
2689 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2690 break;
2691 };
2692
2693 /* copy new IP address in a string */
2694 switch (ip_sin_family) {
2695 case AF_INET:
2696 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2697 break;
2698 case AF_INET6:
2699 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2700 break;
2701 };
2702
2703 /* save log line into a buffer */
2704 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2705 s->proxy->id, s->id, oldip, newip, updater);
2706
2707 /* write the buffer on stderr */
2708 Warning("%s.\n", trash.str);
2709
2710 /* send a log */
2711 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2712 }
2713
2714 /* save the new IP family */
2715 s->addr.ss_family = ip_sin_family;
2716 /* save the new IP address */
2717 switch (ip_sin_family) {
2718 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002719 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002720 break;
2721 case AF_INET6:
2722 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2723 break;
2724 };
2725
2726 return 0;
2727}
2728
2729/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002730 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2731 *
2732 * Caller can pass its name through <updater> to get it integrated in the response message
2733 * returned by the function.
2734 *
2735 * The function first does the following, in that order:
2736 * - validates the new addr and/or port
2737 * - checks if an update is required (new IP or port is different than current ones)
2738 * - checks the update is allowed:
2739 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2740 * - allow all changes if no CHECKS are configured
2741 * - if CHECK is configured:
2742 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2743 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2744 * conditions are met
2745 */
2746const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2747{
2748 struct sockaddr_storage sa;
2749 int ret, port_change_required;
2750 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00002751 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002752 struct chunk *msg;
2753
2754 msg = get_trash_chunk();
2755 chunk_reset(msg);
2756
2757 if (addr) {
2758 memset(&sa, 0, sizeof(struct sockaddr_storage));
2759 if (str2ip2(addr, &sa, 0) == NULL) {
2760 chunk_printf(msg, "Invalid addr '%s'", addr);
2761 goto out;
2762 }
2763
2764 /* changes are allowed on AF_INET* families only */
2765 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2766 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2767 goto out;
2768 }
2769
2770 /* collecting data currently setup */
2771 memset(current_addr, '\0', sizeof(current_addr));
2772 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2773 /* changes are allowed on AF_INET* families only */
2774 if ((ret != AF_INET) && (ret != AF_INET6)) {
2775 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2776 goto out;
2777 }
2778
2779 /* applying ADDR changes if required and allowed
2780 * ipcmp returns 0 when both ADDR are the same
2781 */
2782 if (ipcmp(&s->addr, &sa) == 0) {
2783 chunk_appendf(msg, "no need to change the addr");
2784 goto port;
2785 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002786 ipcpy(&sa, &s->addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002787
2788 /* we also need to update check's ADDR only if it uses the server's one */
2789 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002790 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002791 }
2792
2793 /* we also need to update agent ADDR only if it use the server's one */
2794 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002795 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002796 }
2797
2798 /* update report for caller */
2799 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2800 }
2801
2802 port:
2803 if (port) {
2804 char sign = '\0';
2805 char *endptr;
2806
2807 if (addr)
2808 chunk_appendf(msg, ", ");
2809
2810 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01002811 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002812
2813 /* check if PORT change is required */
2814 port_change_required = 0;
2815
2816 sign = *port;
2817 new_port = strtol(port, &endptr, 10);
2818 if ((errno != 0) || (port == endptr)) {
2819 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2820 goto out;
2821 }
2822
2823 /* check if caller triggers a port mapped or offset */
2824 if (sign == '-' || (sign == '+')) {
2825 /* check if server currently uses port map */
2826 if (!(s->flags & SRV_F_MAPPORTS)) {
2827 /* switch from fixed port to port map mandatorily triggers
2828 * a port change */
2829 port_change_required = 1;
2830 /* check is configured
2831 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2832 * prevent PORT change if check doesn't have it's dedicated port while switching
2833 * to port mapping */
2834 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2835 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.");
2836 goto out;
2837 }
2838 }
2839 /* we're already using port maps */
2840 else {
2841 port_change_required = current_port != new_port;
2842 }
2843 }
2844 /* fixed port */
2845 else {
2846 port_change_required = current_port != new_port;
2847 }
2848
2849 /* applying PORT changes if required and update response message */
2850 if (port_change_required) {
2851 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01002852 s->svc_port = new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002853
2854 /* prepare message */
2855 chunk_appendf(msg, "port changed from '");
2856 if (s->flags & SRV_F_MAPPORTS)
2857 chunk_appendf(msg, "+");
2858 chunk_appendf(msg, "%d' to '", current_port);
2859
2860 if (sign == '-') {
2861 s->flags |= SRV_F_MAPPORTS;
2862 chunk_appendf(msg, "%c", sign);
2863 /* just use for result output */
2864 new_port = -new_port;
2865 }
2866 else if (sign == '+') {
2867 s->flags |= SRV_F_MAPPORTS;
2868 chunk_appendf(msg, "%c", sign);
2869 }
2870 else {
2871 s->flags &= ~SRV_F_MAPPORTS;
2872 }
2873
2874 chunk_appendf(msg, "%d'", new_port);
2875
2876 /* we also need to update health checks port only if it uses server's realport */
2877 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2878 s->check.port = new_port;
2879 }
2880 }
2881 else {
2882 chunk_appendf(msg, "no need to change the port");
2883 }
2884 }
2885
2886out:
2887 if (updater)
2888 chunk_appendf(msg, " by '%s'", updater);
2889 chunk_appendf(msg, "\n");
2890 return msg->str;
2891}
2892
2893
2894/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002895 * update server status based on result of name resolution
2896 * returns:
2897 * 0 if server status is updated
2898 * 1 if server status has not changed
2899 */
2900int snr_update_srv_status(struct server *s)
2901{
2902 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002903 struct dns_resolvers *resolvers;
2904
2905 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002906
2907 switch (resolution->status) {
2908 case RSLV_STATUS_NONE:
2909 /* status when HAProxy has just (re)started */
2910 trigger_resolution(s);
2911 break;
2912
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002913 case RSLV_STATUS_VALID:
2914 /*
2915 * resume health checks
2916 * server will be turned back on if health check is safe
2917 */
2918 if (!(s->admin & SRV_ADMF_RMAINT))
2919 return 1;
2920 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2921 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
2922 s->proxy->id, s->id);
2923
2924 Warning("%s.\n", trash.str);
2925 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2926 return 0;
2927
2928 case RSLV_STATUS_NX:
2929 /* stop server if resolution is NX for a long enough period */
2930 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
2931 if (s->admin & SRV_ADMF_RMAINT)
2932 return 1;
2933 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
2934 return 0;
2935 }
2936 break;
2937
2938 case RSLV_STATUS_TIMEOUT:
2939 /* stop server if resolution is TIMEOUT for a long enough period */
2940 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
2941 if (s->admin & SRV_ADMF_RMAINT)
2942 return 1;
2943 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
2944 return 0;
2945 }
2946 break;
2947
2948 case RSLV_STATUS_REFUSED:
2949 /* stop server if resolution is REFUSED for a long enough period */
2950 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
2951 if (s->admin & SRV_ADMF_RMAINT)
2952 return 1;
2953 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
2954 return 0;
2955 }
2956 break;
2957
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002958 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002959 /* stop server if resolution is in unmatched error for a long enough period */
2960 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
2961 if (s->admin & SRV_ADMF_RMAINT)
2962 return 1;
2963 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
2964 return 0;
2965 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002966 break;
2967 }
2968
2969 return 1;
2970}
2971
2972/*
2973 * Server Name Resolution valid response callback
2974 * It expects:
2975 * - <nameserver>: the name server which answered the valid response
2976 * - <response>: buffer containing a valid DNS response
2977 * - <response_len>: size of <response>
2978 * It performs the following actions:
2979 * - ignore response if current ip found and server family not met
2980 * - update with first new ip found if family is met and current IP is not found
2981 * returns:
2982 * 0 on error
2983 * 1 when no error or safe ignore
2984 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002985int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002986{
2987 struct server *s;
2988 void *serverip, *firstip;
2989 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002990 int ret;
2991 struct chunk *chk = get_trash_chunk();
2992
2993 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002994 firstip = NULL; /* pointer to the first valid response found */
2995 /* it will be used as the new IP if a change is required */
2996 firstip_sin_family = AF_UNSPEC;
2997 serverip = NULL; /* current server IP address */
2998
2999 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003000 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003001
3002 /* initializing server IP pointer */
3003 server_sin_family = s->addr.ss_family;
3004 switch (server_sin_family) {
3005 case AF_INET:
3006 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3007 break;
3008
3009 case AF_INET6:
3010 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3011 break;
3012
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003013 case AF_UNSPEC:
3014 break;
3015
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003016 default:
3017 goto invalid;
3018 }
3019
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003020 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003021 serverip, server_sin_family, &firstip,
3022 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003023
3024 switch (ret) {
3025 case DNS_UPD_NO:
3026 if (resolution->status != RSLV_STATUS_VALID) {
3027 resolution->status = RSLV_STATUS_VALID;
3028 resolution->last_status_change = now_ms;
3029 }
3030 goto stop_resolution;
3031
3032 case DNS_UPD_SRVIP_NOT_FOUND:
3033 goto save_ip;
3034
3035 case DNS_UPD_CNAME:
3036 if (resolution->status != RSLV_STATUS_VALID) {
3037 resolution->status = RSLV_STATUS_VALID;
3038 resolution->last_status_change = now_ms;
3039 }
3040 goto invalid;
3041
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003042 case DNS_UPD_NO_IP_FOUND:
3043 if (resolution->status != RSLV_STATUS_OTHER) {
3044 resolution->status = RSLV_STATUS_OTHER;
3045 resolution->last_status_change = now_ms;
3046 }
3047 goto stop_resolution;
3048
Baptiste Assmannfad03182015-10-28 02:03:32 +01003049 case DNS_UPD_NAME_ERROR:
3050 /* if this is not the last expected response, we ignore it */
3051 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3052 return 0;
3053 /* update resolution status to OTHER error type */
3054 if (resolution->status != RSLV_STATUS_OTHER) {
3055 resolution->status = RSLV_STATUS_OTHER;
3056 resolution->last_status_change = now_ms;
3057 }
3058 goto stop_resolution;
3059
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003060 default:
3061 goto invalid;
3062
3063 }
3064
3065 save_ip:
3066 nameserver->counters.update += 1;
3067 if (resolution->status != RSLV_STATUS_VALID) {
3068 resolution->status = RSLV_STATUS_VALID;
3069 resolution->last_status_change = now_ms;
3070 }
3071
3072 /* save the first ip we found */
3073 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3074 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3075
3076 stop_resolution:
3077 /* update last resolution date and time */
3078 resolution->last_resolution = now_ms;
3079 /* reset current status flag */
3080 resolution->step = RSLV_STEP_NONE;
3081 /* reset values */
3082 dns_reset_resolution(resolution);
3083
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003084 dns_update_resolvers_timeout(nameserver->resolvers);
3085
3086 snr_update_srv_status(s);
3087 return 0;
3088
3089 invalid:
3090 nameserver->counters.invalid += 1;
3091 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3092 goto stop_resolution;
3093
3094 snr_update_srv_status(s);
3095 return 0;
3096}
3097
3098/*
3099 * Server Name Resolution error management callback
3100 * returns:
3101 * 0 on error
3102 * 1 when no error or safe ignore
3103 */
3104int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3105{
3106 struct server *s;
3107 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003108 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003109
3110 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003111 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003112 resolvers = resolution->resolvers;
3113
3114 /* can be ignored if this is not the last response */
3115 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3116 return 1;
3117 }
3118
3119 switch (error_code) {
3120 case DNS_RESP_INVALID:
3121 case DNS_RESP_WRONG_NAME:
3122 if (resolution->status != RSLV_STATUS_INVALID) {
3123 resolution->status = RSLV_STATUS_INVALID;
3124 resolution->last_status_change = now_ms;
3125 }
3126 break;
3127
3128 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003129 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003130 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003131 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003132 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003133 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3134 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003135
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003136 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003137 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003138 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003139 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003140 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003141 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003142 }
3143 else if (res_preferred_afinet) {
3144 /* fallback from A to AAAA */
3145 resolution->query_type = DNS_RTYPE_AAAA;
3146 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003147 else {
3148 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003149 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003150 resolution->query_type = DNS_RTYPE_A;
3151 } else {
3152 resolution->query_type = DNS_RTYPE_AAAA;
3153 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003154 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003155
3156 dns_send_query(resolution);
3157
3158 /*
3159 * move the resolution to the last element of the FIFO queue
3160 * and update timeout wakeup based on the new first entry
3161 */
3162 if (dns_check_resolution_queue(resolvers) > 1) {
3163 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003164 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003165 /* ex first resolution goes to the end of the queue */
3166 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3167 }
3168 dns_update_resolvers_timeout(resolvers);
3169 goto leave;
3170 }
3171 else {
3172 if (resolution->status != RSLV_STATUS_OTHER) {
3173 resolution->status = RSLV_STATUS_OTHER;
3174 resolution->last_status_change = now_ms;
3175 }
3176 }
3177 break;
3178
3179 case DNS_RESP_NX_DOMAIN:
3180 if (resolution->status != RSLV_STATUS_NX) {
3181 resolution->status = RSLV_STATUS_NX;
3182 resolution->last_status_change = now_ms;
3183 }
3184 break;
3185
3186 case DNS_RESP_REFUSED:
3187 if (resolution->status != RSLV_STATUS_REFUSED) {
3188 resolution->status = RSLV_STATUS_REFUSED;
3189 resolution->last_status_change = now_ms;
3190 }
3191 break;
3192
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003193 case DNS_RESP_TIMEOUT:
3194 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3195 resolution->status = RSLV_STATUS_TIMEOUT;
3196 resolution->last_status_change = now_ms;
3197 }
3198 break;
3199 }
3200
3201 /* update last resolution date and time */
3202 resolution->last_resolution = now_ms;
3203 /* reset current status flag */
3204 resolution->step = RSLV_STEP_NONE;
3205 /* reset values */
3206 dns_reset_resolution(resolution);
3207
3208 LIST_DEL(&resolution->list);
3209 dns_update_resolvers_timeout(resolvers);
3210
3211 leave:
3212 snr_update_srv_status(s);
3213 return 1;
3214}
3215
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003216/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3217 * resolver. This is suited for initial address configuration. Returns 0 on
3218 * success otherwise a non-zero error code. In case of error, *err_code, if
3219 * not NULL, is filled up.
3220 */
3221int srv_set_addr_via_libc(struct server *srv, int *err_code)
3222{
3223 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003224 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003225 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003226 return 1;
3227 }
3228 return 0;
3229}
3230
3231/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3232 * from the state file. This is suited for initial address configuration.
3233 * Returns 0 on success otherwise a non-zero error code. In case of error,
3234 * *err_code, if not NULL, is filled up.
3235 */
3236static int srv_apply_lastaddr(struct server *srv, int *err_code)
3237{
3238 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3239 if (err_code)
3240 *err_code |= ERR_WARN;
3241 return 1;
3242 }
3243 return 0;
3244}
3245
Willy Tarreau25e51522016-11-04 15:10:17 +01003246/* returns 0 if no error, otherwise a combination of ERR_* flags */
3247static int srv_iterate_initaddr(struct server *srv)
3248{
3249 int return_code = 0;
3250 int err_code;
3251 unsigned int methods;
3252
3253 methods = srv->init_addr_methods;
3254 if (!methods) { // default to "last,libc"
3255 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3256 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3257 }
3258
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003259 /* "-dr" : always append "none" so that server addresses resolution
3260 * failures are silently ignored, this is convenient to validate some
3261 * configs out of their environment.
3262 */
3263 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3264 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3265
Willy Tarreau25e51522016-11-04 15:10:17 +01003266 while (methods) {
3267 err_code = 0;
3268 switch (srv_get_next_initaddr(&methods)) {
3269 case SRV_IADDR_LAST:
3270 if (!srv->lastaddr)
3271 continue;
3272 if (srv_apply_lastaddr(srv, &err_code) == 0)
3273 return return_code;
3274 return_code |= err_code;
3275 break;
3276
3277 case SRV_IADDR_LIBC:
3278 if (!srv->hostname)
3279 continue;
3280 if (srv_set_addr_via_libc(srv, &err_code) == 0)
3281 return return_code;
3282 return_code |= err_code;
3283 break;
3284
Willy Tarreau37ebe122016-11-04 15:17:58 +01003285 case SRV_IADDR_NONE:
3286 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003287 if (return_code) {
3288 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3289 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3290 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003291 return return_code;
3292
Willy Tarreau4310d362016-11-02 15:05:56 +01003293 case SRV_IADDR_IP:
3294 ipcpy(&srv->init_addr, &srv->addr);
3295 if (return_code) {
3296 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3297 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3298 }
3299 return return_code;
3300
Willy Tarreau25e51522016-11-04 15:10:17 +01003301 default: /* unhandled method */
3302 break;
3303 }
3304 }
3305
3306 if (!return_code) {
3307 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3308 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3309 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003310 else {
3311 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3312 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3313 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003314
3315 return_code |= ERR_ALERT | ERR_FATAL;
3316 return return_code;
3317}
3318
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003319/*
3320 * This function parses all backends and all servers within each backend
3321 * and performs servers' addr resolution based on information provided by:
3322 * - configuration file
3323 * - server-state file (states provided by an 'old' haproxy process)
3324 *
3325 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3326 */
3327int srv_init_addr(void)
3328{
3329 struct proxy *curproxy;
3330 int return_code = 0;
3331
3332 curproxy = proxy;
3333 while (curproxy) {
3334 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003335
3336 /* servers are in backend only */
3337 if (!(curproxy->cap & PR_CAP_BE))
3338 goto srv_init_addr_next;
3339
Willy Tarreau25e51522016-11-04 15:10:17 +01003340 for (srv = curproxy->srv; srv; srv = srv->next)
3341 if (srv->hostname)
3342 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003343
3344 srv_init_addr_next:
3345 curproxy = curproxy->next;
3346 }
3347
3348 return return_code;
3349}
3350
Willy Tarreau21b069d2016-11-23 17:15:08 +01003351/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3352 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003353 * 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 +01003354 * used for CLI commands requiring a server name.
3355 * Important: the <arg> is modified to remove the '/'.
3356 */
3357struct server *cli_find_server(struct appctx *appctx, char *arg)
3358{
3359 struct proxy *px;
3360 struct server *sv;
3361 char *line;
3362
3363 /* split "backend/server" and make <line> point to server */
3364 for (line = arg; *line; line++)
3365 if (*line == '/') {
3366 *line++ = '\0';
3367 break;
3368 }
3369
3370 if (!*line || !*arg) {
3371 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003372 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003373 return NULL;
3374 }
3375
3376 if (!get_backend_server(arg, line, &px, &sv)) {
3377 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003378 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003379 return NULL;
3380 }
3381
3382 if (px->state == PR_STSTOPPED) {
3383 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003384 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003385 return NULL;
3386 }
3387
3388 return sv;
3389}
3390
William Lallemand222baf22016-11-19 02:00:33 +01003391
3392static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3393{
3394 struct server *sv;
3395 const char *warning;
3396
3397 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3398 return 1;
3399
3400 sv = cli_find_server(appctx, args[2]);
3401 if (!sv)
3402 return 1;
3403
3404 if (strcmp(args[3], "weight") == 0) {
3405 warning = server_parse_weight_change_request(sv, args[4]);
3406 if (warning) {
3407 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003408 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003409 }
3410 }
3411 else if (strcmp(args[3], "state") == 0) {
3412 if (strcmp(args[4], "ready") == 0)
3413 srv_adm_set_ready(sv);
3414 else if (strcmp(args[4], "drain") == 0)
3415 srv_adm_set_drain(sv);
3416 else if (strcmp(args[4], "maint") == 0)
3417 srv_adm_set_maint(sv);
3418 else {
3419 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003420 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003421 }
3422 }
3423 else if (strcmp(args[3], "health") == 0) {
3424 if (sv->track) {
3425 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003426 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003427 }
3428 else if (strcmp(args[4], "up") == 0) {
3429 sv->check.health = sv->check.rise + sv->check.fall - 1;
3430 srv_set_running(sv, "changed from CLI");
3431 }
3432 else if (strcmp(args[4], "stopping") == 0) {
3433 sv->check.health = sv->check.rise + sv->check.fall - 1;
3434 srv_set_stopping(sv, "changed from CLI");
3435 }
3436 else if (strcmp(args[4], "down") == 0) {
3437 sv->check.health = 0;
3438 srv_set_stopped(sv, "changed from CLI");
3439 }
3440 else {
3441 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003442 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003443 }
3444 }
3445 else if (strcmp(args[3], "agent") == 0) {
3446 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3447 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003448 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003449 }
3450 else if (strcmp(args[4], "up") == 0) {
3451 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3452 srv_set_running(sv, "changed from CLI");
3453 }
3454 else if (strcmp(args[4], "down") == 0) {
3455 sv->agent.health = 0;
3456 srv_set_stopped(sv, "changed from CLI");
3457 }
3458 else {
3459 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003460 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003461 }
3462 }
3463 else if (strcmp(args[3], "check-port") == 0) {
3464 int i = 0;
3465 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3466 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003467 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003468 }
3469 if ((i < 0) || (i > 65535)) {
3470 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003471 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003472 }
3473 /* prevent the update of port to 0 if MAPPORTS are in use */
3474 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3475 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003476 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003477 return 1;
3478 }
3479 sv->check.port = i;
3480 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003481 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003482 }
3483 else if (strcmp(args[3], "addr") == 0) {
3484 char *addr = NULL;
3485 char *port = NULL;
3486 if (strlen(args[4]) == 0) {
3487 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003488 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003489 return 1;
3490 }
3491 else {
3492 addr = args[4];
3493 }
3494 if (strcmp(args[5], "port") == 0) {
3495 port = args[6];
3496 }
3497 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3498 if (warning) {
3499 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003500 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003501 }
3502 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3503 }
3504 else {
3505 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 +01003506 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003507 }
3508 return 1;
3509}
3510
William Lallemand6b160942016-11-22 12:34:35 +01003511static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3512{
3513 struct stream_interface *si = appctx->owner;
3514 struct proxy *px;
3515 struct server *sv;
3516 char *line;
3517
3518
3519 /* split "backend/server" and make <line> point to server */
3520 for (line = args[2]; *line; line++)
3521 if (*line == '/') {
3522 *line++ = '\0';
3523 break;
3524 }
3525
3526 if (!*line) {
3527 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003528 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003529 return 1;
3530 }
3531
3532 if (!get_backend_server(args[2], line, &px, &sv)) {
3533 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003534 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003535 return 1;
3536 }
3537
3538 /* return server's effective weight at the moment */
3539 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003540 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01003541 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003542 return 0;
3543 }
William Lallemand6b160942016-11-22 12:34:35 +01003544 return 1;
3545}
3546
3547static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
3548{
3549 struct server *sv;
3550 const char *warning;
3551
3552 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3553 return 1;
3554
3555 sv = cli_find_server(appctx, args[2]);
3556 if (!sv)
3557 return 1;
3558
3559 warning = server_parse_weight_change_request(sv, args[3]);
3560 if (warning) {
3561 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003562 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003563 }
3564 return 1;
3565}
3566
Willy Tarreaub8026272016-11-23 11:26:56 +01003567/* parse a "set maxconn server" command. It always returns 1. */
3568static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
3569{
3570 struct server *sv;
3571 const char *warning;
3572
3573 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3574 return 1;
3575
3576 sv = cli_find_server(appctx, args[3]);
3577 if (!sv)
3578 return 1;
3579
3580 warning = server_parse_maxconn_change_request(sv, args[4]);
3581 if (warning) {
3582 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003583 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01003584 }
3585 return 1;
3586}
William Lallemand6b160942016-11-22 12:34:35 +01003587
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003588/* parse a "disable agent" command. It always returns 1. */
3589static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
3590{
3591 struct server *sv;
3592
3593 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3594 return 1;
3595
3596 sv = cli_find_server(appctx, args[2]);
3597 if (!sv)
3598 return 1;
3599
3600 sv->agent.state &= ~CHK_ST_ENABLED;
3601 return 1;
3602}
3603
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003604/* parse a "disable health" command. It always returns 1. */
3605static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
3606{
3607 struct server *sv;
3608
3609 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3610 return 1;
3611
3612 sv = cli_find_server(appctx, args[2]);
3613 if (!sv)
3614 return 1;
3615
3616 sv->check.state &= ~CHK_ST_ENABLED;
3617 return 1;
3618}
3619
Willy Tarreauffb4d582016-11-24 12:47:00 +01003620/* parse a "disable server" command. It always returns 1. */
3621static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
3622{
3623 struct server *sv;
3624
3625 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3626 return 1;
3627
3628 sv = cli_find_server(appctx, args[2]);
3629 if (!sv)
3630 return 1;
3631
3632 srv_adm_set_maint(sv);
3633 return 1;
3634}
3635
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003636/* parse a "enable agent" command. It always returns 1. */
3637static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
3638{
3639 struct server *sv;
3640
3641 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3642 return 1;
3643
3644 sv = cli_find_server(appctx, args[2]);
3645 if (!sv)
3646 return 1;
3647
3648 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
3649 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003650 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003651 return 1;
3652 }
3653
3654 sv->agent.state |= CHK_ST_ENABLED;
3655 return 1;
3656}
3657
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003658/* parse a "enable health" command. It always returns 1. */
3659static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
3660{
3661 struct server *sv;
3662
3663 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3664 return 1;
3665
3666 sv = cli_find_server(appctx, args[2]);
3667 if (!sv)
3668 return 1;
3669
3670 sv->check.state |= CHK_ST_ENABLED;
3671 return 1;
3672}
3673
Willy Tarreauffb4d582016-11-24 12:47:00 +01003674/* parse a "enable server" command. It always returns 1. */
3675static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
3676{
3677 struct server *sv;
3678
3679 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3680 return 1;
3681
3682 sv = cli_find_server(appctx, args[2]);
3683 if (!sv)
3684 return 1;
3685
3686 srv_adm_set_ready(sv);
3687 return 1;
3688}
3689
William Lallemand222baf22016-11-19 02:00:33 +01003690/* register cli keywords */
3691static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003692 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003693 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003694 { { "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 +01003695 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003696 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003697 { { "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 +01003698 { { "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 +01003699 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01003700 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
3701 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
3702
William Lallemand222baf22016-11-19 02:00:33 +01003703 {{},}
3704}};
3705
3706__attribute__((constructor))
3707static void __server_init(void)
3708{
3709 cli_register_kw(&cli_kws);
3710}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003711
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003712/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003713 * Local variables:
3714 * c-indent-level: 8
3715 * c-basic-offset: 8
3716 * End:
3717 */