blob: 067909c8fd520ce578126d29de5c83a5b3ea2e42 [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);
330 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
331
332 for (srv = s->trackers; srv; srv = srv->tracknext)
333 srv_set_running(srv, NULL);
334}
335
Willy Tarreau8eb77842014-05-21 13:54:57 +0200336/* Marks server <s> stopping regardless of its checks' statuses and provided it
337 * isn't in maintenance. Notifies by all available means, recounts the remaining
338 * servers on the proxy and tries to grab requests from the proxy. It
339 * automatically recomputes the number of servers, but not the map. Maintenance
340 * servers are ignored. It reports <reason> if non-null as the reason for going
341 * up. Note that it makes use of the trash to build the log strings, so <reason>
342 * must not be placed there.
343 */
344void srv_set_stopping(struct server *s, const char *reason)
345{
346 struct server *srv;
347 int xferred;
348
349 if (s->admin & SRV_ADMF_MAINT)
350 return;
351
352 if (s->state == SRV_ST_STOPPING)
353 return;
354
355 s->last_change = now.tv_sec;
356 s->state = SRV_ST_STOPPING;
357 if (s->proxy->lbprm.set_server_status_down)
358 s->proxy->lbprm.set_server_status_down(s);
359
360 /* we might have sessions queued on this server and waiting for
361 * a connection. Those which are redispatchable will be queued
362 * to another server or to the proxy itself.
363 */
364 xferred = pendconn_redistribute(s);
365
366 chunk_printf(&trash,
367 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
368 s->proxy->id, s->id);
369
370 srv_append_status(&trash, s, reason, xferred, 0);
371
372 Warning("%s.\n", trash.str);
373 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
374
375 if (!s->proxy->srv_bck && !s->proxy->srv_act)
376 set_backend_down(s->proxy);
377
378 for (srv = s->trackers; srv; srv = srv->tracknext)
379 srv_set_stopping(srv, NULL);
380}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200381
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200382/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
383 * enforce either maint mode or drain mode. It is not allowed to set more than
384 * one flag at once. The equivalent "inherited" flag is propagated to all
385 * tracking servers. Maintenance mode disables health checks (but not agent
386 * checks). When either the flag is already set or no flag is passed, nothing
387 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200388 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200389void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200390{
391 struct check *check = &s->check;
392 struct server *srv;
393 int xferred;
394
395 if (!mode)
396 return;
397
398 /* stop going down as soon as we meet a server already in the same state */
399 if (s->admin & mode)
400 return;
401
402 s->admin |= mode;
403
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200404 /* stop going down if the equivalent flag was already present (forced or inherited) */
405 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
406 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
407 return;
408
409 /* Maintenance must also disable health checks */
410 if (mode & SRV_ADMF_MAINT) {
411 if (s->check.state & CHK_ST_ENABLED) {
412 s->check.state |= CHK_ST_PAUSED;
413 check->health = 0;
414 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200415
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200416 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200417 chunk_printf(&trash,
418 "%sServer %s/%s was DOWN and now enters maintenance",
419 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
420
Willy Tarreaubda92272014-05-20 21:55:30 +0200421 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200422
423 Warning("%s.\n", trash.str);
424 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
425 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200426 else { /* server was still running */
427 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
428 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
429
430 check->health = 0; /* failure */
431 s->last_change = now.tv_sec;
432 s->state = SRV_ST_STOPPED;
433 if (s->proxy->lbprm.set_server_status_down)
434 s->proxy->lbprm.set_server_status_down(s);
435
436 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
437 srv_shutdown_sessions(s, SN_ERR_DOWN);
438
439 /* we might have sessions queued on this server and waiting for
440 * a connection. Those which are redispatchable will be queued
441 * to another server or to the proxy itself.
442 */
443 xferred = pendconn_redistribute(s);
444
445 chunk_printf(&trash,
446 "%sServer %s/%s is going DOWN for maintenance",
447 s->flags & SRV_F_BACKUP ? "Backup " : "",
448 s->proxy->id, s->id);
449
450 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
451
452 Warning("%s.\n", trash.str);
453 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
454
455 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
456 set_backend_down(s->proxy);
457
458 s->counters.down_trans++;
459 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200460 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200461
462 /* drain state is applied only if not yet in maint */
463 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200464 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
465
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200466 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200467 if (s->proxy->lbprm.set_server_status_down)
468 s->proxy->lbprm.set_server_status_down(s);
469
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200470 /* we might have sessions queued on this server and waiting for
471 * a connection. Those which are redispatchable will be queued
472 * to another server or to the proxy itself.
473 */
474 xferred = pendconn_redistribute(s);
475
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200476 chunk_printf(&trash, "%sServer %s/%s enters drain state",
477 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200478
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200479 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200480
481 Warning("%s.\n", trash.str);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200482 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200483
484 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
485 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200486 }
487
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200488 /* compute the inherited flag to propagate */
489 if (mode & SRV_ADMF_MAINT)
490 mode = SRV_ADMF_IMAINT;
491 else if (mode & SRV_ADMF_DRAIN)
492 mode = SRV_ADMF_IDRAIN;
493
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200494 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200495 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200496}
497
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200498/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
499 * stop enforcing either maint mode or drain mode. It is not allowed to set more
500 * than one flag at once. The equivalent "inherited" flag is propagated to all
501 * tracking servers. Leaving maintenance mode re-enables health checks. When
502 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200503 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200504void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200505{
506 struct check *check = &s->check;
507 struct server *srv;
508 int xferred = -1;
509
510 if (!mode)
511 return;
512
513 /* stop going down as soon as we see the flag is not there anymore */
514 if (!(s->admin & mode))
515 return;
516
517 s->admin &= ~mode;
518
519 if (s->admin & SRV_ADMF_MAINT) {
520 /* remaining in maintenance mode, let's inform precisely about the
521 * situation.
522 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200523 if (mode & SRV_ADMF_FMAINT) {
524 chunk_printf(&trash,
525 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
526 s->flags & SRV_F_BACKUP ? "Backup " : "",
527 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200528
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200529 if (s->track) /* normally it's mandatory here */
530 chunk_appendf(&trash, " via %s/%s",
531 s->track->proxy->id, s->track->id);
532 Warning("%s.\n", trash.str);
533 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
534 }
535 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200536 chunk_printf(&trash,
537 "%sServer %s/%s remains in forced maintenance",
538 s->flags & SRV_F_BACKUP ? "Backup " : "",
539 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200540 Warning("%s.\n", trash.str);
541 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
542 }
543 /* don't report anything when leaving drain mode and remaining in maintenance */
544 }
545 else if (mode & SRV_ADMF_MAINT) {
546 /* OK here we're leaving maintenance, we have many things to check,
547 * because the server might possibly be coming back up depending on
548 * its state. In practice, leaving maintenance means that we should
549 * immediately turn to UP (more or less the slowstart) under the
550 * following conditions :
551 * - server is neither checked nor tracked
552 * - server tracks another server which is not checked
553 * - server tracks another server which is already up
554 * Which sums up as something simpler :
555 * "either the tracking server is up or the server's checks are disabled
556 * or up". Otherwise we only re-enable health checks. There's a special
557 * case associated to the stopping state which can be inherited. Note
558 * that the server might still be in drain mode, which is naturally dealt
559 * with by the lower level functions.
560 */
561
562 if (s->check.state & CHK_ST_ENABLED) {
563 s->check.state &= ~CHK_ST_PAUSED;
564 check->health = check->rise; /* start OK but check immediately */
565 }
566
567 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
568 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
569 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
570 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
571 if (s->proxy->last_change < now.tv_sec) // ignore negative times
572 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
573 s->proxy->last_change = now.tv_sec;
574 }
575
576 if (s->last_change < now.tv_sec) // ignore negative times
577 s->down_time += now.tv_sec - s->last_change;
578 s->last_change = now.tv_sec;
579
580 if (s->track && s->track->state == SRV_ST_STOPPING)
581 s->state = SRV_ST_STOPPING;
582 else {
583 s->state = SRV_ST_STARTING;
584 if (s->slowstart > 0)
585 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
586 else
587 s->state = SRV_ST_RUNNING;
588 }
589
590 server_recalc_eweight(s);
591
592 /* If the server is set with "on-marked-up shutdown-backup-sessions",
593 * and it's not a backup server and its effective weight is > 0,
594 * then it can accept new connections, so we shut down all sessions
595 * on all backup servers.
596 */
597 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
598 !(s->flags & SRV_F_BACKUP) && s->eweight)
599 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
600
601 /* check if we can handle some connections queued at the proxy. We
602 * will take as many as we can handle.
603 */
604 xferred = pendconn_grab_from_px(s);
605 }
606
607 if (mode & SRV_ADMF_FMAINT) {
608 chunk_printf(&trash,
609 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
610 s->flags & SRV_F_BACKUP ? "Backup " : "",
611 s->proxy->id, s->id,
612 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
613 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200614 }
615 else {
616 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200617 "%sServer %s/%s is %s/%s (leaving maintenance)",
618 s->flags & SRV_F_BACKUP ? "Backup " : "",
619 s->proxy->id, s->id,
620 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
621 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
622 srv_append_status(&trash, s, NULL, xferred, 0);
623 }
624 Warning("%s.\n", trash.str);
625 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
626 }
627 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
628 /* remaining in drain mode after removing one of its flags */
629
630 if (mode & SRV_ADMF_FDRAIN) {
631 chunk_printf(&trash,
632 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200633 s->flags & SRV_F_BACKUP ? "Backup " : "",
634 s->proxy->id, s->id);
635
636 if (s->track) /* normally it's mandatory here */
637 chunk_appendf(&trash, " via %s/%s",
638 s->track->proxy->id, s->track->id);
639 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200640 else {
641 chunk_printf(&trash,
642 "%sServer %s/%s remains in forced drain mode",
643 s->flags & SRV_F_BACKUP ? "Backup " : "",
644 s->proxy->id, s->id);
645 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200646 Warning("%s.\n", trash.str);
647 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200648 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200649 else if (mode & SRV_ADMF_DRAIN) {
650 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200651 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
652 if (s->proxy->last_change < now.tv_sec) // ignore negative times
653 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
654 s->proxy->last_change = now.tv_sec;
655 }
656
657 if (s->last_change < now.tv_sec) // ignore negative times
658 s->down_time += now.tv_sec - s->last_change;
659 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200660 server_recalc_eweight(s);
661
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200662 if (mode & SRV_ADMF_FDRAIN) {
663 chunk_printf(&trash,
664 "%sServer %s/%s is %s (leaving forced drain)",
665 s->flags & SRV_F_BACKUP ? "Backup " : "",
666 s->proxy->id, s->id,
667 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
668 }
669 else {
670 chunk_printf(&trash,
671 "%sServer %s/%s is %s (leaving drain)",
672 s->flags & SRV_F_BACKUP ? "Backup " : "",
673 s->proxy->id, s->id,
674 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
675 if (s->track) /* normally it's mandatory here */
676 chunk_appendf(&trash, " via %s/%s",
677 s->track->proxy->id, s->track->id);
678 }
679 Warning("%s.\n", trash.str);
680 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200681 }
682
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200683 /* stop going down if the equivalent flag is still present (forced or inherited) */
684 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
685 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
686 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200687
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200688 if (mode & SRV_ADMF_MAINT)
689 mode = SRV_ADMF_IMAINT;
690 else if (mode & SRV_ADMF_DRAIN)
691 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200692
693 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200694 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200695}
696
Willy Tarreaudff55432012-10-10 17:51:05 +0200697/* Note: must not be declared <const> as its list will be overwritten.
698 * Please take care of keeping this list alphabetically sorted, doing so helps
699 * all code contributors.
700 * Optional keywords are also declared with a NULL ->parse() function so that
701 * the config parser can report an appropriate error when a known keyword was
702 * not enabled.
703 */
704static struct srv_kw_list srv_kws = { "ALL", { }, {
705 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
706 { NULL, NULL, 0 },
707}};
708
709__attribute__((constructor))
710static void __listener_init(void)
711{
712 srv_register_keywords(&srv_kws);
713}
714
Willy Tarreau004e0452013-11-21 11:22:01 +0100715/* Recomputes the server's eweight based on its state, uweight, the current time,
716 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
717 * state is automatically disabled if the time is elapsed.
718 */
719void server_recalc_eweight(struct server *sv)
720{
721 struct proxy *px = sv->proxy;
722 unsigned w;
723
724 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
725 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200726 if (sv->state == SRV_ST_STARTING)
727 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100728 }
729
730 /* We must take care of not pushing the server to full throttle during slow starts.
731 * It must also start immediately, at least at the minimal step when leaving maintenance.
732 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200733 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100734 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
735 else
736 w = px->lbprm.wdiv;
737
738 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
739
740 /* now propagate the status change to any LB algorithms */
741 if (px->lbprm.update_server_eweight)
742 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200743 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100744 if (px->lbprm.set_server_status_up)
745 px->lbprm.set_server_status_up(sv);
746 }
747 else {
748 if (px->lbprm.set_server_status_down)
749 px->lbprm.set_server_status_down(sv);
750 }
751}
752
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900754 * Parses weight_str and configures sv accordingly.
755 * Returns NULL on success, error message string otherwise.
756 */
757const char *server_parse_weight_change_request(struct server *sv,
758 const char *weight_str)
759{
760 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900761 long int w;
762 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900763
764 px = sv->proxy;
765
766 /* if the weight is terminated with '%', it is set relative to
767 * the initial weight, otherwise it is absolute.
768 */
769 if (!*weight_str)
770 return "Require <weight> or <weight%>.\n";
771
Simon Hormanb796afa2013-02-12 10:45:53 +0900772 w = strtol(weight_str, &end, 10);
773 if (end == weight_str)
774 return "Empty weight string empty or preceded by garbage";
775 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900776 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900777 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900778 /* Avoid integer overflow */
779 if (w > 25600)
780 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900781 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900782 if (w > 256)
783 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900784 }
785 else if (w < 0 || w > 256)
786 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900787 else if (end[0] != '\0')
788 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900789
790 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
791 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
792
793 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100794 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900795
796 return NULL;
797}
798
Willy Tarreau272adea2014-03-31 10:39:59 +0200799static int init_check(struct check *check, int type, const char * file, int linenum)
800{
801 check->type = type;
802
803 /* Allocate buffer for requests... */
804 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
805 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
806 return ERR_ALERT | ERR_ABORT;
807 }
808 check->bi->size = global.tune.chksize;
809
810 /* Allocate buffer for responses... */
811 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
812 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
813 return ERR_ALERT | ERR_ABORT;
814 }
815 check->bo->size = global.tune.chksize;
816
817 /* Allocate buffer for partial results... */
818 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
819 Alert("parsing [%s:%d] : out of memory while allocating check connection.\n", file, linenum);
820 return ERR_ALERT | ERR_ABORT;
821 }
822
823 check->conn->t.sock.fd = -1; /* no agent in progress yet */
824
825 return 0;
826}
827
828int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
829{
830 struct server *newsrv = NULL;
831 const char *err;
832 char *errmsg = NULL;
833 int err_code = 0;
834 unsigned val;
835
836 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
837 int cur_arg;
838 short realport = 0;
839 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
840
841 if (!defsrv && curproxy == defproxy) {
842 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
843 err_code |= ERR_ALERT | ERR_FATAL;
844 goto out;
845 }
846 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
847 err_code |= ERR_ALERT | ERR_FATAL;
848
849 if (!*args[2]) {
850 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
851 file, linenum, args[0]);
852 err_code |= ERR_ALERT | ERR_FATAL;
853 goto out;
854 }
855
856 err = invalid_char(args[1]);
857 if (err && !defsrv) {
858 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
859 file, linenum, *err, args[1]);
860 err_code |= ERR_ALERT | ERR_FATAL;
861 goto out;
862 }
863
864 if (!defsrv) {
865 struct sockaddr_storage *sk;
866 int port1, port2;
867 struct protocol *proto;
868
869 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
870 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
871 err_code |= ERR_ALERT | ERR_ABORT;
872 goto out;
873 }
874
875 /* the servers are linked backwards first */
876 newsrv->next = curproxy->srv;
877 curproxy->srv = newsrv;
878 newsrv->proxy = curproxy;
879 newsrv->conf.file = strdup(file);
880 newsrv->conf.line = linenum;
881
882 newsrv->obj_type = OBJ_TYPE_SERVER;
883 LIST_INIT(&newsrv->actconns);
884 LIST_INIT(&newsrv->pendconns);
885 do_check = 0;
886 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200887 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200888 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200889 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200890 newsrv->last_change = now.tv_sec;
891 newsrv->id = strdup(args[1]);
892
893 /* several ways to check the port component :
894 * - IP => port=+0, relative (IPv4 only)
895 * - IP: => port=+0, relative
896 * - IP:N => port=N, absolute
897 * - IP:+N => port=+N, relative
898 * - IP:-N => port=-N, relative
899 */
900 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
901 if (!sk) {
902 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
903 err_code |= ERR_ALERT | ERR_FATAL;
904 goto out;
905 }
906
907 proto = protocol_by_family(sk->ss_family);
908 if (!proto || !proto->connect) {
909 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
910 file, linenum, args[0], args[1]);
911 err_code |= ERR_ALERT | ERR_FATAL;
912 goto out;
913 }
914
915 if (!port1 || !port2) {
916 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200917 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200918 }
919 else if (port1 != port2) {
920 /* port range */
921 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
922 file, linenum, args[0], args[1], args[2]);
923 err_code |= ERR_ALERT | ERR_FATAL;
924 goto out;
925 }
926 else {
927 /* used by checks */
928 realport = port1;
929 }
930
931 newsrv->addr = *sk;
932 newsrv->proto = newsrv->check_common.proto = protocol_by_family(newsrv->addr.ss_family);
Cyril Bonté1f96a872014-11-15 22:41:27 +0100933 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200934
935 if (!newsrv->proto) {
936 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
937 file, linenum, newsrv->addr.ss_family, args[2]);
938 err_code |= ERR_ALERT | ERR_FATAL;
939 goto out;
940 }
941
942 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
943 newsrv->check.port = curproxy->defsrv.check.port;
944 newsrv->check.inter = curproxy->defsrv.check.inter;
945 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
946 newsrv->check.downinter = curproxy->defsrv.check.downinter;
947 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
948 newsrv->agent.port = curproxy->defsrv.agent.port;
949 newsrv->agent.inter = curproxy->defsrv.agent.inter;
950 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
951 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
952 newsrv->maxqueue = curproxy->defsrv.maxqueue;
953 newsrv->minconn = curproxy->defsrv.minconn;
954 newsrv->maxconn = curproxy->defsrv.maxconn;
955 newsrv->slowstart = curproxy->defsrv.slowstart;
956 newsrv->onerror = curproxy->defsrv.onerror;
957 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
958 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
959 newsrv->consecutive_errors_limit
960 = curproxy->defsrv.consecutive_errors_limit;
961#ifdef OPENSSL
962 newsrv->use_ssl = curproxy->defsrv.use_ssl;
963#endif
964 newsrv->uweight = newsrv->iweight
965 = curproxy->defsrv.iweight;
966
967 newsrv->check.status = HCHK_STATUS_INI;
968 newsrv->check.rise = curproxy->defsrv.check.rise;
969 newsrv->check.fall = curproxy->defsrv.check.fall;
970 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
971 newsrv->check.server = newsrv;
972
973 newsrv->agent.status = HCHK_STATUS_INI;
974 newsrv->agent.rise = curproxy->defsrv.agent.rise;
975 newsrv->agent.fall = curproxy->defsrv.agent.fall;
976 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
977 newsrv->agent.server = newsrv;
978
979 cur_arg = 3;
980 } else {
981 newsrv = &curproxy->defsrv;
982 cur_arg = 1;
983 }
984
985 while (*args[cur_arg]) {
986 if (!strcmp(args[cur_arg], "agent-check")) {
987 global.maxsock++;
988 do_agent = 1;
989 cur_arg += 1;
990 } else if (!strcmp(args[cur_arg], "agent-inter")) {
991 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
992 if (err) {
993 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
994 file, linenum, *err, newsrv->id);
995 err_code |= ERR_ALERT | ERR_FATAL;
996 goto out;
997 }
998 if (val <= 0) {
999 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1000 file, linenum, val, args[cur_arg], newsrv->id);
1001 err_code |= ERR_ALERT | ERR_FATAL;
1002 goto out;
1003 }
1004 newsrv->agent.inter = val;
1005 cur_arg += 2;
1006 }
1007 else if (!strcmp(args[cur_arg], "agent-port")) {
1008 global.maxsock++;
1009 newsrv->agent.port = atol(args[cur_arg + 1]);
1010 cur_arg += 2;
1011 }
1012 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1013 newsrv->cookie = strdup(args[cur_arg + 1]);
1014 newsrv->cklen = strlen(args[cur_arg + 1]);
1015 cur_arg += 2;
1016 }
1017 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1018 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1019 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1020 cur_arg += 2;
1021 }
1022 else if (!strcmp(args[cur_arg], "rise")) {
1023 if (!*args[cur_arg + 1]) {
1024 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1025 file, linenum, args[cur_arg]);
1026 err_code |= ERR_ALERT | ERR_FATAL;
1027 goto out;
1028 }
1029
1030 newsrv->check.rise = atol(args[cur_arg + 1]);
1031 if (newsrv->check.rise <= 0) {
1032 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1033 file, linenum, args[cur_arg]);
1034 err_code |= ERR_ALERT | ERR_FATAL;
1035 goto out;
1036 }
1037
1038 if (newsrv->check.health)
1039 newsrv->check.health = newsrv->check.rise;
1040 cur_arg += 2;
1041 }
1042 else if (!strcmp(args[cur_arg], "fall")) {
1043 newsrv->check.fall = atol(args[cur_arg + 1]);
1044
1045 if (!*args[cur_arg + 1]) {
1046 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1047 file, linenum, args[cur_arg]);
1048 err_code |= ERR_ALERT | ERR_FATAL;
1049 goto out;
1050 }
1051
1052 if (newsrv->check.fall <= 0) {
1053 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1054 file, linenum, args[cur_arg]);
1055 err_code |= ERR_ALERT | ERR_FATAL;
1056 goto out;
1057 }
1058
1059 cur_arg += 2;
1060 }
1061 else if (!strcmp(args[cur_arg], "inter")) {
1062 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1063 if (err) {
1064 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1065 file, linenum, *err, newsrv->id);
1066 err_code |= ERR_ALERT | ERR_FATAL;
1067 goto out;
1068 }
1069 if (val <= 0) {
1070 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1071 file, linenum, val, args[cur_arg], newsrv->id);
1072 err_code |= ERR_ALERT | ERR_FATAL;
1073 goto out;
1074 }
1075 newsrv->check.inter = val;
1076 cur_arg += 2;
1077 }
1078 else if (!strcmp(args[cur_arg], "fastinter")) {
1079 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1080 if (err) {
1081 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1082 file, linenum, *err, newsrv->id);
1083 err_code |= ERR_ALERT | ERR_FATAL;
1084 goto out;
1085 }
1086 if (val <= 0) {
1087 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1088 file, linenum, val, args[cur_arg], newsrv->id);
1089 err_code |= ERR_ALERT | ERR_FATAL;
1090 goto out;
1091 }
1092 newsrv->check.fastinter = val;
1093 cur_arg += 2;
1094 }
1095 else if (!strcmp(args[cur_arg], "downinter")) {
1096 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1097 if (err) {
1098 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1099 file, linenum, *err, newsrv->id);
1100 err_code |= ERR_ALERT | ERR_FATAL;
1101 goto out;
1102 }
1103 if (val <= 0) {
1104 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1105 file, linenum, val, args[cur_arg], newsrv->id);
1106 err_code |= ERR_ALERT | ERR_FATAL;
1107 goto out;
1108 }
1109 newsrv->check.downinter = val;
1110 cur_arg += 2;
1111 }
1112 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1113 struct sockaddr_storage *sk;
1114 int port1, port2;
1115 struct protocol *proto;
1116
1117 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1118 if (!sk) {
1119 Alert("parsing [%s:%d] : '%s' : %s\n",
1120 file, linenum, args[cur_arg], errmsg);
1121 err_code |= ERR_ALERT | ERR_FATAL;
1122 goto out;
1123 }
1124
1125 proto = protocol_by_family(sk->ss_family);
1126 if (!proto || !proto->connect) {
1127 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1128 file, linenum, args[cur_arg], args[cur_arg + 1]);
1129 err_code |= ERR_ALERT | ERR_FATAL;
1130 goto out;
1131 }
1132
1133 if (port1 != port2) {
1134 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1135 file, linenum, args[cur_arg], args[cur_arg + 1]);
1136 err_code |= ERR_ALERT | ERR_FATAL;
1137 goto out;
1138 }
1139
1140 newsrv->check_common.addr = *sk;
Willy Tarreau640556c2014-05-09 23:38:15 +02001141 newsrv->check_common.proto = protocol_by_family(sk->ss_family);
Willy Tarreau272adea2014-03-31 10:39:59 +02001142 cur_arg += 2;
1143 }
1144 else if (!strcmp(args[cur_arg], "port")) {
1145 newsrv->check.port = atol(args[cur_arg + 1]);
1146 cur_arg += 2;
1147 }
1148 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001149 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001150 cur_arg ++;
1151 }
1152 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001153 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001154 cur_arg ++;
1155 }
1156 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001157 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001158 cur_arg ++;
1159 }
David Safb76832014-05-08 23:42:08 -04001160 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1161 newsrv->pp_opts |= SRV_PP_V2;
1162 cur_arg ++;
1163 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001164 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1165 newsrv->check.send_proxy = 1;
1166 cur_arg ++;
1167 }
1168 else if (!strcmp(args[cur_arg], "weight")) {
1169 int w;
1170 w = atol(args[cur_arg + 1]);
1171 if (w < 0 || w > SRV_UWGHT_MAX) {
1172 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1173 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1174 err_code |= ERR_ALERT | ERR_FATAL;
1175 goto out;
1176 }
1177 newsrv->uweight = newsrv->iweight = w;
1178 cur_arg += 2;
1179 }
1180 else if (!strcmp(args[cur_arg], "minconn")) {
1181 newsrv->minconn = atol(args[cur_arg + 1]);
1182 cur_arg += 2;
1183 }
1184 else if (!strcmp(args[cur_arg], "maxconn")) {
1185 newsrv->maxconn = atol(args[cur_arg + 1]);
1186 cur_arg += 2;
1187 }
1188 else if (!strcmp(args[cur_arg], "maxqueue")) {
1189 newsrv->maxqueue = atol(args[cur_arg + 1]);
1190 cur_arg += 2;
1191 }
1192 else if (!strcmp(args[cur_arg], "slowstart")) {
1193 /* slowstart is stored in seconds */
1194 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1195 if (err) {
1196 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1197 file, linenum, *err, newsrv->id);
1198 err_code |= ERR_ALERT | ERR_FATAL;
1199 goto out;
1200 }
1201 newsrv->slowstart = (val + 999) / 1000;
1202 cur_arg += 2;
1203 }
1204 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1205
1206 if (!*args[cur_arg + 1]) {
1207 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1208 file, linenum);
1209 err_code |= ERR_ALERT | ERR_FATAL;
1210 goto out;
1211 }
1212
1213 newsrv->trackit = strdup(args[cur_arg + 1]);
1214
1215 cur_arg += 2;
1216 }
1217 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1218 global.maxsock++;
1219 do_check = 1;
1220 cur_arg += 1;
1221 }
1222 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001223 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001224 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001225 newsrv->check.state |= CHK_ST_PAUSED;
1226 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001227 cur_arg += 1;
1228 }
1229 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1230 if (!strcmp(args[cur_arg + 1], "none"))
1231 newsrv->observe = HANA_OBS_NONE;
1232 else if (!strcmp(args[cur_arg + 1], "layer4"))
1233 newsrv->observe = HANA_OBS_LAYER4;
1234 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1235 if (curproxy->mode != PR_MODE_HTTP) {
1236 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1237 file, linenum, args[cur_arg + 1]);
1238 err_code |= ERR_ALERT;
1239 }
1240 newsrv->observe = HANA_OBS_LAYER7;
1241 }
1242 else {
1243 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1244 "'layer4', 'layer7' but got '%s'\n",
1245 file, linenum, args[cur_arg], args[cur_arg + 1]);
1246 err_code |= ERR_ALERT | ERR_FATAL;
1247 goto out;
1248 }
1249
1250 cur_arg += 2;
1251 }
1252 else if (!strcmp(args[cur_arg], "on-error")) {
1253 if (!strcmp(args[cur_arg + 1], "fastinter"))
1254 newsrv->onerror = HANA_ONERR_FASTINTER;
1255 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1256 newsrv->onerror = HANA_ONERR_FAILCHK;
1257 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1258 newsrv->onerror = HANA_ONERR_SUDDTH;
1259 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1260 newsrv->onerror = HANA_ONERR_MARKDWN;
1261 else {
1262 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1263 "'fail-check', 'sudden-death' or 'mark-down' 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], "on-marked-down")) {
1272 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1273 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1274 else {
1275 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1276 file, linenum, args[cur_arg], args[cur_arg + 1]);
1277 err_code |= ERR_ALERT | ERR_FATAL;
1278 goto out;
1279 }
1280
1281 cur_arg += 2;
1282 }
1283 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1284 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1285 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1286 else {
1287 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1288 file, linenum, args[cur_arg], args[cur_arg + 1]);
1289 err_code |= ERR_ALERT | ERR_FATAL;
1290 goto out;
1291 }
1292
1293 cur_arg += 2;
1294 }
1295 else if (!strcmp(args[cur_arg], "error-limit")) {
1296 if (!*args[cur_arg + 1]) {
1297 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1298 file, linenum, args[cur_arg]);
1299 err_code |= ERR_ALERT | ERR_FATAL;
1300 goto out;
1301 }
1302
1303 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1304
1305 if (newsrv->consecutive_errors_limit <= 0) {
1306 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1307 file, linenum, args[cur_arg]);
1308 err_code |= ERR_ALERT | ERR_FATAL;
1309 goto out;
1310 }
1311 cur_arg += 2;
1312 }
1313 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1314 int port_low, port_high;
1315 struct sockaddr_storage *sk;
1316 struct protocol *proto;
1317
1318 if (!*args[cur_arg + 1]) {
1319 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1320 file, linenum, "source", "usesrc", "interface");
1321 err_code |= ERR_ALERT | ERR_FATAL;
1322 goto out;
1323 }
1324
1325 newsrv->conn_src.opts |= CO_SRC_BIND;
1326 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1327 if (!sk) {
1328 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1329 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1330 err_code |= ERR_ALERT | ERR_FATAL;
1331 goto out;
1332 }
1333
1334 proto = protocol_by_family(sk->ss_family);
1335 if (!proto || !proto->connect) {
1336 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1337 file, linenum, args[cur_arg], args[cur_arg+1]);
1338 err_code |= ERR_ALERT | ERR_FATAL;
1339 goto out;
1340 }
1341
1342 newsrv->conn_src.source_addr = *sk;
1343
1344 if (port_low != port_high) {
1345 int i;
1346
1347 if (!port_low || !port_high) {
1348 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1349 file, linenum, args[cur_arg], args[cur_arg + 1]);
1350 err_code |= ERR_ALERT | ERR_FATAL;
1351 goto out;
1352 }
1353
1354 if (port_low <= 0 || port_low > 65535 ||
1355 port_high <= 0 || port_high > 65535 ||
1356 port_low > port_high) {
1357 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1358 file, linenum, port_low, port_high);
1359 err_code |= ERR_ALERT | ERR_FATAL;
1360 goto out;
1361 }
1362 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1363 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1364 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1365 }
1366
1367 cur_arg += 2;
1368 while (*(args[cur_arg])) {
1369 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1370#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1371#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001372 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001373 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1374 file, linenum, "usesrc", "source");
1375 err_code |= ERR_ALERT | ERR_FATAL;
1376 goto out;
1377 }
1378#endif
1379 if (!*args[cur_arg + 1]) {
1380 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1381 file, linenum, "usesrc");
1382 err_code |= ERR_ALERT | ERR_FATAL;
1383 goto out;
1384 }
1385 if (!strcmp(args[cur_arg + 1], "client")) {
1386 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1387 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1388 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1389 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1390 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1391 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1392 char *name, *end;
1393
1394 name = args[cur_arg+1] + 7;
1395 while (isspace(*name))
1396 name++;
1397
1398 end = name;
1399 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1400 end++;
1401
1402 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1403 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1404 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1405 newsrv->conn_src.bind_hdr_len = end - name;
1406 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1407 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1408 newsrv->conn_src.bind_hdr_occ = -1;
1409
1410 /* now look for an occurrence number */
1411 while (isspace(*end))
1412 end++;
1413 if (*end == ',') {
1414 end++;
1415 name = end;
1416 if (*end == '-')
1417 end++;
1418 while (isdigit((int)*end))
1419 end++;
1420 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1421 }
1422
1423 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1424 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1425 " occurrences values smaller than %d.\n",
1426 file, linenum, MAX_HDR_HISTORY);
1427 err_code |= ERR_ALERT | ERR_FATAL;
1428 goto out;
1429 }
1430 } else {
1431 struct sockaddr_storage *sk;
1432 int port1, port2;
1433
1434 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1435 if (!sk) {
1436 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1437 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1438 err_code |= ERR_ALERT | ERR_FATAL;
1439 goto out;
1440 }
1441
1442 proto = protocol_by_family(sk->ss_family);
1443 if (!proto || !proto->connect) {
1444 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1445 file, linenum, args[cur_arg], args[cur_arg+1]);
1446 err_code |= ERR_ALERT | ERR_FATAL;
1447 goto out;
1448 }
1449
1450 if (port1 != port2) {
1451 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1452 file, linenum, args[cur_arg], args[cur_arg + 1]);
1453 err_code |= ERR_ALERT | ERR_FATAL;
1454 goto out;
1455 }
1456 newsrv->conn_src.tproxy_addr = *sk;
1457 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1458 }
1459 global.last_checks |= LSTCHK_NETADM;
1460#if !defined(CONFIG_HAP_TRANSPARENT)
1461 global.last_checks |= LSTCHK_CTTPROXY;
1462#endif
1463 cur_arg += 2;
1464 continue;
1465#else /* no TPROXY support */
1466 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1467 file, linenum, "usesrc");
1468 err_code |= ERR_ALERT | ERR_FATAL;
1469 goto out;
1470#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1471 } /* "usesrc" */
1472
1473 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1474#ifdef SO_BINDTODEVICE
1475 if (!*args[cur_arg + 1]) {
1476 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1477 file, linenum, args[0]);
1478 err_code |= ERR_ALERT | ERR_FATAL;
1479 goto out;
1480 }
1481 free(newsrv->conn_src.iface_name);
1482 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1483 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1484 global.last_checks |= LSTCHK_NETADM;
1485#else
1486 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1487 file, linenum, args[0], args[cur_arg]);
1488 err_code |= ERR_ALERT | ERR_FATAL;
1489 goto out;
1490#endif
1491 cur_arg += 2;
1492 continue;
1493 }
1494 /* this keyword in not an option of "source" */
1495 break;
1496 } /* while */
1497 }
1498 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1499 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1500 file, linenum, "usesrc", "source");
1501 err_code |= ERR_ALERT | ERR_FATAL;
1502 goto out;
1503 }
1504 else {
1505 static int srv_dumped;
1506 struct srv_kw *kw;
1507 char *err;
1508
1509 kw = srv_find_kw(args[cur_arg]);
1510 if (kw) {
1511 char *err = NULL;
1512 int code;
1513
1514 if (!kw->parse) {
1515 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1516 file, linenum, args[0], args[1], args[cur_arg]);
1517 cur_arg += 1 + kw->skip ;
1518 err_code |= ERR_ALERT | ERR_FATAL;
1519 goto out;
1520 }
1521
1522 if (defsrv && !kw->default_ok) {
1523 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1524 file, linenum, args[0], args[1], args[cur_arg]);
1525 cur_arg += 1 + kw->skip ;
1526 err_code |= ERR_ALERT;
1527 continue;
1528 }
1529
1530 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1531 err_code |= code;
1532
1533 if (code) {
1534 if (err && *err) {
1535 indent_msg(&err, 2);
1536 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1537 }
1538 else
1539 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1540 file, linenum, args[0], args[1], args[cur_arg]);
1541 if (code & ERR_FATAL) {
1542 free(err);
1543 cur_arg += 1 + kw->skip;
1544 goto out;
1545 }
1546 }
1547 free(err);
1548 cur_arg += 1 + kw->skip;
1549 continue;
1550 }
1551
1552 err = NULL;
1553 if (!srv_dumped) {
1554 srv_dump_kws(&err);
1555 indent_msg(&err, 4);
1556 srv_dumped = 1;
1557 }
1558
1559 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1560 file, linenum, args[0], args[1], args[cur_arg],
1561 err ? " Registered keywords :" : "", err ? err : "");
1562 free(err);
1563
1564 err_code |= ERR_ALERT | ERR_FATAL;
1565 goto out;
1566 }
1567 }
1568
Willy Tarreau272adea2014-03-31 10:39:59 +02001569 if (do_check) {
1570 int ret;
1571
1572 if (newsrv->trackit) {
1573 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1574 file, linenum);
1575 err_code |= ERR_ALERT | ERR_FATAL;
1576 goto out;
1577 }
1578
1579 /* If neither a port nor an addr was specified and no check transport
1580 * layer is forced, then the transport layer used by the checks is the
1581 * same as for the production traffic. Otherwise we use raw_sock by
1582 * default, unless one is specified.
1583 */
1584 if (!newsrv->check.port && !is_addr(&newsrv->check_common.addr)) {
1585#ifdef USE_OPENSSL
1586 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1587#endif
David Safb76832014-05-08 23:42:08 -04001588 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001589 }
1590 /* try to get the port from check_core.addr if check.port not set */
1591 if (!newsrv->check.port)
1592 newsrv->check.port = get_host_port(&newsrv->check_common.addr);
1593
1594 if (!newsrv->check.port)
1595 newsrv->check.port = realport; /* by default */
1596
1597 if (!newsrv->check.port) {
1598 /* not yet valid, because no port was set on
1599 * the server either. We'll check if we have
1600 * a known port on the first listener.
1601 */
1602 struct listener *l;
1603
1604 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1605 newsrv->check.port = get_host_port(&l->addr);
1606 if (newsrv->check.port)
1607 break;
1608 }
1609 }
1610 /*
1611 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001612 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001613 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001614 if (!newsrv->check.port &&
1615 (is_inet_addr(&newsrv->check_common.addr) ||
1616 (!is_addr(&newsrv->check_common.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001617 struct tcpcheck_rule *n = NULL, *r = NULL;
1618 struct list *l;
1619
1620 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1621 if (!r) {
1622 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1623 file, linenum, newsrv->id);
1624 err_code |= ERR_ALERT | ERR_FATAL;
1625 goto out;
1626 }
1627 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1628 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",
1629 file, linenum, newsrv->id);
1630 err_code |= ERR_ALERT | ERR_FATAL;
1631 goto out;
1632 }
1633 else {
1634 /* scan the tcp-check ruleset to ensure a port has been configured */
1635 l = &newsrv->proxy->tcpcheck_rules;
1636 list_for_each_entry(n, l, list) {
1637 r = (struct tcpcheck_rule *)n->list.p;
1638 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 */