blob: a50f9e1237413e4ad091df003db83efd13ffc433 [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>
23
Simon Hormanb1900d52015-01-30 11:22:54 +090024#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020025#include <proto/port_range.h>
26#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020027#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020029#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020030#include <proto/stream.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020031#include <proto/task.h>
32
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
Willy Tarreau21faa912012-10-10 08:27:36 +020034/* List head of all known server keywords */
35static struct srv_kw_list srv_keywords = {
36 .list = LIST_HEAD_INIT(srv_keywords.list)
37};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020038
Simon Hormana3608442013-11-01 16:46:15 +090039int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020040{
Willy Tarreau892337c2014-05-13 23:41:20 +020041 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020042 return s->down_time;
43
44 return now.tv_sec - s->last_change + s->down_time;
45}
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050047int srv_lastsession(const struct server *s)
48{
49 if (s->counters.last_sess)
50 return now.tv_sec - s->counters.last_sess;
51
52 return -1;
53}
54
Simon Horman4a741432013-02-23 15:35:38 +090055int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020056{
Simon Horman4a741432013-02-23 15:35:38 +090057 const struct server *s = check->server;
58
Willy Tarreauff5ae352013-12-11 20:36:34 +010059 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090060 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010061
Willy Tarreau892337c2014-05-13 23:41:20 +020062 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090063 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010064
Simon Horman4a741432013-02-23 15:35:38 +090065 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010066}
67
Willy Tarreau21faa912012-10-10 08:27:36 +020068/*
69 * Registers the server keyword list <kwl> as a list of valid keywords for next
70 * parsing sessions.
71 */
72void srv_register_keywords(struct srv_kw_list *kwl)
73{
74 LIST_ADDQ(&srv_keywords.list, &kwl->list);
75}
76
77/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
78 * keyword is found with a NULL ->parse() function, then an attempt is made to
79 * find one with a valid ->parse() function. This way it is possible to declare
80 * platform-dependant, known keywords as NULL, then only declare them as valid
81 * if some options are met. Note that if the requested keyword contains an
82 * opening parenthesis, everything from this point is ignored.
83 */
84struct srv_kw *srv_find_kw(const char *kw)
85{
86 int index;
87 const char *kwend;
88 struct srv_kw_list *kwl;
89 struct srv_kw *ret = NULL;
90
91 kwend = strchr(kw, '(');
92 if (!kwend)
93 kwend = kw + strlen(kw);
94
95 list_for_each_entry(kwl, &srv_keywords.list, list) {
96 for (index = 0; kwl->kw[index].kw != NULL; index++) {
97 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
98 kwl->kw[index].kw[kwend-kw] == 0) {
99 if (kwl->kw[index].parse)
100 return &kwl->kw[index]; /* found it !*/
101 else
102 ret = &kwl->kw[index]; /* may be OK */
103 }
104 }
105 }
106 return ret;
107}
108
109/* Dumps all registered "server" keywords to the <out> string pointer. The
110 * unsupported keywords are only dumped if their supported form was not
111 * found.
112 */
113void srv_dump_kws(char **out)
114{
115 struct srv_kw_list *kwl;
116 int index;
117
118 *out = NULL;
119 list_for_each_entry(kwl, &srv_keywords.list, list) {
120 for (index = 0; kwl->kw[index].kw != NULL; index++) {
121 if (kwl->kw[index].parse ||
122 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
123 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
124 kwl->scope,
125 kwl->kw[index].kw,
126 kwl->kw[index].skip ? " <arg>" : "",
127 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
128 kwl->kw[index].parse ? "" : " (not supported)");
129 }
130 }
131 }
132}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100133
Willy Tarreaudff55432012-10-10 17:51:05 +0200134/* parse the "id" server keyword */
135static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
136{
137 struct eb32_node *node;
138
139 if (!*args[*cur_arg + 1]) {
140 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
141 return ERR_ALERT | ERR_FATAL;
142 }
143
144 newsrv->puid = atol(args[*cur_arg + 1]);
145 newsrv->conf.id.key = newsrv->puid;
146
147 if (newsrv->puid <= 0) {
148 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
149 return ERR_ALERT | ERR_FATAL;
150 }
151
152 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
153 if (node) {
154 struct server *target = container_of(node, struct server, conf.id);
155 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
156 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
157 target->id);
158 return ERR_ALERT | ERR_FATAL;
159 }
160
161 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
162 return 0;
163}
164
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200165/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200166 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200167 * shutdown.
168 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200169void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200170{
Willy Tarreau87b09662015-04-03 00:22:06 +0200171 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200172
Willy Tarreau87b09662015-04-03 00:22:06 +0200173 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
174 if (stream->srv_conn == srv)
175 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200176}
177
178/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200179 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200180 * the reason for the shutdown.
181 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200182void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200183{
184 struct server *srv;
185
186 for (srv = px->srv; srv != NULL; srv = srv->next)
187 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200188 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200189}
190
Willy Tarreaubda92272014-05-20 21:55:30 +0200191/* Appends some information to a message string related to a server going UP or
192 * DOWN. If both <forced> and <reason> are null and the server tracks another
193 * one, a "via" information will be provided to know where the status came from.
194 * If <reason> is non-null, the entire string will be appended after a comma and
195 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200196 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200197 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200198 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200199void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200200{
Willy Tarreaubda92272014-05-20 21:55:30 +0200201 if (reason)
202 chunk_appendf(msg, ", %s", reason);
203 else if (!forced && s->track)
204 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200205
206 if (xferred >= 0) {
207 if (s->state == SRV_ST_STOPPED)
208 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
209 " %d sessions active, %d requeued, %d remaining in queue",
210 s->proxy->srv_act, s->proxy->srv_bck,
211 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
212 s->cur_sess, xferred, s->nbpend);
213 else
214 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
215 " %d sessions requeued, %d total in queue",
216 s->proxy->srv_act, s->proxy->srv_bck,
217 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
218 xferred, s->nbpend);
219 }
220}
221
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200222/* Marks server <s> down, regardless of its checks' statuses, notifies by all
223 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200224 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200225 * recomputes the number of servers, but not the map. Maintenance servers are
226 * ignored. It reports <reason> if non-null as the reason for going down. Note
227 * that it makes use of the trash to build the log strings, so <reason> must
228 * not be placed there.
229 */
230void srv_set_stopped(struct server *s, const char *reason)
231{
232 struct server *srv;
233 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
234 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900235 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200236 int xferred;
237
238 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
239 return;
240
241 s->last_change = now.tv_sec;
242 s->state = SRV_ST_STOPPED;
243 if (s->proxy->lbprm.set_server_status_down)
244 s->proxy->lbprm.set_server_status_down(s);
245
246 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200247 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200248
Willy Tarreau87b09662015-04-03 00:22:06 +0200249 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200250 * a connection. Those which are redispatchable will be queued
251 * to another server or to the proxy itself.
252 */
253 xferred = pendconn_redistribute(s);
254
255 chunk_printf(&trash,
256 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
257 s->proxy->id, s->id);
258
259 srv_append_status(&trash, s, reason, xferred, 0);
260 Warning("%s.\n", trash.str);
261
262 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900263 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
264 send_log(s->proxy, log_level, "%s.\n", trash.str);
265 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200266
267 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
268 set_backend_down(s->proxy);
269
270 s->counters.down_trans++;
271
272 for (srv = s->trackers; srv; srv = srv->tracknext)
273 srv_set_stopped(srv, NULL);
274}
275
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200276/* Marks server <s> up regardless of its checks' statuses and provided it isn't
277 * in maintenance. Notifies by all available means, recounts the remaining
278 * servers on the proxy and tries to grab requests from the proxy. It
279 * automatically recomputes the number of servers, but not the map. Maintenance
280 * servers are ignored. It reports <reason> if non-null as the reason for going
281 * up. Note that it makes use of the trash to build the log strings, so <reason>
282 * must not be placed there.
283 */
284void srv_set_running(struct server *s, const char *reason)
285{
286 struct server *srv;
287 int xferred;
288
289 if (s->admin & SRV_ADMF_MAINT)
290 return;
291
292 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
293 return;
294
295 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
296 if (s->proxy->last_change < now.tv_sec) // ignore negative times
297 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
298 s->proxy->last_change = now.tv_sec;
299 }
300
301 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
302 s->down_time += now.tv_sec - s->last_change;
303
304 s->last_change = now.tv_sec;
305
306 s->state = SRV_ST_STARTING;
307 if (s->slowstart > 0)
308 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
309 else
310 s->state = SRV_ST_RUNNING;
311
312 server_recalc_eweight(s);
313
314 /* If the server is set with "on-marked-up shutdown-backup-sessions",
315 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200316 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200317 * on all backup servers.
318 */
319 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
320 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200321 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200322
323 /* check if we can handle some connections queued at the proxy. We
324 * will take as many as we can handle.
325 */
326 xferred = pendconn_grab_from_px(s);
327
328 chunk_printf(&trash,
329 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
330 s->proxy->id, s->id);
331
332 srv_append_status(&trash, s, reason, xferred, 0);
333 Warning("%s.\n", trash.str);
334 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
335
336 for (srv = s->trackers; srv; srv = srv->tracknext)
337 srv_set_running(srv, NULL);
338}
339
Willy Tarreau8eb77842014-05-21 13:54:57 +0200340/* Marks server <s> stopping regardless of its checks' statuses and provided it
341 * isn't in maintenance. Notifies by all available means, recounts the remaining
342 * servers on the proxy and tries to grab requests from the proxy. It
343 * automatically recomputes the number of servers, but not the map. Maintenance
344 * servers are ignored. It reports <reason> if non-null as the reason for going
345 * up. Note that it makes use of the trash to build the log strings, so <reason>
346 * must not be placed there.
347 */
348void srv_set_stopping(struct server *s, const char *reason)
349{
350 struct server *srv;
351 int xferred;
352
353 if (s->admin & SRV_ADMF_MAINT)
354 return;
355
356 if (s->state == SRV_ST_STOPPING)
357 return;
358
359 s->last_change = now.tv_sec;
360 s->state = SRV_ST_STOPPING;
361 if (s->proxy->lbprm.set_server_status_down)
362 s->proxy->lbprm.set_server_status_down(s);
363
Willy Tarreau87b09662015-04-03 00:22:06 +0200364 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200365 * a connection. Those which are redispatchable will be queued
366 * to another server or to the proxy itself.
367 */
368 xferred = pendconn_redistribute(s);
369
370 chunk_printf(&trash,
371 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
372 s->proxy->id, s->id);
373
374 srv_append_status(&trash, s, reason, xferred, 0);
375
376 Warning("%s.\n", trash.str);
377 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
378
379 if (!s->proxy->srv_bck && !s->proxy->srv_act)
380 set_backend_down(s->proxy);
381
382 for (srv = s->trackers; srv; srv = srv->tracknext)
383 srv_set_stopping(srv, NULL);
384}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200385
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200386/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
387 * enforce either maint mode or drain mode. It is not allowed to set more than
388 * one flag at once. The equivalent "inherited" flag is propagated to all
389 * tracking servers. Maintenance mode disables health checks (but not agent
390 * checks). When either the flag is already set or no flag is passed, nothing
391 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200392 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200393void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200394{
395 struct check *check = &s->check;
396 struct server *srv;
397 int xferred;
398
399 if (!mode)
400 return;
401
402 /* stop going down as soon as we meet a server already in the same state */
403 if (s->admin & mode)
404 return;
405
406 s->admin |= mode;
407
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200408 /* stop going down if the equivalent flag was already present (forced or inherited) */
409 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
410 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
411 return;
412
413 /* Maintenance must also disable health checks */
414 if (mode & SRV_ADMF_MAINT) {
415 if (s->check.state & CHK_ST_ENABLED) {
416 s->check.state |= CHK_ST_PAUSED;
417 check->health = 0;
418 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200419
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200420 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200421 chunk_printf(&trash,
422 "%sServer %s/%s was DOWN and now enters maintenance",
423 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
424
Willy Tarreaubda92272014-05-20 21:55:30 +0200425 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200426
427 Warning("%s.\n", trash.str);
428 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
429 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200430 else { /* server was still running */
431 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
432 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
433
434 check->health = 0; /* failure */
435 s->last_change = now.tv_sec;
436 s->state = SRV_ST_STOPPED;
437 if (s->proxy->lbprm.set_server_status_down)
438 s->proxy->lbprm.set_server_status_down(s);
439
440 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200441 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200442
Willy Tarreau87b09662015-04-03 00:22:06 +0200443 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200444 * a connection. Those which are redispatchable will be queued
445 * to another server or to the proxy itself.
446 */
447 xferred = pendconn_redistribute(s);
448
449 chunk_printf(&trash,
450 "%sServer %s/%s is going DOWN for maintenance",
451 s->flags & SRV_F_BACKUP ? "Backup " : "",
452 s->proxy->id, s->id);
453
454 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
455
456 Warning("%s.\n", trash.str);
457 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
458
459 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
460 set_backend_down(s->proxy);
461
462 s->counters.down_trans++;
463 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200464 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200465
466 /* drain state is applied only if not yet in maint */
467 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200468 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
469
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200470 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200471 if (s->proxy->lbprm.set_server_status_down)
472 s->proxy->lbprm.set_server_status_down(s);
473
Willy Tarreau87b09662015-04-03 00:22:06 +0200474 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200475 * a connection. Those which are redispatchable will be queued
476 * to another server or to the proxy itself.
477 */
478 xferred = pendconn_redistribute(s);
479
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200480 chunk_printf(&trash, "%sServer %s/%s enters drain state",
481 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200482
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200483 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200484
485 Warning("%s.\n", trash.str);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200486 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200487
488 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
489 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200490 }
491
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200492 /* compute the inherited flag to propagate */
493 if (mode & SRV_ADMF_MAINT)
494 mode = SRV_ADMF_IMAINT;
495 else if (mode & SRV_ADMF_DRAIN)
496 mode = SRV_ADMF_IDRAIN;
497
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200498 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200499 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200500}
501
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200502/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
503 * stop enforcing either maint mode or drain mode. It is not allowed to set more
504 * than one flag at once. The equivalent "inherited" flag is propagated to all
505 * tracking servers. Leaving maintenance mode re-enables health checks. When
506 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200507 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200508void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200509{
510 struct check *check = &s->check;
511 struct server *srv;
512 int xferred = -1;
513
514 if (!mode)
515 return;
516
517 /* stop going down as soon as we see the flag is not there anymore */
518 if (!(s->admin & mode))
519 return;
520
521 s->admin &= ~mode;
522
523 if (s->admin & SRV_ADMF_MAINT) {
524 /* remaining in maintenance mode, let's inform precisely about the
525 * situation.
526 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200527 if (mode & SRV_ADMF_FMAINT) {
528 chunk_printf(&trash,
529 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
530 s->flags & SRV_F_BACKUP ? "Backup " : "",
531 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200532
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200533 if (s->track) /* normally it's mandatory here */
534 chunk_appendf(&trash, " via %s/%s",
535 s->track->proxy->id, s->track->id);
536 Warning("%s.\n", trash.str);
537 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
538 }
539 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200540 chunk_printf(&trash,
541 "%sServer %s/%s remains in forced maintenance",
542 s->flags & SRV_F_BACKUP ? "Backup " : "",
543 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200544 Warning("%s.\n", trash.str);
545 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
546 }
547 /* don't report anything when leaving drain mode and remaining in maintenance */
548 }
549 else if (mode & SRV_ADMF_MAINT) {
550 /* OK here we're leaving maintenance, we have many things to check,
551 * because the server might possibly be coming back up depending on
552 * its state. In practice, leaving maintenance means that we should
553 * immediately turn to UP (more or less the slowstart) under the
554 * following conditions :
555 * - server is neither checked nor tracked
556 * - server tracks another server which is not checked
557 * - server tracks another server which is already up
558 * Which sums up as something simpler :
559 * "either the tracking server is up or the server's checks are disabled
560 * or up". Otherwise we only re-enable health checks. There's a special
561 * case associated to the stopping state which can be inherited. Note
562 * that the server might still be in drain mode, which is naturally dealt
563 * with by the lower level functions.
564 */
565
566 if (s->check.state & CHK_ST_ENABLED) {
567 s->check.state &= ~CHK_ST_PAUSED;
568 check->health = check->rise; /* start OK but check immediately */
569 }
570
571 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
572 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
573 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
574 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
575 if (s->proxy->last_change < now.tv_sec) // ignore negative times
576 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
577 s->proxy->last_change = now.tv_sec;
578 }
579
580 if (s->last_change < now.tv_sec) // ignore negative times
581 s->down_time += now.tv_sec - s->last_change;
582 s->last_change = now.tv_sec;
583
584 if (s->track && s->track->state == SRV_ST_STOPPING)
585 s->state = SRV_ST_STOPPING;
586 else {
587 s->state = SRV_ST_STARTING;
588 if (s->slowstart > 0)
589 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
590 else
591 s->state = SRV_ST_RUNNING;
592 }
593
594 server_recalc_eweight(s);
595
596 /* If the server is set with "on-marked-up shutdown-backup-sessions",
597 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200598 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200599 * on all backup servers.
600 */
601 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
602 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200603 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200604
605 /* check if we can handle some connections queued at the proxy. We
606 * will take as many as we can handle.
607 */
608 xferred = pendconn_grab_from_px(s);
609 }
610
611 if (mode & SRV_ADMF_FMAINT) {
612 chunk_printf(&trash,
613 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
614 s->flags & SRV_F_BACKUP ? "Backup " : "",
615 s->proxy->id, s->id,
616 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
617 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200618 }
619 else {
620 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200621 "%sServer %s/%s is %s/%s (leaving maintenance)",
622 s->flags & SRV_F_BACKUP ? "Backup " : "",
623 s->proxy->id, s->id,
624 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
625 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
626 srv_append_status(&trash, s, NULL, xferred, 0);
627 }
628 Warning("%s.\n", trash.str);
629 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
630 }
631 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
632 /* remaining in drain mode after removing one of its flags */
633
634 if (mode & SRV_ADMF_FDRAIN) {
635 chunk_printf(&trash,
636 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200637 s->flags & SRV_F_BACKUP ? "Backup " : "",
638 s->proxy->id, s->id);
639
640 if (s->track) /* normally it's mandatory here */
641 chunk_appendf(&trash, " via %s/%s",
642 s->track->proxy->id, s->track->id);
643 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200644 else {
645 chunk_printf(&trash,
646 "%sServer %s/%s remains in forced drain mode",
647 s->flags & SRV_F_BACKUP ? "Backup " : "",
648 s->proxy->id, s->id);
649 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200650 Warning("%s.\n", trash.str);
651 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200652 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200653 else if (mode & SRV_ADMF_DRAIN) {
654 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200655 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
656 if (s->proxy->last_change < now.tv_sec) // ignore negative times
657 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
658 s->proxy->last_change = now.tv_sec;
659 }
660
661 if (s->last_change < now.tv_sec) // ignore negative times
662 s->down_time += now.tv_sec - s->last_change;
663 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200664 server_recalc_eweight(s);
665
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200666 if (mode & SRV_ADMF_FDRAIN) {
667 chunk_printf(&trash,
668 "%sServer %s/%s is %s (leaving forced drain)",
669 s->flags & SRV_F_BACKUP ? "Backup " : "",
670 s->proxy->id, s->id,
671 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
672 }
673 else {
674 chunk_printf(&trash,
675 "%sServer %s/%s is %s (leaving drain)",
676 s->flags & SRV_F_BACKUP ? "Backup " : "",
677 s->proxy->id, s->id,
678 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
679 if (s->track) /* normally it's mandatory here */
680 chunk_appendf(&trash, " via %s/%s",
681 s->track->proxy->id, s->track->id);
682 }
683 Warning("%s.\n", trash.str);
684 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200685 }
686
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200687 /* stop going down if the equivalent flag is still present (forced or inherited) */
688 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
689 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
690 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200691
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200692 if (mode & SRV_ADMF_MAINT)
693 mode = SRV_ADMF_IMAINT;
694 else if (mode & SRV_ADMF_DRAIN)
695 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200696
697 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200698 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200699}
700
Willy Tarreaudff55432012-10-10 17:51:05 +0200701/* Note: must not be declared <const> as its list will be overwritten.
702 * Please take care of keeping this list alphabetically sorted, doing so helps
703 * all code contributors.
704 * Optional keywords are also declared with a NULL ->parse() function so that
705 * the config parser can report an appropriate error when a known keyword was
706 * not enabled.
707 */
708static struct srv_kw_list srv_kws = { "ALL", { }, {
709 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
710 { NULL, NULL, 0 },
711}};
712
713__attribute__((constructor))
714static void __listener_init(void)
715{
716 srv_register_keywords(&srv_kws);
717}
718
Willy Tarreau004e0452013-11-21 11:22:01 +0100719/* Recomputes the server's eweight based on its state, uweight, the current time,
720 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
721 * state is automatically disabled if the time is elapsed.
722 */
723void server_recalc_eweight(struct server *sv)
724{
725 struct proxy *px = sv->proxy;
726 unsigned w;
727
728 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
729 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200730 if (sv->state == SRV_ST_STARTING)
731 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100732 }
733
734 /* We must take care of not pushing the server to full throttle during slow starts.
735 * It must also start immediately, at least at the minimal step when leaving maintenance.
736 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200737 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100738 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
739 else
740 w = px->lbprm.wdiv;
741
742 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
743
744 /* now propagate the status change to any LB algorithms */
745 if (px->lbprm.update_server_eweight)
746 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200747 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100748 if (px->lbprm.set_server_status_up)
749 px->lbprm.set_server_status_up(sv);
750 }
751 else {
752 if (px->lbprm.set_server_status_down)
753 px->lbprm.set_server_status_down(sv);
754 }
755}
756
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900758 * Parses weight_str and configures sv accordingly.
759 * Returns NULL on success, error message string otherwise.
760 */
761const char *server_parse_weight_change_request(struct server *sv,
762 const char *weight_str)
763{
764 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900765 long int w;
766 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900767
768 px = sv->proxy;
769
770 /* if the weight is terminated with '%', it is set relative to
771 * the initial weight, otherwise it is absolute.
772 */
773 if (!*weight_str)
774 return "Require <weight> or <weight%>.\n";
775
Simon Hormanb796afa2013-02-12 10:45:53 +0900776 w = strtol(weight_str, &end, 10);
777 if (end == weight_str)
778 return "Empty weight string empty or preceded by garbage";
779 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900780 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900781 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900782 /* Avoid integer overflow */
783 if (w > 25600)
784 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900785 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900786 if (w > 256)
787 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900788 }
789 else if (w < 0 || w > 256)
790 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900791 else if (end[0] != '\0')
792 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900793
794 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
795 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
796
797 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100798 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900799
800 return NULL;
801}
802
Willy Tarreau272adea2014-03-31 10:39:59 +0200803int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
804{
805 struct server *newsrv = NULL;
806 const char *err;
807 char *errmsg = NULL;
808 int err_code = 0;
809 unsigned val;
810
811 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
812 int cur_arg;
813 short realport = 0;
814 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
815
816 if (!defsrv && curproxy == defproxy) {
817 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
818 err_code |= ERR_ALERT | ERR_FATAL;
819 goto out;
820 }
821 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
822 err_code |= ERR_ALERT | ERR_FATAL;
823
824 if (!*args[2]) {
825 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
826 file, linenum, args[0]);
827 err_code |= ERR_ALERT | ERR_FATAL;
828 goto out;
829 }
830
831 err = invalid_char(args[1]);
832 if (err && !defsrv) {
833 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
834 file, linenum, *err, args[1]);
835 err_code |= ERR_ALERT | ERR_FATAL;
836 goto out;
837 }
838
839 if (!defsrv) {
840 struct sockaddr_storage *sk;
841 int port1, port2;
842 struct protocol *proto;
843
844 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
845 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
846 err_code |= ERR_ALERT | ERR_ABORT;
847 goto out;
848 }
849
850 /* the servers are linked backwards first */
851 newsrv->next = curproxy->srv;
852 curproxy->srv = newsrv;
853 newsrv->proxy = curproxy;
854 newsrv->conf.file = strdup(file);
855 newsrv->conf.line = linenum;
856
857 newsrv->obj_type = OBJ_TYPE_SERVER;
858 LIST_INIT(&newsrv->actconns);
859 LIST_INIT(&newsrv->pendconns);
860 do_check = 0;
861 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200862 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200863 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200864 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200865 newsrv->last_change = now.tv_sec;
866 newsrv->id = strdup(args[1]);
867
868 /* several ways to check the port component :
869 * - IP => port=+0, relative (IPv4 only)
870 * - IP: => port=+0, relative
871 * - IP:N => port=N, absolute
872 * - IP:+N => port=+N, relative
873 * - IP:-N => port=-N, relative
874 */
875 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
876 if (!sk) {
877 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
878 err_code |= ERR_ALERT | ERR_FATAL;
879 goto out;
880 }
881
882 proto = protocol_by_family(sk->ss_family);
883 if (!proto || !proto->connect) {
884 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
885 file, linenum, args[0], args[1]);
886 err_code |= ERR_ALERT | ERR_FATAL;
887 goto out;
888 }
889
890 if (!port1 || !port2) {
891 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200892 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200893 }
894 else if (port1 != port2) {
895 /* port range */
896 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
897 file, linenum, args[0], args[1], args[2]);
898 err_code |= ERR_ALERT | ERR_FATAL;
899 goto out;
900 }
901 else {
902 /* used by checks */
903 realport = port1;
904 }
905
906 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +0100907 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200908
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +0100909 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200910 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
911 file, linenum, newsrv->addr.ss_family, args[2]);
912 err_code |= ERR_ALERT | ERR_FATAL;
913 goto out;
914 }
915
916 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
917 newsrv->check.port = curproxy->defsrv.check.port;
918 newsrv->check.inter = curproxy->defsrv.check.inter;
919 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
920 newsrv->check.downinter = curproxy->defsrv.check.downinter;
921 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
922 newsrv->agent.port = curproxy->defsrv.agent.port;
923 newsrv->agent.inter = curproxy->defsrv.agent.inter;
924 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
925 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
926 newsrv->maxqueue = curproxy->defsrv.maxqueue;
927 newsrv->minconn = curproxy->defsrv.minconn;
928 newsrv->maxconn = curproxy->defsrv.maxconn;
929 newsrv->slowstart = curproxy->defsrv.slowstart;
930 newsrv->onerror = curproxy->defsrv.onerror;
931 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
932 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
933 newsrv->consecutive_errors_limit
934 = curproxy->defsrv.consecutive_errors_limit;
935#ifdef OPENSSL
936 newsrv->use_ssl = curproxy->defsrv.use_ssl;
937#endif
938 newsrv->uweight = newsrv->iweight
939 = curproxy->defsrv.iweight;
940
941 newsrv->check.status = HCHK_STATUS_INI;
942 newsrv->check.rise = curproxy->defsrv.check.rise;
943 newsrv->check.fall = curproxy->defsrv.check.fall;
944 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
945 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +0900946 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +0200947
948 newsrv->agent.status = HCHK_STATUS_INI;
949 newsrv->agent.rise = curproxy->defsrv.agent.rise;
950 newsrv->agent.fall = curproxy->defsrv.agent.fall;
951 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
952 newsrv->agent.server = newsrv;
953
954 cur_arg = 3;
955 } else {
956 newsrv = &curproxy->defsrv;
957 cur_arg = 1;
958 }
959
960 while (*args[cur_arg]) {
961 if (!strcmp(args[cur_arg], "agent-check")) {
962 global.maxsock++;
963 do_agent = 1;
964 cur_arg += 1;
965 } else if (!strcmp(args[cur_arg], "agent-inter")) {
966 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
967 if (err) {
968 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
969 file, linenum, *err, newsrv->id);
970 err_code |= ERR_ALERT | ERR_FATAL;
971 goto out;
972 }
973 if (val <= 0) {
974 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
975 file, linenum, val, args[cur_arg], newsrv->id);
976 err_code |= ERR_ALERT | ERR_FATAL;
977 goto out;
978 }
979 newsrv->agent.inter = val;
980 cur_arg += 2;
981 }
982 else if (!strcmp(args[cur_arg], "agent-port")) {
983 global.maxsock++;
984 newsrv->agent.port = atol(args[cur_arg + 1]);
985 cur_arg += 2;
986 }
987 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
988 newsrv->cookie = strdup(args[cur_arg + 1]);
989 newsrv->cklen = strlen(args[cur_arg + 1]);
990 cur_arg += 2;
991 }
992 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
993 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
994 newsrv->rdr_len = strlen(args[cur_arg + 1]);
995 cur_arg += 2;
996 }
997 else if (!strcmp(args[cur_arg], "rise")) {
998 if (!*args[cur_arg + 1]) {
999 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1000 file, linenum, args[cur_arg]);
1001 err_code |= ERR_ALERT | ERR_FATAL;
1002 goto out;
1003 }
1004
1005 newsrv->check.rise = atol(args[cur_arg + 1]);
1006 if (newsrv->check.rise <= 0) {
1007 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1008 file, linenum, args[cur_arg]);
1009 err_code |= ERR_ALERT | ERR_FATAL;
1010 goto out;
1011 }
1012
1013 if (newsrv->check.health)
1014 newsrv->check.health = newsrv->check.rise;
1015 cur_arg += 2;
1016 }
1017 else if (!strcmp(args[cur_arg], "fall")) {
1018 newsrv->check.fall = atol(args[cur_arg + 1]);
1019
1020 if (!*args[cur_arg + 1]) {
1021 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1022 file, linenum, args[cur_arg]);
1023 err_code |= ERR_ALERT | ERR_FATAL;
1024 goto out;
1025 }
1026
1027 if (newsrv->check.fall <= 0) {
1028 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1029 file, linenum, args[cur_arg]);
1030 err_code |= ERR_ALERT | ERR_FATAL;
1031 goto out;
1032 }
1033
1034 cur_arg += 2;
1035 }
1036 else if (!strcmp(args[cur_arg], "inter")) {
1037 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1038 if (err) {
1039 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1040 file, linenum, *err, newsrv->id);
1041 err_code |= ERR_ALERT | ERR_FATAL;
1042 goto out;
1043 }
1044 if (val <= 0) {
1045 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1046 file, linenum, val, args[cur_arg], newsrv->id);
1047 err_code |= ERR_ALERT | ERR_FATAL;
1048 goto out;
1049 }
1050 newsrv->check.inter = val;
1051 cur_arg += 2;
1052 }
1053 else if (!strcmp(args[cur_arg], "fastinter")) {
1054 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1055 if (err) {
1056 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1057 file, linenum, *err, newsrv->id);
1058 err_code |= ERR_ALERT | ERR_FATAL;
1059 goto out;
1060 }
1061 if (val <= 0) {
1062 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1063 file, linenum, val, args[cur_arg], newsrv->id);
1064 err_code |= ERR_ALERT | ERR_FATAL;
1065 goto out;
1066 }
1067 newsrv->check.fastinter = val;
1068 cur_arg += 2;
1069 }
1070 else if (!strcmp(args[cur_arg], "downinter")) {
1071 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1072 if (err) {
1073 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1074 file, linenum, *err, newsrv->id);
1075 err_code |= ERR_ALERT | ERR_FATAL;
1076 goto out;
1077 }
1078 if (val <= 0) {
1079 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1080 file, linenum, val, args[cur_arg], newsrv->id);
1081 err_code |= ERR_ALERT | ERR_FATAL;
1082 goto out;
1083 }
1084 newsrv->check.downinter = val;
1085 cur_arg += 2;
1086 }
1087 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1088 struct sockaddr_storage *sk;
1089 int port1, port2;
1090 struct protocol *proto;
1091
1092 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1093 if (!sk) {
1094 Alert("parsing [%s:%d] : '%s' : %s\n",
1095 file, linenum, args[cur_arg], errmsg);
1096 err_code |= ERR_ALERT | ERR_FATAL;
1097 goto out;
1098 }
1099
1100 proto = protocol_by_family(sk->ss_family);
1101 if (!proto || !proto->connect) {
1102 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1103 file, linenum, args[cur_arg], args[cur_arg + 1]);
1104 err_code |= ERR_ALERT | ERR_FATAL;
1105 goto out;
1106 }
1107
1108 if (port1 != port2) {
1109 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1110 file, linenum, args[cur_arg], args[cur_arg + 1]);
1111 err_code |= ERR_ALERT | ERR_FATAL;
1112 goto out;
1113 }
1114
Simon Horman41f58762015-01-30 11:22:56 +09001115 newsrv->check.addr = newsrv->agent.addr = *sk;
Willy Tarreau272adea2014-03-31 10:39:59 +02001116 cur_arg += 2;
1117 }
1118 else if (!strcmp(args[cur_arg], "port")) {
1119 newsrv->check.port = atol(args[cur_arg + 1]);
1120 cur_arg += 2;
1121 }
1122 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001123 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001124 cur_arg ++;
1125 }
1126 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001127 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001128 cur_arg ++;
1129 }
1130 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001131 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001132 cur_arg ++;
1133 }
David Safb76832014-05-08 23:42:08 -04001134 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1135 newsrv->pp_opts |= SRV_PP_V2;
1136 cur_arg ++;
1137 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001138 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1139 newsrv->check.send_proxy = 1;
1140 cur_arg ++;
1141 }
1142 else if (!strcmp(args[cur_arg], "weight")) {
1143 int w;
1144 w = atol(args[cur_arg + 1]);
1145 if (w < 0 || w > SRV_UWGHT_MAX) {
1146 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1147 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1148 err_code |= ERR_ALERT | ERR_FATAL;
1149 goto out;
1150 }
1151 newsrv->uweight = newsrv->iweight = w;
1152 cur_arg += 2;
1153 }
1154 else if (!strcmp(args[cur_arg], "minconn")) {
1155 newsrv->minconn = atol(args[cur_arg + 1]);
1156 cur_arg += 2;
1157 }
1158 else if (!strcmp(args[cur_arg], "maxconn")) {
1159 newsrv->maxconn = atol(args[cur_arg + 1]);
1160 cur_arg += 2;
1161 }
1162 else if (!strcmp(args[cur_arg], "maxqueue")) {
1163 newsrv->maxqueue = atol(args[cur_arg + 1]);
1164 cur_arg += 2;
1165 }
1166 else if (!strcmp(args[cur_arg], "slowstart")) {
1167 /* slowstart is stored in seconds */
1168 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1169 if (err) {
1170 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1171 file, linenum, *err, newsrv->id);
1172 err_code |= ERR_ALERT | ERR_FATAL;
1173 goto out;
1174 }
1175 newsrv->slowstart = (val + 999) / 1000;
1176 cur_arg += 2;
1177 }
1178 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1179
1180 if (!*args[cur_arg + 1]) {
1181 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1182 file, linenum);
1183 err_code |= ERR_ALERT | ERR_FATAL;
1184 goto out;
1185 }
1186
1187 newsrv->trackit = strdup(args[cur_arg + 1]);
1188
1189 cur_arg += 2;
1190 }
1191 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1192 global.maxsock++;
1193 do_check = 1;
1194 cur_arg += 1;
1195 }
1196 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001197 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001198 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001199 newsrv->check.state |= CHK_ST_PAUSED;
1200 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001201 cur_arg += 1;
1202 }
1203 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1204 if (!strcmp(args[cur_arg + 1], "none"))
1205 newsrv->observe = HANA_OBS_NONE;
1206 else if (!strcmp(args[cur_arg + 1], "layer4"))
1207 newsrv->observe = HANA_OBS_LAYER4;
1208 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1209 if (curproxy->mode != PR_MODE_HTTP) {
1210 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1211 file, linenum, args[cur_arg + 1]);
1212 err_code |= ERR_ALERT;
1213 }
1214 newsrv->observe = HANA_OBS_LAYER7;
1215 }
1216 else {
1217 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1218 "'layer4', 'layer7' but got '%s'\n",
1219 file, linenum, args[cur_arg], args[cur_arg + 1]);
1220 err_code |= ERR_ALERT | ERR_FATAL;
1221 goto out;
1222 }
1223
1224 cur_arg += 2;
1225 }
1226 else if (!strcmp(args[cur_arg], "on-error")) {
1227 if (!strcmp(args[cur_arg + 1], "fastinter"))
1228 newsrv->onerror = HANA_ONERR_FASTINTER;
1229 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1230 newsrv->onerror = HANA_ONERR_FAILCHK;
1231 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1232 newsrv->onerror = HANA_ONERR_SUDDTH;
1233 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1234 newsrv->onerror = HANA_ONERR_MARKDWN;
1235 else {
1236 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1237 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1238 file, linenum, args[cur_arg], args[cur_arg + 1]);
1239 err_code |= ERR_ALERT | ERR_FATAL;
1240 goto out;
1241 }
1242
1243 cur_arg += 2;
1244 }
1245 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1246 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1247 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1248 else {
1249 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1250 file, linenum, args[cur_arg], args[cur_arg + 1]);
1251 err_code |= ERR_ALERT | ERR_FATAL;
1252 goto out;
1253 }
1254
1255 cur_arg += 2;
1256 }
1257 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1258 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1259 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1260 else {
1261 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1262 file, linenum, args[cur_arg], args[cur_arg + 1]);
1263 err_code |= ERR_ALERT | ERR_FATAL;
1264 goto out;
1265 }
1266
1267 cur_arg += 2;
1268 }
1269 else if (!strcmp(args[cur_arg], "error-limit")) {
1270 if (!*args[cur_arg + 1]) {
1271 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1272 file, linenum, args[cur_arg]);
1273 err_code |= ERR_ALERT | ERR_FATAL;
1274 goto out;
1275 }
1276
1277 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1278
1279 if (newsrv->consecutive_errors_limit <= 0) {
1280 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1281 file, linenum, args[cur_arg]);
1282 err_code |= ERR_ALERT | ERR_FATAL;
1283 goto out;
1284 }
1285 cur_arg += 2;
1286 }
1287 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1288 int port_low, port_high;
1289 struct sockaddr_storage *sk;
1290 struct protocol *proto;
1291
1292 if (!*args[cur_arg + 1]) {
1293 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1294 file, linenum, "source", "usesrc", "interface");
1295 err_code |= ERR_ALERT | ERR_FATAL;
1296 goto out;
1297 }
1298
1299 newsrv->conn_src.opts |= CO_SRC_BIND;
1300 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1301 if (!sk) {
1302 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1303 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1304 err_code |= ERR_ALERT | ERR_FATAL;
1305 goto out;
1306 }
1307
1308 proto = protocol_by_family(sk->ss_family);
1309 if (!proto || !proto->connect) {
1310 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1311 file, linenum, args[cur_arg], args[cur_arg+1]);
1312 err_code |= ERR_ALERT | ERR_FATAL;
1313 goto out;
1314 }
1315
1316 newsrv->conn_src.source_addr = *sk;
1317
1318 if (port_low != port_high) {
1319 int i;
1320
1321 if (!port_low || !port_high) {
1322 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1323 file, linenum, args[cur_arg], args[cur_arg + 1]);
1324 err_code |= ERR_ALERT | ERR_FATAL;
1325 goto out;
1326 }
1327
1328 if (port_low <= 0 || port_low > 65535 ||
1329 port_high <= 0 || port_high > 65535 ||
1330 port_low > port_high) {
1331 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1332 file, linenum, port_low, port_high);
1333 err_code |= ERR_ALERT | ERR_FATAL;
1334 goto out;
1335 }
1336 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1337 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1338 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1339 }
1340
1341 cur_arg += 2;
1342 while (*(args[cur_arg])) {
1343 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1344#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1345#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001346 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001347 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1348 file, linenum, "usesrc", "source");
1349 err_code |= ERR_ALERT | ERR_FATAL;
1350 goto out;
1351 }
1352#endif
1353 if (!*args[cur_arg + 1]) {
1354 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1355 file, linenum, "usesrc");
1356 err_code |= ERR_ALERT | ERR_FATAL;
1357 goto out;
1358 }
1359 if (!strcmp(args[cur_arg + 1], "client")) {
1360 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1361 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1362 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1363 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1364 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1365 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1366 char *name, *end;
1367
1368 name = args[cur_arg+1] + 7;
1369 while (isspace(*name))
1370 name++;
1371
1372 end = name;
1373 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1374 end++;
1375
1376 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1377 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1378 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1379 newsrv->conn_src.bind_hdr_len = end - name;
1380 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1381 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1382 newsrv->conn_src.bind_hdr_occ = -1;
1383
1384 /* now look for an occurrence number */
1385 while (isspace(*end))
1386 end++;
1387 if (*end == ',') {
1388 end++;
1389 name = end;
1390 if (*end == '-')
1391 end++;
1392 while (isdigit((int)*end))
1393 end++;
1394 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1395 }
1396
1397 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1398 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1399 " occurrences values smaller than %d.\n",
1400 file, linenum, MAX_HDR_HISTORY);
1401 err_code |= ERR_ALERT | ERR_FATAL;
1402 goto out;
1403 }
1404 } else {
1405 struct sockaddr_storage *sk;
1406 int port1, port2;
1407
1408 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1409 if (!sk) {
1410 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1411 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1412 err_code |= ERR_ALERT | ERR_FATAL;
1413 goto out;
1414 }
1415
1416 proto = protocol_by_family(sk->ss_family);
1417 if (!proto || !proto->connect) {
1418 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\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 (port1 != port2) {
1425 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1426 file, linenum, args[cur_arg], args[cur_arg + 1]);
1427 err_code |= ERR_ALERT | ERR_FATAL;
1428 goto out;
1429 }
1430 newsrv->conn_src.tproxy_addr = *sk;
1431 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1432 }
1433 global.last_checks |= LSTCHK_NETADM;
1434#if !defined(CONFIG_HAP_TRANSPARENT)
1435 global.last_checks |= LSTCHK_CTTPROXY;
1436#endif
1437 cur_arg += 2;
1438 continue;
1439#else /* no TPROXY support */
1440 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1441 file, linenum, "usesrc");
1442 err_code |= ERR_ALERT | ERR_FATAL;
1443 goto out;
1444#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1445 } /* "usesrc" */
1446
1447 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1448#ifdef SO_BINDTODEVICE
1449 if (!*args[cur_arg + 1]) {
1450 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1451 file, linenum, args[0]);
1452 err_code |= ERR_ALERT | ERR_FATAL;
1453 goto out;
1454 }
1455 free(newsrv->conn_src.iface_name);
1456 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1457 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1458 global.last_checks |= LSTCHK_NETADM;
1459#else
1460 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1461 file, linenum, args[0], args[cur_arg]);
1462 err_code |= ERR_ALERT | ERR_FATAL;
1463 goto out;
1464#endif
1465 cur_arg += 2;
1466 continue;
1467 }
1468 /* this keyword in not an option of "source" */
1469 break;
1470 } /* while */
1471 }
1472 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1473 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1474 file, linenum, "usesrc", "source");
1475 err_code |= ERR_ALERT | ERR_FATAL;
1476 goto out;
1477 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001478 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1479#ifdef CONFIG_HAP_NS
1480 char *arg = args[cur_arg + 1];
1481 if (!strcmp(arg, "*")) {
1482 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1483 } else {
1484 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1485
1486 if (newsrv->netns == NULL)
1487 newsrv->netns = netns_store_insert(arg);
1488
1489 if (newsrv->netns == NULL) {
1490 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1491 err_code |= ERR_ALERT | ERR_FATAL;
1492 goto out;
1493 }
1494 }
1495#else
1496 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1497 file, linenum, args[0], args[cur_arg]);
1498 err_code |= ERR_ALERT | ERR_FATAL;
1499 goto out;
1500#endif
1501 cur_arg += 2;
1502 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001503 else {
1504 static int srv_dumped;
1505 struct srv_kw *kw;
1506 char *err;
1507
1508 kw = srv_find_kw(args[cur_arg]);
1509 if (kw) {
1510 char *err = NULL;
1511 int code;
1512
1513 if (!kw->parse) {
1514 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1515 file, linenum, args[0], args[1], args[cur_arg]);
1516 cur_arg += 1 + kw->skip ;
1517 err_code |= ERR_ALERT | ERR_FATAL;
1518 goto out;
1519 }
1520
1521 if (defsrv && !kw->default_ok) {
1522 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1523 file, linenum, args[0], args[1], args[cur_arg]);
1524 cur_arg += 1 + kw->skip ;
1525 err_code |= ERR_ALERT;
1526 continue;
1527 }
1528
1529 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1530 err_code |= code;
1531
1532 if (code) {
1533 if (err && *err) {
1534 indent_msg(&err, 2);
1535 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1536 }
1537 else
1538 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1539 file, linenum, args[0], args[1], args[cur_arg]);
1540 if (code & ERR_FATAL) {
1541 free(err);
1542 cur_arg += 1 + kw->skip;
1543 goto out;
1544 }
1545 }
1546 free(err);
1547 cur_arg += 1 + kw->skip;
1548 continue;
1549 }
1550
1551 err = NULL;
1552 if (!srv_dumped) {
1553 srv_dump_kws(&err);
1554 indent_msg(&err, 4);
1555 srv_dumped = 1;
1556 }
1557
1558 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1559 file, linenum, args[0], args[1], args[cur_arg],
1560 err ? " Registered keywords :" : "", err ? err : "");
1561 free(err);
1562
1563 err_code |= ERR_ALERT | ERR_FATAL;
1564 goto out;
1565 }
1566 }
1567
Willy Tarreau272adea2014-03-31 10:39:59 +02001568 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001569 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001570
1571 if (newsrv->trackit) {
1572 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1573 file, linenum);
1574 err_code |= ERR_ALERT | ERR_FATAL;
1575 goto out;
1576 }
1577
1578 /* If neither a port nor an addr was specified and no check transport
1579 * layer is forced, then the transport layer used by the checks is the
1580 * same as for the production traffic. Otherwise we use raw_sock by
1581 * default, unless one is specified.
1582 */
Simon Horman41f58762015-01-30 11:22:56 +09001583 if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001584#ifdef USE_OPENSSL
1585 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1586#endif
David Safb76832014-05-08 23:42:08 -04001587 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001588 }
1589 /* try to get the port from check_core.addr if check.port not set */
1590 if (!newsrv->check.port)
Simon Horman41f58762015-01-30 11:22:56 +09001591 newsrv->check.port = get_host_port(&newsrv->check.addr);
Willy Tarreau272adea2014-03-31 10:39:59 +02001592
1593 if (!newsrv->check.port)
1594 newsrv->check.port = realport; /* by default */
1595
1596 if (!newsrv->check.port) {
1597 /* not yet valid, because no port was set on
1598 * the server either. We'll check if we have
1599 * a known port on the first listener.
1600 */
1601 struct listener *l;
1602
1603 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1604 newsrv->check.port = get_host_port(&l->addr);
1605 if (newsrv->check.port)
1606 break;
1607 }
1608 }
1609 /*
1610 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001611 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001612 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001613 if (!newsrv->check.port &&
Simon Horman41f58762015-01-30 11:22:56 +09001614 (is_inet_addr(&newsrv->check.addr) ||
1615 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001616 struct tcpcheck_rule *n = NULL, *r = NULL;
1617 struct list *l;
1618
1619 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1620 if (!r) {
1621 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1622 file, linenum, newsrv->id);
1623 err_code |= ERR_ALERT | ERR_FATAL;
1624 goto out;
1625 }
1626 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1627 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",
1628 file, linenum, newsrv->id);
1629 err_code |= ERR_ALERT | ERR_FATAL;
1630 goto out;
1631 }
1632 else {
1633 /* scan the tcp-check ruleset to ensure a port has been configured */
1634 l = &newsrv->proxy->tcpcheck_rules;
1635 list_for_each_entry(n, l, list) {
1636 r = (struct tcpcheck_rule *)n->list.p;
1637 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1638 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",
1639 file, linenum, newsrv->id);
1640 err_code |= ERR_ALERT | ERR_FATAL;
1641 goto out;
1642 }
1643 }
1644 }
1645 }
1646
1647 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001648 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001649 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001650 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1651 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001652 goto out;
1653 }
1654
1655 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1656 }
1657
1658 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001659 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001660
1661 if (!newsrv->agent.port) {
1662 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1663 file, linenum, newsrv->id);
1664 err_code |= ERR_ALERT | ERR_FATAL;
1665 goto out;
1666 }
1667
1668 if (!newsrv->agent.inter)
1669 newsrv->agent.inter = newsrv->check.inter;
1670
Simon Hormanb1900d52015-01-30 11:22:54 +09001671 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001672 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001673 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1674 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001675 goto out;
1676 }
1677
1678 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1679 }
1680
1681 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001682 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001683 curproxy->srv_bck++;
1684 else
1685 curproxy->srv_act++;
1686
Willy Tarreauc5150da2014-05-13 19:27:31 +02001687 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001688 }
1689 }
1690 return 0;
1691
1692 out:
1693 free(errmsg);
1694 return err_code;
1695}
1696
Simon Horman7d09b9a2013-02-12 10:45:51 +09001697/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001698 * Local variables:
1699 * c-indent-level: 8
1700 * c-basic-offset: 8
1701 * End:
1702 */