blob: e62e3406d85065eb4fbb2ed04dedda694ad3be64 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
15
16#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020018#include <common/errors.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020019#include <common/time.h>
20
Willy Tarreau272adea2014-03-31 10:39:59 +020021#include <types/global.h>
22
23#include <proto/port_range.h>
24#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020025#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020026#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020027#include <proto/server.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020028#include <proto/session.h>
29#include <proto/task.h>
30
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreau21faa912012-10-10 08:27:36 +020032/* List head of all known server keywords */
33static struct srv_kw_list srv_keywords = {
34 .list = LIST_HEAD_INIT(srv_keywords.list)
35};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020036
Simon Hormana3608442013-11-01 16:46:15 +090037int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020038{
Willy Tarreau892337c2014-05-13 23:41:20 +020039 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020040 return s->down_time;
41
42 return now.tv_sec - s->last_change + s->down_time;
43}
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050045int srv_lastsession(const struct server *s)
46{
47 if (s->counters.last_sess)
48 return now.tv_sec - s->counters.last_sess;
49
50 return -1;
51}
52
Simon Horman4a741432013-02-23 15:35:38 +090053int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020054{
Simon Horman4a741432013-02-23 15:35:38 +090055 const struct server *s = check->server;
56
Willy Tarreauff5ae352013-12-11 20:36:34 +010057 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090058 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010059
Willy Tarreau892337c2014-05-13 23:41:20 +020060 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090061 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010062
Simon Horman4a741432013-02-23 15:35:38 +090063 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010064}
65
Willy Tarreau21faa912012-10-10 08:27:36 +020066/*
67 * Registers the server keyword list <kwl> as a list of valid keywords for next
68 * parsing sessions.
69 */
70void srv_register_keywords(struct srv_kw_list *kwl)
71{
72 LIST_ADDQ(&srv_keywords.list, &kwl->list);
73}
74
75/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
76 * keyword is found with a NULL ->parse() function, then an attempt is made to
77 * find one with a valid ->parse() function. This way it is possible to declare
78 * platform-dependant, known keywords as NULL, then only declare them as valid
79 * if some options are met. Note that if the requested keyword contains an
80 * opening parenthesis, everything from this point is ignored.
81 */
82struct srv_kw *srv_find_kw(const char *kw)
83{
84 int index;
85 const char *kwend;
86 struct srv_kw_list *kwl;
87 struct srv_kw *ret = NULL;
88
89 kwend = strchr(kw, '(');
90 if (!kwend)
91 kwend = kw + strlen(kw);
92
93 list_for_each_entry(kwl, &srv_keywords.list, list) {
94 for (index = 0; kwl->kw[index].kw != NULL; index++) {
95 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
96 kwl->kw[index].kw[kwend-kw] == 0) {
97 if (kwl->kw[index].parse)
98 return &kwl->kw[index]; /* found it !*/
99 else
100 ret = &kwl->kw[index]; /* may be OK */
101 }
102 }
103 }
104 return ret;
105}
106
107/* Dumps all registered "server" keywords to the <out> string pointer. The
108 * unsupported keywords are only dumped if their supported form was not
109 * found.
110 */
111void srv_dump_kws(char **out)
112{
113 struct srv_kw_list *kwl;
114 int index;
115
116 *out = NULL;
117 list_for_each_entry(kwl, &srv_keywords.list, list) {
118 for (index = 0; kwl->kw[index].kw != NULL; index++) {
119 if (kwl->kw[index].parse ||
120 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
121 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
122 kwl->scope,
123 kwl->kw[index].kw,
124 kwl->kw[index].skip ? " <arg>" : "",
125 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
126 kwl->kw[index].parse ? "" : " (not supported)");
127 }
128 }
129 }
130}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100131
Willy Tarreaudff55432012-10-10 17:51:05 +0200132/* parse the "id" server keyword */
133static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
134{
135 struct eb32_node *node;
136
137 if (!*args[*cur_arg + 1]) {
138 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
139 return ERR_ALERT | ERR_FATAL;
140 }
141
142 newsrv->puid = atol(args[*cur_arg + 1]);
143 newsrv->conf.id.key = newsrv->puid;
144
145 if (newsrv->puid <= 0) {
146 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
147 return ERR_ALERT | ERR_FATAL;
148 }
149
150 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
151 if (node) {
152 struct server *target = container_of(node, struct server, conf.id);
153 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
154 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
155 target->id);
156 return ERR_ALERT | ERR_FATAL;
157 }
158
159 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
160 return 0;
161}
162
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200163/* Shutdown all connections of a server. The caller must pass a termination
164 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
165 * shutdown.
166 */
167void srv_shutdown_sessions(struct server *srv, int why)
168{
169 struct session *session, *session_bck;
170
171 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
172 if (session->srv_conn == srv)
173 session_shutdown(session, why);
174}
175
176/* Shutdown all connections of all backup servers of a proxy. The caller must
177 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
178 * the reason for the shutdown.
179 */
180void srv_shutdown_backup_sessions(struct proxy *px, int why)
181{
182 struct server *srv;
183
184 for (srv = px->srv; srv != NULL; srv = srv->next)
185 if (srv->flags & SRV_F_BACKUP)
186 srv_shutdown_sessions(srv, why);
187}
188
Willy Tarreaubda92272014-05-20 21:55:30 +0200189/* Appends some information to a message string related to a server going UP or
190 * DOWN. If both <forced> and <reason> are null and the server tracks another
191 * one, a "via" information will be provided to know where the status came from.
192 * If <reason> is non-null, the entire string will be appended after a comma and
193 * a space (eg: to report some information from the check that changed the state).
194 * If <xferred> is non-negative, some information about requeued sessions are
195 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200196 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200197void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200198{
Willy Tarreaubda92272014-05-20 21:55:30 +0200199 if (reason)
200 chunk_appendf(msg, ", %s", reason);
201 else if (!forced && s->track)
202 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200203
204 if (xferred >= 0) {
205 if (s->state == SRV_ST_STOPPED)
206 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
207 " %d sessions active, %d requeued, %d remaining in queue",
208 s->proxy->srv_act, s->proxy->srv_bck,
209 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
210 s->cur_sess, xferred, s->nbpend);
211 else
212 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
213 " %d sessions requeued, %d total in queue",
214 s->proxy->srv_act, s->proxy->srv_bck,
215 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
216 xferred, s->nbpend);
217 }
218}
219
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200220/* Marks server <s> down, regardless of its checks' statuses, notifies by all
221 * available means, recounts the remaining servers on the proxy and transfers
222 * queued sessions whenever possible to other servers. It automatically
223 * recomputes the number of servers, but not the map. Maintenance servers are
224 * ignored. It reports <reason> if non-null as the reason for going down. Note
225 * that it makes use of the trash to build the log strings, so <reason> must
226 * not be placed there.
227 */
228void srv_set_stopped(struct server *s, const char *reason)
229{
230 struct server *srv;
231 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
232 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
233 int xferred;
234
235 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
236 return;
237
238 s->last_change = now.tv_sec;
239 s->state = SRV_ST_STOPPED;
240 if (s->proxy->lbprm.set_server_status_down)
241 s->proxy->lbprm.set_server_status_down(s);
242
243 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
244 srv_shutdown_sessions(s, SN_ERR_DOWN);
245
246 /* we might have sessions queued on this server and waiting for
247 * a connection. Those which are redispatchable will be queued
248 * to another server or to the proxy itself.
249 */
250 xferred = pendconn_redistribute(s);
251
252 chunk_printf(&trash,
253 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
254 s->proxy->id, s->id);
255
256 srv_append_status(&trash, s, reason, xferred, 0);
257 Warning("%s.\n", trash.str);
258
259 /* we don't send an alert if the server was previously paused */
260 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
261
262 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
263 set_backend_down(s->proxy);
264
265 s->counters.down_trans++;
266
267 for (srv = s->trackers; srv; srv = srv->tracknext)
268 srv_set_stopped(srv, NULL);
269}
270
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200271/* Puts server <s> into maintenance mode, and propagate that status down to all
272 * tracking servers. This does the same action as the CLI's "disable server x".
273 * A log is emitted for all servers that were not yet in maintenance mode.
274 * Health checks are disabled but not agent checks. The server is marked as
275 * being either forced into maintenance by having <mode> set to SRV_ADMF_FMAINT,
276 * or as inheriting the maintenance status by having <mode> set to
277 * SRV_ADMF_IMAINT. Nothing is done if neither flag is set.
278 */
279void srv_adm_set_maint(struct server *s, enum srv_admin mode)
280{
281 struct check *check = &s->check;
282 struct server *srv;
283 int xferred;
284
285 if (!mode)
286 return;
287
288 /* stop going down as soon as we meet a server already in the same state */
289 if (s->admin & mode)
290 return;
291
292 s->admin |= mode;
293
294 if (s->check.state & CHK_ST_ENABLED) {
295 s->check.state |= CHK_ST_PAUSED;
296 check->health = 0;
297 }
298
299 if (s->state == SRV_ST_STOPPED) { /* server was already down */
300 if (!(s->admin & ~mode & SRV_ADMF_MAINT)) {
301 chunk_printf(&trash,
302 "%sServer %s/%s was DOWN and now enters maintenance",
303 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
304
Willy Tarreaubda92272014-05-20 21:55:30 +0200305 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200306
307 Warning("%s.\n", trash.str);
308 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
309 }
310 }
311 else { /* server was still running */
312 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
313 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
314
315 check->health = 0; /* failure */
316 s->last_change = now.tv_sec;
317 s->state = SRV_ST_STOPPED;
318 if (s->proxy->lbprm.set_server_status_down)
319 s->proxy->lbprm.set_server_status_down(s);
320
321 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
322 srv_shutdown_sessions(s, SN_ERR_DOWN);
323
324 /* we might have sessions queued on this server and waiting for
325 * a connection. Those which are redispatchable will be queued
326 * to another server or to the proxy itself.
327 */
328 xferred = pendconn_redistribute(s);
329
330 chunk_printf(&trash,
331 "%sServer %s/%s is going DOWN for maintenance",
332 s->flags & SRV_F_BACKUP ? "Backup " : "",
333 s->proxy->id, s->id);
334
Willy Tarreaubda92272014-05-20 21:55:30 +0200335 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200336
337 Warning("%s.\n", trash.str);
338 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
339
340 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
341 set_backend_down(s->proxy);
342
343 s->counters.down_trans++;
344 }
345
346 for (srv = s->trackers; srv; srv = srv->tracknext)
347 srv_adm_set_maint(srv, SRV_ADMF_IMAINT);
348}
349
350/* Gets server <s> out of maintenance mode, and propagate that status down to
351 * all tracking servers. This does the same action as the CLI's "enable server x".
352 * A log is emitted for all servers that leave maintenance mode. Health checks
353 * are possibly enabled again. The server is marked as leaving forced maintenance
354 * when <mode> is set to SRV_ADMF_FMAINT, or as leaving inherited maintenance
355 * when <mode> set to SRV_ADMF_IMAINT. Nothing is done if neither flag is set.
356 */
357void srv_adm_set_ready(struct server *s, enum srv_admin mode)
358{
359 struct check *check = &s->check;
360 struct server *srv;
361 int xferred = -1;
362
363 if (!mode)
364 return;
365
366 /* stop going down as soon as we see the flag is not there anymore */
367 if (!(s->admin & mode))
368 return;
369
370 s->admin &= ~mode;
371
372 if (s->admin & SRV_ADMF_MAINT) {
373 /* remaining in maintenance mode, let's inform precisely about the
374 * situation.
375 */
376
377 if (s->admin & SRV_ADMF_FMAINT) {
378 chunk_printf(&trash,
379 "%sServer %s/%s remains in forced maintenance",
380 s->flags & SRV_F_BACKUP ? "Backup " : "",
381 s->proxy->id, s->id);
382 }
383 else {
384 chunk_printf(&trash,
385 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
386 s->flags & SRV_F_BACKUP ? "Backup " : "",
387 s->proxy->id, s->id);
388
389 if (s->track) /* normally it's mandatory here */
390 chunk_appendf(&trash, " via %s/%s",
391 s->track->proxy->id, s->track->id);
392 }
393
394 Warning("%s.\n", trash.str);
395 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
396 return;
397 }
398
399 /* OK here we're leaving maintenance, we have many things to check,
400 * because the server might possibly be coming back up depending on
401 * its state. In practice, leaving maintenance means that we should
402 * immediately turn to UP (more or less the slowstart) under the
403 * following conditions :
404 * - server is neither checked nor tracked
405 * - server tracks another server which is not checked
406 * - server tracks another server which is already up
407 * Which sums up as something simpler :
408 * "either the server's or the tracked server's checks are disabled or up".
409 * Otherwise we only re-enable health checks.
410 */
411
412 if (s->check.state & CHK_ST_ENABLED) {
413 s->check.state &= ~CHK_ST_PAUSED;
414 check->health = check->rise; /* start OK but check immediately */
415 }
416
Willy Tarreau32091232014-05-16 13:52:00 +0200417 srv = s;
418 while (srv->track)
419 srv = srv->track;
420
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200421 if ((!s->track &&
422 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
423 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) ||
424 (s->track &&
Willy Tarreau32091232014-05-16 13:52:00 +0200425 (!(srv->agent.state & CHK_ST_ENABLED) || (srv->agent.health >= srv->agent.rise)) &&
426 (!(srv->check.state & CHK_ST_ENABLED) || (srv->check.health >= srv->check.rise)))) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200427
428 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
429 if (s->proxy->last_change < now.tv_sec) // ignore negative times
430 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
431 s->proxy->last_change = now.tv_sec;
432 }
433
434 if (s->last_change < now.tv_sec) // ignore negative times
435 s->down_time += now.tv_sec - s->last_change;
436 s->last_change = now.tv_sec;
437
438 s->state = SRV_ST_STARTING;
439 if (s->slowstart > 0)
440 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
441 else
442 s->state = SRV_ST_RUNNING;
443
444 server_recalc_eweight(s);
445
446 /* If the server is set with "on-marked-up shutdown-backup-sessions",
447 * and it's not a backup server and its effective weight is > 0,
448 * then it can accept new connections, so we shut down all sessions
449 * on all backup servers.
450 */
451 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
452 !(s->flags & SRV_F_BACKUP) && s->eweight)
453 srv_shutdown_backup_sessions(s->proxy, SN_ERR_UP);
454
455 /* check if we can handle some connections queued at the proxy. We
456 * will take as many as we can handle.
457 */
458 xferred = pendconn_grab_from_px(s);
459 }
460
461 if (mode & SRV_ADMF_FMAINT) {
462 chunk_printf(&trash,
463 "%sServer %s/%s is %s (leaving forced maintenance)",
464 s->flags & SRV_F_BACKUP ? "Backup " : "",
465 s->proxy->id, s->id,
466 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
467 }
468 else {
469 chunk_printf(&trash,
470 "%sServer %s/%s is %s (leaving maintenance)",
471 s->flags & SRV_F_BACKUP ? "Backup " : "",
472 s->proxy->id, s->id,
473 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
Willy Tarreaubda92272014-05-20 21:55:30 +0200474 srv_append_status(&trash, s, NULL, xferred, 0);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200475 }
476
477 Warning("%s.\n", trash.str);
478 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
479
480 for (srv = s->trackers; srv; srv = srv->tracknext)
481 srv_adm_set_ready(srv, SRV_ADMF_IMAINT);
482}
483
Willy Tarreaudff55432012-10-10 17:51:05 +0200484/* Note: must not be declared <const> as its list will be overwritten.
485 * Please take care of keeping this list alphabetically sorted, doing so helps
486 * all code contributors.
487 * Optional keywords are also declared with a NULL ->parse() function so that
488 * the config parser can report an appropriate error when a known keyword was
489 * not enabled.
490 */
491static struct srv_kw_list srv_kws = { "ALL", { }, {
492 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
493 { NULL, NULL, 0 },
494}};
495
496__attribute__((constructor))
497static void __listener_init(void)
498{
499 srv_register_keywords(&srv_kws);
500}
501
Willy Tarreau004e0452013-11-21 11:22:01 +0100502/* Recomputes the server's eweight based on its state, uweight, the current time,
503 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
504 * state is automatically disabled if the time is elapsed.
505 */
506void server_recalc_eweight(struct server *sv)
507{
508 struct proxy *px = sv->proxy;
509 unsigned w;
510
511 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
512 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200513 if (sv->state == SRV_ST_STARTING)
514 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100515 }
516
517 /* We must take care of not pushing the server to full throttle during slow starts.
518 * It must also start immediately, at least at the minimal step when leaving maintenance.
519 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200520 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100521 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
522 else
523 w = px->lbprm.wdiv;
524
525 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
526
527 /* now propagate the status change to any LB algorithms */
528 if (px->lbprm.update_server_eweight)
529 px->lbprm.update_server_eweight(sv);
530 else if (sv->eweight) {
531 if (px->lbprm.set_server_status_up)
532 px->lbprm.set_server_status_up(sv);
533 }
534 else {
535 if (px->lbprm.set_server_status_down)
536 px->lbprm.set_server_status_down(sv);
537 }
538}
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900541 * Parses weight_str and configures sv accordingly.
542 * Returns NULL on success, error message string otherwise.
543 */
544const char *server_parse_weight_change_request(struct server *sv,
545 const char *weight_str)
546{
547 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900548 long int w;
549 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900550
551 px = sv->proxy;
552
553 /* if the weight is terminated with '%', it is set relative to
554 * the initial weight, otherwise it is absolute.
555 */
556 if (!*weight_str)
557 return "Require <weight> or <weight%>.\n";
558
Simon Hormanb796afa2013-02-12 10:45:53 +0900559 w = strtol(weight_str, &end, 10);
560 if (end == weight_str)
561 return "Empty weight string empty or preceded by garbage";
562 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900563 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900564 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900565 /* Avoid integer overflow */
566 if (w > 25600)
567 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900568 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900569 if (w > 256)
570 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900571 }
572 else if (w < 0 || w > 256)
573 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900574 else if (end[0] != '\0')
575 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900576
577 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
578 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
579
580 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100581 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900582
583 return NULL;
584}
585
Willy Tarreau272adea2014-03-31 10:39:59 +0200586static int init_check(struct check *check, int type, const char * file, int linenum)
587{
588 check->type = type;
589
590 /* Allocate buffer for requests... */
591 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
592 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
593 return ERR_ALERT | ERR_ABORT;
594 }
595 check->bi->size = global.tune.chksize;
596
597 /* Allocate buffer for responses... */
598 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
599 Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
600 return ERR_ALERT | ERR_ABORT;
601 }
602 check->bo->size = global.tune.chksize;
603
604 /* Allocate buffer for partial results... */
605 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
606 Alert("parsing [%s:%d] : out of memory while allocating check connection.\n", file, linenum);
607 return ERR_ALERT | ERR_ABORT;
608 }
609
610 check->conn->t.sock.fd = -1; /* no agent in progress yet */
611
612 return 0;
613}
614
615int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
616{
617 struct server *newsrv = NULL;
618 const char *err;
619 char *errmsg = NULL;
620 int err_code = 0;
621 unsigned val;
622
623 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
624 int cur_arg;
625 short realport = 0;
626 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
627
628 if (!defsrv && curproxy == defproxy) {
629 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
630 err_code |= ERR_ALERT | ERR_FATAL;
631 goto out;
632 }
633 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
634 err_code |= ERR_ALERT | ERR_FATAL;
635
636 if (!*args[2]) {
637 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
638 file, linenum, args[0]);
639 err_code |= ERR_ALERT | ERR_FATAL;
640 goto out;
641 }
642
643 err = invalid_char(args[1]);
644 if (err && !defsrv) {
645 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
646 file, linenum, *err, args[1]);
647 err_code |= ERR_ALERT | ERR_FATAL;
648 goto out;
649 }
650
651 if (!defsrv) {
652 struct sockaddr_storage *sk;
653 int port1, port2;
654 struct protocol *proto;
655
656 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
657 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
658 err_code |= ERR_ALERT | ERR_ABORT;
659 goto out;
660 }
661
662 /* the servers are linked backwards first */
663 newsrv->next = curproxy->srv;
664 curproxy->srv = newsrv;
665 newsrv->proxy = curproxy;
666 newsrv->conf.file = strdup(file);
667 newsrv->conf.line = linenum;
668
669 newsrv->obj_type = OBJ_TYPE_SERVER;
670 LIST_INIT(&newsrv->actconns);
671 LIST_INIT(&newsrv->pendconns);
672 do_check = 0;
673 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200674 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200675 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200676 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200677 newsrv->last_change = now.tv_sec;
678 newsrv->id = strdup(args[1]);
679
680 /* several ways to check the port component :
681 * - IP => port=+0, relative (IPv4 only)
682 * - IP: => port=+0, relative
683 * - IP:N => port=N, absolute
684 * - IP:+N => port=+N, relative
685 * - IP:-N => port=-N, relative
686 */
687 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
688 if (!sk) {
689 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
690 err_code |= ERR_ALERT | ERR_FATAL;
691 goto out;
692 }
693
694 proto = protocol_by_family(sk->ss_family);
695 if (!proto || !proto->connect) {
696 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
697 file, linenum, args[0], args[1]);
698 err_code |= ERR_ALERT | ERR_FATAL;
699 goto out;
700 }
701
702 if (!port1 || !port2) {
703 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200704 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200705 }
706 else if (port1 != port2) {
707 /* port range */
708 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
709 file, linenum, args[0], args[1], args[2]);
710 err_code |= ERR_ALERT | ERR_FATAL;
711 goto out;
712 }
713 else {
714 /* used by checks */
715 realport = port1;
716 }
717
718 newsrv->addr = *sk;
719 newsrv->proto = newsrv->check_common.proto = protocol_by_family(newsrv->addr.ss_family);
720 newsrv->xprt = newsrv->check_common.xprt = &raw_sock;
721
722 if (!newsrv->proto) {
723 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
724 file, linenum, newsrv->addr.ss_family, args[2]);
725 err_code |= ERR_ALERT | ERR_FATAL;
726 goto out;
727 }
728
729 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
730 newsrv->check.port = curproxy->defsrv.check.port;
731 newsrv->check.inter = curproxy->defsrv.check.inter;
732 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
733 newsrv->check.downinter = curproxy->defsrv.check.downinter;
734 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
735 newsrv->agent.port = curproxy->defsrv.agent.port;
736 newsrv->agent.inter = curproxy->defsrv.agent.inter;
737 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
738 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
739 newsrv->maxqueue = curproxy->defsrv.maxqueue;
740 newsrv->minconn = curproxy->defsrv.minconn;
741 newsrv->maxconn = curproxy->defsrv.maxconn;
742 newsrv->slowstart = curproxy->defsrv.slowstart;
743 newsrv->onerror = curproxy->defsrv.onerror;
744 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
745 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
746 newsrv->consecutive_errors_limit
747 = curproxy->defsrv.consecutive_errors_limit;
748#ifdef OPENSSL
749 newsrv->use_ssl = curproxy->defsrv.use_ssl;
750#endif
751 newsrv->uweight = newsrv->iweight
752 = curproxy->defsrv.iweight;
753
754 newsrv->check.status = HCHK_STATUS_INI;
755 newsrv->check.rise = curproxy->defsrv.check.rise;
756 newsrv->check.fall = curproxy->defsrv.check.fall;
757 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
758 newsrv->check.server = newsrv;
759
760 newsrv->agent.status = HCHK_STATUS_INI;
761 newsrv->agent.rise = curproxy->defsrv.agent.rise;
762 newsrv->agent.fall = curproxy->defsrv.agent.fall;
763 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
764 newsrv->agent.server = newsrv;
765
766 cur_arg = 3;
767 } else {
768 newsrv = &curproxy->defsrv;
769 cur_arg = 1;
770 }
771
772 while (*args[cur_arg]) {
773 if (!strcmp(args[cur_arg], "agent-check")) {
774 global.maxsock++;
775 do_agent = 1;
776 cur_arg += 1;
777 } else if (!strcmp(args[cur_arg], "agent-inter")) {
778 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
779 if (err) {
780 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
781 file, linenum, *err, newsrv->id);
782 err_code |= ERR_ALERT | ERR_FATAL;
783 goto out;
784 }
785 if (val <= 0) {
786 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
787 file, linenum, val, args[cur_arg], newsrv->id);
788 err_code |= ERR_ALERT | ERR_FATAL;
789 goto out;
790 }
791 newsrv->agent.inter = val;
792 cur_arg += 2;
793 }
794 else if (!strcmp(args[cur_arg], "agent-port")) {
795 global.maxsock++;
796 newsrv->agent.port = atol(args[cur_arg + 1]);
797 cur_arg += 2;
798 }
799 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
800 newsrv->cookie = strdup(args[cur_arg + 1]);
801 newsrv->cklen = strlen(args[cur_arg + 1]);
802 cur_arg += 2;
803 }
804 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
805 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
806 newsrv->rdr_len = strlen(args[cur_arg + 1]);
807 cur_arg += 2;
808 }
809 else if (!strcmp(args[cur_arg], "rise")) {
810 if (!*args[cur_arg + 1]) {
811 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
812 file, linenum, args[cur_arg]);
813 err_code |= ERR_ALERT | ERR_FATAL;
814 goto out;
815 }
816
817 newsrv->check.rise = atol(args[cur_arg + 1]);
818 if (newsrv->check.rise <= 0) {
819 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
820 file, linenum, args[cur_arg]);
821 err_code |= ERR_ALERT | ERR_FATAL;
822 goto out;
823 }
824
825 if (newsrv->check.health)
826 newsrv->check.health = newsrv->check.rise;
827 cur_arg += 2;
828 }
829 else if (!strcmp(args[cur_arg], "fall")) {
830 newsrv->check.fall = atol(args[cur_arg + 1]);
831
832 if (!*args[cur_arg + 1]) {
833 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
834 file, linenum, args[cur_arg]);
835 err_code |= ERR_ALERT | ERR_FATAL;
836 goto out;
837 }
838
839 if (newsrv->check.fall <= 0) {
840 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
841 file, linenum, args[cur_arg]);
842 err_code |= ERR_ALERT | ERR_FATAL;
843 goto out;
844 }
845
846 cur_arg += 2;
847 }
848 else if (!strcmp(args[cur_arg], "inter")) {
849 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
850 if (err) {
851 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
852 file, linenum, *err, newsrv->id);
853 err_code |= ERR_ALERT | ERR_FATAL;
854 goto out;
855 }
856 if (val <= 0) {
857 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
858 file, linenum, val, args[cur_arg], newsrv->id);
859 err_code |= ERR_ALERT | ERR_FATAL;
860 goto out;
861 }
862 newsrv->check.inter = val;
863 cur_arg += 2;
864 }
865 else if (!strcmp(args[cur_arg], "fastinter")) {
866 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
867 if (err) {
868 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
869 file, linenum, *err, newsrv->id);
870 err_code |= ERR_ALERT | ERR_FATAL;
871 goto out;
872 }
873 if (val <= 0) {
874 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
875 file, linenum, val, args[cur_arg], newsrv->id);
876 err_code |= ERR_ALERT | ERR_FATAL;
877 goto out;
878 }
879 newsrv->check.fastinter = val;
880 cur_arg += 2;
881 }
882 else if (!strcmp(args[cur_arg], "downinter")) {
883 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
884 if (err) {
885 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
886 file, linenum, *err, newsrv->id);
887 err_code |= ERR_ALERT | ERR_FATAL;
888 goto out;
889 }
890 if (val <= 0) {
891 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
892 file, linenum, val, args[cur_arg], newsrv->id);
893 err_code |= ERR_ALERT | ERR_FATAL;
894 goto out;
895 }
896 newsrv->check.downinter = val;
897 cur_arg += 2;
898 }
899 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
900 struct sockaddr_storage *sk;
901 int port1, port2;
902 struct protocol *proto;
903
904 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
905 if (!sk) {
906 Alert("parsing [%s:%d] : '%s' : %s\n",
907 file, linenum, args[cur_arg], errmsg);
908 err_code |= ERR_ALERT | ERR_FATAL;
909 goto out;
910 }
911
912 proto = protocol_by_family(sk->ss_family);
913 if (!proto || !proto->connect) {
914 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
915 file, linenum, args[cur_arg], args[cur_arg + 1]);
916 err_code |= ERR_ALERT | ERR_FATAL;
917 goto out;
918 }
919
920 if (port1 != port2) {
921 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
922 file, linenum, args[cur_arg], args[cur_arg + 1]);
923 err_code |= ERR_ALERT | ERR_FATAL;
924 goto out;
925 }
926
927 newsrv->check_common.addr = *sk;
Willy Tarreau640556c2014-05-09 23:38:15 +0200928 newsrv->check_common.proto = protocol_by_family(sk->ss_family);
Willy Tarreau272adea2014-03-31 10:39:59 +0200929 cur_arg += 2;
930 }
931 else if (!strcmp(args[cur_arg], "port")) {
932 newsrv->check.port = atol(args[cur_arg + 1]);
933 cur_arg += 2;
934 }
935 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +0200936 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +0200937 cur_arg ++;
938 }
939 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +0200940 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +0200941 cur_arg ++;
942 }
943 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -0400944 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +0200945 cur_arg ++;
946 }
David Safb76832014-05-08 23:42:08 -0400947 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
948 newsrv->pp_opts |= SRV_PP_V2;
949 cur_arg ++;
950 }
Willy Tarreau272adea2014-03-31 10:39:59 +0200951 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
952 newsrv->check.send_proxy = 1;
953 cur_arg ++;
954 }
955 else if (!strcmp(args[cur_arg], "weight")) {
956 int w;
957 w = atol(args[cur_arg + 1]);
958 if (w < 0 || w > SRV_UWGHT_MAX) {
959 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
960 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
961 err_code |= ERR_ALERT | ERR_FATAL;
962 goto out;
963 }
964 newsrv->uweight = newsrv->iweight = w;
965 cur_arg += 2;
966 }
967 else if (!strcmp(args[cur_arg], "minconn")) {
968 newsrv->minconn = atol(args[cur_arg + 1]);
969 cur_arg += 2;
970 }
971 else if (!strcmp(args[cur_arg], "maxconn")) {
972 newsrv->maxconn = atol(args[cur_arg + 1]);
973 cur_arg += 2;
974 }
975 else if (!strcmp(args[cur_arg], "maxqueue")) {
976 newsrv->maxqueue = atol(args[cur_arg + 1]);
977 cur_arg += 2;
978 }
979 else if (!strcmp(args[cur_arg], "slowstart")) {
980 /* slowstart is stored in seconds */
981 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
982 if (err) {
983 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
984 file, linenum, *err, newsrv->id);
985 err_code |= ERR_ALERT | ERR_FATAL;
986 goto out;
987 }
988 newsrv->slowstart = (val + 999) / 1000;
989 cur_arg += 2;
990 }
991 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
992
993 if (!*args[cur_arg + 1]) {
994 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
995 file, linenum);
996 err_code |= ERR_ALERT | ERR_FATAL;
997 goto out;
998 }
999
1000 newsrv->trackit = strdup(args[cur_arg + 1]);
1001
1002 cur_arg += 2;
1003 }
1004 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1005 global.maxsock++;
1006 do_check = 1;
1007 cur_arg += 1;
1008 }
1009 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001010 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001011 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001012 newsrv->check.state |= CHK_ST_PAUSED;
1013 newsrv->check.health = 0;
1014 newsrv->agent.health = 0;
1015 cur_arg += 1;
1016 }
1017 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1018 if (!strcmp(args[cur_arg + 1], "none"))
1019 newsrv->observe = HANA_OBS_NONE;
1020 else if (!strcmp(args[cur_arg + 1], "layer4"))
1021 newsrv->observe = HANA_OBS_LAYER4;
1022 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1023 if (curproxy->mode != PR_MODE_HTTP) {
1024 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1025 file, linenum, args[cur_arg + 1]);
1026 err_code |= ERR_ALERT;
1027 }
1028 newsrv->observe = HANA_OBS_LAYER7;
1029 }
1030 else {
1031 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1032 "'layer4', 'layer7' but got '%s'\n",
1033 file, linenum, args[cur_arg], args[cur_arg + 1]);
1034 err_code |= ERR_ALERT | ERR_FATAL;
1035 goto out;
1036 }
1037
1038 cur_arg += 2;
1039 }
1040 else if (!strcmp(args[cur_arg], "on-error")) {
1041 if (!strcmp(args[cur_arg + 1], "fastinter"))
1042 newsrv->onerror = HANA_ONERR_FASTINTER;
1043 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1044 newsrv->onerror = HANA_ONERR_FAILCHK;
1045 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1046 newsrv->onerror = HANA_ONERR_SUDDTH;
1047 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1048 newsrv->onerror = HANA_ONERR_MARKDWN;
1049 else {
1050 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1051 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1052 file, linenum, args[cur_arg], args[cur_arg + 1]);
1053 err_code |= ERR_ALERT | ERR_FATAL;
1054 goto out;
1055 }
1056
1057 cur_arg += 2;
1058 }
1059 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1060 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1061 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1062 else {
1063 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1064 file, linenum, args[cur_arg], args[cur_arg + 1]);
1065 err_code |= ERR_ALERT | ERR_FATAL;
1066 goto out;
1067 }
1068
1069 cur_arg += 2;
1070 }
1071 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1072 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1073 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1074 else {
1075 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1076 file, linenum, args[cur_arg], args[cur_arg + 1]);
1077 err_code |= ERR_ALERT | ERR_FATAL;
1078 goto out;
1079 }
1080
1081 cur_arg += 2;
1082 }
1083 else if (!strcmp(args[cur_arg], "error-limit")) {
1084 if (!*args[cur_arg + 1]) {
1085 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1086 file, linenum, args[cur_arg]);
1087 err_code |= ERR_ALERT | ERR_FATAL;
1088 goto out;
1089 }
1090
1091 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1092
1093 if (newsrv->consecutive_errors_limit <= 0) {
1094 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1095 file, linenum, args[cur_arg]);
1096 err_code |= ERR_ALERT | ERR_FATAL;
1097 goto out;
1098 }
1099 cur_arg += 2;
1100 }
1101 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1102 int port_low, port_high;
1103 struct sockaddr_storage *sk;
1104 struct protocol *proto;
1105
1106 if (!*args[cur_arg + 1]) {
1107 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1108 file, linenum, "source", "usesrc", "interface");
1109 err_code |= ERR_ALERT | ERR_FATAL;
1110 goto out;
1111 }
1112
1113 newsrv->conn_src.opts |= CO_SRC_BIND;
1114 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1115 if (!sk) {
1116 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1117 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1118 err_code |= ERR_ALERT | ERR_FATAL;
1119 goto out;
1120 }
1121
1122 proto = protocol_by_family(sk->ss_family);
1123 if (!proto || !proto->connect) {
1124 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1125 file, linenum, args[cur_arg], args[cur_arg+1]);
1126 err_code |= ERR_ALERT | ERR_FATAL;
1127 goto out;
1128 }
1129
1130 newsrv->conn_src.source_addr = *sk;
1131
1132 if (port_low != port_high) {
1133 int i;
1134
1135 if (!port_low || !port_high) {
1136 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1137 file, linenum, args[cur_arg], args[cur_arg + 1]);
1138 err_code |= ERR_ALERT | ERR_FATAL;
1139 goto out;
1140 }
1141
1142 if (port_low <= 0 || port_low > 65535 ||
1143 port_high <= 0 || port_high > 65535 ||
1144 port_low > port_high) {
1145 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1146 file, linenum, port_low, port_high);
1147 err_code |= ERR_ALERT | ERR_FATAL;
1148 goto out;
1149 }
1150 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1151 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1152 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1153 }
1154
1155 cur_arg += 2;
1156 while (*(args[cur_arg])) {
1157 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1158#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1159#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001160 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001161 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1162 file, linenum, "usesrc", "source");
1163 err_code |= ERR_ALERT | ERR_FATAL;
1164 goto out;
1165 }
1166#endif
1167 if (!*args[cur_arg + 1]) {
1168 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1169 file, linenum, "usesrc");
1170 err_code |= ERR_ALERT | ERR_FATAL;
1171 goto out;
1172 }
1173 if (!strcmp(args[cur_arg + 1], "client")) {
1174 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1175 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1176 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1177 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1178 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1179 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1180 char *name, *end;
1181
1182 name = args[cur_arg+1] + 7;
1183 while (isspace(*name))
1184 name++;
1185
1186 end = name;
1187 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1188 end++;
1189
1190 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1191 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1192 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1193 newsrv->conn_src.bind_hdr_len = end - name;
1194 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1195 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1196 newsrv->conn_src.bind_hdr_occ = -1;
1197
1198 /* now look for an occurrence number */
1199 while (isspace(*end))
1200 end++;
1201 if (*end == ',') {
1202 end++;
1203 name = end;
1204 if (*end == '-')
1205 end++;
1206 while (isdigit((int)*end))
1207 end++;
1208 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1209 }
1210
1211 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1212 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1213 " occurrences values smaller than %d.\n",
1214 file, linenum, MAX_HDR_HISTORY);
1215 err_code |= ERR_ALERT | ERR_FATAL;
1216 goto out;
1217 }
1218 } else {
1219 struct sockaddr_storage *sk;
1220 int port1, port2;
1221
1222 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1223 if (!sk) {
1224 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1225 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1226 err_code |= ERR_ALERT | ERR_FATAL;
1227 goto out;
1228 }
1229
1230 proto = protocol_by_family(sk->ss_family);
1231 if (!proto || !proto->connect) {
1232 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1233 file, linenum, args[cur_arg], args[cur_arg+1]);
1234 err_code |= ERR_ALERT | ERR_FATAL;
1235 goto out;
1236 }
1237
1238 if (port1 != port2) {
1239 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1240 file, linenum, args[cur_arg], args[cur_arg + 1]);
1241 err_code |= ERR_ALERT | ERR_FATAL;
1242 goto out;
1243 }
1244 newsrv->conn_src.tproxy_addr = *sk;
1245 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1246 }
1247 global.last_checks |= LSTCHK_NETADM;
1248#if !defined(CONFIG_HAP_TRANSPARENT)
1249 global.last_checks |= LSTCHK_CTTPROXY;
1250#endif
1251 cur_arg += 2;
1252 continue;
1253#else /* no TPROXY support */
1254 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1255 file, linenum, "usesrc");
1256 err_code |= ERR_ALERT | ERR_FATAL;
1257 goto out;
1258#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1259 } /* "usesrc" */
1260
1261 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1262#ifdef SO_BINDTODEVICE
1263 if (!*args[cur_arg + 1]) {
1264 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1265 file, linenum, args[0]);
1266 err_code |= ERR_ALERT | ERR_FATAL;
1267 goto out;
1268 }
1269 free(newsrv->conn_src.iface_name);
1270 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1271 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1272 global.last_checks |= LSTCHK_NETADM;
1273#else
1274 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1275 file, linenum, args[0], args[cur_arg]);
1276 err_code |= ERR_ALERT | ERR_FATAL;
1277 goto out;
1278#endif
1279 cur_arg += 2;
1280 continue;
1281 }
1282 /* this keyword in not an option of "source" */
1283 break;
1284 } /* while */
1285 }
1286 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1287 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1288 file, linenum, "usesrc", "source");
1289 err_code |= ERR_ALERT | ERR_FATAL;
1290 goto out;
1291 }
1292 else {
1293 static int srv_dumped;
1294 struct srv_kw *kw;
1295 char *err;
1296
1297 kw = srv_find_kw(args[cur_arg]);
1298 if (kw) {
1299 char *err = NULL;
1300 int code;
1301
1302 if (!kw->parse) {
1303 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1304 file, linenum, args[0], args[1], args[cur_arg]);
1305 cur_arg += 1 + kw->skip ;
1306 err_code |= ERR_ALERT | ERR_FATAL;
1307 goto out;
1308 }
1309
1310 if (defsrv && !kw->default_ok) {
1311 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1312 file, linenum, args[0], args[1], args[cur_arg]);
1313 cur_arg += 1 + kw->skip ;
1314 err_code |= ERR_ALERT;
1315 continue;
1316 }
1317
1318 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1319 err_code |= code;
1320
1321 if (code) {
1322 if (err && *err) {
1323 indent_msg(&err, 2);
1324 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1325 }
1326 else
1327 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1328 file, linenum, args[0], args[1], args[cur_arg]);
1329 if (code & ERR_FATAL) {
1330 free(err);
1331 cur_arg += 1 + kw->skip;
1332 goto out;
1333 }
1334 }
1335 free(err);
1336 cur_arg += 1 + kw->skip;
1337 continue;
1338 }
1339
1340 err = NULL;
1341 if (!srv_dumped) {
1342 srv_dump_kws(&err);
1343 indent_msg(&err, 4);
1344 srv_dumped = 1;
1345 }
1346
1347 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1348 file, linenum, args[0], args[1], args[cur_arg],
1349 err ? " Registered keywords :" : "", err ? err : "");
1350 free(err);
1351
1352 err_code |= ERR_ALERT | ERR_FATAL;
1353 goto out;
1354 }
1355 }
1356
Willy Tarreau272adea2014-03-31 10:39:59 +02001357 if (do_check) {
1358 int ret;
1359
1360 if (newsrv->trackit) {
1361 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1362 file, linenum);
1363 err_code |= ERR_ALERT | ERR_FATAL;
1364 goto out;
1365 }
1366
1367 /* If neither a port nor an addr was specified and no check transport
1368 * layer is forced, then the transport layer used by the checks is the
1369 * same as for the production traffic. Otherwise we use raw_sock by
1370 * default, unless one is specified.
1371 */
1372 if (!newsrv->check.port && !is_addr(&newsrv->check_common.addr)) {
1373#ifdef USE_OPENSSL
1374 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1375#endif
David Safb76832014-05-08 23:42:08 -04001376 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001377 }
1378 /* try to get the port from check_core.addr if check.port not set */
1379 if (!newsrv->check.port)
1380 newsrv->check.port = get_host_port(&newsrv->check_common.addr);
1381
1382 if (!newsrv->check.port)
1383 newsrv->check.port = realport; /* by default */
1384
1385 if (!newsrv->check.port) {
1386 /* not yet valid, because no port was set on
1387 * the server either. We'll check if we have
1388 * a known port on the first listener.
1389 */
1390 struct listener *l;
1391
1392 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1393 newsrv->check.port = get_host_port(&l->addr);
1394 if (newsrv->check.port)
1395 break;
1396 }
1397 }
1398 /*
1399 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001400 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001401 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001402 if (!newsrv->check.port &&
1403 (is_inet_addr(&newsrv->check_common.addr) ||
1404 (!is_addr(&newsrv->check_common.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001405 struct tcpcheck_rule *n = NULL, *r = NULL;
1406 struct list *l;
1407
1408 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1409 if (!r) {
1410 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1411 file, linenum, newsrv->id);
1412 err_code |= ERR_ALERT | ERR_FATAL;
1413 goto out;
1414 }
1415 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1416 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",
1417 file, linenum, newsrv->id);
1418 err_code |= ERR_ALERT | ERR_FATAL;
1419 goto out;
1420 }
1421 else {
1422 /* scan the tcp-check ruleset to ensure a port has been configured */
1423 l = &newsrv->proxy->tcpcheck_rules;
1424 list_for_each_entry(n, l, list) {
1425 r = (struct tcpcheck_rule *)n->list.p;
1426 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1427 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",
1428 file, linenum, newsrv->id);
1429 err_code |= ERR_ALERT | ERR_FATAL;
1430 goto out;
1431 }
1432 }
1433 }
1434 }
1435
1436 /* note: check type will be set during the config review phase */
1437 ret = init_check(&newsrv->check, 0, file, linenum);
1438 if (ret) {
1439 err_code |= ret;
1440 goto out;
1441 }
1442
1443 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1444 }
1445
1446 if (do_agent) {
1447 int ret;
1448
1449 if (!newsrv->agent.port) {
1450 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1451 file, linenum, newsrv->id);
1452 err_code |= ERR_ALERT | ERR_FATAL;
1453 goto out;
1454 }
1455
1456 if (!newsrv->agent.inter)
1457 newsrv->agent.inter = newsrv->check.inter;
1458
1459 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK, file, linenum);
1460 if (ret) {
1461 err_code |= ret;
1462 goto out;
1463 }
1464
1465 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1466 }
1467
1468 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001469 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001470 curproxy->srv_bck++;
1471 else
1472 curproxy->srv_act++;
1473
Willy Tarreauc5150da2014-05-13 19:27:31 +02001474 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001475 }
1476 }
1477 return 0;
1478
1479 out:
1480 free(errmsg);
1481 return err_code;
1482}
1483
Simon Horman7d09b9a2013-02-12 10:45:51 +09001484/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001485 * Local variables:
1486 * c-indent-level: 8
1487 * c-basic-offset: 8
1488 * End:
1489 */