blob: b8fa1336d0cdd4332e145d63f3474a4fbcf5cb3b [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
220/* Puts server <s> into maintenance mode, and propagate that status down to all
221 * tracking servers. This does the same action as the CLI's "disable server x".
222 * A log is emitted for all servers that were not yet in maintenance mode.
223 * Health checks are disabled but not agent checks. The server is marked as
224 * being either forced into maintenance by having <mode> set to SRV_ADMF_FMAINT,
225 * or as inheriting the maintenance status by having <mode> set to
226 * SRV_ADMF_IMAINT. Nothing is done if neither flag is set.
227 */
228void srv_adm_set_maint(struct server *s, enum srv_admin mode)
229{
230 struct check *check = &s->check;
231 struct server *srv;
232 int xferred;
233
234 if (!mode)
235 return;
236
237 /* stop going down as soon as we meet a server already in the same state */
238 if (s->admin & mode)
239 return;
240
241 s->admin |= mode;
242
243 if (s->check.state & CHK_ST_ENABLED) {
244 s->check.state |= CHK_ST_PAUSED;
245 check->health = 0;
246 }
247
248 if (s->state == SRV_ST_STOPPED) { /* server was already down */
249 if (!(s->admin & ~mode & SRV_ADMF_MAINT)) {
250 chunk_printf(&trash,
251 "%sServer %s/%s was DOWN and now enters maintenance",
252 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
253
Willy Tarreaubda92272014-05-20 21:55:30 +0200254 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200255
256 Warning("%s.\n", trash.str);
257 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
258 }
259 }
260 else { /* server was still running */
261 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
262 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
263
264 check->health = 0; /* failure */
265 s->last_change = now.tv_sec;
266 s->state = SRV_ST_STOPPED;
267 if (s->proxy->lbprm.set_server_status_down)
268 s->proxy->lbprm.set_server_status_down(s);
269
270 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
271 srv_shutdown_sessions(s, SN_ERR_DOWN);
272
273 /* we might have sessions queued on this server and waiting for
274 * a connection. Those which are redispatchable will be queued
275 * to another server or to the proxy itself.
276 */
277 xferred = pendconn_redistribute(s);
278
279 chunk_printf(&trash,
280 "%sServer %s/%s is going DOWN for maintenance",
281 s->flags & SRV_F_BACKUP ? "Backup " : "",
282 s->proxy->id, s->id);
283
Willy Tarreaubda92272014-05-20 21:55:30 +0200284 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200285
286 Warning("%s.\n", trash.str);
287 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
288
289 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
290 set_backend_down(s->proxy);
291
292 s->counters.down_trans++;
293 }
294
295 for (srv = s->trackers; srv; srv = srv->tracknext)
296 srv_adm_set_maint(srv, SRV_ADMF_IMAINT);
297}
298
299/* Gets server <s> out of maintenance mode, and propagate that status down to
300 * all tracking servers. This does the same action as the CLI's "enable server x".
301 * A log is emitted for all servers that leave maintenance mode. Health checks
302 * are possibly enabled again. The server is marked as leaving forced maintenance
303 * when <mode> is set to SRV_ADMF_FMAINT, or as leaving inherited maintenance
304 * when <mode> set to SRV_ADMF_IMAINT. Nothing is done if neither flag is set.
305 */
306void srv_adm_set_ready(struct server *s, enum srv_admin mode)
307{
308 struct check *check = &s->check;
309 struct server *srv;
310 int xferred = -1;
311
312 if (!mode)
313 return;
314
315 /* stop going down as soon as we see the flag is not there anymore */
316 if (!(s->admin & mode))
317 return;
318
319 s->admin &= ~mode;
320
321 if (s->admin & SRV_ADMF_MAINT) {
322 /* remaining in maintenance mode, let's inform precisely about the
323 * situation.
324 */
325
326 if (s->admin & SRV_ADMF_FMAINT) {
327 chunk_printf(&trash,
328 "%sServer %s/%s remains in forced maintenance",
329 s->flags & SRV_F_BACKUP ? "Backup " : "",
330 s->proxy->id, s->id);
331 }
332 else {
333 chunk_printf(&trash,
334 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
335 s->flags & SRV_F_BACKUP ? "Backup " : "",
336 s->proxy->id, s->id);
337
338 if (s->track) /* normally it's mandatory here */
339 chunk_appendf(&trash, " via %s/%s",
340 s->track->proxy->id, s->track->id);
341 }
342
343 Warning("%s.\n", trash.str);
344 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
345 return;
346 }
347
348 /* OK here we're leaving maintenance, we have many things to check,
349 * because the server might possibly be coming back up depending on
350 * its state. In practice, leaving maintenance means that we should
351 * immediately turn to UP (more or less the slowstart) under the
352 * following conditions :
353 * - server is neither checked nor tracked
354 * - server tracks another server which is not checked
355 * - server tracks another server which is already up
356 * Which sums up as something simpler :
357 * "either the server's or the tracked server's checks are disabled or up".
358 * Otherwise we only re-enable health checks.
359 */
360
361 if (s->check.state & CHK_ST_ENABLED) {
362 s->check.state &= ~CHK_ST_PAUSED;
363 check->health = check->rise; /* start OK but check immediately */
364 }
365
Willy Tarreau32091232014-05-16 13:52:00 +0200366 srv = s;
367 while (srv->track)
368 srv = srv->track;
369
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200370 if ((!s->track &&
371 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
372 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) ||
373 (s->track &&
Willy Tarreau32091232014-05-16 13:52:00 +0200374 (!(srv->agent.state & CHK_ST_ENABLED) || (srv->agent.health >= srv->agent.rise)) &&
375 (!(srv->check.state & CHK_ST_ENABLED) || (srv->check.health >= srv->check.rise)))) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200376
377 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
378 if (s->proxy->last_change < now.tv_sec) // ignore negative times
379 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
380 s->proxy->last_change = now.tv_sec;
381 }
382
383 if (s->last_change < now.tv_sec) // ignore negative times
384 s->down_time += now.tv_sec - s->last_change;
385 s->last_change = now.tv_sec;
386
387 s->state = SRV_ST_STARTING;
388 if (s->slowstart > 0)
389 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
390 else
391 s->state = SRV_ST_RUNNING;
392
393 server_recalc_eweight(s);
394
395 /* If the server is set with "on-marked-up shutdown-backup-sessions",
396 * and it's not a backup server and its effective weight is > 0,
397 * then it can accept new connections, so we shut down all sessions
398 * on all backup servers.
399 */
400 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
401 !(s->flags & SRV_F_BACKUP) && s->eweight)
402 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
403
404 /* check if we can handle some connections queued at the proxy. We
405 * will take as many as we can handle.
406 */
407 xferred = pendconn_grab_from_px(s);
408 }
409
410 if (mode & SRV_ADMF_FMAINT) {
411 chunk_printf(&trash,
412 "%sServer %s/%s is %s (leaving forced maintenance)",
413 s->flags & SRV_F_BACKUP ? "Backup " : "",
414 s->proxy->id, s->id,
415 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
416 }
417 else {
418 chunk_printf(&trash,
419 "%sServer %s/%s is %s (leaving maintenance)",
420 s->flags & SRV_F_BACKUP ? "Backup " : "",
421 s->proxy->id, s->id,
422 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
Willy Tarreaubda92272014-05-20 21:55:30 +0200423 srv_append_status(&trash, s, NULL, xferred, 0);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200424 }
425
426 Warning("%s.\n", trash.str);
427 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
428
429 for (srv = s->trackers; srv; srv = srv->tracknext)
430 srv_adm_set_ready(srv, SRV_ADMF_IMAINT);
431}
432
Willy Tarreaudff55432012-10-10 17:51:05 +0200433/* Note: must not be declared <const> as its list will be overwritten.
434 * Please take care of keeping this list alphabetically sorted, doing so helps
435 * all code contributors.
436 * Optional keywords are also declared with a NULL ->parse() function so that
437 * the config parser can report an appropriate error when a known keyword was
438 * not enabled.
439 */
440static struct srv_kw_list srv_kws = { "ALL", { }, {
441 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
442 { NULL, NULL, 0 },
443}};
444
445__attribute__((constructor))
446static void __listener_init(void)
447{
448 srv_register_keywords(&srv_kws);
449}
450
Willy Tarreau004e0452013-11-21 11:22:01 +0100451/* Recomputes the server's eweight based on its state, uweight, the current time,
452 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
453 * state is automatically disabled if the time is elapsed.
454 */
455void server_recalc_eweight(struct server *sv)
456{
457 struct proxy *px = sv->proxy;
458 unsigned w;
459
460 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
461 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200462 if (sv->state == SRV_ST_STARTING)
463 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100464 }
465
466 /* We must take care of not pushing the server to full throttle during slow starts.
467 * It must also start immediately, at least at the minimal step when leaving maintenance.
468 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200469 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100470 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
471 else
472 w = px->lbprm.wdiv;
473
474 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
475
476 /* now propagate the status change to any LB algorithms */
477 if (px->lbprm.update_server_eweight)
478 px->lbprm.update_server_eweight(sv);
479 else if (sv->eweight) {
480 if (px->lbprm.set_server_status_up)
481 px->lbprm.set_server_status_up(sv);
482 }
483 else {
484 if (px->lbprm.set_server_status_down)
485 px->lbprm.set_server_status_down(sv);
486 }
487}
488
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900490 * Parses weight_str and configures sv accordingly.
491 * Returns NULL on success, error message string otherwise.
492 */
493const char *server_parse_weight_change_request(struct server *sv,
494 const char *weight_str)
495{
496 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900497 long int w;
498 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900499
500 px = sv->proxy;
501
502 /* if the weight is terminated with '%', it is set relative to
503 * the initial weight, otherwise it is absolute.
504 */
505 if (!*weight_str)
506 return "Require <weight> or <weight%>.\n";
507
Simon Hormanb796afa2013-02-12 10:45:53 +0900508 w = strtol(weight_str, &end, 10);
509 if (end == weight_str)
510 return "Empty weight string empty or preceded by garbage";
511 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900512 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900513 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900514 /* Avoid integer overflow */
515 if (w > 25600)
516 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900517 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900518 if (w > 256)
519 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900520 }
521 else if (w < 0 || w > 256)
522 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900523 else if (end[0] != '\0')
524 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900525
526 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
527 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
528
529 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100530 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900531
532 return NULL;
533}
534
Willy Tarreau272adea2014-03-31 10:39:59 +0200535static int init_check(struct check *check, int type, const char * file, int linenum)
536{
537 check->type = type;
538
539 /* Allocate buffer for requests... */
540 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
541 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
542 return ERR_ALERT | ERR_ABORT;
543 }
544 check->bi->size = global.tune.chksize;
545
546 /* Allocate buffer for responses... */
547 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
548 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
549 return ERR_ALERT | ERR_ABORT;
550 }
551 check->bo->size = global.tune.chksize;
552
553 /* Allocate buffer for partial results... */
554 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
555 Alert("parsing [%s:%d] : out of memory while allocating check connection.\n", file, linenum);
556 return ERR_ALERT | ERR_ABORT;
557 }
558
559 check->conn->t.sock.fd = -1; /* no agent in progress yet */
560
561 return 0;
562}
563
564int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
565{
566 struct server *newsrv = NULL;
567 const char *err;
568 char *errmsg = NULL;
569 int err_code = 0;
570 unsigned val;
571
572 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
573 int cur_arg;
574 short realport = 0;
575 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
576
577 if (!defsrv && curproxy == defproxy) {
578 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
579 err_code |= ERR_ALERT | ERR_FATAL;
580 goto out;
581 }
582 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
583 err_code |= ERR_ALERT | ERR_FATAL;
584
585 if (!*args[2]) {
586 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
587 file, linenum, args[0]);
588 err_code |= ERR_ALERT | ERR_FATAL;
589 goto out;
590 }
591
592 err = invalid_char(args[1]);
593 if (err && !defsrv) {
594 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
595 file, linenum, *err, args[1]);
596 err_code |= ERR_ALERT | ERR_FATAL;
597 goto out;
598 }
599
600 if (!defsrv) {
601 struct sockaddr_storage *sk;
602 int port1, port2;
603 struct protocol *proto;
604
605 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
606 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
607 err_code |= ERR_ALERT | ERR_ABORT;
608 goto out;
609 }
610
611 /* the servers are linked backwards first */
612 newsrv->next = curproxy->srv;
613 curproxy->srv = newsrv;
614 newsrv->proxy = curproxy;
615 newsrv->conf.file = strdup(file);
616 newsrv->conf.line = linenum;
617
618 newsrv->obj_type = OBJ_TYPE_SERVER;
619 LIST_INIT(&newsrv->actconns);
620 LIST_INIT(&newsrv->pendconns);
621 do_check = 0;
622 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200623 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200624 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200625 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200626 newsrv->last_change = now.tv_sec;
627 newsrv->id = strdup(args[1]);
628
629 /* several ways to check the port component :
630 * - IP => port=+0, relative (IPv4 only)
631 * - IP: => port=+0, relative
632 * - IP:N => port=N, absolute
633 * - IP:+N => port=+N, relative
634 * - IP:-N => port=-N, relative
635 */
636 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
637 if (!sk) {
638 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
639 err_code |= ERR_ALERT | ERR_FATAL;
640 goto out;
641 }
642
643 proto = protocol_by_family(sk->ss_family);
644 if (!proto || !proto->connect) {
645 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
646 file, linenum, args[0], args[1]);
647 err_code |= ERR_ALERT | ERR_FATAL;
648 goto out;
649 }
650
651 if (!port1 || !port2) {
652 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200653 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200654 }
655 else if (port1 != port2) {
656 /* port range */
657 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
658 file, linenum, args[0], args[1], args[2]);
659 err_code |= ERR_ALERT | ERR_FATAL;
660 goto out;
661 }
662 else {
663 /* used by checks */
664 realport = port1;
665 }
666
667 newsrv->addr = *sk;
668 newsrv->proto = newsrv->check_common.proto = protocol_by_family(newsrv->addr.ss_family);
669 newsrv->xprt = newsrv->check_common.xprt = &raw_sock;
670
671 if (!newsrv->proto) {
672 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
673 file, linenum, newsrv->addr.ss_family, args[2]);
674 err_code |= ERR_ALERT | ERR_FATAL;
675 goto out;
676 }
677
678 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
679 newsrv->check.port = curproxy->defsrv.check.port;
680 newsrv->check.inter = curproxy->defsrv.check.inter;
681 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
682 newsrv->check.downinter = curproxy->defsrv.check.downinter;
683 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
684 newsrv->agent.port = curproxy->defsrv.agent.port;
685 newsrv->agent.inter = curproxy->defsrv.agent.inter;
686 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
687 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
688 newsrv->maxqueue = curproxy->defsrv.maxqueue;
689 newsrv->minconn = curproxy->defsrv.minconn;
690 newsrv->maxconn = curproxy->defsrv.maxconn;
691 newsrv->slowstart = curproxy->defsrv.slowstart;
692 newsrv->onerror = curproxy->defsrv.onerror;
693 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
694 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
695 newsrv->consecutive_errors_limit
696 = curproxy->defsrv.consecutive_errors_limit;
697#ifdef OPENSSL
698 newsrv->use_ssl = curproxy->defsrv.use_ssl;
699#endif
700 newsrv->uweight = newsrv->iweight
701 = curproxy->defsrv.iweight;
702
703 newsrv->check.status = HCHK_STATUS_INI;
704 newsrv->check.rise = curproxy->defsrv.check.rise;
705 newsrv->check.fall = curproxy->defsrv.check.fall;
706 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
707 newsrv->check.server = newsrv;
708
709 newsrv->agent.status = HCHK_STATUS_INI;
710 newsrv->agent.rise = curproxy->defsrv.agent.rise;
711 newsrv->agent.fall = curproxy->defsrv.agent.fall;
712 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
713 newsrv->agent.server = newsrv;
714
715 cur_arg = 3;
716 } else {
717 newsrv = &curproxy->defsrv;
718 cur_arg = 1;
719 }
720
721 while (*args[cur_arg]) {
722 if (!strcmp(args[cur_arg], "agent-check")) {
723 global.maxsock++;
724 do_agent = 1;
725 cur_arg += 1;
726 } else if (!strcmp(args[cur_arg], "agent-inter")) {
727 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
728 if (err) {
729 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
730 file, linenum, *err, newsrv->id);
731 err_code |= ERR_ALERT | ERR_FATAL;
732 goto out;
733 }
734 if (val <= 0) {
735 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
736 file, linenum, val, args[cur_arg], newsrv->id);
737 err_code |= ERR_ALERT | ERR_FATAL;
738 goto out;
739 }
740 newsrv->agent.inter = val;
741 cur_arg += 2;
742 }
743 else if (!strcmp(args[cur_arg], "agent-port")) {
744 global.maxsock++;
745 newsrv->agent.port = atol(args[cur_arg + 1]);
746 cur_arg += 2;
747 }
748 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
749 newsrv->cookie = strdup(args[cur_arg + 1]);
750 newsrv->cklen = strlen(args[cur_arg + 1]);
751 cur_arg += 2;
752 }
753 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
754 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
755 newsrv->rdr_len = strlen(args[cur_arg + 1]);
756 cur_arg += 2;
757 }
758 else if (!strcmp(args[cur_arg], "rise")) {
759 if (!*args[cur_arg + 1]) {
760 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
761 file, linenum, args[cur_arg]);
762 err_code |= ERR_ALERT | ERR_FATAL;
763 goto out;
764 }
765
766 newsrv->check.rise = atol(args[cur_arg + 1]);
767 if (newsrv->check.rise <= 0) {
768 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
769 file, linenum, args[cur_arg]);
770 err_code |= ERR_ALERT | ERR_FATAL;
771 goto out;
772 }
773
774 if (newsrv->check.health)
775 newsrv->check.health = newsrv->check.rise;
776 cur_arg += 2;
777 }
778 else if (!strcmp(args[cur_arg], "fall")) {
779 newsrv->check.fall = atol(args[cur_arg + 1]);
780
781 if (!*args[cur_arg + 1]) {
782 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
783 file, linenum, args[cur_arg]);
784 err_code |= ERR_ALERT | ERR_FATAL;
785 goto out;
786 }
787
788 if (newsrv->check.fall <= 0) {
789 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
790 file, linenum, args[cur_arg]);
791 err_code |= ERR_ALERT | ERR_FATAL;
792 goto out;
793 }
794
795 cur_arg += 2;
796 }
797 else if (!strcmp(args[cur_arg], "inter")) {
798 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
799 if (err) {
800 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
801 file, linenum, *err, newsrv->id);
802 err_code |= ERR_ALERT | ERR_FATAL;
803 goto out;
804 }
805 if (val <= 0) {
806 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
807 file, linenum, val, args[cur_arg], newsrv->id);
808 err_code |= ERR_ALERT | ERR_FATAL;
809 goto out;
810 }
811 newsrv->check.inter = val;
812 cur_arg += 2;
813 }
814 else if (!strcmp(args[cur_arg], "fastinter")) {
815 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
816 if (err) {
817 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
818 file, linenum, *err, newsrv->id);
819 err_code |= ERR_ALERT | ERR_FATAL;
820 goto out;
821 }
822 if (val <= 0) {
823 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
824 file, linenum, val, args[cur_arg], newsrv->id);
825 err_code |= ERR_ALERT | ERR_FATAL;
826 goto out;
827 }
828 newsrv->check.fastinter = val;
829 cur_arg += 2;
830 }
831 else if (!strcmp(args[cur_arg], "downinter")) {
832 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
833 if (err) {
834 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
835 file, linenum, *err, newsrv->id);
836 err_code |= ERR_ALERT | ERR_FATAL;
837 goto out;
838 }
839 if (val <= 0) {
840 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
841 file, linenum, val, args[cur_arg], newsrv->id);
842 err_code |= ERR_ALERT | ERR_FATAL;
843 goto out;
844 }
845 newsrv->check.downinter = val;
846 cur_arg += 2;
847 }
848 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
849 struct sockaddr_storage *sk;
850 int port1, port2;
851 struct protocol *proto;
852
853 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
854 if (!sk) {
855 Alert("parsing [%s:%d] : '%s' : %s\n",
856 file, linenum, args[cur_arg], errmsg);
857 err_code |= ERR_ALERT | ERR_FATAL;
858 goto out;
859 }
860
861 proto = protocol_by_family(sk->ss_family);
862 if (!proto || !proto->connect) {
863 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
864 file, linenum, args[cur_arg], args[cur_arg + 1]);
865 err_code |= ERR_ALERT | ERR_FATAL;
866 goto out;
867 }
868
869 if (port1 != port2) {
870 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
871 file, linenum, args[cur_arg], args[cur_arg + 1]);
872 err_code |= ERR_ALERT | ERR_FATAL;
873 goto out;
874 }
875
876 newsrv->check_common.addr = *sk;
Willy Tarreau640556c2014-05-09 23:38:15 +0200877 newsrv->check_common.proto = protocol_by_family(sk->ss_family);
Willy Tarreau272adea2014-03-31 10:39:59 +0200878 cur_arg += 2;
879 }
880 else if (!strcmp(args[cur_arg], "port")) {
881 newsrv->check.port = atol(args[cur_arg + 1]);
882 cur_arg += 2;
883 }
884 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +0200885 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +0200886 cur_arg ++;
887 }
888 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +0200889 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +0200890 cur_arg ++;
891 }
892 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -0400893 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +0200894 cur_arg ++;
895 }
David Safb76832014-05-08 23:42:08 -0400896 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
897 newsrv->pp_opts |= SRV_PP_V2;
898 cur_arg ++;
899 }
Willy Tarreau272adea2014-03-31 10:39:59 +0200900 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
901 newsrv->check.send_proxy = 1;
902 cur_arg ++;
903 }
904 else if (!strcmp(args[cur_arg], "weight")) {
905 int w;
906 w = atol(args[cur_arg + 1]);
907 if (w < 0 || w > SRV_UWGHT_MAX) {
908 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
909 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
910 err_code |= ERR_ALERT | ERR_FATAL;
911 goto out;
912 }
913 newsrv->uweight = newsrv->iweight = w;
914 cur_arg += 2;
915 }
916 else if (!strcmp(args[cur_arg], "minconn")) {
917 newsrv->minconn = atol(args[cur_arg + 1]);
918 cur_arg += 2;
919 }
920 else if (!strcmp(args[cur_arg], "maxconn")) {
921 newsrv->maxconn = atol(args[cur_arg + 1]);
922 cur_arg += 2;
923 }
924 else if (!strcmp(args[cur_arg], "maxqueue")) {
925 newsrv->maxqueue = atol(args[cur_arg + 1]);
926 cur_arg += 2;
927 }
928 else if (!strcmp(args[cur_arg], "slowstart")) {
929 /* slowstart is stored in seconds */
930 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
931 if (err) {
932 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
933 file, linenum, *err, newsrv->id);
934 err_code |= ERR_ALERT | ERR_FATAL;
935 goto out;
936 }
937 newsrv->slowstart = (val + 999) / 1000;
938 cur_arg += 2;
939 }
940 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
941
942 if (!*args[cur_arg + 1]) {
943 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
944 file, linenum);
945 err_code |= ERR_ALERT | ERR_FATAL;
946 goto out;
947 }
948
949 newsrv->trackit = strdup(args[cur_arg + 1]);
950
951 cur_arg += 2;
952 }
953 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
954 global.maxsock++;
955 do_check = 1;
956 cur_arg += 1;
957 }
958 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +0200959 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +0200960 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +0200961 newsrv->check.state |= CHK_ST_PAUSED;
962 newsrv->check.health = 0;
963 newsrv->agent.health = 0;
964 cur_arg += 1;
965 }
966 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
967 if (!strcmp(args[cur_arg + 1], "none"))
968 newsrv->observe = HANA_OBS_NONE;
969 else if (!strcmp(args[cur_arg + 1], "layer4"))
970 newsrv->observe = HANA_OBS_LAYER4;
971 else if (!strcmp(args[cur_arg + 1], "layer7")) {
972 if (curproxy->mode != PR_MODE_HTTP) {
973 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
974 file, linenum, args[cur_arg + 1]);
975 err_code |= ERR_ALERT;
976 }
977 newsrv->observe = HANA_OBS_LAYER7;
978 }
979 else {
980 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
981 "'layer4', 'layer7' but got '%s'\n",
982 file, linenum, args[cur_arg], args[cur_arg + 1]);
983 err_code |= ERR_ALERT | ERR_FATAL;
984 goto out;
985 }
986
987 cur_arg += 2;
988 }
989 else if (!strcmp(args[cur_arg], "on-error")) {
990 if (!strcmp(args[cur_arg + 1], "fastinter"))
991 newsrv->onerror = HANA_ONERR_FASTINTER;
992 else if (!strcmp(args[cur_arg + 1], "fail-check"))
993 newsrv->onerror = HANA_ONERR_FAILCHK;
994 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
995 newsrv->onerror = HANA_ONERR_SUDDTH;
996 else if (!strcmp(args[cur_arg + 1], "mark-down"))
997 newsrv->onerror = HANA_ONERR_MARKDWN;
998 else {
999 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1000 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1001 file, linenum, args[cur_arg], args[cur_arg + 1]);
1002 err_code |= ERR_ALERT | ERR_FATAL;
1003 goto out;
1004 }
1005
1006 cur_arg += 2;
1007 }
1008 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1009 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1010 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1011 else {
1012 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1013 file, linenum, args[cur_arg], args[cur_arg + 1]);
1014 err_code |= ERR_ALERT | ERR_FATAL;
1015 goto out;
1016 }
1017
1018 cur_arg += 2;
1019 }
1020 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1021 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1022 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1023 else {
1024 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1025 file, linenum, args[cur_arg], args[cur_arg + 1]);
1026 err_code |= ERR_ALERT | ERR_FATAL;
1027 goto out;
1028 }
1029
1030 cur_arg += 2;
1031 }
1032 else if (!strcmp(args[cur_arg], "error-limit")) {
1033 if (!*args[cur_arg + 1]) {
1034 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1035 file, linenum, args[cur_arg]);
1036 err_code |= ERR_ALERT | ERR_FATAL;
1037 goto out;
1038 }
1039
1040 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1041
1042 if (newsrv->consecutive_errors_limit <= 0) {
1043 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1044 file, linenum, args[cur_arg]);
1045 err_code |= ERR_ALERT | ERR_FATAL;
1046 goto out;
1047 }
1048 cur_arg += 2;
1049 }
1050 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1051 int port_low, port_high;
1052 struct sockaddr_storage *sk;
1053 struct protocol *proto;
1054
1055 if (!*args[cur_arg + 1]) {
1056 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1057 file, linenum, "source", "usesrc", "interface");
1058 err_code |= ERR_ALERT | ERR_FATAL;
1059 goto out;
1060 }
1061
1062 newsrv->conn_src.opts |= CO_SRC_BIND;
1063 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1064 if (!sk) {
1065 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1066 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1067 err_code |= ERR_ALERT | ERR_FATAL;
1068 goto out;
1069 }
1070
1071 proto = protocol_by_family(sk->ss_family);
1072 if (!proto || !proto->connect) {
1073 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1074 file, linenum, args[cur_arg], args[cur_arg+1]);
1075 err_code |= ERR_ALERT | ERR_FATAL;
1076 goto out;
1077 }
1078
1079 newsrv->conn_src.source_addr = *sk;
1080
1081 if (port_low != port_high) {
1082 int i;
1083
1084 if (!port_low || !port_high) {
1085 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1086 file, linenum, args[cur_arg], args[cur_arg + 1]);
1087 err_code |= ERR_ALERT | ERR_FATAL;
1088 goto out;
1089 }
1090
1091 if (port_low <= 0 || port_low > 65535 ||
1092 port_high <= 0 || port_high > 65535 ||
1093 port_low > port_high) {
1094 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1095 file, linenum, port_low, port_high);
1096 err_code |= ERR_ALERT | ERR_FATAL;
1097 goto out;
1098 }
1099 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1100 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1101 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1102 }
1103
1104 cur_arg += 2;
1105 while (*(args[cur_arg])) {
1106 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1107#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1108#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001109 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001110 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1111 file, linenum, "usesrc", "source");
1112 err_code |= ERR_ALERT | ERR_FATAL;
1113 goto out;
1114 }
1115#endif
1116 if (!*args[cur_arg + 1]) {
1117 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1118 file, linenum, "usesrc");
1119 err_code |= ERR_ALERT | ERR_FATAL;
1120 goto out;
1121 }
1122 if (!strcmp(args[cur_arg + 1], "client")) {
1123 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1124 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1125 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1126 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1127 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1128 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1129 char *name, *end;
1130
1131 name = args[cur_arg+1] + 7;
1132 while (isspace(*name))
1133 name++;
1134
1135 end = name;
1136 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1137 end++;
1138
1139 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1140 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1141 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1142 newsrv->conn_src.bind_hdr_len = end - name;
1143 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1144 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1145 newsrv->conn_src.bind_hdr_occ = -1;
1146
1147 /* now look for an occurrence number */
1148 while (isspace(*end))
1149 end++;
1150 if (*end == ',') {
1151 end++;
1152 name = end;
1153 if (*end == '-')
1154 end++;
1155 while (isdigit((int)*end))
1156 end++;
1157 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1158 }
1159
1160 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1161 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1162 " occurrences values smaller than %d.\n",
1163 file, linenum, MAX_HDR_HISTORY);
1164 err_code |= ERR_ALERT | ERR_FATAL;
1165 goto out;
1166 }
1167 } else {
1168 struct sockaddr_storage *sk;
1169 int port1, port2;
1170
1171 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1172 if (!sk) {
1173 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1174 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1175 err_code |= ERR_ALERT | ERR_FATAL;
1176 goto out;
1177 }
1178
1179 proto = protocol_by_family(sk->ss_family);
1180 if (!proto || !proto->connect) {
1181 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1182 file, linenum, args[cur_arg], args[cur_arg+1]);
1183 err_code |= ERR_ALERT | ERR_FATAL;
1184 goto out;
1185 }
1186
1187 if (port1 != port2) {
1188 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1189 file, linenum, args[cur_arg], args[cur_arg + 1]);
1190 err_code |= ERR_ALERT | ERR_FATAL;
1191 goto out;
1192 }
1193 newsrv->conn_src.tproxy_addr = *sk;
1194 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1195 }
1196 global.last_checks |= LSTCHK_NETADM;
1197#if !defined(CONFIG_HAP_TRANSPARENT)
1198 global.last_checks |= LSTCHK_CTTPROXY;
1199#endif
1200 cur_arg += 2;
1201 continue;
1202#else /* no TPROXY support */
1203 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1204 file, linenum, "usesrc");
1205 err_code |= ERR_ALERT | ERR_FATAL;
1206 goto out;
1207#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1208 } /* "usesrc" */
1209
1210 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1211#ifdef SO_BINDTODEVICE
1212 if (!*args[cur_arg + 1]) {
1213 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1214 file, linenum, args[0]);
1215 err_code |= ERR_ALERT | ERR_FATAL;
1216 goto out;
1217 }
1218 free(newsrv->conn_src.iface_name);
1219 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1220 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1221 global.last_checks |= LSTCHK_NETADM;
1222#else
1223 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1224 file, linenum, args[0], args[cur_arg]);
1225 err_code |= ERR_ALERT | ERR_FATAL;
1226 goto out;
1227#endif
1228 cur_arg += 2;
1229 continue;
1230 }
1231 /* this keyword in not an option of "source" */
1232 break;
1233 } /* while */
1234 }
1235 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1236 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1237 file, linenum, "usesrc", "source");
1238 err_code |= ERR_ALERT | ERR_FATAL;
1239 goto out;
1240 }
1241 else {
1242 static int srv_dumped;
1243 struct srv_kw *kw;
1244 char *err;
1245
1246 kw = srv_find_kw(args[cur_arg]);
1247 if (kw) {
1248 char *err = NULL;
1249 int code;
1250
1251 if (!kw->parse) {
1252 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1253 file, linenum, args[0], args[1], args[cur_arg]);
1254 cur_arg += 1 + kw->skip ;
1255 err_code |= ERR_ALERT | ERR_FATAL;
1256 goto out;
1257 }
1258
1259 if (defsrv && !kw->default_ok) {
1260 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1261 file, linenum, args[0], args[1], args[cur_arg]);
1262 cur_arg += 1 + kw->skip ;
1263 err_code |= ERR_ALERT;
1264 continue;
1265 }
1266
1267 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1268 err_code |= code;
1269
1270 if (code) {
1271 if (err && *err) {
1272 indent_msg(&err, 2);
1273 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1274 }
1275 else
1276 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1277 file, linenum, args[0], args[1], args[cur_arg]);
1278 if (code & ERR_FATAL) {
1279 free(err);
1280 cur_arg += 1 + kw->skip;
1281 goto out;
1282 }
1283 }
1284 free(err);
1285 cur_arg += 1 + kw->skip;
1286 continue;
1287 }
1288
1289 err = NULL;
1290 if (!srv_dumped) {
1291 srv_dump_kws(&err);
1292 indent_msg(&err, 4);
1293 srv_dumped = 1;
1294 }
1295
1296 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1297 file, linenum, args[0], args[1], args[cur_arg],
1298 err ? " Registered keywords :" : "", err ? err : "");
1299 free(err);
1300
1301 err_code |= ERR_ALERT | ERR_FATAL;
1302 goto out;
1303 }
1304 }
1305
Willy Tarreau272adea2014-03-31 10:39:59 +02001306 if (do_check) {
1307 int ret;
1308
1309 if (newsrv->trackit) {
1310 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1311 file, linenum);
1312 err_code |= ERR_ALERT | ERR_FATAL;
1313 goto out;
1314 }
1315
1316 /* If neither a port nor an addr was specified and no check transport
1317 * layer is forced, then the transport layer used by the checks is the
1318 * same as for the production traffic. Otherwise we use raw_sock by
1319 * default, unless one is specified.
1320 */
1321 if (!newsrv->check.port && !is_addr(&newsrv->check_common.addr)) {
1322#ifdef USE_OPENSSL
1323 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1324#endif
David Safb76832014-05-08 23:42:08 -04001325 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001326 }
1327 /* try to get the port from check_core.addr if check.port not set */
1328 if (!newsrv->check.port)
1329 newsrv->check.port = get_host_port(&newsrv->check_common.addr);
1330
1331 if (!newsrv->check.port)
1332 newsrv->check.port = realport; /* by default */
1333
1334 if (!newsrv->check.port) {
1335 /* not yet valid, because no port was set on
1336 * the server either. We'll check if we have
1337 * a known port on the first listener.
1338 */
1339 struct listener *l;
1340
1341 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1342 newsrv->check.port = get_host_port(&l->addr);
1343 if (newsrv->check.port)
1344 break;
1345 }
1346 }
1347 /*
1348 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001349 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001350 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001351 if (!newsrv->check.port &&
1352 (is_inet_addr(&newsrv->check_common.addr) ||
1353 (!is_addr(&newsrv->check_common.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001354 struct tcpcheck_rule *n = NULL, *r = NULL;
1355 struct list *l;
1356
1357 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1358 if (!r) {
1359 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1360 file, linenum, newsrv->id);
1361 err_code |= ERR_ALERT | ERR_FATAL;
1362 goto out;
1363 }
1364 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1365 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",
1366 file, linenum, newsrv->id);
1367 err_code |= ERR_ALERT | ERR_FATAL;
1368 goto out;
1369 }
1370 else {
1371 /* scan the tcp-check ruleset to ensure a port has been configured */
1372 l = &newsrv->proxy->tcpcheck_rules;
1373 list_for_each_entry(n, l, list) {
1374 r = (struct tcpcheck_rule *)n->list.p;
1375 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1376 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",
1377 file, linenum, newsrv->id);
1378 err_code |= ERR_ALERT | ERR_FATAL;
1379 goto out;
1380 }
1381 }
1382 }
1383 }
1384
1385 /* note: check type will be set during the config review phase */
1386 ret = init_check(&newsrv->check, 0, file, linenum);
1387 if (ret) {
1388 err_code |= ret;
1389 goto out;
1390 }
1391
1392 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1393 }
1394
1395 if (do_agent) {
1396 int ret;
1397
1398 if (!newsrv->agent.port) {
1399 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1400 file, linenum, newsrv->id);
1401 err_code |= ERR_ALERT | ERR_FATAL;
1402 goto out;
1403 }
1404
1405 if (!newsrv->agent.inter)
1406 newsrv->agent.inter = newsrv->check.inter;
1407
1408 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK, file, linenum);
1409 if (ret) {
1410 err_code |= ret;
1411 goto out;
1412 }
1413
1414 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1415 }
1416
1417 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001418 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001419 curproxy->srv_bck++;
1420 else
1421 curproxy->srv_act++;
1422
Willy Tarreauc5150da2014-05-13 19:27:31 +02001423 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001424 }
1425 }
1426 return 0;
1427
1428 out:
1429 free(errmsg);
1430 return err_code;
1431}
1432
Simon Horman7d09b9a2013-02-12 10:45:51 +09001433/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001434 * Local variables:
1435 * c-indent-level: 8
1436 * c-basic-offset: 8
1437 * End:
1438 */