blob: 118a1ed995bbbfe49ce96c8023f1ff17651bef38 [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);
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)
247 srv_shutdown_sessions(s, SN_ERR_DOWN);
248
249 /* we might have sessions queued on this server and waiting for
250 * 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,
316 * then it can accept new connections, so we shut down all sessions
317 * on all backup servers.
318 */
319 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
320 !(s->flags & SRV_F_BACKUP) && s->eweight)
321 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
322
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
364 /* we might have sessions queued on this server and waiting for
365 * 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)
441 srv_shutdown_sessions(s, SN_ERR_DOWN);
442
443 /* we might have sessions queued on this server and waiting for
444 * 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 Tarreaua0066dd2014-05-16 11:25:16 +0200474 /* we might have sessions queued on this server and waiting for
475 * 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,
598 * then it can accept new connections, so we shut down all sessions
599 * on all backup servers.
600 */
601 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
602 !(s->flags & SRV_F_BACKUP) && s->eweight)
603 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
604
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;
Simon Horman41f58762015-01-30 11:22:56 +0900907 newsrv->proto = newsrv->check.proto = newsrv->agent.proto = protocol_by_family(newsrv->addr.ss_family);
Cyril Bonté9ce13112014-11-15 22:41:27 +0100908 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200909
910 if (!newsrv->proto) {
911 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
912 file, linenum, newsrv->addr.ss_family, args[2]);
913 err_code |= ERR_ALERT | ERR_FATAL;
914 goto out;
915 }
916
917 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
918 newsrv->check.port = curproxy->defsrv.check.port;
919 newsrv->check.inter = curproxy->defsrv.check.inter;
920 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
921 newsrv->check.downinter = curproxy->defsrv.check.downinter;
922 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
923 newsrv->agent.port = curproxy->defsrv.agent.port;
924 newsrv->agent.inter = curproxy->defsrv.agent.inter;
925 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
926 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
927 newsrv->maxqueue = curproxy->defsrv.maxqueue;
928 newsrv->minconn = curproxy->defsrv.minconn;
929 newsrv->maxconn = curproxy->defsrv.maxconn;
930 newsrv->slowstart = curproxy->defsrv.slowstart;
931 newsrv->onerror = curproxy->defsrv.onerror;
932 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
933 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
934 newsrv->consecutive_errors_limit
935 = curproxy->defsrv.consecutive_errors_limit;
936#ifdef OPENSSL
937 newsrv->use_ssl = curproxy->defsrv.use_ssl;
938#endif
939 newsrv->uweight = newsrv->iweight
940 = curproxy->defsrv.iweight;
941
942 newsrv->check.status = HCHK_STATUS_INI;
943 newsrv->check.rise = curproxy->defsrv.check.rise;
944 newsrv->check.fall = curproxy->defsrv.check.fall;
945 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
946 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +0900947 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +0200948
949 newsrv->agent.status = HCHK_STATUS_INI;
950 newsrv->agent.rise = curproxy->defsrv.agent.rise;
951 newsrv->agent.fall = curproxy->defsrv.agent.fall;
952 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
953 newsrv->agent.server = newsrv;
954
955 cur_arg = 3;
956 } else {
957 newsrv = &curproxy->defsrv;
958 cur_arg = 1;
959 }
960
961 while (*args[cur_arg]) {
962 if (!strcmp(args[cur_arg], "agent-check")) {
963 global.maxsock++;
964 do_agent = 1;
965 cur_arg += 1;
966 } else if (!strcmp(args[cur_arg], "agent-inter")) {
967 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
968 if (err) {
969 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
970 file, linenum, *err, newsrv->id);
971 err_code |= ERR_ALERT | ERR_FATAL;
972 goto out;
973 }
974 if (val <= 0) {
975 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
976 file, linenum, val, args[cur_arg], newsrv->id);
977 err_code |= ERR_ALERT | ERR_FATAL;
978 goto out;
979 }
980 newsrv->agent.inter = val;
981 cur_arg += 2;
982 }
983 else if (!strcmp(args[cur_arg], "agent-port")) {
984 global.maxsock++;
985 newsrv->agent.port = atol(args[cur_arg + 1]);
986 cur_arg += 2;
987 }
988 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
989 newsrv->cookie = strdup(args[cur_arg + 1]);
990 newsrv->cklen = strlen(args[cur_arg + 1]);
991 cur_arg += 2;
992 }
993 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
994 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
995 newsrv->rdr_len = strlen(args[cur_arg + 1]);
996 cur_arg += 2;
997 }
998 else if (!strcmp(args[cur_arg], "rise")) {
999 if (!*args[cur_arg + 1]) {
1000 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1001 file, linenum, args[cur_arg]);
1002 err_code |= ERR_ALERT | ERR_FATAL;
1003 goto out;
1004 }
1005
1006 newsrv->check.rise = atol(args[cur_arg + 1]);
1007 if (newsrv->check.rise <= 0) {
1008 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1009 file, linenum, args[cur_arg]);
1010 err_code |= ERR_ALERT | ERR_FATAL;
1011 goto out;
1012 }
1013
1014 if (newsrv->check.health)
1015 newsrv->check.health = newsrv->check.rise;
1016 cur_arg += 2;
1017 }
1018 else if (!strcmp(args[cur_arg], "fall")) {
1019 newsrv->check.fall = atol(args[cur_arg + 1]);
1020
1021 if (!*args[cur_arg + 1]) {
1022 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1023 file, linenum, args[cur_arg]);
1024 err_code |= ERR_ALERT | ERR_FATAL;
1025 goto out;
1026 }
1027
1028 if (newsrv->check.fall <= 0) {
1029 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1030 file, linenum, args[cur_arg]);
1031 err_code |= ERR_ALERT | ERR_FATAL;
1032 goto out;
1033 }
1034
1035 cur_arg += 2;
1036 }
1037 else if (!strcmp(args[cur_arg], "inter")) {
1038 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1039 if (err) {
1040 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1041 file, linenum, *err, newsrv->id);
1042 err_code |= ERR_ALERT | ERR_FATAL;
1043 goto out;
1044 }
1045 if (val <= 0) {
1046 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1047 file, linenum, val, args[cur_arg], newsrv->id);
1048 err_code |= ERR_ALERT | ERR_FATAL;
1049 goto out;
1050 }
1051 newsrv->check.inter = val;
1052 cur_arg += 2;
1053 }
1054 else if (!strcmp(args[cur_arg], "fastinter")) {
1055 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1056 if (err) {
1057 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1058 file, linenum, *err, newsrv->id);
1059 err_code |= ERR_ALERT | ERR_FATAL;
1060 goto out;
1061 }
1062 if (val <= 0) {
1063 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1064 file, linenum, val, args[cur_arg], newsrv->id);
1065 err_code |= ERR_ALERT | ERR_FATAL;
1066 goto out;
1067 }
1068 newsrv->check.fastinter = val;
1069 cur_arg += 2;
1070 }
1071 else if (!strcmp(args[cur_arg], "downinter")) {
1072 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1073 if (err) {
1074 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1075 file, linenum, *err, newsrv->id);
1076 err_code |= ERR_ALERT | ERR_FATAL;
1077 goto out;
1078 }
1079 if (val <= 0) {
1080 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1081 file, linenum, val, args[cur_arg], newsrv->id);
1082 err_code |= ERR_ALERT | ERR_FATAL;
1083 goto out;
1084 }
1085 newsrv->check.downinter = val;
1086 cur_arg += 2;
1087 }
1088 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1089 struct sockaddr_storage *sk;
1090 int port1, port2;
1091 struct protocol *proto;
1092
1093 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1094 if (!sk) {
1095 Alert("parsing [%s:%d] : '%s' : %s\n",
1096 file, linenum, args[cur_arg], errmsg);
1097 err_code |= ERR_ALERT | ERR_FATAL;
1098 goto out;
1099 }
1100
1101 proto = protocol_by_family(sk->ss_family);
1102 if (!proto || !proto->connect) {
1103 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1104 file, linenum, args[cur_arg], args[cur_arg + 1]);
1105 err_code |= ERR_ALERT | ERR_FATAL;
1106 goto out;
1107 }
1108
1109 if (port1 != port2) {
1110 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1111 file, linenum, args[cur_arg], args[cur_arg + 1]);
1112 err_code |= ERR_ALERT | ERR_FATAL;
1113 goto out;
1114 }
1115
Simon Horman41f58762015-01-30 11:22:56 +09001116 newsrv->check.addr = newsrv->agent.addr = *sk;
1117 newsrv->check.proto = newsrv->agent.proto = protocol_by_family(sk->ss_family);
Willy Tarreau272adea2014-03-31 10:39:59 +02001118 cur_arg += 2;
1119 }
1120 else if (!strcmp(args[cur_arg], "port")) {
1121 newsrv->check.port = atol(args[cur_arg + 1]);
1122 cur_arg += 2;
1123 }
1124 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001125 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001126 cur_arg ++;
1127 }
1128 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001129 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001130 cur_arg ++;
1131 }
1132 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001133 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001134 cur_arg ++;
1135 }
David Safb76832014-05-08 23:42:08 -04001136 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1137 newsrv->pp_opts |= SRV_PP_V2;
1138 cur_arg ++;
1139 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001140 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1141 newsrv->check.send_proxy = 1;
1142 cur_arg ++;
1143 }
1144 else if (!strcmp(args[cur_arg], "weight")) {
1145 int w;
1146 w = atol(args[cur_arg + 1]);
1147 if (w < 0 || w > SRV_UWGHT_MAX) {
1148 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1149 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1150 err_code |= ERR_ALERT | ERR_FATAL;
1151 goto out;
1152 }
1153 newsrv->uweight = newsrv->iweight = w;
1154 cur_arg += 2;
1155 }
1156 else if (!strcmp(args[cur_arg], "minconn")) {
1157 newsrv->minconn = atol(args[cur_arg + 1]);
1158 cur_arg += 2;
1159 }
1160 else if (!strcmp(args[cur_arg], "maxconn")) {
1161 newsrv->maxconn = atol(args[cur_arg + 1]);
1162 cur_arg += 2;
1163 }
1164 else if (!strcmp(args[cur_arg], "maxqueue")) {
1165 newsrv->maxqueue = atol(args[cur_arg + 1]);
1166 cur_arg += 2;
1167 }
1168 else if (!strcmp(args[cur_arg], "slowstart")) {
1169 /* slowstart is stored in seconds */
1170 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1171 if (err) {
1172 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1173 file, linenum, *err, newsrv->id);
1174 err_code |= ERR_ALERT | ERR_FATAL;
1175 goto out;
1176 }
1177 newsrv->slowstart = (val + 999) / 1000;
1178 cur_arg += 2;
1179 }
1180 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1181
1182 if (!*args[cur_arg + 1]) {
1183 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1184 file, linenum);
1185 err_code |= ERR_ALERT | ERR_FATAL;
1186 goto out;
1187 }
1188
1189 newsrv->trackit = strdup(args[cur_arg + 1]);
1190
1191 cur_arg += 2;
1192 }
1193 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1194 global.maxsock++;
1195 do_check = 1;
1196 cur_arg += 1;
1197 }
1198 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001199 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001200 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001201 newsrv->check.state |= CHK_ST_PAUSED;
1202 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001203 cur_arg += 1;
1204 }
1205 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1206 if (!strcmp(args[cur_arg + 1], "none"))
1207 newsrv->observe = HANA_OBS_NONE;
1208 else if (!strcmp(args[cur_arg + 1], "layer4"))
1209 newsrv->observe = HANA_OBS_LAYER4;
1210 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1211 if (curproxy->mode != PR_MODE_HTTP) {
1212 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1213 file, linenum, args[cur_arg + 1]);
1214 err_code |= ERR_ALERT;
1215 }
1216 newsrv->observe = HANA_OBS_LAYER7;
1217 }
1218 else {
1219 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1220 "'layer4', 'layer7' but got '%s'\n",
1221 file, linenum, args[cur_arg], args[cur_arg + 1]);
1222 err_code |= ERR_ALERT | ERR_FATAL;
1223 goto out;
1224 }
1225
1226 cur_arg += 2;
1227 }
1228 else if (!strcmp(args[cur_arg], "on-error")) {
1229 if (!strcmp(args[cur_arg + 1], "fastinter"))
1230 newsrv->onerror = HANA_ONERR_FASTINTER;
1231 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1232 newsrv->onerror = HANA_ONERR_FAILCHK;
1233 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1234 newsrv->onerror = HANA_ONERR_SUDDTH;
1235 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1236 newsrv->onerror = HANA_ONERR_MARKDWN;
1237 else {
1238 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1239 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1240 file, linenum, args[cur_arg], args[cur_arg + 1]);
1241 err_code |= ERR_ALERT | ERR_FATAL;
1242 goto out;
1243 }
1244
1245 cur_arg += 2;
1246 }
1247 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1248 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1249 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1250 else {
1251 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1252 file, linenum, args[cur_arg], args[cur_arg + 1]);
1253 err_code |= ERR_ALERT | ERR_FATAL;
1254 goto out;
1255 }
1256
1257 cur_arg += 2;
1258 }
1259 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1260 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1261 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1262 else {
1263 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1264 file, linenum, args[cur_arg], args[cur_arg + 1]);
1265 err_code |= ERR_ALERT | ERR_FATAL;
1266 goto out;
1267 }
1268
1269 cur_arg += 2;
1270 }
1271 else if (!strcmp(args[cur_arg], "error-limit")) {
1272 if (!*args[cur_arg + 1]) {
1273 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1274 file, linenum, args[cur_arg]);
1275 err_code |= ERR_ALERT | ERR_FATAL;
1276 goto out;
1277 }
1278
1279 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1280
1281 if (newsrv->consecutive_errors_limit <= 0) {
1282 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1283 file, linenum, args[cur_arg]);
1284 err_code |= ERR_ALERT | ERR_FATAL;
1285 goto out;
1286 }
1287 cur_arg += 2;
1288 }
1289 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1290 int port_low, port_high;
1291 struct sockaddr_storage *sk;
1292 struct protocol *proto;
1293
1294 if (!*args[cur_arg + 1]) {
1295 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1296 file, linenum, "source", "usesrc", "interface");
1297 err_code |= ERR_ALERT | ERR_FATAL;
1298 goto out;
1299 }
1300
1301 newsrv->conn_src.opts |= CO_SRC_BIND;
1302 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1303 if (!sk) {
1304 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1305 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1306 err_code |= ERR_ALERT | ERR_FATAL;
1307 goto out;
1308 }
1309
1310 proto = protocol_by_family(sk->ss_family);
1311 if (!proto || !proto->connect) {
1312 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1313 file, linenum, args[cur_arg], args[cur_arg+1]);
1314 err_code |= ERR_ALERT | ERR_FATAL;
1315 goto out;
1316 }
1317
1318 newsrv->conn_src.source_addr = *sk;
1319
1320 if (port_low != port_high) {
1321 int i;
1322
1323 if (!port_low || !port_high) {
1324 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1325 file, linenum, args[cur_arg], args[cur_arg + 1]);
1326 err_code |= ERR_ALERT | ERR_FATAL;
1327 goto out;
1328 }
1329
1330 if (port_low <= 0 || port_low > 65535 ||
1331 port_high <= 0 || port_high > 65535 ||
1332 port_low > port_high) {
1333 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1334 file, linenum, port_low, port_high);
1335 err_code |= ERR_ALERT | ERR_FATAL;
1336 goto out;
1337 }
1338 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1339 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1340 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1341 }
1342
1343 cur_arg += 2;
1344 while (*(args[cur_arg])) {
1345 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1346#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1347#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001348 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001349 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1350 file, linenum, "usesrc", "source");
1351 err_code |= ERR_ALERT | ERR_FATAL;
1352 goto out;
1353 }
1354#endif
1355 if (!*args[cur_arg + 1]) {
1356 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1357 file, linenum, "usesrc");
1358 err_code |= ERR_ALERT | ERR_FATAL;
1359 goto out;
1360 }
1361 if (!strcmp(args[cur_arg + 1], "client")) {
1362 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1363 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1364 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1365 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1366 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1367 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1368 char *name, *end;
1369
1370 name = args[cur_arg+1] + 7;
1371 while (isspace(*name))
1372 name++;
1373
1374 end = name;
1375 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1376 end++;
1377
1378 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1379 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1380 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1381 newsrv->conn_src.bind_hdr_len = end - name;
1382 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1383 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1384 newsrv->conn_src.bind_hdr_occ = -1;
1385
1386 /* now look for an occurrence number */
1387 while (isspace(*end))
1388 end++;
1389 if (*end == ',') {
1390 end++;
1391 name = end;
1392 if (*end == '-')
1393 end++;
1394 while (isdigit((int)*end))
1395 end++;
1396 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1397 }
1398
1399 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1400 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1401 " occurrences values smaller than %d.\n",
1402 file, linenum, MAX_HDR_HISTORY);
1403 err_code |= ERR_ALERT | ERR_FATAL;
1404 goto out;
1405 }
1406 } else {
1407 struct sockaddr_storage *sk;
1408 int port1, port2;
1409
1410 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1411 if (!sk) {
1412 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1413 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1414 err_code |= ERR_ALERT | ERR_FATAL;
1415 goto out;
1416 }
1417
1418 proto = protocol_by_family(sk->ss_family);
1419 if (!proto || !proto->connect) {
1420 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1421 file, linenum, args[cur_arg], args[cur_arg+1]);
1422 err_code |= ERR_ALERT | ERR_FATAL;
1423 goto out;
1424 }
1425
1426 if (port1 != port2) {
1427 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1428 file, linenum, args[cur_arg], args[cur_arg + 1]);
1429 err_code |= ERR_ALERT | ERR_FATAL;
1430 goto out;
1431 }
1432 newsrv->conn_src.tproxy_addr = *sk;
1433 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1434 }
1435 global.last_checks |= LSTCHK_NETADM;
1436#if !defined(CONFIG_HAP_TRANSPARENT)
1437 global.last_checks |= LSTCHK_CTTPROXY;
1438#endif
1439 cur_arg += 2;
1440 continue;
1441#else /* no TPROXY support */
1442 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1443 file, linenum, "usesrc");
1444 err_code |= ERR_ALERT | ERR_FATAL;
1445 goto out;
1446#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1447 } /* "usesrc" */
1448
1449 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1450#ifdef SO_BINDTODEVICE
1451 if (!*args[cur_arg + 1]) {
1452 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1453 file, linenum, args[0]);
1454 err_code |= ERR_ALERT | ERR_FATAL;
1455 goto out;
1456 }
1457 free(newsrv->conn_src.iface_name);
1458 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1459 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1460 global.last_checks |= LSTCHK_NETADM;
1461#else
1462 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1463 file, linenum, args[0], args[cur_arg]);
1464 err_code |= ERR_ALERT | ERR_FATAL;
1465 goto out;
1466#endif
1467 cur_arg += 2;
1468 continue;
1469 }
1470 /* this keyword in not an option of "source" */
1471 break;
1472 } /* while */
1473 }
1474 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1475 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1476 file, linenum, "usesrc", "source");
1477 err_code |= ERR_ALERT | ERR_FATAL;
1478 goto out;
1479 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001480 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1481#ifdef CONFIG_HAP_NS
1482 char *arg = args[cur_arg + 1];
1483 if (!strcmp(arg, "*")) {
1484 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1485 } else {
1486 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1487
1488 if (newsrv->netns == NULL)
1489 newsrv->netns = netns_store_insert(arg);
1490
1491 if (newsrv->netns == NULL) {
1492 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1493 err_code |= ERR_ALERT | ERR_FATAL;
1494 goto out;
1495 }
1496 }
1497#else
1498 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1499 file, linenum, args[0], args[cur_arg]);
1500 err_code |= ERR_ALERT | ERR_FATAL;
1501 goto out;
1502#endif
1503 cur_arg += 2;
1504 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001505 else {
1506 static int srv_dumped;
1507 struct srv_kw *kw;
1508 char *err;
1509
1510 kw = srv_find_kw(args[cur_arg]);
1511 if (kw) {
1512 char *err = NULL;
1513 int code;
1514
1515 if (!kw->parse) {
1516 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1517 file, linenum, args[0], args[1], args[cur_arg]);
1518 cur_arg += 1 + kw->skip ;
1519 err_code |= ERR_ALERT | ERR_FATAL;
1520 goto out;
1521 }
1522
1523 if (defsrv && !kw->default_ok) {
1524 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1525 file, linenum, args[0], args[1], args[cur_arg]);
1526 cur_arg += 1 + kw->skip ;
1527 err_code |= ERR_ALERT;
1528 continue;
1529 }
1530
1531 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1532 err_code |= code;
1533
1534 if (code) {
1535 if (err && *err) {
1536 indent_msg(&err, 2);
1537 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1538 }
1539 else
1540 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1541 file, linenum, args[0], args[1], args[cur_arg]);
1542 if (code & ERR_FATAL) {
1543 free(err);
1544 cur_arg += 1 + kw->skip;
1545 goto out;
1546 }
1547 }
1548 free(err);
1549 cur_arg += 1 + kw->skip;
1550 continue;
1551 }
1552
1553 err = NULL;
1554 if (!srv_dumped) {
1555 srv_dump_kws(&err);
1556 indent_msg(&err, 4);
1557 srv_dumped = 1;
1558 }
1559
1560 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1561 file, linenum, args[0], args[1], args[cur_arg],
1562 err ? " Registered keywords :" : "", err ? err : "");
1563 free(err);
1564
1565 err_code |= ERR_ALERT | ERR_FATAL;
1566 goto out;
1567 }
1568 }
1569
Willy Tarreau272adea2014-03-31 10:39:59 +02001570 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001571 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001572
1573 if (newsrv->trackit) {
1574 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1575 file, linenum);
1576 err_code |= ERR_ALERT | ERR_FATAL;
1577 goto out;
1578 }
1579
1580 /* If neither a port nor an addr was specified and no check transport
1581 * layer is forced, then the transport layer used by the checks is the
1582 * same as for the production traffic. Otherwise we use raw_sock by
1583 * default, unless one is specified.
1584 */
Simon Horman41f58762015-01-30 11:22:56 +09001585 if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001586#ifdef USE_OPENSSL
1587 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1588#endif
David Safb76832014-05-08 23:42:08 -04001589 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001590 }
1591 /* try to get the port from check_core.addr if check.port not set */
1592 if (!newsrv->check.port)
Simon Horman41f58762015-01-30 11:22:56 +09001593 newsrv->check.port = get_host_port(&newsrv->check.addr);
Willy Tarreau272adea2014-03-31 10:39:59 +02001594
1595 if (!newsrv->check.port)
1596 newsrv->check.port = realport; /* by default */
1597
1598 if (!newsrv->check.port) {
1599 /* not yet valid, because no port was set on
1600 * the server either. We'll check if we have
1601 * a known port on the first listener.
1602 */
1603 struct listener *l;
1604
1605 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1606 newsrv->check.port = get_host_port(&l->addr);
1607 if (newsrv->check.port)
1608 break;
1609 }
1610 }
1611 /*
1612 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001613 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001614 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001615 if (!newsrv->check.port &&
Simon Horman41f58762015-01-30 11:22:56 +09001616 (is_inet_addr(&newsrv->check.addr) ||
1617 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001618 struct tcpcheck_rule *n = NULL, *r = NULL;
1619 struct list *l;
1620
1621 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1622 if (!r) {
1623 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1624 file, linenum, newsrv->id);
1625 err_code |= ERR_ALERT | ERR_FATAL;
1626 goto out;
1627 }
1628 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1629 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",
1630 file, linenum, newsrv->id);
1631 err_code |= ERR_ALERT | ERR_FATAL;
1632 goto out;
1633 }
1634 else {
1635 /* scan the tcp-check ruleset to ensure a port has been configured */
1636 l = &newsrv->proxy->tcpcheck_rules;
1637 list_for_each_entry(n, l, list) {
1638 r = (struct tcpcheck_rule *)n->list.p;
1639 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1640 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",
1641 file, linenum, newsrv->id);
1642 err_code |= ERR_ALERT | ERR_FATAL;
1643 goto out;
1644 }
1645 }
1646 }
1647 }
1648
1649 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001650 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001651 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001652 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1653 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001654 goto out;
1655 }
1656
1657 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1658 }
1659
1660 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001661 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001662
1663 if (!newsrv->agent.port) {
1664 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1665 file, linenum, newsrv->id);
1666 err_code |= ERR_ALERT | ERR_FATAL;
1667 goto out;
1668 }
1669
1670 if (!newsrv->agent.inter)
1671 newsrv->agent.inter = newsrv->check.inter;
1672
Simon Hormanb1900d52015-01-30 11:22:54 +09001673 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001674 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001675 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1676 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001677 goto out;
1678 }
1679
1680 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1681 }
1682
1683 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001684 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001685 curproxy->srv_bck++;
1686 else
1687 curproxy->srv_act++;
1688
Willy Tarreauc5150da2014-05-13 19:27:31 +02001689 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001690 }
1691 }
1692 return 0;
1693
1694 out:
1695 free(errmsg);
1696 return err_code;
1697}
1698
Simon Horman7d09b9a2013-02-12 10:45:51 +09001699/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001700 * Local variables:
1701 * c-indent-level: 8
1702 * c-basic-offset: 8
1703 * End:
1704 */