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