blob: b4d4f5079e0b3094bb9c1eacef6496df3fcaa007 [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
Willy Tarreau272adea2014-03-31 10:39:59 +020023#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020024#include <types/dns.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020025
Simon Hormanb1900d52015-01-30 11:22:54 +090026#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <proto/port_range.h>
28#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020029#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020030#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020031#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020032#include <proto/stream.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020033#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020034#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020035
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020036static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010037static int srv_apply_lastaddr(struct server *srv, int *err_code);
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreau21faa912012-10-10 08:27:36 +020039/* List head of all known server keywords */
40static struct srv_kw_list srv_keywords = {
41 .list = LIST_HEAD_INIT(srv_keywords.list)
42};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020043
Simon Hormana3608442013-11-01 16:46:15 +090044int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020045{
Willy Tarreau892337c2014-05-13 23:41:20 +020046 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020047 return s->down_time;
48
49 return now.tv_sec - s->last_change + s->down_time;
50}
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050052int srv_lastsession(const struct server *s)
53{
54 if (s->counters.last_sess)
55 return now.tv_sec - s->counters.last_sess;
56
57 return -1;
58}
59
Simon Horman4a741432013-02-23 15:35:38 +090060int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020061{
Simon Horman4a741432013-02-23 15:35:38 +090062 const struct server *s = check->server;
63
Willy Tarreauff5ae352013-12-11 20:36:34 +010064 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090065 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010066
Willy Tarreau892337c2014-05-13 23:41:20 +020067 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090068 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010069
Simon Horman4a741432013-02-23 15:35:38 +090070 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010071}
72
Willy Tarreau21faa912012-10-10 08:27:36 +020073/*
74 * Registers the server keyword list <kwl> as a list of valid keywords for next
75 * parsing sessions.
76 */
77void srv_register_keywords(struct srv_kw_list *kwl)
78{
79 LIST_ADDQ(&srv_keywords.list, &kwl->list);
80}
81
82/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
83 * keyword is found with a NULL ->parse() function, then an attempt is made to
84 * find one with a valid ->parse() function. This way it is possible to declare
85 * platform-dependant, known keywords as NULL, then only declare them as valid
86 * if some options are met. Note that if the requested keyword contains an
87 * opening parenthesis, everything from this point is ignored.
88 */
89struct srv_kw *srv_find_kw(const char *kw)
90{
91 int index;
92 const char *kwend;
93 struct srv_kw_list *kwl;
94 struct srv_kw *ret = NULL;
95
96 kwend = strchr(kw, '(');
97 if (!kwend)
98 kwend = kw + strlen(kw);
99
100 list_for_each_entry(kwl, &srv_keywords.list, list) {
101 for (index = 0; kwl->kw[index].kw != NULL; index++) {
102 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
103 kwl->kw[index].kw[kwend-kw] == 0) {
104 if (kwl->kw[index].parse)
105 return &kwl->kw[index]; /* found it !*/
106 else
107 ret = &kwl->kw[index]; /* may be OK */
108 }
109 }
110 }
111 return ret;
112}
113
114/* Dumps all registered "server" keywords to the <out> string pointer. The
115 * unsupported keywords are only dumped if their supported form was not
116 * found.
117 */
118void srv_dump_kws(char **out)
119{
120 struct srv_kw_list *kwl;
121 int index;
122
123 *out = NULL;
124 list_for_each_entry(kwl, &srv_keywords.list, list) {
125 for (index = 0; kwl->kw[index].kw != NULL; index++) {
126 if (kwl->kw[index].parse ||
127 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
128 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
129 kwl->scope,
130 kwl->kw[index].kw,
131 kwl->kw[index].skip ? " <arg>" : "",
132 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
133 kwl->kw[index].parse ? "" : " (not supported)");
134 }
135 }
136 }
137}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100138
Willy Tarreaudff55432012-10-10 17:51:05 +0200139/* parse the "id" server keyword */
140static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
141{
142 struct eb32_node *node;
143
144 if (!*args[*cur_arg + 1]) {
145 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
146 return ERR_ALERT | ERR_FATAL;
147 }
148
149 newsrv->puid = atol(args[*cur_arg + 1]);
150 newsrv->conf.id.key = newsrv->puid;
151
152 if (newsrv->puid <= 0) {
153 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
154 return ERR_ALERT | ERR_FATAL;
155 }
156
157 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
158 if (node) {
159 struct server *target = container_of(node, struct server, conf.id);
160 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
161 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
162 target->id);
163 return ERR_ALERT | ERR_FATAL;
164 }
165
166 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200167 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200168 return 0;
169}
170
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200171/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200172 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200173 * shutdown.
174 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200175void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200176{
Willy Tarreau87b09662015-04-03 00:22:06 +0200177 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200178
Willy Tarreau87b09662015-04-03 00:22:06 +0200179 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
180 if (stream->srv_conn == srv)
181 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200182}
183
184/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200185 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200186 * the reason for the shutdown.
187 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200188void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200189{
190 struct server *srv;
191
192 for (srv = px->srv; srv != NULL; srv = srv->next)
193 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200194 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200195}
196
Willy Tarreaubda92272014-05-20 21:55:30 +0200197/* Appends some information to a message string related to a server going UP or
198 * DOWN. If both <forced> and <reason> are null and the server tracks another
199 * one, a "via" information will be provided to know where the status came from.
200 * If <reason> is non-null, the entire string will be appended after a comma and
201 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200202 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200203 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200204 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200205void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200206{
Willy Tarreaubda92272014-05-20 21:55:30 +0200207 if (reason)
208 chunk_appendf(msg, ", %s", reason);
209 else if (!forced && s->track)
210 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200211
212 if (xferred >= 0) {
213 if (s->state == SRV_ST_STOPPED)
214 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
215 " %d sessions active, %d requeued, %d remaining in queue",
216 s->proxy->srv_act, s->proxy->srv_bck,
217 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
218 s->cur_sess, xferred, s->nbpend);
219 else
220 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
221 " %d sessions requeued, %d total in queue",
222 s->proxy->srv_act, s->proxy->srv_bck,
223 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
224 xferred, s->nbpend);
225 }
226}
227
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200228/* Marks server <s> down, regardless of its checks' statuses, notifies by all
229 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200230 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200231 * recomputes the number of servers, but not the map. Maintenance servers are
232 * ignored. It reports <reason> if non-null as the reason for going down. Note
233 * that it makes use of the trash to build the log strings, so <reason> must
234 * not be placed there.
235 */
236void srv_set_stopped(struct server *s, const char *reason)
237{
238 struct server *srv;
239 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
240 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900241 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200242 int xferred;
243
244 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
245 return;
246
247 s->last_change = now.tv_sec;
248 s->state = SRV_ST_STOPPED;
249 if (s->proxy->lbprm.set_server_status_down)
250 s->proxy->lbprm.set_server_status_down(s);
251
252 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200253 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200254
Willy Tarreau87b09662015-04-03 00:22:06 +0200255 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200256 * a connection. Those which are redispatchable will be queued
257 * to another server or to the proxy itself.
258 */
259 xferred = pendconn_redistribute(s);
260
261 chunk_printf(&trash,
262 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
263 s->proxy->id, s->id);
264
265 srv_append_status(&trash, s, reason, xferred, 0);
266 Warning("%s.\n", trash.str);
267
268 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900269 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
270 send_log(s->proxy, log_level, "%s.\n", trash.str);
271 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200272
273 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
274 set_backend_down(s->proxy);
275
276 s->counters.down_trans++;
277
278 for (srv = s->trackers; srv; srv = srv->tracknext)
279 srv_set_stopped(srv, NULL);
280}
281
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200282/* Marks server <s> up regardless of its checks' statuses and provided it isn't
283 * in maintenance. Notifies by all available means, recounts the remaining
284 * servers on the proxy and tries to grab requests from the proxy. It
285 * automatically recomputes the number of servers, but not the map. Maintenance
286 * servers are ignored. It reports <reason> if non-null as the reason for going
287 * up. Note that it makes use of the trash to build the log strings, so <reason>
288 * must not be placed there.
289 */
290void srv_set_running(struct server *s, const char *reason)
291{
292 struct server *srv;
293 int xferred;
294
295 if (s->admin & SRV_ADMF_MAINT)
296 return;
297
298 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
299 return;
300
301 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
302 if (s->proxy->last_change < now.tv_sec) // ignore negative times
303 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
304 s->proxy->last_change = now.tv_sec;
305 }
306
307 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
308 s->down_time += now.tv_sec - s->last_change;
309
310 s->last_change = now.tv_sec;
311
312 s->state = SRV_ST_STARTING;
313 if (s->slowstart > 0)
314 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
315 else
316 s->state = SRV_ST_RUNNING;
317
318 server_recalc_eweight(s);
319
320 /* If the server is set with "on-marked-up shutdown-backup-sessions",
321 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200322 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200323 * on all backup servers.
324 */
325 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
326 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200327 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200328
329 /* check if we can handle some connections queued at the proxy. We
330 * will take as many as we can handle.
331 */
332 xferred = pendconn_grab_from_px(s);
333
334 chunk_printf(&trash,
335 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
336 s->proxy->id, s->id);
337
338 srv_append_status(&trash, s, reason, xferred, 0);
339 Warning("%s.\n", trash.str);
340 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900341 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200342
343 for (srv = s->trackers; srv; srv = srv->tracknext)
344 srv_set_running(srv, NULL);
345}
346
Willy Tarreau8eb77842014-05-21 13:54:57 +0200347/* Marks server <s> stopping regardless of its checks' statuses and provided it
348 * isn't in maintenance. Notifies by all available means, recounts the remaining
349 * servers on the proxy and tries to grab requests from the proxy. It
350 * automatically recomputes the number of servers, but not the map. Maintenance
351 * servers are ignored. It reports <reason> if non-null as the reason for going
352 * up. Note that it makes use of the trash to build the log strings, so <reason>
353 * must not be placed there.
354 */
355void srv_set_stopping(struct server *s, const char *reason)
356{
357 struct server *srv;
358 int xferred;
359
360 if (s->admin & SRV_ADMF_MAINT)
361 return;
362
363 if (s->state == SRV_ST_STOPPING)
364 return;
365
366 s->last_change = now.tv_sec;
367 s->state = SRV_ST_STOPPING;
368 if (s->proxy->lbprm.set_server_status_down)
369 s->proxy->lbprm.set_server_status_down(s);
370
Willy Tarreau87b09662015-04-03 00:22:06 +0200371 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200372 * a connection. Those which are redispatchable will be queued
373 * to another server or to the proxy itself.
374 */
375 xferred = pendconn_redistribute(s);
376
377 chunk_printf(&trash,
378 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
379 s->proxy->id, s->id);
380
381 srv_append_status(&trash, s, reason, xferred, 0);
382
383 Warning("%s.\n", trash.str);
384 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
385
386 if (!s->proxy->srv_bck && !s->proxy->srv_act)
387 set_backend_down(s->proxy);
388
389 for (srv = s->trackers; srv; srv = srv->tracknext)
390 srv_set_stopping(srv, NULL);
391}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200392
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200393/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
394 * enforce either maint mode or drain mode. It is not allowed to set more than
395 * one flag at once. The equivalent "inherited" flag is propagated to all
396 * tracking servers. Maintenance mode disables health checks (but not agent
397 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100398 * is done. If <cause> is non-null, it will be displayed at the end of the log
399 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200400 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100401void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200402{
403 struct check *check = &s->check;
404 struct server *srv;
405 int xferred;
406
407 if (!mode)
408 return;
409
410 /* stop going down as soon as we meet a server already in the same state */
411 if (s->admin & mode)
412 return;
413
414 s->admin |= mode;
415
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200416 /* stop going down if the equivalent flag was already present (forced or inherited) */
417 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
418 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
419 return;
420
421 /* Maintenance must also disable health checks */
422 if (mode & SRV_ADMF_MAINT) {
423 if (s->check.state & CHK_ST_ENABLED) {
424 s->check.state |= CHK_ST_PAUSED;
425 check->health = 0;
426 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200427
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200428 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200429 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100430 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
431 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
432 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200433
Willy Tarreaubda92272014-05-20 21:55:30 +0200434 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200435
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100436 if (!(global.mode & MODE_STARTING)) {
437 Warning("%s.\n", trash.str);
438 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
439 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200440 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200441 else { /* server was still running */
442 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
443 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
444
445 check->health = 0; /* failure */
446 s->last_change = now.tv_sec;
447 s->state = SRV_ST_STOPPED;
448 if (s->proxy->lbprm.set_server_status_down)
449 s->proxy->lbprm.set_server_status_down(s);
450
451 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200452 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200453
Willy Tarreau87b09662015-04-03 00:22:06 +0200454 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200455 * a connection. Those which are redispatchable will be queued
456 * to another server or to the proxy itself.
457 */
458 xferred = pendconn_redistribute(s);
459
460 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100461 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200462 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +0100463 s->proxy->id, s->id,
464 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200465
466 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
467
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100468 if (!(global.mode & MODE_STARTING)) {
469 Warning("%s.\n", trash.str);
470 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
471 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200472
473 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
474 set_backend_down(s->proxy);
475
476 s->counters.down_trans++;
477 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200478 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200479
480 /* drain state is applied only if not yet in maint */
481 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200482 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
483
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200484 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200485 if (s->proxy->lbprm.set_server_status_down)
486 s->proxy->lbprm.set_server_status_down(s);
487
Willy Tarreau87b09662015-04-03 00:22:06 +0200488 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200489 * a connection. Those which are redispatchable will be queued
490 * to another server or to the proxy itself.
491 */
492 xferred = pendconn_redistribute(s);
493
Willy Tarreau8b428482016-11-07 15:53:43 +0100494 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
495 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
496 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200497
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200498 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200499
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100500 if (!(global.mode & MODE_STARTING)) {
501 Warning("%s.\n", trash.str);
502 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
503 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
504 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200505 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
506 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200507 }
508
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200509 /* compute the inherited flag to propagate */
510 if (mode & SRV_ADMF_MAINT)
511 mode = SRV_ADMF_IMAINT;
512 else if (mode & SRV_ADMF_DRAIN)
513 mode = SRV_ADMF_IDRAIN;
514
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200515 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +0100516 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200517}
518
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200519/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
520 * stop enforcing either maint mode or drain mode. It is not allowed to set more
521 * than one flag at once. The equivalent "inherited" flag is propagated to all
522 * tracking servers. Leaving maintenance mode re-enables health checks. When
523 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200524 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200525void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200526{
527 struct check *check = &s->check;
528 struct server *srv;
529 int xferred = -1;
530
531 if (!mode)
532 return;
533
534 /* stop going down as soon as we see the flag is not there anymore */
535 if (!(s->admin & mode))
536 return;
537
538 s->admin &= ~mode;
539
540 if (s->admin & SRV_ADMF_MAINT) {
541 /* remaining in maintenance mode, let's inform precisely about the
542 * situation.
543 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200544 if (mode & SRV_ADMF_FMAINT) {
545 chunk_printf(&trash,
546 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
547 s->flags & SRV_F_BACKUP ? "Backup " : "",
548 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200549
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200550 if (s->track) /* normally it's mandatory here */
551 chunk_appendf(&trash, " via %s/%s",
552 s->track->proxy->id, s->track->id);
553 Warning("%s.\n", trash.str);
554 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
555 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100556 if (mode & SRV_ADMF_RMAINT) {
557 chunk_printf(&trash,
558 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
559 s->flags & SRV_F_BACKUP ? "Backup " : "",
560 s->proxy->id, s->id, s->hostname);
561
562 if (s->track) /* normally it's mandatory here */
563 chunk_appendf(&trash, " via %s/%s",
564 s->track->proxy->id, s->track->id);
565 Warning("%s.\n", trash.str);
566 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
567 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200568 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200569 chunk_printf(&trash,
570 "%sServer %s/%s remains in forced maintenance",
571 s->flags & SRV_F_BACKUP ? "Backup " : "",
572 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200573 Warning("%s.\n", trash.str);
574 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
575 }
576 /* don't report anything when leaving drain mode and remaining in maintenance */
577 }
578 else if (mode & SRV_ADMF_MAINT) {
579 /* OK here we're leaving maintenance, we have many things to check,
580 * because the server might possibly be coming back up depending on
581 * its state. In practice, leaving maintenance means that we should
582 * immediately turn to UP (more or less the slowstart) under the
583 * following conditions :
584 * - server is neither checked nor tracked
585 * - server tracks another server which is not checked
586 * - server tracks another server which is already up
587 * Which sums up as something simpler :
588 * "either the tracking server is up or the server's checks are disabled
589 * or up". Otherwise we only re-enable health checks. There's a special
590 * case associated to the stopping state which can be inherited. Note
591 * that the server might still be in drain mode, which is naturally dealt
592 * with by the lower level functions.
593 */
594
595 if (s->check.state & CHK_ST_ENABLED) {
596 s->check.state &= ~CHK_ST_PAUSED;
597 check->health = check->rise; /* start OK but check immediately */
598 }
599
600 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
601 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
602 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
603 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
604 if (s->proxy->last_change < now.tv_sec) // ignore negative times
605 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
606 s->proxy->last_change = now.tv_sec;
607 }
608
609 if (s->last_change < now.tv_sec) // ignore negative times
610 s->down_time += now.tv_sec - s->last_change;
611 s->last_change = now.tv_sec;
612
613 if (s->track && s->track->state == SRV_ST_STOPPING)
614 s->state = SRV_ST_STOPPING;
615 else {
616 s->state = SRV_ST_STARTING;
617 if (s->slowstart > 0)
618 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
619 else
620 s->state = SRV_ST_RUNNING;
621 }
622
623 server_recalc_eweight(s);
624
625 /* If the server is set with "on-marked-up shutdown-backup-sessions",
626 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200627 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200628 * on all backup servers.
629 */
630 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
631 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200632 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200633
634 /* check if we can handle some connections queued at the proxy. We
635 * will take as many as we can handle.
636 */
637 xferred = pendconn_grab_from_px(s);
638 }
639
640 if (mode & SRV_ADMF_FMAINT) {
641 chunk_printf(&trash,
642 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
643 s->flags & SRV_F_BACKUP ? "Backup " : "",
644 s->proxy->id, s->id,
645 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
646 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200647 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100648 else if (mode & SRV_ADMF_RMAINT) {
649 chunk_printf(&trash,
650 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
651 s->flags & SRV_F_BACKUP ? "Backup " : "",
652 s->proxy->id, s->id, s->hostname,
653 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
654 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
655 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200656 else {
657 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200658 "%sServer %s/%s is %s/%s (leaving maintenance)",
659 s->flags & SRV_F_BACKUP ? "Backup " : "",
660 s->proxy->id, s->id,
661 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
662 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
663 srv_append_status(&trash, s, NULL, xferred, 0);
664 }
665 Warning("%s.\n", trash.str);
666 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
667 }
668 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
669 /* remaining in drain mode after removing one of its flags */
670
671 if (mode & SRV_ADMF_FDRAIN) {
672 chunk_printf(&trash,
673 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200674 s->flags & SRV_F_BACKUP ? "Backup " : "",
675 s->proxy->id, s->id);
676
677 if (s->track) /* normally it's mandatory here */
678 chunk_appendf(&trash, " via %s/%s",
679 s->track->proxy->id, s->track->id);
680 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200681 else {
682 chunk_printf(&trash,
683 "%sServer %s/%s remains in forced drain mode",
684 s->flags & SRV_F_BACKUP ? "Backup " : "",
685 s->proxy->id, s->id);
686 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200687 Warning("%s.\n", trash.str);
688 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200689 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200690 else if (mode & SRV_ADMF_DRAIN) {
691 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200692 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
693 if (s->proxy->last_change < now.tv_sec) // ignore negative times
694 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
695 s->proxy->last_change = now.tv_sec;
696 }
697
698 if (s->last_change < now.tv_sec) // ignore negative times
699 s->down_time += now.tv_sec - s->last_change;
700 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200701 server_recalc_eweight(s);
702
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200703 if (mode & SRV_ADMF_FDRAIN) {
704 chunk_printf(&trash,
705 "%sServer %s/%s is %s (leaving forced drain)",
706 s->flags & SRV_F_BACKUP ? "Backup " : "",
707 s->proxy->id, s->id,
708 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
709 }
710 else {
711 chunk_printf(&trash,
712 "%sServer %s/%s is %s (leaving drain)",
713 s->flags & SRV_F_BACKUP ? "Backup " : "",
714 s->proxy->id, s->id,
715 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
716 if (s->track) /* normally it's mandatory here */
717 chunk_appendf(&trash, " via %s/%s",
718 s->track->proxy->id, s->track->id);
719 }
720 Warning("%s.\n", trash.str);
721 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200722 }
723
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200724 /* stop going down if the equivalent flag is still present (forced or inherited) */
725 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
726 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
727 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200728
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200729 if (mode & SRV_ADMF_MAINT)
730 mode = SRV_ADMF_IMAINT;
731 else if (mode & SRV_ADMF_DRAIN)
732 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200733
734 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200735 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200736}
737
Willy Tarreau757478e2016-11-03 19:22:19 +0100738/* principle: propagate maint and drain to tracking servers. This is useful
739 * upon startup so that inherited states are correct.
740 */
741static void srv_propagate_admin_state(struct server *srv)
742{
743 struct server *srv2;
744
745 if (!srv->trackers)
746 return;
747
748 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
749 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +0100750 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100751
752 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +0100753 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100754 }
755}
756
757/* Compute and propagate the admin states for all servers in proxy <px>.
758 * Only servers *not* tracking another one are considered, because other
759 * ones will be handled when the server they track is visited.
760 */
761void srv_compute_all_admin_states(struct proxy *px)
762{
763 struct server *srv;
764
765 for (srv = px->srv; srv; srv = srv->next) {
766 if (srv->track)
767 continue;
768 srv_propagate_admin_state(srv);
769 }
770}
771
Willy Tarreaudff55432012-10-10 17:51:05 +0200772/* Note: must not be declared <const> as its list will be overwritten.
773 * Please take care of keeping this list alphabetically sorted, doing so helps
774 * all code contributors.
775 * Optional keywords are also declared with a NULL ->parse() function so that
776 * the config parser can report an appropriate error when a known keyword was
777 * not enabled.
778 */
779static struct srv_kw_list srv_kws = { "ALL", { }, {
780 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
781 { NULL, NULL, 0 },
782}};
783
784__attribute__((constructor))
785static void __listener_init(void)
786{
787 srv_register_keywords(&srv_kws);
788}
789
Willy Tarreau004e0452013-11-21 11:22:01 +0100790/* Recomputes the server's eweight based on its state, uweight, the current time,
791 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
792 * state is automatically disabled if the time is elapsed.
793 */
794void server_recalc_eweight(struct server *sv)
795{
796 struct proxy *px = sv->proxy;
797 unsigned w;
798
799 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
800 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200801 if (sv->state == SRV_ST_STARTING)
802 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100803 }
804
805 /* We must take care of not pushing the server to full throttle during slow starts.
806 * It must also start immediately, at least at the minimal step when leaving maintenance.
807 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200808 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100809 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
810 else
811 w = px->lbprm.wdiv;
812
813 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
814
815 /* now propagate the status change to any LB algorithms */
816 if (px->lbprm.update_server_eweight)
817 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200818 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100819 if (px->lbprm.set_server_status_up)
820 px->lbprm.set_server_status_up(sv);
821 }
822 else {
823 if (px->lbprm.set_server_status_down)
824 px->lbprm.set_server_status_down(sv);
825 }
826}
827
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900829 * Parses weight_str and configures sv accordingly.
830 * Returns NULL on success, error message string otherwise.
831 */
832const char *server_parse_weight_change_request(struct server *sv,
833 const char *weight_str)
834{
835 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900836 long int w;
837 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900838
839 px = sv->proxy;
840
841 /* if the weight is terminated with '%', it is set relative to
842 * the initial weight, otherwise it is absolute.
843 */
844 if (!*weight_str)
845 return "Require <weight> or <weight%>.\n";
846
Simon Hormanb796afa2013-02-12 10:45:53 +0900847 w = strtol(weight_str, &end, 10);
848 if (end == weight_str)
849 return "Empty weight string empty or preceded by garbage";
850 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900851 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900852 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900853 /* Avoid integer overflow */
854 if (w > 25600)
855 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900856 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900857 if (w > 256)
858 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900859 }
860 else if (w < 0 || w > 256)
861 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900862 else if (end[0] != '\0')
863 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900864
865 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
866 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
867
868 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100869 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900870
871 return NULL;
872}
873
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200874/*
Thierry Fournier09a91782016-02-24 08:25:39 +0100875 * Parses <addr_str> and configures <sv> accordingly. <from> precise
876 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200877 * Returns:
878 * - error string on error
879 * - NULL on success
880 */
881const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +0100882 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200883{
884 unsigned char ip[INET6_ADDRSTRLEN];
885
886 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100887 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200888 return NULL;
889 }
890 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100891 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200892 return NULL;
893 }
894
895 return "Could not understand IP address format.\n";
896}
897
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200898const char *server_parse_maxconn_change_request(struct server *sv,
899 const char *maxconn_str)
900{
901 long int v;
902 char *end;
903
904 if (!*maxconn_str)
905 return "Require <maxconn>.\n";
906
907 v = strtol(maxconn_str, &end, 10);
908 if (end == maxconn_str)
909 return "maxconn string empty or preceded by garbage";
910 else if (end[0] != '\0')
911 return "Trailing garbage in maxconn string";
912
913 if (sv->maxconn == sv->minconn) { // static maxconn
914 sv->maxconn = sv->minconn = v;
915 } else { // dynamic maxconn
916 sv->maxconn = v;
917 }
918
919 if (may_dequeue_tasks(sv, sv->proxy))
920 process_srv_queue(sv);
921
922 return NULL;
923}
924
Willy Tarreau272adea2014-03-31 10:39:59 +0200925int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
926{
927 struct server *newsrv = NULL;
928 const char *err;
929 char *errmsg = NULL;
930 int err_code = 0;
931 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +0200932 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +0200933
934 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
935 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +0200936 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
937
938 if (!defsrv && curproxy == defproxy) {
939 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
940 err_code |= ERR_ALERT | ERR_FATAL;
941 goto out;
942 }
943 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
944 err_code |= ERR_ALERT | ERR_FATAL;
945
946 if (!*args[2]) {
947 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
948 file, linenum, args[0]);
949 err_code |= ERR_ALERT | ERR_FATAL;
950 goto out;
951 }
952
953 err = invalid_char(args[1]);
954 if (err && !defsrv) {
955 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
956 file, linenum, *err, args[1]);
957 err_code |= ERR_ALERT | ERR_FATAL;
958 goto out;
959 }
960
961 if (!defsrv) {
962 struct sockaddr_storage *sk;
963 int port1, port2;
964 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200965 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200966
Vincent Bernat02779b62016-04-03 13:48:43 +0200967 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200968 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
969 err_code |= ERR_ALERT | ERR_ABORT;
970 goto out;
971 }
972
973 /* the servers are linked backwards first */
974 newsrv->next = curproxy->srv;
975 curproxy->srv = newsrv;
976 newsrv->proxy = curproxy;
977 newsrv->conf.file = strdup(file);
978 newsrv->conf.line = linenum;
979
980 newsrv->obj_type = OBJ_TYPE_SERVER;
981 LIST_INIT(&newsrv->actconns);
982 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +0200983 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +0200984 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +0200985 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +0200986 do_check = 0;
987 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200988 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200989 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200990 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200991 newsrv->last_change = now.tv_sec;
992 newsrv->id = strdup(args[1]);
993
994 /* several ways to check the port component :
995 * - IP => port=+0, relative (IPv4 only)
996 * - IP: => port=+0, relative
997 * - IP:N => port=N, absolute
998 * - IP:+N => port=+N, relative
999 * - IP:-N => port=-N, relative
1000 */
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01001001 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001002 if (!sk) {
1003 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1004 err_code |= ERR_ALERT | ERR_FATAL;
1005 goto out;
1006 }
1007
1008 proto = protocol_by_family(sk->ss_family);
1009 if (!proto || !proto->connect) {
1010 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1011 file, linenum, args[0], args[1]);
1012 err_code |= ERR_ALERT | ERR_FATAL;
1013 goto out;
1014 }
1015
1016 if (!port1 || !port2) {
1017 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001018 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001019 }
1020 else if (port1 != port2) {
1021 /* port range */
1022 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1023 file, linenum, args[0], args[1], args[2]);
1024 err_code |= ERR_ALERT | ERR_FATAL;
1025 goto out;
1026 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001027
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001028 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001029 newsrv->hostname = fqdn;
1030 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001031 goto skip_name_resolution;
1032
Willy Tarreau07101d52015-09-08 16:16:35 +02001033 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001034 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001035 goto skip_name_resolution;
1036
1037 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1038 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1039 goto skip_name_resolution;
1040 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1041 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1042 file, linenum, args[2]);
1043 err_code |= ERR_ALERT | ERR_FATAL;
1044 goto out;
1045 }
1046
1047 curr_resolution->requester = newsrv;
1048 curr_resolution->requester_cb = snr_resolution_cb;
1049 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1050 curr_resolution->status = RSLV_STATUS_NONE;
1051 curr_resolution->step = RSLV_STEP_NONE;
1052 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001053 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001054 newsrv->resolution = curr_resolution;
1055
1056 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001057 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +01001058 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +02001059
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001060 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001061 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1062 file, linenum, newsrv->addr.ss_family, args[2]);
1063 err_code |= ERR_ALERT | ERR_FATAL;
1064 goto out;
1065 }
1066
1067 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1068 newsrv->check.port = curproxy->defsrv.check.port;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001069 if (newsrv->check.port)
1070 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001071 newsrv->check.inter = curproxy->defsrv.check.inter;
1072 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1073 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1074 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1075 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001076 if (curproxy->defsrv.agent.send_string != NULL)
1077 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1078 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001079 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1080 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1081 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1082 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1083 newsrv->minconn = curproxy->defsrv.minconn;
1084 newsrv->maxconn = curproxy->defsrv.maxconn;
1085 newsrv->slowstart = curproxy->defsrv.slowstart;
1086 newsrv->onerror = curproxy->defsrv.onerror;
1087 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1088 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1089 newsrv->consecutive_errors_limit
1090 = curproxy->defsrv.consecutive_errors_limit;
1091#ifdef OPENSSL
1092 newsrv->use_ssl = curproxy->defsrv.use_ssl;
1093#endif
1094 newsrv->uweight = newsrv->iweight
1095 = curproxy->defsrv.iweight;
1096
1097 newsrv->check.status = HCHK_STATUS_INI;
1098 newsrv->check.rise = curproxy->defsrv.check.rise;
1099 newsrv->check.fall = curproxy->defsrv.check.fall;
1100 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1101 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001102 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001103
1104 newsrv->agent.status = HCHK_STATUS_INI;
1105 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1106 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1107 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1108 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001109 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1110 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1111 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001112 memcpy(newsrv->dns_opts.pref_net,
1113 curproxy->defsrv.dns_opts.pref_net,
1114 sizeof(newsrv->dns_opts.pref_net));
1115 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001116 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1117 newsrv->init_addr = curproxy->defsrv.init_addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001118
1119 cur_arg = 3;
1120 } else {
1121 newsrv = &curproxy->defsrv;
1122 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001123 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001124 }
1125
1126 while (*args[cur_arg]) {
1127 if (!strcmp(args[cur_arg], "agent-check")) {
1128 global.maxsock++;
1129 do_agent = 1;
1130 cur_arg += 1;
1131 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1132 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1133 if (err) {
1134 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1135 file, linenum, *err, newsrv->id);
1136 err_code |= ERR_ALERT | ERR_FATAL;
1137 goto out;
1138 }
1139 if (val <= 0) {
1140 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1141 file, linenum, val, args[cur_arg], newsrv->id);
1142 err_code |= ERR_ALERT | ERR_FATAL;
1143 goto out;
1144 }
1145 newsrv->agent.inter = val;
1146 cur_arg += 2;
1147 }
1148 else if (!strcmp(args[cur_arg], "agent-port")) {
1149 global.maxsock++;
1150 newsrv->agent.port = atol(args[cur_arg + 1]);
1151 cur_arg += 2;
1152 }
James Brown55f9ff12015-10-21 18:19:05 -07001153 else if (!strcmp(args[cur_arg], "agent-send")) {
1154 global.maxsock++;
1155 free(newsrv->agent.send_string);
1156 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1157 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1158 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1159 cur_arg += 2;
1160 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001161 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1162 newsrv->cookie = strdup(args[cur_arg + 1]);
1163 newsrv->cklen = strlen(args[cur_arg + 1]);
1164 cur_arg += 2;
1165 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001166 else if (!strcmp(args[cur_arg], "init-addr")) {
1167 char *p, *end;
1168 int done;
1169
1170 newsrv->init_addr_methods = 0;
1171 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1172
1173 for (p = args[cur_arg + 1]; *p; p = end) {
1174 /* cut on next comma */
1175 for (end = p; *end && *end != ','; end++);
1176 if (*end)
1177 *(end++) = 0;
1178
1179 if (!strcmp(p, "libc")) {
1180 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1181 }
1182 else if (!strcmp(p, "last")) {
1183 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1184 }
1185 else {
1186 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last'.\n",
1187 file, linenum, args[cur_arg], p);
1188 err_code |= ERR_ALERT | ERR_FATAL;
1189 goto out;
1190 }
1191 if (!done) {
1192 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1193 file, linenum, args[cur_arg], p);
1194 err_code |= ERR_ALERT | ERR_FATAL;
1195 goto out;
1196 }
1197 }
1198 cur_arg += 2;
1199 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001200 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1201 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1202 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1203 cur_arg += 2;
1204 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001205 else if (!strcmp(args[cur_arg], "resolvers")) {
1206 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1207 cur_arg += 2;
1208 }
1209 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1210 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001211 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001212 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001213 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001214 else {
1215 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1216 file, linenum, args[cur_arg]);
1217 err_code |= ERR_ALERT | ERR_FATAL;
1218 goto out;
1219 }
1220 cur_arg += 2;
1221 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001222 else if (!strcmp(args[cur_arg], "resolve-net")) {
1223 char *p, *e;
1224 unsigned char mask;
1225 struct dns_options *opt;
1226
1227 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1228 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1229 file, linenum, args[cur_arg]);
1230 err_code |= ERR_ALERT | ERR_FATAL;
1231 goto out;
1232 }
1233
1234 opt = &newsrv->dns_opts;
1235
1236 /* Split arguments by comma, and convert it from ipv4 or ipv6
1237 * string network in in_addr or in6_addr.
1238 */
1239 p = args[cur_arg + 1];
1240 e = p;
1241 while (*p != '\0') {
1242 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001243 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001244 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1245 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1246 err_code |= ERR_ALERT | ERR_FATAL;
1247 goto out;
1248 }
1249 /* look for end or comma. */
1250 while (*e != ',' && *e != '\0')
1251 e++;
1252 if (*e == ',') {
1253 *e = '\0';
1254 e++;
1255 }
1256 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1257 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1258 /* Try to convert input string from ipv4 or ipv6 network. */
1259 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1260 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1261 &mask)) {
1262 /* Try to convert input string from ipv6 network. */
1263 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1264 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1265 } else {
1266 /* All network conversions fail, retrun error. */
1267 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1268 file, linenum, args[cur_arg], p);
1269 err_code |= ERR_ALERT | ERR_FATAL;
1270 goto out;
1271 }
1272 opt->pref_net_nb++;
1273 p = e;
1274 }
1275
1276 cur_arg += 2;
1277 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001278 else if (!strcmp(args[cur_arg], "rise")) {
1279 if (!*args[cur_arg + 1]) {
1280 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1281 file, linenum, args[cur_arg]);
1282 err_code |= ERR_ALERT | ERR_FATAL;
1283 goto out;
1284 }
1285
1286 newsrv->check.rise = atol(args[cur_arg + 1]);
1287 if (newsrv->check.rise <= 0) {
1288 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1289 file, linenum, args[cur_arg]);
1290 err_code |= ERR_ALERT | ERR_FATAL;
1291 goto out;
1292 }
1293
1294 if (newsrv->check.health)
1295 newsrv->check.health = newsrv->check.rise;
1296 cur_arg += 2;
1297 }
1298 else if (!strcmp(args[cur_arg], "fall")) {
1299 newsrv->check.fall = atol(args[cur_arg + 1]);
1300
1301 if (!*args[cur_arg + 1]) {
1302 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1303 file, linenum, args[cur_arg]);
1304 err_code |= ERR_ALERT | ERR_FATAL;
1305 goto out;
1306 }
1307
1308 if (newsrv->check.fall <= 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 cur_arg += 2;
1316 }
1317 else if (!strcmp(args[cur_arg], "inter")) {
1318 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1319 if (err) {
1320 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1321 file, linenum, *err, newsrv->id);
1322 err_code |= ERR_ALERT | ERR_FATAL;
1323 goto out;
1324 }
1325 if (val <= 0) {
1326 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1327 file, linenum, val, args[cur_arg], newsrv->id);
1328 err_code |= ERR_ALERT | ERR_FATAL;
1329 goto out;
1330 }
1331 newsrv->check.inter = val;
1332 cur_arg += 2;
1333 }
1334 else if (!strcmp(args[cur_arg], "fastinter")) {
1335 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1336 if (err) {
1337 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1338 file, linenum, *err, newsrv->id);
1339 err_code |= ERR_ALERT | ERR_FATAL;
1340 goto out;
1341 }
1342 if (val <= 0) {
1343 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1344 file, linenum, val, args[cur_arg], newsrv->id);
1345 err_code |= ERR_ALERT | ERR_FATAL;
1346 goto out;
1347 }
1348 newsrv->check.fastinter = val;
1349 cur_arg += 2;
1350 }
1351 else if (!strcmp(args[cur_arg], "downinter")) {
1352 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1353 if (err) {
1354 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1355 file, linenum, *err, newsrv->id);
1356 err_code |= ERR_ALERT | ERR_FATAL;
1357 goto out;
1358 }
1359 if (val <= 0) {
1360 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1361 file, linenum, val, args[cur_arg], newsrv->id);
1362 err_code |= ERR_ALERT | ERR_FATAL;
1363 goto out;
1364 }
1365 newsrv->check.downinter = val;
1366 cur_arg += 2;
1367 }
1368 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1369 struct sockaddr_storage *sk;
1370 int port1, port2;
1371 struct protocol *proto;
1372
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001373 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001374 if (!sk) {
1375 Alert("parsing [%s:%d] : '%s' : %s\n",
1376 file, linenum, args[cur_arg], errmsg);
1377 err_code |= ERR_ALERT | ERR_FATAL;
1378 goto out;
1379 }
1380
1381 proto = protocol_by_family(sk->ss_family);
1382 if (!proto || !proto->connect) {
1383 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1384 file, linenum, args[cur_arg], args[cur_arg + 1]);
1385 err_code |= ERR_ALERT | ERR_FATAL;
1386 goto out;
1387 }
1388
1389 if (port1 != port2) {
1390 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1391 file, linenum, args[cur_arg], args[cur_arg + 1]);
1392 err_code |= ERR_ALERT | ERR_FATAL;
1393 goto out;
1394 }
1395
Simon Horman41f58762015-01-30 11:22:56 +09001396 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001397 newsrv->flags |= SRV_F_CHECKADDR;
1398 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001399 cur_arg += 2;
1400 }
1401 else if (!strcmp(args[cur_arg], "port")) {
1402 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001403 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001404 cur_arg += 2;
1405 }
1406 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001407 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001408 cur_arg ++;
1409 }
1410 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001411 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001412 cur_arg ++;
1413 }
1414 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001415 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001416 cur_arg ++;
1417 }
David Safb76832014-05-08 23:42:08 -04001418 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1419 newsrv->pp_opts |= SRV_PP_V2;
1420 cur_arg ++;
1421 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001422 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1423 newsrv->check.send_proxy = 1;
1424 cur_arg ++;
1425 }
1426 else if (!strcmp(args[cur_arg], "weight")) {
1427 int w;
1428 w = atol(args[cur_arg + 1]);
1429 if (w < 0 || w > SRV_UWGHT_MAX) {
1430 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1431 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1432 err_code |= ERR_ALERT | ERR_FATAL;
1433 goto out;
1434 }
1435 newsrv->uweight = newsrv->iweight = w;
1436 cur_arg += 2;
1437 }
1438 else if (!strcmp(args[cur_arg], "minconn")) {
1439 newsrv->minconn = atol(args[cur_arg + 1]);
1440 cur_arg += 2;
1441 }
1442 else if (!strcmp(args[cur_arg], "maxconn")) {
1443 newsrv->maxconn = atol(args[cur_arg + 1]);
1444 cur_arg += 2;
1445 }
1446 else if (!strcmp(args[cur_arg], "maxqueue")) {
1447 newsrv->maxqueue = atol(args[cur_arg + 1]);
1448 cur_arg += 2;
1449 }
1450 else if (!strcmp(args[cur_arg], "slowstart")) {
1451 /* slowstart is stored in seconds */
1452 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1453 if (err) {
1454 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1455 file, linenum, *err, newsrv->id);
1456 err_code |= ERR_ALERT | ERR_FATAL;
1457 goto out;
1458 }
1459 newsrv->slowstart = (val + 999) / 1000;
1460 cur_arg += 2;
1461 }
1462 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1463
1464 if (!*args[cur_arg + 1]) {
1465 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1466 file, linenum);
1467 err_code |= ERR_ALERT | ERR_FATAL;
1468 goto out;
1469 }
1470
1471 newsrv->trackit = strdup(args[cur_arg + 1]);
1472
1473 cur_arg += 2;
1474 }
1475 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1476 global.maxsock++;
1477 do_check = 1;
1478 cur_arg += 1;
1479 }
1480 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001481 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001482 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001483 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001484 newsrv->check.state |= CHK_ST_PAUSED;
1485 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001486 cur_arg += 1;
1487 }
1488 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1489 if (!strcmp(args[cur_arg + 1], "none"))
1490 newsrv->observe = HANA_OBS_NONE;
1491 else if (!strcmp(args[cur_arg + 1], "layer4"))
1492 newsrv->observe = HANA_OBS_LAYER4;
1493 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1494 if (curproxy->mode != PR_MODE_HTTP) {
1495 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1496 file, linenum, args[cur_arg + 1]);
1497 err_code |= ERR_ALERT;
1498 }
1499 newsrv->observe = HANA_OBS_LAYER7;
1500 }
1501 else {
1502 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1503 "'layer4', 'layer7' but got '%s'\n",
1504 file, linenum, args[cur_arg], args[cur_arg + 1]);
1505 err_code |= ERR_ALERT | ERR_FATAL;
1506 goto out;
1507 }
1508
1509 cur_arg += 2;
1510 }
1511 else if (!strcmp(args[cur_arg], "on-error")) {
1512 if (!strcmp(args[cur_arg + 1], "fastinter"))
1513 newsrv->onerror = HANA_ONERR_FASTINTER;
1514 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1515 newsrv->onerror = HANA_ONERR_FAILCHK;
1516 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1517 newsrv->onerror = HANA_ONERR_SUDDTH;
1518 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1519 newsrv->onerror = HANA_ONERR_MARKDWN;
1520 else {
1521 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1522 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1523 file, linenum, args[cur_arg], args[cur_arg + 1]);
1524 err_code |= ERR_ALERT | ERR_FATAL;
1525 goto out;
1526 }
1527
1528 cur_arg += 2;
1529 }
1530 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1531 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1532 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1533 else {
1534 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1535 file, linenum, args[cur_arg], args[cur_arg + 1]);
1536 err_code |= ERR_ALERT | ERR_FATAL;
1537 goto out;
1538 }
1539
1540 cur_arg += 2;
1541 }
1542 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1543 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1544 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1545 else {
1546 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1547 file, linenum, args[cur_arg], args[cur_arg + 1]);
1548 err_code |= ERR_ALERT | ERR_FATAL;
1549 goto out;
1550 }
1551
1552 cur_arg += 2;
1553 }
1554 else if (!strcmp(args[cur_arg], "error-limit")) {
1555 if (!*args[cur_arg + 1]) {
1556 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1557 file, linenum, args[cur_arg]);
1558 err_code |= ERR_ALERT | ERR_FATAL;
1559 goto out;
1560 }
1561
1562 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1563
1564 if (newsrv->consecutive_errors_limit <= 0) {
1565 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1566 file, linenum, args[cur_arg]);
1567 err_code |= ERR_ALERT | ERR_FATAL;
1568 goto out;
1569 }
1570 cur_arg += 2;
1571 }
1572 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1573 int port_low, port_high;
1574 struct sockaddr_storage *sk;
1575 struct protocol *proto;
1576
1577 if (!*args[cur_arg + 1]) {
1578 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1579 file, linenum, "source", "usesrc", "interface");
1580 err_code |= ERR_ALERT | ERR_FATAL;
1581 goto out;
1582 }
1583
1584 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001585 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001586 if (!sk) {
1587 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1588 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1589 err_code |= ERR_ALERT | ERR_FATAL;
1590 goto out;
1591 }
1592
1593 proto = protocol_by_family(sk->ss_family);
1594 if (!proto || !proto->connect) {
1595 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1596 file, linenum, args[cur_arg], args[cur_arg+1]);
1597 err_code |= ERR_ALERT | ERR_FATAL;
1598 goto out;
1599 }
1600
1601 newsrv->conn_src.source_addr = *sk;
1602
1603 if (port_low != port_high) {
1604 int i;
1605
1606 if (!port_low || !port_high) {
1607 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1608 file, linenum, args[cur_arg], args[cur_arg + 1]);
1609 err_code |= ERR_ALERT | ERR_FATAL;
1610 goto out;
1611 }
1612
1613 if (port_low <= 0 || port_low > 65535 ||
1614 port_high <= 0 || port_high > 65535 ||
1615 port_low > port_high) {
1616 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1617 file, linenum, port_low, port_high);
1618 err_code |= ERR_ALERT | ERR_FATAL;
1619 goto out;
1620 }
1621 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1622 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1623 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1624 }
1625
1626 cur_arg += 2;
1627 while (*(args[cur_arg])) {
1628 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001629#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001630 if (!*args[cur_arg + 1]) {
1631 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1632 file, linenum, "usesrc");
1633 err_code |= ERR_ALERT | ERR_FATAL;
1634 goto out;
1635 }
1636 if (!strcmp(args[cur_arg + 1], "client")) {
1637 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1638 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1639 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1640 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1641 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1642 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1643 char *name, *end;
1644
1645 name = args[cur_arg+1] + 7;
1646 while (isspace(*name))
1647 name++;
1648
1649 end = name;
1650 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1651 end++;
1652
1653 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1654 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1655 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1656 newsrv->conn_src.bind_hdr_len = end - name;
1657 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1658 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1659 newsrv->conn_src.bind_hdr_occ = -1;
1660
1661 /* now look for an occurrence number */
1662 while (isspace(*end))
1663 end++;
1664 if (*end == ',') {
1665 end++;
1666 name = end;
1667 if (*end == '-')
1668 end++;
1669 while (isdigit((int)*end))
1670 end++;
1671 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1672 }
1673
1674 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1675 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1676 " occurrences values smaller than %d.\n",
1677 file, linenum, MAX_HDR_HISTORY);
1678 err_code |= ERR_ALERT | ERR_FATAL;
1679 goto out;
1680 }
1681 } else {
1682 struct sockaddr_storage *sk;
1683 int port1, port2;
1684
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001685 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001686 if (!sk) {
1687 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1688 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1689 err_code |= ERR_ALERT | ERR_FATAL;
1690 goto out;
1691 }
1692
1693 proto = protocol_by_family(sk->ss_family);
1694 if (!proto || !proto->connect) {
1695 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1696 file, linenum, args[cur_arg], args[cur_arg+1]);
1697 err_code |= ERR_ALERT | ERR_FATAL;
1698 goto out;
1699 }
1700
1701 if (port1 != port2) {
1702 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1703 file, linenum, args[cur_arg], args[cur_arg + 1]);
1704 err_code |= ERR_ALERT | ERR_FATAL;
1705 goto out;
1706 }
1707 newsrv->conn_src.tproxy_addr = *sk;
1708 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1709 }
1710 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001711 cur_arg += 2;
1712 continue;
1713#else /* no TPROXY support */
1714 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1715 file, linenum, "usesrc");
1716 err_code |= ERR_ALERT | ERR_FATAL;
1717 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001718#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001719 } /* "usesrc" */
1720
1721 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1722#ifdef SO_BINDTODEVICE
1723 if (!*args[cur_arg + 1]) {
1724 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1725 file, linenum, args[0]);
1726 err_code |= ERR_ALERT | ERR_FATAL;
1727 goto out;
1728 }
1729 free(newsrv->conn_src.iface_name);
1730 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1731 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1732 global.last_checks |= LSTCHK_NETADM;
1733#else
1734 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1735 file, linenum, args[0], args[cur_arg]);
1736 err_code |= ERR_ALERT | ERR_FATAL;
1737 goto out;
1738#endif
1739 cur_arg += 2;
1740 continue;
1741 }
1742 /* this keyword in not an option of "source" */
1743 break;
1744 } /* while */
1745 }
1746 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1747 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1748 file, linenum, "usesrc", "source");
1749 err_code |= ERR_ALERT | ERR_FATAL;
1750 goto out;
1751 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001752 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1753#ifdef CONFIG_HAP_NS
1754 char *arg = args[cur_arg + 1];
1755 if (!strcmp(arg, "*")) {
1756 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1757 } else {
1758 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1759
1760 if (newsrv->netns == NULL)
1761 newsrv->netns = netns_store_insert(arg);
1762
1763 if (newsrv->netns == NULL) {
1764 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1765 err_code |= ERR_ALERT | ERR_FATAL;
1766 goto out;
1767 }
1768 }
1769#else
1770 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1771 file, linenum, args[0], args[cur_arg]);
1772 err_code |= ERR_ALERT | ERR_FATAL;
1773 goto out;
1774#endif
1775 cur_arg += 2;
1776 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001777 else {
1778 static int srv_dumped;
1779 struct srv_kw *kw;
1780 char *err;
1781
1782 kw = srv_find_kw(args[cur_arg]);
1783 if (kw) {
1784 char *err = NULL;
1785 int code;
1786
1787 if (!kw->parse) {
1788 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1789 file, linenum, args[0], args[1], args[cur_arg]);
1790 cur_arg += 1 + kw->skip ;
1791 err_code |= ERR_ALERT | ERR_FATAL;
1792 goto out;
1793 }
1794
1795 if (defsrv && !kw->default_ok) {
1796 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1797 file, linenum, args[0], args[1], args[cur_arg]);
1798 cur_arg += 1 + kw->skip ;
1799 err_code |= ERR_ALERT;
1800 continue;
1801 }
1802
1803 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1804 err_code |= code;
1805
1806 if (code) {
1807 if (err && *err) {
1808 indent_msg(&err, 2);
1809 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1810 }
1811 else
1812 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1813 file, linenum, args[0], args[1], args[cur_arg]);
1814 if (code & ERR_FATAL) {
1815 free(err);
1816 cur_arg += 1 + kw->skip;
1817 goto out;
1818 }
1819 }
1820 free(err);
1821 cur_arg += 1 + kw->skip;
1822 continue;
1823 }
1824
1825 err = NULL;
1826 if (!srv_dumped) {
1827 srv_dump_kws(&err);
1828 indent_msg(&err, 4);
1829 srv_dumped = 1;
1830 }
1831
1832 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1833 file, linenum, args[0], args[1], args[cur_arg],
1834 err ? " Registered keywords :" : "", err ? err : "");
1835 free(err);
1836
1837 err_code |= ERR_ALERT | ERR_FATAL;
1838 goto out;
1839 }
1840 }
1841
Willy Tarreau272adea2014-03-31 10:39:59 +02001842 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001843 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001844
1845 if (newsrv->trackit) {
1846 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1847 file, linenum);
1848 err_code |= ERR_ALERT | ERR_FATAL;
1849 goto out;
1850 }
1851
Willy Tarreau272adea2014-03-31 10:39:59 +02001852 /*
1853 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001854 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001855 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001856 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001857 (is_inet_addr(&newsrv->check.addr) ||
1858 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001859 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001860 struct list *l;
1861
1862 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1863 if (!r) {
1864 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1865 file, linenum, newsrv->id);
1866 err_code |= ERR_ALERT | ERR_FATAL;
1867 goto out;
1868 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001869 /* search the first action (connect / send / expect) in the list */
1870 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001871 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001872 if (r->action != TCPCHK_ACT_COMMENT)
1873 break;
1874 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001875 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1876 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",
1877 file, linenum, newsrv->id);
1878 err_code |= ERR_ALERT | ERR_FATAL;
1879 goto out;
1880 }
1881 else {
1882 /* scan the tcp-check ruleset to ensure a port has been configured */
1883 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001884 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001885 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1886 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",
1887 file, linenum, newsrv->id);
1888 err_code |= ERR_ALERT | ERR_FATAL;
1889 goto out;
1890 }
1891 }
1892 }
1893 }
1894
1895 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001896 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001897 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001898 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1899 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001900 goto out;
1901 }
1902
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001903 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001904 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001905
Willy Tarreau272adea2014-03-31 10:39:59 +02001906 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1907 }
1908
1909 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001910 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001911
1912 if (!newsrv->agent.port) {
1913 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1914 file, linenum, newsrv->id);
1915 err_code |= ERR_ALERT | ERR_FATAL;
1916 goto out;
1917 }
1918
1919 if (!newsrv->agent.inter)
1920 newsrv->agent.inter = newsrv->check.inter;
1921
Simon Hormanb1900d52015-01-30 11:22:54 +09001922 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001923 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001924 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1925 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001926 goto out;
1927 }
1928
1929 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1930 }
1931
1932 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001933 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001934 curproxy->srv_bck++;
1935 else
1936 curproxy->srv_act++;
1937
Willy Tarreauc5150da2014-05-13 19:27:31 +02001938 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001939 }
1940 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001941 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001942 return 0;
1943
1944 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001945 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001946 free(errmsg);
1947 return err_code;
1948}
1949
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001950/* Returns a pointer to the first server matching either id <id>.
1951 * NULL is returned if no match is found.
1952 * the lookup is performed in the backend <bk>
1953 */
1954struct server *server_find_by_id(struct proxy *bk, int id)
1955{
1956 struct eb32_node *eb32;
1957 struct server *curserver;
1958
1959 if (!bk || (id ==0))
1960 return NULL;
1961
1962 /* <bk> has no backend capabilities, so it can't have a server */
1963 if (!(bk->cap & PR_CAP_BE))
1964 return NULL;
1965
1966 curserver = NULL;
1967
1968 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1969 if (eb32)
1970 curserver = container_of(eb32, struct server, conf.id);
1971
1972 return curserver;
1973}
1974
1975/* Returns a pointer to the first server matching either name <name>, or id
1976 * if <name> starts with a '#'. NULL is returned if no match is found.
1977 * the lookup is performed in the backend <bk>
1978 */
1979struct server *server_find_by_name(struct proxy *bk, const char *name)
1980{
1981 struct server *curserver;
1982
1983 if (!bk || !name)
1984 return NULL;
1985
1986 /* <bk> has no backend capabilities, so it can't have a server */
1987 if (!(bk->cap & PR_CAP_BE))
1988 return NULL;
1989
1990 curserver = NULL;
1991 if (*name == '#') {
1992 curserver = server_find_by_id(bk, atoi(name + 1));
1993 if (curserver)
1994 return curserver;
1995 }
1996 else {
1997 curserver = bk->srv;
1998
1999 while (curserver && (strcmp(curserver->id, name) != 0))
2000 curserver = curserver->next;
2001
2002 if (curserver)
2003 return curserver;
2004 }
2005
2006 return NULL;
2007}
2008
2009struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2010{
2011 struct server *byname;
2012 struct server *byid;
2013
2014 if (!name && !id)
2015 return NULL;
2016
2017 if (diff)
2018 *diff = 0;
2019
2020 byname = byid = NULL;
2021
2022 if (name) {
2023 byname = server_find_by_name(bk, name);
2024 if (byname && (!id || byname->puid == id))
2025 return byname;
2026 }
2027
2028 /* remaining possibilities :
2029 * - name not set
2030 * - name set but not found
2031 * - name found but ID doesn't match
2032 */
2033 if (id) {
2034 byid = server_find_by_id(bk, id);
2035 if (byid) {
2036 if (byname) {
2037 /* use id only if forced by configuration */
2038 if (byid->flags & SRV_F_FORCED_ID) {
2039 if (diff)
2040 *diff |= 2;
2041 return byid;
2042 }
2043 else {
2044 if (diff)
2045 *diff |= 1;
2046 return byname;
2047 }
2048 }
2049
2050 /* remaining possibilities:
2051 * - name not set
2052 * - name set but not found
2053 */
2054 if (name && diff)
2055 *diff |= 2;
2056 return byid;
2057 }
2058
2059 /* id bot found */
2060 if (byname) {
2061 if (diff)
2062 *diff |= 1;
2063 return byname;
2064 }
2065 }
2066
2067 return NULL;
2068}
2069
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002070/* Update a server state using the parameters available in the params list */
2071static void srv_update_state(struct server *srv, int version, char **params)
2072{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002073 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002074 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002075
2076 /* fields since version 1
2077 * and common to all other upcoming versions
2078 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002079 enum srv_state srv_op_state;
2080 enum srv_admin srv_admin_state;
2081 unsigned srv_uweight, srv_iweight;
2082 unsigned long srv_last_time_change;
2083 short srv_check_status;
2084 enum chk_result srv_check_result;
2085 int srv_check_health;
2086 int srv_check_state, srv_agent_state;
2087 int bk_f_forced_id;
2088 int srv_f_forced_id;
2089
Willy Tarreau31138fa2015-09-29 18:38:47 +02002090 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002091 switch (version) {
2092 case 1:
2093 /*
2094 * now we can proceed with server's state update:
2095 * srv_addr: params[0]
2096 * srv_op_state: params[1]
2097 * srv_admin_state: params[2]
2098 * srv_uweight: params[3]
2099 * srv_iweight: params[4]
2100 * srv_last_time_change: params[5]
2101 * srv_check_status: params[6]
2102 * srv_check_result: params[7]
2103 * srv_check_health: params[8]
2104 * srv_check_state: params[9]
2105 * srv_agent_state: params[10]
2106 * bk_f_forced_id: params[11]
2107 * srv_f_forced_id: params[12]
2108 */
2109
2110 /* validating srv_op_state */
2111 p = NULL;
2112 errno = 0;
2113 srv_op_state = strtol(params[1], &p, 10);
2114 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2115 (srv_op_state != SRV_ST_STOPPED &&
2116 srv_op_state != SRV_ST_STARTING &&
2117 srv_op_state != SRV_ST_RUNNING &&
2118 srv_op_state != SRV_ST_STOPPING)) {
2119 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2120 }
2121
2122 /* validating srv_admin_state */
2123 p = NULL;
2124 errno = 0;
2125 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002126
2127 /* inherited statuses will be recomputed later */
2128 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2129
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002130 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2131 (srv_admin_state != 0 &&
2132 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002133 srv_admin_state != SRV_ADMF_CMAINT &&
2134 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002135 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002136 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002137 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2138 }
2139
2140 /* validating srv_uweight */
2141 p = NULL;
2142 errno = 0;
2143 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002144 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002145 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2146
2147 /* validating srv_iweight */
2148 p = NULL;
2149 errno = 0;
2150 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002151 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002152 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2153
2154 /* validating srv_last_time_change */
2155 p = NULL;
2156 errno = 0;
2157 srv_last_time_change = strtol(params[5], &p, 10);
2158 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2159 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2160
2161 /* validating srv_check_status */
2162 p = NULL;
2163 errno = 0;
2164 srv_check_status = strtol(params[6], &p, 10);
2165 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2166 (srv_check_status >= HCHK_STATUS_SIZE))
2167 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2168
2169 /* validating srv_check_result */
2170 p = NULL;
2171 errno = 0;
2172 srv_check_result = strtol(params[7], &p, 10);
2173 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2174 (srv_check_result != CHK_RES_UNKNOWN &&
2175 srv_check_result != CHK_RES_NEUTRAL &&
2176 srv_check_result != CHK_RES_FAILED &&
2177 srv_check_result != CHK_RES_PASSED &&
2178 srv_check_result != CHK_RES_CONDPASS)) {
2179 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2180 }
2181
2182 /* validating srv_check_health */
2183 p = NULL;
2184 errno = 0;
2185 srv_check_health = strtol(params[8], &p, 10);
2186 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2187 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2188
2189 /* validating srv_check_state */
2190 p = NULL;
2191 errno = 0;
2192 srv_check_state = strtol(params[9], &p, 10);
2193 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2194 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2195 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2196
2197 /* validating srv_agent_state */
2198 p = NULL;
2199 errno = 0;
2200 srv_agent_state = strtol(params[10], &p, 10);
2201 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2202 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2203 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2204
2205 /* validating bk_f_forced_id */
2206 p = NULL;
2207 errno = 0;
2208 bk_f_forced_id = strtol(params[11], &p, 10);
2209 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2210 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2211
2212 /* validating srv_f_forced_id */
2213 p = NULL;
2214 errno = 0;
2215 srv_f_forced_id = strtol(params[12], &p, 10);
2216 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2217 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2218
2219
2220 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002221 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002222 goto out;
2223
2224 /* recover operational state and apply it to this server
2225 * and all servers tracking this one */
2226 switch (srv_op_state) {
2227 case SRV_ST_STOPPED:
2228 srv->check.health = 0;
2229 srv_set_stopped(srv, "changed from server-state after a reload");
2230 break;
2231 case SRV_ST_STARTING:
2232 srv->state = srv_op_state;
2233 break;
2234 case SRV_ST_STOPPING:
2235 srv->check.health = srv->check.rise + srv->check.fall - 1;
2236 srv_set_stopping(srv, "changed from server-state after a reload");
2237 break;
2238 case SRV_ST_RUNNING:
2239 srv->check.health = srv->check.rise + srv->check.fall - 1;
2240 srv_set_running(srv, "");
2241 break;
2242 }
2243
2244 /* When applying server state, the following rules apply:
2245 * - in case of a configuration change, we apply the setting from the new
2246 * configuration, regardless of old running state
2247 * - if no configuration change, we apply old running state only if old running
2248 * state is different from new configuration state
2249 */
2250 /* configuration has changed */
2251 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2252 if (srv->admin & SRV_ADMF_CMAINT)
2253 srv_adm_set_maint(srv);
2254 else
2255 srv_adm_set_ready(srv);
2256 }
2257 /* configuration is the same, let's compate old running state and new conf state */
2258 else {
2259 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2260 srv_adm_set_maint(srv);
2261 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2262 srv_adm_set_ready(srv);
2263 }
2264 /* apply drain mode if server is currently enabled */
2265 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2266 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002267 * (srv->iweight is the weight set up in configuration).
2268 * There are two possible reasons for FDRAIN to have been present :
2269 * - previous config weight was zero
2270 * - "set server b/s drain" was sent to the CLI
2271 *
2272 * In the first case, we simply want to drop this drain state
2273 * if the new weight is not zero anymore, meaning the administrator
2274 * has intentionally turned the weight back to a positive value to
2275 * enable the server again after an operation. In the second case,
2276 * the drain state was forced on the CLI regardless of the config's
2277 * weight so we don't want a change to the config weight to lose this
2278 * status. What this means is :
2279 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2280 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002281 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002282 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002283 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002284 }
2285
2286 srv->last_change = date.tv_sec - srv_last_time_change;
2287 srv->check.status = srv_check_status;
2288 srv->check.result = srv_check_result;
2289 srv->check.health = srv_check_health;
2290
2291 /* Only case we want to apply is removing ENABLED flag which could have been
2292 * done by the "disable health" command over the stats socket
2293 */
2294 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2295 (srv_check_state & CHK_ST_CONFIGURED) &&
2296 !(srv_check_state & CHK_ST_ENABLED))
2297 srv->check.state &= ~CHK_ST_ENABLED;
2298
2299 /* Only case we want to apply is removing ENABLED flag which could have been
2300 * done by the "disable agent" command over the stats socket
2301 */
2302 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2303 (srv_agent_state & CHK_ST_CONFIGURED) &&
2304 !(srv_agent_state & CHK_ST_ENABLED))
2305 srv->agent.state &= ~CHK_ST_ENABLED;
2306
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002307 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2308 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002309 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002310 * It means that a configuration file change has precedence over a unix socket change
2311 * for server's weight
2312 *
2313 * by default, HAProxy applies the following weight when parsing the configuration
2314 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002315 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002316 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002317 srv->uweight = srv_uweight;
2318 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002319 server_recalc_eweight(srv);
2320
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002321 /* load server IP only if DNS resolution is used on the server */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002322 if (srv->resolution) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002323 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002324 }
2325 break;
2326 default:
2327 chunk_appendf(msg, ", version '%d' not supported", version);
2328 }
2329
2330 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002331 if (msg->len) {
2332 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002333 Warning("server-state application failed for server '%s/%s'%s",
2334 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002335 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002336}
2337
2338/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2339 * For each proxy, it does the following:
2340 * - opens its server state file (either one or local one)
2341 * - read whole file, line by line
2342 * - analyse each line to check if it matches our current backend:
2343 * - backend name matches
2344 * - backend id matches if id is forced and name doesn't match
2345 * - if the server pointed by the line is found, then state is applied
2346 *
2347 * If the running backend uuid or id differs from the state file, then HAProxy reports
2348 * a warning.
2349 */
2350void apply_server_state(void)
2351{
2352 char *cur, *end;
2353 char mybuf[SRV_STATE_LINE_MAXLEN];
2354 int mybuflen;
2355 char *params[SRV_STATE_FILE_MAX_FIELDS];
2356 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2357 int arg, srv_arg, version, diff;
2358 FILE *f;
2359 char *filepath;
2360 char globalfilepath[MAXPATHLEN + 1];
2361 char localfilepath[MAXPATHLEN + 1];
2362 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2363 extern struct proxy *proxy;
2364 struct proxy *curproxy, *bk;
2365 struct server *srv;
2366
2367 globalfilepathlen = 0;
2368 /* create the globalfilepath variable */
2369 if (global.server_state_file) {
2370 /* absolute path or no base directory provided */
2371 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2372 len = strlen(global.server_state_file);
2373 if (len > MAXPATHLEN) {
2374 globalfilepathlen = 0;
2375 goto globalfileerror;
2376 }
2377 memcpy(globalfilepath, global.server_state_file, len);
2378 globalfilepath[len] = '\0';
2379 globalfilepathlen = len;
2380 }
2381 else if (global.server_state_base) {
2382 len = strlen(global.server_state_base);
2383 globalfilepathlen += len;
2384
2385 if (globalfilepathlen > MAXPATHLEN) {
2386 globalfilepathlen = 0;
2387 goto globalfileerror;
2388 }
2389 strncpy(globalfilepath, global.server_state_base, len);
2390 globalfilepath[globalfilepathlen] = 0;
2391
2392 /* append a slash if needed */
2393 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2394 if (globalfilepathlen + 1 > MAXPATHLEN) {
2395 globalfilepathlen = 0;
2396 goto globalfileerror;
2397 }
2398 globalfilepath[globalfilepathlen++] = '/';
2399 }
2400
2401 len = strlen(global.server_state_file);
2402 if (globalfilepathlen + len > MAXPATHLEN) {
2403 globalfilepathlen = 0;
2404 goto globalfileerror;
2405 }
2406 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2407 globalfilepathlen += len;
2408 globalfilepath[globalfilepathlen++] = 0;
2409 }
2410 }
2411 globalfileerror:
2412 if (globalfilepathlen == 0)
2413 globalfilepath[0] = '\0';
2414
2415 /* read servers state from local file */
2416 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2417 /* servers are only in backends */
2418 if (!(curproxy->cap & PR_CAP_BE))
2419 continue;
2420 fileopenerr = 0;
2421 filepath = NULL;
2422
2423 /* search server state file path and name */
2424 switch (curproxy->load_server_state_from_file) {
2425 /* read servers state from global file */
2426 case PR_SRV_STATE_FILE_GLOBAL:
2427 /* there was an error while generating global server state file path */
2428 if (globalfilepathlen == 0)
2429 continue;
2430 filepath = globalfilepath;
2431 fileopenerr = 1;
2432 break;
2433 /* this backend has its own file */
2434 case PR_SRV_STATE_FILE_LOCAL:
2435 localfilepathlen = 0;
2436 localfilepath[0] = '\0';
2437 len = 0;
2438 /* create the localfilepath variable */
2439 /* absolute path or no base directory provided */
2440 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2441 len = strlen(curproxy->server_state_file_name);
2442 if (len > MAXPATHLEN) {
2443 localfilepathlen = 0;
2444 goto localfileerror;
2445 }
2446 memcpy(localfilepath, curproxy->server_state_file_name, len);
2447 localfilepath[len] = '\0';
2448 localfilepathlen = len;
2449 }
2450 else if (global.server_state_base) {
2451 len = strlen(global.server_state_base);
2452 localfilepathlen += len;
2453
2454 if (localfilepathlen > MAXPATHLEN) {
2455 localfilepathlen = 0;
2456 goto localfileerror;
2457 }
2458 strncpy(localfilepath, global.server_state_base, len);
2459 localfilepath[localfilepathlen] = 0;
2460
2461 /* append a slash if needed */
2462 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2463 if (localfilepathlen + 1 > MAXPATHLEN) {
2464 localfilepathlen = 0;
2465 goto localfileerror;
2466 }
2467 localfilepath[localfilepathlen++] = '/';
2468 }
2469
2470 len = strlen(curproxy->server_state_file_name);
2471 if (localfilepathlen + len > MAXPATHLEN) {
2472 localfilepathlen = 0;
2473 goto localfileerror;
2474 }
2475 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2476 localfilepathlen += len;
2477 localfilepath[localfilepathlen++] = 0;
2478 }
2479 filepath = localfilepath;
2480 localfileerror:
2481 if (localfilepathlen == 0)
2482 localfilepath[0] = '\0';
2483
2484 break;
2485 case PR_SRV_STATE_FILE_NONE:
2486 default:
2487 continue;
2488 }
2489
2490 /* preload global state file */
2491 errno = 0;
2492 f = fopen(filepath, "r");
2493 if (errno && fileopenerr)
2494 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2495 if (!f)
2496 continue;
2497
2498 mybuf[0] = '\0';
2499 mybuflen = 0;
2500 version = 0;
2501
2502 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002503 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2504 Warning("Can't read first line of the server state file '%s'\n", filepath);
2505 goto fileclose;
2506 }
2507
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002508 cur = mybuf;
2509 version = atoi(cur);
2510 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2511 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002512 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002513
2514 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2515 int bk_f_forced_id = 0;
2516 int check_id = 0;
2517 int check_name = 0;
2518
2519 mybuflen = strlen(mybuf);
2520 cur = mybuf;
2521 end = cur + mybuflen;
2522
2523 bk = NULL;
2524 srv = NULL;
2525
2526 /* we need at least one character */
2527 if (mybuflen == 0)
2528 continue;
2529
2530 /* ignore blank characters at the beginning of the line */
2531 while (isspace(*cur))
2532 ++cur;
2533
2534 if (cur == end)
2535 continue;
2536
2537 /* ignore comment line */
2538 if (*cur == '#')
2539 continue;
2540
2541 /* we're now ready to move the line into *srv_params[] */
2542 params[0] = cur;
2543 arg = 1;
2544 srv_arg = 0;
2545 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2546 if (isspace(*cur)) {
2547 *cur = '\0';
2548 ++cur;
2549 while (isspace(*cur))
2550 ++cur;
2551 switch (version) {
2552 case 1:
2553 /*
2554 * srv_addr: params[4] => srv_params[0]
2555 * srv_op_state: params[5] => srv_params[1]
2556 * srv_admin_state: params[6] => srv_params[2]
2557 * srv_uweight: params[7] => srv_params[3]
2558 * srv_iweight: params[8] => srv_params[4]
2559 * srv_last_time_change: params[9] => srv_params[5]
2560 * srv_check_status: params[10] => srv_params[6]
2561 * srv_check_result: params[11] => srv_params[7]
2562 * srv_check_health: params[12] => srv_params[8]
2563 * srv_check_state: params[13] => srv_params[9]
2564 * srv_agent_state: params[14] => srv_params[10]
2565 * bk_f_forced_id: params[15] => srv_params[11]
2566 * srv_f_forced_id: params[16] => srv_params[12]
2567 */
2568 if (arg >= 4) {
2569 srv_params[srv_arg] = cur;
2570 ++srv_arg;
2571 }
2572 break;
2573 }
2574
2575 params[arg] = cur;
2576 ++arg;
2577 }
2578 else {
2579 ++cur;
2580 }
2581 }
2582
2583 /* if line is incomplete line, then ignore it.
2584 * otherwise, update useful flags */
2585 switch (version) {
2586 case 1:
2587 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2588 continue;
2589 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2590 check_id = (atoi(params[0]) == curproxy->uuid);
2591 check_name = (strcmp(curproxy->id, params[1]) == 0);
2592 break;
2593 }
2594
2595 diff = 0;
2596 bk = curproxy;
2597
2598 /* if backend can't be found, let's continue */
2599 if (!check_id && !check_name)
2600 continue;
2601 else if (!check_id && check_name) {
2602 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2603 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2604 }
2605 else if (check_id && !check_name) {
2606 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2607 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2608 /* if name doesn't match, we still want to update curproxy if the backend id
2609 * was forced in previous the previous configuration */
2610 if (!bk_f_forced_id)
2611 continue;
2612 }
2613
2614 /* look for the server by its id: param[2] */
2615 /* else look for the server by its name: param[3] */
2616 diff = 0;
2617 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2618
2619 if (!srv) {
2620 /* if no server found, then warning and continue with next line */
2621 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2622 params[3], params[2], params[0], params[1]);
2623 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2624 params[3], params[2], params[0], params[1]);
2625 continue;
2626 }
2627 else if (diff & PR_FBM_MISMATCH_ID) {
2628 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);
2629 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);
2630 }
2631 else if (diff & PR_FBM_MISMATCH_NAME) {
2632 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);
2633 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);
2634 }
2635
2636 /* now we can proceed with server's state update */
2637 srv_update_state(srv, version, srv_params);
2638 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002639fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002640 fclose(f);
2641 }
2642}
2643
Simon Horman7d09b9a2013-02-12 10:45:51 +09002644/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002645 * update a server's current IP address.
2646 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2647 * ip is in network format.
2648 * updater is a string which contains an information about the requester of the update.
2649 * updater is used if not NULL.
2650 *
2651 * A log line and a stderr warning message is generated based on server's backend options.
2652 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002653int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002654{
2655 /* generates a log line and a warning on stderr */
2656 if (1) {
2657 /* book enough space for both IPv4 and IPv6 */
2658 char oldip[INET6_ADDRSTRLEN];
2659 char newip[INET6_ADDRSTRLEN];
2660
2661 memset(oldip, '\0', INET6_ADDRSTRLEN);
2662 memset(newip, '\0', INET6_ADDRSTRLEN);
2663
2664 /* copy old IP address in a string */
2665 switch (s->addr.ss_family) {
2666 case AF_INET:
2667 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2668 break;
2669 case AF_INET6:
2670 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2671 break;
2672 };
2673
2674 /* copy new IP address in a string */
2675 switch (ip_sin_family) {
2676 case AF_INET:
2677 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2678 break;
2679 case AF_INET6:
2680 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2681 break;
2682 };
2683
2684 /* save log line into a buffer */
2685 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2686 s->proxy->id, s->id, oldip, newip, updater);
2687
2688 /* write the buffer on stderr */
2689 Warning("%s.\n", trash.str);
2690
2691 /* send a log */
2692 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2693 }
2694
2695 /* save the new IP family */
2696 s->addr.ss_family = ip_sin_family;
2697 /* save the new IP address */
2698 switch (ip_sin_family) {
2699 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002700 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002701 break;
2702 case AF_INET6:
2703 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2704 break;
2705 };
2706
2707 return 0;
2708}
2709
2710/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002711 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2712 *
2713 * Caller can pass its name through <updater> to get it integrated in the response message
2714 * returned by the function.
2715 *
2716 * The function first does the following, in that order:
2717 * - validates the new addr and/or port
2718 * - checks if an update is required (new IP or port is different than current ones)
2719 * - checks the update is allowed:
2720 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2721 * - allow all changes if no CHECKS are configured
2722 * - if CHECK is configured:
2723 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2724 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2725 * conditions are met
2726 */
2727const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2728{
2729 struct sockaddr_storage sa;
2730 int ret, port_change_required;
2731 char current_addr[INET6_ADDRSTRLEN];
2732 u_int16_t current_port, new_port;
2733 struct chunk *msg;
2734
2735 msg = get_trash_chunk();
2736 chunk_reset(msg);
2737
2738 if (addr) {
2739 memset(&sa, 0, sizeof(struct sockaddr_storage));
2740 if (str2ip2(addr, &sa, 0) == NULL) {
2741 chunk_printf(msg, "Invalid addr '%s'", addr);
2742 goto out;
2743 }
2744
2745 /* changes are allowed on AF_INET* families only */
2746 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2747 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2748 goto out;
2749 }
2750
2751 /* collecting data currently setup */
2752 memset(current_addr, '\0', sizeof(current_addr));
2753 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2754 /* changes are allowed on AF_INET* families only */
2755 if ((ret != AF_INET) && (ret != AF_INET6)) {
2756 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2757 goto out;
2758 }
2759
2760 /* applying ADDR changes if required and allowed
2761 * ipcmp returns 0 when both ADDR are the same
2762 */
2763 if (ipcmp(&s->addr, &sa) == 0) {
2764 chunk_appendf(msg, "no need to change the addr");
2765 goto port;
2766 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002767 ipcpy(&sa, &s->addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002768
2769 /* we also need to update check's ADDR only if it uses the server's one */
2770 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002771 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002772 }
2773
2774 /* we also need to update agent ADDR only if it use the server's one */
2775 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002776 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002777 }
2778
2779 /* update report for caller */
2780 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2781 }
2782
2783 port:
2784 if (port) {
2785 char sign = '\0';
2786 char *endptr;
2787
2788 if (addr)
2789 chunk_appendf(msg, ", ");
2790
2791 /* collecting data currently setup */
2792 current_port = get_host_port(&s->addr);
2793
2794 /* check if PORT change is required */
2795 port_change_required = 0;
2796
2797 sign = *port;
2798 new_port = strtol(port, &endptr, 10);
2799 if ((errno != 0) || (port == endptr)) {
2800 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2801 goto out;
2802 }
2803
2804 /* check if caller triggers a port mapped or offset */
2805 if (sign == '-' || (sign == '+')) {
2806 /* check if server currently uses port map */
2807 if (!(s->flags & SRV_F_MAPPORTS)) {
2808 /* switch from fixed port to port map mandatorily triggers
2809 * a port change */
2810 port_change_required = 1;
2811 /* check is configured
2812 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2813 * prevent PORT change if check doesn't have it's dedicated port while switching
2814 * to port mapping */
2815 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2816 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.");
2817 goto out;
2818 }
2819 }
2820 /* we're already using port maps */
2821 else {
2822 port_change_required = current_port != new_port;
2823 }
2824 }
2825 /* fixed port */
2826 else {
2827 port_change_required = current_port != new_port;
2828 }
2829
2830 /* applying PORT changes if required and update response message */
2831 if (port_change_required) {
2832 /* apply new port */
2833 set_host_port(&s->addr, new_port);
2834
2835 /* prepare message */
2836 chunk_appendf(msg, "port changed from '");
2837 if (s->flags & SRV_F_MAPPORTS)
2838 chunk_appendf(msg, "+");
2839 chunk_appendf(msg, "%d' to '", current_port);
2840
2841 if (sign == '-') {
2842 s->flags |= SRV_F_MAPPORTS;
2843 chunk_appendf(msg, "%c", sign);
2844 /* just use for result output */
2845 new_port = -new_port;
2846 }
2847 else if (sign == '+') {
2848 s->flags |= SRV_F_MAPPORTS;
2849 chunk_appendf(msg, "%c", sign);
2850 }
2851 else {
2852 s->flags &= ~SRV_F_MAPPORTS;
2853 }
2854
2855 chunk_appendf(msg, "%d'", new_port);
2856
2857 /* we also need to update health checks port only if it uses server's realport */
2858 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2859 s->check.port = new_port;
2860 }
2861 }
2862 else {
2863 chunk_appendf(msg, "no need to change the port");
2864 }
2865 }
2866
2867out:
2868 if (updater)
2869 chunk_appendf(msg, " by '%s'", updater);
2870 chunk_appendf(msg, "\n");
2871 return msg->str;
2872}
2873
2874
2875/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002876 * update server status based on result of name resolution
2877 * returns:
2878 * 0 if server status is updated
2879 * 1 if server status has not changed
2880 */
2881int snr_update_srv_status(struct server *s)
2882{
2883 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002884 struct dns_resolvers *resolvers;
2885
2886 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002887
2888 switch (resolution->status) {
2889 case RSLV_STATUS_NONE:
2890 /* status when HAProxy has just (re)started */
2891 trigger_resolution(s);
2892 break;
2893
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002894 case RSLV_STATUS_VALID:
2895 /*
2896 * resume health checks
2897 * server will be turned back on if health check is safe
2898 */
2899 if (!(s->admin & SRV_ADMF_RMAINT))
2900 return 1;
2901 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2902 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
2903 s->proxy->id, s->id);
2904
2905 Warning("%s.\n", trash.str);
2906 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2907 return 0;
2908
2909 case RSLV_STATUS_NX:
2910 /* stop server if resolution is NX for a long enough period */
2911 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
2912 if (s->admin & SRV_ADMF_RMAINT)
2913 return 1;
2914 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
2915 return 0;
2916 }
2917 break;
2918
2919 case RSLV_STATUS_TIMEOUT:
2920 /* stop server if resolution is TIMEOUT for a long enough period */
2921 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
2922 if (s->admin & SRV_ADMF_RMAINT)
2923 return 1;
2924 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
2925 return 0;
2926 }
2927 break;
2928
2929 case RSLV_STATUS_REFUSED:
2930 /* stop server if resolution is REFUSED for a long enough period */
2931 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
2932 if (s->admin & SRV_ADMF_RMAINT)
2933 return 1;
2934 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
2935 return 0;
2936 }
2937 break;
2938
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002939 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002940 /* stop server if resolution is in unmatched error for a long enough period */
2941 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
2942 if (s->admin & SRV_ADMF_RMAINT)
2943 return 1;
2944 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
2945 return 0;
2946 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002947 break;
2948 }
2949
2950 return 1;
2951}
2952
2953/*
2954 * Server Name Resolution valid response callback
2955 * It expects:
2956 * - <nameserver>: the name server which answered the valid response
2957 * - <response>: buffer containing a valid DNS response
2958 * - <response_len>: size of <response>
2959 * It performs the following actions:
2960 * - ignore response if current ip found and server family not met
2961 * - update with first new ip found if family is met and current IP is not found
2962 * returns:
2963 * 0 on error
2964 * 1 when no error or safe ignore
2965 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002966int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002967{
2968 struct server *s;
2969 void *serverip, *firstip;
2970 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002971 int ret;
2972 struct chunk *chk = get_trash_chunk();
2973
2974 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002975 firstip = NULL; /* pointer to the first valid response found */
2976 /* it will be used as the new IP if a change is required */
2977 firstip_sin_family = AF_UNSPEC;
2978 serverip = NULL; /* current server IP address */
2979
2980 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002981 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002982
2983 /* initializing server IP pointer */
2984 server_sin_family = s->addr.ss_family;
2985 switch (server_sin_family) {
2986 case AF_INET:
2987 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
2988 break;
2989
2990 case AF_INET6:
2991 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
2992 break;
2993
2994 default:
2995 goto invalid;
2996 }
2997
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002998 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01002999 serverip, server_sin_family, &firstip,
3000 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003001
3002 switch (ret) {
3003 case DNS_UPD_NO:
3004 if (resolution->status != RSLV_STATUS_VALID) {
3005 resolution->status = RSLV_STATUS_VALID;
3006 resolution->last_status_change = now_ms;
3007 }
3008 goto stop_resolution;
3009
3010 case DNS_UPD_SRVIP_NOT_FOUND:
3011 goto save_ip;
3012
3013 case DNS_UPD_CNAME:
3014 if (resolution->status != RSLV_STATUS_VALID) {
3015 resolution->status = RSLV_STATUS_VALID;
3016 resolution->last_status_change = now_ms;
3017 }
3018 goto invalid;
3019
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003020 case DNS_UPD_NO_IP_FOUND:
3021 if (resolution->status != RSLV_STATUS_OTHER) {
3022 resolution->status = RSLV_STATUS_OTHER;
3023 resolution->last_status_change = now_ms;
3024 }
3025 goto stop_resolution;
3026
Baptiste Assmannfad03182015-10-28 02:03:32 +01003027 case DNS_UPD_NAME_ERROR:
3028 /* if this is not the last expected response, we ignore it */
3029 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3030 return 0;
3031 /* update resolution status to OTHER error type */
3032 if (resolution->status != RSLV_STATUS_OTHER) {
3033 resolution->status = RSLV_STATUS_OTHER;
3034 resolution->last_status_change = now_ms;
3035 }
3036 goto stop_resolution;
3037
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003038 default:
3039 goto invalid;
3040
3041 }
3042
3043 save_ip:
3044 nameserver->counters.update += 1;
3045 if (resolution->status != RSLV_STATUS_VALID) {
3046 resolution->status = RSLV_STATUS_VALID;
3047 resolution->last_status_change = now_ms;
3048 }
3049
3050 /* save the first ip we found */
3051 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3052 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3053
3054 stop_resolution:
3055 /* update last resolution date and time */
3056 resolution->last_resolution = now_ms;
3057 /* reset current status flag */
3058 resolution->step = RSLV_STEP_NONE;
3059 /* reset values */
3060 dns_reset_resolution(resolution);
3061
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003062 dns_update_resolvers_timeout(nameserver->resolvers);
3063
3064 snr_update_srv_status(s);
3065 return 0;
3066
3067 invalid:
3068 nameserver->counters.invalid += 1;
3069 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3070 goto stop_resolution;
3071
3072 snr_update_srv_status(s);
3073 return 0;
3074}
3075
3076/*
3077 * Server Name Resolution error management callback
3078 * returns:
3079 * 0 on error
3080 * 1 when no error or safe ignore
3081 */
3082int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3083{
3084 struct server *s;
3085 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003086 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003087
3088 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003089 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003090 resolvers = resolution->resolvers;
3091
3092 /* can be ignored if this is not the last response */
3093 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3094 return 1;
3095 }
3096
3097 switch (error_code) {
3098 case DNS_RESP_INVALID:
3099 case DNS_RESP_WRONG_NAME:
3100 if (resolution->status != RSLV_STATUS_INVALID) {
3101 resolution->status = RSLV_STATUS_INVALID;
3102 resolution->last_status_change = now_ms;
3103 }
3104 break;
3105
3106 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003107 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003108 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003109 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003110 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003111 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3112 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003113
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003114 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003115 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003116 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003117 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003118 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003119 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003120 }
3121 else if (res_preferred_afinet) {
3122 /* fallback from A to AAAA */
3123 resolution->query_type = DNS_RTYPE_AAAA;
3124 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003125 else {
3126 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003127 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003128 resolution->query_type = DNS_RTYPE_A;
3129 } else {
3130 resolution->query_type = DNS_RTYPE_AAAA;
3131 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003132 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003133
3134 dns_send_query(resolution);
3135
3136 /*
3137 * move the resolution to the last element of the FIFO queue
3138 * and update timeout wakeup based on the new first entry
3139 */
3140 if (dns_check_resolution_queue(resolvers) > 1) {
3141 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003142 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003143 /* ex first resolution goes to the end of the queue */
3144 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3145 }
3146 dns_update_resolvers_timeout(resolvers);
3147 goto leave;
3148 }
3149 else {
3150 if (resolution->status != RSLV_STATUS_OTHER) {
3151 resolution->status = RSLV_STATUS_OTHER;
3152 resolution->last_status_change = now_ms;
3153 }
3154 }
3155 break;
3156
3157 case DNS_RESP_NX_DOMAIN:
3158 if (resolution->status != RSLV_STATUS_NX) {
3159 resolution->status = RSLV_STATUS_NX;
3160 resolution->last_status_change = now_ms;
3161 }
3162 break;
3163
3164 case DNS_RESP_REFUSED:
3165 if (resolution->status != RSLV_STATUS_REFUSED) {
3166 resolution->status = RSLV_STATUS_REFUSED;
3167 resolution->last_status_change = now_ms;
3168 }
3169 break;
3170
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003171 case DNS_RESP_TIMEOUT:
3172 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3173 resolution->status = RSLV_STATUS_TIMEOUT;
3174 resolution->last_status_change = now_ms;
3175 }
3176 break;
3177 }
3178
3179 /* update last resolution date and time */
3180 resolution->last_resolution = now_ms;
3181 /* reset current status flag */
3182 resolution->step = RSLV_STEP_NONE;
3183 /* reset values */
3184 dns_reset_resolution(resolution);
3185
3186 LIST_DEL(&resolution->list);
3187 dns_update_resolvers_timeout(resolvers);
3188
3189 leave:
3190 snr_update_srv_status(s);
3191 return 1;
3192}
3193
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003194/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3195 * resolver. This is suited for initial address configuration. Returns 0 on
3196 * success otherwise a non-zero error code. In case of error, *err_code, if
3197 * not NULL, is filled up.
3198 */
3199int srv_set_addr_via_libc(struct server *srv, int *err_code)
3200{
3201 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
3202 Alert("parsing [%s:%d] : 'server %s' : invalid address: '%s'\n",
3203 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3204 if (err_code)
3205 *err_code |= ERR_ALERT | ERR_FATAL;
3206 return 1;
3207 }
3208 return 0;
3209}
3210
3211/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3212 * from the state file. This is suited for initial address configuration.
3213 * Returns 0 on success otherwise a non-zero error code. In case of error,
3214 * *err_code, if not NULL, is filled up.
3215 */
3216static int srv_apply_lastaddr(struct server *srv, int *err_code)
3217{
3218 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3219 if (err_code)
3220 *err_code |= ERR_WARN;
3221 return 1;
3222 }
3223 return 0;
3224}
3225
Willy Tarreau25e51522016-11-04 15:10:17 +01003226/* returns 0 if no error, otherwise a combination of ERR_* flags */
3227static int srv_iterate_initaddr(struct server *srv)
3228{
3229 int return_code = 0;
3230 int err_code;
3231 unsigned int methods;
3232
3233 methods = srv->init_addr_methods;
3234 if (!methods) { // default to "last,libc"
3235 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3236 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3237 }
3238
3239 while (methods) {
3240 err_code = 0;
3241 switch (srv_get_next_initaddr(&methods)) {
3242 case SRV_IADDR_LAST:
3243 if (!srv->lastaddr)
3244 continue;
3245 if (srv_apply_lastaddr(srv, &err_code) == 0)
3246 return return_code;
3247 return_code |= err_code;
3248 break;
3249
3250 case SRV_IADDR_LIBC:
3251 if (!srv->hostname)
3252 continue;
3253 if (srv_set_addr_via_libc(srv, &err_code) == 0)
3254 return return_code;
3255 return_code |= err_code;
3256 break;
3257
3258 default: /* unhandled method */
3259 break;
3260 }
3261 }
3262
3263 if (!return_code) {
3264 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3265 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3266 }
3267
3268 return_code |= ERR_ALERT | ERR_FATAL;
3269 return return_code;
3270}
3271
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003272/*
3273 * This function parses all backends and all servers within each backend
3274 * and performs servers' addr resolution based on information provided by:
3275 * - configuration file
3276 * - server-state file (states provided by an 'old' haproxy process)
3277 *
3278 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3279 */
3280int srv_init_addr(void)
3281{
3282 struct proxy *curproxy;
3283 int return_code = 0;
3284
3285 curproxy = proxy;
3286 while (curproxy) {
3287 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003288
3289 /* servers are in backend only */
3290 if (!(curproxy->cap & PR_CAP_BE))
3291 goto srv_init_addr_next;
3292
Willy Tarreau25e51522016-11-04 15:10:17 +01003293 for (srv = curproxy->srv; srv; srv = srv->next)
3294 if (srv->hostname)
3295 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003296
3297 srv_init_addr_next:
3298 curproxy = curproxy->next;
3299 }
3300
3301 return return_code;
3302}
3303
3304
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003305/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003306 * Local variables:
3307 * c-indent-level: 8
3308 * c-basic-offset: 8
3309 * End:
3310 */