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