blob: 11add3c6103d4b9569a57dbfad5c131594772fcd [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
17#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020018#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020019#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010020#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020021#include <common/time.h>
22
Willy Tarreau272adea2014-03-31 10:39:59 +020023#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020024#include <types/dns.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020025
Simon Hormanb1900d52015-01-30 11:22:54 +090026#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <proto/port_range.h>
28#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020029#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020030#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020031#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020032#include <proto/stream.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020033#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020034#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020035
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020036static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010037static int srv_apply_lastaddr(struct server *srv, int *err_code);
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreau21faa912012-10-10 08:27:36 +020039/* List head of all known server keywords */
40static struct srv_kw_list srv_keywords = {
41 .list = LIST_HEAD_INIT(srv_keywords.list)
42};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020043
Simon Hormana3608442013-11-01 16:46:15 +090044int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020045{
Willy Tarreau892337c2014-05-13 23:41:20 +020046 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020047 return s->down_time;
48
49 return now.tv_sec - s->last_change + s->down_time;
50}
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050052int srv_lastsession(const struct server *s)
53{
54 if (s->counters.last_sess)
55 return now.tv_sec - s->counters.last_sess;
56
57 return -1;
58}
59
Simon Horman4a741432013-02-23 15:35:38 +090060int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020061{
Simon Horman4a741432013-02-23 15:35:38 +090062 const struct server *s = check->server;
63
Willy Tarreauff5ae352013-12-11 20:36:34 +010064 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090065 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010066
Willy Tarreau892337c2014-05-13 23:41:20 +020067 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090068 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010069
Simon Horman4a741432013-02-23 15:35:38 +090070 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010071}
72
Willy Tarreau21faa912012-10-10 08:27:36 +020073/*
74 * Registers the server keyword list <kwl> as a list of valid keywords for next
75 * parsing sessions.
76 */
77void srv_register_keywords(struct srv_kw_list *kwl)
78{
79 LIST_ADDQ(&srv_keywords.list, &kwl->list);
80}
81
82/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
83 * keyword is found with a NULL ->parse() function, then an attempt is made to
84 * find one with a valid ->parse() function. This way it is possible to declare
85 * platform-dependant, known keywords as NULL, then only declare them as valid
86 * if some options are met. Note that if the requested keyword contains an
87 * opening parenthesis, everything from this point is ignored.
88 */
89struct srv_kw *srv_find_kw(const char *kw)
90{
91 int index;
92 const char *kwend;
93 struct srv_kw_list *kwl;
94 struct srv_kw *ret = NULL;
95
96 kwend = strchr(kw, '(');
97 if (!kwend)
98 kwend = kw + strlen(kw);
99
100 list_for_each_entry(kwl, &srv_keywords.list, list) {
101 for (index = 0; kwl->kw[index].kw != NULL; index++) {
102 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
103 kwl->kw[index].kw[kwend-kw] == 0) {
104 if (kwl->kw[index].parse)
105 return &kwl->kw[index]; /* found it !*/
106 else
107 ret = &kwl->kw[index]; /* may be OK */
108 }
109 }
110 }
111 return ret;
112}
113
114/* Dumps all registered "server" keywords to the <out> string pointer. The
115 * unsupported keywords are only dumped if their supported form was not
116 * found.
117 */
118void srv_dump_kws(char **out)
119{
120 struct srv_kw_list *kwl;
121 int index;
122
123 *out = NULL;
124 list_for_each_entry(kwl, &srv_keywords.list, list) {
125 for (index = 0; kwl->kw[index].kw != NULL; index++) {
126 if (kwl->kw[index].parse ||
127 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
128 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
129 kwl->scope,
130 kwl->kw[index].kw,
131 kwl->kw[index].skip ? " <arg>" : "",
132 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
133 kwl->kw[index].parse ? "" : " (not supported)");
134 }
135 }
136 }
137}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100138
Willy Tarreaudff55432012-10-10 17:51:05 +0200139/* parse the "id" server keyword */
140static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
141{
142 struct eb32_node *node;
143
144 if (!*args[*cur_arg + 1]) {
145 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
146 return ERR_ALERT | ERR_FATAL;
147 }
148
149 newsrv->puid = atol(args[*cur_arg + 1]);
150 newsrv->conf.id.key = newsrv->puid;
151
152 if (newsrv->puid <= 0) {
153 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
154 return ERR_ALERT | ERR_FATAL;
155 }
156
157 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
158 if (node) {
159 struct server *target = container_of(node, struct server, conf.id);
160 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
161 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
162 target->id);
163 return ERR_ALERT | ERR_FATAL;
164 }
165
166 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200167 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200168 return 0;
169}
170
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200171/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200172 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200173 * shutdown.
174 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200175void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200176{
Willy Tarreau87b09662015-04-03 00:22:06 +0200177 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200178
Willy Tarreau87b09662015-04-03 00:22:06 +0200179 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
180 if (stream->srv_conn == srv)
181 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200182}
183
184/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200185 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200186 * the reason for the shutdown.
187 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200188void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200189{
190 struct server *srv;
191
192 for (srv = px->srv; srv != NULL; srv = srv->next)
193 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200194 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200195}
196
Willy Tarreaubda92272014-05-20 21:55:30 +0200197/* Appends some information to a message string related to a server going UP or
198 * DOWN. If both <forced> and <reason> are null and the server tracks another
199 * one, a "via" information will be provided to know where the status came from.
200 * If <reason> is non-null, the entire string will be appended after a comma and
201 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200202 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200203 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200204 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200205void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200206{
Willy Tarreaubda92272014-05-20 21:55:30 +0200207 if (reason)
208 chunk_appendf(msg, ", %s", reason);
209 else if (!forced && s->track)
210 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200211
212 if (xferred >= 0) {
213 if (s->state == SRV_ST_STOPPED)
214 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
215 " %d sessions active, %d requeued, %d remaining in queue",
216 s->proxy->srv_act, s->proxy->srv_bck,
217 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
218 s->cur_sess, xferred, s->nbpend);
219 else
220 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
221 " %d sessions requeued, %d total in queue",
222 s->proxy->srv_act, s->proxy->srv_bck,
223 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
224 xferred, s->nbpend);
225 }
226}
227
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200228/* Marks server <s> down, regardless of its checks' statuses, notifies by all
229 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200230 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200231 * recomputes the number of servers, but not the map. Maintenance servers are
232 * ignored. It reports <reason> if non-null as the reason for going down. Note
233 * that it makes use of the trash to build the log strings, so <reason> must
234 * not be placed there.
235 */
236void srv_set_stopped(struct server *s, const char *reason)
237{
238 struct server *srv;
239 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
240 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900241 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200242 int xferred;
243
244 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
245 return;
246
247 s->last_change = now.tv_sec;
248 s->state = SRV_ST_STOPPED;
249 if (s->proxy->lbprm.set_server_status_down)
250 s->proxy->lbprm.set_server_status_down(s);
251
252 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200253 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200254
Willy Tarreau87b09662015-04-03 00:22:06 +0200255 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200256 * a connection. Those which are redispatchable will be queued
257 * to another server or to the proxy itself.
258 */
259 xferred = pendconn_redistribute(s);
260
261 chunk_printf(&trash,
262 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
263 s->proxy->id, s->id);
264
265 srv_append_status(&trash, s, reason, xferred, 0);
266 Warning("%s.\n", trash.str);
267
268 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900269 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
270 send_log(s->proxy, log_level, "%s.\n", trash.str);
271 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200272
273 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
274 set_backend_down(s->proxy);
275
276 s->counters.down_trans++;
277
278 for (srv = s->trackers; srv; srv = srv->tracknext)
279 srv_set_stopped(srv, NULL);
280}
281
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200282/* Marks server <s> up regardless of its checks' statuses and provided it isn't
283 * in maintenance. Notifies by all available means, recounts the remaining
284 * servers on the proxy and tries to grab requests from the proxy. It
285 * automatically recomputes the number of servers, but not the map. Maintenance
286 * servers are ignored. It reports <reason> if non-null as the reason for going
287 * up. Note that it makes use of the trash to build the log strings, so <reason>
288 * must not be placed there.
289 */
290void srv_set_running(struct server *s, const char *reason)
291{
292 struct server *srv;
293 int xferred;
294
295 if (s->admin & SRV_ADMF_MAINT)
296 return;
297
298 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
299 return;
300
301 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
302 if (s->proxy->last_change < now.tv_sec) // ignore negative times
303 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
304 s->proxy->last_change = now.tv_sec;
305 }
306
307 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
308 s->down_time += now.tv_sec - s->last_change;
309
310 s->last_change = now.tv_sec;
311
312 s->state = SRV_ST_STARTING;
313 if (s->slowstart > 0)
314 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
315 else
316 s->state = SRV_ST_RUNNING;
317
318 server_recalc_eweight(s);
319
320 /* If the server is set with "on-marked-up shutdown-backup-sessions",
321 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200322 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200323 * on all backup servers.
324 */
325 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
326 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200327 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200328
329 /* check if we can handle some connections queued at the proxy. We
330 * will take as many as we can handle.
331 */
332 xferred = pendconn_grab_from_px(s);
333
334 chunk_printf(&trash,
335 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
336 s->proxy->id, s->id);
337
338 srv_append_status(&trash, s, reason, xferred, 0);
339 Warning("%s.\n", trash.str);
340 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900341 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200342
343 for (srv = s->trackers; srv; srv = srv->tracknext)
344 srv_set_running(srv, NULL);
345}
346
Willy Tarreau8eb77842014-05-21 13:54:57 +0200347/* Marks server <s> stopping regardless of its checks' statuses and provided it
348 * isn't in maintenance. Notifies by all available means, recounts the remaining
349 * servers on the proxy and tries to grab requests from the proxy. It
350 * automatically recomputes the number of servers, but not the map. Maintenance
351 * servers are ignored. It reports <reason> if non-null as the reason for going
352 * up. Note that it makes use of the trash to build the log strings, so <reason>
353 * must not be placed there.
354 */
355void srv_set_stopping(struct server *s, const char *reason)
356{
357 struct server *srv;
358 int xferred;
359
360 if (s->admin & SRV_ADMF_MAINT)
361 return;
362
363 if (s->state == SRV_ST_STOPPING)
364 return;
365
366 s->last_change = now.tv_sec;
367 s->state = SRV_ST_STOPPING;
368 if (s->proxy->lbprm.set_server_status_down)
369 s->proxy->lbprm.set_server_status_down(s);
370
Willy Tarreau87b09662015-04-03 00:22:06 +0200371 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200372 * a connection. Those which are redispatchable will be queued
373 * to another server or to the proxy itself.
374 */
375 xferred = pendconn_redistribute(s);
376
377 chunk_printf(&trash,
378 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
379 s->proxy->id, s->id);
380
381 srv_append_status(&trash, s, reason, xferred, 0);
382
383 Warning("%s.\n", trash.str);
384 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
385
386 if (!s->proxy->srv_bck && !s->proxy->srv_act)
387 set_backend_down(s->proxy);
388
389 for (srv = s->trackers; srv; srv = srv->tracknext)
390 srv_set_stopping(srv, NULL);
391}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200392
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200393/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
394 * enforce either maint mode or drain mode. It is not allowed to set more than
395 * one flag at once. The equivalent "inherited" flag is propagated to all
396 * tracking servers. Maintenance mode disables health checks (but not agent
397 * checks). When either the flag is already set or no flag is passed, nothing
398 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200399 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200400void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200401{
402 struct check *check = &s->check;
403 struct server *srv;
404 int xferred;
405
406 if (!mode)
407 return;
408
409 /* stop going down as soon as we meet a server already in the same state */
410 if (s->admin & mode)
411 return;
412
413 s->admin |= mode;
414
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200415 /* stop going down if the equivalent flag was already present (forced or inherited) */
416 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
417 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
418 return;
419
420 /* Maintenance must also disable health checks */
421 if (mode & SRV_ADMF_MAINT) {
422 if (s->check.state & CHK_ST_ENABLED) {
423 s->check.state |= CHK_ST_PAUSED;
424 check->health = 0;
425 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200426
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200427 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200428 chunk_printf(&trash,
429 "%sServer %s/%s was DOWN and now enters maintenance",
430 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
431
Willy Tarreaubda92272014-05-20 21:55:30 +0200432 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200433
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100434 if (!(global.mode & MODE_STARTING)) {
435 Warning("%s.\n", trash.str);
436 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
437 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200438 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200439 else { /* server was still running */
440 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
441 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
442
443 check->health = 0; /* failure */
444 s->last_change = now.tv_sec;
445 s->state = SRV_ST_STOPPED;
446 if (s->proxy->lbprm.set_server_status_down)
447 s->proxy->lbprm.set_server_status_down(s);
448
449 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200450 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200451
Willy Tarreau87b09662015-04-03 00:22:06 +0200452 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200453 * a connection. Those which are redispatchable will be queued
454 * to another server or to the proxy itself.
455 */
456 xferred = pendconn_redistribute(s);
457
458 chunk_printf(&trash,
459 "%sServer %s/%s is going DOWN for maintenance",
460 s->flags & SRV_F_BACKUP ? "Backup " : "",
461 s->proxy->id, s->id);
462
463 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
464
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100465 if (!(global.mode & MODE_STARTING)) {
466 Warning("%s.\n", trash.str);
467 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
468 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200469
470 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
471 set_backend_down(s->proxy);
472
473 s->counters.down_trans++;
474 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200475 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200476
477 /* drain state is applied only if not yet in maint */
478 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200479 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
480
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200481 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200482 if (s->proxy->lbprm.set_server_status_down)
483 s->proxy->lbprm.set_server_status_down(s);
484
Willy Tarreau87b09662015-04-03 00:22:06 +0200485 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200486 * a connection. Those which are redispatchable will be queued
487 * to another server or to the proxy itself.
488 */
489 xferred = pendconn_redistribute(s);
490
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200491 chunk_printf(&trash, "%sServer %s/%s enters drain state",
492 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200493
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200494 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200495
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100496 if (!(global.mode & MODE_STARTING)) {
497 Warning("%s.\n", trash.str);
498 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
499 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
500 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200501 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
502 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200503 }
504
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200505 /* compute the inherited flag to propagate */
506 if (mode & SRV_ADMF_MAINT)
507 mode = SRV_ADMF_IMAINT;
508 else if (mode & SRV_ADMF_DRAIN)
509 mode = SRV_ADMF_IDRAIN;
510
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200511 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200512 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200513}
514
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200515/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
516 * stop enforcing either maint mode or drain mode. It is not allowed to set more
517 * than one flag at once. The equivalent "inherited" flag is propagated to all
518 * tracking servers. Leaving maintenance mode re-enables health checks. When
519 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200520 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200521void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200522{
523 struct check *check = &s->check;
524 struct server *srv;
525 int xferred = -1;
526
527 if (!mode)
528 return;
529
530 /* stop going down as soon as we see the flag is not there anymore */
531 if (!(s->admin & mode))
532 return;
533
534 s->admin &= ~mode;
535
536 if (s->admin & SRV_ADMF_MAINT) {
537 /* remaining in maintenance mode, let's inform precisely about the
538 * situation.
539 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200540 if (mode & SRV_ADMF_FMAINT) {
541 chunk_printf(&trash,
542 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
543 s->flags & SRV_F_BACKUP ? "Backup " : "",
544 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200545
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200546 if (s->track) /* normally it's mandatory here */
547 chunk_appendf(&trash, " via %s/%s",
548 s->track->proxy->id, s->track->id);
549 Warning("%s.\n", trash.str);
550 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
551 }
552 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200553 chunk_printf(&trash,
554 "%sServer %s/%s remains in forced maintenance",
555 s->flags & SRV_F_BACKUP ? "Backup " : "",
556 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200557 Warning("%s.\n", trash.str);
558 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
559 }
560 /* don't report anything when leaving drain mode and remaining in maintenance */
561 }
562 else if (mode & SRV_ADMF_MAINT) {
563 /* OK here we're leaving maintenance, we have many things to check,
564 * because the server might possibly be coming back up depending on
565 * its state. In practice, leaving maintenance means that we should
566 * immediately turn to UP (more or less the slowstart) under the
567 * following conditions :
568 * - server is neither checked nor tracked
569 * - server tracks another server which is not checked
570 * - server tracks another server which is already up
571 * Which sums up as something simpler :
572 * "either the tracking server is up or the server's checks are disabled
573 * or up". Otherwise we only re-enable health checks. There's a special
574 * case associated to the stopping state which can be inherited. Note
575 * that the server might still be in drain mode, which is naturally dealt
576 * with by the lower level functions.
577 */
578
579 if (s->check.state & CHK_ST_ENABLED) {
580 s->check.state &= ~CHK_ST_PAUSED;
581 check->health = check->rise; /* start OK but check immediately */
582 }
583
584 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
585 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
586 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
587 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
588 if (s->proxy->last_change < now.tv_sec) // ignore negative times
589 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
590 s->proxy->last_change = now.tv_sec;
591 }
592
593 if (s->last_change < now.tv_sec) // ignore negative times
594 s->down_time += now.tv_sec - s->last_change;
595 s->last_change = now.tv_sec;
596
597 if (s->track && s->track->state == SRV_ST_STOPPING)
598 s->state = SRV_ST_STOPPING;
599 else {
600 s->state = SRV_ST_STARTING;
601 if (s->slowstart > 0)
602 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
603 else
604 s->state = SRV_ST_RUNNING;
605 }
606
607 server_recalc_eweight(s);
608
609 /* If the server is set with "on-marked-up shutdown-backup-sessions",
610 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200611 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200612 * on all backup servers.
613 */
614 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
615 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200616 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200617
618 /* check if we can handle some connections queued at the proxy. We
619 * will take as many as we can handle.
620 */
621 xferred = pendconn_grab_from_px(s);
622 }
623
624 if (mode & SRV_ADMF_FMAINT) {
625 chunk_printf(&trash,
626 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
627 s->flags & SRV_F_BACKUP ? "Backup " : "",
628 s->proxy->id, s->id,
629 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
630 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200631 }
632 else {
633 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200634 "%sServer %s/%s is %s/%s (leaving maintenance)",
635 s->flags & SRV_F_BACKUP ? "Backup " : "",
636 s->proxy->id, s->id,
637 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
638 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
639 srv_append_status(&trash, s, NULL, xferred, 0);
640 }
641 Warning("%s.\n", trash.str);
642 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
643 }
644 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
645 /* remaining in drain mode after removing one of its flags */
646
647 if (mode & SRV_ADMF_FDRAIN) {
648 chunk_printf(&trash,
649 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200650 s->flags & SRV_F_BACKUP ? "Backup " : "",
651 s->proxy->id, s->id);
652
653 if (s->track) /* normally it's mandatory here */
654 chunk_appendf(&trash, " via %s/%s",
655 s->track->proxy->id, s->track->id);
656 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200657 else {
658 chunk_printf(&trash,
659 "%sServer %s/%s remains in forced drain mode",
660 s->flags & SRV_F_BACKUP ? "Backup " : "",
661 s->proxy->id, s->id);
662 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200663 Warning("%s.\n", trash.str);
664 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200665 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200666 else if (mode & SRV_ADMF_DRAIN) {
667 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200668 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
669 if (s->proxy->last_change < now.tv_sec) // ignore negative times
670 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
671 s->proxy->last_change = now.tv_sec;
672 }
673
674 if (s->last_change < now.tv_sec) // ignore negative times
675 s->down_time += now.tv_sec - s->last_change;
676 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200677 server_recalc_eweight(s);
678
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200679 if (mode & SRV_ADMF_FDRAIN) {
680 chunk_printf(&trash,
681 "%sServer %s/%s is %s (leaving forced drain)",
682 s->flags & SRV_F_BACKUP ? "Backup " : "",
683 s->proxy->id, s->id,
684 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
685 }
686 else {
687 chunk_printf(&trash,
688 "%sServer %s/%s is %s (leaving drain)",
689 s->flags & SRV_F_BACKUP ? "Backup " : "",
690 s->proxy->id, s->id,
691 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
692 if (s->track) /* normally it's mandatory here */
693 chunk_appendf(&trash, " via %s/%s",
694 s->track->proxy->id, s->track->id);
695 }
696 Warning("%s.\n", trash.str);
697 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200698 }
699
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200700 /* stop going down if the equivalent flag is still present (forced or inherited) */
701 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
702 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
703 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200704
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200705 if (mode & SRV_ADMF_MAINT)
706 mode = SRV_ADMF_IMAINT;
707 else if (mode & SRV_ADMF_DRAIN)
708 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200709
710 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200711 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200712}
713
Willy Tarreau757478e2016-11-03 19:22:19 +0100714/* principle: propagate maint and drain to tracking servers. This is useful
715 * upon startup so that inherited states are correct.
716 */
717static void srv_propagate_admin_state(struct server *srv)
718{
719 struct server *srv2;
720
721 if (!srv->trackers)
722 return;
723
724 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
725 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
726 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT);
727
728 if (srv->admin & SRV_ADMF_DRAIN)
729 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN);
730 }
731}
732
733/* Compute and propagate the admin states for all servers in proxy <px>.
734 * Only servers *not* tracking another one are considered, because other
735 * ones will be handled when the server they track is visited.
736 */
737void srv_compute_all_admin_states(struct proxy *px)
738{
739 struct server *srv;
740
741 for (srv = px->srv; srv; srv = srv->next) {
742 if (srv->track)
743 continue;
744 srv_propagate_admin_state(srv);
745 }
746}
747
Willy Tarreaudff55432012-10-10 17:51:05 +0200748/* Note: must not be declared <const> as its list will be overwritten.
749 * Please take care of keeping this list alphabetically sorted, doing so helps
750 * all code contributors.
751 * Optional keywords are also declared with a NULL ->parse() function so that
752 * the config parser can report an appropriate error when a known keyword was
753 * not enabled.
754 */
755static struct srv_kw_list srv_kws = { "ALL", { }, {
756 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
757 { NULL, NULL, 0 },
758}};
759
760__attribute__((constructor))
761static void __listener_init(void)
762{
763 srv_register_keywords(&srv_kws);
764}
765
Willy Tarreau004e0452013-11-21 11:22:01 +0100766/* Recomputes the server's eweight based on its state, uweight, the current time,
767 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
768 * state is automatically disabled if the time is elapsed.
769 */
770void server_recalc_eweight(struct server *sv)
771{
772 struct proxy *px = sv->proxy;
773 unsigned w;
774
775 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
776 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200777 if (sv->state == SRV_ST_STARTING)
778 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100779 }
780
781 /* We must take care of not pushing the server to full throttle during slow starts.
782 * It must also start immediately, at least at the minimal step when leaving maintenance.
783 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200784 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100785 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
786 else
787 w = px->lbprm.wdiv;
788
789 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
790
791 /* now propagate the status change to any LB algorithms */
792 if (px->lbprm.update_server_eweight)
793 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200794 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100795 if (px->lbprm.set_server_status_up)
796 px->lbprm.set_server_status_up(sv);
797 }
798 else {
799 if (px->lbprm.set_server_status_down)
800 px->lbprm.set_server_status_down(sv);
801 }
802}
803
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900805 * Parses weight_str and configures sv accordingly.
806 * Returns NULL on success, error message string otherwise.
807 */
808const char *server_parse_weight_change_request(struct server *sv,
809 const char *weight_str)
810{
811 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900812 long int w;
813 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900814
815 px = sv->proxy;
816
817 /* if the weight is terminated with '%', it is set relative to
818 * the initial weight, otherwise it is absolute.
819 */
820 if (!*weight_str)
821 return "Require <weight> or <weight%>.\n";
822
Simon Hormanb796afa2013-02-12 10:45:53 +0900823 w = strtol(weight_str, &end, 10);
824 if (end == weight_str)
825 return "Empty weight string empty or preceded by garbage";
826 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900827 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900828 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900829 /* Avoid integer overflow */
830 if (w > 25600)
831 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900832 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900833 if (w > 256)
834 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900835 }
836 else if (w < 0 || w > 256)
837 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900838 else if (end[0] != '\0')
839 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900840
841 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
842 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
843
844 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100845 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900846
847 return NULL;
848}
849
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200850/*
Thierry Fournier09a91782016-02-24 08:25:39 +0100851 * Parses <addr_str> and configures <sv> accordingly. <from> precise
852 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200853 * Returns:
854 * - error string on error
855 * - NULL on success
856 */
857const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +0100858 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200859{
860 unsigned char ip[INET6_ADDRSTRLEN];
861
862 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100863 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200864 return NULL;
865 }
866 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100867 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200868 return NULL;
869 }
870
871 return "Could not understand IP address format.\n";
872}
873
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200874const char *server_parse_maxconn_change_request(struct server *sv,
875 const char *maxconn_str)
876{
877 long int v;
878 char *end;
879
880 if (!*maxconn_str)
881 return "Require <maxconn>.\n";
882
883 v = strtol(maxconn_str, &end, 10);
884 if (end == maxconn_str)
885 return "maxconn string empty or preceded by garbage";
886 else if (end[0] != '\0')
887 return "Trailing garbage in maxconn string";
888
889 if (sv->maxconn == sv->minconn) { // static maxconn
890 sv->maxconn = sv->minconn = v;
891 } else { // dynamic maxconn
892 sv->maxconn = v;
893 }
894
895 if (may_dequeue_tasks(sv, sv->proxy))
896 process_srv_queue(sv);
897
898 return NULL;
899}
900
Willy Tarreau272adea2014-03-31 10:39:59 +0200901int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
902{
903 struct server *newsrv = NULL;
904 const char *err;
905 char *errmsg = NULL;
906 int err_code = 0;
907 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +0200908 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +0200909
910 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
911 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +0200912 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
913
914 if (!defsrv && curproxy == defproxy) {
915 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
916 err_code |= ERR_ALERT | ERR_FATAL;
917 goto out;
918 }
919 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
920 err_code |= ERR_ALERT | ERR_FATAL;
921
922 if (!*args[2]) {
923 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
924 file, linenum, args[0]);
925 err_code |= ERR_ALERT | ERR_FATAL;
926 goto out;
927 }
928
929 err = invalid_char(args[1]);
930 if (err && !defsrv) {
931 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
932 file, linenum, *err, args[1]);
933 err_code |= ERR_ALERT | ERR_FATAL;
934 goto out;
935 }
936
937 if (!defsrv) {
938 struct sockaddr_storage *sk;
939 int port1, port2;
940 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200941 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200942
Vincent Bernat02779b62016-04-03 13:48:43 +0200943 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200944 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
945 err_code |= ERR_ALERT | ERR_ABORT;
946 goto out;
947 }
948
949 /* the servers are linked backwards first */
950 newsrv->next = curproxy->srv;
951 curproxy->srv = newsrv;
952 newsrv->proxy = curproxy;
953 newsrv->conf.file = strdup(file);
954 newsrv->conf.line = linenum;
955
956 newsrv->obj_type = OBJ_TYPE_SERVER;
957 LIST_INIT(&newsrv->actconns);
958 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +0200959 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +0200960 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +0200961 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +0200962 do_check = 0;
963 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200964 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200965 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200966 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200967 newsrv->last_change = now.tv_sec;
968 newsrv->id = strdup(args[1]);
969
970 /* several ways to check the port component :
971 * - IP => port=+0, relative (IPv4 only)
972 * - IP: => port=+0, relative
973 * - IP:N => port=N, absolute
974 * - IP:+N => port=+N, relative
975 * - IP:-N => port=-N, relative
976 */
Baptiste Assmann83cbaa52016-11-02 15:34:05 +0100977 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +0200978 if (!sk) {
979 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
980 err_code |= ERR_ALERT | ERR_FATAL;
981 goto out;
982 }
983
984 proto = protocol_by_family(sk->ss_family);
985 if (!proto || !proto->connect) {
986 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
987 file, linenum, args[0], args[1]);
988 err_code |= ERR_ALERT | ERR_FATAL;
989 goto out;
990 }
991
992 if (!port1 || !port2) {
993 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200994 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200995 }
996 else if (port1 != port2) {
997 /* port range */
998 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
999 file, linenum, args[0], args[1], args[2]);
1000 err_code |= ERR_ALERT | ERR_FATAL;
1001 goto out;
1002 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001003
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001004 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001005 newsrv->hostname = fqdn;
1006 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001007 goto skip_name_resolution;
1008
Willy Tarreau07101d52015-09-08 16:16:35 +02001009 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001010 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001011 goto skip_name_resolution;
1012
1013 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1014 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1015 goto skip_name_resolution;
1016 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1017 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1018 file, linenum, args[2]);
1019 err_code |= ERR_ALERT | ERR_FATAL;
1020 goto out;
1021 }
1022
1023 curr_resolution->requester = newsrv;
1024 curr_resolution->requester_cb = snr_resolution_cb;
1025 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1026 curr_resolution->status = RSLV_STATUS_NONE;
1027 curr_resolution->step = RSLV_STEP_NONE;
1028 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001029 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001030 newsrv->resolution = curr_resolution;
1031
1032 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001033 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +01001034 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +02001035
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001036 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001037 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1038 file, linenum, newsrv->addr.ss_family, args[2]);
1039 err_code |= ERR_ALERT | ERR_FATAL;
1040 goto out;
1041 }
1042
1043 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1044 newsrv->check.port = curproxy->defsrv.check.port;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001045 if (newsrv->check.port)
1046 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001047 newsrv->check.inter = curproxy->defsrv.check.inter;
1048 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1049 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1050 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1051 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001052 if (curproxy->defsrv.agent.send_string != NULL)
1053 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1054 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001055 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1056 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1057 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1058 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1059 newsrv->minconn = curproxy->defsrv.minconn;
1060 newsrv->maxconn = curproxy->defsrv.maxconn;
1061 newsrv->slowstart = curproxy->defsrv.slowstart;
1062 newsrv->onerror = curproxy->defsrv.onerror;
1063 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1064 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1065 newsrv->consecutive_errors_limit
1066 = curproxy->defsrv.consecutive_errors_limit;
1067#ifdef OPENSSL
1068 newsrv->use_ssl = curproxy->defsrv.use_ssl;
1069#endif
1070 newsrv->uweight = newsrv->iweight
1071 = curproxy->defsrv.iweight;
1072
1073 newsrv->check.status = HCHK_STATUS_INI;
1074 newsrv->check.rise = curproxy->defsrv.check.rise;
1075 newsrv->check.fall = curproxy->defsrv.check.fall;
1076 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1077 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001078 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001079
1080 newsrv->agent.status = HCHK_STATUS_INI;
1081 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1082 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1083 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1084 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001085 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1086 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1087 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001088 memcpy(newsrv->dns_opts.pref_net,
1089 curproxy->defsrv.dns_opts.pref_net,
1090 sizeof(newsrv->dns_opts.pref_net));
1091 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Willy Tarreau272adea2014-03-31 10:39:59 +02001092
1093 cur_arg = 3;
1094 } else {
1095 newsrv = &curproxy->defsrv;
1096 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001097 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001098 }
1099
1100 while (*args[cur_arg]) {
1101 if (!strcmp(args[cur_arg], "agent-check")) {
1102 global.maxsock++;
1103 do_agent = 1;
1104 cur_arg += 1;
1105 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1106 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1107 if (err) {
1108 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1109 file, linenum, *err, newsrv->id);
1110 err_code |= ERR_ALERT | ERR_FATAL;
1111 goto out;
1112 }
1113 if (val <= 0) {
1114 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1115 file, linenum, val, args[cur_arg], newsrv->id);
1116 err_code |= ERR_ALERT | ERR_FATAL;
1117 goto out;
1118 }
1119 newsrv->agent.inter = val;
1120 cur_arg += 2;
1121 }
1122 else if (!strcmp(args[cur_arg], "agent-port")) {
1123 global.maxsock++;
1124 newsrv->agent.port = atol(args[cur_arg + 1]);
1125 cur_arg += 2;
1126 }
James Brown55f9ff12015-10-21 18:19:05 -07001127 else if (!strcmp(args[cur_arg], "agent-send")) {
1128 global.maxsock++;
1129 free(newsrv->agent.send_string);
1130 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1131 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1132 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1133 cur_arg += 2;
1134 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001135 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1136 newsrv->cookie = strdup(args[cur_arg + 1]);
1137 newsrv->cklen = strlen(args[cur_arg + 1]);
1138 cur_arg += 2;
1139 }
1140 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1141 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1142 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1143 cur_arg += 2;
1144 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001145 else if (!strcmp(args[cur_arg], "resolvers")) {
1146 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1147 cur_arg += 2;
1148 }
1149 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1150 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001151 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001152 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001153 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001154 else {
1155 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1156 file, linenum, args[cur_arg]);
1157 err_code |= ERR_ALERT | ERR_FATAL;
1158 goto out;
1159 }
1160 cur_arg += 2;
1161 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001162 else if (!strcmp(args[cur_arg], "resolve-net")) {
1163 char *p, *e;
1164 unsigned char mask;
1165 struct dns_options *opt;
1166
1167 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1168 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1169 file, linenum, args[cur_arg]);
1170 err_code |= ERR_ALERT | ERR_FATAL;
1171 goto out;
1172 }
1173
1174 opt = &newsrv->dns_opts;
1175
1176 /* Split arguments by comma, and convert it from ipv4 or ipv6
1177 * string network in in_addr or in6_addr.
1178 */
1179 p = args[cur_arg + 1];
1180 e = p;
1181 while (*p != '\0') {
1182 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001183 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001184 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1185 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1186 err_code |= ERR_ALERT | ERR_FATAL;
1187 goto out;
1188 }
1189 /* look for end or comma. */
1190 while (*e != ',' && *e != '\0')
1191 e++;
1192 if (*e == ',') {
1193 *e = '\0';
1194 e++;
1195 }
1196 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1197 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1198 /* Try to convert input string from ipv4 or ipv6 network. */
1199 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1200 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1201 &mask)) {
1202 /* Try to convert input string from ipv6 network. */
1203 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1204 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1205 } else {
1206 /* All network conversions fail, retrun error. */
1207 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1208 file, linenum, args[cur_arg], p);
1209 err_code |= ERR_ALERT | ERR_FATAL;
1210 goto out;
1211 }
1212 opt->pref_net_nb++;
1213 p = e;
1214 }
1215
1216 cur_arg += 2;
1217 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001218 else if (!strcmp(args[cur_arg], "rise")) {
1219 if (!*args[cur_arg + 1]) {
1220 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1221 file, linenum, args[cur_arg]);
1222 err_code |= ERR_ALERT | ERR_FATAL;
1223 goto out;
1224 }
1225
1226 newsrv->check.rise = atol(args[cur_arg + 1]);
1227 if (newsrv->check.rise <= 0) {
1228 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1229 file, linenum, args[cur_arg]);
1230 err_code |= ERR_ALERT | ERR_FATAL;
1231 goto out;
1232 }
1233
1234 if (newsrv->check.health)
1235 newsrv->check.health = newsrv->check.rise;
1236 cur_arg += 2;
1237 }
1238 else if (!strcmp(args[cur_arg], "fall")) {
1239 newsrv->check.fall = atol(args[cur_arg + 1]);
1240
1241 if (!*args[cur_arg + 1]) {
1242 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1243 file, linenum, args[cur_arg]);
1244 err_code |= ERR_ALERT | ERR_FATAL;
1245 goto out;
1246 }
1247
1248 if (newsrv->check.fall <= 0) {
1249 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1250 file, linenum, args[cur_arg]);
1251 err_code |= ERR_ALERT | ERR_FATAL;
1252 goto out;
1253 }
1254
1255 cur_arg += 2;
1256 }
1257 else if (!strcmp(args[cur_arg], "inter")) {
1258 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1259 if (err) {
1260 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1261 file, linenum, *err, newsrv->id);
1262 err_code |= ERR_ALERT | ERR_FATAL;
1263 goto out;
1264 }
1265 if (val <= 0) {
1266 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1267 file, linenum, val, args[cur_arg], newsrv->id);
1268 err_code |= ERR_ALERT | ERR_FATAL;
1269 goto out;
1270 }
1271 newsrv->check.inter = val;
1272 cur_arg += 2;
1273 }
1274 else if (!strcmp(args[cur_arg], "fastinter")) {
1275 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1276 if (err) {
1277 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1278 file, linenum, *err, newsrv->id);
1279 err_code |= ERR_ALERT | ERR_FATAL;
1280 goto out;
1281 }
1282 if (val <= 0) {
1283 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1284 file, linenum, val, args[cur_arg], newsrv->id);
1285 err_code |= ERR_ALERT | ERR_FATAL;
1286 goto out;
1287 }
1288 newsrv->check.fastinter = val;
1289 cur_arg += 2;
1290 }
1291 else if (!strcmp(args[cur_arg], "downinter")) {
1292 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1293 if (err) {
1294 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1295 file, linenum, *err, newsrv->id);
1296 err_code |= ERR_ALERT | ERR_FATAL;
1297 goto out;
1298 }
1299 if (val <= 0) {
1300 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1301 file, linenum, val, args[cur_arg], newsrv->id);
1302 err_code |= ERR_ALERT | ERR_FATAL;
1303 goto out;
1304 }
1305 newsrv->check.downinter = val;
1306 cur_arg += 2;
1307 }
1308 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1309 struct sockaddr_storage *sk;
1310 int port1, port2;
1311 struct protocol *proto;
1312
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001313 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001314 if (!sk) {
1315 Alert("parsing [%s:%d] : '%s' : %s\n",
1316 file, linenum, args[cur_arg], errmsg);
1317 err_code |= ERR_ALERT | ERR_FATAL;
1318 goto out;
1319 }
1320
1321 proto = protocol_by_family(sk->ss_family);
1322 if (!proto || !proto->connect) {
1323 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1324 file, linenum, args[cur_arg], args[cur_arg + 1]);
1325 err_code |= ERR_ALERT | ERR_FATAL;
1326 goto out;
1327 }
1328
1329 if (port1 != port2) {
1330 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1331 file, linenum, args[cur_arg], args[cur_arg + 1]);
1332 err_code |= ERR_ALERT | ERR_FATAL;
1333 goto out;
1334 }
1335
Simon Horman41f58762015-01-30 11:22:56 +09001336 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001337 newsrv->flags |= SRV_F_CHECKADDR;
1338 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001339 cur_arg += 2;
1340 }
1341 else if (!strcmp(args[cur_arg], "port")) {
1342 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001343 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001344 cur_arg += 2;
1345 }
1346 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001347 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001348 cur_arg ++;
1349 }
1350 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001351 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001352 cur_arg ++;
1353 }
1354 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001355 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001356 cur_arg ++;
1357 }
David Safb76832014-05-08 23:42:08 -04001358 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1359 newsrv->pp_opts |= SRV_PP_V2;
1360 cur_arg ++;
1361 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001362 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1363 newsrv->check.send_proxy = 1;
1364 cur_arg ++;
1365 }
1366 else if (!strcmp(args[cur_arg], "weight")) {
1367 int w;
1368 w = atol(args[cur_arg + 1]);
1369 if (w < 0 || w > SRV_UWGHT_MAX) {
1370 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1371 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1372 err_code |= ERR_ALERT | ERR_FATAL;
1373 goto out;
1374 }
1375 newsrv->uweight = newsrv->iweight = w;
1376 cur_arg += 2;
1377 }
1378 else if (!strcmp(args[cur_arg], "minconn")) {
1379 newsrv->minconn = atol(args[cur_arg + 1]);
1380 cur_arg += 2;
1381 }
1382 else if (!strcmp(args[cur_arg], "maxconn")) {
1383 newsrv->maxconn = atol(args[cur_arg + 1]);
1384 cur_arg += 2;
1385 }
1386 else if (!strcmp(args[cur_arg], "maxqueue")) {
1387 newsrv->maxqueue = atol(args[cur_arg + 1]);
1388 cur_arg += 2;
1389 }
1390 else if (!strcmp(args[cur_arg], "slowstart")) {
1391 /* slowstart is stored in seconds */
1392 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1393 if (err) {
1394 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1395 file, linenum, *err, newsrv->id);
1396 err_code |= ERR_ALERT | ERR_FATAL;
1397 goto out;
1398 }
1399 newsrv->slowstart = (val + 999) / 1000;
1400 cur_arg += 2;
1401 }
1402 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1403
1404 if (!*args[cur_arg + 1]) {
1405 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1406 file, linenum);
1407 err_code |= ERR_ALERT | ERR_FATAL;
1408 goto out;
1409 }
1410
1411 newsrv->trackit = strdup(args[cur_arg + 1]);
1412
1413 cur_arg += 2;
1414 }
1415 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1416 global.maxsock++;
1417 do_check = 1;
1418 cur_arg += 1;
1419 }
1420 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001421 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001422 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001423 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001424 newsrv->check.state |= CHK_ST_PAUSED;
1425 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001426 cur_arg += 1;
1427 }
1428 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1429 if (!strcmp(args[cur_arg + 1], "none"))
1430 newsrv->observe = HANA_OBS_NONE;
1431 else if (!strcmp(args[cur_arg + 1], "layer4"))
1432 newsrv->observe = HANA_OBS_LAYER4;
1433 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1434 if (curproxy->mode != PR_MODE_HTTP) {
1435 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1436 file, linenum, args[cur_arg + 1]);
1437 err_code |= ERR_ALERT;
1438 }
1439 newsrv->observe = HANA_OBS_LAYER7;
1440 }
1441 else {
1442 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1443 "'layer4', 'layer7' but got '%s'\n",
1444 file, linenum, args[cur_arg], args[cur_arg + 1]);
1445 err_code |= ERR_ALERT | ERR_FATAL;
1446 goto out;
1447 }
1448
1449 cur_arg += 2;
1450 }
1451 else if (!strcmp(args[cur_arg], "on-error")) {
1452 if (!strcmp(args[cur_arg + 1], "fastinter"))
1453 newsrv->onerror = HANA_ONERR_FASTINTER;
1454 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1455 newsrv->onerror = HANA_ONERR_FAILCHK;
1456 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1457 newsrv->onerror = HANA_ONERR_SUDDTH;
1458 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1459 newsrv->onerror = HANA_ONERR_MARKDWN;
1460 else {
1461 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1462 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1463 file, linenum, args[cur_arg], args[cur_arg + 1]);
1464 err_code |= ERR_ALERT | ERR_FATAL;
1465 goto out;
1466 }
1467
1468 cur_arg += 2;
1469 }
1470 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1471 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1472 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1473 else {
1474 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1475 file, linenum, args[cur_arg], args[cur_arg + 1]);
1476 err_code |= ERR_ALERT | ERR_FATAL;
1477 goto out;
1478 }
1479
1480 cur_arg += 2;
1481 }
1482 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1483 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1484 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1485 else {
1486 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1487 file, linenum, args[cur_arg], args[cur_arg + 1]);
1488 err_code |= ERR_ALERT | ERR_FATAL;
1489 goto out;
1490 }
1491
1492 cur_arg += 2;
1493 }
1494 else if (!strcmp(args[cur_arg], "error-limit")) {
1495 if (!*args[cur_arg + 1]) {
1496 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1497 file, linenum, args[cur_arg]);
1498 err_code |= ERR_ALERT | ERR_FATAL;
1499 goto out;
1500 }
1501
1502 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1503
1504 if (newsrv->consecutive_errors_limit <= 0) {
1505 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1506 file, linenum, args[cur_arg]);
1507 err_code |= ERR_ALERT | ERR_FATAL;
1508 goto out;
1509 }
1510 cur_arg += 2;
1511 }
1512 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1513 int port_low, port_high;
1514 struct sockaddr_storage *sk;
1515 struct protocol *proto;
1516
1517 if (!*args[cur_arg + 1]) {
1518 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1519 file, linenum, "source", "usesrc", "interface");
1520 err_code |= ERR_ALERT | ERR_FATAL;
1521 goto out;
1522 }
1523
1524 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001525 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001526 if (!sk) {
1527 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1528 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1529 err_code |= ERR_ALERT | ERR_FATAL;
1530 goto out;
1531 }
1532
1533 proto = protocol_by_family(sk->ss_family);
1534 if (!proto || !proto->connect) {
1535 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1536 file, linenum, args[cur_arg], args[cur_arg+1]);
1537 err_code |= ERR_ALERT | ERR_FATAL;
1538 goto out;
1539 }
1540
1541 newsrv->conn_src.source_addr = *sk;
1542
1543 if (port_low != port_high) {
1544 int i;
1545
1546 if (!port_low || !port_high) {
1547 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1548 file, linenum, args[cur_arg], args[cur_arg + 1]);
1549 err_code |= ERR_ALERT | ERR_FATAL;
1550 goto out;
1551 }
1552
1553 if (port_low <= 0 || port_low > 65535 ||
1554 port_high <= 0 || port_high > 65535 ||
1555 port_low > port_high) {
1556 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1557 file, linenum, port_low, port_high);
1558 err_code |= ERR_ALERT | ERR_FATAL;
1559 goto out;
1560 }
1561 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1562 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1563 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1564 }
1565
1566 cur_arg += 2;
1567 while (*(args[cur_arg])) {
1568 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001569#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001570 if (!*args[cur_arg + 1]) {
1571 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1572 file, linenum, "usesrc");
1573 err_code |= ERR_ALERT | ERR_FATAL;
1574 goto out;
1575 }
1576 if (!strcmp(args[cur_arg + 1], "client")) {
1577 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1578 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1579 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1580 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1581 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1582 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1583 char *name, *end;
1584
1585 name = args[cur_arg+1] + 7;
1586 while (isspace(*name))
1587 name++;
1588
1589 end = name;
1590 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1591 end++;
1592
1593 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1594 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1595 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1596 newsrv->conn_src.bind_hdr_len = end - name;
1597 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1598 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1599 newsrv->conn_src.bind_hdr_occ = -1;
1600
1601 /* now look for an occurrence number */
1602 while (isspace(*end))
1603 end++;
1604 if (*end == ',') {
1605 end++;
1606 name = end;
1607 if (*end == '-')
1608 end++;
1609 while (isdigit((int)*end))
1610 end++;
1611 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1612 }
1613
1614 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1615 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1616 " occurrences values smaller than %d.\n",
1617 file, linenum, MAX_HDR_HISTORY);
1618 err_code |= ERR_ALERT | ERR_FATAL;
1619 goto out;
1620 }
1621 } else {
1622 struct sockaddr_storage *sk;
1623 int port1, port2;
1624
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001625 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001626 if (!sk) {
1627 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1628 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1629 err_code |= ERR_ALERT | ERR_FATAL;
1630 goto out;
1631 }
1632
1633 proto = protocol_by_family(sk->ss_family);
1634 if (!proto || !proto->connect) {
1635 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1636 file, linenum, args[cur_arg], args[cur_arg+1]);
1637 err_code |= ERR_ALERT | ERR_FATAL;
1638 goto out;
1639 }
1640
1641 if (port1 != port2) {
1642 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1643 file, linenum, args[cur_arg], args[cur_arg + 1]);
1644 err_code |= ERR_ALERT | ERR_FATAL;
1645 goto out;
1646 }
1647 newsrv->conn_src.tproxy_addr = *sk;
1648 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1649 }
1650 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001651 cur_arg += 2;
1652 continue;
1653#else /* no TPROXY support */
1654 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1655 file, linenum, "usesrc");
1656 err_code |= ERR_ALERT | ERR_FATAL;
1657 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001658#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001659 } /* "usesrc" */
1660
1661 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1662#ifdef SO_BINDTODEVICE
1663 if (!*args[cur_arg + 1]) {
1664 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1665 file, linenum, args[0]);
1666 err_code |= ERR_ALERT | ERR_FATAL;
1667 goto out;
1668 }
1669 free(newsrv->conn_src.iface_name);
1670 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1671 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1672 global.last_checks |= LSTCHK_NETADM;
1673#else
1674 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1675 file, linenum, args[0], args[cur_arg]);
1676 err_code |= ERR_ALERT | ERR_FATAL;
1677 goto out;
1678#endif
1679 cur_arg += 2;
1680 continue;
1681 }
1682 /* this keyword in not an option of "source" */
1683 break;
1684 } /* while */
1685 }
1686 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1687 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1688 file, linenum, "usesrc", "source");
1689 err_code |= ERR_ALERT | ERR_FATAL;
1690 goto out;
1691 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001692 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1693#ifdef CONFIG_HAP_NS
1694 char *arg = args[cur_arg + 1];
1695 if (!strcmp(arg, "*")) {
1696 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1697 } else {
1698 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1699
1700 if (newsrv->netns == NULL)
1701 newsrv->netns = netns_store_insert(arg);
1702
1703 if (newsrv->netns == NULL) {
1704 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1705 err_code |= ERR_ALERT | ERR_FATAL;
1706 goto out;
1707 }
1708 }
1709#else
1710 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1711 file, linenum, args[0], args[cur_arg]);
1712 err_code |= ERR_ALERT | ERR_FATAL;
1713 goto out;
1714#endif
1715 cur_arg += 2;
1716 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001717 else {
1718 static int srv_dumped;
1719 struct srv_kw *kw;
1720 char *err;
1721
1722 kw = srv_find_kw(args[cur_arg]);
1723 if (kw) {
1724 char *err = NULL;
1725 int code;
1726
1727 if (!kw->parse) {
1728 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1729 file, linenum, args[0], args[1], args[cur_arg]);
1730 cur_arg += 1 + kw->skip ;
1731 err_code |= ERR_ALERT | ERR_FATAL;
1732 goto out;
1733 }
1734
1735 if (defsrv && !kw->default_ok) {
1736 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1737 file, linenum, args[0], args[1], args[cur_arg]);
1738 cur_arg += 1 + kw->skip ;
1739 err_code |= ERR_ALERT;
1740 continue;
1741 }
1742
1743 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1744 err_code |= code;
1745
1746 if (code) {
1747 if (err && *err) {
1748 indent_msg(&err, 2);
1749 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1750 }
1751 else
1752 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1753 file, linenum, args[0], args[1], args[cur_arg]);
1754 if (code & ERR_FATAL) {
1755 free(err);
1756 cur_arg += 1 + kw->skip;
1757 goto out;
1758 }
1759 }
1760 free(err);
1761 cur_arg += 1 + kw->skip;
1762 continue;
1763 }
1764
1765 err = NULL;
1766 if (!srv_dumped) {
1767 srv_dump_kws(&err);
1768 indent_msg(&err, 4);
1769 srv_dumped = 1;
1770 }
1771
1772 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1773 file, linenum, args[0], args[1], args[cur_arg],
1774 err ? " Registered keywords :" : "", err ? err : "");
1775 free(err);
1776
1777 err_code |= ERR_ALERT | ERR_FATAL;
1778 goto out;
1779 }
1780 }
1781
Willy Tarreau272adea2014-03-31 10:39:59 +02001782 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001783 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001784
1785 if (newsrv->trackit) {
1786 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1787 file, linenum);
1788 err_code |= ERR_ALERT | ERR_FATAL;
1789 goto out;
1790 }
1791
Willy Tarreau272adea2014-03-31 10:39:59 +02001792 /*
1793 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001794 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001795 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001796 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001797 (is_inet_addr(&newsrv->check.addr) ||
1798 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001799 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001800 struct list *l;
1801
1802 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1803 if (!r) {
1804 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1805 file, linenum, newsrv->id);
1806 err_code |= ERR_ALERT | ERR_FATAL;
1807 goto out;
1808 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001809 /* search the first action (connect / send / expect) in the list */
1810 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001811 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001812 if (r->action != TCPCHK_ACT_COMMENT)
1813 break;
1814 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001815 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1816 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",
1817 file, linenum, newsrv->id);
1818 err_code |= ERR_ALERT | ERR_FATAL;
1819 goto out;
1820 }
1821 else {
1822 /* scan the tcp-check ruleset to ensure a port has been configured */
1823 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001824 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001825 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1826 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",
1827 file, linenum, newsrv->id);
1828 err_code |= ERR_ALERT | ERR_FATAL;
1829 goto out;
1830 }
1831 }
1832 }
1833 }
1834
1835 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001836 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001837 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001838 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1839 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001840 goto out;
1841 }
1842
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001843 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001844 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001845
Willy Tarreau272adea2014-03-31 10:39:59 +02001846 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1847 }
1848
1849 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001850 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001851
1852 if (!newsrv->agent.port) {
1853 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1854 file, linenum, newsrv->id);
1855 err_code |= ERR_ALERT | ERR_FATAL;
1856 goto out;
1857 }
1858
1859 if (!newsrv->agent.inter)
1860 newsrv->agent.inter = newsrv->check.inter;
1861
Simon Hormanb1900d52015-01-30 11:22:54 +09001862 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001863 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001864 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1865 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001866 goto out;
1867 }
1868
1869 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1870 }
1871
1872 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001873 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001874 curproxy->srv_bck++;
1875 else
1876 curproxy->srv_act++;
1877
Willy Tarreauc5150da2014-05-13 19:27:31 +02001878 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001879 }
1880 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001881 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001882 return 0;
1883
1884 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001885 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001886 free(errmsg);
1887 return err_code;
1888}
1889
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001890/* Returns a pointer to the first server matching either id <id>.
1891 * NULL is returned if no match is found.
1892 * the lookup is performed in the backend <bk>
1893 */
1894struct server *server_find_by_id(struct proxy *bk, int id)
1895{
1896 struct eb32_node *eb32;
1897 struct server *curserver;
1898
1899 if (!bk || (id ==0))
1900 return NULL;
1901
1902 /* <bk> has no backend capabilities, so it can't have a server */
1903 if (!(bk->cap & PR_CAP_BE))
1904 return NULL;
1905
1906 curserver = NULL;
1907
1908 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1909 if (eb32)
1910 curserver = container_of(eb32, struct server, conf.id);
1911
1912 return curserver;
1913}
1914
1915/* Returns a pointer to the first server matching either name <name>, or id
1916 * if <name> starts with a '#'. NULL is returned if no match is found.
1917 * the lookup is performed in the backend <bk>
1918 */
1919struct server *server_find_by_name(struct proxy *bk, const char *name)
1920{
1921 struct server *curserver;
1922
1923 if (!bk || !name)
1924 return NULL;
1925
1926 /* <bk> has no backend capabilities, so it can't have a server */
1927 if (!(bk->cap & PR_CAP_BE))
1928 return NULL;
1929
1930 curserver = NULL;
1931 if (*name == '#') {
1932 curserver = server_find_by_id(bk, atoi(name + 1));
1933 if (curserver)
1934 return curserver;
1935 }
1936 else {
1937 curserver = bk->srv;
1938
1939 while (curserver && (strcmp(curserver->id, name) != 0))
1940 curserver = curserver->next;
1941
1942 if (curserver)
1943 return curserver;
1944 }
1945
1946 return NULL;
1947}
1948
1949struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
1950{
1951 struct server *byname;
1952 struct server *byid;
1953
1954 if (!name && !id)
1955 return NULL;
1956
1957 if (diff)
1958 *diff = 0;
1959
1960 byname = byid = NULL;
1961
1962 if (name) {
1963 byname = server_find_by_name(bk, name);
1964 if (byname && (!id || byname->puid == id))
1965 return byname;
1966 }
1967
1968 /* remaining possibilities :
1969 * - name not set
1970 * - name set but not found
1971 * - name found but ID doesn't match
1972 */
1973 if (id) {
1974 byid = server_find_by_id(bk, id);
1975 if (byid) {
1976 if (byname) {
1977 /* use id only if forced by configuration */
1978 if (byid->flags & SRV_F_FORCED_ID) {
1979 if (diff)
1980 *diff |= 2;
1981 return byid;
1982 }
1983 else {
1984 if (diff)
1985 *diff |= 1;
1986 return byname;
1987 }
1988 }
1989
1990 /* remaining possibilities:
1991 * - name not set
1992 * - name set but not found
1993 */
1994 if (name && diff)
1995 *diff |= 2;
1996 return byid;
1997 }
1998
1999 /* id bot found */
2000 if (byname) {
2001 if (diff)
2002 *diff |= 1;
2003 return byname;
2004 }
2005 }
2006
2007 return NULL;
2008}
2009
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002010/* Update a server state using the parameters available in the params list */
2011static void srv_update_state(struct server *srv, int version, char **params)
2012{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002013 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002014 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002015
2016 /* fields since version 1
2017 * and common to all other upcoming versions
2018 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002019 enum srv_state srv_op_state;
2020 enum srv_admin srv_admin_state;
2021 unsigned srv_uweight, srv_iweight;
2022 unsigned long srv_last_time_change;
2023 short srv_check_status;
2024 enum chk_result srv_check_result;
2025 int srv_check_health;
2026 int srv_check_state, srv_agent_state;
2027 int bk_f_forced_id;
2028 int srv_f_forced_id;
2029
Willy Tarreau31138fa2015-09-29 18:38:47 +02002030 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002031 switch (version) {
2032 case 1:
2033 /*
2034 * now we can proceed with server's state update:
2035 * srv_addr: params[0]
2036 * srv_op_state: params[1]
2037 * srv_admin_state: params[2]
2038 * srv_uweight: params[3]
2039 * srv_iweight: params[4]
2040 * srv_last_time_change: params[5]
2041 * srv_check_status: params[6]
2042 * srv_check_result: params[7]
2043 * srv_check_health: params[8]
2044 * srv_check_state: params[9]
2045 * srv_agent_state: params[10]
2046 * bk_f_forced_id: params[11]
2047 * srv_f_forced_id: params[12]
2048 */
2049
2050 /* validating srv_op_state */
2051 p = NULL;
2052 errno = 0;
2053 srv_op_state = strtol(params[1], &p, 10);
2054 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2055 (srv_op_state != SRV_ST_STOPPED &&
2056 srv_op_state != SRV_ST_STARTING &&
2057 srv_op_state != SRV_ST_RUNNING &&
2058 srv_op_state != SRV_ST_STOPPING)) {
2059 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2060 }
2061
2062 /* validating srv_admin_state */
2063 p = NULL;
2064 errno = 0;
2065 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002066
2067 /* inherited statuses will be recomputed later */
2068 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2069
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002070 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2071 (srv_admin_state != 0 &&
2072 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002073 srv_admin_state != SRV_ADMF_CMAINT &&
2074 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002075 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002076 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002077 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2078 }
2079
2080 /* validating srv_uweight */
2081 p = NULL;
2082 errno = 0;
2083 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002084 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002085 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2086
2087 /* validating srv_iweight */
2088 p = NULL;
2089 errno = 0;
2090 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002091 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002092 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2093
2094 /* validating srv_last_time_change */
2095 p = NULL;
2096 errno = 0;
2097 srv_last_time_change = strtol(params[5], &p, 10);
2098 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2099 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2100
2101 /* validating srv_check_status */
2102 p = NULL;
2103 errno = 0;
2104 srv_check_status = strtol(params[6], &p, 10);
2105 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2106 (srv_check_status >= HCHK_STATUS_SIZE))
2107 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2108
2109 /* validating srv_check_result */
2110 p = NULL;
2111 errno = 0;
2112 srv_check_result = strtol(params[7], &p, 10);
2113 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2114 (srv_check_result != CHK_RES_UNKNOWN &&
2115 srv_check_result != CHK_RES_NEUTRAL &&
2116 srv_check_result != CHK_RES_FAILED &&
2117 srv_check_result != CHK_RES_PASSED &&
2118 srv_check_result != CHK_RES_CONDPASS)) {
2119 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2120 }
2121
2122 /* validating srv_check_health */
2123 p = NULL;
2124 errno = 0;
2125 srv_check_health = strtol(params[8], &p, 10);
2126 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2127 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2128
2129 /* validating srv_check_state */
2130 p = NULL;
2131 errno = 0;
2132 srv_check_state = strtol(params[9], &p, 10);
2133 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2134 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2135 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2136
2137 /* validating srv_agent_state */
2138 p = NULL;
2139 errno = 0;
2140 srv_agent_state = strtol(params[10], &p, 10);
2141 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2142 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2143 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2144
2145 /* validating bk_f_forced_id */
2146 p = NULL;
2147 errno = 0;
2148 bk_f_forced_id = strtol(params[11], &p, 10);
2149 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2150 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2151
2152 /* validating srv_f_forced_id */
2153 p = NULL;
2154 errno = 0;
2155 srv_f_forced_id = strtol(params[12], &p, 10);
2156 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2157 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2158
2159
2160 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002161 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002162 goto out;
2163
2164 /* recover operational state and apply it to this server
2165 * and all servers tracking this one */
2166 switch (srv_op_state) {
2167 case SRV_ST_STOPPED:
2168 srv->check.health = 0;
2169 srv_set_stopped(srv, "changed from server-state after a reload");
2170 break;
2171 case SRV_ST_STARTING:
2172 srv->state = srv_op_state;
2173 break;
2174 case SRV_ST_STOPPING:
2175 srv->check.health = srv->check.rise + srv->check.fall - 1;
2176 srv_set_stopping(srv, "changed from server-state after a reload");
2177 break;
2178 case SRV_ST_RUNNING:
2179 srv->check.health = srv->check.rise + srv->check.fall - 1;
2180 srv_set_running(srv, "");
2181 break;
2182 }
2183
2184 /* When applying server state, the following rules apply:
2185 * - in case of a configuration change, we apply the setting from the new
2186 * configuration, regardless of old running state
2187 * - if no configuration change, we apply old running state only if old running
2188 * state is different from new configuration state
2189 */
2190 /* configuration has changed */
2191 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2192 if (srv->admin & SRV_ADMF_CMAINT)
2193 srv_adm_set_maint(srv);
2194 else
2195 srv_adm_set_ready(srv);
2196 }
2197 /* configuration is the same, let's compate old running state and new conf state */
2198 else {
2199 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2200 srv_adm_set_maint(srv);
2201 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2202 srv_adm_set_ready(srv);
2203 }
2204 /* apply drain mode if server is currently enabled */
2205 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2206 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002207 * (srv->iweight is the weight set up in configuration).
2208 * There are two possible reasons for FDRAIN to have been present :
2209 * - previous config weight was zero
2210 * - "set server b/s drain" was sent to the CLI
2211 *
2212 * In the first case, we simply want to drop this drain state
2213 * if the new weight is not zero anymore, meaning the administrator
2214 * has intentionally turned the weight back to a positive value to
2215 * enable the server again after an operation. In the second case,
2216 * the drain state was forced on the CLI regardless of the config's
2217 * weight so we don't want a change to the config weight to lose this
2218 * status. What this means is :
2219 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2220 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002221 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002222 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002223 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002224 }
2225
2226 srv->last_change = date.tv_sec - srv_last_time_change;
2227 srv->check.status = srv_check_status;
2228 srv->check.result = srv_check_result;
2229 srv->check.health = srv_check_health;
2230
2231 /* Only case we want to apply is removing ENABLED flag which could have been
2232 * done by the "disable health" command over the stats socket
2233 */
2234 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2235 (srv_check_state & CHK_ST_CONFIGURED) &&
2236 !(srv_check_state & CHK_ST_ENABLED))
2237 srv->check.state &= ~CHK_ST_ENABLED;
2238
2239 /* Only case we want to apply is removing ENABLED flag which could have been
2240 * done by the "disable agent" command over the stats socket
2241 */
2242 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2243 (srv_agent_state & CHK_ST_CONFIGURED) &&
2244 !(srv_agent_state & CHK_ST_ENABLED))
2245 srv->agent.state &= ~CHK_ST_ENABLED;
2246
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002247 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2248 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002249 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002250 * It means that a configuration file change has precedence over a unix socket change
2251 * for server's weight
2252 *
2253 * by default, HAProxy applies the following weight when parsing the configuration
2254 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002255 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002256 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002257 srv->uweight = srv_uweight;
2258 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002259 server_recalc_eweight(srv);
2260
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002261 /* load server IP only if DNS resolution is used on the server */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002262 if (srv->resolution) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01002263 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002264 }
2265 break;
2266 default:
2267 chunk_appendf(msg, ", version '%d' not supported", version);
2268 }
2269
2270 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002271 if (msg->len) {
2272 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002273 Warning("server-state application failed for server '%s/%s'%s",
2274 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002275 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002276}
2277
2278/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2279 * For each proxy, it does the following:
2280 * - opens its server state file (either one or local one)
2281 * - read whole file, line by line
2282 * - analyse each line to check if it matches our current backend:
2283 * - backend name matches
2284 * - backend id matches if id is forced and name doesn't match
2285 * - if the server pointed by the line is found, then state is applied
2286 *
2287 * If the running backend uuid or id differs from the state file, then HAProxy reports
2288 * a warning.
2289 */
2290void apply_server_state(void)
2291{
2292 char *cur, *end;
2293 char mybuf[SRV_STATE_LINE_MAXLEN];
2294 int mybuflen;
2295 char *params[SRV_STATE_FILE_MAX_FIELDS];
2296 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2297 int arg, srv_arg, version, diff;
2298 FILE *f;
2299 char *filepath;
2300 char globalfilepath[MAXPATHLEN + 1];
2301 char localfilepath[MAXPATHLEN + 1];
2302 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2303 extern struct proxy *proxy;
2304 struct proxy *curproxy, *bk;
2305 struct server *srv;
2306
2307 globalfilepathlen = 0;
2308 /* create the globalfilepath variable */
2309 if (global.server_state_file) {
2310 /* absolute path or no base directory provided */
2311 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2312 len = strlen(global.server_state_file);
2313 if (len > MAXPATHLEN) {
2314 globalfilepathlen = 0;
2315 goto globalfileerror;
2316 }
2317 memcpy(globalfilepath, global.server_state_file, len);
2318 globalfilepath[len] = '\0';
2319 globalfilepathlen = len;
2320 }
2321 else if (global.server_state_base) {
2322 len = strlen(global.server_state_base);
2323 globalfilepathlen += len;
2324
2325 if (globalfilepathlen > MAXPATHLEN) {
2326 globalfilepathlen = 0;
2327 goto globalfileerror;
2328 }
2329 strncpy(globalfilepath, global.server_state_base, len);
2330 globalfilepath[globalfilepathlen] = 0;
2331
2332 /* append a slash if needed */
2333 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2334 if (globalfilepathlen + 1 > MAXPATHLEN) {
2335 globalfilepathlen = 0;
2336 goto globalfileerror;
2337 }
2338 globalfilepath[globalfilepathlen++] = '/';
2339 }
2340
2341 len = strlen(global.server_state_file);
2342 if (globalfilepathlen + len > MAXPATHLEN) {
2343 globalfilepathlen = 0;
2344 goto globalfileerror;
2345 }
2346 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2347 globalfilepathlen += len;
2348 globalfilepath[globalfilepathlen++] = 0;
2349 }
2350 }
2351 globalfileerror:
2352 if (globalfilepathlen == 0)
2353 globalfilepath[0] = '\0';
2354
2355 /* read servers state from local file */
2356 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2357 /* servers are only in backends */
2358 if (!(curproxy->cap & PR_CAP_BE))
2359 continue;
2360 fileopenerr = 0;
2361 filepath = NULL;
2362
2363 /* search server state file path and name */
2364 switch (curproxy->load_server_state_from_file) {
2365 /* read servers state from global file */
2366 case PR_SRV_STATE_FILE_GLOBAL:
2367 /* there was an error while generating global server state file path */
2368 if (globalfilepathlen == 0)
2369 continue;
2370 filepath = globalfilepath;
2371 fileopenerr = 1;
2372 break;
2373 /* this backend has its own file */
2374 case PR_SRV_STATE_FILE_LOCAL:
2375 localfilepathlen = 0;
2376 localfilepath[0] = '\0';
2377 len = 0;
2378 /* create the localfilepath variable */
2379 /* absolute path or no base directory provided */
2380 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2381 len = strlen(curproxy->server_state_file_name);
2382 if (len > MAXPATHLEN) {
2383 localfilepathlen = 0;
2384 goto localfileerror;
2385 }
2386 memcpy(localfilepath, curproxy->server_state_file_name, len);
2387 localfilepath[len] = '\0';
2388 localfilepathlen = len;
2389 }
2390 else if (global.server_state_base) {
2391 len = strlen(global.server_state_base);
2392 localfilepathlen += len;
2393
2394 if (localfilepathlen > MAXPATHLEN) {
2395 localfilepathlen = 0;
2396 goto localfileerror;
2397 }
2398 strncpy(localfilepath, global.server_state_base, len);
2399 localfilepath[localfilepathlen] = 0;
2400
2401 /* append a slash if needed */
2402 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2403 if (localfilepathlen + 1 > MAXPATHLEN) {
2404 localfilepathlen = 0;
2405 goto localfileerror;
2406 }
2407 localfilepath[localfilepathlen++] = '/';
2408 }
2409
2410 len = strlen(curproxy->server_state_file_name);
2411 if (localfilepathlen + len > MAXPATHLEN) {
2412 localfilepathlen = 0;
2413 goto localfileerror;
2414 }
2415 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2416 localfilepathlen += len;
2417 localfilepath[localfilepathlen++] = 0;
2418 }
2419 filepath = localfilepath;
2420 localfileerror:
2421 if (localfilepathlen == 0)
2422 localfilepath[0] = '\0';
2423
2424 break;
2425 case PR_SRV_STATE_FILE_NONE:
2426 default:
2427 continue;
2428 }
2429
2430 /* preload global state file */
2431 errno = 0;
2432 f = fopen(filepath, "r");
2433 if (errno && fileopenerr)
2434 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2435 if (!f)
2436 continue;
2437
2438 mybuf[0] = '\0';
2439 mybuflen = 0;
2440 version = 0;
2441
2442 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002443 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2444 Warning("Can't read first line of the server state file '%s'\n", filepath);
2445 goto fileclose;
2446 }
2447
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002448 cur = mybuf;
2449 version = atoi(cur);
2450 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2451 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002452 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002453
2454 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2455 int bk_f_forced_id = 0;
2456 int check_id = 0;
2457 int check_name = 0;
2458
2459 mybuflen = strlen(mybuf);
2460 cur = mybuf;
2461 end = cur + mybuflen;
2462
2463 bk = NULL;
2464 srv = NULL;
2465
2466 /* we need at least one character */
2467 if (mybuflen == 0)
2468 continue;
2469
2470 /* ignore blank characters at the beginning of the line */
2471 while (isspace(*cur))
2472 ++cur;
2473
2474 if (cur == end)
2475 continue;
2476
2477 /* ignore comment line */
2478 if (*cur == '#')
2479 continue;
2480
2481 /* we're now ready to move the line into *srv_params[] */
2482 params[0] = cur;
2483 arg = 1;
2484 srv_arg = 0;
2485 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2486 if (isspace(*cur)) {
2487 *cur = '\0';
2488 ++cur;
2489 while (isspace(*cur))
2490 ++cur;
2491 switch (version) {
2492 case 1:
2493 /*
2494 * srv_addr: params[4] => srv_params[0]
2495 * srv_op_state: params[5] => srv_params[1]
2496 * srv_admin_state: params[6] => srv_params[2]
2497 * srv_uweight: params[7] => srv_params[3]
2498 * srv_iweight: params[8] => srv_params[4]
2499 * srv_last_time_change: params[9] => srv_params[5]
2500 * srv_check_status: params[10] => srv_params[6]
2501 * srv_check_result: params[11] => srv_params[7]
2502 * srv_check_health: params[12] => srv_params[8]
2503 * srv_check_state: params[13] => srv_params[9]
2504 * srv_agent_state: params[14] => srv_params[10]
2505 * bk_f_forced_id: params[15] => srv_params[11]
2506 * srv_f_forced_id: params[16] => srv_params[12]
2507 */
2508 if (arg >= 4) {
2509 srv_params[srv_arg] = cur;
2510 ++srv_arg;
2511 }
2512 break;
2513 }
2514
2515 params[arg] = cur;
2516 ++arg;
2517 }
2518 else {
2519 ++cur;
2520 }
2521 }
2522
2523 /* if line is incomplete line, then ignore it.
2524 * otherwise, update useful flags */
2525 switch (version) {
2526 case 1:
2527 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2528 continue;
2529 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2530 check_id = (atoi(params[0]) == curproxy->uuid);
2531 check_name = (strcmp(curproxy->id, params[1]) == 0);
2532 break;
2533 }
2534
2535 diff = 0;
2536 bk = curproxy;
2537
2538 /* if backend can't be found, let's continue */
2539 if (!check_id && !check_name)
2540 continue;
2541 else if (!check_id && check_name) {
2542 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2543 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2544 }
2545 else if (check_id && !check_name) {
2546 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2547 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2548 /* if name doesn't match, we still want to update curproxy if the backend id
2549 * was forced in previous the previous configuration */
2550 if (!bk_f_forced_id)
2551 continue;
2552 }
2553
2554 /* look for the server by its id: param[2] */
2555 /* else look for the server by its name: param[3] */
2556 diff = 0;
2557 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2558
2559 if (!srv) {
2560 /* if no server found, then warning and continue with next line */
2561 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2562 params[3], params[2], params[0], params[1]);
2563 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2564 params[3], params[2], params[0], params[1]);
2565 continue;
2566 }
2567 else if (diff & PR_FBM_MISMATCH_ID) {
2568 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);
2569 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);
2570 }
2571 else if (diff & PR_FBM_MISMATCH_NAME) {
2572 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);
2573 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);
2574 }
2575
2576 /* now we can proceed with server's state update */
2577 srv_update_state(srv, version, srv_params);
2578 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002579fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002580 fclose(f);
2581 }
2582}
2583
Simon Horman7d09b9a2013-02-12 10:45:51 +09002584/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002585 * update a server's current IP address.
2586 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2587 * ip is in network format.
2588 * updater is a string which contains an information about the requester of the update.
2589 * updater is used if not NULL.
2590 *
2591 * A log line and a stderr warning message is generated based on server's backend options.
2592 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002593int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002594{
2595 /* generates a log line and a warning on stderr */
2596 if (1) {
2597 /* book enough space for both IPv4 and IPv6 */
2598 char oldip[INET6_ADDRSTRLEN];
2599 char newip[INET6_ADDRSTRLEN];
2600
2601 memset(oldip, '\0', INET6_ADDRSTRLEN);
2602 memset(newip, '\0', INET6_ADDRSTRLEN);
2603
2604 /* copy old IP address in a string */
2605 switch (s->addr.ss_family) {
2606 case AF_INET:
2607 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2608 break;
2609 case AF_INET6:
2610 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2611 break;
2612 };
2613
2614 /* copy new IP address in a string */
2615 switch (ip_sin_family) {
2616 case AF_INET:
2617 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2618 break;
2619 case AF_INET6:
2620 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2621 break;
2622 };
2623
2624 /* save log line into a buffer */
2625 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2626 s->proxy->id, s->id, oldip, newip, updater);
2627
2628 /* write the buffer on stderr */
2629 Warning("%s.\n", trash.str);
2630
2631 /* send a log */
2632 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2633 }
2634
2635 /* save the new IP family */
2636 s->addr.ss_family = ip_sin_family;
2637 /* save the new IP address */
2638 switch (ip_sin_family) {
2639 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002640 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002641 break;
2642 case AF_INET6:
2643 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2644 break;
2645 };
2646
2647 return 0;
2648}
2649
2650/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002651 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2652 *
2653 * Caller can pass its name through <updater> to get it integrated in the response message
2654 * returned by the function.
2655 *
2656 * The function first does the following, in that order:
2657 * - validates the new addr and/or port
2658 * - checks if an update is required (new IP or port is different than current ones)
2659 * - checks the update is allowed:
2660 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2661 * - allow all changes if no CHECKS are configured
2662 * - if CHECK is configured:
2663 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2664 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2665 * conditions are met
2666 */
2667const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2668{
2669 struct sockaddr_storage sa;
2670 int ret, port_change_required;
2671 char current_addr[INET6_ADDRSTRLEN];
2672 u_int16_t current_port, new_port;
2673 struct chunk *msg;
2674
2675 msg = get_trash_chunk();
2676 chunk_reset(msg);
2677
2678 if (addr) {
2679 memset(&sa, 0, sizeof(struct sockaddr_storage));
2680 if (str2ip2(addr, &sa, 0) == NULL) {
2681 chunk_printf(msg, "Invalid addr '%s'", addr);
2682 goto out;
2683 }
2684
2685 /* changes are allowed on AF_INET* families only */
2686 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2687 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2688 goto out;
2689 }
2690
2691 /* collecting data currently setup */
2692 memset(current_addr, '\0', sizeof(current_addr));
2693 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2694 /* changes are allowed on AF_INET* families only */
2695 if ((ret != AF_INET) && (ret != AF_INET6)) {
2696 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2697 goto out;
2698 }
2699
2700 /* applying ADDR changes if required and allowed
2701 * ipcmp returns 0 when both ADDR are the same
2702 */
2703 if (ipcmp(&s->addr, &sa) == 0) {
2704 chunk_appendf(msg, "no need to change the addr");
2705 goto port;
2706 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002707 ipcpy(&sa, &s->addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002708
2709 /* we also need to update check's ADDR only if it uses the server's one */
2710 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002711 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002712 }
2713
2714 /* we also need to update agent ADDR only if it use the server's one */
2715 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002716 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002717 }
2718
2719 /* update report for caller */
2720 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2721 }
2722
2723 port:
2724 if (port) {
2725 char sign = '\0';
2726 char *endptr;
2727
2728 if (addr)
2729 chunk_appendf(msg, ", ");
2730
2731 /* collecting data currently setup */
2732 current_port = get_host_port(&s->addr);
2733
2734 /* check if PORT change is required */
2735 port_change_required = 0;
2736
2737 sign = *port;
2738 new_port = strtol(port, &endptr, 10);
2739 if ((errno != 0) || (port == endptr)) {
2740 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2741 goto out;
2742 }
2743
2744 /* check if caller triggers a port mapped or offset */
2745 if (sign == '-' || (sign == '+')) {
2746 /* check if server currently uses port map */
2747 if (!(s->flags & SRV_F_MAPPORTS)) {
2748 /* switch from fixed port to port map mandatorily triggers
2749 * a port change */
2750 port_change_required = 1;
2751 /* check is configured
2752 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2753 * prevent PORT change if check doesn't have it's dedicated port while switching
2754 * to port mapping */
2755 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2756 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.");
2757 goto out;
2758 }
2759 }
2760 /* we're already using port maps */
2761 else {
2762 port_change_required = current_port != new_port;
2763 }
2764 }
2765 /* fixed port */
2766 else {
2767 port_change_required = current_port != new_port;
2768 }
2769
2770 /* applying PORT changes if required and update response message */
2771 if (port_change_required) {
2772 /* apply new port */
2773 set_host_port(&s->addr, new_port);
2774
2775 /* prepare message */
2776 chunk_appendf(msg, "port changed from '");
2777 if (s->flags & SRV_F_MAPPORTS)
2778 chunk_appendf(msg, "+");
2779 chunk_appendf(msg, "%d' to '", current_port);
2780
2781 if (sign == '-') {
2782 s->flags |= SRV_F_MAPPORTS;
2783 chunk_appendf(msg, "%c", sign);
2784 /* just use for result output */
2785 new_port = -new_port;
2786 }
2787 else if (sign == '+') {
2788 s->flags |= SRV_F_MAPPORTS;
2789 chunk_appendf(msg, "%c", sign);
2790 }
2791 else {
2792 s->flags &= ~SRV_F_MAPPORTS;
2793 }
2794
2795 chunk_appendf(msg, "%d'", new_port);
2796
2797 /* we also need to update health checks port only if it uses server's realport */
2798 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2799 s->check.port = new_port;
2800 }
2801 }
2802 else {
2803 chunk_appendf(msg, "no need to change the port");
2804 }
2805 }
2806
2807out:
2808 if (updater)
2809 chunk_appendf(msg, " by '%s'", updater);
2810 chunk_appendf(msg, "\n");
2811 return msg->str;
2812}
2813
2814
2815/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002816 * update server status based on result of name resolution
2817 * returns:
2818 * 0 if server status is updated
2819 * 1 if server status has not changed
2820 */
2821int snr_update_srv_status(struct server *s)
2822{
2823 struct dns_resolution *resolution = s->resolution;
2824
2825 switch (resolution->status) {
2826 case RSLV_STATUS_NONE:
2827 /* status when HAProxy has just (re)started */
2828 trigger_resolution(s);
2829 break;
2830
2831 default:
2832 break;
2833 }
2834
2835 return 1;
2836}
2837
2838/*
2839 * Server Name Resolution valid response callback
2840 * It expects:
2841 * - <nameserver>: the name server which answered the valid response
2842 * - <response>: buffer containing a valid DNS response
2843 * - <response_len>: size of <response>
2844 * It performs the following actions:
2845 * - ignore response if current ip found and server family not met
2846 * - update with first new ip found if family is met and current IP is not found
2847 * returns:
2848 * 0 on error
2849 * 1 when no error or safe ignore
2850 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002851int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002852{
2853 struct server *s;
2854 void *serverip, *firstip;
2855 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002856 int ret;
2857 struct chunk *chk = get_trash_chunk();
2858
2859 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002860 firstip = NULL; /* pointer to the first valid response found */
2861 /* it will be used as the new IP if a change is required */
2862 firstip_sin_family = AF_UNSPEC;
2863 serverip = NULL; /* current server IP address */
2864
2865 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002866 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002867
2868 /* initializing server IP pointer */
2869 server_sin_family = s->addr.ss_family;
2870 switch (server_sin_family) {
2871 case AF_INET:
2872 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
2873 break;
2874
2875 case AF_INET6:
2876 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
2877 break;
2878
2879 default:
2880 goto invalid;
2881 }
2882
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002883 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01002884 serverip, server_sin_family, &firstip,
2885 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002886
2887 switch (ret) {
2888 case DNS_UPD_NO:
2889 if (resolution->status != RSLV_STATUS_VALID) {
2890 resolution->status = RSLV_STATUS_VALID;
2891 resolution->last_status_change = now_ms;
2892 }
2893 goto stop_resolution;
2894
2895 case DNS_UPD_SRVIP_NOT_FOUND:
2896 goto save_ip;
2897
2898 case DNS_UPD_CNAME:
2899 if (resolution->status != RSLV_STATUS_VALID) {
2900 resolution->status = RSLV_STATUS_VALID;
2901 resolution->last_status_change = now_ms;
2902 }
2903 goto invalid;
2904
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02002905 case DNS_UPD_NO_IP_FOUND:
2906 if (resolution->status != RSLV_STATUS_OTHER) {
2907 resolution->status = RSLV_STATUS_OTHER;
2908 resolution->last_status_change = now_ms;
2909 }
2910 goto stop_resolution;
2911
Baptiste Assmannfad03182015-10-28 02:03:32 +01002912 case DNS_UPD_NAME_ERROR:
2913 /* if this is not the last expected response, we ignore it */
2914 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
2915 return 0;
2916 /* update resolution status to OTHER error type */
2917 if (resolution->status != RSLV_STATUS_OTHER) {
2918 resolution->status = RSLV_STATUS_OTHER;
2919 resolution->last_status_change = now_ms;
2920 }
2921 goto stop_resolution;
2922
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002923 default:
2924 goto invalid;
2925
2926 }
2927
2928 save_ip:
2929 nameserver->counters.update += 1;
2930 if (resolution->status != RSLV_STATUS_VALID) {
2931 resolution->status = RSLV_STATUS_VALID;
2932 resolution->last_status_change = now_ms;
2933 }
2934
2935 /* save the first ip we found */
2936 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
2937 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
2938
2939 stop_resolution:
2940 /* update last resolution date and time */
2941 resolution->last_resolution = now_ms;
2942 /* reset current status flag */
2943 resolution->step = RSLV_STEP_NONE;
2944 /* reset values */
2945 dns_reset_resolution(resolution);
2946
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002947 dns_update_resolvers_timeout(nameserver->resolvers);
2948
2949 snr_update_srv_status(s);
2950 return 0;
2951
2952 invalid:
2953 nameserver->counters.invalid += 1;
2954 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
2955 goto stop_resolution;
2956
2957 snr_update_srv_status(s);
2958 return 0;
2959}
2960
2961/*
2962 * Server Name Resolution error management callback
2963 * returns:
2964 * 0 on error
2965 * 1 when no error or safe ignore
2966 */
2967int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
2968{
2969 struct server *s;
2970 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002971 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002972
2973 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002974 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002975 resolvers = resolution->resolvers;
2976
2977 /* can be ignored if this is not the last response */
2978 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
2979 return 1;
2980 }
2981
2982 switch (error_code) {
2983 case DNS_RESP_INVALID:
2984 case DNS_RESP_WRONG_NAME:
2985 if (resolution->status != RSLV_STATUS_INVALID) {
2986 resolution->status = RSLV_STATUS_INVALID;
2987 resolution->last_status_change = now_ms;
2988 }
2989 break;
2990
2991 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02002992 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002993 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02002994 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02002995 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01002996 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
2997 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02002998
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002999 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003000 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003001 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003002 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003003 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003004 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003005 }
3006 else if (res_preferred_afinet) {
3007 /* fallback from A to AAAA */
3008 resolution->query_type = DNS_RTYPE_AAAA;
3009 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003010 else {
3011 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003012 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003013 resolution->query_type = DNS_RTYPE_A;
3014 } else {
3015 resolution->query_type = DNS_RTYPE_AAAA;
3016 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003017 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003018
3019 dns_send_query(resolution);
3020
3021 /*
3022 * move the resolution to the last element of the FIFO queue
3023 * and update timeout wakeup based on the new first entry
3024 */
3025 if (dns_check_resolution_queue(resolvers) > 1) {
3026 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003027 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003028 /* ex first resolution goes to the end of the queue */
3029 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3030 }
3031 dns_update_resolvers_timeout(resolvers);
3032 goto leave;
3033 }
3034 else {
3035 if (resolution->status != RSLV_STATUS_OTHER) {
3036 resolution->status = RSLV_STATUS_OTHER;
3037 resolution->last_status_change = now_ms;
3038 }
3039 }
3040 break;
3041
3042 case DNS_RESP_NX_DOMAIN:
3043 if (resolution->status != RSLV_STATUS_NX) {
3044 resolution->status = RSLV_STATUS_NX;
3045 resolution->last_status_change = now_ms;
3046 }
3047 break;
3048
3049 case DNS_RESP_REFUSED:
3050 if (resolution->status != RSLV_STATUS_REFUSED) {
3051 resolution->status = RSLV_STATUS_REFUSED;
3052 resolution->last_status_change = now_ms;
3053 }
3054 break;
3055
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003056 case DNS_RESP_TIMEOUT:
3057 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3058 resolution->status = RSLV_STATUS_TIMEOUT;
3059 resolution->last_status_change = now_ms;
3060 }
3061 break;
3062 }
3063
3064 /* update last resolution date and time */
3065 resolution->last_resolution = now_ms;
3066 /* reset current status flag */
3067 resolution->step = RSLV_STEP_NONE;
3068 /* reset values */
3069 dns_reset_resolution(resolution);
3070
3071 LIST_DEL(&resolution->list);
3072 dns_update_resolvers_timeout(resolvers);
3073
3074 leave:
3075 snr_update_srv_status(s);
3076 return 1;
3077}
3078
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003079/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3080 * resolver. This is suited for initial address configuration. Returns 0 on
3081 * success otherwise a non-zero error code. In case of error, *err_code, if
3082 * not NULL, is filled up.
3083 */
3084int srv_set_addr_via_libc(struct server *srv, int *err_code)
3085{
3086 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
3087 Alert("parsing [%s:%d] : 'server %s' : invalid address: '%s'\n",
3088 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3089 if (err_code)
3090 *err_code |= ERR_ALERT | ERR_FATAL;
3091 return 1;
3092 }
3093 return 0;
3094}
3095
3096/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3097 * from the state file. This is suited for initial address configuration.
3098 * Returns 0 on success otherwise a non-zero error code. In case of error,
3099 * *err_code, if not NULL, is filled up.
3100 */
3101static int srv_apply_lastaddr(struct server *srv, int *err_code)
3102{
3103 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3104 if (err_code)
3105 *err_code |= ERR_WARN;
3106 return 1;
3107 }
3108 return 0;
3109}
3110
3111/*
3112 * This function parses all backends and all servers within each backend
3113 * and performs servers' addr resolution based on information provided by:
3114 * - configuration file
3115 * - server-state file (states provided by an 'old' haproxy process)
3116 *
3117 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3118 */
3119int srv_init_addr(void)
3120{
3121 struct proxy *curproxy;
3122 int return_code = 0;
3123
3124 curproxy = proxy;
3125 while (curproxy) {
3126 struct server *srv;
3127 int err_code = 0;
3128
3129 /* servers are in backend only */
3130 if (!(curproxy->cap & PR_CAP_BE))
3131 goto srv_init_addr_next;
3132
3133 for (srv = curproxy->srv; srv; srv = srv->next) {
3134 err_code = 0;
3135
3136 if (srv->lastaddr) {
3137 if (srv_apply_lastaddr(srv, &err_code) == 0)
3138 continue;
3139 return_code |= err_code;
3140 }
3141
3142 if (srv->hostname) {
3143 if (srv_set_addr_via_libc(srv, &err_code) == 0)
3144 continue;
3145 return_code = err_code;
3146 }
3147 }
3148
3149 srv_init_addr_next:
3150 curproxy = curproxy->next;
3151 }
3152
3153 return return_code;
3154}
3155
3156
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003157/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003158 * Local variables:
3159 * c-indent-level: 8
3160 * c-basic-offset: 8
3161 * End:
3162 */