blob: 9a3bf23d2311df0b02b72b247511e5e27a8c9cdc [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 Tarreau4aac7db2014-05-16 11:48:10 +020030#include <proto/session.h>
31#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
166 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
167 * shutdown.
168 */
169void srv_shutdown_sessions(struct server *srv, int why)
170{
171 struct session *session, *session_bck;
172
173 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
174 if (session->srv_conn == srv)
175 session_shutdown(session, why);
176}
177
178/* Shutdown all connections of all backup servers of a proxy. The caller must
179 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
180 * the reason for the shutdown.
181 */
182void srv_shutdown_backup_sessions(struct proxy *px, int why)
183{
184 struct server *srv;
185
186 for (srv = px->srv; srv != NULL; srv = srv->next)
187 if (srv->flags & SRV_F_BACKUP)
188 srv_shutdown_sessions(srv, why);
189}
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).
196 * If <xferred> is non-negative, some information about requeued sessions are
197 * 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
224 * queued sessions whenever possible to other servers. It automatically
225 * 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);
235 int xferred;
236
237 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
238 return;
239
240 s->last_change = now.tv_sec;
241 s->state = SRV_ST_STOPPED;
242 if (s->proxy->lbprm.set_server_status_down)
243 s->proxy->lbprm.set_server_status_down(s);
244
245 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
246 srv_shutdown_sessions(s, SN_ERR_DOWN);
247
248 /* we might have sessions queued on this server and waiting for
249 * a connection. Those which are redispatchable will be queued
250 * to another server or to the proxy itself.
251 */
252 xferred = pendconn_redistribute(s);
253
254 chunk_printf(&trash,
255 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
256 s->proxy->id, s->id);
257
258 srv_append_status(&trash, s, reason, xferred, 0);
259 Warning("%s.\n", trash.str);
260
261 /* we don't send an alert if the server was previously paused */
262 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
263
264 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
265 set_backend_down(s->proxy);
266
267 s->counters.down_trans++;
268
269 for (srv = s->trackers; srv; srv = srv->tracknext)
270 srv_set_stopped(srv, NULL);
271}
272
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200273/* Marks server <s> up regardless of its checks' statuses and provided it isn't
274 * in maintenance. Notifies by all available means, recounts the remaining
275 * servers on the proxy and tries to grab requests from the proxy. It
276 * automatically recomputes the number of servers, but not the map. Maintenance
277 * servers are ignored. It reports <reason> if non-null as the reason for going
278 * up. Note that it makes use of the trash to build the log strings, so <reason>
279 * must not be placed there.
280 */
281void srv_set_running(struct server *s, const char *reason)
282{
283 struct server *srv;
284 int xferred;
285
286 if (s->admin & SRV_ADMF_MAINT)
287 return;
288
289 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
290 return;
291
292 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
293 if (s->proxy->last_change < now.tv_sec) // ignore negative times
294 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
295 s->proxy->last_change = now.tv_sec;
296 }
297
298 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
299 s->down_time += now.tv_sec - s->last_change;
300
301 s->last_change = now.tv_sec;
302
303 s->state = SRV_ST_STARTING;
304 if (s->slowstart > 0)
305 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
306 else
307 s->state = SRV_ST_RUNNING;
308
309 server_recalc_eweight(s);
310
311 /* If the server is set with "on-marked-up shutdown-backup-sessions",
312 * and it's not a backup server and its effective weight is > 0,
313 * then it can accept new connections, so we shut down all sessions
314 * on all backup servers.
315 */
316 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
317 !(s->flags & SRV_F_BACKUP) && s->eweight)
318 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
319
320 /* check if we can handle some connections queued at the proxy. We
321 * will take as many as we can handle.
322 */
323 xferred = pendconn_grab_from_px(s);
324
325 chunk_printf(&trash,
326 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
327 s->proxy->id, s->id);
328
329 srv_append_status(&trash, s, reason, xferred, 0);
330 Warning("%s.\n", trash.str);
331 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
332
333 for (srv = s->trackers; srv; srv = srv->tracknext)
334 srv_set_running(srv, NULL);
335}
336
Willy Tarreau8eb77842014-05-21 13:54:57 +0200337/* Marks server <s> stopping regardless of its checks' statuses and provided it
338 * isn't in maintenance. Notifies by all available means, recounts the remaining
339 * servers on the proxy and tries to grab requests from the proxy. It
340 * automatically recomputes the number of servers, but not the map. Maintenance
341 * servers are ignored. It reports <reason> if non-null as the reason for going
342 * up. Note that it makes use of the trash to build the log strings, so <reason>
343 * must not be placed there.
344 */
345void srv_set_stopping(struct server *s, const char *reason)
346{
347 struct server *srv;
348 int xferred;
349
350 if (s->admin & SRV_ADMF_MAINT)
351 return;
352
353 if (s->state == SRV_ST_STOPPING)
354 return;
355
356 s->last_change = now.tv_sec;
357 s->state = SRV_ST_STOPPING;
358 if (s->proxy->lbprm.set_server_status_down)
359 s->proxy->lbprm.set_server_status_down(s);
360
361 /* we might have sessions queued on this server and waiting for
362 * a connection. Those which are redispatchable will be queued
363 * to another server or to the proxy itself.
364 */
365 xferred = pendconn_redistribute(s);
366
367 chunk_printf(&trash,
368 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
369 s->proxy->id, s->id);
370
371 srv_append_status(&trash, s, reason, xferred, 0);
372
373 Warning("%s.\n", trash.str);
374 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
375
376 if (!s->proxy->srv_bck && !s->proxy->srv_act)
377 set_backend_down(s->proxy);
378
379 for (srv = s->trackers; srv; srv = srv->tracknext)
380 srv_set_stopping(srv, NULL);
381}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200382
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200383/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
384 * enforce either maint mode or drain mode. It is not allowed to set more than
385 * one flag at once. The equivalent "inherited" flag is propagated to all
386 * tracking servers. Maintenance mode disables health checks (but not agent
387 * checks). When either the flag is already set or no flag is passed, nothing
388 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200389 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200390void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200391{
392 struct check *check = &s->check;
393 struct server *srv;
394 int xferred;
395
396 if (!mode)
397 return;
398
399 /* stop going down as soon as we meet a server already in the same state */
400 if (s->admin & mode)
401 return;
402
403 s->admin |= mode;
404
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200405 /* stop going down if the equivalent flag was already present (forced or inherited) */
406 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
407 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
408 return;
409
410 /* Maintenance must also disable health checks */
411 if (mode & SRV_ADMF_MAINT) {
412 if (s->check.state & CHK_ST_ENABLED) {
413 s->check.state |= CHK_ST_PAUSED;
414 check->health = 0;
415 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200416
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200417 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200418 chunk_printf(&trash,
419 "%sServer %s/%s was DOWN and now enters maintenance",
420 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
421
Willy Tarreaubda92272014-05-20 21:55:30 +0200422 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200423
424 Warning("%s.\n", trash.str);
425 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
426 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200427 else { /* server was still running */
428 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
429 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
430
431 check->health = 0; /* failure */
432 s->last_change = now.tv_sec;
433 s->state = SRV_ST_STOPPED;
434 if (s->proxy->lbprm.set_server_status_down)
435 s->proxy->lbprm.set_server_status_down(s);
436
437 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
438 srv_shutdown_sessions(s, SN_ERR_DOWN);
439
440 /* we might have sessions queued on this server and waiting for
441 * a connection. Those which are redispatchable will be queued
442 * to another server or to the proxy itself.
443 */
444 xferred = pendconn_redistribute(s);
445
446 chunk_printf(&trash,
447 "%sServer %s/%s is going DOWN for maintenance",
448 s->flags & SRV_F_BACKUP ? "Backup " : "",
449 s->proxy->id, s->id);
450
451 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
452
453 Warning("%s.\n", trash.str);
454 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
455
456 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
457 set_backend_down(s->proxy);
458
459 s->counters.down_trans++;
460 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200461 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200462
463 /* drain state is applied only if not yet in maint */
464 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200465 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
466
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200467 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200468 if (s->proxy->lbprm.set_server_status_down)
469 s->proxy->lbprm.set_server_status_down(s);
470
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200471 /* we might have sessions queued on this server and waiting for
472 * a connection. Those which are redispatchable will be queued
473 * to another server or to the proxy itself.
474 */
475 xferred = pendconn_redistribute(s);
476
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200477 chunk_printf(&trash, "%sServer %s/%s enters drain state",
478 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200479
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200480 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200481
482 Warning("%s.\n", trash.str);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200483 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200484
485 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
486 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200487 }
488
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200489 /* compute the inherited flag to propagate */
490 if (mode & SRV_ADMF_MAINT)
491 mode = SRV_ADMF_IMAINT;
492 else if (mode & SRV_ADMF_DRAIN)
493 mode = SRV_ADMF_IDRAIN;
494
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200495 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200496 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200497}
498
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200499/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
500 * stop enforcing either maint mode or drain mode. It is not allowed to set more
501 * than one flag at once. The equivalent "inherited" flag is propagated to all
502 * tracking servers. Leaving maintenance mode re-enables health checks. When
503 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200504 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200505void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200506{
507 struct check *check = &s->check;
508 struct server *srv;
509 int xferred = -1;
510
511 if (!mode)
512 return;
513
514 /* stop going down as soon as we see the flag is not there anymore */
515 if (!(s->admin & mode))
516 return;
517
518 s->admin &= ~mode;
519
520 if (s->admin & SRV_ADMF_MAINT) {
521 /* remaining in maintenance mode, let's inform precisely about the
522 * situation.
523 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200524 if (mode & SRV_ADMF_FMAINT) {
525 chunk_printf(&trash,
526 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
527 s->flags & SRV_F_BACKUP ? "Backup " : "",
528 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200529
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200530 if (s->track) /* normally it's mandatory here */
531 chunk_appendf(&trash, " via %s/%s",
532 s->track->proxy->id, s->track->id);
533 Warning("%s.\n", trash.str);
534 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
535 }
536 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200537 chunk_printf(&trash,
538 "%sServer %s/%s remains in forced maintenance",
539 s->flags & SRV_F_BACKUP ? "Backup " : "",
540 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200541 Warning("%s.\n", trash.str);
542 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
543 }
544 /* don't report anything when leaving drain mode and remaining in maintenance */
545 }
546 else if (mode & SRV_ADMF_MAINT) {
547 /* OK here we're leaving maintenance, we have many things to check,
548 * because the server might possibly be coming back up depending on
549 * its state. In practice, leaving maintenance means that we should
550 * immediately turn to UP (more or less the slowstart) under the
551 * following conditions :
552 * - server is neither checked nor tracked
553 * - server tracks another server which is not checked
554 * - server tracks another server which is already up
555 * Which sums up as something simpler :
556 * "either the tracking server is up or the server's checks are disabled
557 * or up". Otherwise we only re-enable health checks. There's a special
558 * case associated to the stopping state which can be inherited. Note
559 * that the server might still be in drain mode, which is naturally dealt
560 * with by the lower level functions.
561 */
562
563 if (s->check.state & CHK_ST_ENABLED) {
564 s->check.state &= ~CHK_ST_PAUSED;
565 check->health = check->rise; /* start OK but check immediately */
566 }
567
568 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
569 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
570 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
571 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
572 if (s->proxy->last_change < now.tv_sec) // ignore negative times
573 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
574 s->proxy->last_change = now.tv_sec;
575 }
576
577 if (s->last_change < now.tv_sec) // ignore negative times
578 s->down_time += now.tv_sec - s->last_change;
579 s->last_change = now.tv_sec;
580
581 if (s->track && s->track->state == SRV_ST_STOPPING)
582 s->state = SRV_ST_STOPPING;
583 else {
584 s->state = SRV_ST_STARTING;
585 if (s->slowstart > 0)
586 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
587 else
588 s->state = SRV_ST_RUNNING;
589 }
590
591 server_recalc_eweight(s);
592
593 /* If the server is set with "on-marked-up shutdown-backup-sessions",
594 * and it's not a backup server and its effective weight is > 0,
595 * then it can accept new connections, so we shut down all sessions
596 * on all backup servers.
597 */
598 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
599 !(s->flags & SRV_F_BACKUP) && s->eweight)
600 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
601
602 /* check if we can handle some connections queued at the proxy. We
603 * will take as many as we can handle.
604 */
605 xferred = pendconn_grab_from_px(s);
606 }
607
608 if (mode & SRV_ADMF_FMAINT) {
609 chunk_printf(&trash,
610 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
611 s->flags & SRV_F_BACKUP ? "Backup " : "",
612 s->proxy->id, s->id,
613 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
614 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200615 }
616 else {
617 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200618 "%sServer %s/%s is %s/%s (leaving maintenance)",
619 s->flags & SRV_F_BACKUP ? "Backup " : "",
620 s->proxy->id, s->id,
621 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
622 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
623 srv_append_status(&trash, s, NULL, xferred, 0);
624 }
625 Warning("%s.\n", trash.str);
626 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
627 }
628 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
629 /* remaining in drain mode after removing one of its flags */
630
631 if (mode & SRV_ADMF_FDRAIN) {
632 chunk_printf(&trash,
633 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200634 s->flags & SRV_F_BACKUP ? "Backup " : "",
635 s->proxy->id, s->id);
636
637 if (s->track) /* normally it's mandatory here */
638 chunk_appendf(&trash, " via %s/%s",
639 s->track->proxy->id, s->track->id);
640 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200641 else {
642 chunk_printf(&trash,
643 "%sServer %s/%s remains in forced drain mode",
644 s->flags & SRV_F_BACKUP ? "Backup " : "",
645 s->proxy->id, s->id);
646 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200647 Warning("%s.\n", trash.str);
648 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200649 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200650 else if (mode & SRV_ADMF_DRAIN) {
651 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200652 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
653 if (s->proxy->last_change < now.tv_sec) // ignore negative times
654 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
655 s->proxy->last_change = now.tv_sec;
656 }
657
658 if (s->last_change < now.tv_sec) // ignore negative times
659 s->down_time += now.tv_sec - s->last_change;
660 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200661 server_recalc_eweight(s);
662
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200663 if (mode & SRV_ADMF_FDRAIN) {
664 chunk_printf(&trash,
665 "%sServer %s/%s is %s (leaving forced drain)",
666 s->flags & SRV_F_BACKUP ? "Backup " : "",
667 s->proxy->id, s->id,
668 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
669 }
670 else {
671 chunk_printf(&trash,
672 "%sServer %s/%s is %s (leaving drain)",
673 s->flags & SRV_F_BACKUP ? "Backup " : "",
674 s->proxy->id, s->id,
675 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
676 if (s->track) /* normally it's mandatory here */
677 chunk_appendf(&trash, " via %s/%s",
678 s->track->proxy->id, s->track->id);
679 }
680 Warning("%s.\n", trash.str);
681 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200682 }
683
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200684 /* stop going down if the equivalent flag is still present (forced or inherited) */
685 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
686 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
687 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200688
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200689 if (mode & SRV_ADMF_MAINT)
690 mode = SRV_ADMF_IMAINT;
691 else if (mode & SRV_ADMF_DRAIN)
692 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200693
694 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200695 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200696}
697
Willy Tarreaudff55432012-10-10 17:51:05 +0200698/* Note: must not be declared <const> as its list will be overwritten.
699 * Please take care of keeping this list alphabetically sorted, doing so helps
700 * all code contributors.
701 * Optional keywords are also declared with a NULL ->parse() function so that
702 * the config parser can report an appropriate error when a known keyword was
703 * not enabled.
704 */
705static struct srv_kw_list srv_kws = { "ALL", { }, {
706 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
707 { NULL, NULL, 0 },
708}};
709
710__attribute__((constructor))
711static void __listener_init(void)
712{
713 srv_register_keywords(&srv_kws);
714}
715
Willy Tarreau004e0452013-11-21 11:22:01 +0100716/* Recomputes the server's eweight based on its state, uweight, the current time,
717 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
718 * state is automatically disabled if the time is elapsed.
719 */
720void server_recalc_eweight(struct server *sv)
721{
722 struct proxy *px = sv->proxy;
723 unsigned w;
724
725 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
726 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200727 if (sv->state == SRV_ST_STARTING)
728 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100729 }
730
731 /* We must take care of not pushing the server to full throttle during slow starts.
732 * It must also start immediately, at least at the minimal step when leaving maintenance.
733 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200734 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100735 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
736 else
737 w = px->lbprm.wdiv;
738
739 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
740
741 /* now propagate the status change to any LB algorithms */
742 if (px->lbprm.update_server_eweight)
743 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200744 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100745 if (px->lbprm.set_server_status_up)
746 px->lbprm.set_server_status_up(sv);
747 }
748 else {
749 if (px->lbprm.set_server_status_down)
750 px->lbprm.set_server_status_down(sv);
751 }
752}
753
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900755 * Parses weight_str and configures sv accordingly.
756 * Returns NULL on success, error message string otherwise.
757 */
758const char *server_parse_weight_change_request(struct server *sv,
759 const char *weight_str)
760{
761 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900762 long int w;
763 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900764
765 px = sv->proxy;
766
767 /* if the weight is terminated with '%', it is set relative to
768 * the initial weight, otherwise it is absolute.
769 */
770 if (!*weight_str)
771 return "Require <weight> or <weight%>.\n";
772
Simon Hormanb796afa2013-02-12 10:45:53 +0900773 w = strtol(weight_str, &end, 10);
774 if (end == weight_str)
775 return "Empty weight string empty or preceded by garbage";
776 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900777 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900778 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900779 /* Avoid integer overflow */
780 if (w > 25600)
781 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900782 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900783 if (w > 256)
784 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900785 }
786 else if (w < 0 || w > 256)
787 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900788 else if (end[0] != '\0')
789 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900790
791 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
792 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
793
794 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100795 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900796
797 return NULL;
798}
799
Willy Tarreau272adea2014-03-31 10:39:59 +0200800int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
801{
802 struct server *newsrv = NULL;
803 const char *err;
804 char *errmsg = NULL;
805 int err_code = 0;
806 unsigned val;
807
808 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
809 int cur_arg;
810 short realport = 0;
811 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
812
813 if (!defsrv && curproxy == defproxy) {
814 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
815 err_code |= ERR_ALERT | ERR_FATAL;
816 goto out;
817 }
818 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
819 err_code |= ERR_ALERT | ERR_FATAL;
820
821 if (!*args[2]) {
822 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
823 file, linenum, args[0]);
824 err_code |= ERR_ALERT | ERR_FATAL;
825 goto out;
826 }
827
828 err = invalid_char(args[1]);
829 if (err && !defsrv) {
830 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
831 file, linenum, *err, args[1]);
832 err_code |= ERR_ALERT | ERR_FATAL;
833 goto out;
834 }
835
836 if (!defsrv) {
837 struct sockaddr_storage *sk;
838 int port1, port2;
839 struct protocol *proto;
840
841 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
842 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
843 err_code |= ERR_ALERT | ERR_ABORT;
844 goto out;
845 }
846
847 /* the servers are linked backwards first */
848 newsrv->next = curproxy->srv;
849 curproxy->srv = newsrv;
850 newsrv->proxy = curproxy;
851 newsrv->conf.file = strdup(file);
852 newsrv->conf.line = linenum;
853
854 newsrv->obj_type = OBJ_TYPE_SERVER;
855 LIST_INIT(&newsrv->actconns);
856 LIST_INIT(&newsrv->pendconns);
857 do_check = 0;
858 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200859 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200860 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200861 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200862 newsrv->last_change = now.tv_sec;
863 newsrv->id = strdup(args[1]);
864
865 /* several ways to check the port component :
866 * - IP => port=+0, relative (IPv4 only)
867 * - IP: => port=+0, relative
868 * - IP:N => port=N, absolute
869 * - IP:+N => port=+N, relative
870 * - IP:-N => port=-N, relative
871 */
872 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
873 if (!sk) {
874 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
875 err_code |= ERR_ALERT | ERR_FATAL;
876 goto out;
877 }
878
879 proto = protocol_by_family(sk->ss_family);
880 if (!proto || !proto->connect) {
881 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
882 file, linenum, args[0], args[1]);
883 err_code |= ERR_ALERT | ERR_FATAL;
884 goto out;
885 }
886
887 if (!port1 || !port2) {
888 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200889 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200890 }
891 else if (port1 != port2) {
892 /* port range */
893 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
894 file, linenum, args[0], args[1], args[2]);
895 err_code |= ERR_ALERT | ERR_FATAL;
896 goto out;
897 }
898 else {
899 /* used by checks */
900 realport = port1;
901 }
902
903 newsrv->addr = *sk;
Simon Horman41f58762015-01-30 11:22:56 +0900904 newsrv->proto = newsrv->check.proto = newsrv->agent.proto = protocol_by_family(newsrv->addr.ss_family);
Cyril Bonté9ce13112014-11-15 22:41:27 +0100905 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200906
907 if (!newsrv->proto) {
908 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
909 file, linenum, newsrv->addr.ss_family, args[2]);
910 err_code |= ERR_ALERT | ERR_FATAL;
911 goto out;
912 }
913
914 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
915 newsrv->check.port = curproxy->defsrv.check.port;
916 newsrv->check.inter = curproxy->defsrv.check.inter;
917 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
918 newsrv->check.downinter = curproxy->defsrv.check.downinter;
919 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
920 newsrv->agent.port = curproxy->defsrv.agent.port;
921 newsrv->agent.inter = curproxy->defsrv.agent.inter;
922 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
923 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
924 newsrv->maxqueue = curproxy->defsrv.maxqueue;
925 newsrv->minconn = curproxy->defsrv.minconn;
926 newsrv->maxconn = curproxy->defsrv.maxconn;
927 newsrv->slowstart = curproxy->defsrv.slowstart;
928 newsrv->onerror = curproxy->defsrv.onerror;
929 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
930 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
931 newsrv->consecutive_errors_limit
932 = curproxy->defsrv.consecutive_errors_limit;
933#ifdef OPENSSL
934 newsrv->use_ssl = curproxy->defsrv.use_ssl;
935#endif
936 newsrv->uweight = newsrv->iweight
937 = curproxy->defsrv.iweight;
938
939 newsrv->check.status = HCHK_STATUS_INI;
940 newsrv->check.rise = curproxy->defsrv.check.rise;
941 newsrv->check.fall = curproxy->defsrv.check.fall;
942 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
943 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +0900944 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +0200945
946 newsrv->agent.status = HCHK_STATUS_INI;
947 newsrv->agent.rise = curproxy->defsrv.agent.rise;
948 newsrv->agent.fall = curproxy->defsrv.agent.fall;
949 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
950 newsrv->agent.server = newsrv;
951
952 cur_arg = 3;
953 } else {
954 newsrv = &curproxy->defsrv;
955 cur_arg = 1;
956 }
957
958 while (*args[cur_arg]) {
959 if (!strcmp(args[cur_arg], "agent-check")) {
960 global.maxsock++;
961 do_agent = 1;
962 cur_arg += 1;
963 } else if (!strcmp(args[cur_arg], "agent-inter")) {
964 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
965 if (err) {
966 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
967 file, linenum, *err, newsrv->id);
968 err_code |= ERR_ALERT | ERR_FATAL;
969 goto out;
970 }
971 if (val <= 0) {
972 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
973 file, linenum, val, args[cur_arg], newsrv->id);
974 err_code |= ERR_ALERT | ERR_FATAL;
975 goto out;
976 }
977 newsrv->agent.inter = val;
978 cur_arg += 2;
979 }
980 else if (!strcmp(args[cur_arg], "agent-port")) {
981 global.maxsock++;
982 newsrv->agent.port = atol(args[cur_arg + 1]);
983 cur_arg += 2;
984 }
985 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
986 newsrv->cookie = strdup(args[cur_arg + 1]);
987 newsrv->cklen = strlen(args[cur_arg + 1]);
988 cur_arg += 2;
989 }
990 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
991 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
992 newsrv->rdr_len = strlen(args[cur_arg + 1]);
993 cur_arg += 2;
994 }
995 else if (!strcmp(args[cur_arg], "rise")) {
996 if (!*args[cur_arg + 1]) {
997 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
998 file, linenum, args[cur_arg]);
999 err_code |= ERR_ALERT | ERR_FATAL;
1000 goto out;
1001 }
1002
1003 newsrv->check.rise = atol(args[cur_arg + 1]);
1004 if (newsrv->check.rise <= 0) {
1005 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1006 file, linenum, args[cur_arg]);
1007 err_code |= ERR_ALERT | ERR_FATAL;
1008 goto out;
1009 }
1010
1011 if (newsrv->check.health)
1012 newsrv->check.health = newsrv->check.rise;
1013 cur_arg += 2;
1014 }
1015 else if (!strcmp(args[cur_arg], "fall")) {
1016 newsrv->check.fall = atol(args[cur_arg + 1]);
1017
1018 if (!*args[cur_arg + 1]) {
1019 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1020 file, linenum, args[cur_arg]);
1021 err_code |= ERR_ALERT | ERR_FATAL;
1022 goto out;
1023 }
1024
1025 if (newsrv->check.fall <= 0) {
1026 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1027 file, linenum, args[cur_arg]);
1028 err_code |= ERR_ALERT | ERR_FATAL;
1029 goto out;
1030 }
1031
1032 cur_arg += 2;
1033 }
1034 else if (!strcmp(args[cur_arg], "inter")) {
1035 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1036 if (err) {
1037 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1038 file, linenum, *err, newsrv->id);
1039 err_code |= ERR_ALERT | ERR_FATAL;
1040 goto out;
1041 }
1042 if (val <= 0) {
1043 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1044 file, linenum, val, args[cur_arg], newsrv->id);
1045 err_code |= ERR_ALERT | ERR_FATAL;
1046 goto out;
1047 }
1048 newsrv->check.inter = val;
1049 cur_arg += 2;
1050 }
1051 else if (!strcmp(args[cur_arg], "fastinter")) {
1052 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1053 if (err) {
1054 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1055 file, linenum, *err, newsrv->id);
1056 err_code |= ERR_ALERT | ERR_FATAL;
1057 goto out;
1058 }
1059 if (val <= 0) {
1060 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1061 file, linenum, val, args[cur_arg], newsrv->id);
1062 err_code |= ERR_ALERT | ERR_FATAL;
1063 goto out;
1064 }
1065 newsrv->check.fastinter = val;
1066 cur_arg += 2;
1067 }
1068 else if (!strcmp(args[cur_arg], "downinter")) {
1069 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1070 if (err) {
1071 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1072 file, linenum, *err, newsrv->id);
1073 err_code |= ERR_ALERT | ERR_FATAL;
1074 goto out;
1075 }
1076 if (val <= 0) {
1077 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1078 file, linenum, val, args[cur_arg], newsrv->id);
1079 err_code |= ERR_ALERT | ERR_FATAL;
1080 goto out;
1081 }
1082 newsrv->check.downinter = val;
1083 cur_arg += 2;
1084 }
1085 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1086 struct sockaddr_storage *sk;
1087 int port1, port2;
1088 struct protocol *proto;
1089
1090 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1091 if (!sk) {
1092 Alert("parsing [%s:%d] : '%s' : %s\n",
1093 file, linenum, args[cur_arg], errmsg);
1094 err_code |= ERR_ALERT | ERR_FATAL;
1095 goto out;
1096 }
1097
1098 proto = protocol_by_family(sk->ss_family);
1099 if (!proto || !proto->connect) {
1100 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1101 file, linenum, args[cur_arg], args[cur_arg + 1]);
1102 err_code |= ERR_ALERT | ERR_FATAL;
1103 goto out;
1104 }
1105
1106 if (port1 != port2) {
1107 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1108 file, linenum, args[cur_arg], args[cur_arg + 1]);
1109 err_code |= ERR_ALERT | ERR_FATAL;
1110 goto out;
1111 }
1112
Simon Horman41f58762015-01-30 11:22:56 +09001113 newsrv->check.addr = newsrv->agent.addr = *sk;
1114 newsrv->check.proto = newsrv->agent.proto = protocol_by_family(sk->ss_family);
Willy Tarreau272adea2014-03-31 10:39:59 +02001115 cur_arg += 2;
1116 }
1117 else if (!strcmp(args[cur_arg], "port")) {
1118 newsrv->check.port = atol(args[cur_arg + 1]);
1119 cur_arg += 2;
1120 }
1121 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001122 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001123 cur_arg ++;
1124 }
1125 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001126 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001127 cur_arg ++;
1128 }
1129 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001130 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001131 cur_arg ++;
1132 }
David Safb76832014-05-08 23:42:08 -04001133 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1134 newsrv->pp_opts |= SRV_PP_V2;
1135 cur_arg ++;
1136 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001137 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1138 newsrv->check.send_proxy = 1;
1139 cur_arg ++;
1140 }
1141 else if (!strcmp(args[cur_arg], "weight")) {
1142 int w;
1143 w = atol(args[cur_arg + 1]);
1144 if (w < 0 || w > SRV_UWGHT_MAX) {
1145 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1146 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1147 err_code |= ERR_ALERT | ERR_FATAL;
1148 goto out;
1149 }
1150 newsrv->uweight = newsrv->iweight = w;
1151 cur_arg += 2;
1152 }
1153 else if (!strcmp(args[cur_arg], "minconn")) {
1154 newsrv->minconn = atol(args[cur_arg + 1]);
1155 cur_arg += 2;
1156 }
1157 else if (!strcmp(args[cur_arg], "maxconn")) {
1158 newsrv->maxconn = atol(args[cur_arg + 1]);
1159 cur_arg += 2;
1160 }
1161 else if (!strcmp(args[cur_arg], "maxqueue")) {
1162 newsrv->maxqueue = atol(args[cur_arg + 1]);
1163 cur_arg += 2;
1164 }
1165 else if (!strcmp(args[cur_arg], "slowstart")) {
1166 /* slowstart is stored in seconds */
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 'slowstart' argument of server %s.\n",
1170 file, linenum, *err, newsrv->id);
1171 err_code |= ERR_ALERT | ERR_FATAL;
1172 goto out;
1173 }
1174 newsrv->slowstart = (val + 999) / 1000;
1175 cur_arg += 2;
1176 }
1177 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1178
1179 if (!*args[cur_arg + 1]) {
1180 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1181 file, linenum);
1182 err_code |= ERR_ALERT | ERR_FATAL;
1183 goto out;
1184 }
1185
1186 newsrv->trackit = strdup(args[cur_arg + 1]);
1187
1188 cur_arg += 2;
1189 }
1190 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1191 global.maxsock++;
1192 do_check = 1;
1193 cur_arg += 1;
1194 }
1195 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001196 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001197 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001198 newsrv->check.state |= CHK_ST_PAUSED;
1199 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001200 cur_arg += 1;
1201 }
1202 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1203 if (!strcmp(args[cur_arg + 1], "none"))
1204 newsrv->observe = HANA_OBS_NONE;
1205 else if (!strcmp(args[cur_arg + 1], "layer4"))
1206 newsrv->observe = HANA_OBS_LAYER4;
1207 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1208 if (curproxy->mode != PR_MODE_HTTP) {
1209 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1210 file, linenum, args[cur_arg + 1]);
1211 err_code |= ERR_ALERT;
1212 }
1213 newsrv->observe = HANA_OBS_LAYER7;
1214 }
1215 else {
1216 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1217 "'layer4', 'layer7' but got '%s'\n",
1218 file, linenum, args[cur_arg], args[cur_arg + 1]);
1219 err_code |= ERR_ALERT | ERR_FATAL;
1220 goto out;
1221 }
1222
1223 cur_arg += 2;
1224 }
1225 else if (!strcmp(args[cur_arg], "on-error")) {
1226 if (!strcmp(args[cur_arg + 1], "fastinter"))
1227 newsrv->onerror = HANA_ONERR_FASTINTER;
1228 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1229 newsrv->onerror = HANA_ONERR_FAILCHK;
1230 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1231 newsrv->onerror = HANA_ONERR_SUDDTH;
1232 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1233 newsrv->onerror = HANA_ONERR_MARKDWN;
1234 else {
1235 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1236 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1237 file, linenum, args[cur_arg], args[cur_arg + 1]);
1238 err_code |= ERR_ALERT | ERR_FATAL;
1239 goto out;
1240 }
1241
1242 cur_arg += 2;
1243 }
1244 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1245 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1246 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1247 else {
1248 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1249 file, linenum, args[cur_arg], args[cur_arg + 1]);
1250 err_code |= ERR_ALERT | ERR_FATAL;
1251 goto out;
1252 }
1253
1254 cur_arg += 2;
1255 }
1256 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1257 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1258 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1259 else {
1260 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1261 file, linenum, args[cur_arg], args[cur_arg + 1]);
1262 err_code |= ERR_ALERT | ERR_FATAL;
1263 goto out;
1264 }
1265
1266 cur_arg += 2;
1267 }
1268 else if (!strcmp(args[cur_arg], "error-limit")) {
1269 if (!*args[cur_arg + 1]) {
1270 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1271 file, linenum, args[cur_arg]);
1272 err_code |= ERR_ALERT | ERR_FATAL;
1273 goto out;
1274 }
1275
1276 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1277
1278 if (newsrv->consecutive_errors_limit <= 0) {
1279 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1280 file, linenum, args[cur_arg]);
1281 err_code |= ERR_ALERT | ERR_FATAL;
1282 goto out;
1283 }
1284 cur_arg += 2;
1285 }
1286 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1287 int port_low, port_high;
1288 struct sockaddr_storage *sk;
1289 struct protocol *proto;
1290
1291 if (!*args[cur_arg + 1]) {
1292 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1293 file, linenum, "source", "usesrc", "interface");
1294 err_code |= ERR_ALERT | ERR_FATAL;
1295 goto out;
1296 }
1297
1298 newsrv->conn_src.opts |= CO_SRC_BIND;
1299 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1300 if (!sk) {
1301 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1302 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1303 err_code |= ERR_ALERT | ERR_FATAL;
1304 goto out;
1305 }
1306
1307 proto = protocol_by_family(sk->ss_family);
1308 if (!proto || !proto->connect) {
1309 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1310 file, linenum, args[cur_arg], args[cur_arg+1]);
1311 err_code |= ERR_ALERT | ERR_FATAL;
1312 goto out;
1313 }
1314
1315 newsrv->conn_src.source_addr = *sk;
1316
1317 if (port_low != port_high) {
1318 int i;
1319
1320 if (!port_low || !port_high) {
1321 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1322 file, linenum, args[cur_arg], args[cur_arg + 1]);
1323 err_code |= ERR_ALERT | ERR_FATAL;
1324 goto out;
1325 }
1326
1327 if (port_low <= 0 || port_low > 65535 ||
1328 port_high <= 0 || port_high > 65535 ||
1329 port_low > port_high) {
1330 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1331 file, linenum, port_low, port_high);
1332 err_code |= ERR_ALERT | ERR_FATAL;
1333 goto out;
1334 }
1335 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1336 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1337 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1338 }
1339
1340 cur_arg += 2;
1341 while (*(args[cur_arg])) {
1342 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1343#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1344#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001345 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001346 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1347 file, linenum, "usesrc", "source");
1348 err_code |= ERR_ALERT | ERR_FATAL;
1349 goto out;
1350 }
1351#endif
1352 if (!*args[cur_arg + 1]) {
1353 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1354 file, linenum, "usesrc");
1355 err_code |= ERR_ALERT | ERR_FATAL;
1356 goto out;
1357 }
1358 if (!strcmp(args[cur_arg + 1], "client")) {
1359 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1360 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1361 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1362 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1363 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1364 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1365 char *name, *end;
1366
1367 name = args[cur_arg+1] + 7;
1368 while (isspace(*name))
1369 name++;
1370
1371 end = name;
1372 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1373 end++;
1374
1375 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1376 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1377 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1378 newsrv->conn_src.bind_hdr_len = end - name;
1379 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1380 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1381 newsrv->conn_src.bind_hdr_occ = -1;
1382
1383 /* now look for an occurrence number */
1384 while (isspace(*end))
1385 end++;
1386 if (*end == ',') {
1387 end++;
1388 name = end;
1389 if (*end == '-')
1390 end++;
1391 while (isdigit((int)*end))
1392 end++;
1393 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1394 }
1395
1396 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1397 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1398 " occurrences values smaller than %d.\n",
1399 file, linenum, MAX_HDR_HISTORY);
1400 err_code |= ERR_ALERT | ERR_FATAL;
1401 goto out;
1402 }
1403 } else {
1404 struct sockaddr_storage *sk;
1405 int port1, port2;
1406
1407 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1408 if (!sk) {
1409 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1410 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1411 err_code |= ERR_ALERT | ERR_FATAL;
1412 goto out;
1413 }
1414
1415 proto = protocol_by_family(sk->ss_family);
1416 if (!proto || !proto->connect) {
1417 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1418 file, linenum, args[cur_arg], args[cur_arg+1]);
1419 err_code |= ERR_ALERT | ERR_FATAL;
1420 goto out;
1421 }
1422
1423 if (port1 != port2) {
1424 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1425 file, linenum, args[cur_arg], args[cur_arg + 1]);
1426 err_code |= ERR_ALERT | ERR_FATAL;
1427 goto out;
1428 }
1429 newsrv->conn_src.tproxy_addr = *sk;
1430 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1431 }
1432 global.last_checks |= LSTCHK_NETADM;
1433#if !defined(CONFIG_HAP_TRANSPARENT)
1434 global.last_checks |= LSTCHK_CTTPROXY;
1435#endif
1436 cur_arg += 2;
1437 continue;
1438#else /* no TPROXY support */
1439 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1440 file, linenum, "usesrc");
1441 err_code |= ERR_ALERT | ERR_FATAL;
1442 goto out;
1443#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1444 } /* "usesrc" */
1445
1446 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1447#ifdef SO_BINDTODEVICE
1448 if (!*args[cur_arg + 1]) {
1449 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1450 file, linenum, args[0]);
1451 err_code |= ERR_ALERT | ERR_FATAL;
1452 goto out;
1453 }
1454 free(newsrv->conn_src.iface_name);
1455 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1456 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1457 global.last_checks |= LSTCHK_NETADM;
1458#else
1459 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1460 file, linenum, args[0], args[cur_arg]);
1461 err_code |= ERR_ALERT | ERR_FATAL;
1462 goto out;
1463#endif
1464 cur_arg += 2;
1465 continue;
1466 }
1467 /* this keyword in not an option of "source" */
1468 break;
1469 } /* while */
1470 }
1471 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1472 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1473 file, linenum, "usesrc", "source");
1474 err_code |= ERR_ALERT | ERR_FATAL;
1475 goto out;
1476 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001477 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1478#ifdef CONFIG_HAP_NS
1479 char *arg = args[cur_arg + 1];
1480 if (!strcmp(arg, "*")) {
1481 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1482 } else {
1483 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1484
1485 if (newsrv->netns == NULL)
1486 newsrv->netns = netns_store_insert(arg);
1487
1488 if (newsrv->netns == NULL) {
1489 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1490 err_code |= ERR_ALERT | ERR_FATAL;
1491 goto out;
1492 }
1493 }
1494#else
1495 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1496 file, linenum, args[0], args[cur_arg]);
1497 err_code |= ERR_ALERT | ERR_FATAL;
1498 goto out;
1499#endif
1500 cur_arg += 2;
1501 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001502 else {
1503 static int srv_dumped;
1504 struct srv_kw *kw;
1505 char *err;
1506
1507 kw = srv_find_kw(args[cur_arg]);
1508 if (kw) {
1509 char *err = NULL;
1510 int code;
1511
1512 if (!kw->parse) {
1513 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1514 file, linenum, args[0], args[1], args[cur_arg]);
1515 cur_arg += 1 + kw->skip ;
1516 err_code |= ERR_ALERT | ERR_FATAL;
1517 goto out;
1518 }
1519
1520 if (defsrv && !kw->default_ok) {
1521 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1522 file, linenum, args[0], args[1], args[cur_arg]);
1523 cur_arg += 1 + kw->skip ;
1524 err_code |= ERR_ALERT;
1525 continue;
1526 }
1527
1528 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1529 err_code |= code;
1530
1531 if (code) {
1532 if (err && *err) {
1533 indent_msg(&err, 2);
1534 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1535 }
1536 else
1537 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1538 file, linenum, args[0], args[1], args[cur_arg]);
1539 if (code & ERR_FATAL) {
1540 free(err);
1541 cur_arg += 1 + kw->skip;
1542 goto out;
1543 }
1544 }
1545 free(err);
1546 cur_arg += 1 + kw->skip;
1547 continue;
1548 }
1549
1550 err = NULL;
1551 if (!srv_dumped) {
1552 srv_dump_kws(&err);
1553 indent_msg(&err, 4);
1554 srv_dumped = 1;
1555 }
1556
1557 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1558 file, linenum, args[0], args[1], args[cur_arg],
1559 err ? " Registered keywords :" : "", err ? err : "");
1560 free(err);
1561
1562 err_code |= ERR_ALERT | ERR_FATAL;
1563 goto out;
1564 }
1565 }
1566
Willy Tarreau272adea2014-03-31 10:39:59 +02001567 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001568 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001569
1570 if (newsrv->trackit) {
1571 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1572 file, linenum);
1573 err_code |= ERR_ALERT | ERR_FATAL;
1574 goto out;
1575 }
1576
1577 /* If neither a port nor an addr was specified and no check transport
1578 * layer is forced, then the transport layer used by the checks is the
1579 * same as for the production traffic. Otherwise we use raw_sock by
1580 * default, unless one is specified.
1581 */
Simon Horman41f58762015-01-30 11:22:56 +09001582 if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001583#ifdef USE_OPENSSL
1584 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1585#endif
David Safb76832014-05-08 23:42:08 -04001586 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001587 }
1588 /* try to get the port from check_core.addr if check.port not set */
1589 if (!newsrv->check.port)
Simon Horman41f58762015-01-30 11:22:56 +09001590 newsrv->check.port = get_host_port(&newsrv->check.addr);
Willy Tarreau272adea2014-03-31 10:39:59 +02001591
1592 if (!newsrv->check.port)
1593 newsrv->check.port = realport; /* by default */
1594
1595 if (!newsrv->check.port) {
1596 /* not yet valid, because no port was set on
1597 * the server either. We'll check if we have
1598 * a known port on the first listener.
1599 */
1600 struct listener *l;
1601
1602 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1603 newsrv->check.port = get_host_port(&l->addr);
1604 if (newsrv->check.port)
1605 break;
1606 }
1607 }
1608 /*
1609 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001610 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001611 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001612 if (!newsrv->check.port &&
Simon Horman41f58762015-01-30 11:22:56 +09001613 (is_inet_addr(&newsrv->check.addr) ||
1614 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001615 struct tcpcheck_rule *n = NULL, *r = NULL;
1616 struct list *l;
1617
1618 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1619 if (!r) {
1620 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1621 file, linenum, newsrv->id);
1622 err_code |= ERR_ALERT | ERR_FATAL;
1623 goto out;
1624 }
1625 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1626 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",
1627 file, linenum, newsrv->id);
1628 err_code |= ERR_ALERT | ERR_FATAL;
1629 goto out;
1630 }
1631 else {
1632 /* scan the tcp-check ruleset to ensure a port has been configured */
1633 l = &newsrv->proxy->tcpcheck_rules;
1634 list_for_each_entry(n, l, list) {
1635 r = (struct tcpcheck_rule *)n->list.p;
1636 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1637 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",
1638 file, linenum, newsrv->id);
1639 err_code |= ERR_ALERT | ERR_FATAL;
1640 goto out;
1641 }
1642 }
1643 }
1644 }
1645
1646 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001647 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001648 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001649 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1650 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001651 goto out;
1652 }
1653
1654 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1655 }
1656
1657 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001658 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001659
1660 if (!newsrv->agent.port) {
1661 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1662 file, linenum, newsrv->id);
1663 err_code |= ERR_ALERT | ERR_FATAL;
1664 goto out;
1665 }
1666
1667 if (!newsrv->agent.inter)
1668 newsrv->agent.inter = newsrv->check.inter;
1669
Simon Hormanb1900d52015-01-30 11:22:54 +09001670 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001671 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001672 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1673 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001674 goto out;
1675 }
1676
1677 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1678 }
1679
1680 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001681 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001682 curproxy->srv_bck++;
1683 else
1684 curproxy->srv_act++;
1685
Willy Tarreauc5150da2014-05-13 19:27:31 +02001686 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001687 }
1688 }
1689 return 0;
1690
1691 out:
1692 free(errmsg);
1693 return err_code;
1694}
1695
Simon Horman7d09b9a2013-02-12 10:45:51 +09001696/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001697 * Local variables:
1698 * c-indent-level: 8
1699 * c-basic-offset: 8
1700 * End:
1701 */