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