blob: 5a2c58a8f2d410605b5a4ba11deb7cab4bc749c3 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
17#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020018#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020019#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010020#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020021#include <common/time.h>
22
Willy Tarreau272adea2014-03-31 10:39:59 +020023#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020024#include <types/dns.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020025
Simon Hormanb1900d52015-01-30 11:22:54 +090026#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <proto/port_range.h>
28#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020029#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020030#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020031#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020032#include <proto/stream.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020033#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020034#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020035
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020036static void srv_update_state(struct server *srv, int version, char **params);
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Willy Tarreau21faa912012-10-10 08:27:36 +020038/* List head of all known server keywords */
39static struct srv_kw_list srv_keywords = {
40 .list = LIST_HEAD_INIT(srv_keywords.list)
41};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020042
Simon Hormana3608442013-11-01 16:46:15 +090043int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020044{
Willy Tarreau892337c2014-05-13 23:41:20 +020045 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020046 return s->down_time;
47
48 return now.tv_sec - s->last_change + s->down_time;
49}
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050051int srv_lastsession(const struct server *s)
52{
53 if (s->counters.last_sess)
54 return now.tv_sec - s->counters.last_sess;
55
56 return -1;
57}
58
Simon Horman4a741432013-02-23 15:35:38 +090059int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020060{
Simon Horman4a741432013-02-23 15:35:38 +090061 const struct server *s = check->server;
62
Willy Tarreauff5ae352013-12-11 20:36:34 +010063 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090064 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010065
Willy Tarreau892337c2014-05-13 23:41:20 +020066 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090067 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010068
Simon Horman4a741432013-02-23 15:35:38 +090069 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010070}
71
Willy Tarreau21faa912012-10-10 08:27:36 +020072/*
73 * Registers the server keyword list <kwl> as a list of valid keywords for next
74 * parsing sessions.
75 */
76void srv_register_keywords(struct srv_kw_list *kwl)
77{
78 LIST_ADDQ(&srv_keywords.list, &kwl->list);
79}
80
81/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
82 * keyword is found with a NULL ->parse() function, then an attempt is made to
83 * find one with a valid ->parse() function. This way it is possible to declare
84 * platform-dependant, known keywords as NULL, then only declare them as valid
85 * if some options are met. Note that if the requested keyword contains an
86 * opening parenthesis, everything from this point is ignored.
87 */
88struct srv_kw *srv_find_kw(const char *kw)
89{
90 int index;
91 const char *kwend;
92 struct srv_kw_list *kwl;
93 struct srv_kw *ret = NULL;
94
95 kwend = strchr(kw, '(');
96 if (!kwend)
97 kwend = kw + strlen(kw);
98
99 list_for_each_entry(kwl, &srv_keywords.list, list) {
100 for (index = 0; kwl->kw[index].kw != NULL; index++) {
101 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
102 kwl->kw[index].kw[kwend-kw] == 0) {
103 if (kwl->kw[index].parse)
104 return &kwl->kw[index]; /* found it !*/
105 else
106 ret = &kwl->kw[index]; /* may be OK */
107 }
108 }
109 }
110 return ret;
111}
112
113/* Dumps all registered "server" keywords to the <out> string pointer. The
114 * unsupported keywords are only dumped if their supported form was not
115 * found.
116 */
117void srv_dump_kws(char **out)
118{
119 struct srv_kw_list *kwl;
120 int index;
121
122 *out = NULL;
123 list_for_each_entry(kwl, &srv_keywords.list, list) {
124 for (index = 0; kwl->kw[index].kw != NULL; index++) {
125 if (kwl->kw[index].parse ||
126 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
127 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
128 kwl->scope,
129 kwl->kw[index].kw,
130 kwl->kw[index].skip ? " <arg>" : "",
131 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
132 kwl->kw[index].parse ? "" : " (not supported)");
133 }
134 }
135 }
136}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100137
Willy Tarreaudff55432012-10-10 17:51:05 +0200138/* parse the "id" server keyword */
139static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
140{
141 struct eb32_node *node;
142
143 if (!*args[*cur_arg + 1]) {
144 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
145 return ERR_ALERT | ERR_FATAL;
146 }
147
148 newsrv->puid = atol(args[*cur_arg + 1]);
149 newsrv->conf.id.key = newsrv->puid;
150
151 if (newsrv->puid <= 0) {
152 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
153 return ERR_ALERT | ERR_FATAL;
154 }
155
156 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
157 if (node) {
158 struct server *target = container_of(node, struct server, conf.id);
159 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
160 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
161 target->id);
162 return ERR_ALERT | ERR_FATAL;
163 }
164
165 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200166 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200167 return 0;
168}
169
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200170/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200171 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200172 * shutdown.
173 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200174void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200175{
Willy Tarreau87b09662015-04-03 00:22:06 +0200176 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200177
Willy Tarreau87b09662015-04-03 00:22:06 +0200178 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
179 if (stream->srv_conn == srv)
180 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200181}
182
183/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200184 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200185 * the reason for the shutdown.
186 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200187void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200188{
189 struct server *srv;
190
191 for (srv = px->srv; srv != NULL; srv = srv->next)
192 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200193 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200194}
195
Willy Tarreaubda92272014-05-20 21:55:30 +0200196/* Appends some information to a message string related to a server going UP or
197 * DOWN. If both <forced> and <reason> are null and the server tracks another
198 * one, a "via" information will be provided to know where the status came from.
199 * If <reason> is non-null, the entire string will be appended after a comma and
200 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200201 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200202 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200203 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200204void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200205{
Willy Tarreaubda92272014-05-20 21:55:30 +0200206 if (reason)
207 chunk_appendf(msg, ", %s", reason);
208 else if (!forced && s->track)
209 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200210
211 if (xferred >= 0) {
212 if (s->state == SRV_ST_STOPPED)
213 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
214 " %d sessions active, %d requeued, %d remaining in queue",
215 s->proxy->srv_act, s->proxy->srv_bck,
216 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
217 s->cur_sess, xferred, s->nbpend);
218 else
219 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
220 " %d sessions requeued, %d total in queue",
221 s->proxy->srv_act, s->proxy->srv_bck,
222 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
223 xferred, s->nbpend);
224 }
225}
226
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200227/* Marks server <s> down, regardless of its checks' statuses, notifies by all
228 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200230 * recomputes the number of servers, but not the map. Maintenance servers are
231 * ignored. It reports <reason> if non-null as the reason for going down. Note
232 * that it makes use of the trash to build the log strings, so <reason> must
233 * not be placed there.
234 */
235void srv_set_stopped(struct server *s, const char *reason)
236{
237 struct server *srv;
238 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
239 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900240 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200241 int xferred;
242
243 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
244 return;
245
246 s->last_change = now.tv_sec;
247 s->state = SRV_ST_STOPPED;
248 if (s->proxy->lbprm.set_server_status_down)
249 s->proxy->lbprm.set_server_status_down(s);
250
251 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200252 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200253
Willy Tarreau87b09662015-04-03 00:22:06 +0200254 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200255 * a connection. Those which are redispatchable will be queued
256 * to another server or to the proxy itself.
257 */
258 xferred = pendconn_redistribute(s);
259
260 chunk_printf(&trash,
261 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
262 s->proxy->id, s->id);
263
264 srv_append_status(&trash, s, reason, xferred, 0);
265 Warning("%s.\n", trash.str);
266
267 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900268 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
269 send_log(s->proxy, log_level, "%s.\n", trash.str);
270 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200271
272 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
273 set_backend_down(s->proxy);
274
275 s->counters.down_trans++;
276
277 for (srv = s->trackers; srv; srv = srv->tracknext)
278 srv_set_stopped(srv, NULL);
279}
280
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200281/* Marks server <s> up regardless of its checks' statuses and provided it isn't
282 * in maintenance. Notifies by all available means, recounts the remaining
283 * servers on the proxy and tries to grab requests from the proxy. It
284 * automatically recomputes the number of servers, but not the map. Maintenance
285 * servers are ignored. It reports <reason> if non-null as the reason for going
286 * up. Note that it makes use of the trash to build the log strings, so <reason>
287 * must not be placed there.
288 */
289void srv_set_running(struct server *s, const char *reason)
290{
291 struct server *srv;
292 int xferred;
293
294 if (s->admin & SRV_ADMF_MAINT)
295 return;
296
297 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
298 return;
299
300 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
301 if (s->proxy->last_change < now.tv_sec) // ignore negative times
302 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
303 s->proxy->last_change = now.tv_sec;
304 }
305
306 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
307 s->down_time += now.tv_sec - s->last_change;
308
309 s->last_change = now.tv_sec;
310
311 s->state = SRV_ST_STARTING;
312 if (s->slowstart > 0)
313 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
314 else
315 s->state = SRV_ST_RUNNING;
316
317 server_recalc_eweight(s);
318
319 /* If the server is set with "on-marked-up shutdown-backup-sessions",
320 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200321 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200322 * on all backup servers.
323 */
324 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
325 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200326 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200327
328 /* check if we can handle some connections queued at the proxy. We
329 * will take as many as we can handle.
330 */
331 xferred = pendconn_grab_from_px(s);
332
333 chunk_printf(&trash,
334 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
335 s->proxy->id, s->id);
336
337 srv_append_status(&trash, s, reason, xferred, 0);
338 Warning("%s.\n", trash.str);
339 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900340 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200341
342 for (srv = s->trackers; srv; srv = srv->tracknext)
343 srv_set_running(srv, NULL);
344}
345
Willy Tarreau8eb77842014-05-21 13:54:57 +0200346/* Marks server <s> stopping regardless of its checks' statuses and provided it
347 * isn't in maintenance. Notifies by all available means, recounts the remaining
348 * servers on the proxy and tries to grab requests from the proxy. It
349 * automatically recomputes the number of servers, but not the map. Maintenance
350 * servers are ignored. It reports <reason> if non-null as the reason for going
351 * up. Note that it makes use of the trash to build the log strings, so <reason>
352 * must not be placed there.
353 */
354void srv_set_stopping(struct server *s, const char *reason)
355{
356 struct server *srv;
357 int xferred;
358
359 if (s->admin & SRV_ADMF_MAINT)
360 return;
361
362 if (s->state == SRV_ST_STOPPING)
363 return;
364
365 s->last_change = now.tv_sec;
366 s->state = SRV_ST_STOPPING;
367 if (s->proxy->lbprm.set_server_status_down)
368 s->proxy->lbprm.set_server_status_down(s);
369
Willy Tarreau87b09662015-04-03 00:22:06 +0200370 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200371 * a connection. Those which are redispatchable will be queued
372 * to another server or to the proxy itself.
373 */
374 xferred = pendconn_redistribute(s);
375
376 chunk_printf(&trash,
377 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
378 s->proxy->id, s->id);
379
380 srv_append_status(&trash, s, reason, xferred, 0);
381
382 Warning("%s.\n", trash.str);
383 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
384
385 if (!s->proxy->srv_bck && !s->proxy->srv_act)
386 set_backend_down(s->proxy);
387
388 for (srv = s->trackers; srv; srv = srv->tracknext)
389 srv_set_stopping(srv, NULL);
390}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200391
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200392/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
393 * enforce either maint mode or drain mode. It is not allowed to set more than
394 * one flag at once. The equivalent "inherited" flag is propagated to all
395 * tracking servers. Maintenance mode disables health checks (but not agent
396 * checks). When either the flag is already set or no flag is passed, nothing
397 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200398 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200399void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200400{
401 struct check *check = &s->check;
402 struct server *srv;
403 int xferred;
404
405 if (!mode)
406 return;
407
408 /* stop going down as soon as we meet a server already in the same state */
409 if (s->admin & mode)
410 return;
411
412 s->admin |= mode;
413
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200414 /* stop going down if the equivalent flag was already present (forced or inherited) */
415 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
416 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
417 return;
418
419 /* Maintenance must also disable health checks */
420 if (mode & SRV_ADMF_MAINT) {
421 if (s->check.state & CHK_ST_ENABLED) {
422 s->check.state |= CHK_ST_PAUSED;
423 check->health = 0;
424 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200425
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200426 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200427 chunk_printf(&trash,
428 "%sServer %s/%s was DOWN and now enters maintenance",
429 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
430
Willy Tarreaubda92272014-05-20 21:55:30 +0200431 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200432
433 Warning("%s.\n", trash.str);
434 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
435 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200436 else { /* server was still running */
437 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
438 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
439
440 check->health = 0; /* failure */
441 s->last_change = now.tv_sec;
442 s->state = SRV_ST_STOPPED;
443 if (s->proxy->lbprm.set_server_status_down)
444 s->proxy->lbprm.set_server_status_down(s);
445
446 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200447 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200448
Willy Tarreau87b09662015-04-03 00:22:06 +0200449 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200450 * a connection. Those which are redispatchable will be queued
451 * to another server or to the proxy itself.
452 */
453 xferred = pendconn_redistribute(s);
454
455 chunk_printf(&trash,
456 "%sServer %s/%s is going DOWN for maintenance",
457 s->flags & SRV_F_BACKUP ? "Backup " : "",
458 s->proxy->id, s->id);
459
460 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
461
462 Warning("%s.\n", trash.str);
463 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
464
465 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
466 set_backend_down(s->proxy);
467
468 s->counters.down_trans++;
469 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200470 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200471
472 /* drain state is applied only if not yet in maint */
473 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200474 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
475
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200476 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200477 if (s->proxy->lbprm.set_server_status_down)
478 s->proxy->lbprm.set_server_status_down(s);
479
Willy Tarreau87b09662015-04-03 00:22:06 +0200480 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200481 * a connection. Those which are redispatchable will be queued
482 * to another server or to the proxy itself.
483 */
484 xferred = pendconn_redistribute(s);
485
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200486 chunk_printf(&trash, "%sServer %s/%s enters drain state",
487 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200488
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200489 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200490
491 Warning("%s.\n", trash.str);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200492 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900493 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200494
495 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
496 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200497 }
498
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200499 /* compute the inherited flag to propagate */
500 if (mode & SRV_ADMF_MAINT)
501 mode = SRV_ADMF_IMAINT;
502 else if (mode & SRV_ADMF_DRAIN)
503 mode = SRV_ADMF_IDRAIN;
504
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200505 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200506 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200507}
508
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200509/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
510 * stop enforcing either maint mode or drain mode. It is not allowed to set more
511 * than one flag at once. The equivalent "inherited" flag is propagated to all
512 * tracking servers. Leaving maintenance mode re-enables health checks. When
513 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200514 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200515void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200516{
517 struct check *check = &s->check;
518 struct server *srv;
519 int xferred = -1;
520
521 if (!mode)
522 return;
523
524 /* stop going down as soon as we see the flag is not there anymore */
525 if (!(s->admin & mode))
526 return;
527
528 s->admin &= ~mode;
529
530 if (s->admin & SRV_ADMF_MAINT) {
531 /* remaining in maintenance mode, let's inform precisely about the
532 * situation.
533 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200534 if (mode & SRV_ADMF_FMAINT) {
535 chunk_printf(&trash,
536 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
537 s->flags & SRV_F_BACKUP ? "Backup " : "",
538 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200539
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200540 if (s->track) /* normally it's mandatory here */
541 chunk_appendf(&trash, " via %s/%s",
542 s->track->proxy->id, s->track->id);
543 Warning("%s.\n", trash.str);
544 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
545 }
546 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200547 chunk_printf(&trash,
548 "%sServer %s/%s remains in forced maintenance",
549 s->flags & SRV_F_BACKUP ? "Backup " : "",
550 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200551 Warning("%s.\n", trash.str);
552 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
553 }
554 /* don't report anything when leaving drain mode and remaining in maintenance */
555 }
556 else if (mode & SRV_ADMF_MAINT) {
557 /* OK here we're leaving maintenance, we have many things to check,
558 * because the server might possibly be coming back up depending on
559 * its state. In practice, leaving maintenance means that we should
560 * immediately turn to UP (more or less the slowstart) under the
561 * following conditions :
562 * - server is neither checked nor tracked
563 * - server tracks another server which is not checked
564 * - server tracks another server which is already up
565 * Which sums up as something simpler :
566 * "either the tracking server is up or the server's checks are disabled
567 * or up". Otherwise we only re-enable health checks. There's a special
568 * case associated to the stopping state which can be inherited. Note
569 * that the server might still be in drain mode, which is naturally dealt
570 * with by the lower level functions.
571 */
572
573 if (s->check.state & CHK_ST_ENABLED) {
574 s->check.state &= ~CHK_ST_PAUSED;
575 check->health = check->rise; /* start OK but check immediately */
576 }
577
578 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
579 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
580 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
581 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
582 if (s->proxy->last_change < now.tv_sec) // ignore negative times
583 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
584 s->proxy->last_change = now.tv_sec;
585 }
586
587 if (s->last_change < now.tv_sec) // ignore negative times
588 s->down_time += now.tv_sec - s->last_change;
589 s->last_change = now.tv_sec;
590
591 if (s->track && s->track->state == SRV_ST_STOPPING)
592 s->state = SRV_ST_STOPPING;
593 else {
594 s->state = SRV_ST_STARTING;
595 if (s->slowstart > 0)
596 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
597 else
598 s->state = SRV_ST_RUNNING;
599 }
600
601 server_recalc_eweight(s);
602
603 /* If the server is set with "on-marked-up shutdown-backup-sessions",
604 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200605 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200606 * on all backup servers.
607 */
608 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
609 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200610 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200611
612 /* check if we can handle some connections queued at the proxy. We
613 * will take as many as we can handle.
614 */
615 xferred = pendconn_grab_from_px(s);
616 }
617
618 if (mode & SRV_ADMF_FMAINT) {
619 chunk_printf(&trash,
620 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
621 s->flags & SRV_F_BACKUP ? "Backup " : "",
622 s->proxy->id, s->id,
623 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
624 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200625 }
626 else {
627 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200628 "%sServer %s/%s is %s/%s (leaving maintenance)",
629 s->flags & SRV_F_BACKUP ? "Backup " : "",
630 s->proxy->id, s->id,
631 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
632 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
633 srv_append_status(&trash, s, NULL, xferred, 0);
634 }
635 Warning("%s.\n", trash.str);
636 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
637 }
638 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
639 /* remaining in drain mode after removing one of its flags */
640
641 if (mode & SRV_ADMF_FDRAIN) {
642 chunk_printf(&trash,
643 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200644 s->flags & SRV_F_BACKUP ? "Backup " : "",
645 s->proxy->id, s->id);
646
647 if (s->track) /* normally it's mandatory here */
648 chunk_appendf(&trash, " via %s/%s",
649 s->track->proxy->id, s->track->id);
650 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200651 else {
652 chunk_printf(&trash,
653 "%sServer %s/%s remains in forced drain mode",
654 s->flags & SRV_F_BACKUP ? "Backup " : "",
655 s->proxy->id, s->id);
656 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200657 Warning("%s.\n", trash.str);
658 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200659 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200660 else if (mode & SRV_ADMF_DRAIN) {
661 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200662 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
663 if (s->proxy->last_change < now.tv_sec) // ignore negative times
664 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
665 s->proxy->last_change = now.tv_sec;
666 }
667
668 if (s->last_change < now.tv_sec) // ignore negative times
669 s->down_time += now.tv_sec - s->last_change;
670 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200671 server_recalc_eweight(s);
672
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200673 if (mode & SRV_ADMF_FDRAIN) {
674 chunk_printf(&trash,
675 "%sServer %s/%s is %s (leaving forced drain)",
676 s->flags & SRV_F_BACKUP ? "Backup " : "",
677 s->proxy->id, s->id,
678 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
679 }
680 else {
681 chunk_printf(&trash,
682 "%sServer %s/%s is %s (leaving drain)",
683 s->flags & SRV_F_BACKUP ? "Backup " : "",
684 s->proxy->id, s->id,
685 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
686 if (s->track) /* normally it's mandatory here */
687 chunk_appendf(&trash, " via %s/%s",
688 s->track->proxy->id, s->track->id);
689 }
690 Warning("%s.\n", trash.str);
691 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200692 }
693
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200694 /* stop going down if the equivalent flag is still present (forced or inherited) */
695 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
696 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
697 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200698
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200699 if (mode & SRV_ADMF_MAINT)
700 mode = SRV_ADMF_IMAINT;
701 else if (mode & SRV_ADMF_DRAIN)
702 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200703
704 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200705 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200706}
707
Willy Tarreaudff55432012-10-10 17:51:05 +0200708/* Note: must not be declared <const> as its list will be overwritten.
709 * Please take care of keeping this list alphabetically sorted, doing so helps
710 * all code contributors.
711 * Optional keywords are also declared with a NULL ->parse() function so that
712 * the config parser can report an appropriate error when a known keyword was
713 * not enabled.
714 */
715static struct srv_kw_list srv_kws = { "ALL", { }, {
716 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
717 { NULL, NULL, 0 },
718}};
719
720__attribute__((constructor))
721static void __listener_init(void)
722{
723 srv_register_keywords(&srv_kws);
724}
725
Willy Tarreau004e0452013-11-21 11:22:01 +0100726/* Recomputes the server's eweight based on its state, uweight, the current time,
727 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
728 * state is automatically disabled if the time is elapsed.
729 */
730void server_recalc_eweight(struct server *sv)
731{
732 struct proxy *px = sv->proxy;
733 unsigned w;
734
735 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
736 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200737 if (sv->state == SRV_ST_STARTING)
738 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100739 }
740
741 /* We must take care of not pushing the server to full throttle during slow starts.
742 * It must also start immediately, at least at the minimal step when leaving maintenance.
743 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200744 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100745 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
746 else
747 w = px->lbprm.wdiv;
748
749 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
750
751 /* now propagate the status change to any LB algorithms */
752 if (px->lbprm.update_server_eweight)
753 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200754 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100755 if (px->lbprm.set_server_status_up)
756 px->lbprm.set_server_status_up(sv);
757 }
758 else {
759 if (px->lbprm.set_server_status_down)
760 px->lbprm.set_server_status_down(sv);
761 }
762}
763
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900765 * Parses weight_str and configures sv accordingly.
766 * Returns NULL on success, error message string otherwise.
767 */
768const char *server_parse_weight_change_request(struct server *sv,
769 const char *weight_str)
770{
771 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900772 long int w;
773 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900774
775 px = sv->proxy;
776
777 /* if the weight is terminated with '%', it is set relative to
778 * the initial weight, otherwise it is absolute.
779 */
780 if (!*weight_str)
781 return "Require <weight> or <weight%>.\n";
782
Simon Hormanb796afa2013-02-12 10:45:53 +0900783 w = strtol(weight_str, &end, 10);
784 if (end == weight_str)
785 return "Empty weight string empty or preceded by garbage";
786 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900787 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900788 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900789 /* Avoid integer overflow */
790 if (w > 25600)
791 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900792 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900793 if (w > 256)
794 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900795 }
796 else if (w < 0 || w > 256)
797 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900798 else if (end[0] != '\0')
799 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900800
801 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
802 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
803
804 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100805 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900806
807 return NULL;
808}
809
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200810/*
Thierry Fournier09a91782016-02-24 08:25:39 +0100811 * Parses <addr_str> and configures <sv> accordingly. <from> precise
812 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200813 * Returns:
814 * - error string on error
815 * - NULL on success
816 */
817const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +0100818 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200819{
820 unsigned char ip[INET6_ADDRSTRLEN];
821
822 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100823 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200824 return NULL;
825 }
826 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100827 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200828 return NULL;
829 }
830
831 return "Could not understand IP address format.\n";
832}
833
Willy Tarreau272adea2014-03-31 10:39:59 +0200834int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
835{
836 struct server *newsrv = NULL;
837 const char *err;
838 char *errmsg = NULL;
839 int err_code = 0;
840 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +0200841 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +0200842
843 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
844 int cur_arg;
845 short realport = 0;
846 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
847
848 if (!defsrv && curproxy == defproxy) {
849 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
850 err_code |= ERR_ALERT | ERR_FATAL;
851 goto out;
852 }
853 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
854 err_code |= ERR_ALERT | ERR_FATAL;
855
856 if (!*args[2]) {
857 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
858 file, linenum, args[0]);
859 err_code |= ERR_ALERT | ERR_FATAL;
860 goto out;
861 }
862
863 err = invalid_char(args[1]);
864 if (err && !defsrv) {
865 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
866 file, linenum, *err, args[1]);
867 err_code |= ERR_ALERT | ERR_FATAL;
868 goto out;
869 }
870
871 if (!defsrv) {
872 struct sockaddr_storage *sk;
873 int port1, port2;
874 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200875 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200876
Vincent Bernat02779b62016-04-03 13:48:43 +0200877 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200878 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
879 err_code |= ERR_ALERT | ERR_ABORT;
880 goto out;
881 }
882
883 /* the servers are linked backwards first */
884 newsrv->next = curproxy->srv;
885 curproxy->srv = newsrv;
886 newsrv->proxy = curproxy;
887 newsrv->conf.file = strdup(file);
888 newsrv->conf.line = linenum;
889
890 newsrv->obj_type = OBJ_TYPE_SERVER;
891 LIST_INIT(&newsrv->actconns);
892 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +0200893 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +0200894 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +0200895 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +0200896 do_check = 0;
897 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200898 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200899 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200900 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200901 newsrv->last_change = now.tv_sec;
902 newsrv->id = strdup(args[1]);
903
904 /* several ways to check the port component :
905 * - IP => port=+0, relative (IPv4 only)
906 * - IP: => port=+0, relative
907 * - IP:N => port=N, absolute
908 * - IP:+N => port=+N, relative
909 * - IP:-N => port=-N, relative
910 */
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +0200911 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +0200912 if (!sk) {
913 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
914 err_code |= ERR_ALERT | ERR_FATAL;
915 goto out;
916 }
917
918 proto = protocol_by_family(sk->ss_family);
919 if (!proto || !proto->connect) {
920 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
921 file, linenum, args[0], args[1]);
922 err_code |= ERR_ALERT | ERR_FATAL;
923 goto out;
924 }
925
926 if (!port1 || !port2) {
927 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200928 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200929 }
930 else if (port1 != port2) {
931 /* port range */
932 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
933 file, linenum, args[0], args[1], args[2]);
934 err_code |= ERR_ALERT | ERR_FATAL;
935 goto out;
936 }
937 else {
938 /* used by checks */
939 realport = port1;
940 }
941
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200942 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +0200943 newsrv->hostname = fqdn;
944 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200945 goto skip_name_resolution;
946
Willy Tarreau07101d52015-09-08 16:16:35 +0200947 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +0200948 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200949 goto skip_name_resolution;
950
951 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
952 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
953 goto skip_name_resolution;
954 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
955 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
956 file, linenum, args[2]);
957 err_code |= ERR_ALERT | ERR_FATAL;
958 goto out;
959 }
960
961 curr_resolution->requester = newsrv;
962 curr_resolution->requester_cb = snr_resolution_cb;
963 curr_resolution->requester_error_cb = snr_resolution_error_cb;
964 curr_resolution->status = RSLV_STATUS_NONE;
965 curr_resolution->step = RSLV_STEP_NONE;
966 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +0200967 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200968 newsrv->resolution = curr_resolution;
969
970 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +0200971 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +0100972 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200973
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +0100974 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200975 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
976 file, linenum, newsrv->addr.ss_family, args[2]);
977 err_code |= ERR_ALERT | ERR_FATAL;
978 goto out;
979 }
980
981 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
982 newsrv->check.port = curproxy->defsrv.check.port;
983 newsrv->check.inter = curproxy->defsrv.check.inter;
984 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
985 newsrv->check.downinter = curproxy->defsrv.check.downinter;
986 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
987 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -0700988 if (curproxy->defsrv.agent.send_string != NULL)
989 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
990 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +0200991 newsrv->agent.inter = curproxy->defsrv.agent.inter;
992 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
993 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
994 newsrv->maxqueue = curproxy->defsrv.maxqueue;
995 newsrv->minconn = curproxy->defsrv.minconn;
996 newsrv->maxconn = curproxy->defsrv.maxconn;
997 newsrv->slowstart = curproxy->defsrv.slowstart;
998 newsrv->onerror = curproxy->defsrv.onerror;
999 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1000 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1001 newsrv->consecutive_errors_limit
1002 = curproxy->defsrv.consecutive_errors_limit;
1003#ifdef OPENSSL
1004 newsrv->use_ssl = curproxy->defsrv.use_ssl;
1005#endif
1006 newsrv->uweight = newsrv->iweight
1007 = curproxy->defsrv.iweight;
1008
1009 newsrv->check.status = HCHK_STATUS_INI;
1010 newsrv->check.rise = curproxy->defsrv.check.rise;
1011 newsrv->check.fall = curproxy->defsrv.check.fall;
1012 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1013 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001014 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001015
1016 newsrv->agent.status = HCHK_STATUS_INI;
1017 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1018 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1019 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1020 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001021 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1022 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1023 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001024 memcpy(newsrv->dns_opts.pref_net,
1025 curproxy->defsrv.dns_opts.pref_net,
1026 sizeof(newsrv->dns_opts.pref_net));
1027 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Willy Tarreau272adea2014-03-31 10:39:59 +02001028
1029 cur_arg = 3;
1030 } else {
1031 newsrv = &curproxy->defsrv;
1032 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001033 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001034 }
1035
1036 while (*args[cur_arg]) {
1037 if (!strcmp(args[cur_arg], "agent-check")) {
1038 global.maxsock++;
1039 do_agent = 1;
1040 cur_arg += 1;
1041 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1042 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1043 if (err) {
1044 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1045 file, linenum, *err, newsrv->id);
1046 err_code |= ERR_ALERT | ERR_FATAL;
1047 goto out;
1048 }
1049 if (val <= 0) {
1050 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1051 file, linenum, val, args[cur_arg], newsrv->id);
1052 err_code |= ERR_ALERT | ERR_FATAL;
1053 goto out;
1054 }
1055 newsrv->agent.inter = val;
1056 cur_arg += 2;
1057 }
1058 else if (!strcmp(args[cur_arg], "agent-port")) {
1059 global.maxsock++;
1060 newsrv->agent.port = atol(args[cur_arg + 1]);
1061 cur_arg += 2;
1062 }
James Brown55f9ff12015-10-21 18:19:05 -07001063 else if (!strcmp(args[cur_arg], "agent-send")) {
1064 global.maxsock++;
1065 free(newsrv->agent.send_string);
1066 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1067 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1068 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1069 cur_arg += 2;
1070 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001071 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1072 newsrv->cookie = strdup(args[cur_arg + 1]);
1073 newsrv->cklen = strlen(args[cur_arg + 1]);
1074 cur_arg += 2;
1075 }
1076 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1077 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1078 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1079 cur_arg += 2;
1080 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001081 else if (!strcmp(args[cur_arg], "resolvers")) {
1082 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1083 cur_arg += 2;
1084 }
1085 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1086 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001087 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001088 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001089 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001090 else {
1091 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1092 file, linenum, args[cur_arg]);
1093 err_code |= ERR_ALERT | ERR_FATAL;
1094 goto out;
1095 }
1096 cur_arg += 2;
1097 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001098 else if (!strcmp(args[cur_arg], "resolve-net")) {
1099 char *p, *e;
1100 unsigned char mask;
1101 struct dns_options *opt;
1102
1103 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1104 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1105 file, linenum, args[cur_arg]);
1106 err_code |= ERR_ALERT | ERR_FATAL;
1107 goto out;
1108 }
1109
1110 opt = &newsrv->dns_opts;
1111
1112 /* Split arguments by comma, and convert it from ipv4 or ipv6
1113 * string network in in_addr or in6_addr.
1114 */
1115 p = args[cur_arg + 1];
1116 e = p;
1117 while (*p != '\0') {
1118 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001119 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001120 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1121 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1122 err_code |= ERR_ALERT | ERR_FATAL;
1123 goto out;
1124 }
1125 /* look for end or comma. */
1126 while (*e != ',' && *e != '\0')
1127 e++;
1128 if (*e == ',') {
1129 *e = '\0';
1130 e++;
1131 }
1132 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1133 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1134 /* Try to convert input string from ipv4 or ipv6 network. */
1135 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1136 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1137 &mask)) {
1138 /* Try to convert input string from ipv6 network. */
1139 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1140 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1141 } else {
1142 /* All network conversions fail, retrun error. */
1143 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1144 file, linenum, args[cur_arg], p);
1145 err_code |= ERR_ALERT | ERR_FATAL;
1146 goto out;
1147 }
1148 opt->pref_net_nb++;
1149 p = e;
1150 }
1151
1152 cur_arg += 2;
1153 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001154 else if (!strcmp(args[cur_arg], "rise")) {
1155 if (!*args[cur_arg + 1]) {
1156 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1157 file, linenum, args[cur_arg]);
1158 err_code |= ERR_ALERT | ERR_FATAL;
1159 goto out;
1160 }
1161
1162 newsrv->check.rise = atol(args[cur_arg + 1]);
1163 if (newsrv->check.rise <= 0) {
1164 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1165 file, linenum, args[cur_arg]);
1166 err_code |= ERR_ALERT | ERR_FATAL;
1167 goto out;
1168 }
1169
1170 if (newsrv->check.health)
1171 newsrv->check.health = newsrv->check.rise;
1172 cur_arg += 2;
1173 }
1174 else if (!strcmp(args[cur_arg], "fall")) {
1175 newsrv->check.fall = atol(args[cur_arg + 1]);
1176
1177 if (!*args[cur_arg + 1]) {
1178 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1179 file, linenum, args[cur_arg]);
1180 err_code |= ERR_ALERT | ERR_FATAL;
1181 goto out;
1182 }
1183
1184 if (newsrv->check.fall <= 0) {
1185 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1186 file, linenum, args[cur_arg]);
1187 err_code |= ERR_ALERT | ERR_FATAL;
1188 goto out;
1189 }
1190
1191 cur_arg += 2;
1192 }
1193 else if (!strcmp(args[cur_arg], "inter")) {
1194 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1195 if (err) {
1196 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1197 file, linenum, *err, newsrv->id);
1198 err_code |= ERR_ALERT | ERR_FATAL;
1199 goto out;
1200 }
1201 if (val <= 0) {
1202 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1203 file, linenum, val, args[cur_arg], newsrv->id);
1204 err_code |= ERR_ALERT | ERR_FATAL;
1205 goto out;
1206 }
1207 newsrv->check.inter = val;
1208 cur_arg += 2;
1209 }
1210 else if (!strcmp(args[cur_arg], "fastinter")) {
1211 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1212 if (err) {
1213 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1214 file, linenum, *err, newsrv->id);
1215 err_code |= ERR_ALERT | ERR_FATAL;
1216 goto out;
1217 }
1218 if (val <= 0) {
1219 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1220 file, linenum, val, args[cur_arg], newsrv->id);
1221 err_code |= ERR_ALERT | ERR_FATAL;
1222 goto out;
1223 }
1224 newsrv->check.fastinter = val;
1225 cur_arg += 2;
1226 }
1227 else if (!strcmp(args[cur_arg], "downinter")) {
1228 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1229 if (err) {
1230 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1231 file, linenum, *err, newsrv->id);
1232 err_code |= ERR_ALERT | ERR_FATAL;
1233 goto out;
1234 }
1235 if (val <= 0) {
1236 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1237 file, linenum, val, args[cur_arg], newsrv->id);
1238 err_code |= ERR_ALERT | ERR_FATAL;
1239 goto out;
1240 }
1241 newsrv->check.downinter = val;
1242 cur_arg += 2;
1243 }
1244 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1245 struct sockaddr_storage *sk;
1246 int port1, port2;
1247 struct protocol *proto;
1248
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001249 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001250 if (!sk) {
1251 Alert("parsing [%s:%d] : '%s' : %s\n",
1252 file, linenum, args[cur_arg], errmsg);
1253 err_code |= ERR_ALERT | ERR_FATAL;
1254 goto out;
1255 }
1256
1257 proto = protocol_by_family(sk->ss_family);
1258 if (!proto || !proto->connect) {
1259 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1260 file, linenum, args[cur_arg], args[cur_arg + 1]);
1261 err_code |= ERR_ALERT | ERR_FATAL;
1262 goto out;
1263 }
1264
1265 if (port1 != port2) {
1266 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1267 file, linenum, args[cur_arg], args[cur_arg + 1]);
1268 err_code |= ERR_ALERT | ERR_FATAL;
1269 goto out;
1270 }
1271
Simon Horman41f58762015-01-30 11:22:56 +09001272 newsrv->check.addr = newsrv->agent.addr = *sk;
Willy Tarreau272adea2014-03-31 10:39:59 +02001273 cur_arg += 2;
1274 }
1275 else if (!strcmp(args[cur_arg], "port")) {
1276 newsrv->check.port = atol(args[cur_arg + 1]);
1277 cur_arg += 2;
1278 }
1279 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001280 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001281 cur_arg ++;
1282 }
1283 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001284 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001285 cur_arg ++;
1286 }
1287 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001288 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001289 cur_arg ++;
1290 }
David Safb76832014-05-08 23:42:08 -04001291 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1292 newsrv->pp_opts |= SRV_PP_V2;
1293 cur_arg ++;
1294 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001295 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1296 newsrv->check.send_proxy = 1;
1297 cur_arg ++;
1298 }
1299 else if (!strcmp(args[cur_arg], "weight")) {
1300 int w;
1301 w = atol(args[cur_arg + 1]);
1302 if (w < 0 || w > SRV_UWGHT_MAX) {
1303 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1304 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1305 err_code |= ERR_ALERT | ERR_FATAL;
1306 goto out;
1307 }
1308 newsrv->uweight = newsrv->iweight = w;
1309 cur_arg += 2;
1310 }
1311 else if (!strcmp(args[cur_arg], "minconn")) {
1312 newsrv->minconn = atol(args[cur_arg + 1]);
1313 cur_arg += 2;
1314 }
1315 else if (!strcmp(args[cur_arg], "maxconn")) {
1316 newsrv->maxconn = atol(args[cur_arg + 1]);
1317 cur_arg += 2;
1318 }
1319 else if (!strcmp(args[cur_arg], "maxqueue")) {
1320 newsrv->maxqueue = atol(args[cur_arg + 1]);
1321 cur_arg += 2;
1322 }
1323 else if (!strcmp(args[cur_arg], "slowstart")) {
1324 /* slowstart is stored in seconds */
1325 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1326 if (err) {
1327 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1328 file, linenum, *err, newsrv->id);
1329 err_code |= ERR_ALERT | ERR_FATAL;
1330 goto out;
1331 }
1332 newsrv->slowstart = (val + 999) / 1000;
1333 cur_arg += 2;
1334 }
1335 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1336
1337 if (!*args[cur_arg + 1]) {
1338 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1339 file, linenum);
1340 err_code |= ERR_ALERT | ERR_FATAL;
1341 goto out;
1342 }
1343
1344 newsrv->trackit = strdup(args[cur_arg + 1]);
1345
1346 cur_arg += 2;
1347 }
1348 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1349 global.maxsock++;
1350 do_check = 1;
1351 cur_arg += 1;
1352 }
1353 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001354 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001355 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001356 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001357 newsrv->check.state |= CHK_ST_PAUSED;
1358 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001359 cur_arg += 1;
1360 }
1361 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1362 if (!strcmp(args[cur_arg + 1], "none"))
1363 newsrv->observe = HANA_OBS_NONE;
1364 else if (!strcmp(args[cur_arg + 1], "layer4"))
1365 newsrv->observe = HANA_OBS_LAYER4;
1366 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1367 if (curproxy->mode != PR_MODE_HTTP) {
1368 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1369 file, linenum, args[cur_arg + 1]);
1370 err_code |= ERR_ALERT;
1371 }
1372 newsrv->observe = HANA_OBS_LAYER7;
1373 }
1374 else {
1375 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1376 "'layer4', 'layer7' but got '%s'\n",
1377 file, linenum, args[cur_arg], args[cur_arg + 1]);
1378 err_code |= ERR_ALERT | ERR_FATAL;
1379 goto out;
1380 }
1381
1382 cur_arg += 2;
1383 }
1384 else if (!strcmp(args[cur_arg], "on-error")) {
1385 if (!strcmp(args[cur_arg + 1], "fastinter"))
1386 newsrv->onerror = HANA_ONERR_FASTINTER;
1387 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1388 newsrv->onerror = HANA_ONERR_FAILCHK;
1389 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1390 newsrv->onerror = HANA_ONERR_SUDDTH;
1391 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1392 newsrv->onerror = HANA_ONERR_MARKDWN;
1393 else {
1394 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1395 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1396 file, linenum, args[cur_arg], args[cur_arg + 1]);
1397 err_code |= ERR_ALERT | ERR_FATAL;
1398 goto out;
1399 }
1400
1401 cur_arg += 2;
1402 }
1403 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1404 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1405 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1406 else {
1407 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1408 file, linenum, args[cur_arg], args[cur_arg + 1]);
1409 err_code |= ERR_ALERT | ERR_FATAL;
1410 goto out;
1411 }
1412
1413 cur_arg += 2;
1414 }
1415 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1416 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1417 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1418 else {
1419 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1420 file, linenum, args[cur_arg], args[cur_arg + 1]);
1421 err_code |= ERR_ALERT | ERR_FATAL;
1422 goto out;
1423 }
1424
1425 cur_arg += 2;
1426 }
1427 else if (!strcmp(args[cur_arg], "error-limit")) {
1428 if (!*args[cur_arg + 1]) {
1429 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1430 file, linenum, args[cur_arg]);
1431 err_code |= ERR_ALERT | ERR_FATAL;
1432 goto out;
1433 }
1434
1435 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1436
1437 if (newsrv->consecutive_errors_limit <= 0) {
1438 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1439 file, linenum, args[cur_arg]);
1440 err_code |= ERR_ALERT | ERR_FATAL;
1441 goto out;
1442 }
1443 cur_arg += 2;
1444 }
1445 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1446 int port_low, port_high;
1447 struct sockaddr_storage *sk;
1448 struct protocol *proto;
1449
1450 if (!*args[cur_arg + 1]) {
1451 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1452 file, linenum, "source", "usesrc", "interface");
1453 err_code |= ERR_ALERT | ERR_FATAL;
1454 goto out;
1455 }
1456
1457 newsrv->conn_src.opts |= CO_SRC_BIND;
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001458 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001459 if (!sk) {
1460 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1461 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1462 err_code |= ERR_ALERT | ERR_FATAL;
1463 goto out;
1464 }
1465
1466 proto = protocol_by_family(sk->ss_family);
1467 if (!proto || !proto->connect) {
1468 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1469 file, linenum, args[cur_arg], args[cur_arg+1]);
1470 err_code |= ERR_ALERT | ERR_FATAL;
1471 goto out;
1472 }
1473
1474 newsrv->conn_src.source_addr = *sk;
1475
1476 if (port_low != port_high) {
1477 int i;
1478
1479 if (!port_low || !port_high) {
1480 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1481 file, linenum, args[cur_arg], args[cur_arg + 1]);
1482 err_code |= ERR_ALERT | ERR_FATAL;
1483 goto out;
1484 }
1485
1486 if (port_low <= 0 || port_low > 65535 ||
1487 port_high <= 0 || port_high > 65535 ||
1488 port_low > port_high) {
1489 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1490 file, linenum, port_low, port_high);
1491 err_code |= ERR_ALERT | ERR_FATAL;
1492 goto out;
1493 }
1494 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1495 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1496 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1497 }
1498
1499 cur_arg += 2;
1500 while (*(args[cur_arg])) {
1501 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001502#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001503 if (!*args[cur_arg + 1]) {
1504 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1505 file, linenum, "usesrc");
1506 err_code |= ERR_ALERT | ERR_FATAL;
1507 goto out;
1508 }
1509 if (!strcmp(args[cur_arg + 1], "client")) {
1510 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1511 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1512 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1513 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1514 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1515 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1516 char *name, *end;
1517
1518 name = args[cur_arg+1] + 7;
1519 while (isspace(*name))
1520 name++;
1521
1522 end = name;
1523 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1524 end++;
1525
1526 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1527 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1528 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1529 newsrv->conn_src.bind_hdr_len = end - name;
1530 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1531 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1532 newsrv->conn_src.bind_hdr_occ = -1;
1533
1534 /* now look for an occurrence number */
1535 while (isspace(*end))
1536 end++;
1537 if (*end == ',') {
1538 end++;
1539 name = end;
1540 if (*end == '-')
1541 end++;
1542 while (isdigit((int)*end))
1543 end++;
1544 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1545 }
1546
1547 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1548 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1549 " occurrences values smaller than %d.\n",
1550 file, linenum, MAX_HDR_HISTORY);
1551 err_code |= ERR_ALERT | ERR_FATAL;
1552 goto out;
1553 }
1554 } else {
1555 struct sockaddr_storage *sk;
1556 int port1, port2;
1557
Thierry FOURNIER7fe3be72015-09-26 20:03:36 +02001558 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001559 if (!sk) {
1560 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1561 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1562 err_code |= ERR_ALERT | ERR_FATAL;
1563 goto out;
1564 }
1565
1566 proto = protocol_by_family(sk->ss_family);
1567 if (!proto || !proto->connect) {
1568 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1569 file, linenum, args[cur_arg], args[cur_arg+1]);
1570 err_code |= ERR_ALERT | ERR_FATAL;
1571 goto out;
1572 }
1573
1574 if (port1 != port2) {
1575 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1576 file, linenum, args[cur_arg], args[cur_arg + 1]);
1577 err_code |= ERR_ALERT | ERR_FATAL;
1578 goto out;
1579 }
1580 newsrv->conn_src.tproxy_addr = *sk;
1581 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1582 }
1583 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001584 cur_arg += 2;
1585 continue;
1586#else /* no TPROXY support */
1587 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1588 file, linenum, "usesrc");
1589 err_code |= ERR_ALERT | ERR_FATAL;
1590 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001591#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001592 } /* "usesrc" */
1593
1594 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1595#ifdef SO_BINDTODEVICE
1596 if (!*args[cur_arg + 1]) {
1597 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1598 file, linenum, args[0]);
1599 err_code |= ERR_ALERT | ERR_FATAL;
1600 goto out;
1601 }
1602 free(newsrv->conn_src.iface_name);
1603 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1604 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1605 global.last_checks |= LSTCHK_NETADM;
1606#else
1607 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1608 file, linenum, args[0], args[cur_arg]);
1609 err_code |= ERR_ALERT | ERR_FATAL;
1610 goto out;
1611#endif
1612 cur_arg += 2;
1613 continue;
1614 }
1615 /* this keyword in not an option of "source" */
1616 break;
1617 } /* while */
1618 }
1619 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1620 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1621 file, linenum, "usesrc", "source");
1622 err_code |= ERR_ALERT | ERR_FATAL;
1623 goto out;
1624 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001625 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1626#ifdef CONFIG_HAP_NS
1627 char *arg = args[cur_arg + 1];
1628 if (!strcmp(arg, "*")) {
1629 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1630 } else {
1631 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1632
1633 if (newsrv->netns == NULL)
1634 newsrv->netns = netns_store_insert(arg);
1635
1636 if (newsrv->netns == NULL) {
1637 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1638 err_code |= ERR_ALERT | ERR_FATAL;
1639 goto out;
1640 }
1641 }
1642#else
1643 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1644 file, linenum, args[0], args[cur_arg]);
1645 err_code |= ERR_ALERT | ERR_FATAL;
1646 goto out;
1647#endif
1648 cur_arg += 2;
1649 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001650 else {
1651 static int srv_dumped;
1652 struct srv_kw *kw;
1653 char *err;
1654
1655 kw = srv_find_kw(args[cur_arg]);
1656 if (kw) {
1657 char *err = NULL;
1658 int code;
1659
1660 if (!kw->parse) {
1661 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1662 file, linenum, args[0], args[1], args[cur_arg]);
1663 cur_arg += 1 + kw->skip ;
1664 err_code |= ERR_ALERT | ERR_FATAL;
1665 goto out;
1666 }
1667
1668 if (defsrv && !kw->default_ok) {
1669 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1670 file, linenum, args[0], args[1], args[cur_arg]);
1671 cur_arg += 1 + kw->skip ;
1672 err_code |= ERR_ALERT;
1673 continue;
1674 }
1675
1676 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1677 err_code |= code;
1678
1679 if (code) {
1680 if (err && *err) {
1681 indent_msg(&err, 2);
1682 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1683 }
1684 else
1685 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1686 file, linenum, args[0], args[1], args[cur_arg]);
1687 if (code & ERR_FATAL) {
1688 free(err);
1689 cur_arg += 1 + kw->skip;
1690 goto out;
1691 }
1692 }
1693 free(err);
1694 cur_arg += 1 + kw->skip;
1695 continue;
1696 }
1697
1698 err = NULL;
1699 if (!srv_dumped) {
1700 srv_dump_kws(&err);
1701 indent_msg(&err, 4);
1702 srv_dumped = 1;
1703 }
1704
1705 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1706 file, linenum, args[0], args[1], args[cur_arg],
1707 err ? " Registered keywords :" : "", err ? err : "");
1708 free(err);
1709
1710 err_code |= ERR_ALERT | ERR_FATAL;
1711 goto out;
1712 }
1713 }
1714
Willy Tarreau272adea2014-03-31 10:39:59 +02001715 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001716 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001717
1718 if (newsrv->trackit) {
1719 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1720 file, linenum);
1721 err_code |= ERR_ALERT | ERR_FATAL;
1722 goto out;
1723 }
1724
1725 /* If neither a port nor an addr was specified and no check transport
1726 * layer is forced, then the transport layer used by the checks is the
1727 * same as for the production traffic. Otherwise we use raw_sock by
1728 * default, unless one is specified.
1729 */
Simon Horman41f58762015-01-30 11:22:56 +09001730 if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001731#ifdef USE_OPENSSL
1732 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1733#endif
David Safb76832014-05-08 23:42:08 -04001734 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001735 }
1736 /* try to get the port from check_core.addr if check.port not set */
1737 if (!newsrv->check.port)
Simon Horman41f58762015-01-30 11:22:56 +09001738 newsrv->check.port = get_host_port(&newsrv->check.addr);
Willy Tarreau272adea2014-03-31 10:39:59 +02001739
1740 if (!newsrv->check.port)
1741 newsrv->check.port = realport; /* by default */
1742
1743 if (!newsrv->check.port) {
1744 /* not yet valid, because no port was set on
1745 * the server either. We'll check if we have
1746 * a known port on the first listener.
1747 */
1748 struct listener *l;
1749
1750 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1751 newsrv->check.port = get_host_port(&l->addr);
1752 if (newsrv->check.port)
1753 break;
1754 }
1755 }
1756 /*
1757 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001758 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001759 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001760 if (!newsrv->check.port &&
Simon Horman41f58762015-01-30 11:22:56 +09001761 (is_inet_addr(&newsrv->check.addr) ||
1762 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001763 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001764 struct list *l;
1765
1766 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1767 if (!r) {
1768 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1769 file, linenum, newsrv->id);
1770 err_code |= ERR_ALERT | ERR_FATAL;
1771 goto out;
1772 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001773 /* search the first action (connect / send / expect) in the list */
1774 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001775 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001776 if (r->action != TCPCHK_ACT_COMMENT)
1777 break;
1778 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001779 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1780 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",
1781 file, linenum, newsrv->id);
1782 err_code |= ERR_ALERT | ERR_FATAL;
1783 goto out;
1784 }
1785 else {
1786 /* scan the tcp-check ruleset to ensure a port has been configured */
1787 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001788 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001789 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1790 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",
1791 file, linenum, newsrv->id);
1792 err_code |= ERR_ALERT | ERR_FATAL;
1793 goto out;
1794 }
1795 }
1796 }
1797 }
1798
1799 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001800 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001801 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001802 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1803 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001804 goto out;
1805 }
1806
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001807 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01001808 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001809
Willy Tarreau272adea2014-03-31 10:39:59 +02001810 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1811 }
1812
1813 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001814 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001815
1816 if (!newsrv->agent.port) {
1817 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1818 file, linenum, newsrv->id);
1819 err_code |= ERR_ALERT | ERR_FATAL;
1820 goto out;
1821 }
1822
1823 if (!newsrv->agent.inter)
1824 newsrv->agent.inter = newsrv->check.inter;
1825
Simon Hormanb1900d52015-01-30 11:22:54 +09001826 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001827 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001828 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1829 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001830 goto out;
1831 }
1832
1833 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1834 }
1835
1836 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001837 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001838 curproxy->srv_bck++;
1839 else
1840 curproxy->srv_act++;
1841
Willy Tarreauc5150da2014-05-13 19:27:31 +02001842 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001843 }
1844 }
Willy Tarreau07101d52015-09-08 16:16:35 +02001845 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001846 return 0;
1847
1848 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02001849 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02001850 free(errmsg);
1851 return err_code;
1852}
1853
Baptiste Assmann19a106d2015-07-08 22:03:56 +02001854/* Returns a pointer to the first server matching either id <id>.
1855 * NULL is returned if no match is found.
1856 * the lookup is performed in the backend <bk>
1857 */
1858struct server *server_find_by_id(struct proxy *bk, int id)
1859{
1860 struct eb32_node *eb32;
1861 struct server *curserver;
1862
1863 if (!bk || (id ==0))
1864 return NULL;
1865
1866 /* <bk> has no backend capabilities, so it can't have a server */
1867 if (!(bk->cap & PR_CAP_BE))
1868 return NULL;
1869
1870 curserver = NULL;
1871
1872 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
1873 if (eb32)
1874 curserver = container_of(eb32, struct server, conf.id);
1875
1876 return curserver;
1877}
1878
1879/* Returns a pointer to the first server matching either name <name>, or id
1880 * if <name> starts with a '#'. NULL is returned if no match is found.
1881 * the lookup is performed in the backend <bk>
1882 */
1883struct server *server_find_by_name(struct proxy *bk, const char *name)
1884{
1885 struct server *curserver;
1886
1887 if (!bk || !name)
1888 return NULL;
1889
1890 /* <bk> has no backend capabilities, so it can't have a server */
1891 if (!(bk->cap & PR_CAP_BE))
1892 return NULL;
1893
1894 curserver = NULL;
1895 if (*name == '#') {
1896 curserver = server_find_by_id(bk, atoi(name + 1));
1897 if (curserver)
1898 return curserver;
1899 }
1900 else {
1901 curserver = bk->srv;
1902
1903 while (curserver && (strcmp(curserver->id, name) != 0))
1904 curserver = curserver->next;
1905
1906 if (curserver)
1907 return curserver;
1908 }
1909
1910 return NULL;
1911}
1912
1913struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
1914{
1915 struct server *byname;
1916 struct server *byid;
1917
1918 if (!name && !id)
1919 return NULL;
1920
1921 if (diff)
1922 *diff = 0;
1923
1924 byname = byid = NULL;
1925
1926 if (name) {
1927 byname = server_find_by_name(bk, name);
1928 if (byname && (!id || byname->puid == id))
1929 return byname;
1930 }
1931
1932 /* remaining possibilities :
1933 * - name not set
1934 * - name set but not found
1935 * - name found but ID doesn't match
1936 */
1937 if (id) {
1938 byid = server_find_by_id(bk, id);
1939 if (byid) {
1940 if (byname) {
1941 /* use id only if forced by configuration */
1942 if (byid->flags & SRV_F_FORCED_ID) {
1943 if (diff)
1944 *diff |= 2;
1945 return byid;
1946 }
1947 else {
1948 if (diff)
1949 *diff |= 1;
1950 return byname;
1951 }
1952 }
1953
1954 /* remaining possibilities:
1955 * - name not set
1956 * - name set but not found
1957 */
1958 if (name && diff)
1959 *diff |= 2;
1960 return byid;
1961 }
1962
1963 /* id bot found */
1964 if (byname) {
1965 if (diff)
1966 *diff |= 1;
1967 return byname;
1968 }
1969 }
1970
1971 return NULL;
1972}
1973
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001974/* Update a server state using the parameters available in the params list */
1975static void srv_update_state(struct server *srv, int version, char **params)
1976{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001977 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02001978 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001979
1980 /* fields since version 1
1981 * and common to all other upcoming versions
1982 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001983 enum srv_state srv_op_state;
1984 enum srv_admin srv_admin_state;
1985 unsigned srv_uweight, srv_iweight;
1986 unsigned long srv_last_time_change;
1987 short srv_check_status;
1988 enum chk_result srv_check_result;
1989 int srv_check_health;
1990 int srv_check_state, srv_agent_state;
1991 int bk_f_forced_id;
1992 int srv_f_forced_id;
1993
Willy Tarreau31138fa2015-09-29 18:38:47 +02001994 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02001995 switch (version) {
1996 case 1:
1997 /*
1998 * now we can proceed with server's state update:
1999 * srv_addr: params[0]
2000 * srv_op_state: params[1]
2001 * srv_admin_state: params[2]
2002 * srv_uweight: params[3]
2003 * srv_iweight: params[4]
2004 * srv_last_time_change: params[5]
2005 * srv_check_status: params[6]
2006 * srv_check_result: params[7]
2007 * srv_check_health: params[8]
2008 * srv_check_state: params[9]
2009 * srv_agent_state: params[10]
2010 * bk_f_forced_id: params[11]
2011 * srv_f_forced_id: params[12]
2012 */
2013
2014 /* validating srv_op_state */
2015 p = NULL;
2016 errno = 0;
2017 srv_op_state = strtol(params[1], &p, 10);
2018 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2019 (srv_op_state != SRV_ST_STOPPED &&
2020 srv_op_state != SRV_ST_STARTING &&
2021 srv_op_state != SRV_ST_RUNNING &&
2022 srv_op_state != SRV_ST_STOPPING)) {
2023 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2024 }
2025
2026 /* validating srv_admin_state */
2027 p = NULL;
2028 errno = 0;
2029 srv_admin_state = strtol(params[2], &p, 10);
2030 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2031 (srv_admin_state != 0 &&
2032 srv_admin_state != SRV_ADMF_FMAINT &&
2033 srv_admin_state != SRV_ADMF_IMAINT &&
2034 srv_admin_state != SRV_ADMF_CMAINT &&
2035 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
2036 srv_admin_state != SRV_ADMF_FDRAIN &&
2037 srv_admin_state != SRV_ADMF_IDRAIN)) {
2038 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2039 }
2040
2041 /* validating srv_uweight */
2042 p = NULL;
2043 errno = 0;
2044 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002045 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002046 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2047
2048 /* validating srv_iweight */
2049 p = NULL;
2050 errno = 0;
2051 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002052 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002053 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2054
2055 /* validating srv_last_time_change */
2056 p = NULL;
2057 errno = 0;
2058 srv_last_time_change = strtol(params[5], &p, 10);
2059 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2060 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2061
2062 /* validating srv_check_status */
2063 p = NULL;
2064 errno = 0;
2065 srv_check_status = strtol(params[6], &p, 10);
2066 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2067 (srv_check_status >= HCHK_STATUS_SIZE))
2068 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2069
2070 /* validating srv_check_result */
2071 p = NULL;
2072 errno = 0;
2073 srv_check_result = strtol(params[7], &p, 10);
2074 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2075 (srv_check_result != CHK_RES_UNKNOWN &&
2076 srv_check_result != CHK_RES_NEUTRAL &&
2077 srv_check_result != CHK_RES_FAILED &&
2078 srv_check_result != CHK_RES_PASSED &&
2079 srv_check_result != CHK_RES_CONDPASS)) {
2080 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2081 }
2082
2083 /* validating srv_check_health */
2084 p = NULL;
2085 errno = 0;
2086 srv_check_health = strtol(params[8], &p, 10);
2087 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2088 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2089
2090 /* validating srv_check_state */
2091 p = NULL;
2092 errno = 0;
2093 srv_check_state = strtol(params[9], &p, 10);
2094 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2095 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2096 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2097
2098 /* validating srv_agent_state */
2099 p = NULL;
2100 errno = 0;
2101 srv_agent_state = strtol(params[10], &p, 10);
2102 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2103 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2104 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2105
2106 /* validating bk_f_forced_id */
2107 p = NULL;
2108 errno = 0;
2109 bk_f_forced_id = strtol(params[11], &p, 10);
2110 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2111 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2112
2113 /* validating srv_f_forced_id */
2114 p = NULL;
2115 errno = 0;
2116 srv_f_forced_id = strtol(params[12], &p, 10);
2117 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2118 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2119
2120
2121 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002122 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002123 goto out;
2124
2125 /* recover operational state and apply it to this server
2126 * and all servers tracking this one */
2127 switch (srv_op_state) {
2128 case SRV_ST_STOPPED:
2129 srv->check.health = 0;
2130 srv_set_stopped(srv, "changed from server-state after a reload");
2131 break;
2132 case SRV_ST_STARTING:
2133 srv->state = srv_op_state;
2134 break;
2135 case SRV_ST_STOPPING:
2136 srv->check.health = srv->check.rise + srv->check.fall - 1;
2137 srv_set_stopping(srv, "changed from server-state after a reload");
2138 break;
2139 case SRV_ST_RUNNING:
2140 srv->check.health = srv->check.rise + srv->check.fall - 1;
2141 srv_set_running(srv, "");
2142 break;
2143 }
2144
2145 /* When applying server state, the following rules apply:
2146 * - in case of a configuration change, we apply the setting from the new
2147 * configuration, regardless of old running state
2148 * - if no configuration change, we apply old running state only if old running
2149 * state is different from new configuration state
2150 */
2151 /* configuration has changed */
2152 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2153 if (srv->admin & SRV_ADMF_CMAINT)
2154 srv_adm_set_maint(srv);
2155 else
2156 srv_adm_set_ready(srv);
2157 }
2158 /* configuration is the same, let's compate old running state and new conf state */
2159 else {
2160 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2161 srv_adm_set_maint(srv);
2162 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2163 srv_adm_set_ready(srv);
2164 }
2165 /* apply drain mode if server is currently enabled */
2166 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2167 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
2168 * (srv->iweight is the weight set up in configuration)
2169 * so we don't want to apply it when srv_iweight is 0 and
2170 * srv->iweight is greater than 0. Purpose is to give the
2171 * chance to the admin to re-enable this server from configuration
2172 * file by setting a new weight > 0.
2173 */
2174 if ((srv_iweight == 0) && (srv->iweight > 0)) {
2175 srv_adm_set_drain(srv);
2176 }
2177 }
2178
2179 srv->last_change = date.tv_sec - srv_last_time_change;
2180 srv->check.status = srv_check_status;
2181 srv->check.result = srv_check_result;
2182 srv->check.health = srv_check_health;
2183
2184 /* Only case we want to apply is removing ENABLED flag which could have been
2185 * done by the "disable health" command over the stats socket
2186 */
2187 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2188 (srv_check_state & CHK_ST_CONFIGURED) &&
2189 !(srv_check_state & CHK_ST_ENABLED))
2190 srv->check.state &= ~CHK_ST_ENABLED;
2191
2192 /* Only case we want to apply is removing ENABLED flag which could have been
2193 * done by the "disable agent" command over the stats socket
2194 */
2195 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2196 (srv_agent_state & CHK_ST_CONFIGURED) &&
2197 !(srv_agent_state & CHK_ST_ENABLED))
2198 srv->agent.state &= ~CHK_ST_ENABLED;
2199
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002200 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2201 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002202 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002203 * It means that a configuration file change has precedence over a unix socket change
2204 * for server's weight
2205 *
2206 * by default, HAProxy applies the following weight when parsing the configuration
2207 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002208 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002209 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002210 srv->uweight = srv_uweight;
2211 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002212 server_recalc_eweight(srv);
2213
2214 /* update server IP only if DNS resolution is used on the server */
2215 if (srv->resolution) {
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002216 struct sockaddr_storage addr;
2217
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002218 memset(&addr, 0, sizeof(struct sockaddr_storage));
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002219
2220 if (str2ip2(params[0], &addr, AF_UNSPEC)) {
2221 int port;
2222
2223 /* save the port, applies the new IP then reconfigure the port */
Willy Tarreauf3c7a832016-01-21 13:51:56 +01002224 port = get_host_port(&srv->addr);
Baptiste Assmanna875b1f2016-01-21 00:17:09 +01002225 srv->addr.ss_family = addr.ss_family;
2226 str2ip2(params[0], &srv->addr, srv->addr.ss_family);
2227 set_host_port(&srv->addr, port);
2228 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002229 else
2230 chunk_appendf(msg, ", can't parse IP: %s", params[0]);
2231 }
2232 break;
2233 default:
2234 chunk_appendf(msg, ", version '%d' not supported", version);
2235 }
2236
2237 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002238 if (msg->len) {
2239 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002240 Warning("server-state application failed for server '%s/%s'%s",
2241 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002242 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002243}
2244
2245/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2246 * For each proxy, it does the following:
2247 * - opens its server state file (either one or local one)
2248 * - read whole file, line by line
2249 * - analyse each line to check if it matches our current backend:
2250 * - backend name matches
2251 * - backend id matches if id is forced and name doesn't match
2252 * - if the server pointed by the line is found, then state is applied
2253 *
2254 * If the running backend uuid or id differs from the state file, then HAProxy reports
2255 * a warning.
2256 */
2257void apply_server_state(void)
2258{
2259 char *cur, *end;
2260 char mybuf[SRV_STATE_LINE_MAXLEN];
2261 int mybuflen;
2262 char *params[SRV_STATE_FILE_MAX_FIELDS];
2263 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2264 int arg, srv_arg, version, diff;
2265 FILE *f;
2266 char *filepath;
2267 char globalfilepath[MAXPATHLEN + 1];
2268 char localfilepath[MAXPATHLEN + 1];
2269 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2270 extern struct proxy *proxy;
2271 struct proxy *curproxy, *bk;
2272 struct server *srv;
2273
2274 globalfilepathlen = 0;
2275 /* create the globalfilepath variable */
2276 if (global.server_state_file) {
2277 /* absolute path or no base directory provided */
2278 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2279 len = strlen(global.server_state_file);
2280 if (len > MAXPATHLEN) {
2281 globalfilepathlen = 0;
2282 goto globalfileerror;
2283 }
2284 memcpy(globalfilepath, global.server_state_file, len);
2285 globalfilepath[len] = '\0';
2286 globalfilepathlen = len;
2287 }
2288 else if (global.server_state_base) {
2289 len = strlen(global.server_state_base);
2290 globalfilepathlen += len;
2291
2292 if (globalfilepathlen > MAXPATHLEN) {
2293 globalfilepathlen = 0;
2294 goto globalfileerror;
2295 }
2296 strncpy(globalfilepath, global.server_state_base, len);
2297 globalfilepath[globalfilepathlen] = 0;
2298
2299 /* append a slash if needed */
2300 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2301 if (globalfilepathlen + 1 > MAXPATHLEN) {
2302 globalfilepathlen = 0;
2303 goto globalfileerror;
2304 }
2305 globalfilepath[globalfilepathlen++] = '/';
2306 }
2307
2308 len = strlen(global.server_state_file);
2309 if (globalfilepathlen + len > MAXPATHLEN) {
2310 globalfilepathlen = 0;
2311 goto globalfileerror;
2312 }
2313 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2314 globalfilepathlen += len;
2315 globalfilepath[globalfilepathlen++] = 0;
2316 }
2317 }
2318 globalfileerror:
2319 if (globalfilepathlen == 0)
2320 globalfilepath[0] = '\0';
2321
2322 /* read servers state from local file */
2323 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2324 /* servers are only in backends */
2325 if (!(curproxy->cap & PR_CAP_BE))
2326 continue;
2327 fileopenerr = 0;
2328 filepath = NULL;
2329
2330 /* search server state file path and name */
2331 switch (curproxy->load_server_state_from_file) {
2332 /* read servers state from global file */
2333 case PR_SRV_STATE_FILE_GLOBAL:
2334 /* there was an error while generating global server state file path */
2335 if (globalfilepathlen == 0)
2336 continue;
2337 filepath = globalfilepath;
2338 fileopenerr = 1;
2339 break;
2340 /* this backend has its own file */
2341 case PR_SRV_STATE_FILE_LOCAL:
2342 localfilepathlen = 0;
2343 localfilepath[0] = '\0';
2344 len = 0;
2345 /* create the localfilepath variable */
2346 /* absolute path or no base directory provided */
2347 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2348 len = strlen(curproxy->server_state_file_name);
2349 if (len > MAXPATHLEN) {
2350 localfilepathlen = 0;
2351 goto localfileerror;
2352 }
2353 memcpy(localfilepath, curproxy->server_state_file_name, len);
2354 localfilepath[len] = '\0';
2355 localfilepathlen = len;
2356 }
2357 else if (global.server_state_base) {
2358 len = strlen(global.server_state_base);
2359 localfilepathlen += len;
2360
2361 if (localfilepathlen > MAXPATHLEN) {
2362 localfilepathlen = 0;
2363 goto localfileerror;
2364 }
2365 strncpy(localfilepath, global.server_state_base, len);
2366 localfilepath[localfilepathlen] = 0;
2367
2368 /* append a slash if needed */
2369 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2370 if (localfilepathlen + 1 > MAXPATHLEN) {
2371 localfilepathlen = 0;
2372 goto localfileerror;
2373 }
2374 localfilepath[localfilepathlen++] = '/';
2375 }
2376
2377 len = strlen(curproxy->server_state_file_name);
2378 if (localfilepathlen + len > MAXPATHLEN) {
2379 localfilepathlen = 0;
2380 goto localfileerror;
2381 }
2382 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2383 localfilepathlen += len;
2384 localfilepath[localfilepathlen++] = 0;
2385 }
2386 filepath = localfilepath;
2387 localfileerror:
2388 if (localfilepathlen == 0)
2389 localfilepath[0] = '\0';
2390
2391 break;
2392 case PR_SRV_STATE_FILE_NONE:
2393 default:
2394 continue;
2395 }
2396
2397 /* preload global state file */
2398 errno = 0;
2399 f = fopen(filepath, "r");
2400 if (errno && fileopenerr)
2401 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2402 if (!f)
2403 continue;
2404
2405 mybuf[0] = '\0';
2406 mybuflen = 0;
2407 version = 0;
2408
2409 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002410 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2411 Warning("Can't read first line of the server state file '%s'\n", filepath);
2412 goto fileclose;
2413 }
2414
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002415 cur = mybuf;
2416 version = atoi(cur);
2417 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2418 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002419 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002420
2421 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2422 int bk_f_forced_id = 0;
2423 int check_id = 0;
2424 int check_name = 0;
2425
2426 mybuflen = strlen(mybuf);
2427 cur = mybuf;
2428 end = cur + mybuflen;
2429
2430 bk = NULL;
2431 srv = NULL;
2432
2433 /* we need at least one character */
2434 if (mybuflen == 0)
2435 continue;
2436
2437 /* ignore blank characters at the beginning of the line */
2438 while (isspace(*cur))
2439 ++cur;
2440
2441 if (cur == end)
2442 continue;
2443
2444 /* ignore comment line */
2445 if (*cur == '#')
2446 continue;
2447
2448 /* we're now ready to move the line into *srv_params[] */
2449 params[0] = cur;
2450 arg = 1;
2451 srv_arg = 0;
2452 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2453 if (isspace(*cur)) {
2454 *cur = '\0';
2455 ++cur;
2456 while (isspace(*cur))
2457 ++cur;
2458 switch (version) {
2459 case 1:
2460 /*
2461 * srv_addr: params[4] => srv_params[0]
2462 * srv_op_state: params[5] => srv_params[1]
2463 * srv_admin_state: params[6] => srv_params[2]
2464 * srv_uweight: params[7] => srv_params[3]
2465 * srv_iweight: params[8] => srv_params[4]
2466 * srv_last_time_change: params[9] => srv_params[5]
2467 * srv_check_status: params[10] => srv_params[6]
2468 * srv_check_result: params[11] => srv_params[7]
2469 * srv_check_health: params[12] => srv_params[8]
2470 * srv_check_state: params[13] => srv_params[9]
2471 * srv_agent_state: params[14] => srv_params[10]
2472 * bk_f_forced_id: params[15] => srv_params[11]
2473 * srv_f_forced_id: params[16] => srv_params[12]
2474 */
2475 if (arg >= 4) {
2476 srv_params[srv_arg] = cur;
2477 ++srv_arg;
2478 }
2479 break;
2480 }
2481
2482 params[arg] = cur;
2483 ++arg;
2484 }
2485 else {
2486 ++cur;
2487 }
2488 }
2489
2490 /* if line is incomplete line, then ignore it.
2491 * otherwise, update useful flags */
2492 switch (version) {
2493 case 1:
2494 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2495 continue;
2496 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2497 check_id = (atoi(params[0]) == curproxy->uuid);
2498 check_name = (strcmp(curproxy->id, params[1]) == 0);
2499 break;
2500 }
2501
2502 diff = 0;
2503 bk = curproxy;
2504
2505 /* if backend can't be found, let's continue */
2506 if (!check_id && !check_name)
2507 continue;
2508 else if (!check_id && check_name) {
2509 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2510 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2511 }
2512 else if (check_id && !check_name) {
2513 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2514 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2515 /* if name doesn't match, we still want to update curproxy if the backend id
2516 * was forced in previous the previous configuration */
2517 if (!bk_f_forced_id)
2518 continue;
2519 }
2520
2521 /* look for the server by its id: param[2] */
2522 /* else look for the server by its name: param[3] */
2523 diff = 0;
2524 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2525
2526 if (!srv) {
2527 /* if no server found, then warning and continue with next line */
2528 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2529 params[3], params[2], params[0], params[1]);
2530 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2531 params[3], params[2], params[0], params[1]);
2532 continue;
2533 }
2534 else if (diff & PR_FBM_MISMATCH_ID) {
2535 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);
2536 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);
2537 }
2538 else if (diff & PR_FBM_MISMATCH_NAME) {
2539 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);
2540 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);
2541 }
2542
2543 /* now we can proceed with server's state update */
2544 srv_update_state(srv, version, srv_params);
2545 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002546fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002547 fclose(f);
2548 }
2549}
2550
Simon Horman7d09b9a2013-02-12 10:45:51 +09002551/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002552 * update a server's current IP address.
2553 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2554 * ip is in network format.
2555 * updater is a string which contains an information about the requester of the update.
2556 * updater is used if not NULL.
2557 *
2558 * A log line and a stderr warning message is generated based on server's backend options.
2559 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002560int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002561{
2562 /* generates a log line and a warning on stderr */
2563 if (1) {
2564 /* book enough space for both IPv4 and IPv6 */
2565 char oldip[INET6_ADDRSTRLEN];
2566 char newip[INET6_ADDRSTRLEN];
2567
2568 memset(oldip, '\0', INET6_ADDRSTRLEN);
2569 memset(newip, '\0', INET6_ADDRSTRLEN);
2570
2571 /* copy old IP address in a string */
2572 switch (s->addr.ss_family) {
2573 case AF_INET:
2574 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2575 break;
2576 case AF_INET6:
2577 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2578 break;
2579 };
2580
2581 /* copy new IP address in a string */
2582 switch (ip_sin_family) {
2583 case AF_INET:
2584 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2585 break;
2586 case AF_INET6:
2587 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2588 break;
2589 };
2590
2591 /* save log line into a buffer */
2592 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2593 s->proxy->id, s->id, oldip, newip, updater);
2594
2595 /* write the buffer on stderr */
2596 Warning("%s.\n", trash.str);
2597
2598 /* send a log */
2599 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2600 }
2601
2602 /* save the new IP family */
2603 s->addr.ss_family = ip_sin_family;
2604 /* save the new IP address */
2605 switch (ip_sin_family) {
2606 case AF_INET:
2607 ((struct sockaddr_in *)&s->addr)->sin_addr.s_addr = *(uint32_t *)ip;
2608 break;
2609 case AF_INET6:
2610 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2611 break;
2612 };
2613
2614 return 0;
2615}
2616
2617/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002618 * update server status based on result of name resolution
2619 * returns:
2620 * 0 if server status is updated
2621 * 1 if server status has not changed
2622 */
2623int snr_update_srv_status(struct server *s)
2624{
2625 struct dns_resolution *resolution = s->resolution;
2626
2627 switch (resolution->status) {
2628 case RSLV_STATUS_NONE:
2629 /* status when HAProxy has just (re)started */
2630 trigger_resolution(s);
2631 break;
2632
2633 default:
2634 break;
2635 }
2636
2637 return 1;
2638}
2639
2640/*
2641 * Server Name Resolution valid response callback
2642 * It expects:
2643 * - <nameserver>: the name server which answered the valid response
2644 * - <response>: buffer containing a valid DNS response
2645 * - <response_len>: size of <response>
2646 * It performs the following actions:
2647 * - ignore response if current ip found and server family not met
2648 * - update with first new ip found if family is met and current IP is not found
2649 * returns:
2650 * 0 on error
2651 * 1 when no error or safe ignore
2652 */
2653int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, unsigned char *response, int response_len)
2654{
2655 struct server *s;
2656 void *serverip, *firstip;
2657 short server_sin_family, firstip_sin_family;
2658 unsigned char *response_end;
2659 int ret;
2660 struct chunk *chk = get_trash_chunk();
2661
2662 /* initializing variables */
2663 response_end = response + response_len; /* pointer to mark the end of the response */
2664 firstip = NULL; /* pointer to the first valid response found */
2665 /* it will be used as the new IP if a change is required */
2666 firstip_sin_family = AF_UNSPEC;
2667 serverip = NULL; /* current server IP address */
2668
2669 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002670 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002671
2672 /* initializing server IP pointer */
2673 server_sin_family = s->addr.ss_family;
2674 switch (server_sin_family) {
2675 case AF_INET:
2676 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
2677 break;
2678
2679 case AF_INET6:
2680 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
2681 break;
2682
2683 default:
2684 goto invalid;
2685 }
2686
Thierry Fournierada34842016-02-17 21:25:09 +01002687 ret = dns_get_ip_from_response(response, response_end, resolution,
2688 serverip, server_sin_family, &firstip,
2689 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002690
2691 switch (ret) {
2692 case DNS_UPD_NO:
2693 if (resolution->status != RSLV_STATUS_VALID) {
2694 resolution->status = RSLV_STATUS_VALID;
2695 resolution->last_status_change = now_ms;
2696 }
2697 goto stop_resolution;
2698
2699 case DNS_UPD_SRVIP_NOT_FOUND:
2700 goto save_ip;
2701
2702 case DNS_UPD_CNAME:
2703 if (resolution->status != RSLV_STATUS_VALID) {
2704 resolution->status = RSLV_STATUS_VALID;
2705 resolution->last_status_change = now_ms;
2706 }
2707 goto invalid;
2708
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02002709 case DNS_UPD_NO_IP_FOUND:
2710 if (resolution->status != RSLV_STATUS_OTHER) {
2711 resolution->status = RSLV_STATUS_OTHER;
2712 resolution->last_status_change = now_ms;
2713 }
2714 goto stop_resolution;
2715
Baptiste Assmannfad03182015-10-28 02:03:32 +01002716 case DNS_UPD_NAME_ERROR:
2717 /* if this is not the last expected response, we ignore it */
2718 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
2719 return 0;
2720 /* update resolution status to OTHER error type */
2721 if (resolution->status != RSLV_STATUS_OTHER) {
2722 resolution->status = RSLV_STATUS_OTHER;
2723 resolution->last_status_change = now_ms;
2724 }
2725 goto stop_resolution;
2726
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002727 default:
2728 goto invalid;
2729
2730 }
2731
2732 save_ip:
2733 nameserver->counters.update += 1;
2734 if (resolution->status != RSLV_STATUS_VALID) {
2735 resolution->status = RSLV_STATUS_VALID;
2736 resolution->last_status_change = now_ms;
2737 }
2738
2739 /* save the first ip we found */
2740 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
2741 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
2742
2743 stop_resolution:
2744 /* update last resolution date and time */
2745 resolution->last_resolution = now_ms;
2746 /* reset current status flag */
2747 resolution->step = RSLV_STEP_NONE;
2748 /* reset values */
2749 dns_reset_resolution(resolution);
2750
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002751 dns_update_resolvers_timeout(nameserver->resolvers);
2752
2753 snr_update_srv_status(s);
2754 return 0;
2755
2756 invalid:
2757 nameserver->counters.invalid += 1;
2758 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
2759 goto stop_resolution;
2760
2761 snr_update_srv_status(s);
2762 return 0;
2763}
2764
2765/*
2766 * Server Name Resolution error management callback
2767 * returns:
2768 * 0 on error
2769 * 1 when no error or safe ignore
2770 */
2771int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
2772{
2773 struct server *s;
2774 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002775 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002776
2777 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002778 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002779 resolvers = resolution->resolvers;
2780
2781 /* can be ignored if this is not the last response */
2782 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
2783 return 1;
2784 }
2785
2786 switch (error_code) {
2787 case DNS_RESP_INVALID:
2788 case DNS_RESP_WRONG_NAME:
2789 if (resolution->status != RSLV_STATUS_INVALID) {
2790 resolution->status = RSLV_STATUS_INVALID;
2791 resolution->last_status_change = now_ms;
2792 }
2793 break;
2794
2795 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02002796 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002797 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02002798 case DNS_RESP_NO_EXPECTED_RECORD:
Thierry Fournierada34842016-02-17 21:25:09 +01002799 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
2800 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02002801
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002802 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002803 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002804 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002805 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02002806 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002807 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02002808 }
2809 else if (res_preferred_afinet) {
2810 /* fallback from A to AAAA */
2811 resolution->query_type = DNS_RTYPE_AAAA;
2812 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002813 else {
2814 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002815 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002816 resolution->query_type = DNS_RTYPE_A;
2817 } else {
2818 resolution->query_type = DNS_RTYPE_AAAA;
2819 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002820 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002821
2822 dns_send_query(resolution);
2823
2824 /*
2825 * move the resolution to the last element of the FIFO queue
2826 * and update timeout wakeup based on the new first entry
2827 */
2828 if (dns_check_resolution_queue(resolvers) > 1) {
2829 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02002830 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002831 /* ex first resolution goes to the end of the queue */
2832 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
2833 }
2834 dns_update_resolvers_timeout(resolvers);
2835 goto leave;
2836 }
2837 else {
2838 if (resolution->status != RSLV_STATUS_OTHER) {
2839 resolution->status = RSLV_STATUS_OTHER;
2840 resolution->last_status_change = now_ms;
2841 }
2842 }
2843 break;
2844
2845 case DNS_RESP_NX_DOMAIN:
2846 if (resolution->status != RSLV_STATUS_NX) {
2847 resolution->status = RSLV_STATUS_NX;
2848 resolution->last_status_change = now_ms;
2849 }
2850 break;
2851
2852 case DNS_RESP_REFUSED:
2853 if (resolution->status != RSLV_STATUS_REFUSED) {
2854 resolution->status = RSLV_STATUS_REFUSED;
2855 resolution->last_status_change = now_ms;
2856 }
2857 break;
2858
2859 case DNS_RESP_CNAME_ERROR:
2860 break;
2861
2862 case DNS_RESP_TIMEOUT:
2863 if (resolution->status != RSLV_STATUS_TIMEOUT) {
2864 resolution->status = RSLV_STATUS_TIMEOUT;
2865 resolution->last_status_change = now_ms;
2866 }
2867 break;
2868 }
2869
2870 /* update last resolution date and time */
2871 resolution->last_resolution = now_ms;
2872 /* reset current status flag */
2873 resolution->step = RSLV_STEP_NONE;
2874 /* reset values */
2875 dns_reset_resolution(resolution);
2876
2877 LIST_DEL(&resolution->list);
2878 dns_update_resolvers_timeout(resolvers);
2879
2880 leave:
2881 snr_update_srv_status(s);
2882 return 1;
2883}
2884
2885/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886 * Local variables:
2887 * c-indent-level: 8
2888 * c-basic-offset: 8
2889 * End:
2890 */