blob: 5f63f0df6e76624b343e2702f88afa8ed51ff82d [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>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020019#include <common/time.h>
20
Willy Tarreau272adea2014-03-31 10:39:59 +020021#include <types/global.h>
22
23#include <proto/port_range.h>
24#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020025#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020026#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020027#include <proto/server.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020028#include <proto/session.h>
29#include <proto/task.h>
30
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreau21faa912012-10-10 08:27:36 +020032/* List head of all known server keywords */
33static struct srv_kw_list srv_keywords = {
34 .list = LIST_HEAD_INIT(srv_keywords.list)
35};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020036
Simon Hormana3608442013-11-01 16:46:15 +090037int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020038{
Willy Tarreau892337c2014-05-13 23:41:20 +020039 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020040 return s->down_time;
41
42 return now.tv_sec - s->last_change + s->down_time;
43}
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050045int srv_lastsession(const struct server *s)
46{
47 if (s->counters.last_sess)
48 return now.tv_sec - s->counters.last_sess;
49
50 return -1;
51}
52
Simon Horman4a741432013-02-23 15:35:38 +090053int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020054{
Simon Horman4a741432013-02-23 15:35:38 +090055 const struct server *s = check->server;
56
Willy Tarreauff5ae352013-12-11 20:36:34 +010057 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090058 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010059
Willy Tarreau892337c2014-05-13 23:41:20 +020060 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090061 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010062
Simon Horman4a741432013-02-23 15:35:38 +090063 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010064}
65
Willy Tarreau21faa912012-10-10 08:27:36 +020066/*
67 * Registers the server keyword list <kwl> as a list of valid keywords for next
68 * parsing sessions.
69 */
70void srv_register_keywords(struct srv_kw_list *kwl)
71{
72 LIST_ADDQ(&srv_keywords.list, &kwl->list);
73}
74
75/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
76 * keyword is found with a NULL ->parse() function, then an attempt is made to
77 * find one with a valid ->parse() function. This way it is possible to declare
78 * platform-dependant, known keywords as NULL, then only declare them as valid
79 * if some options are met. Note that if the requested keyword contains an
80 * opening parenthesis, everything from this point is ignored.
81 */
82struct srv_kw *srv_find_kw(const char *kw)
83{
84 int index;
85 const char *kwend;
86 struct srv_kw_list *kwl;
87 struct srv_kw *ret = NULL;
88
89 kwend = strchr(kw, '(');
90 if (!kwend)
91 kwend = kw + strlen(kw);
92
93 list_for_each_entry(kwl, &srv_keywords.list, list) {
94 for (index = 0; kwl->kw[index].kw != NULL; index++) {
95 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
96 kwl->kw[index].kw[kwend-kw] == 0) {
97 if (kwl->kw[index].parse)
98 return &kwl->kw[index]; /* found it !*/
99 else
100 ret = &kwl->kw[index]; /* may be OK */
101 }
102 }
103 }
104 return ret;
105}
106
107/* Dumps all registered "server" keywords to the <out> string pointer. The
108 * unsupported keywords are only dumped if their supported form was not
109 * found.
110 */
111void srv_dump_kws(char **out)
112{
113 struct srv_kw_list *kwl;
114 int index;
115
116 *out = NULL;
117 list_for_each_entry(kwl, &srv_keywords.list, list) {
118 for (index = 0; kwl->kw[index].kw != NULL; index++) {
119 if (kwl->kw[index].parse ||
120 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
121 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
122 kwl->scope,
123 kwl->kw[index].kw,
124 kwl->kw[index].skip ? " <arg>" : "",
125 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
126 kwl->kw[index].parse ? "" : " (not supported)");
127 }
128 }
129 }
130}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100131
Willy Tarreaudff55432012-10-10 17:51:05 +0200132/* parse the "id" server keyword */
133static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
134{
135 struct eb32_node *node;
136
137 if (!*args[*cur_arg + 1]) {
138 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
139 return ERR_ALERT | ERR_FATAL;
140 }
141
142 newsrv->puid = atol(args[*cur_arg + 1]);
143 newsrv->conf.id.key = newsrv->puid;
144
145 if (newsrv->puid <= 0) {
146 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
147 return ERR_ALERT | ERR_FATAL;
148 }
149
150 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
151 if (node) {
152 struct server *target = container_of(node, struct server, conf.id);
153 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
154 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
155 target->id);
156 return ERR_ALERT | ERR_FATAL;
157 }
158
159 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
160 return 0;
161}
162
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200163/* Shutdown all connections of a server. The caller must pass a termination
164 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
165 * shutdown.
166 */
167void srv_shutdown_sessions(struct server *srv, int why)
168{
169 struct session *session, *session_bck;
170
171 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
172 if (session->srv_conn == srv)
173 session_shutdown(session, why);
174}
175
176/* Shutdown all connections of all backup servers of a proxy. The caller must
177 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
178 * the reason for the shutdown.
179 */
180void srv_shutdown_backup_sessions(struct proxy *px, int why)
181{
182 struct server *srv;
183
184 for (srv = px->srv; srv != NULL; srv = srv->next)
185 if (srv->flags & SRV_F_BACKUP)
186 srv_shutdown_sessions(srv, why);
187}
188
Willy Tarreaubda92272014-05-20 21:55:30 +0200189/* Appends some information to a message string related to a server going UP or
190 * DOWN. If both <forced> and <reason> are null and the server tracks another
191 * one, a "via" information will be provided to know where the status came from.
192 * If <reason> is non-null, the entire string will be appended after a comma and
193 * a space (eg: to report some information from the check that changed the state).
194 * If <xferred> is non-negative, some information about requeued sessions are
195 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200196 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200197void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200198{
Willy Tarreaubda92272014-05-20 21:55:30 +0200199 if (reason)
200 chunk_appendf(msg, ", %s", reason);
201 else if (!forced && s->track)
202 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200203
204 if (xferred >= 0) {
205 if (s->state == SRV_ST_STOPPED)
206 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
207 " %d sessions active, %d requeued, %d remaining in queue",
208 s->proxy->srv_act, s->proxy->srv_bck,
209 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
210 s->cur_sess, xferred, s->nbpend);
211 else
212 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
213 " %d sessions requeued, %d total in queue",
214 s->proxy->srv_act, s->proxy->srv_bck,
215 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
216 xferred, s->nbpend);
217 }
218}
219
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200220/* Marks server <s> down, regardless of its checks' statuses, notifies by all
221 * available means, recounts the remaining servers on the proxy and transfers
222 * queued sessions whenever possible to other servers. It automatically
223 * recomputes the number of servers, but not the map. Maintenance servers are
224 * ignored. It reports <reason> if non-null as the reason for going down. Note
225 * that it makes use of the trash to build the log strings, so <reason> must
226 * not be placed there.
227 */
228void srv_set_stopped(struct server *s, const char *reason)
229{
230 struct server *srv;
231 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
232 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
233 int xferred;
234
235 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
236 return;
237
238 s->last_change = now.tv_sec;
239 s->state = SRV_ST_STOPPED;
240 if (s->proxy->lbprm.set_server_status_down)
241 s->proxy->lbprm.set_server_status_down(s);
242
243 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
244 srv_shutdown_sessions(s, SN_ERR_DOWN);
245
246 /* we might have sessions queued on this server and waiting for
247 * a connection. Those which are redispatchable will be queued
248 * to another server or to the proxy itself.
249 */
250 xferred = pendconn_redistribute(s);
251
252 chunk_printf(&trash,
253 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
254 s->proxy->id, s->id);
255
256 srv_append_status(&trash, s, reason, xferred, 0);
257 Warning("%s.\n", trash.str);
natalie.chen2d0e45b2015-04-22 14:10:12 +0800258 Emaila("%s \n", trash.str);
259
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200260 /* we don't send an alert if the server was previously paused */
261 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
262
263 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
264 set_backend_down(s->proxy);
265
266 s->counters.down_trans++;
267
268 for (srv = s->trackers; srv; srv = srv->tracknext)
269 srv_set_stopped(srv, NULL);
270}
271
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200272/* Marks server <s> up regardless of its checks' statuses and provided it isn't
273 * in maintenance. Notifies by all available means, recounts the remaining
274 * servers on the proxy and tries to grab requests from the proxy. It
275 * automatically recomputes the number of servers, but not the map. Maintenance
276 * servers are ignored. It reports <reason> if non-null as the reason for going
277 * up. Note that it makes use of the trash to build the log strings, so <reason>
278 * must not be placed there.
279 */
280void srv_set_running(struct server *s, const char *reason)
281{
282 struct server *srv;
283 int xferred;
284
285 if (s->admin & SRV_ADMF_MAINT)
286 return;
287
288 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
289 return;
290
291 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
292 if (s->proxy->last_change < now.tv_sec) // ignore negative times
293 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
294 s->proxy->last_change = now.tv_sec;
295 }
296
297 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
298 s->down_time += now.tv_sec - s->last_change;
299
300 s->last_change = now.tv_sec;
301
302 s->state = SRV_ST_STARTING;
303 if (s->slowstart > 0)
304 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
305 else
306 s->state = SRV_ST_RUNNING;
307
308 server_recalc_eweight(s);
309
310 /* If the server is set with "on-marked-up shutdown-backup-sessions",
311 * and it's not a backup server and its effective weight is > 0,
312 * then it can accept new connections, so we shut down all sessions
313 * on all backup servers.
314 */
315 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
316 !(s->flags & SRV_F_BACKUP) && s->eweight)
317 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
318
319 /* check if we can handle some connections queued at the proxy. We
320 * will take as many as we can handle.
321 */
322 xferred = pendconn_grab_from_px(s);
323
324 chunk_printf(&trash,
325 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
326 s->proxy->id, s->id);
327
328 srv_append_status(&trash, s, reason, xferred, 0);
329 Warning("%s.\n", trash.str);
natalie.chencd4d6bb2015-04-24 11:59:21 +0800330 Emaila("%s \n", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200331 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 +0200800static int init_check(struct check *check, int type, const char * file, int linenum)
801{
802 check->type = type;
803
804 /* Allocate buffer for requests... */
805 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
806 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
807 return ERR_ALERT | ERR_ABORT;
808 }
809 check->bi->size = global.tune.chksize;
810
811 /* Allocate buffer for responses... */
812 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
813 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
814 return ERR_ALERT | ERR_ABORT;
815 }
816 check->bo->size = global.tune.chksize;
817
818 /* Allocate buffer for partial results... */
819 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
820 Alert("parsing [%s:%d] : out of memory while allocating check connection.\n", file, linenum);
821 return ERR_ALERT | ERR_ABORT;
822 }
823
824 check->conn->t.sock.fd = -1; /* no agent in progress yet */
825
826 return 0;
827}
828
829int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
830{
831 struct server *newsrv = NULL;
832 const char *err;
833 char *errmsg = NULL;
834 int err_code = 0;
835 unsigned val;
836
837 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
838 int cur_arg;
839 short realport = 0;
840 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
841
842 if (!defsrv && curproxy == defproxy) {
843 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
844 err_code |= ERR_ALERT | ERR_FATAL;
845 goto out;
846 }
847 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
848 err_code |= ERR_ALERT | ERR_FATAL;
849
850 if (!*args[2]) {
851 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
852 file, linenum, args[0]);
853 err_code |= ERR_ALERT | ERR_FATAL;
854 goto out;
855 }
856
857 err = invalid_char(args[1]);
858 if (err && !defsrv) {
859 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
860 file, linenum, *err, args[1]);
861 err_code |= ERR_ALERT | ERR_FATAL;
862 goto out;
863 }
864
865 if (!defsrv) {
866 struct sockaddr_storage *sk;
867 int port1, port2;
868 struct protocol *proto;
869
870 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
871 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
872 err_code |= ERR_ALERT | ERR_ABORT;
873 goto out;
874 }
875
876 /* the servers are linked backwards first */
877 newsrv->next = curproxy->srv;
878 curproxy->srv = newsrv;
879 newsrv->proxy = curproxy;
880 newsrv->conf.file = strdup(file);
881 newsrv->conf.line = linenum;
882
883 newsrv->obj_type = OBJ_TYPE_SERVER;
884 LIST_INIT(&newsrv->actconns);
885 LIST_INIT(&newsrv->pendconns);
886 do_check = 0;
887 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200888 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200889 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200890 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200891 newsrv->last_change = now.tv_sec;
892 newsrv->id = strdup(args[1]);
893
894 /* several ways to check the port component :
895 * - IP => port=+0, relative (IPv4 only)
896 * - IP: => port=+0, relative
897 * - IP:N => port=N, absolute
898 * - IP:+N => port=+N, relative
899 * - IP:-N => port=-N, relative
900 */
901 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
902 if (!sk) {
903 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
904 err_code |= ERR_ALERT | ERR_FATAL;
905 goto out;
906 }
907
908 proto = protocol_by_family(sk->ss_family);
909 if (!proto || !proto->connect) {
910 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
911 file, linenum, args[0], args[1]);
912 err_code |= ERR_ALERT | ERR_FATAL;
913 goto out;
914 }
915
916 if (!port1 || !port2) {
917 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200918 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200919 }
920 else if (port1 != port2) {
921 /* port range */
922 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
923 file, linenum, args[0], args[1], args[2]);
924 err_code |= ERR_ALERT | ERR_FATAL;
925 goto out;
926 }
927 else {
928 /* used by checks */
929 realport = port1;
930 }
931
932 newsrv->addr = *sk;
933 newsrv->proto = newsrv->check_common.proto = protocol_by_family(newsrv->addr.ss_family);
Cyril Bonté1f96a872014-11-15 22:41:27 +0100934 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200935
936 if (!newsrv->proto) {
937 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
938 file, linenum, newsrv->addr.ss_family, args[2]);
939 err_code |= ERR_ALERT | ERR_FATAL;
940 goto out;
941 }
942
943 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
944 newsrv->check.port = curproxy->defsrv.check.port;
945 newsrv->check.inter = curproxy->defsrv.check.inter;
946 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
947 newsrv->check.downinter = curproxy->defsrv.check.downinter;
948 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
949 newsrv->agent.port = curproxy->defsrv.agent.port;
950 newsrv->agent.inter = curproxy->defsrv.agent.inter;
951 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
952 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
953 newsrv->maxqueue = curproxy->defsrv.maxqueue;
954 newsrv->minconn = curproxy->defsrv.minconn;
955 newsrv->maxconn = curproxy->defsrv.maxconn;
956 newsrv->slowstart = curproxy->defsrv.slowstart;
957 newsrv->onerror = curproxy->defsrv.onerror;
958 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
959 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
960 newsrv->consecutive_errors_limit
961 = curproxy->defsrv.consecutive_errors_limit;
962#ifdef OPENSSL
963 newsrv->use_ssl = curproxy->defsrv.use_ssl;
964#endif
965 newsrv->uweight = newsrv->iweight
966 = curproxy->defsrv.iweight;
967
968 newsrv->check.status = HCHK_STATUS_INI;
969 newsrv->check.rise = curproxy->defsrv.check.rise;
970 newsrv->check.fall = curproxy->defsrv.check.fall;
971 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
972 newsrv->check.server = newsrv;
973
974 newsrv->agent.status = HCHK_STATUS_INI;
975 newsrv->agent.rise = curproxy->defsrv.agent.rise;
976 newsrv->agent.fall = curproxy->defsrv.agent.fall;
977 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
978 newsrv->agent.server = newsrv;
979
980 cur_arg = 3;
981 } else {
982 newsrv = &curproxy->defsrv;
983 cur_arg = 1;
984 }
985
986 while (*args[cur_arg]) {
987 if (!strcmp(args[cur_arg], "agent-check")) {
988 global.maxsock++;
989 do_agent = 1;
990 cur_arg += 1;
991 } else if (!strcmp(args[cur_arg], "agent-inter")) {
992 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
993 if (err) {
994 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
995 file, linenum, *err, newsrv->id);
996 err_code |= ERR_ALERT | ERR_FATAL;
997 goto out;
998 }
999 if (val <= 0) {
1000 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1001 file, linenum, val, args[cur_arg], newsrv->id);
1002 err_code |= ERR_ALERT | ERR_FATAL;
1003 goto out;
1004 }
1005 newsrv->agent.inter = val;
1006 cur_arg += 2;
1007 }
1008 else if (!strcmp(args[cur_arg], "agent-port")) {
1009 global.maxsock++;
1010 newsrv->agent.port = atol(args[cur_arg + 1]);
1011 cur_arg += 2;
1012 }
1013 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1014 newsrv->cookie = strdup(args[cur_arg + 1]);
1015 newsrv->cklen = strlen(args[cur_arg + 1]);
1016 cur_arg += 2;
1017 }
1018 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1019 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1020 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1021 cur_arg += 2;
1022 }
1023 else if (!strcmp(args[cur_arg], "rise")) {
1024 if (!*args[cur_arg + 1]) {
1025 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1026 file, linenum, args[cur_arg]);
1027 err_code |= ERR_ALERT | ERR_FATAL;
1028 goto out;
1029 }
1030
1031 newsrv->check.rise = atol(args[cur_arg + 1]);
1032 if (newsrv->check.rise <= 0) {
1033 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1034 file, linenum, args[cur_arg]);
1035 err_code |= ERR_ALERT | ERR_FATAL;
1036 goto out;
1037 }
1038
1039 if (newsrv->check.health)
1040 newsrv->check.health = newsrv->check.rise;
1041 cur_arg += 2;
1042 }
1043 else if (!strcmp(args[cur_arg], "fall")) {
1044 newsrv->check.fall = atol(args[cur_arg + 1]);
1045
1046 if (!*args[cur_arg + 1]) {
1047 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1048 file, linenum, args[cur_arg]);
1049 err_code |= ERR_ALERT | ERR_FATAL;
1050 goto out;
1051 }
1052
1053 if (newsrv->check.fall <= 0) {
1054 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1055 file, linenum, args[cur_arg]);
1056 err_code |= ERR_ALERT | ERR_FATAL;
1057 goto out;
1058 }
1059
1060 cur_arg += 2;
1061 }
1062 else if (!strcmp(args[cur_arg], "inter")) {
1063 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1064 if (err) {
1065 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1066 file, linenum, *err, newsrv->id);
1067 err_code |= ERR_ALERT | ERR_FATAL;
1068 goto out;
1069 }
1070 if (val <= 0) {
1071 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1072 file, linenum, val, args[cur_arg], newsrv->id);
1073 err_code |= ERR_ALERT | ERR_FATAL;
1074 goto out;
1075 }
1076 newsrv->check.inter = val;
1077 cur_arg += 2;
1078 }
1079 else if (!strcmp(args[cur_arg], "fastinter")) {
1080 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1081 if (err) {
1082 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1083 file, linenum, *err, newsrv->id);
1084 err_code |= ERR_ALERT | ERR_FATAL;
1085 goto out;
1086 }
1087 if (val <= 0) {
1088 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1089 file, linenum, val, args[cur_arg], newsrv->id);
1090 err_code |= ERR_ALERT | ERR_FATAL;
1091 goto out;
1092 }
1093 newsrv->check.fastinter = val;
1094 cur_arg += 2;
1095 }
1096 else if (!strcmp(args[cur_arg], "downinter")) {
1097 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1098 if (err) {
1099 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1100 file, linenum, *err, newsrv->id);
1101 err_code |= ERR_ALERT | ERR_FATAL;
1102 goto out;
1103 }
1104 if (val <= 0) {
1105 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1106 file, linenum, val, args[cur_arg], newsrv->id);
1107 err_code |= ERR_ALERT | ERR_FATAL;
1108 goto out;
1109 }
1110 newsrv->check.downinter = val;
1111 cur_arg += 2;
1112 }
1113 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1114 struct sockaddr_storage *sk;
1115 int port1, port2;
1116 struct protocol *proto;
1117
1118 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1119 if (!sk) {
1120 Alert("parsing [%s:%d] : '%s' : %s\n",
1121 file, linenum, args[cur_arg], errmsg);
1122 err_code |= ERR_ALERT | ERR_FATAL;
1123 goto out;
1124 }
1125
1126 proto = protocol_by_family(sk->ss_family);
1127 if (!proto || !proto->connect) {
1128 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1129 file, linenum, args[cur_arg], args[cur_arg + 1]);
1130 err_code |= ERR_ALERT | ERR_FATAL;
1131 goto out;
1132 }
1133
1134 if (port1 != port2) {
1135 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1136 file, linenum, args[cur_arg], args[cur_arg + 1]);
1137 err_code |= ERR_ALERT | ERR_FATAL;
1138 goto out;
1139 }
1140
1141 newsrv->check_common.addr = *sk;
Willy Tarreau640556c2014-05-09 23:38:15 +02001142 newsrv->check_common.proto = protocol_by_family(sk->ss_family);
Willy Tarreau272adea2014-03-31 10:39:59 +02001143 cur_arg += 2;
1144 }
1145 else if (!strcmp(args[cur_arg], "port")) {
1146 newsrv->check.port = atol(args[cur_arg + 1]);
1147 cur_arg += 2;
1148 }
1149 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001150 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001151 cur_arg ++;
1152 }
1153 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001154 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001155 cur_arg ++;
1156 }
1157 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001158 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001159 cur_arg ++;
1160 }
David Safb76832014-05-08 23:42:08 -04001161 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1162 newsrv->pp_opts |= SRV_PP_V2;
1163 cur_arg ++;
1164 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001165 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1166 newsrv->check.send_proxy = 1;
1167 cur_arg ++;
1168 }
1169 else if (!strcmp(args[cur_arg], "weight")) {
1170 int w;
1171 w = atol(args[cur_arg + 1]);
1172 if (w < 0 || w > SRV_UWGHT_MAX) {
1173 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1174 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1175 err_code |= ERR_ALERT | ERR_FATAL;
1176 goto out;
1177 }
1178 newsrv->uweight = newsrv->iweight = w;
1179 cur_arg += 2;
1180 }
1181 else if (!strcmp(args[cur_arg], "minconn")) {
1182 newsrv->minconn = atol(args[cur_arg + 1]);
1183 cur_arg += 2;
1184 }
1185 else if (!strcmp(args[cur_arg], "maxconn")) {
1186 newsrv->maxconn = atol(args[cur_arg + 1]);
1187 cur_arg += 2;
1188 }
1189 else if (!strcmp(args[cur_arg], "maxqueue")) {
1190 newsrv->maxqueue = atol(args[cur_arg + 1]);
1191 cur_arg += 2;
1192 }
1193 else if (!strcmp(args[cur_arg], "slowstart")) {
1194 /* slowstart is stored in seconds */
1195 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1196 if (err) {
1197 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1198 file, linenum, *err, newsrv->id);
1199 err_code |= ERR_ALERT | ERR_FATAL;
1200 goto out;
1201 }
1202 newsrv->slowstart = (val + 999) / 1000;
1203 cur_arg += 2;
1204 }
1205 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1206
1207 if (!*args[cur_arg + 1]) {
1208 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1209 file, linenum);
1210 err_code |= ERR_ALERT | ERR_FATAL;
1211 goto out;
1212 }
1213
1214 newsrv->trackit = strdup(args[cur_arg + 1]);
1215
1216 cur_arg += 2;
1217 }
1218 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1219 global.maxsock++;
1220 do_check = 1;
1221 cur_arg += 1;
1222 }
1223 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001224 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001225 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001226 newsrv->check.state |= CHK_ST_PAUSED;
1227 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001228 cur_arg += 1;
1229 }
1230 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1231 if (!strcmp(args[cur_arg + 1], "none"))
1232 newsrv->observe = HANA_OBS_NONE;
1233 else if (!strcmp(args[cur_arg + 1], "layer4"))
1234 newsrv->observe = HANA_OBS_LAYER4;
1235 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1236 if (curproxy->mode != PR_MODE_HTTP) {
1237 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1238 file, linenum, args[cur_arg + 1]);
1239 err_code |= ERR_ALERT;
1240 }
1241 newsrv->observe = HANA_OBS_LAYER7;
1242 }
1243 else {
1244 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1245 "'layer4', 'layer7' but got '%s'\n",
1246 file, linenum, args[cur_arg], args[cur_arg + 1]);
1247 err_code |= ERR_ALERT | ERR_FATAL;
1248 goto out;
1249 }
1250
1251 cur_arg += 2;
1252 }
1253 else if (!strcmp(args[cur_arg], "on-error")) {
1254 if (!strcmp(args[cur_arg + 1], "fastinter"))
1255 newsrv->onerror = HANA_ONERR_FASTINTER;
1256 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1257 newsrv->onerror = HANA_ONERR_FAILCHK;
1258 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1259 newsrv->onerror = HANA_ONERR_SUDDTH;
1260 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1261 newsrv->onerror = HANA_ONERR_MARKDWN;
1262 else {
1263 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1264 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1265 file, linenum, args[cur_arg], args[cur_arg + 1]);
1266 err_code |= ERR_ALERT | ERR_FATAL;
1267 goto out;
1268 }
1269
1270 cur_arg += 2;
1271 }
1272 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1273 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1274 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1275 else {
1276 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1277 file, linenum, args[cur_arg], args[cur_arg + 1]);
1278 err_code |= ERR_ALERT | ERR_FATAL;
1279 goto out;
1280 }
1281
1282 cur_arg += 2;
1283 }
1284 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1285 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1286 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1287 else {
1288 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1289 file, linenum, args[cur_arg], args[cur_arg + 1]);
1290 err_code |= ERR_ALERT | ERR_FATAL;
1291 goto out;
1292 }
1293
1294 cur_arg += 2;
1295 }
1296 else if (!strcmp(args[cur_arg], "error-limit")) {
1297 if (!*args[cur_arg + 1]) {
1298 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1299 file, linenum, args[cur_arg]);
1300 err_code |= ERR_ALERT | ERR_FATAL;
1301 goto out;
1302 }
1303
1304 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1305
1306 if (newsrv->consecutive_errors_limit <= 0) {
1307 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1308 file, linenum, args[cur_arg]);
1309 err_code |= ERR_ALERT | ERR_FATAL;
1310 goto out;
1311 }
1312 cur_arg += 2;
1313 }
1314 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1315 int port_low, port_high;
1316 struct sockaddr_storage *sk;
1317 struct protocol *proto;
1318
1319 if (!*args[cur_arg + 1]) {
1320 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1321 file, linenum, "source", "usesrc", "interface");
1322 err_code |= ERR_ALERT | ERR_FATAL;
1323 goto out;
1324 }
1325
1326 newsrv->conn_src.opts |= CO_SRC_BIND;
1327 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1328 if (!sk) {
1329 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1330 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1331 err_code |= ERR_ALERT | ERR_FATAL;
1332 goto out;
1333 }
1334
1335 proto = protocol_by_family(sk->ss_family);
1336 if (!proto || !proto->connect) {
1337 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1338 file, linenum, args[cur_arg], args[cur_arg+1]);
1339 err_code |= ERR_ALERT | ERR_FATAL;
1340 goto out;
1341 }
1342
1343 newsrv->conn_src.source_addr = *sk;
1344
1345 if (port_low != port_high) {
1346 int i;
1347
1348 if (!port_low || !port_high) {
1349 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1350 file, linenum, args[cur_arg], args[cur_arg + 1]);
1351 err_code |= ERR_ALERT | ERR_FATAL;
1352 goto out;
1353 }
1354
1355 if (port_low <= 0 || port_low > 65535 ||
1356 port_high <= 0 || port_high > 65535 ||
1357 port_low > port_high) {
1358 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1359 file, linenum, port_low, port_high);
1360 err_code |= ERR_ALERT | ERR_FATAL;
1361 goto out;
1362 }
1363 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1364 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1365 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1366 }
1367
1368 cur_arg += 2;
1369 while (*(args[cur_arg])) {
1370 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1371#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1372#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001373 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001374 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1375 file, linenum, "usesrc", "source");
1376 err_code |= ERR_ALERT | ERR_FATAL;
1377 goto out;
1378 }
1379#endif
1380 if (!*args[cur_arg + 1]) {
1381 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1382 file, linenum, "usesrc");
1383 err_code |= ERR_ALERT | ERR_FATAL;
1384 goto out;
1385 }
1386 if (!strcmp(args[cur_arg + 1], "client")) {
1387 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1388 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1389 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1390 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1391 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1392 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1393 char *name, *end;
1394
1395 name = args[cur_arg+1] + 7;
1396 while (isspace(*name))
1397 name++;
1398
1399 end = name;
1400 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1401 end++;
1402
1403 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1404 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1405 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1406 newsrv->conn_src.bind_hdr_len = end - name;
1407 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1408 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1409 newsrv->conn_src.bind_hdr_occ = -1;
1410
1411 /* now look for an occurrence number */
1412 while (isspace(*end))
1413 end++;
1414 if (*end == ',') {
1415 end++;
1416 name = end;
1417 if (*end == '-')
1418 end++;
1419 while (isdigit((int)*end))
1420 end++;
1421 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1422 }
1423
1424 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1425 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1426 " occurrences values smaller than %d.\n",
1427 file, linenum, MAX_HDR_HISTORY);
1428 err_code |= ERR_ALERT | ERR_FATAL;
1429 goto out;
1430 }
1431 } else {
1432 struct sockaddr_storage *sk;
1433 int port1, port2;
1434
1435 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1436 if (!sk) {
1437 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1438 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1439 err_code |= ERR_ALERT | ERR_FATAL;
1440 goto out;
1441 }
1442
1443 proto = protocol_by_family(sk->ss_family);
1444 if (!proto || !proto->connect) {
1445 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1446 file, linenum, args[cur_arg], args[cur_arg+1]);
1447 err_code |= ERR_ALERT | ERR_FATAL;
1448 goto out;
1449 }
1450
1451 if (port1 != port2) {
1452 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1453 file, linenum, args[cur_arg], args[cur_arg + 1]);
1454 err_code |= ERR_ALERT | ERR_FATAL;
1455 goto out;
1456 }
1457 newsrv->conn_src.tproxy_addr = *sk;
1458 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1459 }
1460 global.last_checks |= LSTCHK_NETADM;
1461#if !defined(CONFIG_HAP_TRANSPARENT)
1462 global.last_checks |= LSTCHK_CTTPROXY;
1463#endif
1464 cur_arg += 2;
1465 continue;
1466#else /* no TPROXY support */
1467 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1468 file, linenum, "usesrc");
1469 err_code |= ERR_ALERT | ERR_FATAL;
1470 goto out;
1471#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1472 } /* "usesrc" */
1473
1474 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1475#ifdef SO_BINDTODEVICE
1476 if (!*args[cur_arg + 1]) {
1477 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1478 file, linenum, args[0]);
1479 err_code |= ERR_ALERT | ERR_FATAL;
1480 goto out;
1481 }
1482 free(newsrv->conn_src.iface_name);
1483 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1484 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1485 global.last_checks |= LSTCHK_NETADM;
1486#else
1487 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1488 file, linenum, args[0], args[cur_arg]);
1489 err_code |= ERR_ALERT | ERR_FATAL;
1490 goto out;
1491#endif
1492 cur_arg += 2;
1493 continue;
1494 }
1495 /* this keyword in not an option of "source" */
1496 break;
1497 } /* while */
1498 }
1499 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1500 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1501 file, linenum, "usesrc", "source");
1502 err_code |= ERR_ALERT | ERR_FATAL;
1503 goto out;
1504 }
1505 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) {
1571 int ret;
1572
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 */
1585 if (!newsrv->check.port && !is_addr(&newsrv->check_common.addr)) {
1586#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)
1593 newsrv->check.port = get_host_port(&newsrv->check_common.addr);
1594
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 &&
1616 (is_inet_addr(&newsrv->check_common.addr) ||
1617 (!is_addr(&newsrv->check_common.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau6ae30f82016-03-08 15:20:25 +01001618 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001619 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;
Willy Tarreau6ae30f82016-03-08 15:20:25 +01001637 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001638 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1639 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",
1640 file, linenum, newsrv->id);
1641 err_code |= ERR_ALERT | ERR_FATAL;
1642 goto out;
1643 }
1644 }
1645 }
1646 }
1647
1648 /* note: check type will be set during the config review phase */
1649 ret = init_check(&newsrv->check, 0, file, linenum);
1650 if (ret) {
1651 err_code |= ret;
1652 goto out;
1653 }
1654
1655 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1656 }
1657
1658 if (do_agent) {
1659 int ret;
1660
1661 if (!newsrv->agent.port) {
1662 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1663 file, linenum, newsrv->id);
1664 err_code |= ERR_ALERT | ERR_FATAL;
1665 goto out;
1666 }
1667
1668 if (!newsrv->agent.inter)
1669 newsrv->agent.inter = newsrv->check.inter;
1670
1671 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK, file, linenum);
1672 if (ret) {
1673 err_code |= ret;
1674 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 */