blob: 9cc02f74f1df73dd418a578436191a5b60e9515b [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;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +0100970 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +0200971 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 */
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001008 sk = str2sa_range(args[2], &port, &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);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001016 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001017 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 Tarreau6ecb10a2017-01-06 18:36:06 +01001065 newsrv->svc_port = port;
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
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001068 if (!newsrv->hostname && !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 }
Misiekea849332017-01-09 09:39:51 +01001154 else if (!strcmp(args[cur_arg], "agent-addr")) {
1155 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1156 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1157 goto out;
1158 }
1159
1160 cur_arg += 2;
1161 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001162 else if (!strcmp(args[cur_arg], "agent-port")) {
1163 global.maxsock++;
1164 newsrv->agent.port = atol(args[cur_arg + 1]);
1165 cur_arg += 2;
1166 }
James Brown55f9ff12015-10-21 18:19:05 -07001167 else if (!strcmp(args[cur_arg], "agent-send")) {
1168 global.maxsock++;
1169 free(newsrv->agent.send_string);
1170 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1171 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1172 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1173 cur_arg += 2;
1174 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001175 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1176 newsrv->cookie = strdup(args[cur_arg + 1]);
1177 newsrv->cklen = strlen(args[cur_arg + 1]);
1178 cur_arg += 2;
1179 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001180 else if (!strcmp(args[cur_arg], "init-addr")) {
1181 char *p, *end;
1182 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001183 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001184
1185 newsrv->init_addr_methods = 0;
1186 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1187
1188 for (p = args[cur_arg + 1]; *p; p = end) {
1189 /* cut on next comma */
1190 for (end = p; *end && *end != ','; end++);
1191 if (*end)
1192 *(end++) = 0;
1193
Willy Tarreau4310d362016-11-02 15:05:56 +01001194 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001195 if (!strcmp(p, "libc")) {
1196 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1197 }
1198 else if (!strcmp(p, "last")) {
1199 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1200 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001201 else if (!strcmp(p, "none")) {
1202 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1203 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001204 else if (str2ip2(p, &sa, 0)) {
1205 if (is_addr(&newsrv->init_addr)) {
1206 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1207 file, linenum, args[cur_arg], p);
1208 err_code |= ERR_ALERT | ERR_FATAL;
1209 goto out;
1210 }
1211 newsrv->init_addr = sa;
1212 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1213 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001214 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001215 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001216 file, linenum, args[cur_arg], p);
1217 err_code |= ERR_ALERT | ERR_FATAL;
1218 goto out;
1219 }
1220 if (!done) {
1221 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1222 file, linenum, args[cur_arg], p);
1223 err_code |= ERR_ALERT | ERR_FATAL;
1224 goto out;
1225 }
1226 }
1227 cur_arg += 2;
1228 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001229 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1230 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1231 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1232 cur_arg += 2;
1233 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001234 else if (!strcmp(args[cur_arg], "resolvers")) {
1235 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1236 cur_arg += 2;
1237 }
1238 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1239 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001240 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001241 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001242 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001243 else {
1244 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1245 file, linenum, args[cur_arg]);
1246 err_code |= ERR_ALERT | ERR_FATAL;
1247 goto out;
1248 }
1249 cur_arg += 2;
1250 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001251 else if (!strcmp(args[cur_arg], "resolve-net")) {
1252 char *p, *e;
1253 unsigned char mask;
1254 struct dns_options *opt;
1255
1256 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1257 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1258 file, linenum, args[cur_arg]);
1259 err_code |= ERR_ALERT | ERR_FATAL;
1260 goto out;
1261 }
1262
1263 opt = &newsrv->dns_opts;
1264
1265 /* Split arguments by comma, and convert it from ipv4 or ipv6
1266 * string network in in_addr or in6_addr.
1267 */
1268 p = args[cur_arg + 1];
1269 e = p;
1270 while (*p != '\0') {
1271 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001272 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001273 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1274 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1275 err_code |= ERR_ALERT | ERR_FATAL;
1276 goto out;
1277 }
1278 /* look for end or comma. */
1279 while (*e != ',' && *e != '\0')
1280 e++;
1281 if (*e == ',') {
1282 *e = '\0';
1283 e++;
1284 }
1285 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1286 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1287 /* Try to convert input string from ipv4 or ipv6 network. */
1288 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1289 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1290 &mask)) {
1291 /* Try to convert input string from ipv6 network. */
1292 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1293 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1294 } else {
1295 /* All network conversions fail, retrun error. */
1296 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1297 file, linenum, args[cur_arg], p);
1298 err_code |= ERR_ALERT | ERR_FATAL;
1299 goto out;
1300 }
1301 opt->pref_net_nb++;
1302 p = e;
1303 }
1304
1305 cur_arg += 2;
1306 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001307 else if (!strcmp(args[cur_arg], "rise")) {
1308 if (!*args[cur_arg + 1]) {
1309 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1310 file, linenum, args[cur_arg]);
1311 err_code |= ERR_ALERT | ERR_FATAL;
1312 goto out;
1313 }
1314
1315 newsrv->check.rise = atol(args[cur_arg + 1]);
1316 if (newsrv->check.rise <= 0) {
1317 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1318 file, linenum, args[cur_arg]);
1319 err_code |= ERR_ALERT | ERR_FATAL;
1320 goto out;
1321 }
1322
1323 if (newsrv->check.health)
1324 newsrv->check.health = newsrv->check.rise;
1325 cur_arg += 2;
1326 }
1327 else if (!strcmp(args[cur_arg], "fall")) {
1328 newsrv->check.fall = atol(args[cur_arg + 1]);
1329
1330 if (!*args[cur_arg + 1]) {
1331 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1332 file, linenum, args[cur_arg]);
1333 err_code |= ERR_ALERT | ERR_FATAL;
1334 goto out;
1335 }
1336
1337 if (newsrv->check.fall <= 0) {
1338 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1339 file, linenum, args[cur_arg]);
1340 err_code |= ERR_ALERT | ERR_FATAL;
1341 goto out;
1342 }
1343
1344 cur_arg += 2;
1345 }
1346 else if (!strcmp(args[cur_arg], "inter")) {
1347 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1348 if (err) {
1349 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1350 file, linenum, *err, newsrv->id);
1351 err_code |= ERR_ALERT | ERR_FATAL;
1352 goto out;
1353 }
1354 if (val <= 0) {
1355 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1356 file, linenum, val, args[cur_arg], newsrv->id);
1357 err_code |= ERR_ALERT | ERR_FATAL;
1358 goto out;
1359 }
1360 newsrv->check.inter = val;
1361 cur_arg += 2;
1362 }
1363 else if (!strcmp(args[cur_arg], "fastinter")) {
1364 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1365 if (err) {
1366 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1367 file, linenum, *err, newsrv->id);
1368 err_code |= ERR_ALERT | ERR_FATAL;
1369 goto out;
1370 }
1371 if (val <= 0) {
1372 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1373 file, linenum, val, args[cur_arg], newsrv->id);
1374 err_code |= ERR_ALERT | ERR_FATAL;
1375 goto out;
1376 }
1377 newsrv->check.fastinter = val;
1378 cur_arg += 2;
1379 }
1380 else if (!strcmp(args[cur_arg], "downinter")) {
1381 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1382 if (err) {
1383 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1384 file, linenum, *err, newsrv->id);
1385 err_code |= ERR_ALERT | ERR_FATAL;
1386 goto out;
1387 }
1388 if (val <= 0) {
1389 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1390 file, linenum, val, args[cur_arg], newsrv->id);
1391 err_code |= ERR_ALERT | ERR_FATAL;
1392 goto out;
1393 }
1394 newsrv->check.downinter = val;
1395 cur_arg += 2;
1396 }
1397 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1398 struct sockaddr_storage *sk;
1399 int port1, port2;
1400 struct protocol *proto;
1401
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001402 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001403 if (!sk) {
1404 Alert("parsing [%s:%d] : '%s' : %s\n",
1405 file, linenum, args[cur_arg], errmsg);
1406 err_code |= ERR_ALERT | ERR_FATAL;
1407 goto out;
1408 }
1409
1410 proto = protocol_by_family(sk->ss_family);
1411 if (!proto || !proto->connect) {
1412 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1413 file, linenum, args[cur_arg], args[cur_arg + 1]);
1414 err_code |= ERR_ALERT | ERR_FATAL;
1415 goto out;
1416 }
1417
1418 if (port1 != port2) {
1419 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1420 file, linenum, args[cur_arg], args[cur_arg + 1]);
1421 err_code |= ERR_ALERT | ERR_FATAL;
1422 goto out;
1423 }
1424
Simon Horman41f58762015-01-30 11:22:56 +09001425 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001426 newsrv->flags |= SRV_F_CHECKADDR;
1427 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001428 cur_arg += 2;
1429 }
1430 else if (!strcmp(args[cur_arg], "port")) {
1431 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001432 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001433 cur_arg += 2;
1434 }
1435 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001436 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001437 cur_arg ++;
1438 }
1439 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001440 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001441 cur_arg ++;
1442 }
1443 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001444 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001445 cur_arg ++;
1446 }
David Safb76832014-05-08 23:42:08 -04001447 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1448 newsrv->pp_opts |= SRV_PP_V2;
1449 cur_arg ++;
1450 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001451 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1452 newsrv->check.send_proxy = 1;
1453 cur_arg ++;
1454 }
1455 else if (!strcmp(args[cur_arg], "weight")) {
1456 int w;
1457 w = atol(args[cur_arg + 1]);
1458 if (w < 0 || w > SRV_UWGHT_MAX) {
1459 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1460 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1461 err_code |= ERR_ALERT | ERR_FATAL;
1462 goto out;
1463 }
1464 newsrv->uweight = newsrv->iweight = w;
1465 cur_arg += 2;
1466 }
1467 else if (!strcmp(args[cur_arg], "minconn")) {
1468 newsrv->minconn = atol(args[cur_arg + 1]);
1469 cur_arg += 2;
1470 }
1471 else if (!strcmp(args[cur_arg], "maxconn")) {
1472 newsrv->maxconn = atol(args[cur_arg + 1]);
1473 cur_arg += 2;
1474 }
1475 else if (!strcmp(args[cur_arg], "maxqueue")) {
1476 newsrv->maxqueue = atol(args[cur_arg + 1]);
1477 cur_arg += 2;
1478 }
1479 else if (!strcmp(args[cur_arg], "slowstart")) {
1480 /* slowstart is stored in seconds */
1481 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1482 if (err) {
1483 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1484 file, linenum, *err, newsrv->id);
1485 err_code |= ERR_ALERT | ERR_FATAL;
1486 goto out;
1487 }
1488 newsrv->slowstart = (val + 999) / 1000;
1489 cur_arg += 2;
1490 }
1491 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1492
1493 if (!*args[cur_arg + 1]) {
1494 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1495 file, linenum);
1496 err_code |= ERR_ALERT | ERR_FATAL;
1497 goto out;
1498 }
1499
1500 newsrv->trackit = strdup(args[cur_arg + 1]);
1501
1502 cur_arg += 2;
1503 }
1504 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1505 global.maxsock++;
1506 do_check = 1;
1507 cur_arg += 1;
1508 }
1509 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001510 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001511 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001512 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001513 newsrv->check.state |= CHK_ST_PAUSED;
1514 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001515 cur_arg += 1;
1516 }
1517 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1518 if (!strcmp(args[cur_arg + 1], "none"))
1519 newsrv->observe = HANA_OBS_NONE;
1520 else if (!strcmp(args[cur_arg + 1], "layer4"))
1521 newsrv->observe = HANA_OBS_LAYER4;
1522 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1523 if (curproxy->mode != PR_MODE_HTTP) {
1524 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1525 file, linenum, args[cur_arg + 1]);
1526 err_code |= ERR_ALERT;
1527 }
1528 newsrv->observe = HANA_OBS_LAYER7;
1529 }
1530 else {
1531 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1532 "'layer4', 'layer7' but got '%s'\n",
1533 file, linenum, args[cur_arg], args[cur_arg + 1]);
1534 err_code |= ERR_ALERT | ERR_FATAL;
1535 goto out;
1536 }
1537
1538 cur_arg += 2;
1539 }
1540 else if (!strcmp(args[cur_arg], "on-error")) {
1541 if (!strcmp(args[cur_arg + 1], "fastinter"))
1542 newsrv->onerror = HANA_ONERR_FASTINTER;
1543 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1544 newsrv->onerror = HANA_ONERR_FAILCHK;
1545 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1546 newsrv->onerror = HANA_ONERR_SUDDTH;
1547 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1548 newsrv->onerror = HANA_ONERR_MARKDWN;
1549 else {
1550 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1551 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1552 file, linenum, args[cur_arg], args[cur_arg + 1]);
1553 err_code |= ERR_ALERT | ERR_FATAL;
1554 goto out;
1555 }
1556
1557 cur_arg += 2;
1558 }
1559 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1560 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1561 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1562 else {
1563 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1564 file, linenum, args[cur_arg], args[cur_arg + 1]);
1565 err_code |= ERR_ALERT | ERR_FATAL;
1566 goto out;
1567 }
1568
1569 cur_arg += 2;
1570 }
1571 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1572 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1573 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1574 else {
1575 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1576 file, linenum, args[cur_arg], args[cur_arg + 1]);
1577 err_code |= ERR_ALERT | ERR_FATAL;
1578 goto out;
1579 }
1580
1581 cur_arg += 2;
1582 }
1583 else if (!strcmp(args[cur_arg], "error-limit")) {
1584 if (!*args[cur_arg + 1]) {
1585 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1586 file, linenum, args[cur_arg]);
1587 err_code |= ERR_ALERT | ERR_FATAL;
1588 goto out;
1589 }
1590
1591 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1592
1593 if (newsrv->consecutive_errors_limit <= 0) {
1594 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1595 file, linenum, args[cur_arg]);
1596 err_code |= ERR_ALERT | ERR_FATAL;
1597 goto out;
1598 }
1599 cur_arg += 2;
1600 }
1601 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1602 int port_low, port_high;
1603 struct sockaddr_storage *sk;
1604 struct protocol *proto;
1605
1606 if (!*args[cur_arg + 1]) {
1607 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1608 file, linenum, "source", "usesrc", "interface");
1609 err_code |= ERR_ALERT | ERR_FATAL;
1610 goto out;
1611 }
1612
1613 newsrv->conn_src.opts |= CO_SRC_BIND;
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001614 sk = str2sa_range(args[cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001615 if (!sk) {
1616 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1617 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1618 err_code |= ERR_ALERT | ERR_FATAL;
1619 goto out;
1620 }
1621
1622 proto = protocol_by_family(sk->ss_family);
1623 if (!proto || !proto->connect) {
1624 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1625 file, linenum, args[cur_arg], args[cur_arg+1]);
1626 err_code |= ERR_ALERT | ERR_FATAL;
1627 goto out;
1628 }
1629
1630 newsrv->conn_src.source_addr = *sk;
1631
1632 if (port_low != port_high) {
1633 int i;
1634
1635 if (!port_low || !port_high) {
1636 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1637 file, linenum, args[cur_arg], args[cur_arg + 1]);
1638 err_code |= ERR_ALERT | ERR_FATAL;
1639 goto out;
1640 }
1641
1642 if (port_low <= 0 || port_low > 65535 ||
1643 port_high <= 0 || port_high > 65535 ||
1644 port_low > port_high) {
1645 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1646 file, linenum, port_low, port_high);
1647 err_code |= ERR_ALERT | ERR_FATAL;
1648 goto out;
1649 }
1650 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1651 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1652 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1653 }
1654
1655 cur_arg += 2;
1656 while (*(args[cur_arg])) {
1657 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001658#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001659 if (!*args[cur_arg + 1]) {
1660 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1661 file, linenum, "usesrc");
1662 err_code |= ERR_ALERT | ERR_FATAL;
1663 goto out;
1664 }
1665 if (!strcmp(args[cur_arg + 1], "client")) {
1666 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1667 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1668 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1669 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1670 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1671 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1672 char *name, *end;
1673
1674 name = args[cur_arg+1] + 7;
1675 while (isspace(*name))
1676 name++;
1677
1678 end = name;
1679 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1680 end++;
1681
1682 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1683 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1684 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1685 newsrv->conn_src.bind_hdr_len = end - name;
1686 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1687 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1688 newsrv->conn_src.bind_hdr_occ = -1;
1689
1690 /* now look for an occurrence number */
1691 while (isspace(*end))
1692 end++;
1693 if (*end == ',') {
1694 end++;
1695 name = end;
1696 if (*end == '-')
1697 end++;
1698 while (isdigit((int)*end))
1699 end++;
1700 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1701 }
1702
1703 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1704 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1705 " occurrences values smaller than %d.\n",
1706 file, linenum, MAX_HDR_HISTORY);
1707 err_code |= ERR_ALERT | ERR_FATAL;
1708 goto out;
1709 }
1710 } else {
1711 struct sockaddr_storage *sk;
1712 int port1, port2;
1713
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001714 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001715 if (!sk) {
1716 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1717 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1718 err_code |= ERR_ALERT | ERR_FATAL;
1719 goto out;
1720 }
1721
1722 proto = protocol_by_family(sk->ss_family);
1723 if (!proto || !proto->connect) {
1724 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1725 file, linenum, args[cur_arg], args[cur_arg+1]);
1726 err_code |= ERR_ALERT | ERR_FATAL;
1727 goto out;
1728 }
1729
1730 if (port1 != port2) {
1731 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1732 file, linenum, args[cur_arg], args[cur_arg + 1]);
1733 err_code |= ERR_ALERT | ERR_FATAL;
1734 goto out;
1735 }
1736 newsrv->conn_src.tproxy_addr = *sk;
1737 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1738 }
1739 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001740 cur_arg += 2;
1741 continue;
1742#else /* no TPROXY support */
1743 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1744 file, linenum, "usesrc");
1745 err_code |= ERR_ALERT | ERR_FATAL;
1746 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001747#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001748 } /* "usesrc" */
1749
1750 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1751#ifdef SO_BINDTODEVICE
1752 if (!*args[cur_arg + 1]) {
1753 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1754 file, linenum, args[0]);
1755 err_code |= ERR_ALERT | ERR_FATAL;
1756 goto out;
1757 }
1758 free(newsrv->conn_src.iface_name);
1759 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1760 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1761 global.last_checks |= LSTCHK_NETADM;
1762#else
1763 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1764 file, linenum, args[0], args[cur_arg]);
1765 err_code |= ERR_ALERT | ERR_FATAL;
1766 goto out;
1767#endif
1768 cur_arg += 2;
1769 continue;
1770 }
1771 /* this keyword in not an option of "source" */
1772 break;
1773 } /* while */
1774 }
1775 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1776 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1777 file, linenum, "usesrc", "source");
1778 err_code |= ERR_ALERT | ERR_FATAL;
1779 goto out;
1780 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001781 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1782#ifdef CONFIG_HAP_NS
1783 char *arg = args[cur_arg + 1];
1784 if (!strcmp(arg, "*")) {
1785 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1786 } else {
1787 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1788
1789 if (newsrv->netns == NULL)
1790 newsrv->netns = netns_store_insert(arg);
1791
1792 if (newsrv->netns == NULL) {
1793 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1794 err_code |= ERR_ALERT | ERR_FATAL;
1795 goto out;
1796 }
1797 }
1798#else
1799 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1800 file, linenum, args[0], args[cur_arg]);
1801 err_code |= ERR_ALERT | ERR_FATAL;
1802 goto out;
1803#endif
1804 cur_arg += 2;
1805 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001806 else {
1807 static int srv_dumped;
1808 struct srv_kw *kw;
1809 char *err;
1810
1811 kw = srv_find_kw(args[cur_arg]);
1812 if (kw) {
1813 char *err = NULL;
1814 int code;
1815
1816 if (!kw->parse) {
1817 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1818 file, linenum, args[0], args[1], args[cur_arg]);
1819 cur_arg += 1 + kw->skip ;
1820 err_code |= ERR_ALERT | ERR_FATAL;
1821 goto out;
1822 }
1823
1824 if (defsrv && !kw->default_ok) {
1825 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1826 file, linenum, args[0], args[1], args[cur_arg]);
1827 cur_arg += 1 + kw->skip ;
1828 err_code |= ERR_ALERT;
1829 continue;
1830 }
1831
1832 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1833 err_code |= code;
1834
1835 if (code) {
1836 if (err && *err) {
1837 indent_msg(&err, 2);
1838 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1839 }
1840 else
1841 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1842 file, linenum, args[0], args[1], args[cur_arg]);
1843 if (code & ERR_FATAL) {
1844 free(err);
1845 cur_arg += 1 + kw->skip;
1846 goto out;
1847 }
1848 }
1849 free(err);
1850 cur_arg += 1 + kw->skip;
1851 continue;
1852 }
1853
1854 err = NULL;
1855 if (!srv_dumped) {
1856 srv_dump_kws(&err);
1857 indent_msg(&err, 4);
1858 srv_dumped = 1;
1859 }
1860
1861 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1862 file, linenum, args[0], args[1], args[cur_arg],
1863 err ? " Registered keywords :" : "", err ? err : "");
1864 free(err);
1865
1866 err_code |= ERR_ALERT | ERR_FATAL;
1867 goto out;
1868 }
1869 }
1870
Willy Tarreau272adea2014-03-31 10:39:59 +02001871 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001872 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001873
1874 if (newsrv->trackit) {
1875 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1876 file, linenum);
1877 err_code |= ERR_ALERT | ERR_FATAL;
1878 goto out;
1879 }
1880
Willy Tarreau272adea2014-03-31 10:39:59 +02001881 /*
1882 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001883 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001884 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001885 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001886 (is_inet_addr(&newsrv->check.addr) ||
1887 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001888 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001889 struct list *l;
1890
1891 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1892 if (!r) {
1893 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1894 file, linenum, newsrv->id);
1895 err_code |= ERR_ALERT | ERR_FATAL;
1896 goto out;
1897 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001898 /* search the first action (connect / send / expect) in the list */
1899 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001900 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001901 if (r->action != TCPCHK_ACT_COMMENT)
1902 break;
1903 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001904 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1905 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",
1906 file, linenum, newsrv->id);
1907 err_code |= ERR_ALERT | ERR_FATAL;
1908 goto out;
1909 }
1910 else {
1911 /* scan the tcp-check ruleset to ensure a port has been configured */
1912 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001913 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001914 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1915 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",
1916 file, linenum, newsrv->id);
1917 err_code |= ERR_ALERT | ERR_FATAL;
1918 goto out;
1919 }
1920 }
1921 }
1922 }
1923
1924 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001925 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001926 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001927 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1928 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001929 goto out;
1930 }
1931
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001932 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001933 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001934
Willy Tarreau272adea2014-03-31 10:39:59 +02001935 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1936 }
1937
1938 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001939 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001940
1941 if (!newsrv->agent.port) {
1942 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1943 file, linenum, newsrv->id);
1944 err_code |= ERR_ALERT | ERR_FATAL;
1945 goto out;
1946 }
1947
1948 if (!newsrv->agent.inter)
1949 newsrv->agent.inter = newsrv->check.inter;
1950
Simon Hormanb1900d52015-01-30 11:22:54 +09001951 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001952 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001953 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1954 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001955 goto out;
1956 }
1957
1958 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1959 }
1960
1961 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001962 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001963 curproxy->srv_bck++;
1964 else
1965 curproxy->srv_act++;
1966
Willy Tarreauc5150da2014-05-13 19:27:31 +02001967 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001968 }
1969 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001970 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001971 return 0;
1972
1973 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001974 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001975 free(errmsg);
1976 return err_code;
1977}
1978
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001979/* Returns a pointer to the first server matching either id <id>.
1980 * NULL is returned if no match is found.
1981 * the lookup is performed in the backend <bk>
1982 */
1983struct server *server_find_by_id(struct proxy *bk, int id)
1984{
1985 struct eb32_node *eb32;
1986 struct server *curserver;
1987
1988 if (!bk || (id ==0))
1989 return NULL;
1990
1991 /* <bk> has no backend capabilities, so it can't have a server */
1992 if (!(bk->cap & PR_CAP_BE))
1993 return NULL;
1994
1995 curserver = NULL;
1996
1997 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1998 if (eb32)
1999 curserver = container_of(eb32, struct server, conf.id);
2000
2001 return curserver;
2002}
2003
2004/* Returns a pointer to the first server matching either name <name>, or id
2005 * if <name> starts with a '#'. NULL is returned if no match is found.
2006 * the lookup is performed in the backend <bk>
2007 */
2008struct server *server_find_by_name(struct proxy *bk, const char *name)
2009{
2010 struct server *curserver;
2011
2012 if (!bk || !name)
2013 return NULL;
2014
2015 /* <bk> has no backend capabilities, so it can't have a server */
2016 if (!(bk->cap & PR_CAP_BE))
2017 return NULL;
2018
2019 curserver = NULL;
2020 if (*name == '#') {
2021 curserver = server_find_by_id(bk, atoi(name + 1));
2022 if (curserver)
2023 return curserver;
2024 }
2025 else {
2026 curserver = bk->srv;
2027
2028 while (curserver && (strcmp(curserver->id, name) != 0))
2029 curserver = curserver->next;
2030
2031 if (curserver)
2032 return curserver;
2033 }
2034
2035 return NULL;
2036}
2037
2038struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2039{
2040 struct server *byname;
2041 struct server *byid;
2042
2043 if (!name && !id)
2044 return NULL;
2045
2046 if (diff)
2047 *diff = 0;
2048
2049 byname = byid = NULL;
2050
2051 if (name) {
2052 byname = server_find_by_name(bk, name);
2053 if (byname && (!id || byname->puid == id))
2054 return byname;
2055 }
2056
2057 /* remaining possibilities :
2058 * - name not set
2059 * - name set but not found
2060 * - name found but ID doesn't match
2061 */
2062 if (id) {
2063 byid = server_find_by_id(bk, id);
2064 if (byid) {
2065 if (byname) {
2066 /* use id only if forced by configuration */
2067 if (byid->flags & SRV_F_FORCED_ID) {
2068 if (diff)
2069 *diff |= 2;
2070 return byid;
2071 }
2072 else {
2073 if (diff)
2074 *diff |= 1;
2075 return byname;
2076 }
2077 }
2078
2079 /* remaining possibilities:
2080 * - name not set
2081 * - name set but not found
2082 */
2083 if (name && diff)
2084 *diff |= 2;
2085 return byid;
2086 }
2087
2088 /* id bot found */
2089 if (byname) {
2090 if (diff)
2091 *diff |= 1;
2092 return byname;
2093 }
2094 }
2095
2096 return NULL;
2097}
2098
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002099/* Update a server state using the parameters available in the params list */
2100static void srv_update_state(struct server *srv, int version, char **params)
2101{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002102 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002103 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002104
2105 /* fields since version 1
2106 * and common to all other upcoming versions
2107 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002108 enum srv_state srv_op_state;
2109 enum srv_admin srv_admin_state;
2110 unsigned srv_uweight, srv_iweight;
2111 unsigned long srv_last_time_change;
2112 short srv_check_status;
2113 enum chk_result srv_check_result;
2114 int srv_check_health;
2115 int srv_check_state, srv_agent_state;
2116 int bk_f_forced_id;
2117 int srv_f_forced_id;
2118
Willy Tarreau31138fa2015-09-29 18:38:47 +02002119 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002120 switch (version) {
2121 case 1:
2122 /*
2123 * now we can proceed with server's state update:
2124 * srv_addr: params[0]
2125 * srv_op_state: params[1]
2126 * srv_admin_state: params[2]
2127 * srv_uweight: params[3]
2128 * srv_iweight: params[4]
2129 * srv_last_time_change: params[5]
2130 * srv_check_status: params[6]
2131 * srv_check_result: params[7]
2132 * srv_check_health: params[8]
2133 * srv_check_state: params[9]
2134 * srv_agent_state: params[10]
2135 * bk_f_forced_id: params[11]
2136 * srv_f_forced_id: params[12]
2137 */
2138
2139 /* validating srv_op_state */
2140 p = NULL;
2141 errno = 0;
2142 srv_op_state = strtol(params[1], &p, 10);
2143 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2144 (srv_op_state != SRV_ST_STOPPED &&
2145 srv_op_state != SRV_ST_STARTING &&
2146 srv_op_state != SRV_ST_RUNNING &&
2147 srv_op_state != SRV_ST_STOPPING)) {
2148 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2149 }
2150
2151 /* validating srv_admin_state */
2152 p = NULL;
2153 errno = 0;
2154 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002155
2156 /* inherited statuses will be recomputed later */
2157 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2158
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002159 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2160 (srv_admin_state != 0 &&
2161 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002162 srv_admin_state != SRV_ADMF_CMAINT &&
2163 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002164 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002165 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002166 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2167 }
2168
2169 /* validating srv_uweight */
2170 p = NULL;
2171 errno = 0;
2172 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002173 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002174 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2175
2176 /* validating srv_iweight */
2177 p = NULL;
2178 errno = 0;
2179 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002180 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002181 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2182
2183 /* validating srv_last_time_change */
2184 p = NULL;
2185 errno = 0;
2186 srv_last_time_change = strtol(params[5], &p, 10);
2187 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2188 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2189
2190 /* validating srv_check_status */
2191 p = NULL;
2192 errno = 0;
2193 srv_check_status = strtol(params[6], &p, 10);
2194 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2195 (srv_check_status >= HCHK_STATUS_SIZE))
2196 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2197
2198 /* validating srv_check_result */
2199 p = NULL;
2200 errno = 0;
2201 srv_check_result = strtol(params[7], &p, 10);
2202 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2203 (srv_check_result != CHK_RES_UNKNOWN &&
2204 srv_check_result != CHK_RES_NEUTRAL &&
2205 srv_check_result != CHK_RES_FAILED &&
2206 srv_check_result != CHK_RES_PASSED &&
2207 srv_check_result != CHK_RES_CONDPASS)) {
2208 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2209 }
2210
2211 /* validating srv_check_health */
2212 p = NULL;
2213 errno = 0;
2214 srv_check_health = strtol(params[8], &p, 10);
2215 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2216 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2217
2218 /* validating srv_check_state */
2219 p = NULL;
2220 errno = 0;
2221 srv_check_state = strtol(params[9], &p, 10);
2222 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2223 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2224 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2225
2226 /* validating srv_agent_state */
2227 p = NULL;
2228 errno = 0;
2229 srv_agent_state = strtol(params[10], &p, 10);
2230 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2231 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2232 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2233
2234 /* validating bk_f_forced_id */
2235 p = NULL;
2236 errno = 0;
2237 bk_f_forced_id = strtol(params[11], &p, 10);
2238 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2239 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2240
2241 /* validating srv_f_forced_id */
2242 p = NULL;
2243 errno = 0;
2244 srv_f_forced_id = strtol(params[12], &p, 10);
2245 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2246 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2247
2248
2249 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002250 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002251 goto out;
2252
2253 /* recover operational state and apply it to this server
2254 * and all servers tracking this one */
2255 switch (srv_op_state) {
2256 case SRV_ST_STOPPED:
2257 srv->check.health = 0;
2258 srv_set_stopped(srv, "changed from server-state after a reload");
2259 break;
2260 case SRV_ST_STARTING:
2261 srv->state = srv_op_state;
2262 break;
2263 case SRV_ST_STOPPING:
2264 srv->check.health = srv->check.rise + srv->check.fall - 1;
2265 srv_set_stopping(srv, "changed from server-state after a reload");
2266 break;
2267 case SRV_ST_RUNNING:
2268 srv->check.health = srv->check.rise + srv->check.fall - 1;
2269 srv_set_running(srv, "");
2270 break;
2271 }
2272
2273 /* When applying server state, the following rules apply:
2274 * - in case of a configuration change, we apply the setting from the new
2275 * configuration, regardless of old running state
2276 * - if no configuration change, we apply old running state only if old running
2277 * state is different from new configuration state
2278 */
2279 /* configuration has changed */
2280 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2281 if (srv->admin & SRV_ADMF_CMAINT)
2282 srv_adm_set_maint(srv);
2283 else
2284 srv_adm_set_ready(srv);
2285 }
2286 /* configuration is the same, let's compate old running state and new conf state */
2287 else {
2288 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2289 srv_adm_set_maint(srv);
2290 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2291 srv_adm_set_ready(srv);
2292 }
2293 /* apply drain mode if server is currently enabled */
2294 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2295 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002296 * (srv->iweight is the weight set up in configuration).
2297 * There are two possible reasons for FDRAIN to have been present :
2298 * - previous config weight was zero
2299 * - "set server b/s drain" was sent to the CLI
2300 *
2301 * In the first case, we simply want to drop this drain state
2302 * if the new weight is not zero anymore, meaning the administrator
2303 * has intentionally turned the weight back to a positive value to
2304 * enable the server again after an operation. In the second case,
2305 * the drain state was forced on the CLI regardless of the config's
2306 * weight so we don't want a change to the config weight to lose this
2307 * status. What this means is :
2308 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2309 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002310 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002311 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002312 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002313 }
2314
2315 srv->last_change = date.tv_sec - srv_last_time_change;
2316 srv->check.status = srv_check_status;
2317 srv->check.result = srv_check_result;
2318 srv->check.health = srv_check_health;
2319
2320 /* Only case we want to apply is removing ENABLED flag which could have been
2321 * done by the "disable health" command over the stats socket
2322 */
2323 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2324 (srv_check_state & CHK_ST_CONFIGURED) &&
2325 !(srv_check_state & CHK_ST_ENABLED))
2326 srv->check.state &= ~CHK_ST_ENABLED;
2327
2328 /* Only case we want to apply is removing ENABLED flag which could have been
2329 * done by the "disable agent" command over the stats socket
2330 */
2331 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2332 (srv_agent_state & CHK_ST_CONFIGURED) &&
2333 !(srv_agent_state & CHK_ST_ENABLED))
2334 srv->agent.state &= ~CHK_ST_ENABLED;
2335
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002336 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2337 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002338 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002339 * It means that a configuration file change has precedence over a unix socket change
2340 * for server's weight
2341 *
2342 * by default, HAProxy applies the following weight when parsing the configuration
2343 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002344 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002345 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002346 srv->uweight = srv_uweight;
2347 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002348 server_recalc_eweight(srv);
2349
Willy Tarreaue5a60682016-11-09 14:54:53 +01002350 /* load server IP address */
2351 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002352 break;
2353 default:
2354 chunk_appendf(msg, ", version '%d' not supported", version);
2355 }
2356
2357 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002358 if (msg->len) {
2359 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002360 Warning("server-state application failed for server '%s/%s'%s",
2361 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002362 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002363}
2364
2365/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2366 * For each proxy, it does the following:
2367 * - opens its server state file (either one or local one)
2368 * - read whole file, line by line
2369 * - analyse each line to check if it matches our current backend:
2370 * - backend name matches
2371 * - backend id matches if id is forced and name doesn't match
2372 * - if the server pointed by the line is found, then state is applied
2373 *
2374 * If the running backend uuid or id differs from the state file, then HAProxy reports
2375 * a warning.
2376 */
2377void apply_server_state(void)
2378{
2379 char *cur, *end;
2380 char mybuf[SRV_STATE_LINE_MAXLEN];
2381 int mybuflen;
2382 char *params[SRV_STATE_FILE_MAX_FIELDS];
2383 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2384 int arg, srv_arg, version, diff;
2385 FILE *f;
2386 char *filepath;
2387 char globalfilepath[MAXPATHLEN + 1];
2388 char localfilepath[MAXPATHLEN + 1];
2389 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2390 extern struct proxy *proxy;
2391 struct proxy *curproxy, *bk;
2392 struct server *srv;
2393
2394 globalfilepathlen = 0;
2395 /* create the globalfilepath variable */
2396 if (global.server_state_file) {
2397 /* absolute path or no base directory provided */
2398 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2399 len = strlen(global.server_state_file);
2400 if (len > MAXPATHLEN) {
2401 globalfilepathlen = 0;
2402 goto globalfileerror;
2403 }
2404 memcpy(globalfilepath, global.server_state_file, len);
2405 globalfilepath[len] = '\0';
2406 globalfilepathlen = len;
2407 }
2408 else if (global.server_state_base) {
2409 len = strlen(global.server_state_base);
2410 globalfilepathlen += len;
2411
2412 if (globalfilepathlen > MAXPATHLEN) {
2413 globalfilepathlen = 0;
2414 goto globalfileerror;
2415 }
2416 strncpy(globalfilepath, global.server_state_base, len);
2417 globalfilepath[globalfilepathlen] = 0;
2418
2419 /* append a slash if needed */
2420 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2421 if (globalfilepathlen + 1 > MAXPATHLEN) {
2422 globalfilepathlen = 0;
2423 goto globalfileerror;
2424 }
2425 globalfilepath[globalfilepathlen++] = '/';
2426 }
2427
2428 len = strlen(global.server_state_file);
2429 if (globalfilepathlen + len > MAXPATHLEN) {
2430 globalfilepathlen = 0;
2431 goto globalfileerror;
2432 }
2433 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2434 globalfilepathlen += len;
2435 globalfilepath[globalfilepathlen++] = 0;
2436 }
2437 }
2438 globalfileerror:
2439 if (globalfilepathlen == 0)
2440 globalfilepath[0] = '\0';
2441
2442 /* read servers state from local file */
2443 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2444 /* servers are only in backends */
2445 if (!(curproxy->cap & PR_CAP_BE))
2446 continue;
2447 fileopenerr = 0;
2448 filepath = NULL;
2449
2450 /* search server state file path and name */
2451 switch (curproxy->load_server_state_from_file) {
2452 /* read servers state from global file */
2453 case PR_SRV_STATE_FILE_GLOBAL:
2454 /* there was an error while generating global server state file path */
2455 if (globalfilepathlen == 0)
2456 continue;
2457 filepath = globalfilepath;
2458 fileopenerr = 1;
2459 break;
2460 /* this backend has its own file */
2461 case PR_SRV_STATE_FILE_LOCAL:
2462 localfilepathlen = 0;
2463 localfilepath[0] = '\0';
2464 len = 0;
2465 /* create the localfilepath variable */
2466 /* absolute path or no base directory provided */
2467 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2468 len = strlen(curproxy->server_state_file_name);
2469 if (len > MAXPATHLEN) {
2470 localfilepathlen = 0;
2471 goto localfileerror;
2472 }
2473 memcpy(localfilepath, curproxy->server_state_file_name, len);
2474 localfilepath[len] = '\0';
2475 localfilepathlen = len;
2476 }
2477 else if (global.server_state_base) {
2478 len = strlen(global.server_state_base);
2479 localfilepathlen += len;
2480
2481 if (localfilepathlen > MAXPATHLEN) {
2482 localfilepathlen = 0;
2483 goto localfileerror;
2484 }
2485 strncpy(localfilepath, global.server_state_base, len);
2486 localfilepath[localfilepathlen] = 0;
2487
2488 /* append a slash if needed */
2489 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2490 if (localfilepathlen + 1 > MAXPATHLEN) {
2491 localfilepathlen = 0;
2492 goto localfileerror;
2493 }
2494 localfilepath[localfilepathlen++] = '/';
2495 }
2496
2497 len = strlen(curproxy->server_state_file_name);
2498 if (localfilepathlen + len > MAXPATHLEN) {
2499 localfilepathlen = 0;
2500 goto localfileerror;
2501 }
2502 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2503 localfilepathlen += len;
2504 localfilepath[localfilepathlen++] = 0;
2505 }
2506 filepath = localfilepath;
2507 localfileerror:
2508 if (localfilepathlen == 0)
2509 localfilepath[0] = '\0';
2510
2511 break;
2512 case PR_SRV_STATE_FILE_NONE:
2513 default:
2514 continue;
2515 }
2516
2517 /* preload global state file */
2518 errno = 0;
2519 f = fopen(filepath, "r");
2520 if (errno && fileopenerr)
2521 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2522 if (!f)
2523 continue;
2524
2525 mybuf[0] = '\0';
2526 mybuflen = 0;
2527 version = 0;
2528
2529 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002530 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2531 Warning("Can't read first line of the server state file '%s'\n", filepath);
2532 goto fileclose;
2533 }
2534
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002535 cur = mybuf;
2536 version = atoi(cur);
2537 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2538 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002539 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002540
2541 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2542 int bk_f_forced_id = 0;
2543 int check_id = 0;
2544 int check_name = 0;
2545
2546 mybuflen = strlen(mybuf);
2547 cur = mybuf;
2548 end = cur + mybuflen;
2549
2550 bk = NULL;
2551 srv = NULL;
2552
2553 /* we need at least one character */
2554 if (mybuflen == 0)
2555 continue;
2556
2557 /* ignore blank characters at the beginning of the line */
2558 while (isspace(*cur))
2559 ++cur;
2560
2561 if (cur == end)
2562 continue;
2563
2564 /* ignore comment line */
2565 if (*cur == '#')
2566 continue;
2567
2568 /* we're now ready to move the line into *srv_params[] */
2569 params[0] = cur;
2570 arg = 1;
2571 srv_arg = 0;
2572 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2573 if (isspace(*cur)) {
2574 *cur = '\0';
2575 ++cur;
2576 while (isspace(*cur))
2577 ++cur;
2578 switch (version) {
2579 case 1:
2580 /*
2581 * srv_addr: params[4] => srv_params[0]
2582 * srv_op_state: params[5] => srv_params[1]
2583 * srv_admin_state: params[6] => srv_params[2]
2584 * srv_uweight: params[7] => srv_params[3]
2585 * srv_iweight: params[8] => srv_params[4]
2586 * srv_last_time_change: params[9] => srv_params[5]
2587 * srv_check_status: params[10] => srv_params[6]
2588 * srv_check_result: params[11] => srv_params[7]
2589 * srv_check_health: params[12] => srv_params[8]
2590 * srv_check_state: params[13] => srv_params[9]
2591 * srv_agent_state: params[14] => srv_params[10]
2592 * bk_f_forced_id: params[15] => srv_params[11]
2593 * srv_f_forced_id: params[16] => srv_params[12]
2594 */
2595 if (arg >= 4) {
2596 srv_params[srv_arg] = cur;
2597 ++srv_arg;
2598 }
2599 break;
2600 }
2601
2602 params[arg] = cur;
2603 ++arg;
2604 }
2605 else {
2606 ++cur;
2607 }
2608 }
2609
2610 /* if line is incomplete line, then ignore it.
2611 * otherwise, update useful flags */
2612 switch (version) {
2613 case 1:
2614 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2615 continue;
2616 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2617 check_id = (atoi(params[0]) == curproxy->uuid);
2618 check_name = (strcmp(curproxy->id, params[1]) == 0);
2619 break;
2620 }
2621
2622 diff = 0;
2623 bk = curproxy;
2624
2625 /* if backend can't be found, let's continue */
2626 if (!check_id && !check_name)
2627 continue;
2628 else if (!check_id && check_name) {
2629 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2630 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2631 }
2632 else if (check_id && !check_name) {
2633 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2634 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2635 /* if name doesn't match, we still want to update curproxy if the backend id
2636 * was forced in previous the previous configuration */
2637 if (!bk_f_forced_id)
2638 continue;
2639 }
2640
2641 /* look for the server by its id: param[2] */
2642 /* else look for the server by its name: param[3] */
2643 diff = 0;
2644 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2645
2646 if (!srv) {
2647 /* if no server found, then warning and continue with next line */
2648 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2649 params[3], params[2], params[0], params[1]);
2650 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2651 params[3], params[2], params[0], params[1]);
2652 continue;
2653 }
2654 else if (diff & PR_FBM_MISMATCH_ID) {
2655 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);
2656 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);
2657 }
2658 else if (diff & PR_FBM_MISMATCH_NAME) {
2659 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);
2660 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);
2661 }
2662
2663 /* now we can proceed with server's state update */
2664 srv_update_state(srv, version, srv_params);
2665 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002666fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002667 fclose(f);
2668 }
2669}
2670
Simon Horman7d09b9a2013-02-12 10:45:51 +09002671/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002672 * update a server's current IP address.
2673 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2674 * ip is in network format.
2675 * updater is a string which contains an information about the requester of the update.
2676 * updater is used if not NULL.
2677 *
2678 * A log line and a stderr warning message is generated based on server's backend options.
2679 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002680int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002681{
2682 /* generates a log line and a warning on stderr */
2683 if (1) {
2684 /* book enough space for both IPv4 and IPv6 */
2685 char oldip[INET6_ADDRSTRLEN];
2686 char newip[INET6_ADDRSTRLEN];
2687
2688 memset(oldip, '\0', INET6_ADDRSTRLEN);
2689 memset(newip, '\0', INET6_ADDRSTRLEN);
2690
2691 /* copy old IP address in a string */
2692 switch (s->addr.ss_family) {
2693 case AF_INET:
2694 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2695 break;
2696 case AF_INET6:
2697 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2698 break;
2699 };
2700
2701 /* copy new IP address in a string */
2702 switch (ip_sin_family) {
2703 case AF_INET:
2704 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2705 break;
2706 case AF_INET6:
2707 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2708 break;
2709 };
2710
2711 /* save log line into a buffer */
2712 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2713 s->proxy->id, s->id, oldip, newip, updater);
2714
2715 /* write the buffer on stderr */
2716 Warning("%s.\n", trash.str);
2717
2718 /* send a log */
2719 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2720 }
2721
2722 /* save the new IP family */
2723 s->addr.ss_family = ip_sin_family;
2724 /* save the new IP address */
2725 switch (ip_sin_family) {
2726 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002727 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002728 break;
2729 case AF_INET6:
2730 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2731 break;
2732 };
2733
2734 return 0;
2735}
2736
2737/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002738 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2739 *
2740 * Caller can pass its name through <updater> to get it integrated in the response message
2741 * returned by the function.
2742 *
2743 * The function first does the following, in that order:
2744 * - validates the new addr and/or port
2745 * - checks if an update is required (new IP or port is different than current ones)
2746 * - checks the update is allowed:
2747 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2748 * - allow all changes if no CHECKS are configured
2749 * - if CHECK is configured:
2750 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2751 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2752 * conditions are met
2753 */
2754const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2755{
2756 struct sockaddr_storage sa;
2757 int ret, port_change_required;
2758 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00002759 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002760 struct chunk *msg;
2761
2762 msg = get_trash_chunk();
2763 chunk_reset(msg);
2764
2765 if (addr) {
2766 memset(&sa, 0, sizeof(struct sockaddr_storage));
2767 if (str2ip2(addr, &sa, 0) == NULL) {
2768 chunk_printf(msg, "Invalid addr '%s'", addr);
2769 goto out;
2770 }
2771
2772 /* changes are allowed on AF_INET* families only */
2773 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2774 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2775 goto out;
2776 }
2777
2778 /* collecting data currently setup */
2779 memset(current_addr, '\0', sizeof(current_addr));
2780 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2781 /* changes are allowed on AF_INET* families only */
2782 if ((ret != AF_INET) && (ret != AF_INET6)) {
2783 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2784 goto out;
2785 }
2786
2787 /* applying ADDR changes if required and allowed
2788 * ipcmp returns 0 when both ADDR are the same
2789 */
2790 if (ipcmp(&s->addr, &sa) == 0) {
2791 chunk_appendf(msg, "no need to change the addr");
2792 goto port;
2793 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002794 ipcpy(&sa, &s->addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002795
2796 /* we also need to update check's ADDR only if it uses the server's one */
2797 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002798 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002799 }
2800
2801 /* we also need to update agent ADDR only if it use the server's one */
2802 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002803 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002804 }
2805
2806 /* update report for caller */
2807 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2808 }
2809
2810 port:
2811 if (port) {
2812 char sign = '\0';
2813 char *endptr;
2814
2815 if (addr)
2816 chunk_appendf(msg, ", ");
2817
2818 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01002819 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002820
2821 /* check if PORT change is required */
2822 port_change_required = 0;
2823
2824 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04002825 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002826 new_port = strtol(port, &endptr, 10);
2827 if ((errno != 0) || (port == endptr)) {
2828 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2829 goto out;
2830 }
2831
2832 /* check if caller triggers a port mapped or offset */
2833 if (sign == '-' || (sign == '+')) {
2834 /* check if server currently uses port map */
2835 if (!(s->flags & SRV_F_MAPPORTS)) {
2836 /* switch from fixed port to port map mandatorily triggers
2837 * a port change */
2838 port_change_required = 1;
2839 /* check is configured
2840 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2841 * prevent PORT change if check doesn't have it's dedicated port while switching
2842 * to port mapping */
2843 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2844 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.");
2845 goto out;
2846 }
2847 }
2848 /* we're already using port maps */
2849 else {
2850 port_change_required = current_port != new_port;
2851 }
2852 }
2853 /* fixed port */
2854 else {
2855 port_change_required = current_port != new_port;
2856 }
2857
2858 /* applying PORT changes if required and update response message */
2859 if (port_change_required) {
2860 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01002861 s->svc_port = new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002862
2863 /* prepare message */
2864 chunk_appendf(msg, "port changed from '");
2865 if (s->flags & SRV_F_MAPPORTS)
2866 chunk_appendf(msg, "+");
2867 chunk_appendf(msg, "%d' to '", current_port);
2868
2869 if (sign == '-') {
2870 s->flags |= SRV_F_MAPPORTS;
2871 chunk_appendf(msg, "%c", sign);
2872 /* just use for result output */
2873 new_port = -new_port;
2874 }
2875 else if (sign == '+') {
2876 s->flags |= SRV_F_MAPPORTS;
2877 chunk_appendf(msg, "%c", sign);
2878 }
2879 else {
2880 s->flags &= ~SRV_F_MAPPORTS;
2881 }
2882
2883 chunk_appendf(msg, "%d'", new_port);
2884
2885 /* we also need to update health checks port only if it uses server's realport */
2886 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2887 s->check.port = new_port;
2888 }
2889 }
2890 else {
2891 chunk_appendf(msg, "no need to change the port");
2892 }
2893 }
2894
2895out:
2896 if (updater)
2897 chunk_appendf(msg, " by '%s'", updater);
2898 chunk_appendf(msg, "\n");
2899 return msg->str;
2900}
2901
2902
2903/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002904 * update server status based on result of name resolution
2905 * returns:
2906 * 0 if server status is updated
2907 * 1 if server status has not changed
2908 */
2909int snr_update_srv_status(struct server *s)
2910{
2911 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002912 struct dns_resolvers *resolvers;
2913
2914 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002915
2916 switch (resolution->status) {
2917 case RSLV_STATUS_NONE:
2918 /* status when HAProxy has just (re)started */
2919 trigger_resolution(s);
2920 break;
2921
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002922 case RSLV_STATUS_VALID:
2923 /*
2924 * resume health checks
2925 * server will be turned back on if health check is safe
2926 */
2927 if (!(s->admin & SRV_ADMF_RMAINT))
2928 return 1;
2929 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2930 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
2931 s->proxy->id, s->id);
2932
2933 Warning("%s.\n", trash.str);
2934 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2935 return 0;
2936
2937 case RSLV_STATUS_NX:
2938 /* stop server if resolution is NX for a long enough period */
2939 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
2940 if (s->admin & SRV_ADMF_RMAINT)
2941 return 1;
2942 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
2943 return 0;
2944 }
2945 break;
2946
2947 case RSLV_STATUS_TIMEOUT:
2948 /* stop server if resolution is TIMEOUT for a long enough period */
2949 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
2950 if (s->admin & SRV_ADMF_RMAINT)
2951 return 1;
2952 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
2953 return 0;
2954 }
2955 break;
2956
2957 case RSLV_STATUS_REFUSED:
2958 /* stop server if resolution is REFUSED for a long enough period */
2959 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
2960 if (s->admin & SRV_ADMF_RMAINT)
2961 return 1;
2962 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
2963 return 0;
2964 }
2965 break;
2966
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002967 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002968 /* stop server if resolution is in unmatched error for a long enough period */
2969 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
2970 if (s->admin & SRV_ADMF_RMAINT)
2971 return 1;
2972 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
2973 return 0;
2974 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002975 break;
2976 }
2977
2978 return 1;
2979}
2980
2981/*
2982 * Server Name Resolution valid response callback
2983 * It expects:
2984 * - <nameserver>: the name server which answered the valid response
2985 * - <response>: buffer containing a valid DNS response
2986 * - <response_len>: size of <response>
2987 * It performs the following actions:
2988 * - ignore response if current ip found and server family not met
2989 * - update with first new ip found if family is met and current IP is not found
2990 * returns:
2991 * 0 on error
2992 * 1 when no error or safe ignore
2993 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002994int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002995{
2996 struct server *s;
2997 void *serverip, *firstip;
2998 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002999 int ret;
3000 struct chunk *chk = get_trash_chunk();
3001
3002 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003003 firstip = NULL; /* pointer to the first valid response found */
3004 /* it will be used as the new IP if a change is required */
3005 firstip_sin_family = AF_UNSPEC;
3006 serverip = NULL; /* current server IP address */
3007
3008 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003009 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003010
3011 /* initializing server IP pointer */
3012 server_sin_family = s->addr.ss_family;
3013 switch (server_sin_family) {
3014 case AF_INET:
3015 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3016 break;
3017
3018 case AF_INET6:
3019 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3020 break;
3021
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003022 case AF_UNSPEC:
3023 break;
3024
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003025 default:
3026 goto invalid;
3027 }
3028
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003029 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003030 serverip, server_sin_family, &firstip,
3031 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003032
3033 switch (ret) {
3034 case DNS_UPD_NO:
3035 if (resolution->status != RSLV_STATUS_VALID) {
3036 resolution->status = RSLV_STATUS_VALID;
3037 resolution->last_status_change = now_ms;
3038 }
3039 goto stop_resolution;
3040
3041 case DNS_UPD_SRVIP_NOT_FOUND:
3042 goto save_ip;
3043
3044 case DNS_UPD_CNAME:
3045 if (resolution->status != RSLV_STATUS_VALID) {
3046 resolution->status = RSLV_STATUS_VALID;
3047 resolution->last_status_change = now_ms;
3048 }
3049 goto invalid;
3050
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003051 case DNS_UPD_NO_IP_FOUND:
3052 if (resolution->status != RSLV_STATUS_OTHER) {
3053 resolution->status = RSLV_STATUS_OTHER;
3054 resolution->last_status_change = now_ms;
3055 }
3056 goto stop_resolution;
3057
Baptiste Assmannfad03182015-10-28 02:03:32 +01003058 case DNS_UPD_NAME_ERROR:
3059 /* if this is not the last expected response, we ignore it */
3060 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3061 return 0;
3062 /* update resolution status to OTHER error type */
3063 if (resolution->status != RSLV_STATUS_OTHER) {
3064 resolution->status = RSLV_STATUS_OTHER;
3065 resolution->last_status_change = now_ms;
3066 }
3067 goto stop_resolution;
3068
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003069 default:
3070 goto invalid;
3071
3072 }
3073
3074 save_ip:
3075 nameserver->counters.update += 1;
3076 if (resolution->status != RSLV_STATUS_VALID) {
3077 resolution->status = RSLV_STATUS_VALID;
3078 resolution->last_status_change = now_ms;
3079 }
3080
3081 /* save the first ip we found */
3082 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3083 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3084
3085 stop_resolution:
3086 /* update last resolution date and time */
3087 resolution->last_resolution = now_ms;
3088 /* reset current status flag */
3089 resolution->step = RSLV_STEP_NONE;
3090 /* reset values */
3091 dns_reset_resolution(resolution);
3092
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003093 dns_update_resolvers_timeout(nameserver->resolvers);
3094
3095 snr_update_srv_status(s);
3096 return 0;
3097
3098 invalid:
3099 nameserver->counters.invalid += 1;
3100 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3101 goto stop_resolution;
3102
3103 snr_update_srv_status(s);
3104 return 0;
3105}
3106
3107/*
3108 * Server Name Resolution error management callback
3109 * returns:
3110 * 0 on error
3111 * 1 when no error or safe ignore
3112 */
3113int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3114{
3115 struct server *s;
3116 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003117 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003118
3119 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003120 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003121 resolvers = resolution->resolvers;
3122
3123 /* can be ignored if this is not the last response */
3124 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3125 return 1;
3126 }
3127
3128 switch (error_code) {
3129 case DNS_RESP_INVALID:
3130 case DNS_RESP_WRONG_NAME:
3131 if (resolution->status != RSLV_STATUS_INVALID) {
3132 resolution->status = RSLV_STATUS_INVALID;
3133 resolution->last_status_change = now_ms;
3134 }
3135 break;
3136
3137 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003138 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003139 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003140 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003141 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003142 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3143 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003144
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003145 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003146 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003147 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003148 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003149 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003150 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003151 }
3152 else if (res_preferred_afinet) {
3153 /* fallback from A to AAAA */
3154 resolution->query_type = DNS_RTYPE_AAAA;
3155 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003156 else {
3157 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003158 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003159 resolution->query_type = DNS_RTYPE_A;
3160 } else {
3161 resolution->query_type = DNS_RTYPE_AAAA;
3162 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003163 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003164
3165 dns_send_query(resolution);
3166
3167 /*
3168 * move the resolution to the last element of the FIFO queue
3169 * and update timeout wakeup based on the new first entry
3170 */
3171 if (dns_check_resolution_queue(resolvers) > 1) {
3172 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003173 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003174 /* ex first resolution goes to the end of the queue */
3175 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3176 }
3177 dns_update_resolvers_timeout(resolvers);
3178 goto leave;
3179 }
3180 else {
3181 if (resolution->status != RSLV_STATUS_OTHER) {
3182 resolution->status = RSLV_STATUS_OTHER;
3183 resolution->last_status_change = now_ms;
3184 }
3185 }
3186 break;
3187
3188 case DNS_RESP_NX_DOMAIN:
3189 if (resolution->status != RSLV_STATUS_NX) {
3190 resolution->status = RSLV_STATUS_NX;
3191 resolution->last_status_change = now_ms;
3192 }
3193 break;
3194
3195 case DNS_RESP_REFUSED:
3196 if (resolution->status != RSLV_STATUS_REFUSED) {
3197 resolution->status = RSLV_STATUS_REFUSED;
3198 resolution->last_status_change = now_ms;
3199 }
3200 break;
3201
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003202 case DNS_RESP_TIMEOUT:
3203 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3204 resolution->status = RSLV_STATUS_TIMEOUT;
3205 resolution->last_status_change = now_ms;
3206 }
3207 break;
3208 }
3209
3210 /* update last resolution date and time */
3211 resolution->last_resolution = now_ms;
3212 /* reset current status flag */
3213 resolution->step = RSLV_STEP_NONE;
3214 /* reset values */
3215 dns_reset_resolution(resolution);
3216
3217 LIST_DEL(&resolution->list);
3218 dns_update_resolvers_timeout(resolvers);
3219
3220 leave:
3221 snr_update_srv_status(s);
3222 return 1;
3223}
3224
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003225/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3226 * resolver. This is suited for initial address configuration. Returns 0 on
3227 * success otherwise a non-zero error code. In case of error, *err_code, if
3228 * not NULL, is filled up.
3229 */
3230int srv_set_addr_via_libc(struct server *srv, int *err_code)
3231{
3232 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003233 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003234 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003235 return 1;
3236 }
3237 return 0;
3238}
3239
3240/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3241 * from the state file. This is suited for initial address configuration.
3242 * Returns 0 on success otherwise a non-zero error code. In case of error,
3243 * *err_code, if not NULL, is filled up.
3244 */
3245static int srv_apply_lastaddr(struct server *srv, int *err_code)
3246{
3247 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3248 if (err_code)
3249 *err_code |= ERR_WARN;
3250 return 1;
3251 }
3252 return 0;
3253}
3254
Willy Tarreau25e51522016-11-04 15:10:17 +01003255/* returns 0 if no error, otherwise a combination of ERR_* flags */
3256static int srv_iterate_initaddr(struct server *srv)
3257{
3258 int return_code = 0;
3259 int err_code;
3260 unsigned int methods;
3261
3262 methods = srv->init_addr_methods;
3263 if (!methods) { // default to "last,libc"
3264 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3265 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3266 }
3267
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003268 /* "-dr" : always append "none" so that server addresses resolution
3269 * failures are silently ignored, this is convenient to validate some
3270 * configs out of their environment.
3271 */
3272 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3273 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3274
Willy Tarreau25e51522016-11-04 15:10:17 +01003275 while (methods) {
3276 err_code = 0;
3277 switch (srv_get_next_initaddr(&methods)) {
3278 case SRV_IADDR_LAST:
3279 if (!srv->lastaddr)
3280 continue;
3281 if (srv_apply_lastaddr(srv, &err_code) == 0)
3282 return return_code;
3283 return_code |= err_code;
3284 break;
3285
3286 case SRV_IADDR_LIBC:
3287 if (!srv->hostname)
3288 continue;
3289 if (srv_set_addr_via_libc(srv, &err_code) == 0)
3290 return return_code;
3291 return_code |= err_code;
3292 break;
3293
Willy Tarreau37ebe122016-11-04 15:17:58 +01003294 case SRV_IADDR_NONE:
3295 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003296 if (return_code) {
3297 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3298 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3299 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003300 return return_code;
3301
Willy Tarreau4310d362016-11-02 15:05:56 +01003302 case SRV_IADDR_IP:
3303 ipcpy(&srv->init_addr, &srv->addr);
3304 if (return_code) {
3305 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3306 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3307 }
3308 return return_code;
3309
Willy Tarreau25e51522016-11-04 15:10:17 +01003310 default: /* unhandled method */
3311 break;
3312 }
3313 }
3314
3315 if (!return_code) {
3316 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3317 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3318 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003319 else {
3320 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3321 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3322 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003323
3324 return_code |= ERR_ALERT | ERR_FATAL;
3325 return return_code;
3326}
3327
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003328/*
3329 * This function parses all backends and all servers within each backend
3330 * and performs servers' addr resolution based on information provided by:
3331 * - configuration file
3332 * - server-state file (states provided by an 'old' haproxy process)
3333 *
3334 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3335 */
3336int srv_init_addr(void)
3337{
3338 struct proxy *curproxy;
3339 int return_code = 0;
3340
3341 curproxy = proxy;
3342 while (curproxy) {
3343 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003344
3345 /* servers are in backend only */
3346 if (!(curproxy->cap & PR_CAP_BE))
3347 goto srv_init_addr_next;
3348
Willy Tarreau25e51522016-11-04 15:10:17 +01003349 for (srv = curproxy->srv; srv; srv = srv->next)
3350 if (srv->hostname)
3351 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003352
3353 srv_init_addr_next:
3354 curproxy = curproxy->next;
3355 }
3356
3357 return return_code;
3358}
3359
Willy Tarreau21b069d2016-11-23 17:15:08 +01003360/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3361 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003362 * 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 +01003363 * used for CLI commands requiring a server name.
3364 * Important: the <arg> is modified to remove the '/'.
3365 */
3366struct server *cli_find_server(struct appctx *appctx, char *arg)
3367{
3368 struct proxy *px;
3369 struct server *sv;
3370 char *line;
3371
3372 /* split "backend/server" and make <line> point to server */
3373 for (line = arg; *line; line++)
3374 if (*line == '/') {
3375 *line++ = '\0';
3376 break;
3377 }
3378
3379 if (!*line || !*arg) {
3380 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003381 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003382 return NULL;
3383 }
3384
3385 if (!get_backend_server(arg, line, &px, &sv)) {
3386 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003387 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003388 return NULL;
3389 }
3390
3391 if (px->state == PR_STSTOPPED) {
3392 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003393 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003394 return NULL;
3395 }
3396
3397 return sv;
3398}
3399
William Lallemand222baf22016-11-19 02:00:33 +01003400
3401static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3402{
3403 struct server *sv;
3404 const char *warning;
3405
3406 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3407 return 1;
3408
3409 sv = cli_find_server(appctx, args[2]);
3410 if (!sv)
3411 return 1;
3412
3413 if (strcmp(args[3], "weight") == 0) {
3414 warning = server_parse_weight_change_request(sv, args[4]);
3415 if (warning) {
3416 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003417 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003418 }
3419 }
3420 else if (strcmp(args[3], "state") == 0) {
3421 if (strcmp(args[4], "ready") == 0)
3422 srv_adm_set_ready(sv);
3423 else if (strcmp(args[4], "drain") == 0)
3424 srv_adm_set_drain(sv);
3425 else if (strcmp(args[4], "maint") == 0)
3426 srv_adm_set_maint(sv);
3427 else {
3428 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003429 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003430 }
3431 }
3432 else if (strcmp(args[3], "health") == 0) {
3433 if (sv->track) {
3434 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003435 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003436 }
3437 else if (strcmp(args[4], "up") == 0) {
3438 sv->check.health = sv->check.rise + sv->check.fall - 1;
3439 srv_set_running(sv, "changed from CLI");
3440 }
3441 else if (strcmp(args[4], "stopping") == 0) {
3442 sv->check.health = sv->check.rise + sv->check.fall - 1;
3443 srv_set_stopping(sv, "changed from CLI");
3444 }
3445 else if (strcmp(args[4], "down") == 0) {
3446 sv->check.health = 0;
3447 srv_set_stopped(sv, "changed from CLI");
3448 }
3449 else {
3450 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003451 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003452 }
3453 }
3454 else if (strcmp(args[3], "agent") == 0) {
3455 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3456 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003457 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003458 }
3459 else if (strcmp(args[4], "up") == 0) {
3460 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3461 srv_set_running(sv, "changed from CLI");
3462 }
3463 else if (strcmp(args[4], "down") == 0) {
3464 sv->agent.health = 0;
3465 srv_set_stopped(sv, "changed from CLI");
3466 }
3467 else {
3468 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003469 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003470 }
3471 }
Misiek2da082d2017-01-09 09:40:42 +01003472 else if (strcmp(args[3], "agent-addr") == 0) {
3473 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3474 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3475 appctx->st0 = CLI_ST_PRINT;
3476 } else {
3477 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3478 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3479 appctx->st0 = CLI_ST_PRINT;
3480 }
3481 }
3482 }
3483 else if (strcmp(args[3], "agent-send") == 0) {
3484 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3485 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3486 appctx->st0 = CLI_ST_PRINT;
3487 } else {
3488 char *nss = strdup(args[4]);
3489 if (!nss) {
3490 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3491 appctx->st0 = CLI_ST_PRINT;
3492 } else {
3493 free(sv->agent.send_string);
3494 sv->agent.send_string = nss;
3495 sv->agent.send_string_len = strlen(args[4]);
3496 }
3497 }
3498 }
William Lallemand222baf22016-11-19 02:00:33 +01003499 else if (strcmp(args[3], "check-port") == 0) {
3500 int i = 0;
3501 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3502 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003503 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003504 }
3505 if ((i < 0) || (i > 65535)) {
3506 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003507 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003508 }
3509 /* prevent the update of port to 0 if MAPPORTS are in use */
3510 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3511 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003512 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003513 return 1;
3514 }
3515 sv->check.port = i;
3516 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003517 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003518 }
3519 else if (strcmp(args[3], "addr") == 0) {
3520 char *addr = NULL;
3521 char *port = NULL;
3522 if (strlen(args[4]) == 0) {
3523 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003524 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003525 return 1;
3526 }
3527 else {
3528 addr = args[4];
3529 }
3530 if (strcmp(args[5], "port") == 0) {
3531 port = args[6];
3532 }
3533 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3534 if (warning) {
3535 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003536 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003537 }
3538 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3539 }
3540 else {
3541 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 +01003542 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003543 }
3544 return 1;
3545}
3546
William Lallemand6b160942016-11-22 12:34:35 +01003547static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3548{
3549 struct stream_interface *si = appctx->owner;
3550 struct proxy *px;
3551 struct server *sv;
3552 char *line;
3553
3554
3555 /* split "backend/server" and make <line> point to server */
3556 for (line = args[2]; *line; line++)
3557 if (*line == '/') {
3558 *line++ = '\0';
3559 break;
3560 }
3561
3562 if (!*line) {
3563 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003564 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003565 return 1;
3566 }
3567
3568 if (!get_backend_server(args[2], line, &px, &sv)) {
3569 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003570 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003571 return 1;
3572 }
3573
3574 /* return server's effective weight at the moment */
3575 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003576 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01003577 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003578 return 0;
3579 }
William Lallemand6b160942016-11-22 12:34:35 +01003580 return 1;
3581}
3582
3583static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
3584{
3585 struct server *sv;
3586 const char *warning;
3587
3588 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3589 return 1;
3590
3591 sv = cli_find_server(appctx, args[2]);
3592 if (!sv)
3593 return 1;
3594
3595 warning = server_parse_weight_change_request(sv, args[3]);
3596 if (warning) {
3597 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003598 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003599 }
3600 return 1;
3601}
3602
Willy Tarreaub8026272016-11-23 11:26:56 +01003603/* parse a "set maxconn server" command. It always returns 1. */
3604static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
3605{
3606 struct server *sv;
3607 const char *warning;
3608
3609 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3610 return 1;
3611
3612 sv = cli_find_server(appctx, args[3]);
3613 if (!sv)
3614 return 1;
3615
3616 warning = server_parse_maxconn_change_request(sv, args[4]);
3617 if (warning) {
3618 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003619 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01003620 }
3621 return 1;
3622}
William Lallemand6b160942016-11-22 12:34:35 +01003623
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003624/* parse a "disable agent" command. It always returns 1. */
3625static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
3626{
3627 struct server *sv;
3628
3629 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3630 return 1;
3631
3632 sv = cli_find_server(appctx, args[2]);
3633 if (!sv)
3634 return 1;
3635
3636 sv->agent.state &= ~CHK_ST_ENABLED;
3637 return 1;
3638}
3639
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003640/* parse a "disable health" command. It always returns 1. */
3641static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
3642{
3643 struct server *sv;
3644
3645 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3646 return 1;
3647
3648 sv = cli_find_server(appctx, args[2]);
3649 if (!sv)
3650 return 1;
3651
3652 sv->check.state &= ~CHK_ST_ENABLED;
3653 return 1;
3654}
3655
Willy Tarreauffb4d582016-11-24 12:47:00 +01003656/* parse a "disable server" command. It always returns 1. */
3657static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
3658{
3659 struct server *sv;
3660
3661 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3662 return 1;
3663
3664 sv = cli_find_server(appctx, args[2]);
3665 if (!sv)
3666 return 1;
3667
3668 srv_adm_set_maint(sv);
3669 return 1;
3670}
3671
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003672/* parse a "enable agent" command. It always returns 1. */
3673static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
3674{
3675 struct server *sv;
3676
3677 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3678 return 1;
3679
3680 sv = cli_find_server(appctx, args[2]);
3681 if (!sv)
3682 return 1;
3683
3684 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
3685 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003686 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003687 return 1;
3688 }
3689
3690 sv->agent.state |= CHK_ST_ENABLED;
3691 return 1;
3692}
3693
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003694/* parse a "enable health" command. It always returns 1. */
3695static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
3696{
3697 struct server *sv;
3698
3699 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3700 return 1;
3701
3702 sv = cli_find_server(appctx, args[2]);
3703 if (!sv)
3704 return 1;
3705
3706 sv->check.state |= CHK_ST_ENABLED;
3707 return 1;
3708}
3709
Willy Tarreauffb4d582016-11-24 12:47:00 +01003710/* parse a "enable server" command. It always returns 1. */
3711static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
3712{
3713 struct server *sv;
3714
3715 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3716 return 1;
3717
3718 sv = cli_find_server(appctx, args[2]);
3719 if (!sv)
3720 return 1;
3721
3722 srv_adm_set_ready(sv);
3723 return 1;
3724}
3725
William Lallemand222baf22016-11-19 02:00:33 +01003726/* register cli keywords */
3727static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003728 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003729 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003730 { { "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 +01003731 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003732 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003733 { { "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 +01003734 { { "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 +01003735 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01003736 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
3737 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
3738
William Lallemand222baf22016-11-19 02:00:33 +01003739 {{},}
3740}};
3741
3742__attribute__((constructor))
3743static void __server_init(void)
3744{
3745 cli_register_kw(&cli_kws);
3746}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003747
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003748/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003749 * Local variables:
3750 * c-indent-level: 8
3751 * c-basic-offset: 8
3752 * End:
3753 */