blob: 5da95c999495563dca9c76530541cfada566c970 [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;
Baptiste Assmann25938272016-09-21 20:26:16 +02001116 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1117 newsrv->init_addr = curproxy->defsrv.init_addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001118
1119 cur_arg = 3;
1120 } else {
1121 newsrv = &curproxy->defsrv;
1122 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001123 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001124 }
1125
1126 while (*args[cur_arg]) {
1127 if (!strcmp(args[cur_arg], "agent-check")) {
1128 global.maxsock++;
1129 do_agent = 1;
1130 cur_arg += 1;
1131 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1132 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1133 if (err) {
1134 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1135 file, linenum, *err, newsrv->id);
1136 err_code |= ERR_ALERT | ERR_FATAL;
1137 goto out;
1138 }
1139 if (val <= 0) {
1140 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1141 file, linenum, val, args[cur_arg], newsrv->id);
1142 err_code |= ERR_ALERT | ERR_FATAL;
1143 goto out;
1144 }
1145 newsrv->agent.inter = val;
1146 cur_arg += 2;
1147 }
1148 else if (!strcmp(args[cur_arg], "agent-port")) {
1149 global.maxsock++;
1150 newsrv->agent.port = atol(args[cur_arg + 1]);
1151 cur_arg += 2;
1152 }
James Brown55f9ff12015-10-21 18:19:05 -07001153 else if (!strcmp(args[cur_arg], "agent-send")) {
1154 global.maxsock++;
1155 free(newsrv->agent.send_string);
1156 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1157 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1158 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1159 cur_arg += 2;
1160 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001161 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1162 newsrv->cookie = strdup(args[cur_arg + 1]);
1163 newsrv->cklen = strlen(args[cur_arg + 1]);
1164 cur_arg += 2;
1165 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001166 else if (!strcmp(args[cur_arg], "init-addr")) {
1167 char *p, *end;
1168 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001169 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001170
1171 newsrv->init_addr_methods = 0;
1172 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1173
1174 for (p = args[cur_arg + 1]; *p; p = end) {
1175 /* cut on next comma */
1176 for (end = p; *end && *end != ','; end++);
1177 if (*end)
1178 *(end++) = 0;
1179
Willy Tarreau4310d362016-11-02 15:05:56 +01001180 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001181 if (!strcmp(p, "libc")) {
1182 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1183 }
1184 else if (!strcmp(p, "last")) {
1185 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1186 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001187 else if (!strcmp(p, "none")) {
1188 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1189 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001190 else if (str2ip2(p, &sa, 0)) {
1191 if (is_addr(&newsrv->init_addr)) {
1192 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1193 file, linenum, args[cur_arg], p);
1194 err_code |= ERR_ALERT | ERR_FATAL;
1195 goto out;
1196 }
1197 newsrv->init_addr = sa;
1198 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1199 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001200 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001201 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001202 file, linenum, args[cur_arg], p);
1203 err_code |= ERR_ALERT | ERR_FATAL;
1204 goto out;
1205 }
1206 if (!done) {
1207 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1208 file, linenum, args[cur_arg], p);
1209 err_code |= ERR_ALERT | ERR_FATAL;
1210 goto out;
1211 }
1212 }
1213 cur_arg += 2;
1214 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001215 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1216 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1217 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1218 cur_arg += 2;
1219 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001220 else if (!strcmp(args[cur_arg], "resolvers")) {
1221 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1222 cur_arg += 2;
1223 }
1224 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1225 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001226 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001227 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001228 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001229 else {
1230 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1231 file, linenum, args[cur_arg]);
1232 err_code |= ERR_ALERT | ERR_FATAL;
1233 goto out;
1234 }
1235 cur_arg += 2;
1236 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001237 else if (!strcmp(args[cur_arg], "resolve-net")) {
1238 char *p, *e;
1239 unsigned char mask;
1240 struct dns_options *opt;
1241
1242 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1243 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1244 file, linenum, args[cur_arg]);
1245 err_code |= ERR_ALERT | ERR_FATAL;
1246 goto out;
1247 }
1248
1249 opt = &newsrv->dns_opts;
1250
1251 /* Split arguments by comma, and convert it from ipv4 or ipv6
1252 * string network in in_addr or in6_addr.
1253 */
1254 p = args[cur_arg + 1];
1255 e = p;
1256 while (*p != '\0') {
1257 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001258 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001259 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1260 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1261 err_code |= ERR_ALERT | ERR_FATAL;
1262 goto out;
1263 }
1264 /* look for end or comma. */
1265 while (*e != ',' && *e != '\0')
1266 e++;
1267 if (*e == ',') {
1268 *e = '\0';
1269 e++;
1270 }
1271 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1272 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1273 /* Try to convert input string from ipv4 or ipv6 network. */
1274 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1275 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1276 &mask)) {
1277 /* Try to convert input string from ipv6 network. */
1278 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1279 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1280 } else {
1281 /* All network conversions fail, retrun error. */
1282 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1283 file, linenum, args[cur_arg], p);
1284 err_code |= ERR_ALERT | ERR_FATAL;
1285 goto out;
1286 }
1287 opt->pref_net_nb++;
1288 p = e;
1289 }
1290
1291 cur_arg += 2;
1292 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001293 else if (!strcmp(args[cur_arg], "rise")) {
1294 if (!*args[cur_arg + 1]) {
1295 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1296 file, linenum, args[cur_arg]);
1297 err_code |= ERR_ALERT | ERR_FATAL;
1298 goto out;
1299 }
1300
1301 newsrv->check.rise = atol(args[cur_arg + 1]);
1302 if (newsrv->check.rise <= 0) {
1303 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1304 file, linenum, args[cur_arg]);
1305 err_code |= ERR_ALERT | ERR_FATAL;
1306 goto out;
1307 }
1308
1309 if (newsrv->check.health)
1310 newsrv->check.health = newsrv->check.rise;
1311 cur_arg += 2;
1312 }
1313 else if (!strcmp(args[cur_arg], "fall")) {
1314 newsrv->check.fall = atol(args[cur_arg + 1]);
1315
1316 if (!*args[cur_arg + 1]) {
1317 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1318 file, linenum, args[cur_arg]);
1319 err_code |= ERR_ALERT | ERR_FATAL;
1320 goto out;
1321 }
1322
1323 if (newsrv->check.fall <= 0) {
1324 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1325 file, linenum, args[cur_arg]);
1326 err_code |= ERR_ALERT | ERR_FATAL;
1327 goto out;
1328 }
1329
1330 cur_arg += 2;
1331 }
1332 else if (!strcmp(args[cur_arg], "inter")) {
1333 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1334 if (err) {
1335 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1336 file, linenum, *err, newsrv->id);
1337 err_code |= ERR_ALERT | ERR_FATAL;
1338 goto out;
1339 }
1340 if (val <= 0) {
1341 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1342 file, linenum, val, args[cur_arg], newsrv->id);
1343 err_code |= ERR_ALERT | ERR_FATAL;
1344 goto out;
1345 }
1346 newsrv->check.inter = val;
1347 cur_arg += 2;
1348 }
1349 else if (!strcmp(args[cur_arg], "fastinter")) {
1350 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1351 if (err) {
1352 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1353 file, linenum, *err, newsrv->id);
1354 err_code |= ERR_ALERT | ERR_FATAL;
1355 goto out;
1356 }
1357 if (val <= 0) {
1358 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1359 file, linenum, val, args[cur_arg], newsrv->id);
1360 err_code |= ERR_ALERT | ERR_FATAL;
1361 goto out;
1362 }
1363 newsrv->check.fastinter = val;
1364 cur_arg += 2;
1365 }
1366 else if (!strcmp(args[cur_arg], "downinter")) {
1367 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1368 if (err) {
1369 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1370 file, linenum, *err, newsrv->id);
1371 err_code |= ERR_ALERT | ERR_FATAL;
1372 goto out;
1373 }
1374 if (val <= 0) {
1375 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1376 file, linenum, val, args[cur_arg], newsrv->id);
1377 err_code |= ERR_ALERT | ERR_FATAL;
1378 goto out;
1379 }
1380 newsrv->check.downinter = val;
1381 cur_arg += 2;
1382 }
1383 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1384 struct sockaddr_storage *sk;
1385 int port1, port2;
1386 struct protocol *proto;
1387
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001388 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001389 if (!sk) {
1390 Alert("parsing [%s:%d] : '%s' : %s\n",
1391 file, linenum, args[cur_arg], errmsg);
1392 err_code |= ERR_ALERT | ERR_FATAL;
1393 goto out;
1394 }
1395
1396 proto = protocol_by_family(sk->ss_family);
1397 if (!proto || !proto->connect) {
1398 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1399 file, linenum, args[cur_arg], args[cur_arg + 1]);
1400 err_code |= ERR_ALERT | ERR_FATAL;
1401 goto out;
1402 }
1403
1404 if (port1 != port2) {
1405 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1406 file, linenum, args[cur_arg], args[cur_arg + 1]);
1407 err_code |= ERR_ALERT | ERR_FATAL;
1408 goto out;
1409 }
1410
Simon Horman41f58762015-01-30 11:22:56 +09001411 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001412 newsrv->flags |= SRV_F_CHECKADDR;
1413 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001414 cur_arg += 2;
1415 }
1416 else if (!strcmp(args[cur_arg], "port")) {
1417 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001418 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001419 cur_arg += 2;
1420 }
1421 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001422 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001423 cur_arg ++;
1424 }
1425 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001426 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001427 cur_arg ++;
1428 }
1429 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001430 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001431 cur_arg ++;
1432 }
David Safb76832014-05-08 23:42:08 -04001433 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1434 newsrv->pp_opts |= SRV_PP_V2;
1435 cur_arg ++;
1436 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001437 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1438 newsrv->check.send_proxy = 1;
1439 cur_arg ++;
1440 }
1441 else if (!strcmp(args[cur_arg], "weight")) {
1442 int w;
1443 w = atol(args[cur_arg + 1]);
1444 if (w < 0 || w > SRV_UWGHT_MAX) {
1445 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1446 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1447 err_code |= ERR_ALERT | ERR_FATAL;
1448 goto out;
1449 }
1450 newsrv->uweight = newsrv->iweight = w;
1451 cur_arg += 2;
1452 }
1453 else if (!strcmp(args[cur_arg], "minconn")) {
1454 newsrv->minconn = atol(args[cur_arg + 1]);
1455 cur_arg += 2;
1456 }
1457 else if (!strcmp(args[cur_arg], "maxconn")) {
1458 newsrv->maxconn = atol(args[cur_arg + 1]);
1459 cur_arg += 2;
1460 }
1461 else if (!strcmp(args[cur_arg], "maxqueue")) {
1462 newsrv->maxqueue = atol(args[cur_arg + 1]);
1463 cur_arg += 2;
1464 }
1465 else if (!strcmp(args[cur_arg], "slowstart")) {
1466 /* slowstart is stored in seconds */
1467 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1468 if (err) {
1469 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1470 file, linenum, *err, newsrv->id);
1471 err_code |= ERR_ALERT | ERR_FATAL;
1472 goto out;
1473 }
1474 newsrv->slowstart = (val + 999) / 1000;
1475 cur_arg += 2;
1476 }
1477 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1478
1479 if (!*args[cur_arg + 1]) {
1480 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1481 file, linenum);
1482 err_code |= ERR_ALERT | ERR_FATAL;
1483 goto out;
1484 }
1485
1486 newsrv->trackit = strdup(args[cur_arg + 1]);
1487
1488 cur_arg += 2;
1489 }
1490 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1491 global.maxsock++;
1492 do_check = 1;
1493 cur_arg += 1;
1494 }
1495 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001496 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001497 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001498 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001499 newsrv->check.state |= CHK_ST_PAUSED;
1500 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001501 cur_arg += 1;
1502 }
1503 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1504 if (!strcmp(args[cur_arg + 1], "none"))
1505 newsrv->observe = HANA_OBS_NONE;
1506 else if (!strcmp(args[cur_arg + 1], "layer4"))
1507 newsrv->observe = HANA_OBS_LAYER4;
1508 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1509 if (curproxy->mode != PR_MODE_HTTP) {
1510 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1511 file, linenum, args[cur_arg + 1]);
1512 err_code |= ERR_ALERT;
1513 }
1514 newsrv->observe = HANA_OBS_LAYER7;
1515 }
1516 else {
1517 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1518 "'layer4', 'layer7' but got '%s'\n",
1519 file, linenum, args[cur_arg], args[cur_arg + 1]);
1520 err_code |= ERR_ALERT | ERR_FATAL;
1521 goto out;
1522 }
1523
1524 cur_arg += 2;
1525 }
1526 else if (!strcmp(args[cur_arg], "on-error")) {
1527 if (!strcmp(args[cur_arg + 1], "fastinter"))
1528 newsrv->onerror = HANA_ONERR_FASTINTER;
1529 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1530 newsrv->onerror = HANA_ONERR_FAILCHK;
1531 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1532 newsrv->onerror = HANA_ONERR_SUDDTH;
1533 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1534 newsrv->onerror = HANA_ONERR_MARKDWN;
1535 else {
1536 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1537 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1538 file, linenum, args[cur_arg], args[cur_arg + 1]);
1539 err_code |= ERR_ALERT | ERR_FATAL;
1540 goto out;
1541 }
1542
1543 cur_arg += 2;
1544 }
1545 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1546 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1547 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1548 else {
1549 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1550 file, linenum, args[cur_arg], args[cur_arg + 1]);
1551 err_code |= ERR_ALERT | ERR_FATAL;
1552 goto out;
1553 }
1554
1555 cur_arg += 2;
1556 }
1557 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1558 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1559 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1560 else {
1561 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1562 file, linenum, args[cur_arg], args[cur_arg + 1]);
1563 err_code |= ERR_ALERT | ERR_FATAL;
1564 goto out;
1565 }
1566
1567 cur_arg += 2;
1568 }
1569 else if (!strcmp(args[cur_arg], "error-limit")) {
1570 if (!*args[cur_arg + 1]) {
1571 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1572 file, linenum, args[cur_arg]);
1573 err_code |= ERR_ALERT | ERR_FATAL;
1574 goto out;
1575 }
1576
1577 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1578
1579 if (newsrv->consecutive_errors_limit <= 0) {
1580 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1581 file, linenum, args[cur_arg]);
1582 err_code |= ERR_ALERT | ERR_FATAL;
1583 goto out;
1584 }
1585 cur_arg += 2;
1586 }
1587 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1588 int port_low, port_high;
1589 struct sockaddr_storage *sk;
1590 struct protocol *proto;
1591
1592 if (!*args[cur_arg + 1]) {
1593 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1594 file, linenum, "source", "usesrc", "interface");
1595 err_code |= ERR_ALERT | ERR_FATAL;
1596 goto out;
1597 }
1598
1599 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001600 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001601 if (!sk) {
1602 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1603 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1604 err_code |= ERR_ALERT | ERR_FATAL;
1605 goto out;
1606 }
1607
1608 proto = protocol_by_family(sk->ss_family);
1609 if (!proto || !proto->connect) {
1610 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1611 file, linenum, args[cur_arg], args[cur_arg+1]);
1612 err_code |= ERR_ALERT | ERR_FATAL;
1613 goto out;
1614 }
1615
1616 newsrv->conn_src.source_addr = *sk;
1617
1618 if (port_low != port_high) {
1619 int i;
1620
1621 if (!port_low || !port_high) {
1622 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1623 file, linenum, args[cur_arg], args[cur_arg + 1]);
1624 err_code |= ERR_ALERT | ERR_FATAL;
1625 goto out;
1626 }
1627
1628 if (port_low <= 0 || port_low > 65535 ||
1629 port_high <= 0 || port_high > 65535 ||
1630 port_low > port_high) {
1631 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1632 file, linenum, port_low, port_high);
1633 err_code |= ERR_ALERT | ERR_FATAL;
1634 goto out;
1635 }
1636 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1637 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1638 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1639 }
1640
1641 cur_arg += 2;
1642 while (*(args[cur_arg])) {
1643 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001644#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001645 if (!*args[cur_arg + 1]) {
1646 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1647 file, linenum, "usesrc");
1648 err_code |= ERR_ALERT | ERR_FATAL;
1649 goto out;
1650 }
1651 if (!strcmp(args[cur_arg + 1], "client")) {
1652 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1653 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1654 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1655 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1656 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1657 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1658 char *name, *end;
1659
1660 name = args[cur_arg+1] + 7;
1661 while (isspace(*name))
1662 name++;
1663
1664 end = name;
1665 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1666 end++;
1667
1668 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1669 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1670 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1671 newsrv->conn_src.bind_hdr_len = end - name;
1672 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1673 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1674 newsrv->conn_src.bind_hdr_occ = -1;
1675
1676 /* now look for an occurrence number */
1677 while (isspace(*end))
1678 end++;
1679 if (*end == ',') {
1680 end++;
1681 name = end;
1682 if (*end == '-')
1683 end++;
1684 while (isdigit((int)*end))
1685 end++;
1686 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1687 }
1688
1689 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1690 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1691 " occurrences values smaller than %d.\n",
1692 file, linenum, MAX_HDR_HISTORY);
1693 err_code |= ERR_ALERT | ERR_FATAL;
1694 goto out;
1695 }
1696 } else {
1697 struct sockaddr_storage *sk;
1698 int port1, port2;
1699
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001700 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001701 if (!sk) {
1702 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1703 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1704 err_code |= ERR_ALERT | ERR_FATAL;
1705 goto out;
1706 }
1707
1708 proto = protocol_by_family(sk->ss_family);
1709 if (!proto || !proto->connect) {
1710 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1711 file, linenum, args[cur_arg], args[cur_arg+1]);
1712 err_code |= ERR_ALERT | ERR_FATAL;
1713 goto out;
1714 }
1715
1716 if (port1 != port2) {
1717 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1718 file, linenum, args[cur_arg], args[cur_arg + 1]);
1719 err_code |= ERR_ALERT | ERR_FATAL;
1720 goto out;
1721 }
1722 newsrv->conn_src.tproxy_addr = *sk;
1723 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1724 }
1725 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001726 cur_arg += 2;
1727 continue;
1728#else /* no TPROXY support */
1729 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1730 file, linenum, "usesrc");
1731 err_code |= ERR_ALERT | ERR_FATAL;
1732 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001733#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001734 } /* "usesrc" */
1735
1736 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1737#ifdef SO_BINDTODEVICE
1738 if (!*args[cur_arg + 1]) {
1739 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1740 file, linenum, args[0]);
1741 err_code |= ERR_ALERT | ERR_FATAL;
1742 goto out;
1743 }
1744 free(newsrv->conn_src.iface_name);
1745 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1746 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1747 global.last_checks |= LSTCHK_NETADM;
1748#else
1749 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1750 file, linenum, args[0], args[cur_arg]);
1751 err_code |= ERR_ALERT | ERR_FATAL;
1752 goto out;
1753#endif
1754 cur_arg += 2;
1755 continue;
1756 }
1757 /* this keyword in not an option of "source" */
1758 break;
1759 } /* while */
1760 }
1761 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1762 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1763 file, linenum, "usesrc", "source");
1764 err_code |= ERR_ALERT | ERR_FATAL;
1765 goto out;
1766 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001767 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1768#ifdef CONFIG_HAP_NS
1769 char *arg = args[cur_arg + 1];
1770 if (!strcmp(arg, "*")) {
1771 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1772 } else {
1773 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1774
1775 if (newsrv->netns == NULL)
1776 newsrv->netns = netns_store_insert(arg);
1777
1778 if (newsrv->netns == NULL) {
1779 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1780 err_code |= ERR_ALERT | ERR_FATAL;
1781 goto out;
1782 }
1783 }
1784#else
1785 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1786 file, linenum, args[0], args[cur_arg]);
1787 err_code |= ERR_ALERT | ERR_FATAL;
1788 goto out;
1789#endif
1790 cur_arg += 2;
1791 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001792 else {
1793 static int srv_dumped;
1794 struct srv_kw *kw;
1795 char *err;
1796
1797 kw = srv_find_kw(args[cur_arg]);
1798 if (kw) {
1799 char *err = NULL;
1800 int code;
1801
1802 if (!kw->parse) {
1803 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1804 file, linenum, args[0], args[1], args[cur_arg]);
1805 cur_arg += 1 + kw->skip ;
1806 err_code |= ERR_ALERT | ERR_FATAL;
1807 goto out;
1808 }
1809
1810 if (defsrv && !kw->default_ok) {
1811 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1812 file, linenum, args[0], args[1], args[cur_arg]);
1813 cur_arg += 1 + kw->skip ;
1814 err_code |= ERR_ALERT;
1815 continue;
1816 }
1817
1818 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1819 err_code |= code;
1820
1821 if (code) {
1822 if (err && *err) {
1823 indent_msg(&err, 2);
1824 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1825 }
1826 else
1827 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1828 file, linenum, args[0], args[1], args[cur_arg]);
1829 if (code & ERR_FATAL) {
1830 free(err);
1831 cur_arg += 1 + kw->skip;
1832 goto out;
1833 }
1834 }
1835 free(err);
1836 cur_arg += 1 + kw->skip;
1837 continue;
1838 }
1839
1840 err = NULL;
1841 if (!srv_dumped) {
1842 srv_dump_kws(&err);
1843 indent_msg(&err, 4);
1844 srv_dumped = 1;
1845 }
1846
1847 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1848 file, linenum, args[0], args[1], args[cur_arg],
1849 err ? " Registered keywords :" : "", err ? err : "");
1850 free(err);
1851
1852 err_code |= ERR_ALERT | ERR_FATAL;
1853 goto out;
1854 }
1855 }
1856
Willy Tarreau272adea2014-03-31 10:39:59 +02001857 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001858 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001859
1860 if (newsrv->trackit) {
1861 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1862 file, linenum);
1863 err_code |= ERR_ALERT | ERR_FATAL;
1864 goto out;
1865 }
1866
Willy Tarreau272adea2014-03-31 10:39:59 +02001867 /*
1868 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001869 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001870 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001871 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001872 (is_inet_addr(&newsrv->check.addr) ||
1873 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001874 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001875 struct list *l;
1876
1877 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1878 if (!r) {
1879 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1880 file, linenum, newsrv->id);
1881 err_code |= ERR_ALERT | ERR_FATAL;
1882 goto out;
1883 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001884 /* search the first action (connect / send / expect) in the list */
1885 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001886 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001887 if (r->action != TCPCHK_ACT_COMMENT)
1888 break;
1889 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001890 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1891 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",
1892 file, linenum, newsrv->id);
1893 err_code |= ERR_ALERT | ERR_FATAL;
1894 goto out;
1895 }
1896 else {
1897 /* scan the tcp-check ruleset to ensure a port has been configured */
1898 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001899 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001900 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1901 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",
1902 file, linenum, newsrv->id);
1903 err_code |= ERR_ALERT | ERR_FATAL;
1904 goto out;
1905 }
1906 }
1907 }
1908 }
1909
1910 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001911 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001912 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001913 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1914 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001915 goto out;
1916 }
1917
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001918 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001919 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001920
Willy Tarreau272adea2014-03-31 10:39:59 +02001921 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1922 }
1923
1924 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001925 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001926
1927 if (!newsrv->agent.port) {
1928 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1929 file, linenum, newsrv->id);
1930 err_code |= ERR_ALERT | ERR_FATAL;
1931 goto out;
1932 }
1933
1934 if (!newsrv->agent.inter)
1935 newsrv->agent.inter = newsrv->check.inter;
1936
Simon Hormanb1900d52015-01-30 11:22:54 +09001937 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001938 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001939 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1940 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001941 goto out;
1942 }
1943
1944 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1945 }
1946
1947 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001948 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 curproxy->srv_bck++;
1950 else
1951 curproxy->srv_act++;
1952
Willy Tarreauc5150da2014-05-13 19:27:31 +02001953 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001954 }
1955 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001956 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001957 return 0;
1958
1959 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001960 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001961 free(errmsg);
1962 return err_code;
1963}
1964
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001965/* Returns a pointer to the first server matching either id <id>.
1966 * NULL is returned if no match is found.
1967 * the lookup is performed in the backend <bk>
1968 */
1969struct server *server_find_by_id(struct proxy *bk, int id)
1970{
1971 struct eb32_node *eb32;
1972 struct server *curserver;
1973
1974 if (!bk || (id ==0))
1975 return NULL;
1976
1977 /* <bk> has no backend capabilities, so it can't have a server */
1978 if (!(bk->cap & PR_CAP_BE))
1979 return NULL;
1980
1981 curserver = NULL;
1982
1983 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1984 if (eb32)
1985 curserver = container_of(eb32, struct server, conf.id);
1986
1987 return curserver;
1988}
1989
1990/* Returns a pointer to the first server matching either name <name>, or id
1991 * if <name> starts with a '#'. NULL is returned if no match is found.
1992 * the lookup is performed in the backend <bk>
1993 */
1994struct server *server_find_by_name(struct proxy *bk, const char *name)
1995{
1996 struct server *curserver;
1997
1998 if (!bk || !name)
1999 return NULL;
2000
2001 /* <bk> has no backend capabilities, so it can't have a server */
2002 if (!(bk->cap & PR_CAP_BE))
2003 return NULL;
2004
2005 curserver = NULL;
2006 if (*name == '#') {
2007 curserver = server_find_by_id(bk, atoi(name + 1));
2008 if (curserver)
2009 return curserver;
2010 }
2011 else {
2012 curserver = bk->srv;
2013
2014 while (curserver && (strcmp(curserver->id, name) != 0))
2015 curserver = curserver->next;
2016
2017 if (curserver)
2018 return curserver;
2019 }
2020
2021 return NULL;
2022}
2023
2024struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2025{
2026 struct server *byname;
2027 struct server *byid;
2028
2029 if (!name && !id)
2030 return NULL;
2031
2032 if (diff)
2033 *diff = 0;
2034
2035 byname = byid = NULL;
2036
2037 if (name) {
2038 byname = server_find_by_name(bk, name);
2039 if (byname && (!id || byname->puid == id))
2040 return byname;
2041 }
2042
2043 /* remaining possibilities :
2044 * - name not set
2045 * - name set but not found
2046 * - name found but ID doesn't match
2047 */
2048 if (id) {
2049 byid = server_find_by_id(bk, id);
2050 if (byid) {
2051 if (byname) {
2052 /* use id only if forced by configuration */
2053 if (byid->flags & SRV_F_FORCED_ID) {
2054 if (diff)
2055 *diff |= 2;
2056 return byid;
2057 }
2058 else {
2059 if (diff)
2060 *diff |= 1;
2061 return byname;
2062 }
2063 }
2064
2065 /* remaining possibilities:
2066 * - name not set
2067 * - name set but not found
2068 */
2069 if (name && diff)
2070 *diff |= 2;
2071 return byid;
2072 }
2073
2074 /* id bot found */
2075 if (byname) {
2076 if (diff)
2077 *diff |= 1;
2078 return byname;
2079 }
2080 }
2081
2082 return NULL;
2083}
2084
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002085/* Update a server state using the parameters available in the params list */
2086static void srv_update_state(struct server *srv, int version, char **params)
2087{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002088 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002089 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002090
2091 /* fields since version 1
2092 * and common to all other upcoming versions
2093 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002094 enum srv_state srv_op_state;
2095 enum srv_admin srv_admin_state;
2096 unsigned srv_uweight, srv_iweight;
2097 unsigned long srv_last_time_change;
2098 short srv_check_status;
2099 enum chk_result srv_check_result;
2100 int srv_check_health;
2101 int srv_check_state, srv_agent_state;
2102 int bk_f_forced_id;
2103 int srv_f_forced_id;
2104
Willy Tarreau31138fa2015-09-29 18:38:47 +02002105 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002106 switch (version) {
2107 case 1:
2108 /*
2109 * now we can proceed with server's state update:
2110 * srv_addr: params[0]
2111 * srv_op_state: params[1]
2112 * srv_admin_state: params[2]
2113 * srv_uweight: params[3]
2114 * srv_iweight: params[4]
2115 * srv_last_time_change: params[5]
2116 * srv_check_status: params[6]
2117 * srv_check_result: params[7]
2118 * srv_check_health: params[8]
2119 * srv_check_state: params[9]
2120 * srv_agent_state: params[10]
2121 * bk_f_forced_id: params[11]
2122 * srv_f_forced_id: params[12]
2123 */
2124
2125 /* validating srv_op_state */
2126 p = NULL;
2127 errno = 0;
2128 srv_op_state = strtol(params[1], &p, 10);
2129 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2130 (srv_op_state != SRV_ST_STOPPED &&
2131 srv_op_state != SRV_ST_STARTING &&
2132 srv_op_state != SRV_ST_RUNNING &&
2133 srv_op_state != SRV_ST_STOPPING)) {
2134 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2135 }
2136
2137 /* validating srv_admin_state */
2138 p = NULL;
2139 errno = 0;
2140 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002141
2142 /* inherited statuses will be recomputed later */
2143 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2144
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002145 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2146 (srv_admin_state != 0 &&
2147 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002148 srv_admin_state != SRV_ADMF_CMAINT &&
2149 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002150 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002151 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002152 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2153 }
2154
2155 /* validating srv_uweight */
2156 p = NULL;
2157 errno = 0;
2158 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002159 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002160 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2161
2162 /* validating srv_iweight */
2163 p = NULL;
2164 errno = 0;
2165 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002166 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002167 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2168
2169 /* validating srv_last_time_change */
2170 p = NULL;
2171 errno = 0;
2172 srv_last_time_change = strtol(params[5], &p, 10);
2173 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2174 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2175
2176 /* validating srv_check_status */
2177 p = NULL;
2178 errno = 0;
2179 srv_check_status = strtol(params[6], &p, 10);
2180 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2181 (srv_check_status >= HCHK_STATUS_SIZE))
2182 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2183
2184 /* validating srv_check_result */
2185 p = NULL;
2186 errno = 0;
2187 srv_check_result = strtol(params[7], &p, 10);
2188 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2189 (srv_check_result != CHK_RES_UNKNOWN &&
2190 srv_check_result != CHK_RES_NEUTRAL &&
2191 srv_check_result != CHK_RES_FAILED &&
2192 srv_check_result != CHK_RES_PASSED &&
2193 srv_check_result != CHK_RES_CONDPASS)) {
2194 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2195 }
2196
2197 /* validating srv_check_health */
2198 p = NULL;
2199 errno = 0;
2200 srv_check_health = strtol(params[8], &p, 10);
2201 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2202 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2203
2204 /* validating srv_check_state */
2205 p = NULL;
2206 errno = 0;
2207 srv_check_state = strtol(params[9], &p, 10);
2208 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2209 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2210 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2211
2212 /* validating srv_agent_state */
2213 p = NULL;
2214 errno = 0;
2215 srv_agent_state = strtol(params[10], &p, 10);
2216 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2217 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2218 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2219
2220 /* validating bk_f_forced_id */
2221 p = NULL;
2222 errno = 0;
2223 bk_f_forced_id = strtol(params[11], &p, 10);
2224 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2225 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2226
2227 /* validating srv_f_forced_id */
2228 p = NULL;
2229 errno = 0;
2230 srv_f_forced_id = strtol(params[12], &p, 10);
2231 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2232 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2233
2234
2235 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002236 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002237 goto out;
2238
2239 /* recover operational state and apply it to this server
2240 * and all servers tracking this one */
2241 switch (srv_op_state) {
2242 case SRV_ST_STOPPED:
2243 srv->check.health = 0;
2244 srv_set_stopped(srv, "changed from server-state after a reload");
2245 break;
2246 case SRV_ST_STARTING:
2247 srv->state = srv_op_state;
2248 break;
2249 case SRV_ST_STOPPING:
2250 srv->check.health = srv->check.rise + srv->check.fall - 1;
2251 srv_set_stopping(srv, "changed from server-state after a reload");
2252 break;
2253 case SRV_ST_RUNNING:
2254 srv->check.health = srv->check.rise + srv->check.fall - 1;
2255 srv_set_running(srv, "");
2256 break;
2257 }
2258
2259 /* When applying server state, the following rules apply:
2260 * - in case of a configuration change, we apply the setting from the new
2261 * configuration, regardless of old running state
2262 * - if no configuration change, we apply old running state only if old running
2263 * state is different from new configuration state
2264 */
2265 /* configuration has changed */
2266 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2267 if (srv->admin & SRV_ADMF_CMAINT)
2268 srv_adm_set_maint(srv);
2269 else
2270 srv_adm_set_ready(srv);
2271 }
2272 /* configuration is the same, let's compate old running state and new conf state */
2273 else {
2274 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2275 srv_adm_set_maint(srv);
2276 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2277 srv_adm_set_ready(srv);
2278 }
2279 /* apply drain mode if server is currently enabled */
2280 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2281 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002282 * (srv->iweight is the weight set up in configuration).
2283 * There are two possible reasons for FDRAIN to have been present :
2284 * - previous config weight was zero
2285 * - "set server b/s drain" was sent to the CLI
2286 *
2287 * In the first case, we simply want to drop this drain state
2288 * if the new weight is not zero anymore, meaning the administrator
2289 * has intentionally turned the weight back to a positive value to
2290 * enable the server again after an operation. In the second case,
2291 * the drain state was forced on the CLI regardless of the config's
2292 * weight so we don't want a change to the config weight to lose this
2293 * status. What this means is :
2294 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2295 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002296 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002297 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002298 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002299 }
2300
2301 srv->last_change = date.tv_sec - srv_last_time_change;
2302 srv->check.status = srv_check_status;
2303 srv->check.result = srv_check_result;
2304 srv->check.health = srv_check_health;
2305
2306 /* Only case we want to apply is removing ENABLED flag which could have been
2307 * done by the "disable health" command over the stats socket
2308 */
2309 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2310 (srv_check_state & CHK_ST_CONFIGURED) &&
2311 !(srv_check_state & CHK_ST_ENABLED))
2312 srv->check.state &= ~CHK_ST_ENABLED;
2313
2314 /* Only case we want to apply is removing ENABLED flag which could have been
2315 * done by the "disable agent" command over the stats socket
2316 */
2317 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2318 (srv_agent_state & CHK_ST_CONFIGURED) &&
2319 !(srv_agent_state & CHK_ST_ENABLED))
2320 srv->agent.state &= ~CHK_ST_ENABLED;
2321
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002322 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2323 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002324 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002325 * It means that a configuration file change has precedence over a unix socket change
2326 * for server's weight
2327 *
2328 * by default, HAProxy applies the following weight when parsing the configuration
2329 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002330 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002331 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002332 srv->uweight = srv_uweight;
2333 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002334 server_recalc_eweight(srv);
2335
Willy Tarreaue5a60682016-11-09 14:54:53 +01002336 /* load server IP address */
2337 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002338 break;
2339 default:
2340 chunk_appendf(msg, ", version '%d' not supported", version);
2341 }
2342
2343 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002344 if (msg->len) {
2345 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002346 Warning("server-state application failed for server '%s/%s'%s",
2347 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002348 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002349}
2350
2351/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2352 * For each proxy, it does the following:
2353 * - opens its server state file (either one or local one)
2354 * - read whole file, line by line
2355 * - analyse each line to check if it matches our current backend:
2356 * - backend name matches
2357 * - backend id matches if id is forced and name doesn't match
2358 * - if the server pointed by the line is found, then state is applied
2359 *
2360 * If the running backend uuid or id differs from the state file, then HAProxy reports
2361 * a warning.
2362 */
2363void apply_server_state(void)
2364{
2365 char *cur, *end;
2366 char mybuf[SRV_STATE_LINE_MAXLEN];
2367 int mybuflen;
2368 char *params[SRV_STATE_FILE_MAX_FIELDS];
2369 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2370 int arg, srv_arg, version, diff;
2371 FILE *f;
2372 char *filepath;
2373 char globalfilepath[MAXPATHLEN + 1];
2374 char localfilepath[MAXPATHLEN + 1];
2375 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2376 extern struct proxy *proxy;
2377 struct proxy *curproxy, *bk;
2378 struct server *srv;
2379
2380 globalfilepathlen = 0;
2381 /* create the globalfilepath variable */
2382 if (global.server_state_file) {
2383 /* absolute path or no base directory provided */
2384 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2385 len = strlen(global.server_state_file);
2386 if (len > MAXPATHLEN) {
2387 globalfilepathlen = 0;
2388 goto globalfileerror;
2389 }
2390 memcpy(globalfilepath, global.server_state_file, len);
2391 globalfilepath[len] = '\0';
2392 globalfilepathlen = len;
2393 }
2394 else if (global.server_state_base) {
2395 len = strlen(global.server_state_base);
2396 globalfilepathlen += len;
2397
2398 if (globalfilepathlen > MAXPATHLEN) {
2399 globalfilepathlen = 0;
2400 goto globalfileerror;
2401 }
2402 strncpy(globalfilepath, global.server_state_base, len);
2403 globalfilepath[globalfilepathlen] = 0;
2404
2405 /* append a slash if needed */
2406 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2407 if (globalfilepathlen + 1 > MAXPATHLEN) {
2408 globalfilepathlen = 0;
2409 goto globalfileerror;
2410 }
2411 globalfilepath[globalfilepathlen++] = '/';
2412 }
2413
2414 len = strlen(global.server_state_file);
2415 if (globalfilepathlen + len > MAXPATHLEN) {
2416 globalfilepathlen = 0;
2417 goto globalfileerror;
2418 }
2419 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2420 globalfilepathlen += len;
2421 globalfilepath[globalfilepathlen++] = 0;
2422 }
2423 }
2424 globalfileerror:
2425 if (globalfilepathlen == 0)
2426 globalfilepath[0] = '\0';
2427
2428 /* read servers state from local file */
2429 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2430 /* servers are only in backends */
2431 if (!(curproxy->cap & PR_CAP_BE))
2432 continue;
2433 fileopenerr = 0;
2434 filepath = NULL;
2435
2436 /* search server state file path and name */
2437 switch (curproxy->load_server_state_from_file) {
2438 /* read servers state from global file */
2439 case PR_SRV_STATE_FILE_GLOBAL:
2440 /* there was an error while generating global server state file path */
2441 if (globalfilepathlen == 0)
2442 continue;
2443 filepath = globalfilepath;
2444 fileopenerr = 1;
2445 break;
2446 /* this backend has its own file */
2447 case PR_SRV_STATE_FILE_LOCAL:
2448 localfilepathlen = 0;
2449 localfilepath[0] = '\0';
2450 len = 0;
2451 /* create the localfilepath variable */
2452 /* absolute path or no base directory provided */
2453 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2454 len = strlen(curproxy->server_state_file_name);
2455 if (len > MAXPATHLEN) {
2456 localfilepathlen = 0;
2457 goto localfileerror;
2458 }
2459 memcpy(localfilepath, curproxy->server_state_file_name, len);
2460 localfilepath[len] = '\0';
2461 localfilepathlen = len;
2462 }
2463 else if (global.server_state_base) {
2464 len = strlen(global.server_state_base);
2465 localfilepathlen += len;
2466
2467 if (localfilepathlen > MAXPATHLEN) {
2468 localfilepathlen = 0;
2469 goto localfileerror;
2470 }
2471 strncpy(localfilepath, global.server_state_base, len);
2472 localfilepath[localfilepathlen] = 0;
2473
2474 /* append a slash if needed */
2475 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2476 if (localfilepathlen + 1 > MAXPATHLEN) {
2477 localfilepathlen = 0;
2478 goto localfileerror;
2479 }
2480 localfilepath[localfilepathlen++] = '/';
2481 }
2482
2483 len = strlen(curproxy->server_state_file_name);
2484 if (localfilepathlen + len > MAXPATHLEN) {
2485 localfilepathlen = 0;
2486 goto localfileerror;
2487 }
2488 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2489 localfilepathlen += len;
2490 localfilepath[localfilepathlen++] = 0;
2491 }
2492 filepath = localfilepath;
2493 localfileerror:
2494 if (localfilepathlen == 0)
2495 localfilepath[0] = '\0';
2496
2497 break;
2498 case PR_SRV_STATE_FILE_NONE:
2499 default:
2500 continue;
2501 }
2502
2503 /* preload global state file */
2504 errno = 0;
2505 f = fopen(filepath, "r");
2506 if (errno && fileopenerr)
2507 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2508 if (!f)
2509 continue;
2510
2511 mybuf[0] = '\0';
2512 mybuflen = 0;
2513 version = 0;
2514
2515 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002516 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2517 Warning("Can't read first line of the server state file '%s'\n", filepath);
2518 goto fileclose;
2519 }
2520
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002521 cur = mybuf;
2522 version = atoi(cur);
2523 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2524 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002525 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002526
2527 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2528 int bk_f_forced_id = 0;
2529 int check_id = 0;
2530 int check_name = 0;
2531
2532 mybuflen = strlen(mybuf);
2533 cur = mybuf;
2534 end = cur + mybuflen;
2535
2536 bk = NULL;
2537 srv = NULL;
2538
2539 /* we need at least one character */
2540 if (mybuflen == 0)
2541 continue;
2542
2543 /* ignore blank characters at the beginning of the line */
2544 while (isspace(*cur))
2545 ++cur;
2546
2547 if (cur == end)
2548 continue;
2549
2550 /* ignore comment line */
2551 if (*cur == '#')
2552 continue;
2553
2554 /* we're now ready to move the line into *srv_params[] */
2555 params[0] = cur;
2556 arg = 1;
2557 srv_arg = 0;
2558 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2559 if (isspace(*cur)) {
2560 *cur = '\0';
2561 ++cur;
2562 while (isspace(*cur))
2563 ++cur;
2564 switch (version) {
2565 case 1:
2566 /*
2567 * srv_addr: params[4] => srv_params[0]
2568 * srv_op_state: params[5] => srv_params[1]
2569 * srv_admin_state: params[6] => srv_params[2]
2570 * srv_uweight: params[7] => srv_params[3]
2571 * srv_iweight: params[8] => srv_params[4]
2572 * srv_last_time_change: params[9] => srv_params[5]
2573 * srv_check_status: params[10] => srv_params[6]
2574 * srv_check_result: params[11] => srv_params[7]
2575 * srv_check_health: params[12] => srv_params[8]
2576 * srv_check_state: params[13] => srv_params[9]
2577 * srv_agent_state: params[14] => srv_params[10]
2578 * bk_f_forced_id: params[15] => srv_params[11]
2579 * srv_f_forced_id: params[16] => srv_params[12]
2580 */
2581 if (arg >= 4) {
2582 srv_params[srv_arg] = cur;
2583 ++srv_arg;
2584 }
2585 break;
2586 }
2587
2588 params[arg] = cur;
2589 ++arg;
2590 }
2591 else {
2592 ++cur;
2593 }
2594 }
2595
2596 /* if line is incomplete line, then ignore it.
2597 * otherwise, update useful flags */
2598 switch (version) {
2599 case 1:
2600 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2601 continue;
2602 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2603 check_id = (atoi(params[0]) == curproxy->uuid);
2604 check_name = (strcmp(curproxy->id, params[1]) == 0);
2605 break;
2606 }
2607
2608 diff = 0;
2609 bk = curproxy;
2610
2611 /* if backend can't be found, let's continue */
2612 if (!check_id && !check_name)
2613 continue;
2614 else if (!check_id && check_name) {
2615 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2616 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2617 }
2618 else if (check_id && !check_name) {
2619 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2620 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2621 /* if name doesn't match, we still want to update curproxy if the backend id
2622 * was forced in previous the previous configuration */
2623 if (!bk_f_forced_id)
2624 continue;
2625 }
2626
2627 /* look for the server by its id: param[2] */
2628 /* else look for the server by its name: param[3] */
2629 diff = 0;
2630 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2631
2632 if (!srv) {
2633 /* if no server found, then warning and continue with next line */
2634 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2635 params[3], params[2], params[0], params[1]);
2636 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2637 params[3], params[2], params[0], params[1]);
2638 continue;
2639 }
2640 else if (diff & PR_FBM_MISMATCH_ID) {
2641 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);
2642 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);
2643 }
2644 else if (diff & PR_FBM_MISMATCH_NAME) {
2645 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);
2646 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);
2647 }
2648
2649 /* now we can proceed with server's state update */
2650 srv_update_state(srv, version, srv_params);
2651 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002652fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002653 fclose(f);
2654 }
2655}
2656
Simon Horman7d09b9a2013-02-12 10:45:51 +09002657/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002658 * update a server's current IP address.
2659 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2660 * ip is in network format.
2661 * updater is a string which contains an information about the requester of the update.
2662 * updater is used if not NULL.
2663 *
2664 * A log line and a stderr warning message is generated based on server's backend options.
2665 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002666int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002667{
2668 /* generates a log line and a warning on stderr */
2669 if (1) {
2670 /* book enough space for both IPv4 and IPv6 */
2671 char oldip[INET6_ADDRSTRLEN];
2672 char newip[INET6_ADDRSTRLEN];
2673
2674 memset(oldip, '\0', INET6_ADDRSTRLEN);
2675 memset(newip, '\0', INET6_ADDRSTRLEN);
2676
2677 /* copy old IP address in a string */
2678 switch (s->addr.ss_family) {
2679 case AF_INET:
2680 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2681 break;
2682 case AF_INET6:
2683 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2684 break;
2685 };
2686
2687 /* copy new IP address in a string */
2688 switch (ip_sin_family) {
2689 case AF_INET:
2690 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2691 break;
2692 case AF_INET6:
2693 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2694 break;
2695 };
2696
2697 /* save log line into a buffer */
2698 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2699 s->proxy->id, s->id, oldip, newip, updater);
2700
2701 /* write the buffer on stderr */
2702 Warning("%s.\n", trash.str);
2703
2704 /* send a log */
2705 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2706 }
2707
2708 /* save the new IP family */
2709 s->addr.ss_family = ip_sin_family;
2710 /* save the new IP address */
2711 switch (ip_sin_family) {
2712 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002713 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002714 break;
2715 case AF_INET6:
2716 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2717 break;
2718 };
2719
2720 return 0;
2721}
2722
2723/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002724 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2725 *
2726 * Caller can pass its name through <updater> to get it integrated in the response message
2727 * returned by the function.
2728 *
2729 * The function first does the following, in that order:
2730 * - validates the new addr and/or port
2731 * - checks if an update is required (new IP or port is different than current ones)
2732 * - checks the update is allowed:
2733 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2734 * - allow all changes if no CHECKS are configured
2735 * - if CHECK is configured:
2736 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2737 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2738 * conditions are met
2739 */
2740const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2741{
2742 struct sockaddr_storage sa;
2743 int ret, port_change_required;
2744 char current_addr[INET6_ADDRSTRLEN];
2745 u_int16_t current_port, new_port;
2746 struct chunk *msg;
2747
2748 msg = get_trash_chunk();
2749 chunk_reset(msg);
2750
2751 if (addr) {
2752 memset(&sa, 0, sizeof(struct sockaddr_storage));
2753 if (str2ip2(addr, &sa, 0) == NULL) {
2754 chunk_printf(msg, "Invalid addr '%s'", addr);
2755 goto out;
2756 }
2757
2758 /* changes are allowed on AF_INET* families only */
2759 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2760 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2761 goto out;
2762 }
2763
2764 /* collecting data currently setup */
2765 memset(current_addr, '\0', sizeof(current_addr));
2766 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2767 /* changes are allowed on AF_INET* families only */
2768 if ((ret != AF_INET) && (ret != AF_INET6)) {
2769 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2770 goto out;
2771 }
2772
2773 /* applying ADDR changes if required and allowed
2774 * ipcmp returns 0 when both ADDR are the same
2775 */
2776 if (ipcmp(&s->addr, &sa) == 0) {
2777 chunk_appendf(msg, "no need to change the addr");
2778 goto port;
2779 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002780 ipcpy(&sa, &s->addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002781
2782 /* we also need to update check's ADDR only if it uses the server's one */
2783 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002784 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002785 }
2786
2787 /* we also need to update agent ADDR only if it use the server's one */
2788 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002789 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002790 }
2791
2792 /* update report for caller */
2793 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2794 }
2795
2796 port:
2797 if (port) {
2798 char sign = '\0';
2799 char *endptr;
2800
2801 if (addr)
2802 chunk_appendf(msg, ", ");
2803
2804 /* collecting data currently setup */
2805 current_port = get_host_port(&s->addr);
2806
2807 /* check if PORT change is required */
2808 port_change_required = 0;
2809
2810 sign = *port;
2811 new_port = strtol(port, &endptr, 10);
2812 if ((errno != 0) || (port == endptr)) {
2813 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2814 goto out;
2815 }
2816
2817 /* check if caller triggers a port mapped or offset */
2818 if (sign == '-' || (sign == '+')) {
2819 /* check if server currently uses port map */
2820 if (!(s->flags & SRV_F_MAPPORTS)) {
2821 /* switch from fixed port to port map mandatorily triggers
2822 * a port change */
2823 port_change_required = 1;
2824 /* check is configured
2825 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2826 * prevent PORT change if check doesn't have it's dedicated port while switching
2827 * to port mapping */
2828 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2829 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.");
2830 goto out;
2831 }
2832 }
2833 /* we're already using port maps */
2834 else {
2835 port_change_required = current_port != new_port;
2836 }
2837 }
2838 /* fixed port */
2839 else {
2840 port_change_required = current_port != new_port;
2841 }
2842
2843 /* applying PORT changes if required and update response message */
2844 if (port_change_required) {
2845 /* apply new port */
2846 set_host_port(&s->addr, new_port);
2847
2848 /* prepare message */
2849 chunk_appendf(msg, "port changed from '");
2850 if (s->flags & SRV_F_MAPPORTS)
2851 chunk_appendf(msg, "+");
2852 chunk_appendf(msg, "%d' to '", current_port);
2853
2854 if (sign == '-') {
2855 s->flags |= SRV_F_MAPPORTS;
2856 chunk_appendf(msg, "%c", sign);
2857 /* just use for result output */
2858 new_port = -new_port;
2859 }
2860 else if (sign == '+') {
2861 s->flags |= SRV_F_MAPPORTS;
2862 chunk_appendf(msg, "%c", sign);
2863 }
2864 else {
2865 s->flags &= ~SRV_F_MAPPORTS;
2866 }
2867
2868 chunk_appendf(msg, "%d'", new_port);
2869
2870 /* we also need to update health checks port only if it uses server's realport */
2871 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2872 s->check.port = new_port;
2873 }
2874 }
2875 else {
2876 chunk_appendf(msg, "no need to change the port");
2877 }
2878 }
2879
2880out:
2881 if (updater)
2882 chunk_appendf(msg, " by '%s'", updater);
2883 chunk_appendf(msg, "\n");
2884 return msg->str;
2885}
2886
2887
2888/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002889 * update server status based on result of name resolution
2890 * returns:
2891 * 0 if server status is updated
2892 * 1 if server status has not changed
2893 */
2894int snr_update_srv_status(struct server *s)
2895{
2896 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002897 struct dns_resolvers *resolvers;
2898
2899 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002900
2901 switch (resolution->status) {
2902 case RSLV_STATUS_NONE:
2903 /* status when HAProxy has just (re)started */
2904 trigger_resolution(s);
2905 break;
2906
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002907 case RSLV_STATUS_VALID:
2908 /*
2909 * resume health checks
2910 * server will be turned back on if health check is safe
2911 */
2912 if (!(s->admin & SRV_ADMF_RMAINT))
2913 return 1;
2914 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2915 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
2916 s->proxy->id, s->id);
2917
2918 Warning("%s.\n", trash.str);
2919 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2920 return 0;
2921
2922 case RSLV_STATUS_NX:
2923 /* stop server if resolution is NX for a long enough period */
2924 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
2925 if (s->admin & SRV_ADMF_RMAINT)
2926 return 1;
2927 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
2928 return 0;
2929 }
2930 break;
2931
2932 case RSLV_STATUS_TIMEOUT:
2933 /* stop server if resolution is TIMEOUT for a long enough period */
2934 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
2935 if (s->admin & SRV_ADMF_RMAINT)
2936 return 1;
2937 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
2938 return 0;
2939 }
2940 break;
2941
2942 case RSLV_STATUS_REFUSED:
2943 /* stop server if resolution is REFUSED for a long enough period */
2944 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
2945 if (s->admin & SRV_ADMF_RMAINT)
2946 return 1;
2947 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
2948 return 0;
2949 }
2950 break;
2951
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002952 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002953 /* stop server if resolution is in unmatched error for a long enough period */
2954 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
2955 if (s->admin & SRV_ADMF_RMAINT)
2956 return 1;
2957 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
2958 return 0;
2959 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002960 break;
2961 }
2962
2963 return 1;
2964}
2965
2966/*
2967 * Server Name Resolution valid response callback
2968 * It expects:
2969 * - <nameserver>: the name server which answered the valid response
2970 * - <response>: buffer containing a valid DNS response
2971 * - <response_len>: size of <response>
2972 * It performs the following actions:
2973 * - ignore response if current ip found and server family not met
2974 * - update with first new ip found if family is met and current IP is not found
2975 * returns:
2976 * 0 on error
2977 * 1 when no error or safe ignore
2978 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02002979int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002980{
2981 struct server *s;
2982 void *serverip, *firstip;
2983 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002984 int ret;
2985 struct chunk *chk = get_trash_chunk();
2986
2987 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002988 firstip = NULL; /* pointer to the first valid response found */
2989 /* it will be used as the new IP if a change is required */
2990 firstip_sin_family = AF_UNSPEC;
2991 serverip = NULL; /* current server IP address */
2992
2993 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002994 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002995
2996 /* initializing server IP pointer */
2997 server_sin_family = s->addr.ss_family;
2998 switch (server_sin_family) {
2999 case AF_INET:
3000 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3001 break;
3002
3003 case AF_INET6:
3004 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3005 break;
3006
3007 default:
3008 goto invalid;
3009 }
3010
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003011 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003012 serverip, server_sin_family, &firstip,
3013 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003014
3015 switch (ret) {
3016 case DNS_UPD_NO:
3017 if (resolution->status != RSLV_STATUS_VALID) {
3018 resolution->status = RSLV_STATUS_VALID;
3019 resolution->last_status_change = now_ms;
3020 }
3021 goto stop_resolution;
3022
3023 case DNS_UPD_SRVIP_NOT_FOUND:
3024 goto save_ip;
3025
3026 case DNS_UPD_CNAME:
3027 if (resolution->status != RSLV_STATUS_VALID) {
3028 resolution->status = RSLV_STATUS_VALID;
3029 resolution->last_status_change = now_ms;
3030 }
3031 goto invalid;
3032
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003033 case DNS_UPD_NO_IP_FOUND:
3034 if (resolution->status != RSLV_STATUS_OTHER) {
3035 resolution->status = RSLV_STATUS_OTHER;
3036 resolution->last_status_change = now_ms;
3037 }
3038 goto stop_resolution;
3039
Baptiste Assmannfad03182015-10-28 02:03:32 +01003040 case DNS_UPD_NAME_ERROR:
3041 /* if this is not the last expected response, we ignore it */
3042 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3043 return 0;
3044 /* update resolution status to OTHER error type */
3045 if (resolution->status != RSLV_STATUS_OTHER) {
3046 resolution->status = RSLV_STATUS_OTHER;
3047 resolution->last_status_change = now_ms;
3048 }
3049 goto stop_resolution;
3050
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003051 default:
3052 goto invalid;
3053
3054 }
3055
3056 save_ip:
3057 nameserver->counters.update += 1;
3058 if (resolution->status != RSLV_STATUS_VALID) {
3059 resolution->status = RSLV_STATUS_VALID;
3060 resolution->last_status_change = now_ms;
3061 }
3062
3063 /* save the first ip we found */
3064 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3065 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3066
3067 stop_resolution:
3068 /* update last resolution date and time */
3069 resolution->last_resolution = now_ms;
3070 /* reset current status flag */
3071 resolution->step = RSLV_STEP_NONE;
3072 /* reset values */
3073 dns_reset_resolution(resolution);
3074
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003075 dns_update_resolvers_timeout(nameserver->resolvers);
3076
3077 snr_update_srv_status(s);
3078 return 0;
3079
3080 invalid:
3081 nameserver->counters.invalid += 1;
3082 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3083 goto stop_resolution;
3084
3085 snr_update_srv_status(s);
3086 return 0;
3087}
3088
3089/*
3090 * Server Name Resolution error management callback
3091 * returns:
3092 * 0 on error
3093 * 1 when no error or safe ignore
3094 */
3095int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3096{
3097 struct server *s;
3098 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003099 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003100
3101 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003102 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003103 resolvers = resolution->resolvers;
3104
3105 /* can be ignored if this is not the last response */
3106 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3107 return 1;
3108 }
3109
3110 switch (error_code) {
3111 case DNS_RESP_INVALID:
3112 case DNS_RESP_WRONG_NAME:
3113 if (resolution->status != RSLV_STATUS_INVALID) {
3114 resolution->status = RSLV_STATUS_INVALID;
3115 resolution->last_status_change = now_ms;
3116 }
3117 break;
3118
3119 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003120 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003121 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003122 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003123 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003124 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3125 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003126
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003127 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003128 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003129 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003130 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003131 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003132 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003133 }
3134 else if (res_preferred_afinet) {
3135 /* fallback from A to AAAA */
3136 resolution->query_type = DNS_RTYPE_AAAA;
3137 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003138 else {
3139 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003140 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003141 resolution->query_type = DNS_RTYPE_A;
3142 } else {
3143 resolution->query_type = DNS_RTYPE_AAAA;
3144 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003145 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003146
3147 dns_send_query(resolution);
3148
3149 /*
3150 * move the resolution to the last element of the FIFO queue
3151 * and update timeout wakeup based on the new first entry
3152 */
3153 if (dns_check_resolution_queue(resolvers) > 1) {
3154 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003155 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003156 /* ex first resolution goes to the end of the queue */
3157 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3158 }
3159 dns_update_resolvers_timeout(resolvers);
3160 goto leave;
3161 }
3162 else {
3163 if (resolution->status != RSLV_STATUS_OTHER) {
3164 resolution->status = RSLV_STATUS_OTHER;
3165 resolution->last_status_change = now_ms;
3166 }
3167 }
3168 break;
3169
3170 case DNS_RESP_NX_DOMAIN:
3171 if (resolution->status != RSLV_STATUS_NX) {
3172 resolution->status = RSLV_STATUS_NX;
3173 resolution->last_status_change = now_ms;
3174 }
3175 break;
3176
3177 case DNS_RESP_REFUSED:
3178 if (resolution->status != RSLV_STATUS_REFUSED) {
3179 resolution->status = RSLV_STATUS_REFUSED;
3180 resolution->last_status_change = now_ms;
3181 }
3182 break;
3183
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003184 case DNS_RESP_TIMEOUT:
3185 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3186 resolution->status = RSLV_STATUS_TIMEOUT;
3187 resolution->last_status_change = now_ms;
3188 }
3189 break;
3190 }
3191
3192 /* update last resolution date and time */
3193 resolution->last_resolution = now_ms;
3194 /* reset current status flag */
3195 resolution->step = RSLV_STEP_NONE;
3196 /* reset values */
3197 dns_reset_resolution(resolution);
3198
3199 LIST_DEL(&resolution->list);
3200 dns_update_resolvers_timeout(resolvers);
3201
3202 leave:
3203 snr_update_srv_status(s);
3204 return 1;
3205}
3206
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003207/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3208 * resolver. This is suited for initial address configuration. Returns 0 on
3209 * success otherwise a non-zero error code. In case of error, *err_code, if
3210 * not NULL, is filled up.
3211 */
3212int srv_set_addr_via_libc(struct server *srv, int *err_code)
3213{
3214 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003215 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003216 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003217 return 1;
3218 }
3219 return 0;
3220}
3221
3222/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3223 * from the state file. This is suited for initial address configuration.
3224 * Returns 0 on success otherwise a non-zero error code. In case of error,
3225 * *err_code, if not NULL, is filled up.
3226 */
3227static int srv_apply_lastaddr(struct server *srv, int *err_code)
3228{
3229 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3230 if (err_code)
3231 *err_code |= ERR_WARN;
3232 return 1;
3233 }
3234 return 0;
3235}
3236
Willy Tarreau25e51522016-11-04 15:10:17 +01003237/* returns 0 if no error, otherwise a combination of ERR_* flags */
3238static int srv_iterate_initaddr(struct server *srv)
3239{
3240 int return_code = 0;
3241 int err_code;
3242 unsigned int methods;
3243
3244 methods = srv->init_addr_methods;
3245 if (!methods) { // default to "last,libc"
3246 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3247 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3248 }
3249
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003250 /* "-dr" : always append "none" so that server addresses resolution
3251 * failures are silently ignored, this is convenient to validate some
3252 * configs out of their environment.
3253 */
3254 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3255 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3256
Willy Tarreau25e51522016-11-04 15:10:17 +01003257 while (methods) {
3258 err_code = 0;
3259 switch (srv_get_next_initaddr(&methods)) {
3260 case SRV_IADDR_LAST:
3261 if (!srv->lastaddr)
3262 continue;
3263 if (srv_apply_lastaddr(srv, &err_code) == 0)
3264 return return_code;
3265 return_code |= err_code;
3266 break;
3267
3268 case SRV_IADDR_LIBC:
3269 if (!srv->hostname)
3270 continue;
3271 if (srv_set_addr_via_libc(srv, &err_code) == 0)
3272 return return_code;
3273 return_code |= err_code;
3274 break;
3275
Willy Tarreau37ebe122016-11-04 15:17:58 +01003276 case SRV_IADDR_NONE:
3277 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003278 if (return_code) {
3279 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3280 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3281 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003282 return return_code;
3283
Willy Tarreau4310d362016-11-02 15:05:56 +01003284 case SRV_IADDR_IP:
3285 ipcpy(&srv->init_addr, &srv->addr);
3286 if (return_code) {
3287 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3288 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3289 }
3290 return return_code;
3291
Willy Tarreau25e51522016-11-04 15:10:17 +01003292 default: /* unhandled method */
3293 break;
3294 }
3295 }
3296
3297 if (!return_code) {
3298 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3299 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3300 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003301 else {
3302 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3303 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3304 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003305
3306 return_code |= ERR_ALERT | ERR_FATAL;
3307 return return_code;
3308}
3309
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003310/*
3311 * This function parses all backends and all servers within each backend
3312 * and performs servers' addr resolution based on information provided by:
3313 * - configuration file
3314 * - server-state file (states provided by an 'old' haproxy process)
3315 *
3316 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3317 */
3318int srv_init_addr(void)
3319{
3320 struct proxy *curproxy;
3321 int return_code = 0;
3322
3323 curproxy = proxy;
3324 while (curproxy) {
3325 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003326
3327 /* servers are in backend only */
3328 if (!(curproxy->cap & PR_CAP_BE))
3329 goto srv_init_addr_next;
3330
Willy Tarreau25e51522016-11-04 15:10:17 +01003331 for (srv = curproxy->srv; srv; srv = srv->next)
3332 if (srv->hostname)
3333 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003334
3335 srv_init_addr_next:
3336 curproxy = curproxy->next;
3337 }
3338
3339 return return_code;
3340}
3341
3342
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003343/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003344 * Local variables:
3345 * c-indent-level: 8
3346 * c-basic-offset: 8
3347 * End:
3348 */