blob: ee52903f6e889990508d96c644d675f55073d506 [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>
15
16#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020018#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010019#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020020#include <common/time.h>
21
Willy Tarreau272adea2014-03-31 10:39:59 +020022#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020023#include <types/dns.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020024
Simon Hormanb1900d52015-01-30 11:22:54 +090025#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020026#include <proto/port_range.h>
27#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020028#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020030#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020031#include <proto/stream.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020032#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020033#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020034
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreau21faa912012-10-10 08:27:36 +020036/* List head of all known server keywords */
37static struct srv_kw_list srv_keywords = {
38 .list = LIST_HEAD_INIT(srv_keywords.list)
39};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020040
Simon Hormana3608442013-11-01 16:46:15 +090041int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020042{
Willy Tarreau892337c2014-05-13 23:41:20 +020043 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020044 return s->down_time;
45
46 return now.tv_sec - s->last_change + s->down_time;
47}
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050049int srv_lastsession(const struct server *s)
50{
51 if (s->counters.last_sess)
52 return now.tv_sec - s->counters.last_sess;
53
54 return -1;
55}
56
Simon Horman4a741432013-02-23 15:35:38 +090057int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020058{
Simon Horman4a741432013-02-23 15:35:38 +090059 const struct server *s = check->server;
60
Willy Tarreauff5ae352013-12-11 20:36:34 +010061 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090062 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010063
Willy Tarreau892337c2014-05-13 23:41:20 +020064 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090065 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010066
Simon Horman4a741432013-02-23 15:35:38 +090067 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010068}
69
Willy Tarreau21faa912012-10-10 08:27:36 +020070/*
71 * Registers the server keyword list <kwl> as a list of valid keywords for next
72 * parsing sessions.
73 */
74void srv_register_keywords(struct srv_kw_list *kwl)
75{
76 LIST_ADDQ(&srv_keywords.list, &kwl->list);
77}
78
79/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
80 * keyword is found with a NULL ->parse() function, then an attempt is made to
81 * find one with a valid ->parse() function. This way it is possible to declare
82 * platform-dependant, known keywords as NULL, then only declare them as valid
83 * if some options are met. Note that if the requested keyword contains an
84 * opening parenthesis, everything from this point is ignored.
85 */
86struct srv_kw *srv_find_kw(const char *kw)
87{
88 int index;
89 const char *kwend;
90 struct srv_kw_list *kwl;
91 struct srv_kw *ret = NULL;
92
93 kwend = strchr(kw, '(');
94 if (!kwend)
95 kwend = kw + strlen(kw);
96
97 list_for_each_entry(kwl, &srv_keywords.list, list) {
98 for (index = 0; kwl->kw[index].kw != NULL; index++) {
99 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
100 kwl->kw[index].kw[kwend-kw] == 0) {
101 if (kwl->kw[index].parse)
102 return &kwl->kw[index]; /* found it !*/
103 else
104 ret = &kwl->kw[index]; /* may be OK */
105 }
106 }
107 }
108 return ret;
109}
110
111/* Dumps all registered "server" keywords to the <out> string pointer. The
112 * unsupported keywords are only dumped if their supported form was not
113 * found.
114 */
115void srv_dump_kws(char **out)
116{
117 struct srv_kw_list *kwl;
118 int index;
119
120 *out = NULL;
121 list_for_each_entry(kwl, &srv_keywords.list, list) {
122 for (index = 0; kwl->kw[index].kw != NULL; index++) {
123 if (kwl->kw[index].parse ||
124 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
125 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
126 kwl->scope,
127 kwl->kw[index].kw,
128 kwl->kw[index].skip ? " <arg>" : "",
129 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
130 kwl->kw[index].parse ? "" : " (not supported)");
131 }
132 }
133 }
134}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100135
Willy Tarreaudff55432012-10-10 17:51:05 +0200136/* parse the "id" server keyword */
137static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
138{
139 struct eb32_node *node;
140
141 if (!*args[*cur_arg + 1]) {
142 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
143 return ERR_ALERT | ERR_FATAL;
144 }
145
146 newsrv->puid = atol(args[*cur_arg + 1]);
147 newsrv->conf.id.key = newsrv->puid;
148
149 if (newsrv->puid <= 0) {
150 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
151 return ERR_ALERT | ERR_FATAL;
152 }
153
154 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
155 if (node) {
156 struct server *target = container_of(node, struct server, conf.id);
157 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
158 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
159 target->id);
160 return ERR_ALERT | ERR_FATAL;
161 }
162
163 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
164 return 0;
165}
166
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200167/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200168 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200169 * shutdown.
170 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200171void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200172{
Willy Tarreau87b09662015-04-03 00:22:06 +0200173 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200174
Willy Tarreau87b09662015-04-03 00:22:06 +0200175 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
176 if (stream->srv_conn == srv)
177 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200178}
179
180/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200181 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200182 * the reason for the shutdown.
183 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200184void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200185{
186 struct server *srv;
187
188 for (srv = px->srv; srv != NULL; srv = srv->next)
189 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200190 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200191}
192
Willy Tarreaubda92272014-05-20 21:55:30 +0200193/* Appends some information to a message string related to a server going UP or
194 * DOWN. If both <forced> and <reason> are null and the server tracks another
195 * one, a "via" information will be provided to know where the status came from.
196 * If <reason> is non-null, the entire string will be appended after a comma and
197 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200198 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200199 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200200 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200201void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200202{
Willy Tarreaubda92272014-05-20 21:55:30 +0200203 if (reason)
204 chunk_appendf(msg, ", %s", reason);
205 else if (!forced && s->track)
206 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200207
208 if (xferred >= 0) {
209 if (s->state == SRV_ST_STOPPED)
210 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
211 " %d sessions active, %d requeued, %d remaining in queue",
212 s->proxy->srv_act, s->proxy->srv_bck,
213 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
214 s->cur_sess, xferred, s->nbpend);
215 else
216 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
217 " %d sessions requeued, %d total in queue",
218 s->proxy->srv_act, s->proxy->srv_bck,
219 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
220 xferred, s->nbpend);
221 }
222}
223
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200224/* Marks server <s> down, regardless of its checks' statuses, notifies by all
225 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200226 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200227 * recomputes the number of servers, but not the map. Maintenance servers are
228 * ignored. It reports <reason> if non-null as the reason for going down. Note
229 * that it makes use of the trash to build the log strings, so <reason> must
230 * not be placed there.
231 */
232void srv_set_stopped(struct server *s, const char *reason)
233{
234 struct server *srv;
235 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
236 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900237 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200238 int xferred;
239
240 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
241 return;
242
243 s->last_change = now.tv_sec;
244 s->state = SRV_ST_STOPPED;
245 if (s->proxy->lbprm.set_server_status_down)
246 s->proxy->lbprm.set_server_status_down(s);
247
248 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200249 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200250
Willy Tarreau87b09662015-04-03 00:22:06 +0200251 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200252 * a connection. Those which are redispatchable will be queued
253 * to another server or to the proxy itself.
254 */
255 xferred = pendconn_redistribute(s);
256
257 chunk_printf(&trash,
258 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
259 s->proxy->id, s->id);
260
261 srv_append_status(&trash, s, reason, xferred, 0);
262 Warning("%s.\n", trash.str);
263
264 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900265 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
266 send_log(s->proxy, log_level, "%s.\n", trash.str);
267 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200268
269 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
270 set_backend_down(s->proxy);
271
272 s->counters.down_trans++;
273
274 for (srv = s->trackers; srv; srv = srv->tracknext)
275 srv_set_stopped(srv, NULL);
276}
277
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200278/* Marks server <s> up regardless of its checks' statuses and provided it isn't
279 * in maintenance. Notifies by all available means, recounts the remaining
280 * servers on the proxy and tries to grab requests from the proxy. It
281 * automatically recomputes the number of servers, but not the map. Maintenance
282 * servers are ignored. It reports <reason> if non-null as the reason for going
283 * up. Note that it makes use of the trash to build the log strings, so <reason>
284 * must not be placed there.
285 */
286void srv_set_running(struct server *s, const char *reason)
287{
288 struct server *srv;
289 int xferred;
290
291 if (s->admin & SRV_ADMF_MAINT)
292 return;
293
294 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
295 return;
296
297 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
298 if (s->proxy->last_change < now.tv_sec) // ignore negative times
299 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
300 s->proxy->last_change = now.tv_sec;
301 }
302
303 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
304 s->down_time += now.tv_sec - s->last_change;
305
306 s->last_change = now.tv_sec;
307
308 s->state = SRV_ST_STARTING;
309 if (s->slowstart > 0)
310 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
311 else
312 s->state = SRV_ST_RUNNING;
313
314 server_recalc_eweight(s);
315
316 /* If the server is set with "on-marked-up shutdown-backup-sessions",
317 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200318 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200319 * on all backup servers.
320 */
321 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
322 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200323 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200324
325 /* check if we can handle some connections queued at the proxy. We
326 * will take as many as we can handle.
327 */
328 xferred = pendconn_grab_from_px(s);
329
330 chunk_printf(&trash,
331 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
332 s->proxy->id, s->id);
333
334 srv_append_status(&trash, s, reason, xferred, 0);
335 Warning("%s.\n", trash.str);
336 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900337 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200338
339 for (srv = s->trackers; srv; srv = srv->tracknext)
340 srv_set_running(srv, NULL);
341}
342
Willy Tarreau8eb77842014-05-21 13:54:57 +0200343/* Marks server <s> stopping regardless of its checks' statuses and provided it
344 * isn't in maintenance. Notifies by all available means, recounts the remaining
345 * servers on the proxy and tries to grab requests from the proxy. It
346 * automatically recomputes the number of servers, but not the map. Maintenance
347 * servers are ignored. It reports <reason> if non-null as the reason for going
348 * up. Note that it makes use of the trash to build the log strings, so <reason>
349 * must not be placed there.
350 */
351void srv_set_stopping(struct server *s, const char *reason)
352{
353 struct server *srv;
354 int xferred;
355
356 if (s->admin & SRV_ADMF_MAINT)
357 return;
358
359 if (s->state == SRV_ST_STOPPING)
360 return;
361
362 s->last_change = now.tv_sec;
363 s->state = SRV_ST_STOPPING;
364 if (s->proxy->lbprm.set_server_status_down)
365 s->proxy->lbprm.set_server_status_down(s);
366
Willy Tarreau87b09662015-04-03 00:22:06 +0200367 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200368 * a connection. Those which are redispatchable will be queued
369 * to another server or to the proxy itself.
370 */
371 xferred = pendconn_redistribute(s);
372
373 chunk_printf(&trash,
374 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
375 s->proxy->id, s->id);
376
377 srv_append_status(&trash, s, reason, xferred, 0);
378
379 Warning("%s.\n", trash.str);
380 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
381
382 if (!s->proxy->srv_bck && !s->proxy->srv_act)
383 set_backend_down(s->proxy);
384
385 for (srv = s->trackers; srv; srv = srv->tracknext)
386 srv_set_stopping(srv, NULL);
387}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200388
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200389/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
390 * enforce either maint mode or drain mode. It is not allowed to set more than
391 * one flag at once. The equivalent "inherited" flag is propagated to all
392 * tracking servers. Maintenance mode disables health checks (but not agent
393 * checks). When either the flag is already set or no flag is passed, nothing
394 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200395 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200396void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200397{
398 struct check *check = &s->check;
399 struct server *srv;
400 int xferred;
401
402 if (!mode)
403 return;
404
405 /* stop going down as soon as we meet a server already in the same state */
406 if (s->admin & mode)
407 return;
408
409 s->admin |= mode;
410
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200411 /* stop going down if the equivalent flag was already present (forced or inherited) */
412 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
413 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
414 return;
415
416 /* Maintenance must also disable health checks */
417 if (mode & SRV_ADMF_MAINT) {
418 if (s->check.state & CHK_ST_ENABLED) {
419 s->check.state |= CHK_ST_PAUSED;
420 check->health = 0;
421 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200422
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200423 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200424 chunk_printf(&trash,
425 "%sServer %s/%s was DOWN and now enters maintenance",
426 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
427
Willy Tarreaubda92272014-05-20 21:55:30 +0200428 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200429
430 Warning("%s.\n", trash.str);
431 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
432 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200433 else { /* server was still running */
434 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
435 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
436
437 check->health = 0; /* failure */
438 s->last_change = now.tv_sec;
439 s->state = SRV_ST_STOPPED;
440 if (s->proxy->lbprm.set_server_status_down)
441 s->proxy->lbprm.set_server_status_down(s);
442
443 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200444 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200445
Willy Tarreau87b09662015-04-03 00:22:06 +0200446 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200447 * a connection. Those which are redispatchable will be queued
448 * to another server or to the proxy itself.
449 */
450 xferred = pendconn_redistribute(s);
451
452 chunk_printf(&trash,
453 "%sServer %s/%s is going DOWN for maintenance",
454 s->flags & SRV_F_BACKUP ? "Backup " : "",
455 s->proxy->id, s->id);
456
457 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
458
459 Warning("%s.\n", trash.str);
460 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
461
462 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
463 set_backend_down(s->proxy);
464
465 s->counters.down_trans++;
466 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200467 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200468
469 /* drain state is applied only if not yet in maint */
470 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200471 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
472
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200473 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200474 if (s->proxy->lbprm.set_server_status_down)
475 s->proxy->lbprm.set_server_status_down(s);
476
Willy Tarreau87b09662015-04-03 00:22:06 +0200477 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200478 * a connection. Those which are redispatchable will be queued
479 * to another server or to the proxy itself.
480 */
481 xferred = pendconn_redistribute(s);
482
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200483 chunk_printf(&trash, "%sServer %s/%s enters drain state",
484 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200485
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200486 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200487
488 Warning("%s.\n", trash.str);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200489 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900490 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200491
492 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
493 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200494 }
495
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200496 /* compute the inherited flag to propagate */
497 if (mode & SRV_ADMF_MAINT)
498 mode = SRV_ADMF_IMAINT;
499 else if (mode & SRV_ADMF_DRAIN)
500 mode = SRV_ADMF_IDRAIN;
501
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200502 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200503 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200504}
505
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200506/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
507 * stop enforcing either maint mode or drain mode. It is not allowed to set more
508 * than one flag at once. The equivalent "inherited" flag is propagated to all
509 * tracking servers. Leaving maintenance mode re-enables health checks. When
510 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200511 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200512void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200513{
514 struct check *check = &s->check;
515 struct server *srv;
516 int xferred = -1;
517
518 if (!mode)
519 return;
520
521 /* stop going down as soon as we see the flag is not there anymore */
522 if (!(s->admin & mode))
523 return;
524
525 s->admin &= ~mode;
526
527 if (s->admin & SRV_ADMF_MAINT) {
528 /* remaining in maintenance mode, let's inform precisely about the
529 * situation.
530 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200531 if (mode & SRV_ADMF_FMAINT) {
532 chunk_printf(&trash,
533 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
534 s->flags & SRV_F_BACKUP ? "Backup " : "",
535 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200536
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200537 if (s->track) /* normally it's mandatory here */
538 chunk_appendf(&trash, " via %s/%s",
539 s->track->proxy->id, s->track->id);
540 Warning("%s.\n", trash.str);
541 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
542 }
543 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200544 chunk_printf(&trash,
545 "%sServer %s/%s remains in forced maintenance",
546 s->flags & SRV_F_BACKUP ? "Backup " : "",
547 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200548 Warning("%s.\n", trash.str);
549 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
550 }
551 /* don't report anything when leaving drain mode and remaining in maintenance */
552 }
553 else if (mode & SRV_ADMF_MAINT) {
554 /* OK here we're leaving maintenance, we have many things to check,
555 * because the server might possibly be coming back up depending on
556 * its state. In practice, leaving maintenance means that we should
557 * immediately turn to UP (more or less the slowstart) under the
558 * following conditions :
559 * - server is neither checked nor tracked
560 * - server tracks another server which is not checked
561 * - server tracks another server which is already up
562 * Which sums up as something simpler :
563 * "either the tracking server is up or the server's checks are disabled
564 * or up". Otherwise we only re-enable health checks. There's a special
565 * case associated to the stopping state which can be inherited. Note
566 * that the server might still be in drain mode, which is naturally dealt
567 * with by the lower level functions.
568 */
569
570 if (s->check.state & CHK_ST_ENABLED) {
571 s->check.state &= ~CHK_ST_PAUSED;
572 check->health = check->rise; /* start OK but check immediately */
573 }
574
575 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
576 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
577 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
578 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
579 if (s->proxy->last_change < now.tv_sec) // ignore negative times
580 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
581 s->proxy->last_change = now.tv_sec;
582 }
583
584 if (s->last_change < now.tv_sec) // ignore negative times
585 s->down_time += now.tv_sec - s->last_change;
586 s->last_change = now.tv_sec;
587
588 if (s->track && s->track->state == SRV_ST_STOPPING)
589 s->state = SRV_ST_STOPPING;
590 else {
591 s->state = SRV_ST_STARTING;
592 if (s->slowstart > 0)
593 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
594 else
595 s->state = SRV_ST_RUNNING;
596 }
597
598 server_recalc_eweight(s);
599
600 /* If the server is set with "on-marked-up shutdown-backup-sessions",
601 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200602 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200603 * on all backup servers.
604 */
605 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
606 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200607 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200608
609 /* check if we can handle some connections queued at the proxy. We
610 * will take as many as we can handle.
611 */
612 xferred = pendconn_grab_from_px(s);
613 }
614
615 if (mode & SRV_ADMF_FMAINT) {
616 chunk_printf(&trash,
617 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
618 s->flags & SRV_F_BACKUP ? "Backup " : "",
619 s->proxy->id, s->id,
620 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
621 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200622 }
623 else {
624 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200625 "%sServer %s/%s is %s/%s (leaving maintenance)",
626 s->flags & SRV_F_BACKUP ? "Backup " : "",
627 s->proxy->id, s->id,
628 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
629 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
630 srv_append_status(&trash, s, NULL, xferred, 0);
631 }
632 Warning("%s.\n", trash.str);
633 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
634 }
635 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
636 /* remaining in drain mode after removing one of its flags */
637
638 if (mode & SRV_ADMF_FDRAIN) {
639 chunk_printf(&trash,
640 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200641 s->flags & SRV_F_BACKUP ? "Backup " : "",
642 s->proxy->id, s->id);
643
644 if (s->track) /* normally it's mandatory here */
645 chunk_appendf(&trash, " via %s/%s",
646 s->track->proxy->id, s->track->id);
647 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200648 else {
649 chunk_printf(&trash,
650 "%sServer %s/%s remains in forced drain mode",
651 s->flags & SRV_F_BACKUP ? "Backup " : "",
652 s->proxy->id, s->id);
653 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200654 Warning("%s.\n", trash.str);
655 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200656 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200657 else if (mode & SRV_ADMF_DRAIN) {
658 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200659 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
660 if (s->proxy->last_change < now.tv_sec) // ignore negative times
661 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
662 s->proxy->last_change = now.tv_sec;
663 }
664
665 if (s->last_change < now.tv_sec) // ignore negative times
666 s->down_time += now.tv_sec - s->last_change;
667 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200668 server_recalc_eweight(s);
669
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200670 if (mode & SRV_ADMF_FDRAIN) {
671 chunk_printf(&trash,
672 "%sServer %s/%s is %s (leaving forced drain)",
673 s->flags & SRV_F_BACKUP ? "Backup " : "",
674 s->proxy->id, s->id,
675 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
676 }
677 else {
678 chunk_printf(&trash,
679 "%sServer %s/%s is %s (leaving drain)",
680 s->flags & SRV_F_BACKUP ? "Backup " : "",
681 s->proxy->id, s->id,
682 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
683 if (s->track) /* normally it's mandatory here */
684 chunk_appendf(&trash, " via %s/%s",
685 s->track->proxy->id, s->track->id);
686 }
687 Warning("%s.\n", trash.str);
688 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200689 }
690
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200691 /* stop going down if the equivalent flag is still present (forced or inherited) */
692 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
693 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
694 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200695
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200696 if (mode & SRV_ADMF_MAINT)
697 mode = SRV_ADMF_IMAINT;
698 else if (mode & SRV_ADMF_DRAIN)
699 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200700
701 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200702 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200703}
704
Willy Tarreaudff55432012-10-10 17:51:05 +0200705/* Note: must not be declared <const> as its list will be overwritten.
706 * Please take care of keeping this list alphabetically sorted, doing so helps
707 * all code contributors.
708 * Optional keywords are also declared with a NULL ->parse() function so that
709 * the config parser can report an appropriate error when a known keyword was
710 * not enabled.
711 */
712static struct srv_kw_list srv_kws = { "ALL", { }, {
713 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
714 { NULL, NULL, 0 },
715}};
716
717__attribute__((constructor))
718static void __listener_init(void)
719{
720 srv_register_keywords(&srv_kws);
721}
722
Willy Tarreau004e0452013-11-21 11:22:01 +0100723/* Recomputes the server's eweight based on its state, uweight, the current time,
724 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
725 * state is automatically disabled if the time is elapsed.
726 */
727void server_recalc_eweight(struct server *sv)
728{
729 struct proxy *px = sv->proxy;
730 unsigned w;
731
732 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
733 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200734 if (sv->state == SRV_ST_STARTING)
735 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100736 }
737
738 /* We must take care of not pushing the server to full throttle during slow starts.
739 * It must also start immediately, at least at the minimal step when leaving maintenance.
740 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200741 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100742 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
743 else
744 w = px->lbprm.wdiv;
745
746 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
747
748 /* now propagate the status change to any LB algorithms */
749 if (px->lbprm.update_server_eweight)
750 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200751 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100752 if (px->lbprm.set_server_status_up)
753 px->lbprm.set_server_status_up(sv);
754 }
755 else {
756 if (px->lbprm.set_server_status_down)
757 px->lbprm.set_server_status_down(sv);
758 }
759}
760
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900762 * Parses weight_str and configures sv accordingly.
763 * Returns NULL on success, error message string otherwise.
764 */
765const char *server_parse_weight_change_request(struct server *sv,
766 const char *weight_str)
767{
768 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900769 long int w;
770 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900771
772 px = sv->proxy;
773
774 /* if the weight is terminated with '%', it is set relative to
775 * the initial weight, otherwise it is absolute.
776 */
777 if (!*weight_str)
778 return "Require <weight> or <weight%>.\n";
779
Simon Hormanb796afa2013-02-12 10:45:53 +0900780 w = strtol(weight_str, &end, 10);
781 if (end == weight_str)
782 return "Empty weight string empty or preceded by garbage";
783 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900784 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900785 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900786 /* Avoid integer overflow */
787 if (w > 25600)
788 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900789 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900790 if (w > 256)
791 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900792 }
793 else if (w < 0 || w > 256)
794 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900795 else if (end[0] != '\0')
796 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900797
798 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
799 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
800
801 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100802 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900803
804 return NULL;
805}
806
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200807/*
808 * Parses <addr_str> and configures <sv> accordingly.
809 * Returns:
810 * - error string on error
811 * - NULL on success
812 */
813const char *server_parse_addr_change_request(struct server *sv,
814 const char *addr_str)
815{
816 unsigned char ip[INET6_ADDRSTRLEN];
817
818 if (inet_pton(AF_INET6, addr_str, ip)) {
819 update_server_addr(sv, ip, AF_INET6, "stats command\n");
820 return NULL;
821 }
822 if (inet_pton(AF_INET, addr_str, ip)) {
823 update_server_addr(sv, ip, AF_INET, "stats command\n");
824 return NULL;
825 }
826
827 return "Could not understand IP address format.\n";
828}
829
Willy Tarreau272adea2014-03-31 10:39:59 +0200830int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
831{
832 struct server *newsrv = NULL;
833 const char *err;
834 char *errmsg = NULL;
835 int err_code = 0;
836 unsigned val;
837
838 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
839 int cur_arg;
840 short realport = 0;
841 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
842
843 if (!defsrv && curproxy == defproxy) {
844 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
845 err_code |= ERR_ALERT | ERR_FATAL;
846 goto out;
847 }
848 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
849 err_code |= ERR_ALERT | ERR_FATAL;
850
851 if (!*args[2]) {
852 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
853 file, linenum, args[0]);
854 err_code |= ERR_ALERT | ERR_FATAL;
855 goto out;
856 }
857
858 err = invalid_char(args[1]);
859 if (err && !defsrv) {
860 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
861 file, linenum, *err, args[1]);
862 err_code |= ERR_ALERT | ERR_FATAL;
863 goto out;
864 }
865
866 if (!defsrv) {
867 struct sockaddr_storage *sk;
868 int port1, port2;
869 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200870 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +0200871
872 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
873 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
874 err_code |= ERR_ALERT | ERR_ABORT;
875 goto out;
876 }
877
878 /* the servers are linked backwards first */
879 newsrv->next = curproxy->srv;
880 curproxy->srv = newsrv;
881 newsrv->proxy = curproxy;
882 newsrv->conf.file = strdup(file);
883 newsrv->conf.line = linenum;
884
885 newsrv->obj_type = OBJ_TYPE_SERVER;
886 LIST_INIT(&newsrv->actconns);
887 LIST_INIT(&newsrv->pendconns);
888 do_check = 0;
889 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200890 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200891 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200892 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200893 newsrv->last_change = now.tv_sec;
894 newsrv->id = strdup(args[1]);
895
896 /* several ways to check the port component :
897 * - IP => port=+0, relative (IPv4 only)
898 * - IP: => port=+0, relative
899 * - IP:N => port=N, absolute
900 * - IP:+N => port=+N, relative
901 * - IP:-N => port=-N, relative
902 */
903 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
904 if (!sk) {
905 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
906 err_code |= ERR_ALERT | ERR_FATAL;
907 goto out;
908 }
909
910 proto = protocol_by_family(sk->ss_family);
911 if (!proto || !proto->connect) {
912 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
913 file, linenum, args[0], args[1]);
914 err_code |= ERR_ALERT | ERR_FATAL;
915 goto out;
916 }
917
918 if (!port1 || !port2) {
919 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200920 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200921 }
922 else if (port1 != port2) {
923 /* port range */
924 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
925 file, linenum, args[0], args[1], args[2]);
926 err_code |= ERR_ALERT | ERR_FATAL;
927 goto out;
928 }
929 else {
930 /* used by checks */
931 realport = port1;
932 }
933
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200934 /* save hostname and create associated name resolution */
935 switch (sk->ss_family) {
936 case AF_INET: {
937 /* remove the port if any */
938 char *c;
939 if ((c = rindex(args[2], ':')) != NULL) {
940 newsrv->hostname = my_strndup(args[2], c - args[2]);
941 }
942 else {
943 newsrv->hostname = strdup(args[2]);
944 }
945 }
946 break;
947 case AF_INET6:
948 newsrv->hostname = strdup(args[2]);
949 break;
950 default:
951 goto skip_name_resolution;
952 }
953
954 /* no name resolution if an IP has been provided */
955 if (inet_pton(sk->ss_family, newsrv->hostname, trash.str) == 1)
956 goto skip_name_resolution;
957
958 if ((curr_resolution = calloc(1, sizeof(struct dns_resolution))) == NULL)
959 goto skip_name_resolution;
960
961 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
962 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
963 goto skip_name_resolution;
964 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
965 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
966 file, linenum, args[2]);
967 err_code |= ERR_ALERT | ERR_FATAL;
968 goto out;
969 }
970
971 curr_resolution->requester = newsrv;
972 curr_resolution->requester_cb = snr_resolution_cb;
973 curr_resolution->requester_error_cb = snr_resolution_error_cb;
974 curr_resolution->status = RSLV_STATUS_NONE;
975 curr_resolution->step = RSLV_STEP_NONE;
976 /* a first resolution has been done by the configuration parser */
977 curr_resolution->last_resolution = now_ms;
978 newsrv->resolution = curr_resolution;
979
980 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +0200981 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +0100982 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200983
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +0100984 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200985 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
986 file, linenum, newsrv->addr.ss_family, args[2]);
987 err_code |= ERR_ALERT | ERR_FATAL;
988 goto out;
989 }
990
991 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
992 newsrv->check.port = curproxy->defsrv.check.port;
993 newsrv->check.inter = curproxy->defsrv.check.inter;
994 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
995 newsrv->check.downinter = curproxy->defsrv.check.downinter;
996 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
997 newsrv->agent.port = curproxy->defsrv.agent.port;
998 newsrv->agent.inter = curproxy->defsrv.agent.inter;
999 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1000 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1001 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1002 newsrv->minconn = curproxy->defsrv.minconn;
1003 newsrv->maxconn = curproxy->defsrv.maxconn;
1004 newsrv->slowstart = curproxy->defsrv.slowstart;
1005 newsrv->onerror = curproxy->defsrv.onerror;
1006 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1007 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1008 newsrv->consecutive_errors_limit
1009 = curproxy->defsrv.consecutive_errors_limit;
1010#ifdef OPENSSL
1011 newsrv->use_ssl = curproxy->defsrv.use_ssl;
1012#endif
1013 newsrv->uweight = newsrv->iweight
1014 = curproxy->defsrv.iweight;
1015
1016 newsrv->check.status = HCHK_STATUS_INI;
1017 newsrv->check.rise = curproxy->defsrv.check.rise;
1018 newsrv->check.fall = curproxy->defsrv.check.fall;
1019 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1020 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001021 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001022
1023 newsrv->agent.status = HCHK_STATUS_INI;
1024 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1025 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1026 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1027 newsrv->agent.server = newsrv;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001028 newsrv->resolver_family_priority = curproxy->defsrv.resolver_family_priority;
1029 if (newsrv->resolver_family_priority == AF_UNSPEC)
1030 newsrv->resolver_family_priority = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001031
1032 cur_arg = 3;
1033 } else {
1034 newsrv = &curproxy->defsrv;
1035 cur_arg = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001036 newsrv->resolver_family_priority = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001037 }
1038
1039 while (*args[cur_arg]) {
1040 if (!strcmp(args[cur_arg], "agent-check")) {
1041 global.maxsock++;
1042 do_agent = 1;
1043 cur_arg += 1;
1044 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1045 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1046 if (err) {
1047 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1048 file, linenum, *err, newsrv->id);
1049 err_code |= ERR_ALERT | ERR_FATAL;
1050 goto out;
1051 }
1052 if (val <= 0) {
1053 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1054 file, linenum, val, args[cur_arg], newsrv->id);
1055 err_code |= ERR_ALERT | ERR_FATAL;
1056 goto out;
1057 }
1058 newsrv->agent.inter = val;
1059 cur_arg += 2;
1060 }
1061 else if (!strcmp(args[cur_arg], "agent-port")) {
1062 global.maxsock++;
1063 newsrv->agent.port = atol(args[cur_arg + 1]);
1064 cur_arg += 2;
1065 }
1066 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1067 newsrv->cookie = strdup(args[cur_arg + 1]);
1068 newsrv->cklen = strlen(args[cur_arg + 1]);
1069 cur_arg += 2;
1070 }
1071 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1072 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1073 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1074 cur_arg += 2;
1075 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001076 else if (!strcmp(args[cur_arg], "resolvers")) {
1077 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1078 cur_arg += 2;
1079 }
1080 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1081 if (!strcmp(args[cur_arg + 1], "ipv4"))
1082 newsrv->resolver_family_priority = AF_INET;
1083 else if (!strcmp(args[cur_arg + 1], "ipv6"))
1084 newsrv->resolver_family_priority = AF_INET6;
1085 else {
1086 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1087 file, linenum, args[cur_arg]);
1088 err_code |= ERR_ALERT | ERR_FATAL;
1089 goto out;
1090 }
1091 cur_arg += 2;
1092 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001093 else if (!strcmp(args[cur_arg], "rise")) {
1094 if (!*args[cur_arg + 1]) {
1095 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1096 file, linenum, args[cur_arg]);
1097 err_code |= ERR_ALERT | ERR_FATAL;
1098 goto out;
1099 }
1100
1101 newsrv->check.rise = atol(args[cur_arg + 1]);
1102 if (newsrv->check.rise <= 0) {
1103 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1104 file, linenum, args[cur_arg]);
1105 err_code |= ERR_ALERT | ERR_FATAL;
1106 goto out;
1107 }
1108
1109 if (newsrv->check.health)
1110 newsrv->check.health = newsrv->check.rise;
1111 cur_arg += 2;
1112 }
1113 else if (!strcmp(args[cur_arg], "fall")) {
1114 newsrv->check.fall = atol(args[cur_arg + 1]);
1115
1116 if (!*args[cur_arg + 1]) {
1117 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1118 file, linenum, args[cur_arg]);
1119 err_code |= ERR_ALERT | ERR_FATAL;
1120 goto out;
1121 }
1122
1123 if (newsrv->check.fall <= 0) {
1124 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1125 file, linenum, args[cur_arg]);
1126 err_code |= ERR_ALERT | ERR_FATAL;
1127 goto out;
1128 }
1129
1130 cur_arg += 2;
1131 }
1132 else if (!strcmp(args[cur_arg], "inter")) {
1133 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1134 if (err) {
1135 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1136 file, linenum, *err, newsrv->id);
1137 err_code |= ERR_ALERT | ERR_FATAL;
1138 goto out;
1139 }
1140 if (val <= 0) {
1141 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1142 file, linenum, val, args[cur_arg], newsrv->id);
1143 err_code |= ERR_ALERT | ERR_FATAL;
1144 goto out;
1145 }
1146 newsrv->check.inter = val;
1147 cur_arg += 2;
1148 }
1149 else if (!strcmp(args[cur_arg], "fastinter")) {
1150 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1151 if (err) {
1152 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1153 file, linenum, *err, newsrv->id);
1154 err_code |= ERR_ALERT | ERR_FATAL;
1155 goto out;
1156 }
1157 if (val <= 0) {
1158 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1159 file, linenum, val, args[cur_arg], newsrv->id);
1160 err_code |= ERR_ALERT | ERR_FATAL;
1161 goto out;
1162 }
1163 newsrv->check.fastinter = val;
1164 cur_arg += 2;
1165 }
1166 else if (!strcmp(args[cur_arg], "downinter")) {
1167 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1168 if (err) {
1169 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1170 file, linenum, *err, newsrv->id);
1171 err_code |= ERR_ALERT | ERR_FATAL;
1172 goto out;
1173 }
1174 if (val <= 0) {
1175 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1176 file, linenum, val, args[cur_arg], newsrv->id);
1177 err_code |= ERR_ALERT | ERR_FATAL;
1178 goto out;
1179 }
1180 newsrv->check.downinter = val;
1181 cur_arg += 2;
1182 }
1183 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1184 struct sockaddr_storage *sk;
1185 int port1, port2;
1186 struct protocol *proto;
1187
1188 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1189 if (!sk) {
1190 Alert("parsing [%s:%d] : '%s' : %s\n",
1191 file, linenum, args[cur_arg], errmsg);
1192 err_code |= ERR_ALERT | ERR_FATAL;
1193 goto out;
1194 }
1195
1196 proto = protocol_by_family(sk->ss_family);
1197 if (!proto || !proto->connect) {
1198 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1199 file, linenum, args[cur_arg], args[cur_arg + 1]);
1200 err_code |= ERR_ALERT | ERR_FATAL;
1201 goto out;
1202 }
1203
1204 if (port1 != port2) {
1205 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1206 file, linenum, args[cur_arg], args[cur_arg + 1]);
1207 err_code |= ERR_ALERT | ERR_FATAL;
1208 goto out;
1209 }
1210
Simon Horman41f58762015-01-30 11:22:56 +09001211 newsrv->check.addr = newsrv->agent.addr = *sk;
Willy Tarreau272adea2014-03-31 10:39:59 +02001212 cur_arg += 2;
1213 }
1214 else if (!strcmp(args[cur_arg], "port")) {
1215 newsrv->check.port = atol(args[cur_arg + 1]);
1216 cur_arg += 2;
1217 }
1218 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001219 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001220 cur_arg ++;
1221 }
1222 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001223 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001224 cur_arg ++;
1225 }
1226 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001227 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001228 cur_arg ++;
1229 }
David Safb76832014-05-08 23:42:08 -04001230 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1231 newsrv->pp_opts |= SRV_PP_V2;
1232 cur_arg ++;
1233 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001234 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1235 newsrv->check.send_proxy = 1;
1236 cur_arg ++;
1237 }
1238 else if (!strcmp(args[cur_arg], "weight")) {
1239 int w;
1240 w = atol(args[cur_arg + 1]);
1241 if (w < 0 || w > SRV_UWGHT_MAX) {
1242 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1243 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1244 err_code |= ERR_ALERT | ERR_FATAL;
1245 goto out;
1246 }
1247 newsrv->uweight = newsrv->iweight = w;
1248 cur_arg += 2;
1249 }
1250 else if (!strcmp(args[cur_arg], "minconn")) {
1251 newsrv->minconn = atol(args[cur_arg + 1]);
1252 cur_arg += 2;
1253 }
1254 else if (!strcmp(args[cur_arg], "maxconn")) {
1255 newsrv->maxconn = atol(args[cur_arg + 1]);
1256 cur_arg += 2;
1257 }
1258 else if (!strcmp(args[cur_arg], "maxqueue")) {
1259 newsrv->maxqueue = atol(args[cur_arg + 1]);
1260 cur_arg += 2;
1261 }
1262 else if (!strcmp(args[cur_arg], "slowstart")) {
1263 /* slowstart is stored in seconds */
1264 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1265 if (err) {
1266 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1267 file, linenum, *err, newsrv->id);
1268 err_code |= ERR_ALERT | ERR_FATAL;
1269 goto out;
1270 }
1271 newsrv->slowstart = (val + 999) / 1000;
1272 cur_arg += 2;
1273 }
1274 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1275
1276 if (!*args[cur_arg + 1]) {
1277 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1278 file, linenum);
1279 err_code |= ERR_ALERT | ERR_FATAL;
1280 goto out;
1281 }
1282
1283 newsrv->trackit = strdup(args[cur_arg + 1]);
1284
1285 cur_arg += 2;
1286 }
1287 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1288 global.maxsock++;
1289 do_check = 1;
1290 cur_arg += 1;
1291 }
1292 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001293 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001294 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001295 newsrv->check.state |= CHK_ST_PAUSED;
1296 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001297 cur_arg += 1;
1298 }
1299 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1300 if (!strcmp(args[cur_arg + 1], "none"))
1301 newsrv->observe = HANA_OBS_NONE;
1302 else if (!strcmp(args[cur_arg + 1], "layer4"))
1303 newsrv->observe = HANA_OBS_LAYER4;
1304 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1305 if (curproxy->mode != PR_MODE_HTTP) {
1306 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1307 file, linenum, args[cur_arg + 1]);
1308 err_code |= ERR_ALERT;
1309 }
1310 newsrv->observe = HANA_OBS_LAYER7;
1311 }
1312 else {
1313 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1314 "'layer4', 'layer7' but got '%s'\n",
1315 file, linenum, args[cur_arg], args[cur_arg + 1]);
1316 err_code |= ERR_ALERT | ERR_FATAL;
1317 goto out;
1318 }
1319
1320 cur_arg += 2;
1321 }
1322 else if (!strcmp(args[cur_arg], "on-error")) {
1323 if (!strcmp(args[cur_arg + 1], "fastinter"))
1324 newsrv->onerror = HANA_ONERR_FASTINTER;
1325 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1326 newsrv->onerror = HANA_ONERR_FAILCHK;
1327 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1328 newsrv->onerror = HANA_ONERR_SUDDTH;
1329 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1330 newsrv->onerror = HANA_ONERR_MARKDWN;
1331 else {
1332 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1333 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1334 file, linenum, args[cur_arg], args[cur_arg + 1]);
1335 err_code |= ERR_ALERT | ERR_FATAL;
1336 goto out;
1337 }
1338
1339 cur_arg += 2;
1340 }
1341 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1342 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1343 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1344 else {
1345 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1346 file, linenum, args[cur_arg], args[cur_arg + 1]);
1347 err_code |= ERR_ALERT | ERR_FATAL;
1348 goto out;
1349 }
1350
1351 cur_arg += 2;
1352 }
1353 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1354 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1355 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1356 else {
1357 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1358 file, linenum, args[cur_arg], args[cur_arg + 1]);
1359 err_code |= ERR_ALERT | ERR_FATAL;
1360 goto out;
1361 }
1362
1363 cur_arg += 2;
1364 }
1365 else if (!strcmp(args[cur_arg], "error-limit")) {
1366 if (!*args[cur_arg + 1]) {
1367 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1368 file, linenum, args[cur_arg]);
1369 err_code |= ERR_ALERT | ERR_FATAL;
1370 goto out;
1371 }
1372
1373 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1374
1375 if (newsrv->consecutive_errors_limit <= 0) {
1376 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1377 file, linenum, args[cur_arg]);
1378 err_code |= ERR_ALERT | ERR_FATAL;
1379 goto out;
1380 }
1381 cur_arg += 2;
1382 }
1383 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1384 int port_low, port_high;
1385 struct sockaddr_storage *sk;
1386 struct protocol *proto;
1387
1388 if (!*args[cur_arg + 1]) {
1389 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1390 file, linenum, "source", "usesrc", "interface");
1391 err_code |= ERR_ALERT | ERR_FATAL;
1392 goto out;
1393 }
1394
1395 newsrv->conn_src.opts |= CO_SRC_BIND;
1396 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1397 if (!sk) {
1398 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1399 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1400 err_code |= ERR_ALERT | ERR_FATAL;
1401 goto out;
1402 }
1403
1404 proto = protocol_by_family(sk->ss_family);
1405 if (!proto || !proto->connect) {
1406 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1407 file, linenum, args[cur_arg], args[cur_arg+1]);
1408 err_code |= ERR_ALERT | ERR_FATAL;
1409 goto out;
1410 }
1411
1412 newsrv->conn_src.source_addr = *sk;
1413
1414 if (port_low != port_high) {
1415 int i;
1416
1417 if (!port_low || !port_high) {
1418 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1419 file, linenum, args[cur_arg], args[cur_arg + 1]);
1420 err_code |= ERR_ALERT | ERR_FATAL;
1421 goto out;
1422 }
1423
1424 if (port_low <= 0 || port_low > 65535 ||
1425 port_high <= 0 || port_high > 65535 ||
1426 port_low > port_high) {
1427 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1428 file, linenum, port_low, port_high);
1429 err_code |= ERR_ALERT | ERR_FATAL;
1430 goto out;
1431 }
1432 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1433 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1434 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1435 }
1436
1437 cur_arg += 2;
1438 while (*(args[cur_arg])) {
1439 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1440#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1441#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001442 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001443 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1444 file, linenum, "usesrc", "source");
1445 err_code |= ERR_ALERT | ERR_FATAL;
1446 goto out;
1447 }
1448#endif
1449 if (!*args[cur_arg + 1]) {
1450 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1451 file, linenum, "usesrc");
1452 err_code |= ERR_ALERT | ERR_FATAL;
1453 goto out;
1454 }
1455 if (!strcmp(args[cur_arg + 1], "client")) {
1456 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1457 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1458 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1459 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1460 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1461 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1462 char *name, *end;
1463
1464 name = args[cur_arg+1] + 7;
1465 while (isspace(*name))
1466 name++;
1467
1468 end = name;
1469 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1470 end++;
1471
1472 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1473 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1474 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1475 newsrv->conn_src.bind_hdr_len = end - name;
1476 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1477 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1478 newsrv->conn_src.bind_hdr_occ = -1;
1479
1480 /* now look for an occurrence number */
1481 while (isspace(*end))
1482 end++;
1483 if (*end == ',') {
1484 end++;
1485 name = end;
1486 if (*end == '-')
1487 end++;
1488 while (isdigit((int)*end))
1489 end++;
1490 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1491 }
1492
1493 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1494 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1495 " occurrences values smaller than %d.\n",
1496 file, linenum, MAX_HDR_HISTORY);
1497 err_code |= ERR_ALERT | ERR_FATAL;
1498 goto out;
1499 }
1500 } else {
1501 struct sockaddr_storage *sk;
1502 int port1, port2;
1503
1504 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1505 if (!sk) {
1506 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1507 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1508 err_code |= ERR_ALERT | ERR_FATAL;
1509 goto out;
1510 }
1511
1512 proto = protocol_by_family(sk->ss_family);
1513 if (!proto || !proto->connect) {
1514 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1515 file, linenum, args[cur_arg], args[cur_arg+1]);
1516 err_code |= ERR_ALERT | ERR_FATAL;
1517 goto out;
1518 }
1519
1520 if (port1 != port2) {
1521 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1522 file, linenum, args[cur_arg], args[cur_arg + 1]);
1523 err_code |= ERR_ALERT | ERR_FATAL;
1524 goto out;
1525 }
1526 newsrv->conn_src.tproxy_addr = *sk;
1527 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1528 }
1529 global.last_checks |= LSTCHK_NETADM;
1530#if !defined(CONFIG_HAP_TRANSPARENT)
1531 global.last_checks |= LSTCHK_CTTPROXY;
1532#endif
1533 cur_arg += 2;
1534 continue;
1535#else /* no TPROXY support */
1536 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1537 file, linenum, "usesrc");
1538 err_code |= ERR_ALERT | ERR_FATAL;
1539 goto out;
1540#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1541 } /* "usesrc" */
1542
1543 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1544#ifdef SO_BINDTODEVICE
1545 if (!*args[cur_arg + 1]) {
1546 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1547 file, linenum, args[0]);
1548 err_code |= ERR_ALERT | ERR_FATAL;
1549 goto out;
1550 }
1551 free(newsrv->conn_src.iface_name);
1552 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1553 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1554 global.last_checks |= LSTCHK_NETADM;
1555#else
1556 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1557 file, linenum, args[0], args[cur_arg]);
1558 err_code |= ERR_ALERT | ERR_FATAL;
1559 goto out;
1560#endif
1561 cur_arg += 2;
1562 continue;
1563 }
1564 /* this keyword in not an option of "source" */
1565 break;
1566 } /* while */
1567 }
1568 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1569 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1570 file, linenum, "usesrc", "source");
1571 err_code |= ERR_ALERT | ERR_FATAL;
1572 goto out;
1573 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001574 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1575#ifdef CONFIG_HAP_NS
1576 char *arg = args[cur_arg + 1];
1577 if (!strcmp(arg, "*")) {
1578 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1579 } else {
1580 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1581
1582 if (newsrv->netns == NULL)
1583 newsrv->netns = netns_store_insert(arg);
1584
1585 if (newsrv->netns == NULL) {
1586 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1587 err_code |= ERR_ALERT | ERR_FATAL;
1588 goto out;
1589 }
1590 }
1591#else
1592 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1593 file, linenum, args[0], args[cur_arg]);
1594 err_code |= ERR_ALERT | ERR_FATAL;
1595 goto out;
1596#endif
1597 cur_arg += 2;
1598 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001599 else {
1600 static int srv_dumped;
1601 struct srv_kw *kw;
1602 char *err;
1603
1604 kw = srv_find_kw(args[cur_arg]);
1605 if (kw) {
1606 char *err = NULL;
1607 int code;
1608
1609 if (!kw->parse) {
1610 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1611 file, linenum, args[0], args[1], args[cur_arg]);
1612 cur_arg += 1 + kw->skip ;
1613 err_code |= ERR_ALERT | ERR_FATAL;
1614 goto out;
1615 }
1616
1617 if (defsrv && !kw->default_ok) {
1618 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1619 file, linenum, args[0], args[1], args[cur_arg]);
1620 cur_arg += 1 + kw->skip ;
1621 err_code |= ERR_ALERT;
1622 continue;
1623 }
1624
1625 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1626 err_code |= code;
1627
1628 if (code) {
1629 if (err && *err) {
1630 indent_msg(&err, 2);
1631 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1632 }
1633 else
1634 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1635 file, linenum, args[0], args[1], args[cur_arg]);
1636 if (code & ERR_FATAL) {
1637 free(err);
1638 cur_arg += 1 + kw->skip;
1639 goto out;
1640 }
1641 }
1642 free(err);
1643 cur_arg += 1 + kw->skip;
1644 continue;
1645 }
1646
1647 err = NULL;
1648 if (!srv_dumped) {
1649 srv_dump_kws(&err);
1650 indent_msg(&err, 4);
1651 srv_dumped = 1;
1652 }
1653
1654 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1655 file, linenum, args[0], args[1], args[cur_arg],
1656 err ? " Registered keywords :" : "", err ? err : "");
1657 free(err);
1658
1659 err_code |= ERR_ALERT | ERR_FATAL;
1660 goto out;
1661 }
1662 }
1663
Willy Tarreau272adea2014-03-31 10:39:59 +02001664 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001665 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001666
1667 if (newsrv->trackit) {
1668 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1669 file, linenum);
1670 err_code |= ERR_ALERT | ERR_FATAL;
1671 goto out;
1672 }
1673
1674 /* If neither a port nor an addr was specified and no check transport
1675 * layer is forced, then the transport layer used by the checks is the
1676 * same as for the production traffic. Otherwise we use raw_sock by
1677 * default, unless one is specified.
1678 */
Simon Horman41f58762015-01-30 11:22:56 +09001679 if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001680#ifdef USE_OPENSSL
1681 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1682#endif
David Safb76832014-05-08 23:42:08 -04001683 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001684 }
1685 /* try to get the port from check_core.addr if check.port not set */
1686 if (!newsrv->check.port)
Simon Horman41f58762015-01-30 11:22:56 +09001687 newsrv->check.port = get_host_port(&newsrv->check.addr);
Willy Tarreau272adea2014-03-31 10:39:59 +02001688
1689 if (!newsrv->check.port)
1690 newsrv->check.port = realport; /* by default */
1691
1692 if (!newsrv->check.port) {
1693 /* not yet valid, because no port was set on
1694 * the server either. We'll check if we have
1695 * a known port on the first listener.
1696 */
1697 struct listener *l;
1698
1699 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1700 newsrv->check.port = get_host_port(&l->addr);
1701 if (newsrv->check.port)
1702 break;
1703 }
1704 }
1705 /*
1706 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001707 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001708 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001709 if (!newsrv->check.port &&
Simon Horman41f58762015-01-30 11:22:56 +09001710 (is_inet_addr(&newsrv->check.addr) ||
1711 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001712 struct tcpcheck_rule *n = NULL, *r = NULL;
1713 struct list *l;
1714
1715 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1716 if (!r) {
1717 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1718 file, linenum, newsrv->id);
1719 err_code |= ERR_ALERT | ERR_FATAL;
1720 goto out;
1721 }
1722 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1723 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",
1724 file, linenum, newsrv->id);
1725 err_code |= ERR_ALERT | ERR_FATAL;
1726 goto out;
1727 }
1728 else {
1729 /* scan the tcp-check ruleset to ensure a port has been configured */
1730 l = &newsrv->proxy->tcpcheck_rules;
1731 list_for_each_entry(n, l, list) {
1732 r = (struct tcpcheck_rule *)n->list.p;
1733 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1734 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",
1735 file, linenum, newsrv->id);
1736 err_code |= ERR_ALERT | ERR_FATAL;
1737 goto out;
1738 }
1739 }
1740 }
1741 }
1742
1743 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001744 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001745 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001746 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1747 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001748 goto out;
1749 }
1750
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001751 if (newsrv->resolution)
1752 newsrv->resolution->resolver_family_priority = newsrv->resolver_family_priority;
1753
Willy Tarreau272adea2014-03-31 10:39:59 +02001754 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1755 }
1756
1757 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001758 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001759
1760 if (!newsrv->agent.port) {
1761 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1762 file, linenum, newsrv->id);
1763 err_code |= ERR_ALERT | ERR_FATAL;
1764 goto out;
1765 }
1766
1767 if (!newsrv->agent.inter)
1768 newsrv->agent.inter = newsrv->check.inter;
1769
Simon Hormanb1900d52015-01-30 11:22:54 +09001770 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001771 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001772 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1773 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001774 goto out;
1775 }
1776
1777 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1778 }
1779
1780 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001781 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001782 curproxy->srv_bck++;
1783 else
1784 curproxy->srv_act++;
1785
Willy Tarreauc5150da2014-05-13 19:27:31 +02001786 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001787 }
1788 }
1789 return 0;
1790
1791 out:
1792 free(errmsg);
1793 return err_code;
1794}
1795
Simon Horman7d09b9a2013-02-12 10:45:51 +09001796/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02001797 * update a server's current IP address.
1798 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
1799 * ip is in network format.
1800 * updater is a string which contains an information about the requester of the update.
1801 * updater is used if not NULL.
1802 *
1803 * A log line and a stderr warning message is generated based on server's backend options.
1804 */
1805int update_server_addr(struct server *s, void *ip, int ip_sin_family, char *updater)
1806{
1807 /* generates a log line and a warning on stderr */
1808 if (1) {
1809 /* book enough space for both IPv4 and IPv6 */
1810 char oldip[INET6_ADDRSTRLEN];
1811 char newip[INET6_ADDRSTRLEN];
1812
1813 memset(oldip, '\0', INET6_ADDRSTRLEN);
1814 memset(newip, '\0', INET6_ADDRSTRLEN);
1815
1816 /* copy old IP address in a string */
1817 switch (s->addr.ss_family) {
1818 case AF_INET:
1819 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
1820 break;
1821 case AF_INET6:
1822 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
1823 break;
1824 };
1825
1826 /* copy new IP address in a string */
1827 switch (ip_sin_family) {
1828 case AF_INET:
1829 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
1830 break;
1831 case AF_INET6:
1832 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
1833 break;
1834 };
1835
1836 /* save log line into a buffer */
1837 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
1838 s->proxy->id, s->id, oldip, newip, updater);
1839
1840 /* write the buffer on stderr */
1841 Warning("%s.\n", trash.str);
1842
1843 /* send a log */
1844 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1845 }
1846
1847 /* save the new IP family */
1848 s->addr.ss_family = ip_sin_family;
1849 /* save the new IP address */
1850 switch (ip_sin_family) {
1851 case AF_INET:
1852 ((struct sockaddr_in *)&s->addr)->sin_addr.s_addr = *(uint32_t *)ip;
1853 break;
1854 case AF_INET6:
1855 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
1856 break;
1857 };
1858
1859 return 0;
1860}
1861
1862/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001863 * update server status based on result of name resolution
1864 * returns:
1865 * 0 if server status is updated
1866 * 1 if server status has not changed
1867 */
1868int snr_update_srv_status(struct server *s)
1869{
1870 struct dns_resolution *resolution = s->resolution;
1871
1872 switch (resolution->status) {
1873 case RSLV_STATUS_NONE:
1874 /* status when HAProxy has just (re)started */
1875 trigger_resolution(s);
1876 break;
1877
1878 default:
1879 break;
1880 }
1881
1882 return 1;
1883}
1884
1885/*
1886 * Server Name Resolution valid response callback
1887 * It expects:
1888 * - <nameserver>: the name server which answered the valid response
1889 * - <response>: buffer containing a valid DNS response
1890 * - <response_len>: size of <response>
1891 * It performs the following actions:
1892 * - ignore response if current ip found and server family not met
1893 * - update with first new ip found if family is met and current IP is not found
1894 * returns:
1895 * 0 on error
1896 * 1 when no error or safe ignore
1897 */
1898int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, unsigned char *response, int response_len)
1899{
1900 struct server *s;
1901 void *serverip, *firstip;
1902 short server_sin_family, firstip_sin_family;
1903 unsigned char *response_end;
1904 int ret;
1905 struct chunk *chk = get_trash_chunk();
1906
1907 /* initializing variables */
1908 response_end = response + response_len; /* pointer to mark the end of the response */
1909 firstip = NULL; /* pointer to the first valid response found */
1910 /* it will be used as the new IP if a change is required */
1911 firstip_sin_family = AF_UNSPEC;
1912 serverip = NULL; /* current server IP address */
1913
1914 /* shortcut to the server whose name is being resolved */
1915 s = (struct server *)resolution->requester;
1916
1917 /* initializing server IP pointer */
1918 server_sin_family = s->addr.ss_family;
1919 switch (server_sin_family) {
1920 case AF_INET:
1921 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
1922 break;
1923
1924 case AF_INET6:
1925 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
1926 break;
1927
1928 default:
1929 goto invalid;
1930 }
1931
1932 ret = dns_get_ip_from_response(response, response_end, resolution->hostname_dn, resolution->hostname_dn_len,
1933 serverip, server_sin_family, resolution->resolver_family_priority, &firstip,
1934 &firstip_sin_family);
1935
1936 switch (ret) {
1937 case DNS_UPD_NO:
1938 if (resolution->status != RSLV_STATUS_VALID) {
1939 resolution->status = RSLV_STATUS_VALID;
1940 resolution->last_status_change = now_ms;
1941 }
1942 goto stop_resolution;
1943
1944 case DNS_UPD_SRVIP_NOT_FOUND:
1945 goto save_ip;
1946
1947 case DNS_UPD_CNAME:
1948 if (resolution->status != RSLV_STATUS_VALID) {
1949 resolution->status = RSLV_STATUS_VALID;
1950 resolution->last_status_change = now_ms;
1951 }
1952 goto invalid;
1953
1954 default:
1955 goto invalid;
1956
1957 }
1958
1959 save_ip:
1960 nameserver->counters.update += 1;
1961 if (resolution->status != RSLV_STATUS_VALID) {
1962 resolution->status = RSLV_STATUS_VALID;
1963 resolution->last_status_change = now_ms;
1964 }
1965
1966 /* save the first ip we found */
1967 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
1968 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
1969
1970 stop_resolution:
1971 /* update last resolution date and time */
1972 resolution->last_resolution = now_ms;
1973 /* reset current status flag */
1974 resolution->step = RSLV_STEP_NONE;
1975 /* reset values */
1976 dns_reset_resolution(resolution);
1977
1978 LIST_DEL(&resolution->list);
1979 dns_update_resolvers_timeout(nameserver->resolvers);
1980
1981 snr_update_srv_status(s);
1982 return 0;
1983
1984 invalid:
1985 nameserver->counters.invalid += 1;
1986 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
1987 goto stop_resolution;
1988
1989 snr_update_srv_status(s);
1990 return 0;
1991}
1992
1993/*
1994 * Server Name Resolution error management callback
1995 * returns:
1996 * 0 on error
1997 * 1 when no error or safe ignore
1998 */
1999int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
2000{
2001 struct server *s;
2002 struct dns_resolvers *resolvers;
2003
2004 /* shortcut to the server whose name is being resolved */
2005 s = (struct server *)resolution->requester;
2006 resolvers = resolution->resolvers;
2007
2008 /* can be ignored if this is not the last response */
2009 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
2010 return 1;
2011 }
2012
2013 switch (error_code) {
2014 case DNS_RESP_INVALID:
2015 case DNS_RESP_WRONG_NAME:
2016 if (resolution->status != RSLV_STATUS_INVALID) {
2017 resolution->status = RSLV_STATUS_INVALID;
2018 resolution->last_status_change = now_ms;
2019 }
2020 break;
2021
2022 case DNS_RESP_ANCOUNT_ZERO:
2023 case DNS_RESP_ERROR:
2024 if (resolution->query_type == DNS_RTYPE_ANY) {
2025 /* let's change the query type */
2026 if (resolution->resolver_family_priority == AF_INET6)
2027 resolution->query_type = DNS_RTYPE_AAAA;
2028 else
2029 resolution->query_type = DNS_RTYPE_A;
2030
2031 dns_send_query(resolution);
2032
2033 /*
2034 * move the resolution to the last element of the FIFO queue
2035 * and update timeout wakeup based on the new first entry
2036 */
2037 if (dns_check_resolution_queue(resolvers) > 1) {
2038 /* second resolution becomes first one */
2039 LIST_DEL(&resolvers->curr_resolution);
2040 /* ex first resolution goes to the end of the queue */
2041 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
2042 }
2043 dns_update_resolvers_timeout(resolvers);
2044 goto leave;
2045 }
2046 else {
2047 if (resolution->status != RSLV_STATUS_OTHER) {
2048 resolution->status = RSLV_STATUS_OTHER;
2049 resolution->last_status_change = now_ms;
2050 }
2051 }
2052 break;
2053
2054 case DNS_RESP_NX_DOMAIN:
2055 if (resolution->status != RSLV_STATUS_NX) {
2056 resolution->status = RSLV_STATUS_NX;
2057 resolution->last_status_change = now_ms;
2058 }
2059 break;
2060
2061 case DNS_RESP_REFUSED:
2062 if (resolution->status != RSLV_STATUS_REFUSED) {
2063 resolution->status = RSLV_STATUS_REFUSED;
2064 resolution->last_status_change = now_ms;
2065 }
2066 break;
2067
2068 case DNS_RESP_CNAME_ERROR:
2069 break;
2070
2071 case DNS_RESP_TIMEOUT:
2072 if (resolution->status != RSLV_STATUS_TIMEOUT) {
2073 resolution->status = RSLV_STATUS_TIMEOUT;
2074 resolution->last_status_change = now_ms;
2075 }
2076 break;
2077 }
2078
2079 /* update last resolution date and time */
2080 resolution->last_resolution = now_ms;
2081 /* reset current status flag */
2082 resolution->step = RSLV_STEP_NONE;
2083 /* reset values */
2084 dns_reset_resolution(resolution);
2085
2086 LIST_DEL(&resolution->list);
2087 dns_update_resolvers_timeout(resolvers);
2088
2089 leave:
2090 snr_update_srv_status(s);
2091 return 1;
2092}
2093
2094/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002095 * Local variables:
2096 * c-indent-level: 8
2097 * c-basic-offset: 8
2098 * End:
2099 */