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