blob: cc1f0d10ab0bd127b0bdcd59a112497cd5c47184 [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 Tarreaudff55432012-10-10 17:51:05 +0200713/* Note: must not be declared <const> as its list will be overwritten.
714 * Please take care of keeping this list alphabetically sorted, doing so helps
715 * all code contributors.
716 * Optional keywords are also declared with a NULL ->parse() function so that
717 * the config parser can report an appropriate error when a known keyword was
718 * not enabled.
719 */
720static struct srv_kw_list srv_kws = { "ALL", { }, {
721 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
722 { NULL, NULL, 0 },
723}};
724
725__attribute__((constructor))
726static void __listener_init(void)
727{
728 srv_register_keywords(&srv_kws);
729}
730
Willy Tarreau004e0452013-11-21 11:22:01 +0100731/* Recomputes the server's eweight based on its state, uweight, the current time,
732 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
733 * state is automatically disabled if the time is elapsed.
734 */
735void server_recalc_eweight(struct server *sv)
736{
737 struct proxy *px = sv->proxy;
738 unsigned w;
739
740 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
741 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200742 if (sv->state == SRV_ST_STARTING)
743 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100744 }
745
746 /* We must take care of not pushing the server to full throttle during slow starts.
747 * It must also start immediately, at least at the minimal step when leaving maintenance.
748 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200749 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100750 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
751 else
752 w = px->lbprm.wdiv;
753
754 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
755
756 /* now propagate the status change to any LB algorithms */
757 if (px->lbprm.update_server_eweight)
758 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200759 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100760 if (px->lbprm.set_server_status_up)
761 px->lbprm.set_server_status_up(sv);
762 }
763 else {
764 if (px->lbprm.set_server_status_down)
765 px->lbprm.set_server_status_down(sv);
766 }
767}
768
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900770 * Parses weight_str and configures sv accordingly.
771 * Returns NULL on success, error message string otherwise.
772 */
773const char *server_parse_weight_change_request(struct server *sv,
774 const char *weight_str)
775{
776 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900777 long int w;
778 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900779
780 px = sv->proxy;
781
782 /* if the weight is terminated with '%', it is set relative to
783 * the initial weight, otherwise it is absolute.
784 */
785 if (!*weight_str)
786 return "Require <weight> or <weight%>.\n";
787
Simon Hormanb796afa2013-02-12 10:45:53 +0900788 w = strtol(weight_str, &end, 10);
789 if (end == weight_str)
790 return "Empty weight string empty or preceded by garbage";
791 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900792 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900793 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900794 /* Avoid integer overflow */
795 if (w > 25600)
796 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900797 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900798 if (w > 256)
799 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900800 }
801 else if (w < 0 || w > 256)
802 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900803 else if (end[0] != '\0')
804 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900805
806 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
807 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
808
809 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100810 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900811
812 return NULL;
813}
814
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200815/*
Thierry Fournier09a91782016-02-24 08:25:39 +0100816 * Parses <addr_str> and configures <sv> accordingly. <from> precise
817 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200818 * Returns:
819 * - error string on error
820 * - NULL on success
821 */
822const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +0100823 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200824{
825 unsigned char ip[INET6_ADDRSTRLEN];
826
827 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100828 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200829 return NULL;
830 }
831 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100832 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200833 return NULL;
834 }
835
836 return "Could not understand IP address format.\n";
837}
838
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200839const char *server_parse_maxconn_change_request(struct server *sv,
840 const char *maxconn_str)
841{
842 long int v;
843 char *end;
844
845 if (!*maxconn_str)
846 return "Require <maxconn>.\n";
847
848 v = strtol(maxconn_str, &end, 10);
849 if (end == maxconn_str)
850 return "maxconn string empty or preceded by garbage";
851 else if (end[0] != '\0')
852 return "Trailing garbage in maxconn string";
853
854 if (sv->maxconn == sv->minconn) { // static maxconn
855 sv->maxconn = sv->minconn = v;
856 } else { // dynamic maxconn
857 sv->maxconn = v;
858 }
859
860 if (may_dequeue_tasks(sv, sv->proxy))
861 process_srv_queue(sv);
862
863 return NULL;
864}
865
Willy Tarreau272adea2014-03-31 10:39:59 +0200866int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
867{
868 struct server *newsrv = NULL;
869 const char *err;
870 char *errmsg = NULL;
871 int err_code = 0;
872 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +0200873 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +0200874
875 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
876 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +0200877 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
878
879 if (!defsrv && curproxy == defproxy) {
880 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
881 err_code |= ERR_ALERT | ERR_FATAL;
882 goto out;
883 }
884 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
885 err_code |= ERR_ALERT | ERR_FATAL;
886
887 if (!*args[2]) {
888 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
889 file, linenum, args[0]);
890 err_code |= ERR_ALERT | ERR_FATAL;
891 goto out;
892 }
893
894 err = invalid_char(args[1]);
895 if (err && !defsrv) {
896 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
897 file, linenum, *err, args[1]);
898 err_code |= ERR_ALERT | ERR_FATAL;
899 goto out;
900 }
901
902 if (!defsrv) {
903 struct sockaddr_storage *sk;
904 int port1, port2;
905 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200906 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200907
Vincent Bernat02779b62016-04-03 13:48:43 +0200908 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200909 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
910 err_code |= ERR_ALERT | ERR_ABORT;
911 goto out;
912 }
913
914 /* the servers are linked backwards first */
915 newsrv->next = curproxy->srv;
916 curproxy->srv = newsrv;
917 newsrv->proxy = curproxy;
918 newsrv->conf.file = strdup(file);
919 newsrv->conf.line = linenum;
920
921 newsrv->obj_type = OBJ_TYPE_SERVER;
922 LIST_INIT(&newsrv->actconns);
923 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +0200924 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +0200925 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +0200926 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +0200927 do_check = 0;
928 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200929 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200930 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200931 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200932 newsrv->last_change = now.tv_sec;
933 newsrv->id = strdup(args[1]);
934
935 /* several ways to check the port component :
936 * - IP => port=+0, relative (IPv4 only)
937 * - IP: => port=+0, relative
938 * - IP:N => port=N, absolute
939 * - IP:+N => port=+N, relative
940 * - IP:-N => port=-N, relative
941 */
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +0200942 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +0200943 if (!sk) {
944 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
945 err_code |= ERR_ALERT | ERR_FATAL;
946 goto out;
947 }
948
949 proto = protocol_by_family(sk->ss_family);
950 if (!proto || !proto->connect) {
951 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
952 file, linenum, args[0], args[1]);
953 err_code |= ERR_ALERT | ERR_FATAL;
954 goto out;
955 }
956
957 if (!port1 || !port2) {
958 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200959 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200960 }
961 else if (port1 != port2) {
962 /* port range */
963 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
964 file, linenum, args[0], args[1], args[2]);
965 err_code |= ERR_ALERT | ERR_FATAL;
966 goto out;
967 }
Willy Tarreau272adea2014-03-31 10:39:59 +0200968
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200969 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +0200970 newsrv->hostname = fqdn;
971 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200972 goto skip_name_resolution;
973
Willy Tarreau07101d52015-09-08 16:16:35 +0200974 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +0200975 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200976 goto skip_name_resolution;
977
978 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
979 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
980 goto skip_name_resolution;
981 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
982 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
983 file, linenum, args[2]);
984 err_code |= ERR_ALERT | ERR_FATAL;
985 goto out;
986 }
987
988 curr_resolution->requester = newsrv;
989 curr_resolution->requester_cb = snr_resolution_cb;
990 curr_resolution->requester_error_cb = snr_resolution_error_cb;
991 curr_resolution->status = RSLV_STATUS_NONE;
992 curr_resolution->step = RSLV_STEP_NONE;
993 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +0200994 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200995 newsrv->resolution = curr_resolution;
996
997 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +0200998 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +0100999 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +02001000
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001001 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001002 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1003 file, linenum, newsrv->addr.ss_family, args[2]);
1004 err_code |= ERR_ALERT | ERR_FATAL;
1005 goto out;
1006 }
1007
1008 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1009 newsrv->check.port = curproxy->defsrv.check.port;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001010 if (newsrv->check.port)
1011 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001012 newsrv->check.inter = curproxy->defsrv.check.inter;
1013 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1014 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1015 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1016 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001017 if (curproxy->defsrv.agent.send_string != NULL)
1018 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1019 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001020 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1021 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1022 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1023 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1024 newsrv->minconn = curproxy->defsrv.minconn;
1025 newsrv->maxconn = curproxy->defsrv.maxconn;
1026 newsrv->slowstart = curproxy->defsrv.slowstart;
1027 newsrv->onerror = curproxy->defsrv.onerror;
1028 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1029 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1030 newsrv->consecutive_errors_limit
1031 = curproxy->defsrv.consecutive_errors_limit;
1032#ifdef OPENSSL
1033 newsrv->use_ssl = curproxy->defsrv.use_ssl;
1034#endif
1035 newsrv->uweight = newsrv->iweight
1036 = curproxy->defsrv.iweight;
1037
1038 newsrv->check.status = HCHK_STATUS_INI;
1039 newsrv->check.rise = curproxy->defsrv.check.rise;
1040 newsrv->check.fall = curproxy->defsrv.check.fall;
1041 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1042 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001043 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001044
1045 newsrv->agent.status = HCHK_STATUS_INI;
1046 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1047 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1048 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1049 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001050 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1051 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1052 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001053 memcpy(newsrv->dns_opts.pref_net,
1054 curproxy->defsrv.dns_opts.pref_net,
1055 sizeof(newsrv->dns_opts.pref_net));
1056 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Willy Tarreau272adea2014-03-31 10:39:59 +02001057
1058 cur_arg = 3;
1059 } else {
1060 newsrv = &curproxy->defsrv;
1061 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001062 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001063 }
1064
1065 while (*args[cur_arg]) {
1066 if (!strcmp(args[cur_arg], "agent-check")) {
1067 global.maxsock++;
1068 do_agent = 1;
1069 cur_arg += 1;
1070 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1071 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1072 if (err) {
1073 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1074 file, linenum, *err, newsrv->id);
1075 err_code |= ERR_ALERT | ERR_FATAL;
1076 goto out;
1077 }
1078 if (val <= 0) {
1079 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1080 file, linenum, val, args[cur_arg], newsrv->id);
1081 err_code |= ERR_ALERT | ERR_FATAL;
1082 goto out;
1083 }
1084 newsrv->agent.inter = val;
1085 cur_arg += 2;
1086 }
1087 else if (!strcmp(args[cur_arg], "agent-port")) {
1088 global.maxsock++;
1089 newsrv->agent.port = atol(args[cur_arg + 1]);
1090 cur_arg += 2;
1091 }
James Brown55f9ff12015-10-21 18:19:05 -07001092 else if (!strcmp(args[cur_arg], "agent-send")) {
1093 global.maxsock++;
1094 free(newsrv->agent.send_string);
1095 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1096 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1097 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1098 cur_arg += 2;
1099 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001100 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1101 newsrv->cookie = strdup(args[cur_arg + 1]);
1102 newsrv->cklen = strlen(args[cur_arg + 1]);
1103 cur_arg += 2;
1104 }
1105 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1106 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1107 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1108 cur_arg += 2;
1109 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001110 else if (!strcmp(args[cur_arg], "resolvers")) {
1111 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1112 cur_arg += 2;
1113 }
1114 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1115 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001116 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001117 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001118 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001119 else {
1120 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1121 file, linenum, args[cur_arg]);
1122 err_code |= ERR_ALERT | ERR_FATAL;
1123 goto out;
1124 }
1125 cur_arg += 2;
1126 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001127 else if (!strcmp(args[cur_arg], "resolve-net")) {
1128 char *p, *e;
1129 unsigned char mask;
1130 struct dns_options *opt;
1131
1132 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1133 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1134 file, linenum, args[cur_arg]);
1135 err_code |= ERR_ALERT | ERR_FATAL;
1136 goto out;
1137 }
1138
1139 opt = &newsrv->dns_opts;
1140
1141 /* Split arguments by comma, and convert it from ipv4 or ipv6
1142 * string network in in_addr or in6_addr.
1143 */
1144 p = args[cur_arg + 1];
1145 e = p;
1146 while (*p != '\0') {
1147 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001148 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001149 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1150 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1151 err_code |= ERR_ALERT | ERR_FATAL;
1152 goto out;
1153 }
1154 /* look for end or comma. */
1155 while (*e != ',' && *e != '\0')
1156 e++;
1157 if (*e == ',') {
1158 *e = '\0';
1159 e++;
1160 }
1161 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1162 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1163 /* Try to convert input string from ipv4 or ipv6 network. */
1164 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1165 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1166 &mask)) {
1167 /* Try to convert input string from ipv6 network. */
1168 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1169 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1170 } else {
1171 /* All network conversions fail, retrun error. */
1172 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1173 file, linenum, args[cur_arg], p);
1174 err_code |= ERR_ALERT | ERR_FATAL;
1175 goto out;
1176 }
1177 opt->pref_net_nb++;
1178 p = e;
1179 }
1180
1181 cur_arg += 2;
1182 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001183 else if (!strcmp(args[cur_arg], "rise")) {
1184 if (!*args[cur_arg + 1]) {
1185 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1186 file, linenum, args[cur_arg]);
1187 err_code |= ERR_ALERT | ERR_FATAL;
1188 goto out;
1189 }
1190
1191 newsrv->check.rise = atol(args[cur_arg + 1]);
1192 if (newsrv->check.rise <= 0) {
1193 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1194 file, linenum, args[cur_arg]);
1195 err_code |= ERR_ALERT | ERR_FATAL;
1196 goto out;
1197 }
1198
1199 if (newsrv->check.health)
1200 newsrv->check.health = newsrv->check.rise;
1201 cur_arg += 2;
1202 }
1203 else if (!strcmp(args[cur_arg], "fall")) {
1204 newsrv->check.fall = atol(args[cur_arg + 1]);
1205
1206 if (!*args[cur_arg + 1]) {
1207 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1208 file, linenum, args[cur_arg]);
1209 err_code |= ERR_ALERT | ERR_FATAL;
1210 goto out;
1211 }
1212
1213 if (newsrv->check.fall <= 0) {
1214 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1215 file, linenum, args[cur_arg]);
1216 err_code |= ERR_ALERT | ERR_FATAL;
1217 goto out;
1218 }
1219
1220 cur_arg += 2;
1221 }
1222 else if (!strcmp(args[cur_arg], "inter")) {
1223 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1224 if (err) {
1225 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1226 file, linenum, *err, newsrv->id);
1227 err_code |= ERR_ALERT | ERR_FATAL;
1228 goto out;
1229 }
1230 if (val <= 0) {
1231 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1232 file, linenum, val, args[cur_arg], newsrv->id);
1233 err_code |= ERR_ALERT | ERR_FATAL;
1234 goto out;
1235 }
1236 newsrv->check.inter = val;
1237 cur_arg += 2;
1238 }
1239 else if (!strcmp(args[cur_arg], "fastinter")) {
1240 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1241 if (err) {
1242 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1243 file, linenum, *err, newsrv->id);
1244 err_code |= ERR_ALERT | ERR_FATAL;
1245 goto out;
1246 }
1247 if (val <= 0) {
1248 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1249 file, linenum, val, args[cur_arg], newsrv->id);
1250 err_code |= ERR_ALERT | ERR_FATAL;
1251 goto out;
1252 }
1253 newsrv->check.fastinter = val;
1254 cur_arg += 2;
1255 }
1256 else if (!strcmp(args[cur_arg], "downinter")) {
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 'downinter' 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.downinter = val;
1271 cur_arg += 2;
1272 }
1273 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1274 struct sockaddr_storage *sk;
1275 int port1, port2;
1276 struct protocol *proto;
1277
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001278 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001279 if (!sk) {
1280 Alert("parsing [%s:%d] : '%s' : %s\n",
1281 file, linenum, args[cur_arg], errmsg);
1282 err_code |= ERR_ALERT | ERR_FATAL;
1283 goto out;
1284 }
1285
1286 proto = protocol_by_family(sk->ss_family);
1287 if (!proto || !proto->connect) {
1288 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1289 file, linenum, args[cur_arg], args[cur_arg + 1]);
1290 err_code |= ERR_ALERT | ERR_FATAL;
1291 goto out;
1292 }
1293
1294 if (port1 != port2) {
1295 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1296 file, linenum, args[cur_arg], args[cur_arg + 1]);
1297 err_code |= ERR_ALERT | ERR_FATAL;
1298 goto out;
1299 }
1300
Simon Horman41f58762015-01-30 11:22:56 +09001301 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001302 newsrv->flags |= SRV_F_CHECKADDR;
1303 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001304 cur_arg += 2;
1305 }
1306 else if (!strcmp(args[cur_arg], "port")) {
1307 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001308 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001309 cur_arg += 2;
1310 }
1311 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001312 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001313 cur_arg ++;
1314 }
1315 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001316 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001317 cur_arg ++;
1318 }
1319 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001320 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001321 cur_arg ++;
1322 }
David Safb76832014-05-08 23:42:08 -04001323 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1324 newsrv->pp_opts |= SRV_PP_V2;
1325 cur_arg ++;
1326 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001327 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1328 newsrv->check.send_proxy = 1;
1329 cur_arg ++;
1330 }
1331 else if (!strcmp(args[cur_arg], "weight")) {
1332 int w;
1333 w = atol(args[cur_arg + 1]);
1334 if (w < 0 || w > SRV_UWGHT_MAX) {
1335 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1336 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1337 err_code |= ERR_ALERT | ERR_FATAL;
1338 goto out;
1339 }
1340 newsrv->uweight = newsrv->iweight = w;
1341 cur_arg += 2;
1342 }
1343 else if (!strcmp(args[cur_arg], "minconn")) {
1344 newsrv->minconn = atol(args[cur_arg + 1]);
1345 cur_arg += 2;
1346 }
1347 else if (!strcmp(args[cur_arg], "maxconn")) {
1348 newsrv->maxconn = atol(args[cur_arg + 1]);
1349 cur_arg += 2;
1350 }
1351 else if (!strcmp(args[cur_arg], "maxqueue")) {
1352 newsrv->maxqueue = atol(args[cur_arg + 1]);
1353 cur_arg += 2;
1354 }
1355 else if (!strcmp(args[cur_arg], "slowstart")) {
1356 /* slowstart is stored in seconds */
1357 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1358 if (err) {
1359 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1360 file, linenum, *err, newsrv->id);
1361 err_code |= ERR_ALERT | ERR_FATAL;
1362 goto out;
1363 }
1364 newsrv->slowstart = (val + 999) / 1000;
1365 cur_arg += 2;
1366 }
1367 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1368
1369 if (!*args[cur_arg + 1]) {
1370 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1371 file, linenum);
1372 err_code |= ERR_ALERT | ERR_FATAL;
1373 goto out;
1374 }
1375
1376 newsrv->trackit = strdup(args[cur_arg + 1]);
1377
1378 cur_arg += 2;
1379 }
1380 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1381 global.maxsock++;
1382 do_check = 1;
1383 cur_arg += 1;
1384 }
1385 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001386 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001387 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001388 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001389 newsrv->check.state |= CHK_ST_PAUSED;
1390 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001391 cur_arg += 1;
1392 }
1393 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1394 if (!strcmp(args[cur_arg + 1], "none"))
1395 newsrv->observe = HANA_OBS_NONE;
1396 else if (!strcmp(args[cur_arg + 1], "layer4"))
1397 newsrv->observe = HANA_OBS_LAYER4;
1398 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1399 if (curproxy->mode != PR_MODE_HTTP) {
1400 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1401 file, linenum, args[cur_arg + 1]);
1402 err_code |= ERR_ALERT;
1403 }
1404 newsrv->observe = HANA_OBS_LAYER7;
1405 }
1406 else {
1407 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1408 "'layer4', 'layer7' but got '%s'\n",
1409 file, linenum, args[cur_arg], args[cur_arg + 1]);
1410 err_code |= ERR_ALERT | ERR_FATAL;
1411 goto out;
1412 }
1413
1414 cur_arg += 2;
1415 }
1416 else if (!strcmp(args[cur_arg], "on-error")) {
1417 if (!strcmp(args[cur_arg + 1], "fastinter"))
1418 newsrv->onerror = HANA_ONERR_FASTINTER;
1419 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1420 newsrv->onerror = HANA_ONERR_FAILCHK;
1421 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1422 newsrv->onerror = HANA_ONERR_SUDDTH;
1423 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1424 newsrv->onerror = HANA_ONERR_MARKDWN;
1425 else {
1426 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1427 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1428 file, linenum, args[cur_arg], args[cur_arg + 1]);
1429 err_code |= ERR_ALERT | ERR_FATAL;
1430 goto out;
1431 }
1432
1433 cur_arg += 2;
1434 }
1435 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1436 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1437 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1438 else {
1439 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1440 file, linenum, args[cur_arg], args[cur_arg + 1]);
1441 err_code |= ERR_ALERT | ERR_FATAL;
1442 goto out;
1443 }
1444
1445 cur_arg += 2;
1446 }
1447 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1448 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1449 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1450 else {
1451 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1452 file, linenum, args[cur_arg], args[cur_arg + 1]);
1453 err_code |= ERR_ALERT | ERR_FATAL;
1454 goto out;
1455 }
1456
1457 cur_arg += 2;
1458 }
1459 else if (!strcmp(args[cur_arg], "error-limit")) {
1460 if (!*args[cur_arg + 1]) {
1461 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1462 file, linenum, args[cur_arg]);
1463 err_code |= ERR_ALERT | ERR_FATAL;
1464 goto out;
1465 }
1466
1467 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1468
1469 if (newsrv->consecutive_errors_limit <= 0) {
1470 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1471 file, linenum, args[cur_arg]);
1472 err_code |= ERR_ALERT | ERR_FATAL;
1473 goto out;
1474 }
1475 cur_arg += 2;
1476 }
1477 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1478 int port_low, port_high;
1479 struct sockaddr_storage *sk;
1480 struct protocol *proto;
1481
1482 if (!*args[cur_arg + 1]) {
1483 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1484 file, linenum, "source", "usesrc", "interface");
1485 err_code |= ERR_ALERT | ERR_FATAL;
1486 goto out;
1487 }
1488
1489 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001490 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001491 if (!sk) {
1492 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1493 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1494 err_code |= ERR_ALERT | ERR_FATAL;
1495 goto out;
1496 }
1497
1498 proto = protocol_by_family(sk->ss_family);
1499 if (!proto || !proto->connect) {
1500 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1501 file, linenum, args[cur_arg], args[cur_arg+1]);
1502 err_code |= ERR_ALERT | ERR_FATAL;
1503 goto out;
1504 }
1505
1506 newsrv->conn_src.source_addr = *sk;
1507
1508 if (port_low != port_high) {
1509 int i;
1510
1511 if (!port_low || !port_high) {
1512 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1513 file, linenum, args[cur_arg], args[cur_arg + 1]);
1514 err_code |= ERR_ALERT | ERR_FATAL;
1515 goto out;
1516 }
1517
1518 if (port_low <= 0 || port_low > 65535 ||
1519 port_high <= 0 || port_high > 65535 ||
1520 port_low > port_high) {
1521 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1522 file, linenum, port_low, port_high);
1523 err_code |= ERR_ALERT | ERR_FATAL;
1524 goto out;
1525 }
1526 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1527 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1528 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1529 }
1530
1531 cur_arg += 2;
1532 while (*(args[cur_arg])) {
1533 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001534#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001535 if (!*args[cur_arg + 1]) {
1536 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1537 file, linenum, "usesrc");
1538 err_code |= ERR_ALERT | ERR_FATAL;
1539 goto out;
1540 }
1541 if (!strcmp(args[cur_arg + 1], "client")) {
1542 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1543 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1544 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1545 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1546 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1547 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1548 char *name, *end;
1549
1550 name = args[cur_arg+1] + 7;
1551 while (isspace(*name))
1552 name++;
1553
1554 end = name;
1555 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1556 end++;
1557
1558 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1559 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1560 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1561 newsrv->conn_src.bind_hdr_len = end - name;
1562 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1563 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1564 newsrv->conn_src.bind_hdr_occ = -1;
1565
1566 /* now look for an occurrence number */
1567 while (isspace(*end))
1568 end++;
1569 if (*end == ',') {
1570 end++;
1571 name = end;
1572 if (*end == '-')
1573 end++;
1574 while (isdigit((int)*end))
1575 end++;
1576 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1577 }
1578
1579 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1580 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1581 " occurrences values smaller than %d.\n",
1582 file, linenum, MAX_HDR_HISTORY);
1583 err_code |= ERR_ALERT | ERR_FATAL;
1584 goto out;
1585 }
1586 } else {
1587 struct sockaddr_storage *sk;
1588 int port1, port2;
1589
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001590 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001591 if (!sk) {
1592 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1593 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1594 err_code |= ERR_ALERT | ERR_FATAL;
1595 goto out;
1596 }
1597
1598 proto = protocol_by_family(sk->ss_family);
1599 if (!proto || !proto->connect) {
1600 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1601 file, linenum, args[cur_arg], args[cur_arg+1]);
1602 err_code |= ERR_ALERT | ERR_FATAL;
1603 goto out;
1604 }
1605
1606 if (port1 != port2) {
1607 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1608 file, linenum, args[cur_arg], args[cur_arg + 1]);
1609 err_code |= ERR_ALERT | ERR_FATAL;
1610 goto out;
1611 }
1612 newsrv->conn_src.tproxy_addr = *sk;
1613 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1614 }
1615 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001616 cur_arg += 2;
1617 continue;
1618#else /* no TPROXY support */
1619 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1620 file, linenum, "usesrc");
1621 err_code |= ERR_ALERT | ERR_FATAL;
1622 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001623#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001624 } /* "usesrc" */
1625
1626 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1627#ifdef SO_BINDTODEVICE
1628 if (!*args[cur_arg + 1]) {
1629 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1630 file, linenum, args[0]);
1631 err_code |= ERR_ALERT | ERR_FATAL;
1632 goto out;
1633 }
1634 free(newsrv->conn_src.iface_name);
1635 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1636 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1637 global.last_checks |= LSTCHK_NETADM;
1638#else
1639 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1640 file, linenum, args[0], args[cur_arg]);
1641 err_code |= ERR_ALERT | ERR_FATAL;
1642 goto out;
1643#endif
1644 cur_arg += 2;
1645 continue;
1646 }
1647 /* this keyword in not an option of "source" */
1648 break;
1649 } /* while */
1650 }
1651 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1652 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1653 file, linenum, "usesrc", "source");
1654 err_code |= ERR_ALERT | ERR_FATAL;
1655 goto out;
1656 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001657 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1658#ifdef CONFIG_HAP_NS
1659 char *arg = args[cur_arg + 1];
1660 if (!strcmp(arg, "*")) {
1661 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1662 } else {
1663 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1664
1665 if (newsrv->netns == NULL)
1666 newsrv->netns = netns_store_insert(arg);
1667
1668 if (newsrv->netns == NULL) {
1669 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1670 err_code |= ERR_ALERT | ERR_FATAL;
1671 goto out;
1672 }
1673 }
1674#else
1675 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1676 file, linenum, args[0], args[cur_arg]);
1677 err_code |= ERR_ALERT | ERR_FATAL;
1678 goto out;
1679#endif
1680 cur_arg += 2;
1681 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001682 else {
1683 static int srv_dumped;
1684 struct srv_kw *kw;
1685 char *err;
1686
1687 kw = srv_find_kw(args[cur_arg]);
1688 if (kw) {
1689 char *err = NULL;
1690 int code;
1691
1692 if (!kw->parse) {
1693 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1694 file, linenum, args[0], args[1], args[cur_arg]);
1695 cur_arg += 1 + kw->skip ;
1696 err_code |= ERR_ALERT | ERR_FATAL;
1697 goto out;
1698 }
1699
1700 if (defsrv && !kw->default_ok) {
1701 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1702 file, linenum, args[0], args[1], args[cur_arg]);
1703 cur_arg += 1 + kw->skip ;
1704 err_code |= ERR_ALERT;
1705 continue;
1706 }
1707
1708 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1709 err_code |= code;
1710
1711 if (code) {
1712 if (err && *err) {
1713 indent_msg(&err, 2);
1714 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1715 }
1716 else
1717 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1718 file, linenum, args[0], args[1], args[cur_arg]);
1719 if (code & ERR_FATAL) {
1720 free(err);
1721 cur_arg += 1 + kw->skip;
1722 goto out;
1723 }
1724 }
1725 free(err);
1726 cur_arg += 1 + kw->skip;
1727 continue;
1728 }
1729
1730 err = NULL;
1731 if (!srv_dumped) {
1732 srv_dump_kws(&err);
1733 indent_msg(&err, 4);
1734 srv_dumped = 1;
1735 }
1736
1737 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1738 file, linenum, args[0], args[1], args[cur_arg],
1739 err ? " Registered keywords :" : "", err ? err : "");
1740 free(err);
1741
1742 err_code |= ERR_ALERT | ERR_FATAL;
1743 goto out;
1744 }
1745 }
1746
Willy Tarreau272adea2014-03-31 10:39:59 +02001747 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001748 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001749
1750 if (newsrv->trackit) {
1751 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1752 file, linenum);
1753 err_code |= ERR_ALERT | ERR_FATAL;
1754 goto out;
1755 }
1756
Willy Tarreau272adea2014-03-31 10:39:59 +02001757 /*
1758 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001759 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001760 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001761 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001762 (is_inet_addr(&newsrv->check.addr) ||
1763 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001764 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001765 struct list *l;
1766
1767 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1768 if (!r) {
1769 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1770 file, linenum, newsrv->id);
1771 err_code |= ERR_ALERT | ERR_FATAL;
1772 goto out;
1773 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001774 /* search the first action (connect / send / expect) in the list */
1775 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001776 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001777 if (r->action != TCPCHK_ACT_COMMENT)
1778 break;
1779 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001780 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1781 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",
1782 file, linenum, newsrv->id);
1783 err_code |= ERR_ALERT | ERR_FATAL;
1784 goto out;
1785 }
1786 else {
1787 /* scan the tcp-check ruleset to ensure a port has been configured */
1788 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001789 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001790 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1791 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",
1792 file, linenum, newsrv->id);
1793 err_code |= ERR_ALERT | ERR_FATAL;
1794 goto out;
1795 }
1796 }
1797 }
1798 }
1799
1800 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001801 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001802 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001803 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1804 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001805 goto out;
1806 }
1807
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001808 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001809 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001810
Willy Tarreau272adea2014-03-31 10:39:59 +02001811 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1812 }
1813
1814 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001815 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001816
1817 if (!newsrv->agent.port) {
1818 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1819 file, linenum, newsrv->id);
1820 err_code |= ERR_ALERT | ERR_FATAL;
1821 goto out;
1822 }
1823
1824 if (!newsrv->agent.inter)
1825 newsrv->agent.inter = newsrv->check.inter;
1826
Simon Hormanb1900d52015-01-30 11:22:54 +09001827 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001828 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001829 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1830 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001831 goto out;
1832 }
1833
1834 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1835 }
1836
1837 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001838 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001839 curproxy->srv_bck++;
1840 else
1841 curproxy->srv_act++;
1842
Willy Tarreauc5150da2014-05-13 19:27:31 +02001843 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001844 }
1845 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001846 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001847 return 0;
1848
1849 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001850 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001851 free(errmsg);
1852 return err_code;
1853}
1854
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001855/* Returns a pointer to the first server matching either id <id>.
1856 * NULL is returned if no match is found.
1857 * the lookup is performed in the backend <bk>
1858 */
1859struct server *server_find_by_id(struct proxy *bk, int id)
1860{
1861 struct eb32_node *eb32;
1862 struct server *curserver;
1863
1864 if (!bk || (id ==0))
1865 return NULL;
1866
1867 /* <bk> has no backend capabilities, so it can't have a server */
1868 if (!(bk->cap & PR_CAP_BE))
1869 return NULL;
1870
1871 curserver = NULL;
1872
1873 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1874 if (eb32)
1875 curserver = container_of(eb32, struct server, conf.id);
1876
1877 return curserver;
1878}
1879
1880/* Returns a pointer to the first server matching either name <name>, or id
1881 * if <name> starts with a '#'. NULL is returned if no match is found.
1882 * the lookup is performed in the backend <bk>
1883 */
1884struct server *server_find_by_name(struct proxy *bk, const char *name)
1885{
1886 struct server *curserver;
1887
1888 if (!bk || !name)
1889 return NULL;
1890
1891 /* <bk> has no backend capabilities, so it can't have a server */
1892 if (!(bk->cap & PR_CAP_BE))
1893 return NULL;
1894
1895 curserver = NULL;
1896 if (*name == '#') {
1897 curserver = server_find_by_id(bk, atoi(name + 1));
1898 if (curserver)
1899 return curserver;
1900 }
1901 else {
1902 curserver = bk->srv;
1903
1904 while (curserver && (strcmp(curserver->id, name) != 0))
1905 curserver = curserver->next;
1906
1907 if (curserver)
1908 return curserver;
1909 }
1910
1911 return NULL;
1912}
1913
1914struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
1915{
1916 struct server *byname;
1917 struct server *byid;
1918
1919 if (!name && !id)
1920 return NULL;
1921
1922 if (diff)
1923 *diff = 0;
1924
1925 byname = byid = NULL;
1926
1927 if (name) {
1928 byname = server_find_by_name(bk, name);
1929 if (byname && (!id || byname->puid == id))
1930 return byname;
1931 }
1932
1933 /* remaining possibilities :
1934 * - name not set
1935 * - name set but not found
1936 * - name found but ID doesn't match
1937 */
1938 if (id) {
1939 byid = server_find_by_id(bk, id);
1940 if (byid) {
1941 if (byname) {
1942 /* use id only if forced by configuration */
1943 if (byid->flags & SRV_F_FORCED_ID) {
1944 if (diff)
1945 *diff |= 2;
1946 return byid;
1947 }
1948 else {
1949 if (diff)
1950 *diff |= 1;
1951 return byname;
1952 }
1953 }
1954
1955 /* remaining possibilities:
1956 * - name not set
1957 * - name set but not found
1958 */
1959 if (name && diff)
1960 *diff |= 2;
1961 return byid;
1962 }
1963
1964 /* id bot found */
1965 if (byname) {
1966 if (diff)
1967 *diff |= 1;
1968 return byname;
1969 }
1970 }
1971
1972 return NULL;
1973}
1974
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001975/* Update a server state using the parameters available in the params list */
1976static void srv_update_state(struct server *srv, int version, char **params)
1977{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001978 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02001979 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001980
1981 /* fields since version 1
1982 * and common to all other upcoming versions
1983 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001984 enum srv_state srv_op_state;
1985 enum srv_admin srv_admin_state;
1986 unsigned srv_uweight, srv_iweight;
1987 unsigned long srv_last_time_change;
1988 short srv_check_status;
1989 enum chk_result srv_check_result;
1990 int srv_check_health;
1991 int srv_check_state, srv_agent_state;
1992 int bk_f_forced_id;
1993 int srv_f_forced_id;
1994
Willy Tarreau31138fa2015-09-29 18:38:47 +02001995 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001996 switch (version) {
1997 case 1:
1998 /*
1999 * now we can proceed with server's state update:
2000 * srv_addr: params[0]
2001 * srv_op_state: params[1]
2002 * srv_admin_state: params[2]
2003 * srv_uweight: params[3]
2004 * srv_iweight: params[4]
2005 * srv_last_time_change: params[5]
2006 * srv_check_status: params[6]
2007 * srv_check_result: params[7]
2008 * srv_check_health: params[8]
2009 * srv_check_state: params[9]
2010 * srv_agent_state: params[10]
2011 * bk_f_forced_id: params[11]
2012 * srv_f_forced_id: params[12]
2013 */
2014
2015 /* validating srv_op_state */
2016 p = NULL;
2017 errno = 0;
2018 srv_op_state = strtol(params[1], &p, 10);
2019 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2020 (srv_op_state != SRV_ST_STOPPED &&
2021 srv_op_state != SRV_ST_STARTING &&
2022 srv_op_state != SRV_ST_RUNNING &&
2023 srv_op_state != SRV_ST_STOPPING)) {
2024 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2025 }
2026
2027 /* validating srv_admin_state */
2028 p = NULL;
2029 errno = 0;
2030 srv_admin_state = strtol(params[2], &p, 10);
2031 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2032 (srv_admin_state != 0 &&
2033 srv_admin_state != SRV_ADMF_FMAINT &&
2034 srv_admin_state != SRV_ADMF_IMAINT &&
2035 srv_admin_state != SRV_ADMF_CMAINT &&
2036 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002037 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002038 srv_admin_state != SRV_ADMF_FDRAIN &&
2039 srv_admin_state != SRV_ADMF_IDRAIN)) {
2040 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2041 }
2042
2043 /* validating srv_uweight */
2044 p = NULL;
2045 errno = 0;
2046 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002047 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002048 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2049
2050 /* validating srv_iweight */
2051 p = NULL;
2052 errno = 0;
2053 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002054 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002055 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2056
2057 /* validating srv_last_time_change */
2058 p = NULL;
2059 errno = 0;
2060 srv_last_time_change = strtol(params[5], &p, 10);
2061 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2062 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2063
2064 /* validating srv_check_status */
2065 p = NULL;
2066 errno = 0;
2067 srv_check_status = strtol(params[6], &p, 10);
2068 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2069 (srv_check_status >= HCHK_STATUS_SIZE))
2070 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2071
2072 /* validating srv_check_result */
2073 p = NULL;
2074 errno = 0;
2075 srv_check_result = strtol(params[7], &p, 10);
2076 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2077 (srv_check_result != CHK_RES_UNKNOWN &&
2078 srv_check_result != CHK_RES_NEUTRAL &&
2079 srv_check_result != CHK_RES_FAILED &&
2080 srv_check_result != CHK_RES_PASSED &&
2081 srv_check_result != CHK_RES_CONDPASS)) {
2082 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2083 }
2084
2085 /* validating srv_check_health */
2086 p = NULL;
2087 errno = 0;
2088 srv_check_health = strtol(params[8], &p, 10);
2089 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2090 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2091
2092 /* validating srv_check_state */
2093 p = NULL;
2094 errno = 0;
2095 srv_check_state = strtol(params[9], &p, 10);
2096 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2097 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2098 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2099
2100 /* validating srv_agent_state */
2101 p = NULL;
2102 errno = 0;
2103 srv_agent_state = strtol(params[10], &p, 10);
2104 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2105 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2106 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2107
2108 /* validating bk_f_forced_id */
2109 p = NULL;
2110 errno = 0;
2111 bk_f_forced_id = strtol(params[11], &p, 10);
2112 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2113 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2114
2115 /* validating srv_f_forced_id */
2116 p = NULL;
2117 errno = 0;
2118 srv_f_forced_id = strtol(params[12], &p, 10);
2119 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2120 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2121
2122
2123 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002124 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002125 goto out;
2126
2127 /* recover operational state and apply it to this server
2128 * and all servers tracking this one */
2129 switch (srv_op_state) {
2130 case SRV_ST_STOPPED:
2131 srv->check.health = 0;
2132 srv_set_stopped(srv, "changed from server-state after a reload");
2133 break;
2134 case SRV_ST_STARTING:
2135 srv->state = srv_op_state;
2136 break;
2137 case SRV_ST_STOPPING:
2138 srv->check.health = srv->check.rise + srv->check.fall - 1;
2139 srv_set_stopping(srv, "changed from server-state after a reload");
2140 break;
2141 case SRV_ST_RUNNING:
2142 srv->check.health = srv->check.rise + srv->check.fall - 1;
2143 srv_set_running(srv, "");
2144 break;
2145 }
2146
2147 /* When applying server state, the following rules apply:
2148 * - in case of a configuration change, we apply the setting from the new
2149 * configuration, regardless of old running state
2150 * - if no configuration change, we apply old running state only if old running
2151 * state is different from new configuration state
2152 */
2153 /* configuration has changed */
2154 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2155 if (srv->admin & SRV_ADMF_CMAINT)
2156 srv_adm_set_maint(srv);
2157 else
2158 srv_adm_set_ready(srv);
2159 }
2160 /* configuration is the same, let's compate old running state and new conf state */
2161 else {
2162 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2163 srv_adm_set_maint(srv);
2164 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2165 srv_adm_set_ready(srv);
2166 }
2167 /* apply drain mode if server is currently enabled */
2168 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2169 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002170 * (srv->iweight is the weight set up in configuration).
2171 * There are two possible reasons for FDRAIN to have been present :
2172 * - previous config weight was zero
2173 * - "set server b/s drain" was sent to the CLI
2174 *
2175 * In the first case, we simply want to drop this drain state
2176 * if the new weight is not zero anymore, meaning the administrator
2177 * has intentionally turned the weight back to a positive value to
2178 * enable the server again after an operation. In the second case,
2179 * the drain state was forced on the CLI regardless of the config's
2180 * weight so we don't want a change to the config weight to lose this
2181 * status. What this means is :
2182 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2183 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002184 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002185 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002186 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002187 }
2188
2189 srv->last_change = date.tv_sec - srv_last_time_change;
2190 srv->check.status = srv_check_status;
2191 srv->check.result = srv_check_result;
2192 srv->check.health = srv_check_health;
2193
2194 /* Only case we want to apply is removing ENABLED flag which could have been
2195 * done by the "disable health" command over the stats socket
2196 */
2197 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2198 (srv_check_state & CHK_ST_CONFIGURED) &&
2199 !(srv_check_state & CHK_ST_ENABLED))
2200 srv->check.state &= ~CHK_ST_ENABLED;
2201
2202 /* Only case we want to apply is removing ENABLED flag which could have been
2203 * done by the "disable agent" command over the stats socket
2204 */
2205 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2206 (srv_agent_state & CHK_ST_CONFIGURED) &&
2207 !(srv_agent_state & CHK_ST_ENABLED))
2208 srv->agent.state &= ~CHK_ST_ENABLED;
2209
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002210 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2211 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002212 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002213 * It means that a configuration file change has precedence over a unix socket change
2214 * for server's weight
2215 *
2216 * by default, HAProxy applies the following weight when parsing the configuration
2217 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002218 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002219 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002220 srv->uweight = srv_uweight;
2221 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002222 server_recalc_eweight(srv);
2223
2224 /* update server IP only if DNS resolution is used on the server */
2225 if (srv->resolution) {
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002226 struct sockaddr_storage addr;
2227
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002228 memset(&addr, 0, sizeof(struct sockaddr_storage));
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002229
2230 if (str2ip2(params[0], &addr, AF_UNSPEC)) {
2231 int port;
2232
2233 /* save the port, applies the new IP then reconfigure the port */
Willy Tarreauf3c7a832016-01-21 13:51:56 +01002234 port = get_host_port(&srv->addr);
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002235 srv->addr.ss_family = addr.ss_family;
2236 str2ip2(params[0], &srv->addr, srv->addr.ss_family);
2237 set_host_port(&srv->addr, port);
2238 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002239 else
2240 chunk_appendf(msg, ", can't parse IP: %s", params[0]);
2241 }
2242 break;
2243 default:
2244 chunk_appendf(msg, ", version '%d' not supported", version);
2245 }
2246
2247 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002248 if (msg->len) {
2249 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002250 Warning("server-state application failed for server '%s/%s'%s",
2251 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002252 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002253}
2254
2255/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2256 * For each proxy, it does the following:
2257 * - opens its server state file (either one or local one)
2258 * - read whole file, line by line
2259 * - analyse each line to check if it matches our current backend:
2260 * - backend name matches
2261 * - backend id matches if id is forced and name doesn't match
2262 * - if the server pointed by the line is found, then state is applied
2263 *
2264 * If the running backend uuid or id differs from the state file, then HAProxy reports
2265 * a warning.
2266 */
2267void apply_server_state(void)
2268{
2269 char *cur, *end;
2270 char mybuf[SRV_STATE_LINE_MAXLEN];
2271 int mybuflen;
2272 char *params[SRV_STATE_FILE_MAX_FIELDS];
2273 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2274 int arg, srv_arg, version, diff;
2275 FILE *f;
2276 char *filepath;
2277 char globalfilepath[MAXPATHLEN + 1];
2278 char localfilepath[MAXPATHLEN + 1];
2279 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2280 extern struct proxy *proxy;
2281 struct proxy *curproxy, *bk;
2282 struct server *srv;
2283
2284 globalfilepathlen = 0;
2285 /* create the globalfilepath variable */
2286 if (global.server_state_file) {
2287 /* absolute path or no base directory provided */
2288 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2289 len = strlen(global.server_state_file);
2290 if (len > MAXPATHLEN) {
2291 globalfilepathlen = 0;
2292 goto globalfileerror;
2293 }
2294 memcpy(globalfilepath, global.server_state_file, len);
2295 globalfilepath[len] = '\0';
2296 globalfilepathlen = len;
2297 }
2298 else if (global.server_state_base) {
2299 len = strlen(global.server_state_base);
2300 globalfilepathlen += len;
2301
2302 if (globalfilepathlen > MAXPATHLEN) {
2303 globalfilepathlen = 0;
2304 goto globalfileerror;
2305 }
2306 strncpy(globalfilepath, global.server_state_base, len);
2307 globalfilepath[globalfilepathlen] = 0;
2308
2309 /* append a slash if needed */
2310 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2311 if (globalfilepathlen + 1 > MAXPATHLEN) {
2312 globalfilepathlen = 0;
2313 goto globalfileerror;
2314 }
2315 globalfilepath[globalfilepathlen++] = '/';
2316 }
2317
2318 len = strlen(global.server_state_file);
2319 if (globalfilepathlen + len > MAXPATHLEN) {
2320 globalfilepathlen = 0;
2321 goto globalfileerror;
2322 }
2323 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2324 globalfilepathlen += len;
2325 globalfilepath[globalfilepathlen++] = 0;
2326 }
2327 }
2328 globalfileerror:
2329 if (globalfilepathlen == 0)
2330 globalfilepath[0] = '\0';
2331
2332 /* read servers state from local file */
2333 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2334 /* servers are only in backends */
2335 if (!(curproxy->cap & PR_CAP_BE))
2336 continue;
2337 fileopenerr = 0;
2338 filepath = NULL;
2339
2340 /* search server state file path and name */
2341 switch (curproxy->load_server_state_from_file) {
2342 /* read servers state from global file */
2343 case PR_SRV_STATE_FILE_GLOBAL:
2344 /* there was an error while generating global server state file path */
2345 if (globalfilepathlen == 0)
2346 continue;
2347 filepath = globalfilepath;
2348 fileopenerr = 1;
2349 break;
2350 /* this backend has its own file */
2351 case PR_SRV_STATE_FILE_LOCAL:
2352 localfilepathlen = 0;
2353 localfilepath[0] = '\0';
2354 len = 0;
2355 /* create the localfilepath variable */
2356 /* absolute path or no base directory provided */
2357 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2358 len = strlen(curproxy->server_state_file_name);
2359 if (len > MAXPATHLEN) {
2360 localfilepathlen = 0;
2361 goto localfileerror;
2362 }
2363 memcpy(localfilepath, curproxy->server_state_file_name, len);
2364 localfilepath[len] = '\0';
2365 localfilepathlen = len;
2366 }
2367 else if (global.server_state_base) {
2368 len = strlen(global.server_state_base);
2369 localfilepathlen += len;
2370
2371 if (localfilepathlen > MAXPATHLEN) {
2372 localfilepathlen = 0;
2373 goto localfileerror;
2374 }
2375 strncpy(localfilepath, global.server_state_base, len);
2376 localfilepath[localfilepathlen] = 0;
2377
2378 /* append a slash if needed */
2379 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2380 if (localfilepathlen + 1 > MAXPATHLEN) {
2381 localfilepathlen = 0;
2382 goto localfileerror;
2383 }
2384 localfilepath[localfilepathlen++] = '/';
2385 }
2386
2387 len = strlen(curproxy->server_state_file_name);
2388 if (localfilepathlen + len > MAXPATHLEN) {
2389 localfilepathlen = 0;
2390 goto localfileerror;
2391 }
2392 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2393 localfilepathlen += len;
2394 localfilepath[localfilepathlen++] = 0;
2395 }
2396 filepath = localfilepath;
2397 localfileerror:
2398 if (localfilepathlen == 0)
2399 localfilepath[0] = '\0';
2400
2401 break;
2402 case PR_SRV_STATE_FILE_NONE:
2403 default:
2404 continue;
2405 }
2406
2407 /* preload global state file */
2408 errno = 0;
2409 f = fopen(filepath, "r");
2410 if (errno && fileopenerr)
2411 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2412 if (!f)
2413 continue;
2414
2415 mybuf[0] = '\0';
2416 mybuflen = 0;
2417 version = 0;
2418
2419 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002420 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2421 Warning("Can't read first line of the server state file '%s'\n", filepath);
2422 goto fileclose;
2423 }
2424
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002425 cur = mybuf;
2426 version = atoi(cur);
2427 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2428 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002429 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002430
2431 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2432 int bk_f_forced_id = 0;
2433 int check_id = 0;
2434 int check_name = 0;
2435
2436 mybuflen = strlen(mybuf);
2437 cur = mybuf;
2438 end = cur + mybuflen;
2439
2440 bk = NULL;
2441 srv = NULL;
2442
2443 /* we need at least one character */
2444 if (mybuflen == 0)
2445 continue;
2446
2447 /* ignore blank characters at the beginning of the line */
2448 while (isspace(*cur))
2449 ++cur;
2450
2451 if (cur == end)
2452 continue;
2453
2454 /* ignore comment line */
2455 if (*cur == '#')
2456 continue;
2457
2458 /* we're now ready to move the line into *srv_params[] */
2459 params[0] = cur;
2460 arg = 1;
2461 srv_arg = 0;
2462 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2463 if (isspace(*cur)) {
2464 *cur = '\0';
2465 ++cur;
2466 while (isspace(*cur))
2467 ++cur;
2468 switch (version) {
2469 case 1:
2470 /*
2471 * srv_addr: params[4] => srv_params[0]
2472 * srv_op_state: params[5] => srv_params[1]
2473 * srv_admin_state: params[6] => srv_params[2]
2474 * srv_uweight: params[7] => srv_params[3]
2475 * srv_iweight: params[8] => srv_params[4]
2476 * srv_last_time_change: params[9] => srv_params[5]
2477 * srv_check_status: params[10] => srv_params[6]
2478 * srv_check_result: params[11] => srv_params[7]
2479 * srv_check_health: params[12] => srv_params[8]
2480 * srv_check_state: params[13] => srv_params[9]
2481 * srv_agent_state: params[14] => srv_params[10]
2482 * bk_f_forced_id: params[15] => srv_params[11]
2483 * srv_f_forced_id: params[16] => srv_params[12]
2484 */
2485 if (arg >= 4) {
2486 srv_params[srv_arg] = cur;
2487 ++srv_arg;
2488 }
2489 break;
2490 }
2491
2492 params[arg] = cur;
2493 ++arg;
2494 }
2495 else {
2496 ++cur;
2497 }
2498 }
2499
2500 /* if line is incomplete line, then ignore it.
2501 * otherwise, update useful flags */
2502 switch (version) {
2503 case 1:
2504 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2505 continue;
2506 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2507 check_id = (atoi(params[0]) == curproxy->uuid);
2508 check_name = (strcmp(curproxy->id, params[1]) == 0);
2509 break;
2510 }
2511
2512 diff = 0;
2513 bk = curproxy;
2514
2515 /* if backend can't be found, let's continue */
2516 if (!check_id && !check_name)
2517 continue;
2518 else if (!check_id && check_name) {
2519 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2520 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2521 }
2522 else if (check_id && !check_name) {
2523 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2524 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2525 /* if name doesn't match, we still want to update curproxy if the backend id
2526 * was forced in previous the previous configuration */
2527 if (!bk_f_forced_id)
2528 continue;
2529 }
2530
2531 /* look for the server by its id: param[2] */
2532 /* else look for the server by its name: param[3] */
2533 diff = 0;
2534 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2535
2536 if (!srv) {
2537 /* if no server found, then warning and continue with next line */
2538 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2539 params[3], params[2], params[0], params[1]);
2540 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2541 params[3], params[2], params[0], params[1]);
2542 continue;
2543 }
2544 else if (diff & PR_FBM_MISMATCH_ID) {
2545 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);
2546 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);
2547 }
2548 else if (diff & PR_FBM_MISMATCH_NAME) {
2549 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);
2550 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);
2551 }
2552
2553 /* now we can proceed with server's state update */
2554 srv_update_state(srv, version, srv_params);
2555 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002556fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002557 fclose(f);
2558 }
2559}
2560
Simon Horman7d09b9a2013-02-12 10:45:51 +09002561/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002562 * update a server's current IP address.
2563 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2564 * ip is in network format.
2565 * updater is a string which contains an information about the requester of the update.
2566 * updater is used if not NULL.
2567 *
2568 * A log line and a stderr warning message is generated based on server's backend options.
2569 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002570int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002571{
2572 /* generates a log line and a warning on stderr */
2573 if (1) {
2574 /* book enough space for both IPv4 and IPv6 */
2575 char oldip[INET6_ADDRSTRLEN];
2576 char newip[INET6_ADDRSTRLEN];
2577
2578 memset(oldip, '\0', INET6_ADDRSTRLEN);
2579 memset(newip, '\0', INET6_ADDRSTRLEN);
2580
2581 /* copy old IP address in a string */
2582 switch (s->addr.ss_family) {
2583 case AF_INET:
2584 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2585 break;
2586 case AF_INET6:
2587 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2588 break;
2589 };
2590
2591 /* copy new IP address in a string */
2592 switch (ip_sin_family) {
2593 case AF_INET:
2594 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2595 break;
2596 case AF_INET6:
2597 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2598 break;
2599 };
2600
2601 /* save log line into a buffer */
2602 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2603 s->proxy->id, s->id, oldip, newip, updater);
2604
2605 /* write the buffer on stderr */
2606 Warning("%s.\n", trash.str);
2607
2608 /* send a log */
2609 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2610 }
2611
2612 /* save the new IP family */
2613 s->addr.ss_family = ip_sin_family;
2614 /* save the new IP address */
2615 switch (ip_sin_family) {
2616 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002617 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002618 break;
2619 case AF_INET6:
2620 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2621 break;
2622 };
2623
2624 return 0;
2625}
2626
2627/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002628 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2629 *
2630 * Caller can pass its name through <updater> to get it integrated in the response message
2631 * returned by the function.
2632 *
2633 * The function first does the following, in that order:
2634 * - validates the new addr and/or port
2635 * - checks if an update is required (new IP or port is different than current ones)
2636 * - checks the update is allowed:
2637 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2638 * - allow all changes if no CHECKS are configured
2639 * - if CHECK is configured:
2640 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2641 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2642 * conditions are met
2643 */
2644const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2645{
2646 struct sockaddr_storage sa;
2647 int ret, port_change_required;
2648 char current_addr[INET6_ADDRSTRLEN];
2649 u_int16_t current_port, new_port;
2650 struct chunk *msg;
2651
2652 msg = get_trash_chunk();
2653 chunk_reset(msg);
2654
2655 if (addr) {
2656 memset(&sa, 0, sizeof(struct sockaddr_storage));
2657 if (str2ip2(addr, &sa, 0) == NULL) {
2658 chunk_printf(msg, "Invalid addr '%s'", addr);
2659 goto out;
2660 }
2661
2662 /* changes are allowed on AF_INET* families only */
2663 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2664 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2665 goto out;
2666 }
2667
2668 /* collecting data currently setup */
2669 memset(current_addr, '\0', sizeof(current_addr));
2670 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2671 /* changes are allowed on AF_INET* families only */
2672 if ((ret != AF_INET) && (ret != AF_INET6)) {
2673 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2674 goto out;
2675 }
2676
2677 /* applying ADDR changes if required and allowed
2678 * ipcmp returns 0 when both ADDR are the same
2679 */
2680 if (ipcmp(&s->addr, &sa) == 0) {
2681 chunk_appendf(msg, "no need to change the addr");
2682 goto port;
2683 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002684 ipcpy(&sa, &s->addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002685
2686 /* we also need to update check's ADDR only if it uses the server's one */
2687 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002688 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002689 }
2690
2691 /* we also need to update agent ADDR only if it use the server's one */
2692 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002693 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002694 }
2695
2696 /* update report for caller */
2697 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2698 }
2699
2700 port:
2701 if (port) {
2702 char sign = '\0';
2703 char *endptr;
2704
2705 if (addr)
2706 chunk_appendf(msg, ", ");
2707
2708 /* collecting data currently setup */
2709 current_port = get_host_port(&s->addr);
2710
2711 /* check if PORT change is required */
2712 port_change_required = 0;
2713
2714 sign = *port;
2715 new_port = strtol(port, &endptr, 10);
2716 if ((errno != 0) || (port == endptr)) {
2717 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2718 goto out;
2719 }
2720
2721 /* check if caller triggers a port mapped or offset */
2722 if (sign == '-' || (sign == '+')) {
2723 /* check if server currently uses port map */
2724 if (!(s->flags & SRV_F_MAPPORTS)) {
2725 /* switch from fixed port to port map mandatorily triggers
2726 * a port change */
2727 port_change_required = 1;
2728 /* check is configured
2729 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2730 * prevent PORT change if check doesn't have it's dedicated port while switching
2731 * to port mapping */
2732 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2733 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.");
2734 goto out;
2735 }
2736 }
2737 /* we're already using port maps */
2738 else {
2739 port_change_required = current_port != new_port;
2740 }
2741 }
2742 /* fixed port */
2743 else {
2744 port_change_required = current_port != new_port;
2745 }
2746
2747 /* applying PORT changes if required and update response message */
2748 if (port_change_required) {
2749 /* apply new port */
2750 set_host_port(&s->addr, new_port);
2751
2752 /* prepare message */
2753 chunk_appendf(msg, "port changed from '");
2754 if (s->flags & SRV_F_MAPPORTS)
2755 chunk_appendf(msg, "+");
2756 chunk_appendf(msg, "%d' to '", current_port);
2757
2758 if (sign == '-') {
2759 s->flags |= SRV_F_MAPPORTS;
2760 chunk_appendf(msg, "%c", sign);
2761 /* just use for result output */
2762 new_port = -new_port;
2763 }
2764 else if (sign == '+') {
2765 s->flags |= SRV_F_MAPPORTS;
2766 chunk_appendf(msg, "%c", sign);
2767 }
2768 else {
2769 s->flags &= ~SRV_F_MAPPORTS;
2770 }
2771
2772 chunk_appendf(msg, "%d'", new_port);
2773
2774 /* we also need to update health checks port only if it uses server's realport */
2775 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2776 s->check.port = new_port;
2777 }
2778 }
2779 else {
2780 chunk_appendf(msg, "no need to change the port");
2781 }
2782 }
2783
2784out:
2785 if (updater)
2786 chunk_appendf(msg, " by '%s'", updater);
2787 chunk_appendf(msg, "\n");
2788 return msg->str;
2789}
2790
2791
2792/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002793 * update server status based on result of name resolution
2794 * returns:
2795 * 0 if server status is updated
2796 * 1 if server status has not changed
2797 */
2798int snr_update_srv_status(struct server *s)
2799{
2800 struct dns_resolution *resolution = s->resolution;
2801
2802 switch (resolution->status) {
2803 case RSLV_STATUS_NONE:
2804 /* status when HAProxy has just (re)started */
2805 trigger_resolution(s);
2806 break;
2807
2808 default:
2809 break;
2810 }
2811
2812 return 1;
2813}
2814
2815/*
2816 * Server Name Resolution valid response callback
2817 * It expects:
2818 * - <nameserver>: the name server which answered the valid response
2819 * - <response>: buffer containing a valid DNS response
2820 * - <response_len>: size of <response>
2821 * It performs the following actions:
2822 * - ignore response if current ip found and server family not met
2823 * - update with first new ip found if family is met and current IP is not found
2824 * returns:
2825 * 0 on error
2826 * 1 when no error or safe ignore
2827 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002828int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002829{
2830 struct server *s;
2831 void *serverip, *firstip;
2832 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002833 int ret;
2834 struct chunk *chk = get_trash_chunk();
2835
2836 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002837 firstip = NULL; /* pointer to the first valid response found */
2838 /* it will be used as the new IP if a change is required */
2839 firstip_sin_family = AF_UNSPEC;
2840 serverip = NULL; /* current server IP address */
2841
2842 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002843 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002844
2845 /* initializing server IP pointer */
2846 server_sin_family = s->addr.ss_family;
2847 switch (server_sin_family) {
2848 case AF_INET:
2849 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
2850 break;
2851
2852 case AF_INET6:
2853 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
2854 break;
2855
2856 default:
2857 goto invalid;
2858 }
2859
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002860 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01002861 serverip, server_sin_family, &firstip,
2862 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002863
2864 switch (ret) {
2865 case DNS_UPD_NO:
2866 if (resolution->status != RSLV_STATUS_VALID) {
2867 resolution->status = RSLV_STATUS_VALID;
2868 resolution->last_status_change = now_ms;
2869 }
2870 goto stop_resolution;
2871
2872 case DNS_UPD_SRVIP_NOT_FOUND:
2873 goto save_ip;
2874
2875 case DNS_UPD_CNAME:
2876 if (resolution->status != RSLV_STATUS_VALID) {
2877 resolution->status = RSLV_STATUS_VALID;
2878 resolution->last_status_change = now_ms;
2879 }
2880 goto invalid;
2881
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02002882 case DNS_UPD_NO_IP_FOUND:
2883 if (resolution->status != RSLV_STATUS_OTHER) {
2884 resolution->status = RSLV_STATUS_OTHER;
2885 resolution->last_status_change = now_ms;
2886 }
2887 goto stop_resolution;
2888
Baptiste Assmannfad03182015-10-28 02:03:32 +01002889 case DNS_UPD_NAME_ERROR:
2890 /* if this is not the last expected response, we ignore it */
2891 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
2892 return 0;
2893 /* update resolution status to OTHER error type */
2894 if (resolution->status != RSLV_STATUS_OTHER) {
2895 resolution->status = RSLV_STATUS_OTHER;
2896 resolution->last_status_change = now_ms;
2897 }
2898 goto stop_resolution;
2899
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002900 default:
2901 goto invalid;
2902
2903 }
2904
2905 save_ip:
2906 nameserver->counters.update += 1;
2907 if (resolution->status != RSLV_STATUS_VALID) {
2908 resolution->status = RSLV_STATUS_VALID;
2909 resolution->last_status_change = now_ms;
2910 }
2911
2912 /* save the first ip we found */
2913 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
2914 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
2915
2916 stop_resolution:
2917 /* update last resolution date and time */
2918 resolution->last_resolution = now_ms;
2919 /* reset current status flag */
2920 resolution->step = RSLV_STEP_NONE;
2921 /* reset values */
2922 dns_reset_resolution(resolution);
2923
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002924 dns_update_resolvers_timeout(nameserver->resolvers);
2925
2926 snr_update_srv_status(s);
2927 return 0;
2928
2929 invalid:
2930 nameserver->counters.invalid += 1;
2931 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
2932 goto stop_resolution;
2933
2934 snr_update_srv_status(s);
2935 return 0;
2936}
2937
2938/*
2939 * Server Name Resolution error management callback
2940 * returns:
2941 * 0 on error
2942 * 1 when no error or safe ignore
2943 */
2944int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
2945{
2946 struct server *s;
2947 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002948 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002949
2950 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002951 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002952 resolvers = resolution->resolvers;
2953
2954 /* can be ignored if this is not the last response */
2955 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
2956 return 1;
2957 }
2958
2959 switch (error_code) {
2960 case DNS_RESP_INVALID:
2961 case DNS_RESP_WRONG_NAME:
2962 if (resolution->status != RSLV_STATUS_INVALID) {
2963 resolution->status = RSLV_STATUS_INVALID;
2964 resolution->last_status_change = now_ms;
2965 }
2966 break;
2967
2968 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02002969 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002970 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02002971 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02002972 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01002973 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
2974 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02002975
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002976 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002977 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002978 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002979 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02002980 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002981 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02002982 }
2983 else if (res_preferred_afinet) {
2984 /* fallback from A to AAAA */
2985 resolution->query_type = DNS_RTYPE_AAAA;
2986 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002987 else {
2988 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002989 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002990 resolution->query_type = DNS_RTYPE_A;
2991 } else {
2992 resolution->query_type = DNS_RTYPE_AAAA;
2993 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002994 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002995
2996 dns_send_query(resolution);
2997
2998 /*
2999 * move the resolution to the last element of the FIFO queue
3000 * and update timeout wakeup based on the new first entry
3001 */
3002 if (dns_check_resolution_queue(resolvers) > 1) {
3003 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003004 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003005 /* ex first resolution goes to the end of the queue */
3006 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3007 }
3008 dns_update_resolvers_timeout(resolvers);
3009 goto leave;
3010 }
3011 else {
3012 if (resolution->status != RSLV_STATUS_OTHER) {
3013 resolution->status = RSLV_STATUS_OTHER;
3014 resolution->last_status_change = now_ms;
3015 }
3016 }
3017 break;
3018
3019 case DNS_RESP_NX_DOMAIN:
3020 if (resolution->status != RSLV_STATUS_NX) {
3021 resolution->status = RSLV_STATUS_NX;
3022 resolution->last_status_change = now_ms;
3023 }
3024 break;
3025
3026 case DNS_RESP_REFUSED:
3027 if (resolution->status != RSLV_STATUS_REFUSED) {
3028 resolution->status = RSLV_STATUS_REFUSED;
3029 resolution->last_status_change = now_ms;
3030 }
3031 break;
3032
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003033 case DNS_RESP_TIMEOUT:
3034 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3035 resolution->status = RSLV_STATUS_TIMEOUT;
3036 resolution->last_status_change = now_ms;
3037 }
3038 break;
3039 }
3040
3041 /* update last resolution date and time */
3042 resolution->last_resolution = now_ms;
3043 /* reset current status flag */
3044 resolution->step = RSLV_STEP_NONE;
3045 /* reset values */
3046 dns_reset_resolution(resolution);
3047
3048 LIST_DEL(&resolution->list);
3049 dns_update_resolvers_timeout(resolvers);
3050
3051 leave:
3052 snr_update_srv_status(s);
3053 return 1;
3054}
3055
3056/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003057 * Local variables:
3058 * c-indent-level: 8
3059 * c-basic-offset: 8
3060 * End:
3061 */