blob: c92623d481e9c4d59ef66af12bcc75c5a5acc195 [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);
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Willy Tarreau21faa912012-10-10 08:27:36 +020038/* List head of all known server keywords */
39static struct srv_kw_list srv_keywords = {
40 .list = LIST_HEAD_INIT(srv_keywords.list)
41};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020042
Simon Hormana3608442013-11-01 16:46:15 +090043int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020044{
Willy Tarreau892337c2014-05-13 23:41:20 +020045 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020046 return s->down_time;
47
48 return now.tv_sec - s->last_change + s->down_time;
49}
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050051int srv_lastsession(const struct server *s)
52{
53 if (s->counters.last_sess)
54 return now.tv_sec - s->counters.last_sess;
55
56 return -1;
57}
58
Simon Horman4a741432013-02-23 15:35:38 +090059int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020060{
Simon Horman4a741432013-02-23 15:35:38 +090061 const struct server *s = check->server;
62
Willy Tarreauff5ae352013-12-11 20:36:34 +010063 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090064 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010065
Willy Tarreau892337c2014-05-13 23:41:20 +020066 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090067 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010068
Simon Horman4a741432013-02-23 15:35:38 +090069 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010070}
71
Willy Tarreau21faa912012-10-10 08:27:36 +020072/*
73 * Registers the server keyword list <kwl> as a list of valid keywords for next
74 * parsing sessions.
75 */
76void srv_register_keywords(struct srv_kw_list *kwl)
77{
78 LIST_ADDQ(&srv_keywords.list, &kwl->list);
79}
80
81/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
82 * keyword is found with a NULL ->parse() function, then an attempt is made to
83 * find one with a valid ->parse() function. This way it is possible to declare
84 * platform-dependant, known keywords as NULL, then only declare them as valid
85 * if some options are met. Note that if the requested keyword contains an
86 * opening parenthesis, everything from this point is ignored.
87 */
88struct srv_kw *srv_find_kw(const char *kw)
89{
90 int index;
91 const char *kwend;
92 struct srv_kw_list *kwl;
93 struct srv_kw *ret = NULL;
94
95 kwend = strchr(kw, '(');
96 if (!kwend)
97 kwend = kw + strlen(kw);
98
99 list_for_each_entry(kwl, &srv_keywords.list, list) {
100 for (index = 0; kwl->kw[index].kw != NULL; index++) {
101 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
102 kwl->kw[index].kw[kwend-kw] == 0) {
103 if (kwl->kw[index].parse)
104 return &kwl->kw[index]; /* found it !*/
105 else
106 ret = &kwl->kw[index]; /* may be OK */
107 }
108 }
109 }
110 return ret;
111}
112
113/* Dumps all registered "server" keywords to the <out> string pointer. The
114 * unsupported keywords are only dumped if their supported form was not
115 * found.
116 */
117void srv_dump_kws(char **out)
118{
119 struct srv_kw_list *kwl;
120 int index;
121
122 *out = NULL;
123 list_for_each_entry(kwl, &srv_keywords.list, list) {
124 for (index = 0; kwl->kw[index].kw != NULL; index++) {
125 if (kwl->kw[index].parse ||
126 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
127 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
128 kwl->scope,
129 kwl->kw[index].kw,
130 kwl->kw[index].skip ? " <arg>" : "",
131 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
132 kwl->kw[index].parse ? "" : " (not supported)");
133 }
134 }
135 }
136}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100137
Willy Tarreaudff55432012-10-10 17:51:05 +0200138/* parse the "id" server keyword */
139static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
140{
141 struct eb32_node *node;
142
143 if (!*args[*cur_arg + 1]) {
144 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
145 return ERR_ALERT | ERR_FATAL;
146 }
147
148 newsrv->puid = atol(args[*cur_arg + 1]);
149 newsrv->conf.id.key = newsrv->puid;
150
151 if (newsrv->puid <= 0) {
152 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
153 return ERR_ALERT | ERR_FATAL;
154 }
155
156 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
157 if (node) {
158 struct server *target = container_of(node, struct server, conf.id);
159 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
160 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
161 target->id);
162 return ERR_ALERT | ERR_FATAL;
163 }
164
165 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200166 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200167 return 0;
168}
169
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200170/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200171 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200172 * shutdown.
173 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200174void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200175{
Willy Tarreau87b09662015-04-03 00:22:06 +0200176 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200177
Willy Tarreau87b09662015-04-03 00:22:06 +0200178 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
179 if (stream->srv_conn == srv)
180 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200181}
182
183/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200184 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200185 * the reason for the shutdown.
186 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200187void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200188{
189 struct server *srv;
190
191 for (srv = px->srv; srv != NULL; srv = srv->next)
192 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200193 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200194}
195
Willy Tarreaubda92272014-05-20 21:55:30 +0200196/* Appends some information to a message string related to a server going UP or
197 * DOWN. If both <forced> and <reason> are null and the server tracks another
198 * one, a "via" information will be provided to know where the status came from.
199 * If <reason> is non-null, the entire string will be appended after a comma and
200 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200201 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200202 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200203 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200204void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200205{
Willy Tarreaubda92272014-05-20 21:55:30 +0200206 if (reason)
207 chunk_appendf(msg, ", %s", reason);
208 else if (!forced && s->track)
209 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200210
211 if (xferred >= 0) {
212 if (s->state == SRV_ST_STOPPED)
213 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
214 " %d sessions active, %d requeued, %d remaining in queue",
215 s->proxy->srv_act, s->proxy->srv_bck,
216 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
217 s->cur_sess, xferred, s->nbpend);
218 else
219 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
220 " %d sessions requeued, %d total in queue",
221 s->proxy->srv_act, s->proxy->srv_bck,
222 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
223 xferred, s->nbpend);
224 }
225}
226
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200227/* Marks server <s> down, regardless of its checks' statuses, notifies by all
228 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200230 * recomputes the number of servers, but not the map. Maintenance servers are
231 * ignored. It reports <reason> if non-null as the reason for going down. Note
232 * that it makes use of the trash to build the log strings, so <reason> must
233 * not be placed there.
234 */
235void srv_set_stopped(struct server *s, const char *reason)
236{
237 struct server *srv;
238 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
239 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900240 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200241 int xferred;
242
243 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
244 return;
245
246 s->last_change = now.tv_sec;
247 s->state = SRV_ST_STOPPED;
248 if (s->proxy->lbprm.set_server_status_down)
249 s->proxy->lbprm.set_server_status_down(s);
250
251 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200252 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200253
Willy Tarreau87b09662015-04-03 00:22:06 +0200254 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200255 * a connection. Those which are redispatchable will be queued
256 * to another server or to the proxy itself.
257 */
258 xferred = pendconn_redistribute(s);
259
260 chunk_printf(&trash,
261 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
262 s->proxy->id, s->id);
263
264 srv_append_status(&trash, s, reason, xferred, 0);
265 Warning("%s.\n", trash.str);
266
267 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900268 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
269 send_log(s->proxy, log_level, "%s.\n", trash.str);
270 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200271
272 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
273 set_backend_down(s->proxy);
274
275 s->counters.down_trans++;
276
277 for (srv = s->trackers; srv; srv = srv->tracknext)
278 srv_set_stopped(srv, NULL);
279}
280
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200281/* Marks server <s> up regardless of its checks' statuses and provided it isn't
282 * in maintenance. Notifies by all available means, recounts the remaining
283 * servers on the proxy and tries to grab requests from the proxy. It
284 * automatically recomputes the number of servers, but not the map. Maintenance
285 * servers are ignored. It reports <reason> if non-null as the reason for going
286 * up. Note that it makes use of the trash to build the log strings, so <reason>
287 * must not be placed there.
288 */
289void srv_set_running(struct server *s, const char *reason)
290{
291 struct server *srv;
292 int xferred;
293
294 if (s->admin & SRV_ADMF_MAINT)
295 return;
296
297 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
298 return;
299
300 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
301 if (s->proxy->last_change < now.tv_sec) // ignore negative times
302 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
303 s->proxy->last_change = now.tv_sec;
304 }
305
306 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
307 s->down_time += now.tv_sec - s->last_change;
308
309 s->last_change = now.tv_sec;
310
311 s->state = SRV_ST_STARTING;
312 if (s->slowstart > 0)
313 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
314 else
315 s->state = SRV_ST_RUNNING;
316
317 server_recalc_eweight(s);
318
319 /* If the server is set with "on-marked-up shutdown-backup-sessions",
320 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200321 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200322 * on all backup servers.
323 */
324 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
325 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200326 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200327
328 /* check if we can handle some connections queued at the proxy. We
329 * will take as many as we can handle.
330 */
331 xferred = pendconn_grab_from_px(s);
332
333 chunk_printf(&trash,
334 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
335 s->proxy->id, s->id);
336
337 srv_append_status(&trash, s, reason, xferred, 0);
338 Warning("%s.\n", trash.str);
339 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900340 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200341
342 for (srv = s->trackers; srv; srv = srv->tracknext)
343 srv_set_running(srv, NULL);
344}
345
Willy Tarreau8eb77842014-05-21 13:54:57 +0200346/* Marks server <s> stopping regardless of its checks' statuses and provided it
347 * isn't in maintenance. Notifies by all available means, recounts the remaining
348 * servers on the proxy and tries to grab requests from the proxy. It
349 * automatically recomputes the number of servers, but not the map. Maintenance
350 * servers are ignored. It reports <reason> if non-null as the reason for going
351 * up. Note that it makes use of the trash to build the log strings, so <reason>
352 * must not be placed there.
353 */
354void srv_set_stopping(struct server *s, const char *reason)
355{
356 struct server *srv;
357 int xferred;
358
359 if (s->admin & SRV_ADMF_MAINT)
360 return;
361
362 if (s->state == SRV_ST_STOPPING)
363 return;
364
365 s->last_change = now.tv_sec;
366 s->state = SRV_ST_STOPPING;
367 if (s->proxy->lbprm.set_server_status_down)
368 s->proxy->lbprm.set_server_status_down(s);
369
Willy Tarreau87b09662015-04-03 00:22:06 +0200370 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200371 * a connection. Those which are redispatchable will be queued
372 * to another server or to the proxy itself.
373 */
374 xferred = pendconn_redistribute(s);
375
376 chunk_printf(&trash,
377 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
378 s->proxy->id, s->id);
379
380 srv_append_status(&trash, s, reason, xferred, 0);
381
382 Warning("%s.\n", trash.str);
383 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
384
385 if (!s->proxy->srv_bck && !s->proxy->srv_act)
386 set_backend_down(s->proxy);
387
388 for (srv = s->trackers; srv; srv = srv->tracknext)
389 srv_set_stopping(srv, NULL);
390}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200391
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200392/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
393 * enforce either maint mode or drain mode. It is not allowed to set more than
394 * one flag at once. The equivalent "inherited" flag is propagated to all
395 * tracking servers. Maintenance mode disables health checks (but not agent
396 * checks). When either the flag is already set or no flag is passed, nothing
397 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200398 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200399void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200400{
401 struct check *check = &s->check;
402 struct server *srv;
403 int xferred;
404
405 if (!mode)
406 return;
407
408 /* stop going down as soon as we meet a server already in the same state */
409 if (s->admin & mode)
410 return;
411
412 s->admin |= mode;
413
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200414 /* stop going down if the equivalent flag was already present (forced or inherited) */
415 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
416 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
417 return;
418
419 /* Maintenance must also disable health checks */
420 if (mode & SRV_ADMF_MAINT) {
421 if (s->check.state & CHK_ST_ENABLED) {
422 s->check.state |= CHK_ST_PAUSED;
423 check->health = 0;
424 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200425
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200426 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200427 chunk_printf(&trash,
428 "%sServer %s/%s was DOWN and now enters maintenance",
429 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
430
Willy Tarreaubda92272014-05-20 21:55:30 +0200431 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200432
433 Warning("%s.\n", trash.str);
434 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
435 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200436 else { /* server was still running */
437 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
438 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
439
440 check->health = 0; /* failure */
441 s->last_change = now.tv_sec;
442 s->state = SRV_ST_STOPPED;
443 if (s->proxy->lbprm.set_server_status_down)
444 s->proxy->lbprm.set_server_status_down(s);
445
446 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200447 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200448
Willy Tarreau87b09662015-04-03 00:22:06 +0200449 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200450 * a connection. Those which are redispatchable will be queued
451 * to another server or to the proxy itself.
452 */
453 xferred = pendconn_redistribute(s);
454
455 chunk_printf(&trash,
456 "%sServer %s/%s is going DOWN for maintenance",
457 s->flags & SRV_F_BACKUP ? "Backup " : "",
458 s->proxy->id, s->id);
459
460 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
461
462 Warning("%s.\n", trash.str);
463 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
464
465 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
466 set_backend_down(s->proxy);
467
468 s->counters.down_trans++;
469 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200470 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200471
472 /* drain state is applied only if not yet in maint */
473 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200474 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
475
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200476 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200477 if (s->proxy->lbprm.set_server_status_down)
478 s->proxy->lbprm.set_server_status_down(s);
479
Willy Tarreau87b09662015-04-03 00:22:06 +0200480 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200481 * a connection. Those which are redispatchable will be queued
482 * to another server or to the proxy itself.
483 */
484 xferred = pendconn_redistribute(s);
485
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200486 chunk_printf(&trash, "%sServer %s/%s enters drain state",
487 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200488
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200489 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200490
491 Warning("%s.\n", trash.str);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200492 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900493 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200494
495 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
496 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200497 }
498
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200499 /* compute the inherited flag to propagate */
500 if (mode & SRV_ADMF_MAINT)
501 mode = SRV_ADMF_IMAINT;
502 else if (mode & SRV_ADMF_DRAIN)
503 mode = SRV_ADMF_IDRAIN;
504
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200505 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200506 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200507}
508
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200509/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
510 * stop enforcing either maint mode or drain mode. It is not allowed to set more
511 * than one flag at once. The equivalent "inherited" flag is propagated to all
512 * tracking servers. Leaving maintenance mode re-enables health checks. When
513 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200514 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200515void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200516{
517 struct check *check = &s->check;
518 struct server *srv;
519 int xferred = -1;
520
521 if (!mode)
522 return;
523
524 /* stop going down as soon as we see the flag is not there anymore */
525 if (!(s->admin & mode))
526 return;
527
528 s->admin &= ~mode;
529
530 if (s->admin & SRV_ADMF_MAINT) {
531 /* remaining in maintenance mode, let's inform precisely about the
532 * situation.
533 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200534 if (mode & SRV_ADMF_FMAINT) {
535 chunk_printf(&trash,
536 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
537 s->flags & SRV_F_BACKUP ? "Backup " : "",
538 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200539
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200540 if (s->track) /* normally it's mandatory here */
541 chunk_appendf(&trash, " via %s/%s",
542 s->track->proxy->id, s->track->id);
543 Warning("%s.\n", trash.str);
544 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
545 }
546 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200547 chunk_printf(&trash,
548 "%sServer %s/%s remains in forced maintenance",
549 s->flags & SRV_F_BACKUP ? "Backup " : "",
550 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200551 Warning("%s.\n", trash.str);
552 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
553 }
554 /* don't report anything when leaving drain mode and remaining in maintenance */
555 }
556 else if (mode & SRV_ADMF_MAINT) {
557 /* OK here we're leaving maintenance, we have many things to check,
558 * because the server might possibly be coming back up depending on
559 * its state. In practice, leaving maintenance means that we should
560 * immediately turn to UP (more or less the slowstart) under the
561 * following conditions :
562 * - server is neither checked nor tracked
563 * - server tracks another server which is not checked
564 * - server tracks another server which is already up
565 * Which sums up as something simpler :
566 * "either the tracking server is up or the server's checks are disabled
567 * or up". Otherwise we only re-enable health checks. There's a special
568 * case associated to the stopping state which can be inherited. Note
569 * that the server might still be in drain mode, which is naturally dealt
570 * with by the lower level functions.
571 */
572
573 if (s->check.state & CHK_ST_ENABLED) {
574 s->check.state &= ~CHK_ST_PAUSED;
575 check->health = check->rise; /* start OK but check immediately */
576 }
577
578 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
579 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
580 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
581 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
582 if (s->proxy->last_change < now.tv_sec) // ignore negative times
583 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
584 s->proxy->last_change = now.tv_sec;
585 }
586
587 if (s->last_change < now.tv_sec) // ignore negative times
588 s->down_time += now.tv_sec - s->last_change;
589 s->last_change = now.tv_sec;
590
591 if (s->track && s->track->state == SRV_ST_STOPPING)
592 s->state = SRV_ST_STOPPING;
593 else {
594 s->state = SRV_ST_STARTING;
595 if (s->slowstart > 0)
596 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
597 else
598 s->state = SRV_ST_RUNNING;
599 }
600
601 server_recalc_eweight(s);
602
603 /* If the server is set with "on-marked-up shutdown-backup-sessions",
604 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200605 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200606 * on all backup servers.
607 */
608 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
609 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200610 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200611
612 /* check if we can handle some connections queued at the proxy. We
613 * will take as many as we can handle.
614 */
615 xferred = pendconn_grab_from_px(s);
616 }
617
618 if (mode & SRV_ADMF_FMAINT) {
619 chunk_printf(&trash,
620 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
621 s->flags & SRV_F_BACKUP ? "Backup " : "",
622 s->proxy->id, s->id,
623 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
624 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200625 }
626 else {
627 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200628 "%sServer %s/%s is %s/%s (leaving maintenance)",
629 s->flags & SRV_F_BACKUP ? "Backup " : "",
630 s->proxy->id, s->id,
631 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
632 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
633 srv_append_status(&trash, s, NULL, xferred, 0);
634 }
635 Warning("%s.\n", trash.str);
636 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
637 }
638 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
639 /* remaining in drain mode after removing one of its flags */
640
641 if (mode & SRV_ADMF_FDRAIN) {
642 chunk_printf(&trash,
643 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200644 s->flags & SRV_F_BACKUP ? "Backup " : "",
645 s->proxy->id, s->id);
646
647 if (s->track) /* normally it's mandatory here */
648 chunk_appendf(&trash, " via %s/%s",
649 s->track->proxy->id, s->track->id);
650 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200651 else {
652 chunk_printf(&trash,
653 "%sServer %s/%s remains in forced drain mode",
654 s->flags & SRV_F_BACKUP ? "Backup " : "",
655 s->proxy->id, s->id);
656 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200657 Warning("%s.\n", trash.str);
658 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200659 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200660 else if (mode & SRV_ADMF_DRAIN) {
661 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200662 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
663 if (s->proxy->last_change < now.tv_sec) // ignore negative times
664 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
665 s->proxy->last_change = now.tv_sec;
666 }
667
668 if (s->last_change < now.tv_sec) // ignore negative times
669 s->down_time += now.tv_sec - s->last_change;
670 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200671 server_recalc_eweight(s);
672
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200673 if (mode & SRV_ADMF_FDRAIN) {
674 chunk_printf(&trash,
675 "%sServer %s/%s is %s (leaving forced drain)",
676 s->flags & SRV_F_BACKUP ? "Backup " : "",
677 s->proxy->id, s->id,
678 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
679 }
680 else {
681 chunk_printf(&trash,
682 "%sServer %s/%s is %s (leaving drain)",
683 s->flags & SRV_F_BACKUP ? "Backup " : "",
684 s->proxy->id, s->id,
685 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
686 if (s->track) /* normally it's mandatory here */
687 chunk_appendf(&trash, " via %s/%s",
688 s->track->proxy->id, s->track->id);
689 }
690 Warning("%s.\n", trash.str);
691 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200692 }
693
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200694 /* stop going down if the equivalent flag is still present (forced or inherited) */
695 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
696 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
697 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200698
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200699 if (mode & SRV_ADMF_MAINT)
700 mode = SRV_ADMF_IMAINT;
701 else if (mode & SRV_ADMF_DRAIN)
702 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200703
704 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200705 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200706}
707
Willy Tarreaudff55432012-10-10 17:51:05 +0200708/* Note: must not be declared <const> as its list will be overwritten.
709 * Please take care of keeping this list alphabetically sorted, doing so helps
710 * all code contributors.
711 * Optional keywords are also declared with a NULL ->parse() function so that
712 * the config parser can report an appropriate error when a known keyword was
713 * not enabled.
714 */
715static struct srv_kw_list srv_kws = { "ALL", { }, {
716 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
717 { NULL, NULL, 0 },
718}};
719
720__attribute__((constructor))
721static void __listener_init(void)
722{
723 srv_register_keywords(&srv_kws);
724}
725
Willy Tarreau004e0452013-11-21 11:22:01 +0100726/* Recomputes the server's eweight based on its state, uweight, the current time,
727 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
728 * state is automatically disabled if the time is elapsed.
729 */
730void server_recalc_eweight(struct server *sv)
731{
732 struct proxy *px = sv->proxy;
733 unsigned w;
734
735 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
736 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200737 if (sv->state == SRV_ST_STARTING)
738 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100739 }
740
741 /* We must take care of not pushing the server to full throttle during slow starts.
742 * It must also start immediately, at least at the minimal step when leaving maintenance.
743 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200744 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100745 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
746 else
747 w = px->lbprm.wdiv;
748
749 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
750
751 /* now propagate the status change to any LB algorithms */
752 if (px->lbprm.update_server_eweight)
753 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200754 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100755 if (px->lbprm.set_server_status_up)
756 px->lbprm.set_server_status_up(sv);
757 }
758 else {
759 if (px->lbprm.set_server_status_down)
760 px->lbprm.set_server_status_down(sv);
761 }
762}
763
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900765 * Parses weight_str and configures sv accordingly.
766 * Returns NULL on success, error message string otherwise.
767 */
768const char *server_parse_weight_change_request(struct server *sv,
769 const char *weight_str)
770{
771 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900772 long int w;
773 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900774
775 px = sv->proxy;
776
777 /* if the weight is terminated with '%', it is set relative to
778 * the initial weight, otherwise it is absolute.
779 */
780 if (!*weight_str)
781 return "Require <weight> or <weight%>.\n";
782
Simon Hormanb796afa2013-02-12 10:45:53 +0900783 w = strtol(weight_str, &end, 10);
784 if (end == weight_str)
785 return "Empty weight string empty or preceded by garbage";
786 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900787 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900788 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900789 /* Avoid integer overflow */
790 if (w > 25600)
791 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900792 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900793 if (w > 256)
794 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900795 }
796 else if (w < 0 || w > 256)
797 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900798 else if (end[0] != '\0')
799 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900800
801 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
802 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
803
804 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100805 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900806
807 return NULL;
808}
809
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200810/*
811 * Parses <addr_str> and configures <sv> accordingly.
812 * Returns:
813 * - error string on error
814 * - NULL on success
815 */
816const char *server_parse_addr_change_request(struct server *sv,
817 const char *addr_str)
818{
819 unsigned char ip[INET6_ADDRSTRLEN];
820
821 if (inet_pton(AF_INET6, addr_str, ip)) {
822 update_server_addr(sv, ip, AF_INET6, "stats command\n");
823 return NULL;
824 }
825 if (inet_pton(AF_INET, addr_str, ip)) {
826 update_server_addr(sv, ip, AF_INET, "stats command\n");
827 return NULL;
828 }
829
830 return "Could not understand IP address format.\n";
831}
832
Willy Tarreau272adea2014-03-31 10:39:59 +0200833int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
834{
835 struct server *newsrv = NULL;
836 const char *err;
837 char *errmsg = NULL;
838 int err_code = 0;
839 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +0200840 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +0200841
842 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
843 int cur_arg;
844 short realport = 0;
845 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
846
847 if (!defsrv && curproxy == defproxy) {
848 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
849 err_code |= ERR_ALERT | ERR_FATAL;
850 goto out;
851 }
852 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
853 err_code |= ERR_ALERT | ERR_FATAL;
854
855 if (!*args[2]) {
856 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
857 file, linenum, args[0]);
858 err_code |= ERR_ALERT | ERR_FATAL;
859 goto out;
860 }
861
862 err = invalid_char(args[1]);
863 if (err && !defsrv) {
864 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
865 file, linenum, *err, args[1]);
866 err_code |= ERR_ALERT | ERR_FATAL;
867 goto out;
868 }
869
870 if (!defsrv) {
871 struct sockaddr_storage *sk;
872 int port1, port2;
873 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200874 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200875
876 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
877 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
878 err_code |= ERR_ALERT | ERR_ABORT;
879 goto out;
880 }
881
882 /* the servers are linked backwards first */
883 newsrv->next = curproxy->srv;
884 curproxy->srv = newsrv;
885 newsrv->proxy = curproxy;
886 newsrv->conf.file = strdup(file);
887 newsrv->conf.line = linenum;
888
889 newsrv->obj_type = OBJ_TYPE_SERVER;
890 LIST_INIT(&newsrv->actconns);
891 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +0200892 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +0200893 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +0200894 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +0200895 do_check = 0;
896 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200897 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200898 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200899 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200900 newsrv->last_change = now.tv_sec;
901 newsrv->id = strdup(args[1]);
902
903 /* several ways to check the port component :
904 * - IP => port=+0, relative (IPv4 only)
905 * - IP: => port=+0, relative
906 * - IP:N => port=N, absolute
907 * - IP:+N => port=+N, relative
908 * - IP:-N => port=-N, relative
909 */
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +0200910 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +0200911 if (!sk) {
912 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
913 err_code |= ERR_ALERT | ERR_FATAL;
914 goto out;
915 }
916
917 proto = protocol_by_family(sk->ss_family);
918 if (!proto || !proto->connect) {
919 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
920 file, linenum, args[0], args[1]);
921 err_code |= ERR_ALERT | ERR_FATAL;
922 goto out;
923 }
924
925 if (!port1 || !port2) {
926 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200927 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200928 }
929 else if (port1 != port2) {
930 /* port range */
931 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
932 file, linenum, args[0], args[1], args[2]);
933 err_code |= ERR_ALERT | ERR_FATAL;
934 goto out;
935 }
936 else {
937 /* used by checks */
938 realport = port1;
939 }
940
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200941 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +0200942 newsrv->hostname = fqdn;
943 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200944 goto skip_name_resolution;
945
Willy Tarreau07101d52015-09-08 16:16:35 +0200946 fqdn = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200947 if ((curr_resolution = calloc(1, sizeof(struct dns_resolution))) == NULL)
948 goto skip_name_resolution;
949
950 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
951 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
952 goto skip_name_resolution;
953 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
954 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
955 file, linenum, args[2]);
956 err_code |= ERR_ALERT | ERR_FATAL;
957 goto out;
958 }
959
960 curr_resolution->requester = newsrv;
961 curr_resolution->requester_cb = snr_resolution_cb;
962 curr_resolution->requester_error_cb = snr_resolution_error_cb;
963 curr_resolution->status = RSLV_STATUS_NONE;
964 curr_resolution->step = RSLV_STEP_NONE;
965 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +0200966 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200967 newsrv->resolution = curr_resolution;
968
969 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +0200970 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +0100971 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200972
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +0100973 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200974 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
975 file, linenum, newsrv->addr.ss_family, args[2]);
976 err_code |= ERR_ALERT | ERR_FATAL;
977 goto out;
978 }
979
980 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
981 newsrv->check.port = curproxy->defsrv.check.port;
982 newsrv->check.inter = curproxy->defsrv.check.inter;
983 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
984 newsrv->check.downinter = curproxy->defsrv.check.downinter;
985 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
986 newsrv->agent.port = curproxy->defsrv.agent.port;
987 newsrv->agent.inter = curproxy->defsrv.agent.inter;
988 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
989 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
990 newsrv->maxqueue = curproxy->defsrv.maxqueue;
991 newsrv->minconn = curproxy->defsrv.minconn;
992 newsrv->maxconn = curproxy->defsrv.maxconn;
993 newsrv->slowstart = curproxy->defsrv.slowstart;
994 newsrv->onerror = curproxy->defsrv.onerror;
995 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
996 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
997 newsrv->consecutive_errors_limit
998 = curproxy->defsrv.consecutive_errors_limit;
999#ifdef OPENSSL
1000 newsrv->use_ssl = curproxy->defsrv.use_ssl;
1001#endif
1002 newsrv->uweight = newsrv->iweight
1003 = curproxy->defsrv.iweight;
1004
1005 newsrv->check.status = HCHK_STATUS_INI;
1006 newsrv->check.rise = curproxy->defsrv.check.rise;
1007 newsrv->check.fall = curproxy->defsrv.check.fall;
1008 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1009 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001010 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001011
1012 newsrv->agent.status = HCHK_STATUS_INI;
1013 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1014 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1015 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1016 newsrv->agent.server = newsrv;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001017 newsrv->resolver_family_priority = curproxy->defsrv.resolver_family_priority;
1018 if (newsrv->resolver_family_priority == AF_UNSPEC)
1019 newsrv->resolver_family_priority = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001020
1021 cur_arg = 3;
1022 } else {
1023 newsrv = &curproxy->defsrv;
1024 cur_arg = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001025 newsrv->resolver_family_priority = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001026 }
1027
1028 while (*args[cur_arg]) {
1029 if (!strcmp(args[cur_arg], "agent-check")) {
1030 global.maxsock++;
1031 do_agent = 1;
1032 cur_arg += 1;
1033 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1034 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1035 if (err) {
1036 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1037 file, linenum, *err, newsrv->id);
1038 err_code |= ERR_ALERT | ERR_FATAL;
1039 goto out;
1040 }
1041 if (val <= 0) {
1042 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1043 file, linenum, val, args[cur_arg], newsrv->id);
1044 err_code |= ERR_ALERT | ERR_FATAL;
1045 goto out;
1046 }
1047 newsrv->agent.inter = val;
1048 cur_arg += 2;
1049 }
1050 else if (!strcmp(args[cur_arg], "agent-port")) {
1051 global.maxsock++;
1052 newsrv->agent.port = atol(args[cur_arg + 1]);
1053 cur_arg += 2;
1054 }
1055 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1056 newsrv->cookie = strdup(args[cur_arg + 1]);
1057 newsrv->cklen = strlen(args[cur_arg + 1]);
1058 cur_arg += 2;
1059 }
1060 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1061 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1062 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1063 cur_arg += 2;
1064 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001065 else if (!strcmp(args[cur_arg], "resolvers")) {
1066 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1067 cur_arg += 2;
1068 }
1069 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1070 if (!strcmp(args[cur_arg + 1], "ipv4"))
1071 newsrv->resolver_family_priority = AF_INET;
1072 else if (!strcmp(args[cur_arg + 1], "ipv6"))
1073 newsrv->resolver_family_priority = AF_INET6;
1074 else {
1075 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1076 file, linenum, args[cur_arg]);
1077 err_code |= ERR_ALERT | ERR_FATAL;
1078 goto out;
1079 }
1080 cur_arg += 2;
1081 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001082 else if (!strcmp(args[cur_arg], "rise")) {
1083 if (!*args[cur_arg + 1]) {
1084 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1085 file, linenum, args[cur_arg]);
1086 err_code |= ERR_ALERT | ERR_FATAL;
1087 goto out;
1088 }
1089
1090 newsrv->check.rise = atol(args[cur_arg + 1]);
1091 if (newsrv->check.rise <= 0) {
1092 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1093 file, linenum, args[cur_arg]);
1094 err_code |= ERR_ALERT | ERR_FATAL;
1095 goto out;
1096 }
1097
1098 if (newsrv->check.health)
1099 newsrv->check.health = newsrv->check.rise;
1100 cur_arg += 2;
1101 }
1102 else if (!strcmp(args[cur_arg], "fall")) {
1103 newsrv->check.fall = atol(args[cur_arg + 1]);
1104
1105 if (!*args[cur_arg + 1]) {
1106 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1107 file, linenum, args[cur_arg]);
1108 err_code |= ERR_ALERT | ERR_FATAL;
1109 goto out;
1110 }
1111
1112 if (newsrv->check.fall <= 0) {
1113 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1114 file, linenum, args[cur_arg]);
1115 err_code |= ERR_ALERT | ERR_FATAL;
1116 goto out;
1117 }
1118
1119 cur_arg += 2;
1120 }
1121 else if (!strcmp(args[cur_arg], "inter")) {
1122 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1123 if (err) {
1124 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1125 file, linenum, *err, newsrv->id);
1126 err_code |= ERR_ALERT | ERR_FATAL;
1127 goto out;
1128 }
1129 if (val <= 0) {
1130 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1131 file, linenum, val, args[cur_arg], newsrv->id);
1132 err_code |= ERR_ALERT | ERR_FATAL;
1133 goto out;
1134 }
1135 newsrv->check.inter = val;
1136 cur_arg += 2;
1137 }
1138 else if (!strcmp(args[cur_arg], "fastinter")) {
1139 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1140 if (err) {
1141 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1142 file, linenum, *err, newsrv->id);
1143 err_code |= ERR_ALERT | ERR_FATAL;
1144 goto out;
1145 }
1146 if (val <= 0) {
1147 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1148 file, linenum, val, args[cur_arg], newsrv->id);
1149 err_code |= ERR_ALERT | ERR_FATAL;
1150 goto out;
1151 }
1152 newsrv->check.fastinter = val;
1153 cur_arg += 2;
1154 }
1155 else if (!strcmp(args[cur_arg], "downinter")) {
1156 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1157 if (err) {
1158 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1159 file, linenum, *err, newsrv->id);
1160 err_code |= ERR_ALERT | ERR_FATAL;
1161 goto out;
1162 }
1163 if (val <= 0) {
1164 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1165 file, linenum, val, args[cur_arg], newsrv->id);
1166 err_code |= ERR_ALERT | ERR_FATAL;
1167 goto out;
1168 }
1169 newsrv->check.downinter = val;
1170 cur_arg += 2;
1171 }
1172 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1173 struct sockaddr_storage *sk;
1174 int port1, port2;
1175 struct protocol *proto;
1176
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001177 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001178 if (!sk) {
1179 Alert("parsing [%s:%d] : '%s' : %s\n",
1180 file, linenum, args[cur_arg], errmsg);
1181 err_code |= ERR_ALERT | ERR_FATAL;
1182 goto out;
1183 }
1184
1185 proto = protocol_by_family(sk->ss_family);
1186 if (!proto || !proto->connect) {
1187 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1188 file, linenum, args[cur_arg], args[cur_arg + 1]);
1189 err_code |= ERR_ALERT | ERR_FATAL;
1190 goto out;
1191 }
1192
1193 if (port1 != port2) {
1194 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1195 file, linenum, args[cur_arg], args[cur_arg + 1]);
1196 err_code |= ERR_ALERT | ERR_FATAL;
1197 goto out;
1198 }
1199
Simon Horman41f58762015-01-30 11:22:56 +09001200 newsrv->check.addr = newsrv->agent.addr = *sk;
Willy Tarreau272adea2014-03-31 10:39:59 +02001201 cur_arg += 2;
1202 }
1203 else if (!strcmp(args[cur_arg], "port")) {
1204 newsrv->check.port = atol(args[cur_arg + 1]);
1205 cur_arg += 2;
1206 }
1207 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001208 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001209 cur_arg ++;
1210 }
1211 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001212 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001213 cur_arg ++;
1214 }
1215 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001216 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001217 cur_arg ++;
1218 }
David Safb76832014-05-08 23:42:08 -04001219 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1220 newsrv->pp_opts |= SRV_PP_V2;
1221 cur_arg ++;
1222 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001223 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1224 newsrv->check.send_proxy = 1;
1225 cur_arg ++;
1226 }
1227 else if (!strcmp(args[cur_arg], "weight")) {
1228 int w;
1229 w = atol(args[cur_arg + 1]);
1230 if (w < 0 || w > SRV_UWGHT_MAX) {
1231 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1232 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1233 err_code |= ERR_ALERT | ERR_FATAL;
1234 goto out;
1235 }
1236 newsrv->uweight = newsrv->iweight = w;
1237 cur_arg += 2;
1238 }
1239 else if (!strcmp(args[cur_arg], "minconn")) {
1240 newsrv->minconn = atol(args[cur_arg + 1]);
1241 cur_arg += 2;
1242 }
1243 else if (!strcmp(args[cur_arg], "maxconn")) {
1244 newsrv->maxconn = atol(args[cur_arg + 1]);
1245 cur_arg += 2;
1246 }
1247 else if (!strcmp(args[cur_arg], "maxqueue")) {
1248 newsrv->maxqueue = atol(args[cur_arg + 1]);
1249 cur_arg += 2;
1250 }
1251 else if (!strcmp(args[cur_arg], "slowstart")) {
1252 /* slowstart is stored in seconds */
1253 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1254 if (err) {
1255 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1256 file, linenum, *err, newsrv->id);
1257 err_code |= ERR_ALERT | ERR_FATAL;
1258 goto out;
1259 }
1260 newsrv->slowstart = (val + 999) / 1000;
1261 cur_arg += 2;
1262 }
1263 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1264
1265 if (!*args[cur_arg + 1]) {
1266 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1267 file, linenum);
1268 err_code |= ERR_ALERT | ERR_FATAL;
1269 goto out;
1270 }
1271
1272 newsrv->trackit = strdup(args[cur_arg + 1]);
1273
1274 cur_arg += 2;
1275 }
1276 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1277 global.maxsock++;
1278 do_check = 1;
1279 cur_arg += 1;
1280 }
1281 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001282 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001283 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001284 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001285 newsrv->check.state |= CHK_ST_PAUSED;
1286 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001287 cur_arg += 1;
1288 }
1289 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1290 if (!strcmp(args[cur_arg + 1], "none"))
1291 newsrv->observe = HANA_OBS_NONE;
1292 else if (!strcmp(args[cur_arg + 1], "layer4"))
1293 newsrv->observe = HANA_OBS_LAYER4;
1294 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1295 if (curproxy->mode != PR_MODE_HTTP) {
1296 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1297 file, linenum, args[cur_arg + 1]);
1298 err_code |= ERR_ALERT;
1299 }
1300 newsrv->observe = HANA_OBS_LAYER7;
1301 }
1302 else {
1303 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1304 "'layer4', 'layer7' but got '%s'\n",
1305 file, linenum, args[cur_arg], args[cur_arg + 1]);
1306 err_code |= ERR_ALERT | ERR_FATAL;
1307 goto out;
1308 }
1309
1310 cur_arg += 2;
1311 }
1312 else if (!strcmp(args[cur_arg], "on-error")) {
1313 if (!strcmp(args[cur_arg + 1], "fastinter"))
1314 newsrv->onerror = HANA_ONERR_FASTINTER;
1315 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1316 newsrv->onerror = HANA_ONERR_FAILCHK;
1317 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1318 newsrv->onerror = HANA_ONERR_SUDDTH;
1319 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1320 newsrv->onerror = HANA_ONERR_MARKDWN;
1321 else {
1322 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1323 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1324 file, linenum, args[cur_arg], args[cur_arg + 1]);
1325 err_code |= ERR_ALERT | ERR_FATAL;
1326 goto out;
1327 }
1328
1329 cur_arg += 2;
1330 }
1331 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1332 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1333 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1334 else {
1335 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1336 file, linenum, args[cur_arg], args[cur_arg + 1]);
1337 err_code |= ERR_ALERT | ERR_FATAL;
1338 goto out;
1339 }
1340
1341 cur_arg += 2;
1342 }
1343 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1344 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1345 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1346 else {
1347 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1348 file, linenum, args[cur_arg], args[cur_arg + 1]);
1349 err_code |= ERR_ALERT | ERR_FATAL;
1350 goto out;
1351 }
1352
1353 cur_arg += 2;
1354 }
1355 else if (!strcmp(args[cur_arg], "error-limit")) {
1356 if (!*args[cur_arg + 1]) {
1357 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1358 file, linenum, args[cur_arg]);
1359 err_code |= ERR_ALERT | ERR_FATAL;
1360 goto out;
1361 }
1362
1363 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1364
1365 if (newsrv->consecutive_errors_limit <= 0) {
1366 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1367 file, linenum, args[cur_arg]);
1368 err_code |= ERR_ALERT | ERR_FATAL;
1369 goto out;
1370 }
1371 cur_arg += 2;
1372 }
1373 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1374 int port_low, port_high;
1375 struct sockaddr_storage *sk;
1376 struct protocol *proto;
1377
1378 if (!*args[cur_arg + 1]) {
1379 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1380 file, linenum, "source", "usesrc", "interface");
1381 err_code |= ERR_ALERT | ERR_FATAL;
1382 goto out;
1383 }
1384
1385 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001386 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001387 if (!sk) {
1388 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1389 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1390 err_code |= ERR_ALERT | ERR_FATAL;
1391 goto out;
1392 }
1393
1394 proto = protocol_by_family(sk->ss_family);
1395 if (!proto || !proto->connect) {
1396 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1397 file, linenum, args[cur_arg], args[cur_arg+1]);
1398 err_code |= ERR_ALERT | ERR_FATAL;
1399 goto out;
1400 }
1401
1402 newsrv->conn_src.source_addr = *sk;
1403
1404 if (port_low != port_high) {
1405 int i;
1406
1407 if (!port_low || !port_high) {
1408 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1409 file, linenum, args[cur_arg], args[cur_arg + 1]);
1410 err_code |= ERR_ALERT | ERR_FATAL;
1411 goto out;
1412 }
1413
1414 if (port_low <= 0 || port_low > 65535 ||
1415 port_high <= 0 || port_high > 65535 ||
1416 port_low > port_high) {
1417 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1418 file, linenum, port_low, port_high);
1419 err_code |= ERR_ALERT | ERR_FATAL;
1420 goto out;
1421 }
1422 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1423 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1424 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1425 }
1426
1427 cur_arg += 2;
1428 while (*(args[cur_arg])) {
1429 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001430#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001431 if (!*args[cur_arg + 1]) {
1432 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1433 file, linenum, "usesrc");
1434 err_code |= ERR_ALERT | ERR_FATAL;
1435 goto out;
1436 }
1437 if (!strcmp(args[cur_arg + 1], "client")) {
1438 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1439 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1440 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1441 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1442 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1443 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1444 char *name, *end;
1445
1446 name = args[cur_arg+1] + 7;
1447 while (isspace(*name))
1448 name++;
1449
1450 end = name;
1451 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1452 end++;
1453
1454 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1455 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1456 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1457 newsrv->conn_src.bind_hdr_len = end - name;
1458 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1459 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1460 newsrv->conn_src.bind_hdr_occ = -1;
1461
1462 /* now look for an occurrence number */
1463 while (isspace(*end))
1464 end++;
1465 if (*end == ',') {
1466 end++;
1467 name = end;
1468 if (*end == '-')
1469 end++;
1470 while (isdigit((int)*end))
1471 end++;
1472 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1473 }
1474
1475 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1476 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1477 " occurrences values smaller than %d.\n",
1478 file, linenum, MAX_HDR_HISTORY);
1479 err_code |= ERR_ALERT | ERR_FATAL;
1480 goto out;
1481 }
1482 } else {
1483 struct sockaddr_storage *sk;
1484 int port1, port2;
1485
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001486 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001487 if (!sk) {
1488 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1489 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1490 err_code |= ERR_ALERT | ERR_FATAL;
1491 goto out;
1492 }
1493
1494 proto = protocol_by_family(sk->ss_family);
1495 if (!proto || !proto->connect) {
1496 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1497 file, linenum, args[cur_arg], args[cur_arg+1]);
1498 err_code |= ERR_ALERT | ERR_FATAL;
1499 goto out;
1500 }
1501
1502 if (port1 != port2) {
1503 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1504 file, linenum, args[cur_arg], args[cur_arg + 1]);
1505 err_code |= ERR_ALERT | ERR_FATAL;
1506 goto out;
1507 }
1508 newsrv->conn_src.tproxy_addr = *sk;
1509 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1510 }
1511 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001512 cur_arg += 2;
1513 continue;
1514#else /* no TPROXY support */
1515 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1516 file, linenum, "usesrc");
1517 err_code |= ERR_ALERT | ERR_FATAL;
1518 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001519#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001520 } /* "usesrc" */
1521
1522 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1523#ifdef SO_BINDTODEVICE
1524 if (!*args[cur_arg + 1]) {
1525 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1526 file, linenum, args[0]);
1527 err_code |= ERR_ALERT | ERR_FATAL;
1528 goto out;
1529 }
1530 free(newsrv->conn_src.iface_name);
1531 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1532 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1533 global.last_checks |= LSTCHK_NETADM;
1534#else
1535 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1536 file, linenum, args[0], args[cur_arg]);
1537 err_code |= ERR_ALERT | ERR_FATAL;
1538 goto out;
1539#endif
1540 cur_arg += 2;
1541 continue;
1542 }
1543 /* this keyword in not an option of "source" */
1544 break;
1545 } /* while */
1546 }
1547 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1548 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1549 file, linenum, "usesrc", "source");
1550 err_code |= ERR_ALERT | ERR_FATAL;
1551 goto out;
1552 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001553 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1554#ifdef CONFIG_HAP_NS
1555 char *arg = args[cur_arg + 1];
1556 if (!strcmp(arg, "*")) {
1557 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1558 } else {
1559 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1560
1561 if (newsrv->netns == NULL)
1562 newsrv->netns = netns_store_insert(arg);
1563
1564 if (newsrv->netns == NULL) {
1565 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1566 err_code |= ERR_ALERT | ERR_FATAL;
1567 goto out;
1568 }
1569 }
1570#else
1571 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1572 file, linenum, args[0], args[cur_arg]);
1573 err_code |= ERR_ALERT | ERR_FATAL;
1574 goto out;
1575#endif
1576 cur_arg += 2;
1577 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001578 else {
1579 static int srv_dumped;
1580 struct srv_kw *kw;
1581 char *err;
1582
1583 kw = srv_find_kw(args[cur_arg]);
1584 if (kw) {
1585 char *err = NULL;
1586 int code;
1587
1588 if (!kw->parse) {
1589 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1590 file, linenum, args[0], args[1], args[cur_arg]);
1591 cur_arg += 1 + kw->skip ;
1592 err_code |= ERR_ALERT | ERR_FATAL;
1593 goto out;
1594 }
1595
1596 if (defsrv && !kw->default_ok) {
1597 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1598 file, linenum, args[0], args[1], args[cur_arg]);
1599 cur_arg += 1 + kw->skip ;
1600 err_code |= ERR_ALERT;
1601 continue;
1602 }
1603
1604 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1605 err_code |= code;
1606
1607 if (code) {
1608 if (err && *err) {
1609 indent_msg(&err, 2);
1610 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1611 }
1612 else
1613 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1614 file, linenum, args[0], args[1], args[cur_arg]);
1615 if (code & ERR_FATAL) {
1616 free(err);
1617 cur_arg += 1 + kw->skip;
1618 goto out;
1619 }
1620 }
1621 free(err);
1622 cur_arg += 1 + kw->skip;
1623 continue;
1624 }
1625
1626 err = NULL;
1627 if (!srv_dumped) {
1628 srv_dump_kws(&err);
1629 indent_msg(&err, 4);
1630 srv_dumped = 1;
1631 }
1632
1633 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1634 file, linenum, args[0], args[1], args[cur_arg],
1635 err ? " Registered keywords :" : "", err ? err : "");
1636 free(err);
1637
1638 err_code |= ERR_ALERT | ERR_FATAL;
1639 goto out;
1640 }
1641 }
1642
Willy Tarreau272adea2014-03-31 10:39:59 +02001643 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001644 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001645
1646 if (newsrv->trackit) {
1647 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1648 file, linenum);
1649 err_code |= ERR_ALERT | ERR_FATAL;
1650 goto out;
1651 }
1652
1653 /* If neither a port nor an addr was specified and no check transport
1654 * layer is forced, then the transport layer used by the checks is the
1655 * same as for the production traffic. Otherwise we use raw_sock by
1656 * default, unless one is specified.
1657 */
Simon Horman41f58762015-01-30 11:22:56 +09001658 if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001659#ifdef USE_OPENSSL
1660 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1661#endif
David Safb76832014-05-08 23:42:08 -04001662 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001663 }
1664 /* try to get the port from check_core.addr if check.port not set */
1665 if (!newsrv->check.port)
Simon Horman41f58762015-01-30 11:22:56 +09001666 newsrv->check.port = get_host_port(&newsrv->check.addr);
Willy Tarreau272adea2014-03-31 10:39:59 +02001667
1668 if (!newsrv->check.port)
1669 newsrv->check.port = realport; /* by default */
1670
1671 if (!newsrv->check.port) {
1672 /* not yet valid, because no port was set on
1673 * the server either. We'll check if we have
1674 * a known port on the first listener.
1675 */
1676 struct listener *l;
1677
1678 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1679 newsrv->check.port = get_host_port(&l->addr);
1680 if (newsrv->check.port)
1681 break;
1682 }
1683 }
1684 /*
1685 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001686 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001687 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001688 if (!newsrv->check.port &&
Simon Horman41f58762015-01-30 11:22:56 +09001689 (is_inet_addr(&newsrv->check.addr) ||
1690 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001691 struct tcpcheck_rule *n = NULL, *r = NULL;
1692 struct list *l;
1693
1694 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1695 if (!r) {
1696 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1697 file, linenum, newsrv->id);
1698 err_code |= ERR_ALERT | ERR_FATAL;
1699 goto out;
1700 }
1701 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1702 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",
1703 file, linenum, newsrv->id);
1704 err_code |= ERR_ALERT | ERR_FATAL;
1705 goto out;
1706 }
1707 else {
1708 /* scan the tcp-check ruleset to ensure a port has been configured */
1709 l = &newsrv->proxy->tcpcheck_rules;
1710 list_for_each_entry(n, l, list) {
1711 r = (struct tcpcheck_rule *)n->list.p;
1712 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1713 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",
1714 file, linenum, newsrv->id);
1715 err_code |= ERR_ALERT | ERR_FATAL;
1716 goto out;
1717 }
1718 }
1719 }
1720 }
1721
1722 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001723 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001724 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001725 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1726 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001727 goto out;
1728 }
1729
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001730 if (newsrv->resolution)
1731 newsrv->resolution->resolver_family_priority = newsrv->resolver_family_priority;
1732
Willy Tarreau272adea2014-03-31 10:39:59 +02001733 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1734 }
1735
1736 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001737 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001738
1739 if (!newsrv->agent.port) {
1740 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1741 file, linenum, newsrv->id);
1742 err_code |= ERR_ALERT | ERR_FATAL;
1743 goto out;
1744 }
1745
1746 if (!newsrv->agent.inter)
1747 newsrv->agent.inter = newsrv->check.inter;
1748
Simon Hormanb1900d52015-01-30 11:22:54 +09001749 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001750 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001751 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1752 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001753 goto out;
1754 }
1755
1756 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1757 }
1758
1759 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001760 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001761 curproxy->srv_bck++;
1762 else
1763 curproxy->srv_act++;
1764
Willy Tarreauc5150da2014-05-13 19:27:31 +02001765 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001766 }
1767 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001768 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001769 return 0;
1770
1771 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001772 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001773 free(errmsg);
1774 return err_code;
1775}
1776
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001777/* Returns a pointer to the first server matching either id <id>.
1778 * NULL is returned if no match is found.
1779 * the lookup is performed in the backend <bk>
1780 */
1781struct server *server_find_by_id(struct proxy *bk, int id)
1782{
1783 struct eb32_node *eb32;
1784 struct server *curserver;
1785
1786 if (!bk || (id ==0))
1787 return NULL;
1788
1789 /* <bk> has no backend capabilities, so it can't have a server */
1790 if (!(bk->cap & PR_CAP_BE))
1791 return NULL;
1792
1793 curserver = NULL;
1794
1795 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1796 if (eb32)
1797 curserver = container_of(eb32, struct server, conf.id);
1798
1799 return curserver;
1800}
1801
1802/* Returns a pointer to the first server matching either name <name>, or id
1803 * if <name> starts with a '#'. NULL is returned if no match is found.
1804 * the lookup is performed in the backend <bk>
1805 */
1806struct server *server_find_by_name(struct proxy *bk, const char *name)
1807{
1808 struct server *curserver;
1809
1810 if (!bk || !name)
1811 return NULL;
1812
1813 /* <bk> has no backend capabilities, so it can't have a server */
1814 if (!(bk->cap & PR_CAP_BE))
1815 return NULL;
1816
1817 curserver = NULL;
1818 if (*name == '#') {
1819 curserver = server_find_by_id(bk, atoi(name + 1));
1820 if (curserver)
1821 return curserver;
1822 }
1823 else {
1824 curserver = bk->srv;
1825
1826 while (curserver && (strcmp(curserver->id, name) != 0))
1827 curserver = curserver->next;
1828
1829 if (curserver)
1830 return curserver;
1831 }
1832
1833 return NULL;
1834}
1835
1836struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
1837{
1838 struct server *byname;
1839 struct server *byid;
1840
1841 if (!name && !id)
1842 return NULL;
1843
1844 if (diff)
1845 *diff = 0;
1846
1847 byname = byid = NULL;
1848
1849 if (name) {
1850 byname = server_find_by_name(bk, name);
1851 if (byname && (!id || byname->puid == id))
1852 return byname;
1853 }
1854
1855 /* remaining possibilities :
1856 * - name not set
1857 * - name set but not found
1858 * - name found but ID doesn't match
1859 */
1860 if (id) {
1861 byid = server_find_by_id(bk, id);
1862 if (byid) {
1863 if (byname) {
1864 /* use id only if forced by configuration */
1865 if (byid->flags & SRV_F_FORCED_ID) {
1866 if (diff)
1867 *diff |= 2;
1868 return byid;
1869 }
1870 else {
1871 if (diff)
1872 *diff |= 1;
1873 return byname;
1874 }
1875 }
1876
1877 /* remaining possibilities:
1878 * - name not set
1879 * - name set but not found
1880 */
1881 if (name && diff)
1882 *diff |= 2;
1883 return byid;
1884 }
1885
1886 /* id bot found */
1887 if (byname) {
1888 if (diff)
1889 *diff |= 1;
1890 return byname;
1891 }
1892 }
1893
1894 return NULL;
1895}
1896
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001897/* Update a server state using the parameters available in the params list */
1898static void srv_update_state(struct server *srv, int version, char **params)
1899{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001900 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02001901 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001902
1903 /* fields since version 1
1904 * and common to all other upcoming versions
1905 */
1906 struct sockaddr_storage addr;
1907 enum srv_state srv_op_state;
1908 enum srv_admin srv_admin_state;
1909 unsigned srv_uweight, srv_iweight;
1910 unsigned long srv_last_time_change;
1911 short srv_check_status;
1912 enum chk_result srv_check_result;
1913 int srv_check_health;
1914 int srv_check_state, srv_agent_state;
1915 int bk_f_forced_id;
1916 int srv_f_forced_id;
1917
Willy Tarreau31138fa2015-09-29 18:38:47 +02001918 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001919 switch (version) {
1920 case 1:
1921 /*
1922 * now we can proceed with server's state update:
1923 * srv_addr: params[0]
1924 * srv_op_state: params[1]
1925 * srv_admin_state: params[2]
1926 * srv_uweight: params[3]
1927 * srv_iweight: params[4]
1928 * srv_last_time_change: params[5]
1929 * srv_check_status: params[6]
1930 * srv_check_result: params[7]
1931 * srv_check_health: params[8]
1932 * srv_check_state: params[9]
1933 * srv_agent_state: params[10]
1934 * bk_f_forced_id: params[11]
1935 * srv_f_forced_id: params[12]
1936 */
1937
1938 /* validating srv_op_state */
1939 p = NULL;
1940 errno = 0;
1941 srv_op_state = strtol(params[1], &p, 10);
1942 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
1943 (srv_op_state != SRV_ST_STOPPED &&
1944 srv_op_state != SRV_ST_STARTING &&
1945 srv_op_state != SRV_ST_RUNNING &&
1946 srv_op_state != SRV_ST_STOPPING)) {
1947 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
1948 }
1949
1950 /* validating srv_admin_state */
1951 p = NULL;
1952 errno = 0;
1953 srv_admin_state = strtol(params[2], &p, 10);
1954 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
1955 (srv_admin_state != 0 &&
1956 srv_admin_state != SRV_ADMF_FMAINT &&
1957 srv_admin_state != SRV_ADMF_IMAINT &&
1958 srv_admin_state != SRV_ADMF_CMAINT &&
1959 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
1960 srv_admin_state != SRV_ADMF_FDRAIN &&
1961 srv_admin_state != SRV_ADMF_IDRAIN)) {
1962 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
1963 }
1964
1965 /* validating srv_uweight */
1966 p = NULL;
1967 errno = 0;
1968 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02001969 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001970 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
1971
1972 /* validating srv_iweight */
1973 p = NULL;
1974 errno = 0;
1975 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02001976 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001977 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
1978
1979 /* validating srv_last_time_change */
1980 p = NULL;
1981 errno = 0;
1982 srv_last_time_change = strtol(params[5], &p, 10);
1983 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
1984 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
1985
1986 /* validating srv_check_status */
1987 p = NULL;
1988 errno = 0;
1989 srv_check_status = strtol(params[6], &p, 10);
1990 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
1991 (srv_check_status >= HCHK_STATUS_SIZE))
1992 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
1993
1994 /* validating srv_check_result */
1995 p = NULL;
1996 errno = 0;
1997 srv_check_result = strtol(params[7], &p, 10);
1998 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
1999 (srv_check_result != CHK_RES_UNKNOWN &&
2000 srv_check_result != CHK_RES_NEUTRAL &&
2001 srv_check_result != CHK_RES_FAILED &&
2002 srv_check_result != CHK_RES_PASSED &&
2003 srv_check_result != CHK_RES_CONDPASS)) {
2004 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2005 }
2006
2007 /* validating srv_check_health */
2008 p = NULL;
2009 errno = 0;
2010 srv_check_health = strtol(params[8], &p, 10);
2011 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2012 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2013
2014 /* validating srv_check_state */
2015 p = NULL;
2016 errno = 0;
2017 srv_check_state = strtol(params[9], &p, 10);
2018 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2019 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2020 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2021
2022 /* validating srv_agent_state */
2023 p = NULL;
2024 errno = 0;
2025 srv_agent_state = strtol(params[10], &p, 10);
2026 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2027 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2028 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2029
2030 /* validating bk_f_forced_id */
2031 p = NULL;
2032 errno = 0;
2033 bk_f_forced_id = strtol(params[11], &p, 10);
2034 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2035 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2036
2037 /* validating srv_f_forced_id */
2038 p = NULL;
2039 errno = 0;
2040 srv_f_forced_id = strtol(params[12], &p, 10);
2041 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2042 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2043
2044
2045 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002046 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002047 goto out;
2048
2049 /* recover operational state and apply it to this server
2050 * and all servers tracking this one */
2051 switch (srv_op_state) {
2052 case SRV_ST_STOPPED:
2053 srv->check.health = 0;
2054 srv_set_stopped(srv, "changed from server-state after a reload");
2055 break;
2056 case SRV_ST_STARTING:
2057 srv->state = srv_op_state;
2058 break;
2059 case SRV_ST_STOPPING:
2060 srv->check.health = srv->check.rise + srv->check.fall - 1;
2061 srv_set_stopping(srv, "changed from server-state after a reload");
2062 break;
2063 case SRV_ST_RUNNING:
2064 srv->check.health = srv->check.rise + srv->check.fall - 1;
2065 srv_set_running(srv, "");
2066 break;
2067 }
2068
2069 /* When applying server state, the following rules apply:
2070 * - in case of a configuration change, we apply the setting from the new
2071 * configuration, regardless of old running state
2072 * - if no configuration change, we apply old running state only if old running
2073 * state is different from new configuration state
2074 */
2075 /* configuration has changed */
2076 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2077 if (srv->admin & SRV_ADMF_CMAINT)
2078 srv_adm_set_maint(srv);
2079 else
2080 srv_adm_set_ready(srv);
2081 }
2082 /* configuration is the same, let's compate old running state and new conf state */
2083 else {
2084 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2085 srv_adm_set_maint(srv);
2086 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2087 srv_adm_set_ready(srv);
2088 }
2089 /* apply drain mode if server is currently enabled */
2090 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2091 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
2092 * (srv->iweight is the weight set up in configuration)
2093 * so we don't want to apply it when srv_iweight is 0 and
2094 * srv->iweight is greater than 0. Purpose is to give the
2095 * chance to the admin to re-enable this server from configuration
2096 * file by setting a new weight > 0.
2097 */
2098 if ((srv_iweight == 0) && (srv->iweight > 0)) {
2099 srv_adm_set_drain(srv);
2100 }
2101 }
2102
2103 srv->last_change = date.tv_sec - srv_last_time_change;
2104 srv->check.status = srv_check_status;
2105 srv->check.result = srv_check_result;
2106 srv->check.health = srv_check_health;
2107
2108 /* Only case we want to apply is removing ENABLED flag which could have been
2109 * done by the "disable health" command over the stats socket
2110 */
2111 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2112 (srv_check_state & CHK_ST_CONFIGURED) &&
2113 !(srv_check_state & CHK_ST_ENABLED))
2114 srv->check.state &= ~CHK_ST_ENABLED;
2115
2116 /* Only case we want to apply is removing ENABLED flag which could have been
2117 * done by the "disable agent" command over the stats socket
2118 */
2119 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2120 (srv_agent_state & CHK_ST_CONFIGURED) &&
2121 !(srv_agent_state & CHK_ST_ENABLED))
2122 srv->agent.state &= ~CHK_ST_ENABLED;
2123
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002124 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2125 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002126 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002127 * It means that a configuration file change has precedence over a unix socket change
2128 * for server's weight
2129 *
2130 * by default, HAProxy applies the following weight when parsing the configuration
2131 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002132 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002133 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002134 srv->uweight = srv_uweight;
2135 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002136 server_recalc_eweight(srv);
2137
2138 /* update server IP only if DNS resolution is used on the server */
2139 if (srv->resolution) {
2140 memset(&addr, 0, sizeof(struct sockaddr_storage));
2141 if (str2ip2(params[0], &addr, 0))
2142 memcpy(&srv->addr, &addr, sizeof(struct sockaddr_storage));
2143 else
2144 chunk_appendf(msg, ", can't parse IP: %s", params[0]);
2145 }
2146 break;
2147 default:
2148 chunk_appendf(msg, ", version '%d' not supported", version);
2149 }
2150
2151 out:
Willy Tarreau31138fa2015-09-29 18:38:47 +02002152 if (msg->len)
2153 Warning("server-state application failed for server '%s/%s'%s",
2154 srv->proxy->id, srv->id, msg->str);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002155}
2156
2157/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2158 * For each proxy, it does the following:
2159 * - opens its server state file (either one or local one)
2160 * - read whole file, line by line
2161 * - analyse each line to check if it matches our current backend:
2162 * - backend name matches
2163 * - backend id matches if id is forced and name doesn't match
2164 * - if the server pointed by the line is found, then state is applied
2165 *
2166 * If the running backend uuid or id differs from the state file, then HAProxy reports
2167 * a warning.
2168 */
2169void apply_server_state(void)
2170{
2171 char *cur, *end;
2172 char mybuf[SRV_STATE_LINE_MAXLEN];
2173 int mybuflen;
2174 char *params[SRV_STATE_FILE_MAX_FIELDS];
2175 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2176 int arg, srv_arg, version, diff;
2177 FILE *f;
2178 char *filepath;
2179 char globalfilepath[MAXPATHLEN + 1];
2180 char localfilepath[MAXPATHLEN + 1];
2181 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2182 extern struct proxy *proxy;
2183 struct proxy *curproxy, *bk;
2184 struct server *srv;
2185
2186 globalfilepathlen = 0;
2187 /* create the globalfilepath variable */
2188 if (global.server_state_file) {
2189 /* absolute path or no base directory provided */
2190 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2191 len = strlen(global.server_state_file);
2192 if (len > MAXPATHLEN) {
2193 globalfilepathlen = 0;
2194 goto globalfileerror;
2195 }
2196 memcpy(globalfilepath, global.server_state_file, len);
2197 globalfilepath[len] = '\0';
2198 globalfilepathlen = len;
2199 }
2200 else if (global.server_state_base) {
2201 len = strlen(global.server_state_base);
2202 globalfilepathlen += len;
2203
2204 if (globalfilepathlen > MAXPATHLEN) {
2205 globalfilepathlen = 0;
2206 goto globalfileerror;
2207 }
2208 strncpy(globalfilepath, global.server_state_base, len);
2209 globalfilepath[globalfilepathlen] = 0;
2210
2211 /* append a slash if needed */
2212 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2213 if (globalfilepathlen + 1 > MAXPATHLEN) {
2214 globalfilepathlen = 0;
2215 goto globalfileerror;
2216 }
2217 globalfilepath[globalfilepathlen++] = '/';
2218 }
2219
2220 len = strlen(global.server_state_file);
2221 if (globalfilepathlen + len > MAXPATHLEN) {
2222 globalfilepathlen = 0;
2223 goto globalfileerror;
2224 }
2225 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2226 globalfilepathlen += len;
2227 globalfilepath[globalfilepathlen++] = 0;
2228 }
2229 }
2230 globalfileerror:
2231 if (globalfilepathlen == 0)
2232 globalfilepath[0] = '\0';
2233
2234 /* read servers state from local file */
2235 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2236 /* servers are only in backends */
2237 if (!(curproxy->cap & PR_CAP_BE))
2238 continue;
2239 fileopenerr = 0;
2240 filepath = NULL;
2241
2242 /* search server state file path and name */
2243 switch (curproxy->load_server_state_from_file) {
2244 /* read servers state from global file */
2245 case PR_SRV_STATE_FILE_GLOBAL:
2246 /* there was an error while generating global server state file path */
2247 if (globalfilepathlen == 0)
2248 continue;
2249 filepath = globalfilepath;
2250 fileopenerr = 1;
2251 break;
2252 /* this backend has its own file */
2253 case PR_SRV_STATE_FILE_LOCAL:
2254 localfilepathlen = 0;
2255 localfilepath[0] = '\0';
2256 len = 0;
2257 /* create the localfilepath variable */
2258 /* absolute path or no base directory provided */
2259 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2260 len = strlen(curproxy->server_state_file_name);
2261 if (len > MAXPATHLEN) {
2262 localfilepathlen = 0;
2263 goto localfileerror;
2264 }
2265 memcpy(localfilepath, curproxy->server_state_file_name, len);
2266 localfilepath[len] = '\0';
2267 localfilepathlen = len;
2268 }
2269 else if (global.server_state_base) {
2270 len = strlen(global.server_state_base);
2271 localfilepathlen += len;
2272
2273 if (localfilepathlen > MAXPATHLEN) {
2274 localfilepathlen = 0;
2275 goto localfileerror;
2276 }
2277 strncpy(localfilepath, global.server_state_base, len);
2278 localfilepath[localfilepathlen] = 0;
2279
2280 /* append a slash if needed */
2281 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2282 if (localfilepathlen + 1 > MAXPATHLEN) {
2283 localfilepathlen = 0;
2284 goto localfileerror;
2285 }
2286 localfilepath[localfilepathlen++] = '/';
2287 }
2288
2289 len = strlen(curproxy->server_state_file_name);
2290 if (localfilepathlen + len > MAXPATHLEN) {
2291 localfilepathlen = 0;
2292 goto localfileerror;
2293 }
2294 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2295 localfilepathlen += len;
2296 localfilepath[localfilepathlen++] = 0;
2297 }
2298 filepath = localfilepath;
2299 localfileerror:
2300 if (localfilepathlen == 0)
2301 localfilepath[0] = '\0';
2302
2303 break;
2304 case PR_SRV_STATE_FILE_NONE:
2305 default:
2306 continue;
2307 }
2308
2309 /* preload global state file */
2310 errno = 0;
2311 f = fopen(filepath, "r");
2312 if (errno && fileopenerr)
2313 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2314 if (!f)
2315 continue;
2316
2317 mybuf[0] = '\0';
2318 mybuflen = 0;
2319 version = 0;
2320
2321 /* first character of first line of the file must contain the version of the export */
2322 fgets(mybuf, SRV_STATE_LINE_MAXLEN, f);
2323 cur = mybuf;
2324 version = atoi(cur);
2325 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2326 (version > SRV_STATE_FILE_VERSION_MAX))
2327 continue;
2328
2329 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2330 int bk_f_forced_id = 0;
2331 int check_id = 0;
2332 int check_name = 0;
2333
2334 mybuflen = strlen(mybuf);
2335 cur = mybuf;
2336 end = cur + mybuflen;
2337
2338 bk = NULL;
2339 srv = NULL;
2340
2341 /* we need at least one character */
2342 if (mybuflen == 0)
2343 continue;
2344
2345 /* ignore blank characters at the beginning of the line */
2346 while (isspace(*cur))
2347 ++cur;
2348
2349 if (cur == end)
2350 continue;
2351
2352 /* ignore comment line */
2353 if (*cur == '#')
2354 continue;
2355
2356 /* we're now ready to move the line into *srv_params[] */
2357 params[0] = cur;
2358 arg = 1;
2359 srv_arg = 0;
2360 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2361 if (isspace(*cur)) {
2362 *cur = '\0';
2363 ++cur;
2364 while (isspace(*cur))
2365 ++cur;
2366 switch (version) {
2367 case 1:
2368 /*
2369 * srv_addr: params[4] => srv_params[0]
2370 * srv_op_state: params[5] => srv_params[1]
2371 * srv_admin_state: params[6] => srv_params[2]
2372 * srv_uweight: params[7] => srv_params[3]
2373 * srv_iweight: params[8] => srv_params[4]
2374 * srv_last_time_change: params[9] => srv_params[5]
2375 * srv_check_status: params[10] => srv_params[6]
2376 * srv_check_result: params[11] => srv_params[7]
2377 * srv_check_health: params[12] => srv_params[8]
2378 * srv_check_state: params[13] => srv_params[9]
2379 * srv_agent_state: params[14] => srv_params[10]
2380 * bk_f_forced_id: params[15] => srv_params[11]
2381 * srv_f_forced_id: params[16] => srv_params[12]
2382 */
2383 if (arg >= 4) {
2384 srv_params[srv_arg] = cur;
2385 ++srv_arg;
2386 }
2387 break;
2388 }
2389
2390 params[arg] = cur;
2391 ++arg;
2392 }
2393 else {
2394 ++cur;
2395 }
2396 }
2397
2398 /* if line is incomplete line, then ignore it.
2399 * otherwise, update useful flags */
2400 switch (version) {
2401 case 1:
2402 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2403 continue;
2404 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2405 check_id = (atoi(params[0]) == curproxy->uuid);
2406 check_name = (strcmp(curproxy->id, params[1]) == 0);
2407 break;
2408 }
2409
2410 diff = 0;
2411 bk = curproxy;
2412
2413 /* if backend can't be found, let's continue */
2414 if (!check_id && !check_name)
2415 continue;
2416 else if (!check_id && check_name) {
2417 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2418 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2419 }
2420 else if (check_id && !check_name) {
2421 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2422 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2423 /* if name doesn't match, we still want to update curproxy if the backend id
2424 * was forced in previous the previous configuration */
2425 if (!bk_f_forced_id)
2426 continue;
2427 }
2428
2429 /* look for the server by its id: param[2] */
2430 /* else look for the server by its name: param[3] */
2431 diff = 0;
2432 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2433
2434 if (!srv) {
2435 /* if no server found, then warning and continue with next line */
2436 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2437 params[3], params[2], params[0], params[1]);
2438 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2439 params[3], params[2], params[0], params[1]);
2440 continue;
2441 }
2442 else if (diff & PR_FBM_MISMATCH_ID) {
2443 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);
2444 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);
2445 }
2446 else if (diff & PR_FBM_MISMATCH_NAME) {
2447 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);
2448 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);
2449 }
2450
2451 /* now we can proceed with server's state update */
2452 srv_update_state(srv, version, srv_params);
2453 }
2454 fclose(f);
2455 }
2456}
2457
Simon Horman7d09b9a2013-02-12 10:45:51 +09002458/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002459 * update a server's current IP address.
2460 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2461 * ip is in network format.
2462 * updater is a string which contains an information about the requester of the update.
2463 * updater is used if not NULL.
2464 *
2465 * A log line and a stderr warning message is generated based on server's backend options.
2466 */
2467int update_server_addr(struct server *s, void *ip, int ip_sin_family, char *updater)
2468{
2469 /* generates a log line and a warning on stderr */
2470 if (1) {
2471 /* book enough space for both IPv4 and IPv6 */
2472 char oldip[INET6_ADDRSTRLEN];
2473 char newip[INET6_ADDRSTRLEN];
2474
2475 memset(oldip, '\0', INET6_ADDRSTRLEN);
2476 memset(newip, '\0', INET6_ADDRSTRLEN);
2477
2478 /* copy old IP address in a string */
2479 switch (s->addr.ss_family) {
2480 case AF_INET:
2481 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2482 break;
2483 case AF_INET6:
2484 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2485 break;
2486 };
2487
2488 /* copy new IP address in a string */
2489 switch (ip_sin_family) {
2490 case AF_INET:
2491 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2492 break;
2493 case AF_INET6:
2494 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2495 break;
2496 };
2497
2498 /* save log line into a buffer */
2499 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2500 s->proxy->id, s->id, oldip, newip, updater);
2501
2502 /* write the buffer on stderr */
2503 Warning("%s.\n", trash.str);
2504
2505 /* send a log */
2506 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2507 }
2508
2509 /* save the new IP family */
2510 s->addr.ss_family = ip_sin_family;
2511 /* save the new IP address */
2512 switch (ip_sin_family) {
2513 case AF_INET:
2514 ((struct sockaddr_in *)&s->addr)->sin_addr.s_addr = *(uint32_t *)ip;
2515 break;
2516 case AF_INET6:
2517 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2518 break;
2519 };
2520
2521 return 0;
2522}
2523
2524/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002525 * update server status based on result of name resolution
2526 * returns:
2527 * 0 if server status is updated
2528 * 1 if server status has not changed
2529 */
2530int snr_update_srv_status(struct server *s)
2531{
2532 struct dns_resolution *resolution = s->resolution;
2533
2534 switch (resolution->status) {
2535 case RSLV_STATUS_NONE:
2536 /* status when HAProxy has just (re)started */
2537 trigger_resolution(s);
2538 break;
2539
2540 default:
2541 break;
2542 }
2543
2544 return 1;
2545}
2546
2547/*
2548 * Server Name Resolution valid response callback
2549 * It expects:
2550 * - <nameserver>: the name server which answered the valid response
2551 * - <response>: buffer containing a valid DNS response
2552 * - <response_len>: size of <response>
2553 * It performs the following actions:
2554 * - ignore response if current ip found and server family not met
2555 * - update with first new ip found if family is met and current IP is not found
2556 * returns:
2557 * 0 on error
2558 * 1 when no error or safe ignore
2559 */
2560int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, unsigned char *response, int response_len)
2561{
2562 struct server *s;
2563 void *serverip, *firstip;
2564 short server_sin_family, firstip_sin_family;
2565 unsigned char *response_end;
2566 int ret;
2567 struct chunk *chk = get_trash_chunk();
2568
2569 /* initializing variables */
2570 response_end = response + response_len; /* pointer to mark the end of the response */
2571 firstip = NULL; /* pointer to the first valid response found */
2572 /* it will be used as the new IP if a change is required */
2573 firstip_sin_family = AF_UNSPEC;
2574 serverip = NULL; /* current server IP address */
2575
2576 /* shortcut to the server whose name is being resolved */
2577 s = (struct server *)resolution->requester;
2578
2579 /* initializing server IP pointer */
2580 server_sin_family = s->addr.ss_family;
2581 switch (server_sin_family) {
2582 case AF_INET:
2583 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
2584 break;
2585
2586 case AF_INET6:
2587 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
2588 break;
2589
2590 default:
2591 goto invalid;
2592 }
2593
2594 ret = dns_get_ip_from_response(response, response_end, resolution->hostname_dn, resolution->hostname_dn_len,
2595 serverip, server_sin_family, resolution->resolver_family_priority, &firstip,
2596 &firstip_sin_family);
2597
2598 switch (ret) {
2599 case DNS_UPD_NO:
2600 if (resolution->status != RSLV_STATUS_VALID) {
2601 resolution->status = RSLV_STATUS_VALID;
2602 resolution->last_status_change = now_ms;
2603 }
2604 goto stop_resolution;
2605
2606 case DNS_UPD_SRVIP_NOT_FOUND:
2607 goto save_ip;
2608
2609 case DNS_UPD_CNAME:
2610 if (resolution->status != RSLV_STATUS_VALID) {
2611 resolution->status = RSLV_STATUS_VALID;
2612 resolution->last_status_change = now_ms;
2613 }
2614 goto invalid;
2615
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02002616 case DNS_UPD_NO_IP_FOUND:
2617 if (resolution->status != RSLV_STATUS_OTHER) {
2618 resolution->status = RSLV_STATUS_OTHER;
2619 resolution->last_status_change = now_ms;
2620 }
2621 goto stop_resolution;
2622
Baptiste Assmannfad03182015-10-28 02:03:32 +01002623 case DNS_UPD_NAME_ERROR:
2624 /* if this is not the last expected response, we ignore it */
2625 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
2626 return 0;
2627 /* update resolution status to OTHER error type */
2628 if (resolution->status != RSLV_STATUS_OTHER) {
2629 resolution->status = RSLV_STATUS_OTHER;
2630 resolution->last_status_change = now_ms;
2631 }
2632 goto stop_resolution;
2633
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002634 default:
2635 goto invalid;
2636
2637 }
2638
2639 save_ip:
2640 nameserver->counters.update += 1;
2641 if (resolution->status != RSLV_STATUS_VALID) {
2642 resolution->status = RSLV_STATUS_VALID;
2643 resolution->last_status_change = now_ms;
2644 }
2645
2646 /* save the first ip we found */
2647 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
2648 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
2649
2650 stop_resolution:
2651 /* update last resolution date and time */
2652 resolution->last_resolution = now_ms;
2653 /* reset current status flag */
2654 resolution->step = RSLV_STEP_NONE;
2655 /* reset values */
2656 dns_reset_resolution(resolution);
2657
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002658 dns_update_resolvers_timeout(nameserver->resolvers);
2659
2660 snr_update_srv_status(s);
2661 return 0;
2662
2663 invalid:
2664 nameserver->counters.invalid += 1;
2665 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
2666 goto stop_resolution;
2667
2668 snr_update_srv_status(s);
2669 return 0;
2670}
2671
2672/*
2673 * Server Name Resolution error management callback
2674 * returns:
2675 * 0 on error
2676 * 1 when no error or safe ignore
2677 */
2678int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
2679{
2680 struct server *s;
2681 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002682 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002683
2684 /* shortcut to the server whose name is being resolved */
2685 s = (struct server *)resolution->requester;
2686 resolvers = resolution->resolvers;
2687
2688 /* can be ignored if this is not the last response */
2689 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
2690 return 1;
2691 }
2692
2693 switch (error_code) {
2694 case DNS_RESP_INVALID:
2695 case DNS_RESP_WRONG_NAME:
2696 if (resolution->status != RSLV_STATUS_INVALID) {
2697 resolution->status = RSLV_STATUS_INVALID;
2698 resolution->last_status_change = now_ms;
2699 }
2700 break;
2701
2702 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02002703 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002704 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02002705 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann90447582015-09-02 22:20:56 +02002706 res_preferred_afinet = resolution->resolver_family_priority == AF_INET && resolution->query_type == DNS_RTYPE_A;
2707 res_preferred_afinet6 = resolution->resolver_family_priority == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
2708
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002709 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002710 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002711 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002712 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02002713 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002714 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02002715 }
2716 else if (res_preferred_afinet) {
2717 /* fallback from A to AAAA */
2718 resolution->query_type = DNS_RTYPE_AAAA;
2719 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002720 else {
2721 resolution->try -= 1;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002722 if (resolution->resolver_family_priority == AF_INET) {
2723 resolution->query_type = DNS_RTYPE_A;
2724 } else {
2725 resolution->query_type = DNS_RTYPE_AAAA;
2726 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002727 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002728
2729 dns_send_query(resolution);
2730
2731 /*
2732 * move the resolution to the last element of the FIFO queue
2733 * and update timeout wakeup based on the new first entry
2734 */
2735 if (dns_check_resolution_queue(resolvers) > 1) {
2736 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02002737 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002738 /* ex first resolution goes to the end of the queue */
2739 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
2740 }
2741 dns_update_resolvers_timeout(resolvers);
2742 goto leave;
2743 }
2744 else {
2745 if (resolution->status != RSLV_STATUS_OTHER) {
2746 resolution->status = RSLV_STATUS_OTHER;
2747 resolution->last_status_change = now_ms;
2748 }
2749 }
2750 break;
2751
2752 case DNS_RESP_NX_DOMAIN:
2753 if (resolution->status != RSLV_STATUS_NX) {
2754 resolution->status = RSLV_STATUS_NX;
2755 resolution->last_status_change = now_ms;
2756 }
2757 break;
2758
2759 case DNS_RESP_REFUSED:
2760 if (resolution->status != RSLV_STATUS_REFUSED) {
2761 resolution->status = RSLV_STATUS_REFUSED;
2762 resolution->last_status_change = now_ms;
2763 }
2764 break;
2765
2766 case DNS_RESP_CNAME_ERROR:
2767 break;
2768
2769 case DNS_RESP_TIMEOUT:
2770 if (resolution->status != RSLV_STATUS_TIMEOUT) {
2771 resolution->status = RSLV_STATUS_TIMEOUT;
2772 resolution->last_status_change = now_ms;
2773 }
2774 break;
2775 }
2776
2777 /* update last resolution date and time */
2778 resolution->last_resolution = now_ms;
2779 /* reset current status flag */
2780 resolution->step = RSLV_STEP_NONE;
2781 /* reset values */
2782 dns_reset_resolution(resolution);
2783
2784 LIST_DEL(&resolution->list);
2785 dns_update_resolvers_timeout(resolvers);
2786
2787 leave:
2788 snr_update_srv_status(s);
2789 return 1;
2790}
2791
2792/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002793 * Local variables:
2794 * c-indent-level: 8
2795 * c-basic-offset: 8
2796 * End:
2797 */