blob: f38ee8d0c280d188c490ff7f03bce67f43aaa9ff [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
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100433 if (!(global.mode & MODE_STARTING)) {
434 Warning("%s.\n", trash.str);
435 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
436 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200437 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200438 else { /* server was still running */
439 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
440 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
441
442 check->health = 0; /* failure */
443 s->last_change = now.tv_sec;
444 s->state = SRV_ST_STOPPED;
445 if (s->proxy->lbprm.set_server_status_down)
446 s->proxy->lbprm.set_server_status_down(s);
447
448 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200449 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200450
Willy Tarreau87b09662015-04-03 00:22:06 +0200451 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200452 * a connection. Those which are redispatchable will be queued
453 * to another server or to the proxy itself.
454 */
455 xferred = pendconn_redistribute(s);
456
457 chunk_printf(&trash,
458 "%sServer %s/%s is going DOWN for maintenance",
459 s->flags & SRV_F_BACKUP ? "Backup " : "",
460 s->proxy->id, s->id);
461
462 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
463
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100464 if (!(global.mode & MODE_STARTING)) {
465 Warning("%s.\n", trash.str);
466 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
467 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200468
469 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
470 set_backend_down(s->proxy);
471
472 s->counters.down_trans++;
473 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200474 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200475
476 /* drain state is applied only if not yet in maint */
477 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200478 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
479
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200480 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200481 if (s->proxy->lbprm.set_server_status_down)
482 s->proxy->lbprm.set_server_status_down(s);
483
Willy Tarreau87b09662015-04-03 00:22:06 +0200484 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200485 * a connection. Those which are redispatchable will be queued
486 * to another server or to the proxy itself.
487 */
488 xferred = pendconn_redistribute(s);
489
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200490 chunk_printf(&trash, "%sServer %s/%s enters drain state",
491 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200492
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200493 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200494
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100495 if (!(global.mode & MODE_STARTING)) {
496 Warning("%s.\n", trash.str);
497 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
498 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
499 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200500 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
501 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200502 }
503
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200504 /* compute the inherited flag to propagate */
505 if (mode & SRV_ADMF_MAINT)
506 mode = SRV_ADMF_IMAINT;
507 else if (mode & SRV_ADMF_DRAIN)
508 mode = SRV_ADMF_IDRAIN;
509
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200510 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200511 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200512}
513
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200514/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
515 * stop enforcing either maint mode or drain mode. It is not allowed to set more
516 * than one flag at once. The equivalent "inherited" flag is propagated to all
517 * tracking servers. Leaving maintenance mode re-enables health checks. When
518 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200519 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200520void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200521{
522 struct check *check = &s->check;
523 struct server *srv;
524 int xferred = -1;
525
526 if (!mode)
527 return;
528
529 /* stop going down as soon as we see the flag is not there anymore */
530 if (!(s->admin & mode))
531 return;
532
533 s->admin &= ~mode;
534
535 if (s->admin & SRV_ADMF_MAINT) {
536 /* remaining in maintenance mode, let's inform precisely about the
537 * situation.
538 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200539 if (mode & SRV_ADMF_FMAINT) {
540 chunk_printf(&trash,
541 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
542 s->flags & SRV_F_BACKUP ? "Backup " : "",
543 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200544
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200545 if (s->track) /* normally it's mandatory here */
546 chunk_appendf(&trash, " via %s/%s",
547 s->track->proxy->id, s->track->id);
548 Warning("%s.\n", trash.str);
549 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
550 }
551 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200552 chunk_printf(&trash,
553 "%sServer %s/%s remains in forced maintenance",
554 s->flags & SRV_F_BACKUP ? "Backup " : "",
555 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200556 Warning("%s.\n", trash.str);
557 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
558 }
559 /* don't report anything when leaving drain mode and remaining in maintenance */
560 }
561 else if (mode & SRV_ADMF_MAINT) {
562 /* OK here we're leaving maintenance, we have many things to check,
563 * because the server might possibly be coming back up depending on
564 * its state. In practice, leaving maintenance means that we should
565 * immediately turn to UP (more or less the slowstart) under the
566 * following conditions :
567 * - server is neither checked nor tracked
568 * - server tracks another server which is not checked
569 * - server tracks another server which is already up
570 * Which sums up as something simpler :
571 * "either the tracking server is up or the server's checks are disabled
572 * or up". Otherwise we only re-enable health checks. There's a special
573 * case associated to the stopping state which can be inherited. Note
574 * that the server might still be in drain mode, which is naturally dealt
575 * with by the lower level functions.
576 */
577
578 if (s->check.state & CHK_ST_ENABLED) {
579 s->check.state &= ~CHK_ST_PAUSED;
580 check->health = check->rise; /* start OK but check immediately */
581 }
582
583 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
584 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
585 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
586 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
587 if (s->proxy->last_change < now.tv_sec) // ignore negative times
588 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
589 s->proxy->last_change = now.tv_sec;
590 }
591
592 if (s->last_change < now.tv_sec) // ignore negative times
593 s->down_time += now.tv_sec - s->last_change;
594 s->last_change = now.tv_sec;
595
596 if (s->track && s->track->state == SRV_ST_STOPPING)
597 s->state = SRV_ST_STOPPING;
598 else {
599 s->state = SRV_ST_STARTING;
600 if (s->slowstart > 0)
601 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
602 else
603 s->state = SRV_ST_RUNNING;
604 }
605
606 server_recalc_eweight(s);
607
608 /* If the server is set with "on-marked-up shutdown-backup-sessions",
609 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200610 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200611 * on all backup servers.
612 */
613 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
614 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200615 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200616
617 /* check if we can handle some connections queued at the proxy. We
618 * will take as many as we can handle.
619 */
620 xferred = pendconn_grab_from_px(s);
621 }
622
623 if (mode & SRV_ADMF_FMAINT) {
624 chunk_printf(&trash,
625 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
626 s->flags & SRV_F_BACKUP ? "Backup " : "",
627 s->proxy->id, s->id,
628 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
629 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200630 }
631 else {
632 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200633 "%sServer %s/%s is %s/%s (leaving maintenance)",
634 s->flags & SRV_F_BACKUP ? "Backup " : "",
635 s->proxy->id, s->id,
636 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
637 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
638 srv_append_status(&trash, s, NULL, xferred, 0);
639 }
640 Warning("%s.\n", trash.str);
641 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
642 }
643 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
644 /* remaining in drain mode after removing one of its flags */
645
646 if (mode & SRV_ADMF_FDRAIN) {
647 chunk_printf(&trash,
648 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200649 s->flags & SRV_F_BACKUP ? "Backup " : "",
650 s->proxy->id, s->id);
651
652 if (s->track) /* normally it's mandatory here */
653 chunk_appendf(&trash, " via %s/%s",
654 s->track->proxy->id, s->track->id);
655 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200656 else {
657 chunk_printf(&trash,
658 "%sServer %s/%s remains in forced drain mode",
659 s->flags & SRV_F_BACKUP ? "Backup " : "",
660 s->proxy->id, s->id);
661 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200662 Warning("%s.\n", trash.str);
663 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200664 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200665 else if (mode & SRV_ADMF_DRAIN) {
666 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200667 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
668 if (s->proxy->last_change < now.tv_sec) // ignore negative times
669 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
670 s->proxy->last_change = now.tv_sec;
671 }
672
673 if (s->last_change < now.tv_sec) // ignore negative times
674 s->down_time += now.tv_sec - s->last_change;
675 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200676 server_recalc_eweight(s);
677
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200678 if (mode & SRV_ADMF_FDRAIN) {
679 chunk_printf(&trash,
680 "%sServer %s/%s is %s (leaving forced drain)",
681 s->flags & SRV_F_BACKUP ? "Backup " : "",
682 s->proxy->id, s->id,
683 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
684 }
685 else {
686 chunk_printf(&trash,
687 "%sServer %s/%s is %s (leaving drain)",
688 s->flags & SRV_F_BACKUP ? "Backup " : "",
689 s->proxy->id, s->id,
690 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
691 if (s->track) /* normally it's mandatory here */
692 chunk_appendf(&trash, " via %s/%s",
693 s->track->proxy->id, s->track->id);
694 }
695 Warning("%s.\n", trash.str);
696 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200697 }
698
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200699 /* stop going down if the equivalent flag is still present (forced or inherited) */
700 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
701 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
702 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200703
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200704 if (mode & SRV_ADMF_MAINT)
705 mode = SRV_ADMF_IMAINT;
706 else if (mode & SRV_ADMF_DRAIN)
707 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200708
709 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200710 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200711}
712
Willy Tarreau757478e2016-11-03 19:22:19 +0100713/* principle: propagate maint and drain to tracking servers. This is useful
714 * upon startup so that inherited states are correct.
715 */
716static void srv_propagate_admin_state(struct server *srv)
717{
718 struct server *srv2;
719
720 if (!srv->trackers)
721 return;
722
723 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
724 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
725 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT);
726
727 if (srv->admin & SRV_ADMF_DRAIN)
728 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN);
729 }
730}
731
732/* Compute and propagate the admin states for all servers in proxy <px>.
733 * Only servers *not* tracking another one are considered, because other
734 * ones will be handled when the server they track is visited.
735 */
736void srv_compute_all_admin_states(struct proxy *px)
737{
738 struct server *srv;
739
740 for (srv = px->srv; srv; srv = srv->next) {
741 if (srv->track)
742 continue;
743 srv_propagate_admin_state(srv);
744 }
745}
746
Willy Tarreaudff55432012-10-10 17:51:05 +0200747/* Note: must not be declared <const> as its list will be overwritten.
748 * Please take care of keeping this list alphabetically sorted, doing so helps
749 * all code contributors.
750 * Optional keywords are also declared with a NULL ->parse() function so that
751 * the config parser can report an appropriate error when a known keyword was
752 * not enabled.
753 */
754static struct srv_kw_list srv_kws = { "ALL", { }, {
755 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
756 { NULL, NULL, 0 },
757}};
758
759__attribute__((constructor))
760static void __listener_init(void)
761{
762 srv_register_keywords(&srv_kws);
763}
764
Willy Tarreau004e0452013-11-21 11:22:01 +0100765/* Recomputes the server's eweight based on its state, uweight, the current time,
766 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
767 * state is automatically disabled if the time is elapsed.
768 */
769void server_recalc_eweight(struct server *sv)
770{
771 struct proxy *px = sv->proxy;
772 unsigned w;
773
774 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
775 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200776 if (sv->state == SRV_ST_STARTING)
777 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100778 }
779
780 /* We must take care of not pushing the server to full throttle during slow starts.
781 * It must also start immediately, at least at the minimal step when leaving maintenance.
782 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200783 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100784 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
785 else
786 w = px->lbprm.wdiv;
787
788 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
789
790 /* now propagate the status change to any LB algorithms */
791 if (px->lbprm.update_server_eweight)
792 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200793 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100794 if (px->lbprm.set_server_status_up)
795 px->lbprm.set_server_status_up(sv);
796 }
797 else {
798 if (px->lbprm.set_server_status_down)
799 px->lbprm.set_server_status_down(sv);
800 }
801}
802
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900804 * Parses weight_str and configures sv accordingly.
805 * Returns NULL on success, error message string otherwise.
806 */
807const char *server_parse_weight_change_request(struct server *sv,
808 const char *weight_str)
809{
810 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900811 long int w;
812 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900813
814 px = sv->proxy;
815
816 /* if the weight is terminated with '%', it is set relative to
817 * the initial weight, otherwise it is absolute.
818 */
819 if (!*weight_str)
820 return "Require <weight> or <weight%>.\n";
821
Simon Hormanb796afa2013-02-12 10:45:53 +0900822 w = strtol(weight_str, &end, 10);
823 if (end == weight_str)
824 return "Empty weight string empty or preceded by garbage";
825 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900826 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900827 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900828 /* Avoid integer overflow */
829 if (w > 25600)
830 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900831 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900832 if (w > 256)
833 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900834 }
835 else if (w < 0 || w > 256)
836 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900837 else if (end[0] != '\0')
838 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900839
840 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
841 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
842
843 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100844 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900845
846 return NULL;
847}
848
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200849/*
Thierry Fournier09a91782016-02-24 08:25:39 +0100850 * Parses <addr_str> and configures <sv> accordingly. <from> precise
851 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200852 * Returns:
853 * - error string on error
854 * - NULL on success
855 */
856const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +0100857 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200858{
859 unsigned char ip[INET6_ADDRSTRLEN];
860
861 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100862 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200863 return NULL;
864 }
865 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100866 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200867 return NULL;
868 }
869
870 return "Could not understand IP address format.\n";
871}
872
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200873const char *server_parse_maxconn_change_request(struct server *sv,
874 const char *maxconn_str)
875{
876 long int v;
877 char *end;
878
879 if (!*maxconn_str)
880 return "Require <maxconn>.\n";
881
882 v = strtol(maxconn_str, &end, 10);
883 if (end == maxconn_str)
884 return "maxconn string empty or preceded by garbage";
885 else if (end[0] != '\0')
886 return "Trailing garbage in maxconn string";
887
888 if (sv->maxconn == sv->minconn) { // static maxconn
889 sv->maxconn = sv->minconn = v;
890 } else { // dynamic maxconn
891 sv->maxconn = v;
892 }
893
894 if (may_dequeue_tasks(sv, sv->proxy))
895 process_srv_queue(sv);
896
897 return NULL;
898}
899
Willy Tarreau272adea2014-03-31 10:39:59 +0200900int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
901{
902 struct server *newsrv = NULL;
903 const char *err;
904 char *errmsg = NULL;
905 int err_code = 0;
906 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +0200907 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +0200908
909 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
910 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +0200911 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
912
913 if (!defsrv && curproxy == defproxy) {
914 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
915 err_code |= ERR_ALERT | ERR_FATAL;
916 goto out;
917 }
918 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
919 err_code |= ERR_ALERT | ERR_FATAL;
920
921 if (!*args[2]) {
922 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
923 file, linenum, args[0]);
924 err_code |= ERR_ALERT | ERR_FATAL;
925 goto out;
926 }
927
928 err = invalid_char(args[1]);
929 if (err && !defsrv) {
930 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
931 file, linenum, *err, args[1]);
932 err_code |= ERR_ALERT | ERR_FATAL;
933 goto out;
934 }
935
936 if (!defsrv) {
937 struct sockaddr_storage *sk;
938 int port1, port2;
939 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200940 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200941
Vincent Bernat02779b62016-04-03 13:48:43 +0200942 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200943 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
944 err_code |= ERR_ALERT | ERR_ABORT;
945 goto out;
946 }
947
948 /* the servers are linked backwards first */
949 newsrv->next = curproxy->srv;
950 curproxy->srv = newsrv;
951 newsrv->proxy = curproxy;
952 newsrv->conf.file = strdup(file);
953 newsrv->conf.line = linenum;
954
955 newsrv->obj_type = OBJ_TYPE_SERVER;
956 LIST_INIT(&newsrv->actconns);
957 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +0200958 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +0200959 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +0200960 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +0200961 do_check = 0;
962 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200963 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200964 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200965 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200966 newsrv->last_change = now.tv_sec;
967 newsrv->id = strdup(args[1]);
968
969 /* several ways to check the port component :
970 * - IP => port=+0, relative (IPv4 only)
971 * - IP: => port=+0, relative
972 * - IP:N => port=N, absolute
973 * - IP:+N => port=+N, relative
974 * - IP:-N => port=-N, relative
975 */
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +0200976 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +0200977 if (!sk) {
978 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
979 err_code |= ERR_ALERT | ERR_FATAL;
980 goto out;
981 }
982
983 proto = protocol_by_family(sk->ss_family);
984 if (!proto || !proto->connect) {
985 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
986 file, linenum, args[0], args[1]);
987 err_code |= ERR_ALERT | ERR_FATAL;
988 goto out;
989 }
990
991 if (!port1 || !port2) {
992 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200993 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200994 }
995 else if (port1 != port2) {
996 /* port range */
997 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
998 file, linenum, args[0], args[1], args[2]);
999 err_code |= ERR_ALERT | ERR_FATAL;
1000 goto out;
1001 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001002
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001003 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001004 newsrv->hostname = fqdn;
1005 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001006 goto skip_name_resolution;
1007
Willy Tarreau07101d52015-09-08 16:16:35 +02001008 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001009 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001010 goto skip_name_resolution;
1011
1012 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1013 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1014 goto skip_name_resolution;
1015 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1016 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1017 file, linenum, args[2]);
1018 err_code |= ERR_ALERT | ERR_FATAL;
1019 goto out;
1020 }
1021
1022 curr_resolution->requester = newsrv;
1023 curr_resolution->requester_cb = snr_resolution_cb;
1024 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1025 curr_resolution->status = RSLV_STATUS_NONE;
1026 curr_resolution->step = RSLV_STEP_NONE;
1027 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001028 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001029 newsrv->resolution = curr_resolution;
1030
1031 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001032 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +01001033 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +02001034
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001035 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001036 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1037 file, linenum, newsrv->addr.ss_family, args[2]);
1038 err_code |= ERR_ALERT | ERR_FATAL;
1039 goto out;
1040 }
1041
1042 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1043 newsrv->check.port = curproxy->defsrv.check.port;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001044 if (newsrv->check.port)
1045 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001046 newsrv->check.inter = curproxy->defsrv.check.inter;
1047 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1048 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1049 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1050 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001051 if (curproxy->defsrv.agent.send_string != NULL)
1052 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1053 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001054 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1055 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1056 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1057 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1058 newsrv->minconn = curproxy->defsrv.minconn;
1059 newsrv->maxconn = curproxy->defsrv.maxconn;
1060 newsrv->slowstart = curproxy->defsrv.slowstart;
1061 newsrv->onerror = curproxy->defsrv.onerror;
1062 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1063 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1064 newsrv->consecutive_errors_limit
1065 = curproxy->defsrv.consecutive_errors_limit;
1066#ifdef OPENSSL
1067 newsrv->use_ssl = curproxy->defsrv.use_ssl;
1068#endif
1069 newsrv->uweight = newsrv->iweight
1070 = curproxy->defsrv.iweight;
1071
1072 newsrv->check.status = HCHK_STATUS_INI;
1073 newsrv->check.rise = curproxy->defsrv.check.rise;
1074 newsrv->check.fall = curproxy->defsrv.check.fall;
1075 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1076 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001077 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001078
1079 newsrv->agent.status = HCHK_STATUS_INI;
1080 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1081 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1082 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1083 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001084 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1085 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1086 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001087 memcpy(newsrv->dns_opts.pref_net,
1088 curproxy->defsrv.dns_opts.pref_net,
1089 sizeof(newsrv->dns_opts.pref_net));
1090 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Willy Tarreau272adea2014-03-31 10:39:59 +02001091
1092 cur_arg = 3;
1093 } else {
1094 newsrv = &curproxy->defsrv;
1095 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001096 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001097 }
1098
1099 while (*args[cur_arg]) {
1100 if (!strcmp(args[cur_arg], "agent-check")) {
1101 global.maxsock++;
1102 do_agent = 1;
1103 cur_arg += 1;
1104 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1105 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1106 if (err) {
1107 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1108 file, linenum, *err, newsrv->id);
1109 err_code |= ERR_ALERT | ERR_FATAL;
1110 goto out;
1111 }
1112 if (val <= 0) {
1113 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1114 file, linenum, val, args[cur_arg], newsrv->id);
1115 err_code |= ERR_ALERT | ERR_FATAL;
1116 goto out;
1117 }
1118 newsrv->agent.inter = val;
1119 cur_arg += 2;
1120 }
1121 else if (!strcmp(args[cur_arg], "agent-port")) {
1122 global.maxsock++;
1123 newsrv->agent.port = atol(args[cur_arg + 1]);
1124 cur_arg += 2;
1125 }
James Brown55f9ff12015-10-21 18:19:05 -07001126 else if (!strcmp(args[cur_arg], "agent-send")) {
1127 global.maxsock++;
1128 free(newsrv->agent.send_string);
1129 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1130 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1131 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1132 cur_arg += 2;
1133 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001134 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1135 newsrv->cookie = strdup(args[cur_arg + 1]);
1136 newsrv->cklen = strlen(args[cur_arg + 1]);
1137 cur_arg += 2;
1138 }
1139 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1140 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1141 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1142 cur_arg += 2;
1143 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001144 else if (!strcmp(args[cur_arg], "resolvers")) {
1145 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1146 cur_arg += 2;
1147 }
1148 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1149 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001150 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001151 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001152 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001153 else {
1154 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1155 file, linenum, args[cur_arg]);
1156 err_code |= ERR_ALERT | ERR_FATAL;
1157 goto out;
1158 }
1159 cur_arg += 2;
1160 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001161 else if (!strcmp(args[cur_arg], "resolve-net")) {
1162 char *p, *e;
1163 unsigned char mask;
1164 struct dns_options *opt;
1165
1166 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1167 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1168 file, linenum, args[cur_arg]);
1169 err_code |= ERR_ALERT | ERR_FATAL;
1170 goto out;
1171 }
1172
1173 opt = &newsrv->dns_opts;
1174
1175 /* Split arguments by comma, and convert it from ipv4 or ipv6
1176 * string network in in_addr or in6_addr.
1177 */
1178 p = args[cur_arg + 1];
1179 e = p;
1180 while (*p != '\0') {
1181 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001182 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001183 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1184 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1185 err_code |= ERR_ALERT | ERR_FATAL;
1186 goto out;
1187 }
1188 /* look for end or comma. */
1189 while (*e != ',' && *e != '\0')
1190 e++;
1191 if (*e == ',') {
1192 *e = '\0';
1193 e++;
1194 }
1195 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1196 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1197 /* Try to convert input string from ipv4 or ipv6 network. */
1198 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1199 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1200 &mask)) {
1201 /* Try to convert input string from ipv6 network. */
1202 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1203 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1204 } else {
1205 /* All network conversions fail, retrun error. */
1206 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1207 file, linenum, args[cur_arg], p);
1208 err_code |= ERR_ALERT | ERR_FATAL;
1209 goto out;
1210 }
1211 opt->pref_net_nb++;
1212 p = e;
1213 }
1214
1215 cur_arg += 2;
1216 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001217 else if (!strcmp(args[cur_arg], "rise")) {
1218 if (!*args[cur_arg + 1]) {
1219 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1220 file, linenum, args[cur_arg]);
1221 err_code |= ERR_ALERT | ERR_FATAL;
1222 goto out;
1223 }
1224
1225 newsrv->check.rise = atol(args[cur_arg + 1]);
1226 if (newsrv->check.rise <= 0) {
1227 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1228 file, linenum, args[cur_arg]);
1229 err_code |= ERR_ALERT | ERR_FATAL;
1230 goto out;
1231 }
1232
1233 if (newsrv->check.health)
1234 newsrv->check.health = newsrv->check.rise;
1235 cur_arg += 2;
1236 }
1237 else if (!strcmp(args[cur_arg], "fall")) {
1238 newsrv->check.fall = atol(args[cur_arg + 1]);
1239
1240 if (!*args[cur_arg + 1]) {
1241 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1242 file, linenum, args[cur_arg]);
1243 err_code |= ERR_ALERT | ERR_FATAL;
1244 goto out;
1245 }
1246
1247 if (newsrv->check.fall <= 0) {
1248 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1249 file, linenum, args[cur_arg]);
1250 err_code |= ERR_ALERT | ERR_FATAL;
1251 goto out;
1252 }
1253
1254 cur_arg += 2;
1255 }
1256 else if (!strcmp(args[cur_arg], "inter")) {
1257 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1258 if (err) {
1259 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1260 file, linenum, *err, newsrv->id);
1261 err_code |= ERR_ALERT | ERR_FATAL;
1262 goto out;
1263 }
1264 if (val <= 0) {
1265 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1266 file, linenum, val, args[cur_arg], newsrv->id);
1267 err_code |= ERR_ALERT | ERR_FATAL;
1268 goto out;
1269 }
1270 newsrv->check.inter = val;
1271 cur_arg += 2;
1272 }
1273 else if (!strcmp(args[cur_arg], "fastinter")) {
1274 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1275 if (err) {
1276 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1277 file, linenum, *err, newsrv->id);
1278 err_code |= ERR_ALERT | ERR_FATAL;
1279 goto out;
1280 }
1281 if (val <= 0) {
1282 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1283 file, linenum, val, args[cur_arg], newsrv->id);
1284 err_code |= ERR_ALERT | ERR_FATAL;
1285 goto out;
1286 }
1287 newsrv->check.fastinter = val;
1288 cur_arg += 2;
1289 }
1290 else if (!strcmp(args[cur_arg], "downinter")) {
1291 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1292 if (err) {
1293 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1294 file, linenum, *err, newsrv->id);
1295 err_code |= ERR_ALERT | ERR_FATAL;
1296 goto out;
1297 }
1298 if (val <= 0) {
1299 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1300 file, linenum, val, args[cur_arg], newsrv->id);
1301 err_code |= ERR_ALERT | ERR_FATAL;
1302 goto out;
1303 }
1304 newsrv->check.downinter = val;
1305 cur_arg += 2;
1306 }
1307 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1308 struct sockaddr_storage *sk;
1309 int port1, port2;
1310 struct protocol *proto;
1311
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001312 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001313 if (!sk) {
1314 Alert("parsing [%s:%d] : '%s' : %s\n",
1315 file, linenum, args[cur_arg], errmsg);
1316 err_code |= ERR_ALERT | ERR_FATAL;
1317 goto out;
1318 }
1319
1320 proto = protocol_by_family(sk->ss_family);
1321 if (!proto || !proto->connect) {
1322 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1323 file, linenum, args[cur_arg], args[cur_arg + 1]);
1324 err_code |= ERR_ALERT | ERR_FATAL;
1325 goto out;
1326 }
1327
1328 if (port1 != port2) {
1329 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1330 file, linenum, args[cur_arg], args[cur_arg + 1]);
1331 err_code |= ERR_ALERT | ERR_FATAL;
1332 goto out;
1333 }
1334
Simon Horman41f58762015-01-30 11:22:56 +09001335 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001336 newsrv->flags |= SRV_F_CHECKADDR;
1337 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001338 cur_arg += 2;
1339 }
1340 else if (!strcmp(args[cur_arg], "port")) {
1341 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001342 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001343 cur_arg += 2;
1344 }
1345 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001346 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001347 cur_arg ++;
1348 }
1349 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001350 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001351 cur_arg ++;
1352 }
1353 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001354 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001355 cur_arg ++;
1356 }
David Safb76832014-05-08 23:42:08 -04001357 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1358 newsrv->pp_opts |= SRV_PP_V2;
1359 cur_arg ++;
1360 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001361 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1362 newsrv->check.send_proxy = 1;
1363 cur_arg ++;
1364 }
1365 else if (!strcmp(args[cur_arg], "weight")) {
1366 int w;
1367 w = atol(args[cur_arg + 1]);
1368 if (w < 0 || w > SRV_UWGHT_MAX) {
1369 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1370 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1371 err_code |= ERR_ALERT | ERR_FATAL;
1372 goto out;
1373 }
1374 newsrv->uweight = newsrv->iweight = w;
1375 cur_arg += 2;
1376 }
1377 else if (!strcmp(args[cur_arg], "minconn")) {
1378 newsrv->minconn = atol(args[cur_arg + 1]);
1379 cur_arg += 2;
1380 }
1381 else if (!strcmp(args[cur_arg], "maxconn")) {
1382 newsrv->maxconn = atol(args[cur_arg + 1]);
1383 cur_arg += 2;
1384 }
1385 else if (!strcmp(args[cur_arg], "maxqueue")) {
1386 newsrv->maxqueue = atol(args[cur_arg + 1]);
1387 cur_arg += 2;
1388 }
1389 else if (!strcmp(args[cur_arg], "slowstart")) {
1390 /* slowstart is stored in seconds */
1391 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1392 if (err) {
1393 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1394 file, linenum, *err, newsrv->id);
1395 err_code |= ERR_ALERT | ERR_FATAL;
1396 goto out;
1397 }
1398 newsrv->slowstart = (val + 999) / 1000;
1399 cur_arg += 2;
1400 }
1401 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1402
1403 if (!*args[cur_arg + 1]) {
1404 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1405 file, linenum);
1406 err_code |= ERR_ALERT | ERR_FATAL;
1407 goto out;
1408 }
1409
1410 newsrv->trackit = strdup(args[cur_arg + 1]);
1411
1412 cur_arg += 2;
1413 }
1414 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1415 global.maxsock++;
1416 do_check = 1;
1417 cur_arg += 1;
1418 }
1419 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001420 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001421 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001422 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001423 newsrv->check.state |= CHK_ST_PAUSED;
1424 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001425 cur_arg += 1;
1426 }
1427 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1428 if (!strcmp(args[cur_arg + 1], "none"))
1429 newsrv->observe = HANA_OBS_NONE;
1430 else if (!strcmp(args[cur_arg + 1], "layer4"))
1431 newsrv->observe = HANA_OBS_LAYER4;
1432 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1433 if (curproxy->mode != PR_MODE_HTTP) {
1434 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1435 file, linenum, args[cur_arg + 1]);
1436 err_code |= ERR_ALERT;
1437 }
1438 newsrv->observe = HANA_OBS_LAYER7;
1439 }
1440 else {
1441 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1442 "'layer4', 'layer7' but got '%s'\n",
1443 file, linenum, args[cur_arg], args[cur_arg + 1]);
1444 err_code |= ERR_ALERT | ERR_FATAL;
1445 goto out;
1446 }
1447
1448 cur_arg += 2;
1449 }
1450 else if (!strcmp(args[cur_arg], "on-error")) {
1451 if (!strcmp(args[cur_arg + 1], "fastinter"))
1452 newsrv->onerror = HANA_ONERR_FASTINTER;
1453 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1454 newsrv->onerror = HANA_ONERR_FAILCHK;
1455 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1456 newsrv->onerror = HANA_ONERR_SUDDTH;
1457 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1458 newsrv->onerror = HANA_ONERR_MARKDWN;
1459 else {
1460 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1461 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1462 file, linenum, args[cur_arg], args[cur_arg + 1]);
1463 err_code |= ERR_ALERT | ERR_FATAL;
1464 goto out;
1465 }
1466
1467 cur_arg += 2;
1468 }
1469 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1470 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1471 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1472 else {
1473 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1474 file, linenum, args[cur_arg], args[cur_arg + 1]);
1475 err_code |= ERR_ALERT | ERR_FATAL;
1476 goto out;
1477 }
1478
1479 cur_arg += 2;
1480 }
1481 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1482 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1483 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1484 else {
1485 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1486 file, linenum, args[cur_arg], args[cur_arg + 1]);
1487 err_code |= ERR_ALERT | ERR_FATAL;
1488 goto out;
1489 }
1490
1491 cur_arg += 2;
1492 }
1493 else if (!strcmp(args[cur_arg], "error-limit")) {
1494 if (!*args[cur_arg + 1]) {
1495 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1496 file, linenum, args[cur_arg]);
1497 err_code |= ERR_ALERT | ERR_FATAL;
1498 goto out;
1499 }
1500
1501 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1502
1503 if (newsrv->consecutive_errors_limit <= 0) {
1504 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1505 file, linenum, args[cur_arg]);
1506 err_code |= ERR_ALERT | ERR_FATAL;
1507 goto out;
1508 }
1509 cur_arg += 2;
1510 }
1511 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1512 int port_low, port_high;
1513 struct sockaddr_storage *sk;
1514 struct protocol *proto;
1515
1516 if (!*args[cur_arg + 1]) {
1517 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1518 file, linenum, "source", "usesrc", "interface");
1519 err_code |= ERR_ALERT | ERR_FATAL;
1520 goto out;
1521 }
1522
1523 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001524 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001525 if (!sk) {
1526 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1527 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1528 err_code |= ERR_ALERT | ERR_FATAL;
1529 goto out;
1530 }
1531
1532 proto = protocol_by_family(sk->ss_family);
1533 if (!proto || !proto->connect) {
1534 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1535 file, linenum, args[cur_arg], args[cur_arg+1]);
1536 err_code |= ERR_ALERT | ERR_FATAL;
1537 goto out;
1538 }
1539
1540 newsrv->conn_src.source_addr = *sk;
1541
1542 if (port_low != port_high) {
1543 int i;
1544
1545 if (!port_low || !port_high) {
1546 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1547 file, linenum, args[cur_arg], args[cur_arg + 1]);
1548 err_code |= ERR_ALERT | ERR_FATAL;
1549 goto out;
1550 }
1551
1552 if (port_low <= 0 || port_low > 65535 ||
1553 port_high <= 0 || port_high > 65535 ||
1554 port_low > port_high) {
1555 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1556 file, linenum, port_low, port_high);
1557 err_code |= ERR_ALERT | ERR_FATAL;
1558 goto out;
1559 }
1560 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1561 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1562 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1563 }
1564
1565 cur_arg += 2;
1566 while (*(args[cur_arg])) {
1567 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001568#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001569 if (!*args[cur_arg + 1]) {
1570 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1571 file, linenum, "usesrc");
1572 err_code |= ERR_ALERT | ERR_FATAL;
1573 goto out;
1574 }
1575 if (!strcmp(args[cur_arg + 1], "client")) {
1576 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1577 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1578 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1579 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1580 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1581 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1582 char *name, *end;
1583
1584 name = args[cur_arg+1] + 7;
1585 while (isspace(*name))
1586 name++;
1587
1588 end = name;
1589 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1590 end++;
1591
1592 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1593 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1594 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1595 newsrv->conn_src.bind_hdr_len = end - name;
1596 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1597 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1598 newsrv->conn_src.bind_hdr_occ = -1;
1599
1600 /* now look for an occurrence number */
1601 while (isspace(*end))
1602 end++;
1603 if (*end == ',') {
1604 end++;
1605 name = end;
1606 if (*end == '-')
1607 end++;
1608 while (isdigit((int)*end))
1609 end++;
1610 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1611 }
1612
1613 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1614 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1615 " occurrences values smaller than %d.\n",
1616 file, linenum, MAX_HDR_HISTORY);
1617 err_code |= ERR_ALERT | ERR_FATAL;
1618 goto out;
1619 }
1620 } else {
1621 struct sockaddr_storage *sk;
1622 int port1, port2;
1623
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001624 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001625 if (!sk) {
1626 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1627 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1628 err_code |= ERR_ALERT | ERR_FATAL;
1629 goto out;
1630 }
1631
1632 proto = protocol_by_family(sk->ss_family);
1633 if (!proto || !proto->connect) {
1634 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1635 file, linenum, args[cur_arg], args[cur_arg+1]);
1636 err_code |= ERR_ALERT | ERR_FATAL;
1637 goto out;
1638 }
1639
1640 if (port1 != port2) {
1641 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1642 file, linenum, args[cur_arg], args[cur_arg + 1]);
1643 err_code |= ERR_ALERT | ERR_FATAL;
1644 goto out;
1645 }
1646 newsrv->conn_src.tproxy_addr = *sk;
1647 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1648 }
1649 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001650 cur_arg += 2;
1651 continue;
1652#else /* no TPROXY support */
1653 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1654 file, linenum, "usesrc");
1655 err_code |= ERR_ALERT | ERR_FATAL;
1656 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001657#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001658 } /* "usesrc" */
1659
1660 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1661#ifdef SO_BINDTODEVICE
1662 if (!*args[cur_arg + 1]) {
1663 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1664 file, linenum, args[0]);
1665 err_code |= ERR_ALERT | ERR_FATAL;
1666 goto out;
1667 }
1668 free(newsrv->conn_src.iface_name);
1669 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1670 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1671 global.last_checks |= LSTCHK_NETADM;
1672#else
1673 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1674 file, linenum, args[0], args[cur_arg]);
1675 err_code |= ERR_ALERT | ERR_FATAL;
1676 goto out;
1677#endif
1678 cur_arg += 2;
1679 continue;
1680 }
1681 /* this keyword in not an option of "source" */
1682 break;
1683 } /* while */
1684 }
1685 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1686 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1687 file, linenum, "usesrc", "source");
1688 err_code |= ERR_ALERT | ERR_FATAL;
1689 goto out;
1690 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001691 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1692#ifdef CONFIG_HAP_NS
1693 char *arg = args[cur_arg + 1];
1694 if (!strcmp(arg, "*")) {
1695 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1696 } else {
1697 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1698
1699 if (newsrv->netns == NULL)
1700 newsrv->netns = netns_store_insert(arg);
1701
1702 if (newsrv->netns == NULL) {
1703 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1704 err_code |= ERR_ALERT | ERR_FATAL;
1705 goto out;
1706 }
1707 }
1708#else
1709 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1710 file, linenum, args[0], args[cur_arg]);
1711 err_code |= ERR_ALERT | ERR_FATAL;
1712 goto out;
1713#endif
1714 cur_arg += 2;
1715 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001716 else {
1717 static int srv_dumped;
1718 struct srv_kw *kw;
1719 char *err;
1720
1721 kw = srv_find_kw(args[cur_arg]);
1722 if (kw) {
1723 char *err = NULL;
1724 int code;
1725
1726 if (!kw->parse) {
1727 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1728 file, linenum, args[0], args[1], args[cur_arg]);
1729 cur_arg += 1 + kw->skip ;
1730 err_code |= ERR_ALERT | ERR_FATAL;
1731 goto out;
1732 }
1733
1734 if (defsrv && !kw->default_ok) {
1735 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1736 file, linenum, args[0], args[1], args[cur_arg]);
1737 cur_arg += 1 + kw->skip ;
1738 err_code |= ERR_ALERT;
1739 continue;
1740 }
1741
1742 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1743 err_code |= code;
1744
1745 if (code) {
1746 if (err && *err) {
1747 indent_msg(&err, 2);
1748 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1749 }
1750 else
1751 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1752 file, linenum, args[0], args[1], args[cur_arg]);
1753 if (code & ERR_FATAL) {
1754 free(err);
1755 cur_arg += 1 + kw->skip;
1756 goto out;
1757 }
1758 }
1759 free(err);
1760 cur_arg += 1 + kw->skip;
1761 continue;
1762 }
1763
1764 err = NULL;
1765 if (!srv_dumped) {
1766 srv_dump_kws(&err);
1767 indent_msg(&err, 4);
1768 srv_dumped = 1;
1769 }
1770
1771 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1772 file, linenum, args[0], args[1], args[cur_arg],
1773 err ? " Registered keywords :" : "", err ? err : "");
1774 free(err);
1775
1776 err_code |= ERR_ALERT | ERR_FATAL;
1777 goto out;
1778 }
1779 }
1780
Willy Tarreau272adea2014-03-31 10:39:59 +02001781 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001782 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001783
1784 if (newsrv->trackit) {
1785 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1786 file, linenum);
1787 err_code |= ERR_ALERT | ERR_FATAL;
1788 goto out;
1789 }
1790
Willy Tarreau272adea2014-03-31 10:39:59 +02001791 /*
1792 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001793 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001794 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001795 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001796 (is_inet_addr(&newsrv->check.addr) ||
1797 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001798 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001799 struct list *l;
1800
1801 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1802 if (!r) {
1803 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1804 file, linenum, newsrv->id);
1805 err_code |= ERR_ALERT | ERR_FATAL;
1806 goto out;
1807 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001808 /* search the first action (connect / send / expect) in the list */
1809 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001810 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001811 if (r->action != TCPCHK_ACT_COMMENT)
1812 break;
1813 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001814 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1815 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",
1816 file, linenum, newsrv->id);
1817 err_code |= ERR_ALERT | ERR_FATAL;
1818 goto out;
1819 }
1820 else {
1821 /* scan the tcp-check ruleset to ensure a port has been configured */
1822 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001823 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001824 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1825 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",
1826 file, linenum, newsrv->id);
1827 err_code |= ERR_ALERT | ERR_FATAL;
1828 goto out;
1829 }
1830 }
1831 }
1832 }
1833
1834 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001835 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001836 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001837 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1838 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001839 goto out;
1840 }
1841
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001842 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001843 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001844
Willy Tarreau272adea2014-03-31 10:39:59 +02001845 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1846 }
1847
1848 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001849 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001850
1851 if (!newsrv->agent.port) {
1852 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1853 file, linenum, newsrv->id);
1854 err_code |= ERR_ALERT | ERR_FATAL;
1855 goto out;
1856 }
1857
1858 if (!newsrv->agent.inter)
1859 newsrv->agent.inter = newsrv->check.inter;
1860
Simon Hormanb1900d52015-01-30 11:22:54 +09001861 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001862 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001863 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1864 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001865 goto out;
1866 }
1867
1868 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1869 }
1870
1871 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001872 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001873 curproxy->srv_bck++;
1874 else
1875 curproxy->srv_act++;
1876
Willy Tarreauc5150da2014-05-13 19:27:31 +02001877 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001878 }
1879 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001880 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001881 return 0;
1882
1883 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001884 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001885 free(errmsg);
1886 return err_code;
1887}
1888
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001889/* Returns a pointer to the first server matching either id <id>.
1890 * NULL is returned if no match is found.
1891 * the lookup is performed in the backend <bk>
1892 */
1893struct server *server_find_by_id(struct proxy *bk, int id)
1894{
1895 struct eb32_node *eb32;
1896 struct server *curserver;
1897
1898 if (!bk || (id ==0))
1899 return NULL;
1900
1901 /* <bk> has no backend capabilities, so it can't have a server */
1902 if (!(bk->cap & PR_CAP_BE))
1903 return NULL;
1904
1905 curserver = NULL;
1906
1907 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1908 if (eb32)
1909 curserver = container_of(eb32, struct server, conf.id);
1910
1911 return curserver;
1912}
1913
1914/* Returns a pointer to the first server matching either name <name>, or id
1915 * if <name> starts with a '#'. NULL is returned if no match is found.
1916 * the lookup is performed in the backend <bk>
1917 */
1918struct server *server_find_by_name(struct proxy *bk, const char *name)
1919{
1920 struct server *curserver;
1921
1922 if (!bk || !name)
1923 return NULL;
1924
1925 /* <bk> has no backend capabilities, so it can't have a server */
1926 if (!(bk->cap & PR_CAP_BE))
1927 return NULL;
1928
1929 curserver = NULL;
1930 if (*name == '#') {
1931 curserver = server_find_by_id(bk, atoi(name + 1));
1932 if (curserver)
1933 return curserver;
1934 }
1935 else {
1936 curserver = bk->srv;
1937
1938 while (curserver && (strcmp(curserver->id, name) != 0))
1939 curserver = curserver->next;
1940
1941 if (curserver)
1942 return curserver;
1943 }
1944
1945 return NULL;
1946}
1947
1948struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
1949{
1950 struct server *byname;
1951 struct server *byid;
1952
1953 if (!name && !id)
1954 return NULL;
1955
1956 if (diff)
1957 *diff = 0;
1958
1959 byname = byid = NULL;
1960
1961 if (name) {
1962 byname = server_find_by_name(bk, name);
1963 if (byname && (!id || byname->puid == id))
1964 return byname;
1965 }
1966
1967 /* remaining possibilities :
1968 * - name not set
1969 * - name set but not found
1970 * - name found but ID doesn't match
1971 */
1972 if (id) {
1973 byid = server_find_by_id(bk, id);
1974 if (byid) {
1975 if (byname) {
1976 /* use id only if forced by configuration */
1977 if (byid->flags & SRV_F_FORCED_ID) {
1978 if (diff)
1979 *diff |= 2;
1980 return byid;
1981 }
1982 else {
1983 if (diff)
1984 *diff |= 1;
1985 return byname;
1986 }
1987 }
1988
1989 /* remaining possibilities:
1990 * - name not set
1991 * - name set but not found
1992 */
1993 if (name && diff)
1994 *diff |= 2;
1995 return byid;
1996 }
1997
1998 /* id bot found */
1999 if (byname) {
2000 if (diff)
2001 *diff |= 1;
2002 return byname;
2003 }
2004 }
2005
2006 return NULL;
2007}
2008
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002009/* Update a server state using the parameters available in the params list */
2010static void srv_update_state(struct server *srv, int version, char **params)
2011{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002012 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002013 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002014
2015 /* fields since version 1
2016 * and common to all other upcoming versions
2017 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002018 enum srv_state srv_op_state;
2019 enum srv_admin srv_admin_state;
2020 unsigned srv_uweight, srv_iweight;
2021 unsigned long srv_last_time_change;
2022 short srv_check_status;
2023 enum chk_result srv_check_result;
2024 int srv_check_health;
2025 int srv_check_state, srv_agent_state;
2026 int bk_f_forced_id;
2027 int srv_f_forced_id;
2028
Willy Tarreau31138fa2015-09-29 18:38:47 +02002029 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002030 switch (version) {
2031 case 1:
2032 /*
2033 * now we can proceed with server's state update:
2034 * srv_addr: params[0]
2035 * srv_op_state: params[1]
2036 * srv_admin_state: params[2]
2037 * srv_uweight: params[3]
2038 * srv_iweight: params[4]
2039 * srv_last_time_change: params[5]
2040 * srv_check_status: params[6]
2041 * srv_check_result: params[7]
2042 * srv_check_health: params[8]
2043 * srv_check_state: params[9]
2044 * srv_agent_state: params[10]
2045 * bk_f_forced_id: params[11]
2046 * srv_f_forced_id: params[12]
2047 */
2048
2049 /* validating srv_op_state */
2050 p = NULL;
2051 errno = 0;
2052 srv_op_state = strtol(params[1], &p, 10);
2053 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2054 (srv_op_state != SRV_ST_STOPPED &&
2055 srv_op_state != SRV_ST_STARTING &&
2056 srv_op_state != SRV_ST_RUNNING &&
2057 srv_op_state != SRV_ST_STOPPING)) {
2058 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2059 }
2060
2061 /* validating srv_admin_state */
2062 p = NULL;
2063 errno = 0;
2064 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002065
2066 /* inherited statuses will be recomputed later */
2067 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2068
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002069 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2070 (srv_admin_state != 0 &&
2071 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002072 srv_admin_state != SRV_ADMF_CMAINT &&
2073 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002074 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002075 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002076 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2077 }
2078
2079 /* validating srv_uweight */
2080 p = NULL;
2081 errno = 0;
2082 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002083 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002084 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2085
2086 /* validating srv_iweight */
2087 p = NULL;
2088 errno = 0;
2089 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002090 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002091 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2092
2093 /* validating srv_last_time_change */
2094 p = NULL;
2095 errno = 0;
2096 srv_last_time_change = strtol(params[5], &p, 10);
2097 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2098 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2099
2100 /* validating srv_check_status */
2101 p = NULL;
2102 errno = 0;
2103 srv_check_status = strtol(params[6], &p, 10);
2104 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2105 (srv_check_status >= HCHK_STATUS_SIZE))
2106 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2107
2108 /* validating srv_check_result */
2109 p = NULL;
2110 errno = 0;
2111 srv_check_result = strtol(params[7], &p, 10);
2112 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2113 (srv_check_result != CHK_RES_UNKNOWN &&
2114 srv_check_result != CHK_RES_NEUTRAL &&
2115 srv_check_result != CHK_RES_FAILED &&
2116 srv_check_result != CHK_RES_PASSED &&
2117 srv_check_result != CHK_RES_CONDPASS)) {
2118 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2119 }
2120
2121 /* validating srv_check_health */
2122 p = NULL;
2123 errno = 0;
2124 srv_check_health = strtol(params[8], &p, 10);
2125 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2126 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2127
2128 /* validating srv_check_state */
2129 p = NULL;
2130 errno = 0;
2131 srv_check_state = strtol(params[9], &p, 10);
2132 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2133 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2134 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2135
2136 /* validating srv_agent_state */
2137 p = NULL;
2138 errno = 0;
2139 srv_agent_state = strtol(params[10], &p, 10);
2140 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2141 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2142 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2143
2144 /* validating bk_f_forced_id */
2145 p = NULL;
2146 errno = 0;
2147 bk_f_forced_id = strtol(params[11], &p, 10);
2148 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2149 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2150
2151 /* validating srv_f_forced_id */
2152 p = NULL;
2153 errno = 0;
2154 srv_f_forced_id = strtol(params[12], &p, 10);
2155 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2156 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2157
2158
2159 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002160 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002161 goto out;
2162
2163 /* recover operational state and apply it to this server
2164 * and all servers tracking this one */
2165 switch (srv_op_state) {
2166 case SRV_ST_STOPPED:
2167 srv->check.health = 0;
2168 srv_set_stopped(srv, "changed from server-state after a reload");
2169 break;
2170 case SRV_ST_STARTING:
2171 srv->state = srv_op_state;
2172 break;
2173 case SRV_ST_STOPPING:
2174 srv->check.health = srv->check.rise + srv->check.fall - 1;
2175 srv_set_stopping(srv, "changed from server-state after a reload");
2176 break;
2177 case SRV_ST_RUNNING:
2178 srv->check.health = srv->check.rise + srv->check.fall - 1;
2179 srv_set_running(srv, "");
2180 break;
2181 }
2182
2183 /* When applying server state, the following rules apply:
2184 * - in case of a configuration change, we apply the setting from the new
2185 * configuration, regardless of old running state
2186 * - if no configuration change, we apply old running state only if old running
2187 * state is different from new configuration state
2188 */
2189 /* configuration has changed */
2190 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2191 if (srv->admin & SRV_ADMF_CMAINT)
2192 srv_adm_set_maint(srv);
2193 else
2194 srv_adm_set_ready(srv);
2195 }
2196 /* configuration is the same, let's compate old running state and new conf state */
2197 else {
2198 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2199 srv_adm_set_maint(srv);
2200 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2201 srv_adm_set_ready(srv);
2202 }
2203 /* apply drain mode if server is currently enabled */
2204 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2205 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002206 * (srv->iweight is the weight set up in configuration).
2207 * There are two possible reasons for FDRAIN to have been present :
2208 * - previous config weight was zero
2209 * - "set server b/s drain" was sent to the CLI
2210 *
2211 * In the first case, we simply want to drop this drain state
2212 * if the new weight is not zero anymore, meaning the administrator
2213 * has intentionally turned the weight back to a positive value to
2214 * enable the server again after an operation. In the second case,
2215 * the drain state was forced on the CLI regardless of the config's
2216 * weight so we don't want a change to the config weight to lose this
2217 * status. What this means is :
2218 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2219 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002220 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002221 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002222 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002223 }
2224
2225 srv->last_change = date.tv_sec - srv_last_time_change;
2226 srv->check.status = srv_check_status;
2227 srv->check.result = srv_check_result;
2228 srv->check.health = srv_check_health;
2229
2230 /* Only case we want to apply is removing ENABLED flag which could have been
2231 * done by the "disable health" command over the stats socket
2232 */
2233 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2234 (srv_check_state & CHK_ST_CONFIGURED) &&
2235 !(srv_check_state & CHK_ST_ENABLED))
2236 srv->check.state &= ~CHK_ST_ENABLED;
2237
2238 /* Only case we want to apply is removing ENABLED flag which could have been
2239 * done by the "disable agent" command over the stats socket
2240 */
2241 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2242 (srv_agent_state & CHK_ST_CONFIGURED) &&
2243 !(srv_agent_state & CHK_ST_ENABLED))
2244 srv->agent.state &= ~CHK_ST_ENABLED;
2245
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002246 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2247 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002248 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002249 * It means that a configuration file change has precedence over a unix socket change
2250 * for server's weight
2251 *
2252 * by default, HAProxy applies the following weight when parsing the configuration
2253 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002254 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002255 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002256 srv->uweight = srv_uweight;
2257 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002258 server_recalc_eweight(srv);
2259
2260 /* update server IP only if DNS resolution is used on the server */
2261 if (srv->resolution) {
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002262 struct sockaddr_storage addr;
2263
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002264 memset(&addr, 0, sizeof(struct sockaddr_storage));
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002265
2266 if (str2ip2(params[0], &addr, AF_UNSPEC)) {
2267 int port;
2268
2269 /* save the port, applies the new IP then reconfigure the port */
Willy Tarreauf3c7a832016-01-21 13:51:56 +01002270 port = get_host_port(&srv->addr);
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002271 srv->addr.ss_family = addr.ss_family;
2272 str2ip2(params[0], &srv->addr, srv->addr.ss_family);
2273 set_host_port(&srv->addr, port);
2274 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002275 else
2276 chunk_appendf(msg, ", can't parse IP: %s", params[0]);
2277 }
2278 break;
2279 default:
2280 chunk_appendf(msg, ", version '%d' not supported", version);
2281 }
2282
2283 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002284 if (msg->len) {
2285 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002286 Warning("server-state application failed for server '%s/%s'%s",
2287 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002288 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002289}
2290
2291/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2292 * For each proxy, it does the following:
2293 * - opens its server state file (either one or local one)
2294 * - read whole file, line by line
2295 * - analyse each line to check if it matches our current backend:
2296 * - backend name matches
2297 * - backend id matches if id is forced and name doesn't match
2298 * - if the server pointed by the line is found, then state is applied
2299 *
2300 * If the running backend uuid or id differs from the state file, then HAProxy reports
2301 * a warning.
2302 */
2303void apply_server_state(void)
2304{
2305 char *cur, *end;
2306 char mybuf[SRV_STATE_LINE_MAXLEN];
2307 int mybuflen;
2308 char *params[SRV_STATE_FILE_MAX_FIELDS];
2309 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2310 int arg, srv_arg, version, diff;
2311 FILE *f;
2312 char *filepath;
2313 char globalfilepath[MAXPATHLEN + 1];
2314 char localfilepath[MAXPATHLEN + 1];
2315 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2316 extern struct proxy *proxy;
2317 struct proxy *curproxy, *bk;
2318 struct server *srv;
2319
2320 globalfilepathlen = 0;
2321 /* create the globalfilepath variable */
2322 if (global.server_state_file) {
2323 /* absolute path or no base directory provided */
2324 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2325 len = strlen(global.server_state_file);
2326 if (len > MAXPATHLEN) {
2327 globalfilepathlen = 0;
2328 goto globalfileerror;
2329 }
2330 memcpy(globalfilepath, global.server_state_file, len);
2331 globalfilepath[len] = '\0';
2332 globalfilepathlen = len;
2333 }
2334 else if (global.server_state_base) {
2335 len = strlen(global.server_state_base);
2336 globalfilepathlen += len;
2337
2338 if (globalfilepathlen > MAXPATHLEN) {
2339 globalfilepathlen = 0;
2340 goto globalfileerror;
2341 }
2342 strncpy(globalfilepath, global.server_state_base, len);
2343 globalfilepath[globalfilepathlen] = 0;
2344
2345 /* append a slash if needed */
2346 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2347 if (globalfilepathlen + 1 > MAXPATHLEN) {
2348 globalfilepathlen = 0;
2349 goto globalfileerror;
2350 }
2351 globalfilepath[globalfilepathlen++] = '/';
2352 }
2353
2354 len = strlen(global.server_state_file);
2355 if (globalfilepathlen + len > MAXPATHLEN) {
2356 globalfilepathlen = 0;
2357 goto globalfileerror;
2358 }
2359 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2360 globalfilepathlen += len;
2361 globalfilepath[globalfilepathlen++] = 0;
2362 }
2363 }
2364 globalfileerror:
2365 if (globalfilepathlen == 0)
2366 globalfilepath[0] = '\0';
2367
2368 /* read servers state from local file */
2369 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2370 /* servers are only in backends */
2371 if (!(curproxy->cap & PR_CAP_BE))
2372 continue;
2373 fileopenerr = 0;
2374 filepath = NULL;
2375
2376 /* search server state file path and name */
2377 switch (curproxy->load_server_state_from_file) {
2378 /* read servers state from global file */
2379 case PR_SRV_STATE_FILE_GLOBAL:
2380 /* there was an error while generating global server state file path */
2381 if (globalfilepathlen == 0)
2382 continue;
2383 filepath = globalfilepath;
2384 fileopenerr = 1;
2385 break;
2386 /* this backend has its own file */
2387 case PR_SRV_STATE_FILE_LOCAL:
2388 localfilepathlen = 0;
2389 localfilepath[0] = '\0';
2390 len = 0;
2391 /* create the localfilepath variable */
2392 /* absolute path or no base directory provided */
2393 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2394 len = strlen(curproxy->server_state_file_name);
2395 if (len > MAXPATHLEN) {
2396 localfilepathlen = 0;
2397 goto localfileerror;
2398 }
2399 memcpy(localfilepath, curproxy->server_state_file_name, len);
2400 localfilepath[len] = '\0';
2401 localfilepathlen = len;
2402 }
2403 else if (global.server_state_base) {
2404 len = strlen(global.server_state_base);
2405 localfilepathlen += len;
2406
2407 if (localfilepathlen > MAXPATHLEN) {
2408 localfilepathlen = 0;
2409 goto localfileerror;
2410 }
2411 strncpy(localfilepath, global.server_state_base, len);
2412 localfilepath[localfilepathlen] = 0;
2413
2414 /* append a slash if needed */
2415 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2416 if (localfilepathlen + 1 > MAXPATHLEN) {
2417 localfilepathlen = 0;
2418 goto localfileerror;
2419 }
2420 localfilepath[localfilepathlen++] = '/';
2421 }
2422
2423 len = strlen(curproxy->server_state_file_name);
2424 if (localfilepathlen + len > MAXPATHLEN) {
2425 localfilepathlen = 0;
2426 goto localfileerror;
2427 }
2428 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2429 localfilepathlen += len;
2430 localfilepath[localfilepathlen++] = 0;
2431 }
2432 filepath = localfilepath;
2433 localfileerror:
2434 if (localfilepathlen == 0)
2435 localfilepath[0] = '\0';
2436
2437 break;
2438 case PR_SRV_STATE_FILE_NONE:
2439 default:
2440 continue;
2441 }
2442
2443 /* preload global state file */
2444 errno = 0;
2445 f = fopen(filepath, "r");
2446 if (errno && fileopenerr)
2447 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2448 if (!f)
2449 continue;
2450
2451 mybuf[0] = '\0';
2452 mybuflen = 0;
2453 version = 0;
2454
2455 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002456 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2457 Warning("Can't read first line of the server state file '%s'\n", filepath);
2458 goto fileclose;
2459 }
2460
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002461 cur = mybuf;
2462 version = atoi(cur);
2463 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2464 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002465 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002466
2467 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2468 int bk_f_forced_id = 0;
2469 int check_id = 0;
2470 int check_name = 0;
2471
2472 mybuflen = strlen(mybuf);
2473 cur = mybuf;
2474 end = cur + mybuflen;
2475
2476 bk = NULL;
2477 srv = NULL;
2478
2479 /* we need at least one character */
2480 if (mybuflen == 0)
2481 continue;
2482
2483 /* ignore blank characters at the beginning of the line */
2484 while (isspace(*cur))
2485 ++cur;
2486
2487 if (cur == end)
2488 continue;
2489
2490 /* ignore comment line */
2491 if (*cur == '#')
2492 continue;
2493
2494 /* we're now ready to move the line into *srv_params[] */
2495 params[0] = cur;
2496 arg = 1;
2497 srv_arg = 0;
2498 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2499 if (isspace(*cur)) {
2500 *cur = '\0';
2501 ++cur;
2502 while (isspace(*cur))
2503 ++cur;
2504 switch (version) {
2505 case 1:
2506 /*
2507 * srv_addr: params[4] => srv_params[0]
2508 * srv_op_state: params[5] => srv_params[1]
2509 * srv_admin_state: params[6] => srv_params[2]
2510 * srv_uweight: params[7] => srv_params[3]
2511 * srv_iweight: params[8] => srv_params[4]
2512 * srv_last_time_change: params[9] => srv_params[5]
2513 * srv_check_status: params[10] => srv_params[6]
2514 * srv_check_result: params[11] => srv_params[7]
2515 * srv_check_health: params[12] => srv_params[8]
2516 * srv_check_state: params[13] => srv_params[9]
2517 * srv_agent_state: params[14] => srv_params[10]
2518 * bk_f_forced_id: params[15] => srv_params[11]
2519 * srv_f_forced_id: params[16] => srv_params[12]
2520 */
2521 if (arg >= 4) {
2522 srv_params[srv_arg] = cur;
2523 ++srv_arg;
2524 }
2525 break;
2526 }
2527
2528 params[arg] = cur;
2529 ++arg;
2530 }
2531 else {
2532 ++cur;
2533 }
2534 }
2535
2536 /* if line is incomplete line, then ignore it.
2537 * otherwise, update useful flags */
2538 switch (version) {
2539 case 1:
2540 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2541 continue;
2542 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2543 check_id = (atoi(params[0]) == curproxy->uuid);
2544 check_name = (strcmp(curproxy->id, params[1]) == 0);
2545 break;
2546 }
2547
2548 diff = 0;
2549 bk = curproxy;
2550
2551 /* if backend can't be found, let's continue */
2552 if (!check_id && !check_name)
2553 continue;
2554 else if (!check_id && check_name) {
2555 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2556 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2557 }
2558 else if (check_id && !check_name) {
2559 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2560 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2561 /* if name doesn't match, we still want to update curproxy if the backend id
2562 * was forced in previous the previous configuration */
2563 if (!bk_f_forced_id)
2564 continue;
2565 }
2566
2567 /* look for the server by its id: param[2] */
2568 /* else look for the server by its name: param[3] */
2569 diff = 0;
2570 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2571
2572 if (!srv) {
2573 /* if no server found, then warning and continue with next line */
2574 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2575 params[3], params[2], params[0], params[1]);
2576 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2577 params[3], params[2], params[0], params[1]);
2578 continue;
2579 }
2580 else if (diff & PR_FBM_MISMATCH_ID) {
2581 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);
2582 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);
2583 }
2584 else if (diff & PR_FBM_MISMATCH_NAME) {
2585 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);
2586 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);
2587 }
2588
2589 /* now we can proceed with server's state update */
2590 srv_update_state(srv, version, srv_params);
2591 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002592fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002593 fclose(f);
2594 }
2595}
2596
Simon Horman7d09b9a2013-02-12 10:45:51 +09002597/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002598 * update a server's current IP address.
2599 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2600 * ip is in network format.
2601 * updater is a string which contains an information about the requester of the update.
2602 * updater is used if not NULL.
2603 *
2604 * A log line and a stderr warning message is generated based on server's backend options.
2605 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002606int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002607{
2608 /* generates a log line and a warning on stderr */
2609 if (1) {
2610 /* book enough space for both IPv4 and IPv6 */
2611 char oldip[INET6_ADDRSTRLEN];
2612 char newip[INET6_ADDRSTRLEN];
2613
2614 memset(oldip, '\0', INET6_ADDRSTRLEN);
2615 memset(newip, '\0', INET6_ADDRSTRLEN);
2616
2617 /* copy old IP address in a string */
2618 switch (s->addr.ss_family) {
2619 case AF_INET:
2620 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2621 break;
2622 case AF_INET6:
2623 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2624 break;
2625 };
2626
2627 /* copy new IP address in a string */
2628 switch (ip_sin_family) {
2629 case AF_INET:
2630 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2631 break;
2632 case AF_INET6:
2633 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2634 break;
2635 };
2636
2637 /* save log line into a buffer */
2638 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2639 s->proxy->id, s->id, oldip, newip, updater);
2640
2641 /* write the buffer on stderr */
2642 Warning("%s.\n", trash.str);
2643
2644 /* send a log */
2645 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2646 }
2647
2648 /* save the new IP family */
2649 s->addr.ss_family = ip_sin_family;
2650 /* save the new IP address */
2651 switch (ip_sin_family) {
2652 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002653 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002654 break;
2655 case AF_INET6:
2656 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2657 break;
2658 };
2659
2660 return 0;
2661}
2662
2663/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002664 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2665 *
2666 * Caller can pass its name through <updater> to get it integrated in the response message
2667 * returned by the function.
2668 *
2669 * The function first does the following, in that order:
2670 * - validates the new addr and/or port
2671 * - checks if an update is required (new IP or port is different than current ones)
2672 * - checks the update is allowed:
2673 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2674 * - allow all changes if no CHECKS are configured
2675 * - if CHECK is configured:
2676 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2677 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2678 * conditions are met
2679 */
2680const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2681{
2682 struct sockaddr_storage sa;
2683 int ret, port_change_required;
2684 char current_addr[INET6_ADDRSTRLEN];
2685 u_int16_t current_port, new_port;
2686 struct chunk *msg;
2687
2688 msg = get_trash_chunk();
2689 chunk_reset(msg);
2690
2691 if (addr) {
2692 memset(&sa, 0, sizeof(struct sockaddr_storage));
2693 if (str2ip2(addr, &sa, 0) == NULL) {
2694 chunk_printf(msg, "Invalid addr '%s'", addr);
2695 goto out;
2696 }
2697
2698 /* changes are allowed on AF_INET* families only */
2699 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2700 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2701 goto out;
2702 }
2703
2704 /* collecting data currently setup */
2705 memset(current_addr, '\0', sizeof(current_addr));
2706 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2707 /* changes are allowed on AF_INET* families only */
2708 if ((ret != AF_INET) && (ret != AF_INET6)) {
2709 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2710 goto out;
2711 }
2712
2713 /* applying ADDR changes if required and allowed
2714 * ipcmp returns 0 when both ADDR are the same
2715 */
2716 if (ipcmp(&s->addr, &sa) == 0) {
2717 chunk_appendf(msg, "no need to change the addr");
2718 goto port;
2719 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002720 ipcpy(&sa, &s->addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002721
2722 /* we also need to update check's ADDR only if it uses the server's one */
2723 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002724 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002725 }
2726
2727 /* we also need to update agent ADDR only if it use the server's one */
2728 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002729 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002730 }
2731
2732 /* update report for caller */
2733 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2734 }
2735
2736 port:
2737 if (port) {
2738 char sign = '\0';
2739 char *endptr;
2740
2741 if (addr)
2742 chunk_appendf(msg, ", ");
2743
2744 /* collecting data currently setup */
2745 current_port = get_host_port(&s->addr);
2746
2747 /* check if PORT change is required */
2748 port_change_required = 0;
2749
2750 sign = *port;
2751 new_port = strtol(port, &endptr, 10);
2752 if ((errno != 0) || (port == endptr)) {
2753 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2754 goto out;
2755 }
2756
2757 /* check if caller triggers a port mapped or offset */
2758 if (sign == '-' || (sign == '+')) {
2759 /* check if server currently uses port map */
2760 if (!(s->flags & SRV_F_MAPPORTS)) {
2761 /* switch from fixed port to port map mandatorily triggers
2762 * a port change */
2763 port_change_required = 1;
2764 /* check is configured
2765 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2766 * prevent PORT change if check doesn't have it's dedicated port while switching
2767 * to port mapping */
2768 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2769 chunk_appendf(msg, "can't change <port> to port map because it is incompatible with current health check port configuration (use 'port' statement from the 'server' directive.");
2770 goto out;
2771 }
2772 }
2773 /* we're already using port maps */
2774 else {
2775 port_change_required = current_port != new_port;
2776 }
2777 }
2778 /* fixed port */
2779 else {
2780 port_change_required = current_port != new_port;
2781 }
2782
2783 /* applying PORT changes if required and update response message */
2784 if (port_change_required) {
2785 /* apply new port */
2786 set_host_port(&s->addr, new_port);
2787
2788 /* prepare message */
2789 chunk_appendf(msg, "port changed from '");
2790 if (s->flags & SRV_F_MAPPORTS)
2791 chunk_appendf(msg, "+");
2792 chunk_appendf(msg, "%d' to '", current_port);
2793
2794 if (sign == '-') {
2795 s->flags |= SRV_F_MAPPORTS;
2796 chunk_appendf(msg, "%c", sign);
2797 /* just use for result output */
2798 new_port = -new_port;
2799 }
2800 else if (sign == '+') {
2801 s->flags |= SRV_F_MAPPORTS;
2802 chunk_appendf(msg, "%c", sign);
2803 }
2804 else {
2805 s->flags &= ~SRV_F_MAPPORTS;
2806 }
2807
2808 chunk_appendf(msg, "%d'", new_port);
2809
2810 /* we also need to update health checks port only if it uses server's realport */
2811 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2812 s->check.port = new_port;
2813 }
2814 }
2815 else {
2816 chunk_appendf(msg, "no need to change the port");
2817 }
2818 }
2819
2820out:
2821 if (updater)
2822 chunk_appendf(msg, " by '%s'", updater);
2823 chunk_appendf(msg, "\n");
2824 return msg->str;
2825}
2826
2827
2828/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002829 * update server status based on result of name resolution
2830 * returns:
2831 * 0 if server status is updated
2832 * 1 if server status has not changed
2833 */
2834int snr_update_srv_status(struct server *s)
2835{
2836 struct dns_resolution *resolution = s->resolution;
2837
2838 switch (resolution->status) {
2839 case RSLV_STATUS_NONE:
2840 /* status when HAProxy has just (re)started */
2841 trigger_resolution(s);
2842 break;
2843
2844 default:
2845 break;
2846 }
2847
2848 return 1;
2849}
2850
2851/*
2852 * Server Name Resolution valid response callback
2853 * It expects:
2854 * - <nameserver>: the name server which answered the valid response
2855 * - <response>: buffer containing a valid DNS response
2856 * - <response_len>: size of <response>
2857 * It performs the following actions:
2858 * - ignore response if current ip found and server family not met
2859 * - update with first new ip found if family is met and current IP is not found
2860 * returns:
2861 * 0 on error
2862 * 1 when no error or safe ignore
2863 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002864int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002865{
2866 struct server *s;
2867 void *serverip, *firstip;
2868 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002869 int ret;
2870 struct chunk *chk = get_trash_chunk();
2871
2872 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002873 firstip = NULL; /* pointer to the first valid response found */
2874 /* it will be used as the new IP if a change is required */
2875 firstip_sin_family = AF_UNSPEC;
2876 serverip = NULL; /* current server IP address */
2877
2878 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002879 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002880
2881 /* initializing server IP pointer */
2882 server_sin_family = s->addr.ss_family;
2883 switch (server_sin_family) {
2884 case AF_INET:
2885 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
2886 break;
2887
2888 case AF_INET6:
2889 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
2890 break;
2891
2892 default:
2893 goto invalid;
2894 }
2895
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002896 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01002897 serverip, server_sin_family, &firstip,
2898 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002899
2900 switch (ret) {
2901 case DNS_UPD_NO:
2902 if (resolution->status != RSLV_STATUS_VALID) {
2903 resolution->status = RSLV_STATUS_VALID;
2904 resolution->last_status_change = now_ms;
2905 }
2906 goto stop_resolution;
2907
2908 case DNS_UPD_SRVIP_NOT_FOUND:
2909 goto save_ip;
2910
2911 case DNS_UPD_CNAME:
2912 if (resolution->status != RSLV_STATUS_VALID) {
2913 resolution->status = RSLV_STATUS_VALID;
2914 resolution->last_status_change = now_ms;
2915 }
2916 goto invalid;
2917
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02002918 case DNS_UPD_NO_IP_FOUND:
2919 if (resolution->status != RSLV_STATUS_OTHER) {
2920 resolution->status = RSLV_STATUS_OTHER;
2921 resolution->last_status_change = now_ms;
2922 }
2923 goto stop_resolution;
2924
Baptiste Assmannfad03182015-10-28 02:03:32 +01002925 case DNS_UPD_NAME_ERROR:
2926 /* if this is not the last expected response, we ignore it */
2927 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
2928 return 0;
2929 /* update resolution status to OTHER error type */
2930 if (resolution->status != RSLV_STATUS_OTHER) {
2931 resolution->status = RSLV_STATUS_OTHER;
2932 resolution->last_status_change = now_ms;
2933 }
2934 goto stop_resolution;
2935
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002936 default:
2937 goto invalid;
2938
2939 }
2940
2941 save_ip:
2942 nameserver->counters.update += 1;
2943 if (resolution->status != RSLV_STATUS_VALID) {
2944 resolution->status = RSLV_STATUS_VALID;
2945 resolution->last_status_change = now_ms;
2946 }
2947
2948 /* save the first ip we found */
2949 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
2950 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
2951
2952 stop_resolution:
2953 /* update last resolution date and time */
2954 resolution->last_resolution = now_ms;
2955 /* reset current status flag */
2956 resolution->step = RSLV_STEP_NONE;
2957 /* reset values */
2958 dns_reset_resolution(resolution);
2959
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002960 dns_update_resolvers_timeout(nameserver->resolvers);
2961
2962 snr_update_srv_status(s);
2963 return 0;
2964
2965 invalid:
2966 nameserver->counters.invalid += 1;
2967 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
2968 goto stop_resolution;
2969
2970 snr_update_srv_status(s);
2971 return 0;
2972}
2973
2974/*
2975 * Server Name Resolution error management callback
2976 * returns:
2977 * 0 on error
2978 * 1 when no error or safe ignore
2979 */
2980int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
2981{
2982 struct server *s;
2983 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002984 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002985
2986 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002987 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002988 resolvers = resolution->resolvers;
2989
2990 /* can be ignored if this is not the last response */
2991 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
2992 return 1;
2993 }
2994
2995 switch (error_code) {
2996 case DNS_RESP_INVALID:
2997 case DNS_RESP_WRONG_NAME:
2998 if (resolution->status != RSLV_STATUS_INVALID) {
2999 resolution->status = RSLV_STATUS_INVALID;
3000 resolution->last_status_change = now_ms;
3001 }
3002 break;
3003
3004 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003005 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003006 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003007 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003008 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003009 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3010 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003011
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003012 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003013 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003014 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003015 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003016 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003017 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003018 }
3019 else if (res_preferred_afinet) {
3020 /* fallback from A to AAAA */
3021 resolution->query_type = DNS_RTYPE_AAAA;
3022 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003023 else {
3024 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003025 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003026 resolution->query_type = DNS_RTYPE_A;
3027 } else {
3028 resolution->query_type = DNS_RTYPE_AAAA;
3029 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003030 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003031
3032 dns_send_query(resolution);
3033
3034 /*
3035 * move the resolution to the last element of the FIFO queue
3036 * and update timeout wakeup based on the new first entry
3037 */
3038 if (dns_check_resolution_queue(resolvers) > 1) {
3039 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003040 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003041 /* ex first resolution goes to the end of the queue */
3042 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3043 }
3044 dns_update_resolvers_timeout(resolvers);
3045 goto leave;
3046 }
3047 else {
3048 if (resolution->status != RSLV_STATUS_OTHER) {
3049 resolution->status = RSLV_STATUS_OTHER;
3050 resolution->last_status_change = now_ms;
3051 }
3052 }
3053 break;
3054
3055 case DNS_RESP_NX_DOMAIN:
3056 if (resolution->status != RSLV_STATUS_NX) {
3057 resolution->status = RSLV_STATUS_NX;
3058 resolution->last_status_change = now_ms;
3059 }
3060 break;
3061
3062 case DNS_RESP_REFUSED:
3063 if (resolution->status != RSLV_STATUS_REFUSED) {
3064 resolution->status = RSLV_STATUS_REFUSED;
3065 resolution->last_status_change = now_ms;
3066 }
3067 break;
3068
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003069 case DNS_RESP_TIMEOUT:
3070 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3071 resolution->status = RSLV_STATUS_TIMEOUT;
3072 resolution->last_status_change = now_ms;
3073 }
3074 break;
3075 }
3076
3077 /* update last resolution date and time */
3078 resolution->last_resolution = now_ms;
3079 /* reset current status flag */
3080 resolution->step = RSLV_STEP_NONE;
3081 /* reset values */
3082 dns_reset_resolution(resolution);
3083
3084 LIST_DEL(&resolution->list);
3085 dns_update_resolvers_timeout(resolvers);
3086
3087 leave:
3088 snr_update_srv_status(s);
3089 return 1;
3090}
3091
3092/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093 * Local variables:
3094 * c-indent-level: 8
3095 * c-basic-offset: 8
3096 * End:
3097 */