blob: ee6b8508dac003251c872cca6963612dfc15a10c [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>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010019#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020020#include <common/time.h>
21
Willy Tarreau272adea2014-03-31 10:39:59 +020022#include <types/global.h>
23
Simon Hormanb1900d52015-01-30 11:22:54 +090024#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020025#include <proto/port_range.h>
26#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020027#include <proto/queue.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <proto/raw_sock.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020029#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020030#include <proto/stream.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020031#include <proto/task.h>
32
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
Willy Tarreau21faa912012-10-10 08:27:36 +020034/* List head of all known server keywords */
35static struct srv_kw_list srv_keywords = {
36 .list = LIST_HEAD_INIT(srv_keywords.list)
37};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020038
Simon Hormana3608442013-11-01 16:46:15 +090039int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020040{
Willy Tarreau892337c2014-05-13 23:41:20 +020041 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020042 return s->down_time;
43
44 return now.tv_sec - s->last_change + s->down_time;
45}
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050047int srv_lastsession(const struct server *s)
48{
49 if (s->counters.last_sess)
50 return now.tv_sec - s->counters.last_sess;
51
52 return -1;
53}
54
Simon Horman4a741432013-02-23 15:35:38 +090055int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020056{
Simon Horman4a741432013-02-23 15:35:38 +090057 const struct server *s = check->server;
58
Willy Tarreauff5ae352013-12-11 20:36:34 +010059 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090060 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010061
Willy Tarreau892337c2014-05-13 23:41:20 +020062 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090063 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010064
Simon Horman4a741432013-02-23 15:35:38 +090065 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010066}
67
Willy Tarreau21faa912012-10-10 08:27:36 +020068/*
69 * Registers the server keyword list <kwl> as a list of valid keywords for next
70 * parsing sessions.
71 */
72void srv_register_keywords(struct srv_kw_list *kwl)
73{
74 LIST_ADDQ(&srv_keywords.list, &kwl->list);
75}
76
77/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
78 * keyword is found with a NULL ->parse() function, then an attempt is made to
79 * find one with a valid ->parse() function. This way it is possible to declare
80 * platform-dependant, known keywords as NULL, then only declare them as valid
81 * if some options are met. Note that if the requested keyword contains an
82 * opening parenthesis, everything from this point is ignored.
83 */
84struct srv_kw *srv_find_kw(const char *kw)
85{
86 int index;
87 const char *kwend;
88 struct srv_kw_list *kwl;
89 struct srv_kw *ret = NULL;
90
91 kwend = strchr(kw, '(');
92 if (!kwend)
93 kwend = kw + strlen(kw);
94
95 list_for_each_entry(kwl, &srv_keywords.list, list) {
96 for (index = 0; kwl->kw[index].kw != NULL; index++) {
97 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
98 kwl->kw[index].kw[kwend-kw] == 0) {
99 if (kwl->kw[index].parse)
100 return &kwl->kw[index]; /* found it !*/
101 else
102 ret = &kwl->kw[index]; /* may be OK */
103 }
104 }
105 }
106 return ret;
107}
108
109/* Dumps all registered "server" keywords to the <out> string pointer. The
110 * unsupported keywords are only dumped if their supported form was not
111 * found.
112 */
113void srv_dump_kws(char **out)
114{
115 struct srv_kw_list *kwl;
116 int index;
117
118 *out = NULL;
119 list_for_each_entry(kwl, &srv_keywords.list, list) {
120 for (index = 0; kwl->kw[index].kw != NULL; index++) {
121 if (kwl->kw[index].parse ||
122 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
123 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
124 kwl->scope,
125 kwl->kw[index].kw,
126 kwl->kw[index].skip ? " <arg>" : "",
127 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
128 kwl->kw[index].parse ? "" : " (not supported)");
129 }
130 }
131 }
132}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100133
Willy Tarreaudff55432012-10-10 17:51:05 +0200134/* parse the "id" server keyword */
135static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
136{
137 struct eb32_node *node;
138
139 if (!*args[*cur_arg + 1]) {
140 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
141 return ERR_ALERT | ERR_FATAL;
142 }
143
144 newsrv->puid = atol(args[*cur_arg + 1]);
145 newsrv->conf.id.key = newsrv->puid;
146
147 if (newsrv->puid <= 0) {
148 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
149 return ERR_ALERT | ERR_FATAL;
150 }
151
152 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
153 if (node) {
154 struct server *target = container_of(node, struct server, conf.id);
155 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
156 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
157 target->id);
158 return ERR_ALERT | ERR_FATAL;
159 }
160
161 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
162 return 0;
163}
164
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200165/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200166 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200167 * shutdown.
168 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200169void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200170{
Willy Tarreau87b09662015-04-03 00:22:06 +0200171 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200172
Willy Tarreau87b09662015-04-03 00:22:06 +0200173 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
174 if (stream->srv_conn == srv)
175 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200176}
177
178/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200179 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200180 * the reason for the shutdown.
181 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200182void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200183{
184 struct server *srv;
185
186 for (srv = px->srv; srv != NULL; srv = srv->next)
187 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200188 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200189}
190
Willy Tarreaubda92272014-05-20 21:55:30 +0200191/* Appends some information to a message string related to a server going UP or
192 * DOWN. If both <forced> and <reason> are null and the server tracks another
193 * one, a "via" information will be provided to know where the status came from.
194 * If <reason> is non-null, the entire string will be appended after a comma and
195 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200196 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200197 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200198 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200199void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200200{
Willy Tarreaubda92272014-05-20 21:55:30 +0200201 if (reason)
202 chunk_appendf(msg, ", %s", reason);
203 else if (!forced && s->track)
204 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200205
206 if (xferred >= 0) {
207 if (s->state == SRV_ST_STOPPED)
208 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
209 " %d sessions active, %d requeued, %d remaining in queue",
210 s->proxy->srv_act, s->proxy->srv_bck,
211 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
212 s->cur_sess, xferred, s->nbpend);
213 else
214 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
215 " %d sessions requeued, %d total in queue",
216 s->proxy->srv_act, s->proxy->srv_bck,
217 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
218 xferred, s->nbpend);
219 }
220}
221
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200222/* Marks server <s> down, regardless of its checks' statuses, notifies by all
223 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200224 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200225 * recomputes the number of servers, but not the map. Maintenance servers are
226 * ignored. It reports <reason> if non-null as the reason for going down. Note
227 * that it makes use of the trash to build the log strings, so <reason> must
228 * not be placed there.
229 */
230void srv_set_stopped(struct server *s, const char *reason)
231{
232 struct server *srv;
233 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
234 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900235 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200236 int xferred;
237
238 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
239 return;
240
241 s->last_change = now.tv_sec;
242 s->state = SRV_ST_STOPPED;
243 if (s->proxy->lbprm.set_server_status_down)
244 s->proxy->lbprm.set_server_status_down(s);
245
246 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200247 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200248
Willy Tarreau87b09662015-04-03 00:22:06 +0200249 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200250 * a connection. Those which are redispatchable will be queued
251 * to another server or to the proxy itself.
252 */
253 xferred = pendconn_redistribute(s);
254
255 chunk_printf(&trash,
256 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
257 s->proxy->id, s->id);
258
259 srv_append_status(&trash, s, reason, xferred, 0);
260 Warning("%s.\n", trash.str);
261
262 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900263 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
264 send_log(s->proxy, log_level, "%s.\n", trash.str);
265 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200266
267 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
268 set_backend_down(s->proxy);
269
270 s->counters.down_trans++;
271
272 for (srv = s->trackers; srv; srv = srv->tracknext)
273 srv_set_stopped(srv, NULL);
274}
275
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200276/* Marks server <s> up regardless of its checks' statuses and provided it isn't
277 * in maintenance. Notifies by all available means, recounts the remaining
278 * servers on the proxy and tries to grab requests from the proxy. It
279 * automatically recomputes the number of servers, but not the map. Maintenance
280 * servers are ignored. It reports <reason> if non-null as the reason for going
281 * up. Note that it makes use of the trash to build the log strings, so <reason>
282 * must not be placed there.
283 */
284void srv_set_running(struct server *s, const char *reason)
285{
286 struct server *srv;
287 int xferred;
288
289 if (s->admin & SRV_ADMF_MAINT)
290 return;
291
292 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
293 return;
294
295 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
296 if (s->proxy->last_change < now.tv_sec) // ignore negative times
297 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
298 s->proxy->last_change = now.tv_sec;
299 }
300
301 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
302 s->down_time += now.tv_sec - s->last_change;
303
304 s->last_change = now.tv_sec;
305
306 s->state = SRV_ST_STARTING;
307 if (s->slowstart > 0)
308 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
309 else
310 s->state = SRV_ST_RUNNING;
311
312 server_recalc_eweight(s);
313
314 /* If the server is set with "on-marked-up shutdown-backup-sessions",
315 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200316 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200317 * on all backup servers.
318 */
319 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
320 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200321 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200322
323 /* check if we can handle some connections queued at the proxy. We
324 * will take as many as we can handle.
325 */
326 xferred = pendconn_grab_from_px(s);
327
328 chunk_printf(&trash,
329 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
330 s->proxy->id, s->id);
331
332 srv_append_status(&trash, s, reason, xferred, 0);
333 Warning("%s.\n", trash.str);
334 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900335 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200336
337 for (srv = s->trackers; srv; srv = srv->tracknext)
338 srv_set_running(srv, NULL);
339}
340
Willy Tarreau8eb77842014-05-21 13:54:57 +0200341/* Marks server <s> stopping regardless of its checks' statuses and provided it
342 * isn't in maintenance. Notifies by all available means, recounts the remaining
343 * servers on the proxy and tries to grab requests from the proxy. It
344 * automatically recomputes the number of servers, but not the map. Maintenance
345 * servers are ignored. It reports <reason> if non-null as the reason for going
346 * up. Note that it makes use of the trash to build the log strings, so <reason>
347 * must not be placed there.
348 */
349void srv_set_stopping(struct server *s, const char *reason)
350{
351 struct server *srv;
352 int xferred;
353
354 if (s->admin & SRV_ADMF_MAINT)
355 return;
356
357 if (s->state == SRV_ST_STOPPING)
358 return;
359
360 s->last_change = now.tv_sec;
361 s->state = SRV_ST_STOPPING;
362 if (s->proxy->lbprm.set_server_status_down)
363 s->proxy->lbprm.set_server_status_down(s);
364
Willy Tarreau87b09662015-04-03 00:22:06 +0200365 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200366 * a connection. Those which are redispatchable will be queued
367 * to another server or to the proxy itself.
368 */
369 xferred = pendconn_redistribute(s);
370
371 chunk_printf(&trash,
372 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
373 s->proxy->id, s->id);
374
375 srv_append_status(&trash, s, reason, xferred, 0);
376
377 Warning("%s.\n", trash.str);
378 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
379
380 if (!s->proxy->srv_bck && !s->proxy->srv_act)
381 set_backend_down(s->proxy);
382
383 for (srv = s->trackers; srv; srv = srv->tracknext)
384 srv_set_stopping(srv, NULL);
385}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200386
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200387/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
388 * enforce either maint mode or drain mode. It is not allowed to set more than
389 * one flag at once. The equivalent "inherited" flag is propagated to all
390 * tracking servers. Maintenance mode disables health checks (but not agent
391 * checks). When either the flag is already set or no flag is passed, nothing
392 * is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200393 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200394void srv_set_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200395{
396 struct check *check = &s->check;
397 struct server *srv;
398 int xferred;
399
400 if (!mode)
401 return;
402
403 /* stop going down as soon as we meet a server already in the same state */
404 if (s->admin & mode)
405 return;
406
407 s->admin |= mode;
408
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200409 /* stop going down if the equivalent flag was already present (forced or inherited) */
410 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
411 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
412 return;
413
414 /* Maintenance must also disable health checks */
415 if (mode & SRV_ADMF_MAINT) {
416 if (s->check.state & CHK_ST_ENABLED) {
417 s->check.state |= CHK_ST_PAUSED;
418 check->health = 0;
419 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200420
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200421 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200422 chunk_printf(&trash,
423 "%sServer %s/%s was DOWN and now enters maintenance",
424 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
425
Willy Tarreaubda92272014-05-20 21:55:30 +0200426 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200427
428 Warning("%s.\n", trash.str);
429 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
430 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200431 else { /* server was still running */
432 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
433 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
434
435 check->health = 0; /* failure */
436 s->last_change = now.tv_sec;
437 s->state = SRV_ST_STOPPED;
438 if (s->proxy->lbprm.set_server_status_down)
439 s->proxy->lbprm.set_server_status_down(s);
440
441 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200442 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200443
Willy Tarreau87b09662015-04-03 00:22:06 +0200444 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200445 * a connection. Those which are redispatchable will be queued
446 * to another server or to the proxy itself.
447 */
448 xferred = pendconn_redistribute(s);
449
450 chunk_printf(&trash,
451 "%sServer %s/%s is going DOWN for maintenance",
452 s->flags & SRV_F_BACKUP ? "Backup " : "",
453 s->proxy->id, s->id);
454
455 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
456
457 Warning("%s.\n", trash.str);
458 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
459
460 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
461 set_backend_down(s->proxy);
462
463 s->counters.down_trans++;
464 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200465 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200466
467 /* drain state is applied only if not yet in maint */
468 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200469 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
470
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200471 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200472 if (s->proxy->lbprm.set_server_status_down)
473 s->proxy->lbprm.set_server_status_down(s);
474
Willy Tarreau87b09662015-04-03 00:22:06 +0200475 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200476 * a connection. Those which are redispatchable will be queued
477 * to another server or to the proxy itself.
478 */
479 xferred = pendconn_redistribute(s);
480
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200481 chunk_printf(&trash, "%sServer %s/%s enters drain state",
482 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200483
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200484 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200485
486 Warning("%s.\n", trash.str);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200487 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900488 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200489
490 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
491 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200492 }
493
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200494 /* compute the inherited flag to propagate */
495 if (mode & SRV_ADMF_MAINT)
496 mode = SRV_ADMF_IMAINT;
497 else if (mode & SRV_ADMF_DRAIN)
498 mode = SRV_ADMF_IDRAIN;
499
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200500 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200501 srv_set_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200502}
503
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200504/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
505 * stop enforcing either maint mode or drain mode. It is not allowed to set more
506 * than one flag at once. The equivalent "inherited" flag is propagated to all
507 * tracking servers. Leaving maintenance mode re-enables health checks. When
508 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200509 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200510void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200511{
512 struct check *check = &s->check;
513 struct server *srv;
514 int xferred = -1;
515
516 if (!mode)
517 return;
518
519 /* stop going down as soon as we see the flag is not there anymore */
520 if (!(s->admin & mode))
521 return;
522
523 s->admin &= ~mode;
524
525 if (s->admin & SRV_ADMF_MAINT) {
526 /* remaining in maintenance mode, let's inform precisely about the
527 * situation.
528 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200529 if (mode & SRV_ADMF_FMAINT) {
530 chunk_printf(&trash,
531 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
532 s->flags & SRV_F_BACKUP ? "Backup " : "",
533 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200534
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200535 if (s->track) /* normally it's mandatory here */
536 chunk_appendf(&trash, " via %s/%s",
537 s->track->proxy->id, s->track->id);
538 Warning("%s.\n", trash.str);
539 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
540 }
541 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200542 chunk_printf(&trash,
543 "%sServer %s/%s remains in forced maintenance",
544 s->flags & SRV_F_BACKUP ? "Backup " : "",
545 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200546 Warning("%s.\n", trash.str);
547 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
548 }
549 /* don't report anything when leaving drain mode and remaining in maintenance */
550 }
551 else if (mode & SRV_ADMF_MAINT) {
552 /* OK here we're leaving maintenance, we have many things to check,
553 * because the server might possibly be coming back up depending on
554 * its state. In practice, leaving maintenance means that we should
555 * immediately turn to UP (more or less the slowstart) under the
556 * following conditions :
557 * - server is neither checked nor tracked
558 * - server tracks another server which is not checked
559 * - server tracks another server which is already up
560 * Which sums up as something simpler :
561 * "either the tracking server is up or the server's checks are disabled
562 * or up". Otherwise we only re-enable health checks. There's a special
563 * case associated to the stopping state which can be inherited. Note
564 * that the server might still be in drain mode, which is naturally dealt
565 * with by the lower level functions.
566 */
567
568 if (s->check.state & CHK_ST_ENABLED) {
569 s->check.state &= ~CHK_ST_PAUSED;
570 check->health = check->rise; /* start OK but check immediately */
571 }
572
573 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
574 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
575 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
576 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
577 if (s->proxy->last_change < now.tv_sec) // ignore negative times
578 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
579 s->proxy->last_change = now.tv_sec;
580 }
581
582 if (s->last_change < now.tv_sec) // ignore negative times
583 s->down_time += now.tv_sec - s->last_change;
584 s->last_change = now.tv_sec;
585
586 if (s->track && s->track->state == SRV_ST_STOPPING)
587 s->state = SRV_ST_STOPPING;
588 else {
589 s->state = SRV_ST_STARTING;
590 if (s->slowstart > 0)
591 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
592 else
593 s->state = SRV_ST_RUNNING;
594 }
595
596 server_recalc_eweight(s);
597
598 /* If the server is set with "on-marked-up shutdown-backup-sessions",
599 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200600 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200601 * on all backup servers.
602 */
603 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
604 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200605 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200606
607 /* check if we can handle some connections queued at the proxy. We
608 * will take as many as we can handle.
609 */
610 xferred = pendconn_grab_from_px(s);
611 }
612
613 if (mode & SRV_ADMF_FMAINT) {
614 chunk_printf(&trash,
615 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
616 s->flags & SRV_F_BACKUP ? "Backup " : "",
617 s->proxy->id, s->id,
618 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
619 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200620 }
621 else {
622 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200623 "%sServer %s/%s is %s/%s (leaving maintenance)",
624 s->flags & SRV_F_BACKUP ? "Backup " : "",
625 s->proxy->id, s->id,
626 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
627 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
628 srv_append_status(&trash, s, NULL, xferred, 0);
629 }
630 Warning("%s.\n", trash.str);
631 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
632 }
633 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
634 /* remaining in drain mode after removing one of its flags */
635
636 if (mode & SRV_ADMF_FDRAIN) {
637 chunk_printf(&trash,
638 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200639 s->flags & SRV_F_BACKUP ? "Backup " : "",
640 s->proxy->id, s->id);
641
642 if (s->track) /* normally it's mandatory here */
643 chunk_appendf(&trash, " via %s/%s",
644 s->track->proxy->id, s->track->id);
645 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200646 else {
647 chunk_printf(&trash,
648 "%sServer %s/%s remains in forced drain mode",
649 s->flags & SRV_F_BACKUP ? "Backup " : "",
650 s->proxy->id, s->id);
651 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200652 Warning("%s.\n", trash.str);
653 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200654 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200655 else if (mode & SRV_ADMF_DRAIN) {
656 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200657 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
658 if (s->proxy->last_change < now.tv_sec) // ignore negative times
659 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
660 s->proxy->last_change = now.tv_sec;
661 }
662
663 if (s->last_change < now.tv_sec) // ignore negative times
664 s->down_time += now.tv_sec - s->last_change;
665 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200666 server_recalc_eweight(s);
667
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200668 if (mode & SRV_ADMF_FDRAIN) {
669 chunk_printf(&trash,
670 "%sServer %s/%s is %s (leaving forced drain)",
671 s->flags & SRV_F_BACKUP ? "Backup " : "",
672 s->proxy->id, s->id,
673 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
674 }
675 else {
676 chunk_printf(&trash,
677 "%sServer %s/%s is %s (leaving drain)",
678 s->flags & SRV_F_BACKUP ? "Backup " : "",
679 s->proxy->id, s->id,
680 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
681 if (s->track) /* normally it's mandatory here */
682 chunk_appendf(&trash, " via %s/%s",
683 s->track->proxy->id, s->track->id);
684 }
685 Warning("%s.\n", trash.str);
686 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200687 }
688
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200689 /* stop going down if the equivalent flag is still present (forced or inherited) */
690 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
691 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
692 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200693
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200694 if (mode & SRV_ADMF_MAINT)
695 mode = SRV_ADMF_IMAINT;
696 else if (mode & SRV_ADMF_DRAIN)
697 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200698
699 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200700 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200701}
702
Willy Tarreaudff55432012-10-10 17:51:05 +0200703/* Note: must not be declared <const> as its list will be overwritten.
704 * Please take care of keeping this list alphabetically sorted, doing so helps
705 * all code contributors.
706 * Optional keywords are also declared with a NULL ->parse() function so that
707 * the config parser can report an appropriate error when a known keyword was
708 * not enabled.
709 */
710static struct srv_kw_list srv_kws = { "ALL", { }, {
711 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
712 { NULL, NULL, 0 },
713}};
714
715__attribute__((constructor))
716static void __listener_init(void)
717{
718 srv_register_keywords(&srv_kws);
719}
720
Willy Tarreau004e0452013-11-21 11:22:01 +0100721/* Recomputes the server's eweight based on its state, uweight, the current time,
722 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
723 * state is automatically disabled if the time is elapsed.
724 */
725void server_recalc_eweight(struct server *sv)
726{
727 struct proxy *px = sv->proxy;
728 unsigned w;
729
730 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
731 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200732 if (sv->state == SRV_ST_STARTING)
733 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100734 }
735
736 /* We must take care of not pushing the server to full throttle during slow starts.
737 * It must also start immediately, at least at the minimal step when leaving maintenance.
738 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200739 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100740 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
741 else
742 w = px->lbprm.wdiv;
743
744 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
745
746 /* now propagate the status change to any LB algorithms */
747 if (px->lbprm.update_server_eweight)
748 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200749 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100750 if (px->lbprm.set_server_status_up)
751 px->lbprm.set_server_status_up(sv);
752 }
753 else {
754 if (px->lbprm.set_server_status_down)
755 px->lbprm.set_server_status_down(sv);
756 }
757}
758
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900760 * Parses weight_str and configures sv accordingly.
761 * Returns NULL on success, error message string otherwise.
762 */
763const char *server_parse_weight_change_request(struct server *sv,
764 const char *weight_str)
765{
766 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900767 long int w;
768 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900769
770 px = sv->proxy;
771
772 /* if the weight is terminated with '%', it is set relative to
773 * the initial weight, otherwise it is absolute.
774 */
775 if (!*weight_str)
776 return "Require <weight> or <weight%>.\n";
777
Simon Hormanb796afa2013-02-12 10:45:53 +0900778 w = strtol(weight_str, &end, 10);
779 if (end == weight_str)
780 return "Empty weight string empty or preceded by garbage";
781 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900782 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900783 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900784 /* Avoid integer overflow */
785 if (w > 25600)
786 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900787 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900788 if (w > 256)
789 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900790 }
791 else if (w < 0 || w > 256)
792 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900793 else if (end[0] != '\0')
794 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900795
796 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
797 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
798
799 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100800 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900801
802 return NULL;
803}
804
Willy Tarreau272adea2014-03-31 10:39:59 +0200805int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
806{
807 struct server *newsrv = NULL;
808 const char *err;
809 char *errmsg = NULL;
810 int err_code = 0;
811 unsigned val;
812
813 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
814 int cur_arg;
815 short realport = 0;
816 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
817
818 if (!defsrv && curproxy == defproxy) {
819 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
820 err_code |= ERR_ALERT | ERR_FATAL;
821 goto out;
822 }
823 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
824 err_code |= ERR_ALERT | ERR_FATAL;
825
826 if (!*args[2]) {
827 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
828 file, linenum, args[0]);
829 err_code |= ERR_ALERT | ERR_FATAL;
830 goto out;
831 }
832
833 err = invalid_char(args[1]);
834 if (err && !defsrv) {
835 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
836 file, linenum, *err, args[1]);
837 err_code |= ERR_ALERT | ERR_FATAL;
838 goto out;
839 }
840
841 if (!defsrv) {
842 struct sockaddr_storage *sk;
843 int port1, port2;
844 struct protocol *proto;
845
846 if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) {
847 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
848 err_code |= ERR_ALERT | ERR_ABORT;
849 goto out;
850 }
851
852 /* the servers are linked backwards first */
853 newsrv->next = curproxy->srv;
854 curproxy->srv = newsrv;
855 newsrv->proxy = curproxy;
856 newsrv->conf.file = strdup(file);
857 newsrv->conf.line = linenum;
858
859 newsrv->obj_type = OBJ_TYPE_SERVER;
860 LIST_INIT(&newsrv->actconns);
861 LIST_INIT(&newsrv->pendconns);
862 do_check = 0;
863 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200864 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +0200865 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200866 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +0200867 newsrv->last_change = now.tv_sec;
868 newsrv->id = strdup(args[1]);
869
870 /* several ways to check the port component :
871 * - IP => port=+0, relative (IPv4 only)
872 * - IP: => port=+0, relative
873 * - IP:N => port=N, absolute
874 * - IP:+N => port=+N, relative
875 * - IP:-N => port=-N, relative
876 */
877 sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL);
878 if (!sk) {
879 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
880 err_code |= ERR_ALERT | ERR_FATAL;
881 goto out;
882 }
883
884 proto = protocol_by_family(sk->ss_family);
885 if (!proto || !proto->connect) {
886 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
887 file, linenum, args[0], args[1]);
888 err_code |= ERR_ALERT | ERR_FATAL;
889 goto out;
890 }
891
892 if (!port1 || !port2) {
893 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200894 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +0200895 }
896 else if (port1 != port2) {
897 /* port range */
898 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
899 file, linenum, args[0], args[1], args[2]);
900 err_code |= ERR_ALERT | ERR_FATAL;
901 goto out;
902 }
903 else {
904 /* used by checks */
905 realport = port1;
906 }
907
908 newsrv->addr = *sk;
Cyril Bonté9ce13112014-11-15 22:41:27 +0100909 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock;
Willy Tarreau272adea2014-03-31 10:39:59 +0200910
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +0100911 if (!protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +0200912 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
913 file, linenum, newsrv->addr.ss_family, args[2]);
914 err_code |= ERR_ALERT | ERR_FATAL;
915 goto out;
916 }
917
918 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
919 newsrv->check.port = curproxy->defsrv.check.port;
920 newsrv->check.inter = curproxy->defsrv.check.inter;
921 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
922 newsrv->check.downinter = curproxy->defsrv.check.downinter;
923 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
924 newsrv->agent.port = curproxy->defsrv.agent.port;
925 newsrv->agent.inter = curproxy->defsrv.agent.inter;
926 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
927 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
928 newsrv->maxqueue = curproxy->defsrv.maxqueue;
929 newsrv->minconn = curproxy->defsrv.minconn;
930 newsrv->maxconn = curproxy->defsrv.maxconn;
931 newsrv->slowstart = curproxy->defsrv.slowstart;
932 newsrv->onerror = curproxy->defsrv.onerror;
933 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
934 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
935 newsrv->consecutive_errors_limit
936 = curproxy->defsrv.consecutive_errors_limit;
937#ifdef OPENSSL
938 newsrv->use_ssl = curproxy->defsrv.use_ssl;
939#endif
940 newsrv->uweight = newsrv->iweight
941 = curproxy->defsrv.iweight;
942
943 newsrv->check.status = HCHK_STATUS_INI;
944 newsrv->check.rise = curproxy->defsrv.check.rise;
945 newsrv->check.fall = curproxy->defsrv.check.fall;
946 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
947 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +0900948 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +0200949
950 newsrv->agent.status = HCHK_STATUS_INI;
951 newsrv->agent.rise = curproxy->defsrv.agent.rise;
952 newsrv->agent.fall = curproxy->defsrv.agent.fall;
953 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
954 newsrv->agent.server = newsrv;
955
956 cur_arg = 3;
957 } else {
958 newsrv = &curproxy->defsrv;
959 cur_arg = 1;
960 }
961
962 while (*args[cur_arg]) {
963 if (!strcmp(args[cur_arg], "agent-check")) {
964 global.maxsock++;
965 do_agent = 1;
966 cur_arg += 1;
967 } else if (!strcmp(args[cur_arg], "agent-inter")) {
968 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
969 if (err) {
970 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
971 file, linenum, *err, newsrv->id);
972 err_code |= ERR_ALERT | ERR_FATAL;
973 goto out;
974 }
975 if (val <= 0) {
976 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
977 file, linenum, val, args[cur_arg], newsrv->id);
978 err_code |= ERR_ALERT | ERR_FATAL;
979 goto out;
980 }
981 newsrv->agent.inter = val;
982 cur_arg += 2;
983 }
984 else if (!strcmp(args[cur_arg], "agent-port")) {
985 global.maxsock++;
986 newsrv->agent.port = atol(args[cur_arg + 1]);
987 cur_arg += 2;
988 }
989 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
990 newsrv->cookie = strdup(args[cur_arg + 1]);
991 newsrv->cklen = strlen(args[cur_arg + 1]);
992 cur_arg += 2;
993 }
994 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
995 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
996 newsrv->rdr_len = strlen(args[cur_arg + 1]);
997 cur_arg += 2;
998 }
999 else if (!strcmp(args[cur_arg], "rise")) {
1000 if (!*args[cur_arg + 1]) {
1001 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1002 file, linenum, args[cur_arg]);
1003 err_code |= ERR_ALERT | ERR_FATAL;
1004 goto out;
1005 }
1006
1007 newsrv->check.rise = atol(args[cur_arg + 1]);
1008 if (newsrv->check.rise <= 0) {
1009 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1010 file, linenum, args[cur_arg]);
1011 err_code |= ERR_ALERT | ERR_FATAL;
1012 goto out;
1013 }
1014
1015 if (newsrv->check.health)
1016 newsrv->check.health = newsrv->check.rise;
1017 cur_arg += 2;
1018 }
1019 else if (!strcmp(args[cur_arg], "fall")) {
1020 newsrv->check.fall = atol(args[cur_arg + 1]);
1021
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 if (newsrv->check.fall <= 0) {
1030 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1031 file, linenum, args[cur_arg]);
1032 err_code |= ERR_ALERT | ERR_FATAL;
1033 goto out;
1034 }
1035
1036 cur_arg += 2;
1037 }
1038 else if (!strcmp(args[cur_arg], "inter")) {
1039 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1040 if (err) {
1041 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1042 file, linenum, *err, newsrv->id);
1043 err_code |= ERR_ALERT | ERR_FATAL;
1044 goto out;
1045 }
1046 if (val <= 0) {
1047 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1048 file, linenum, val, args[cur_arg], newsrv->id);
1049 err_code |= ERR_ALERT | ERR_FATAL;
1050 goto out;
1051 }
1052 newsrv->check.inter = val;
1053 cur_arg += 2;
1054 }
1055 else if (!strcmp(args[cur_arg], "fastinter")) {
1056 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1057 if (err) {
1058 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1059 file, linenum, *err, newsrv->id);
1060 err_code |= ERR_ALERT | ERR_FATAL;
1061 goto out;
1062 }
1063 if (val <= 0) {
1064 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1065 file, linenum, val, args[cur_arg], newsrv->id);
1066 err_code |= ERR_ALERT | ERR_FATAL;
1067 goto out;
1068 }
1069 newsrv->check.fastinter = val;
1070 cur_arg += 2;
1071 }
1072 else if (!strcmp(args[cur_arg], "downinter")) {
1073 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1074 if (err) {
1075 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1076 file, linenum, *err, newsrv->id);
1077 err_code |= ERR_ALERT | ERR_FATAL;
1078 goto out;
1079 }
1080 if (val <= 0) {
1081 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1082 file, linenum, val, args[cur_arg], newsrv->id);
1083 err_code |= ERR_ALERT | ERR_FATAL;
1084 goto out;
1085 }
1086 newsrv->check.downinter = val;
1087 cur_arg += 2;
1088 }
1089 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1090 struct sockaddr_storage *sk;
1091 int port1, port2;
1092 struct protocol *proto;
1093
1094 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1095 if (!sk) {
1096 Alert("parsing [%s:%d] : '%s' : %s\n",
1097 file, linenum, args[cur_arg], errmsg);
1098 err_code |= ERR_ALERT | ERR_FATAL;
1099 goto out;
1100 }
1101
1102 proto = protocol_by_family(sk->ss_family);
1103 if (!proto || !proto->connect) {
1104 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1105 file, linenum, args[cur_arg], args[cur_arg + 1]);
1106 err_code |= ERR_ALERT | ERR_FATAL;
1107 goto out;
1108 }
1109
1110 if (port1 != port2) {
1111 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1112 file, linenum, args[cur_arg], args[cur_arg + 1]);
1113 err_code |= ERR_ALERT | ERR_FATAL;
1114 goto out;
1115 }
1116
Simon Horman41f58762015-01-30 11:22:56 +09001117 newsrv->check.addr = newsrv->agent.addr = *sk;
Willy Tarreau272adea2014-03-31 10:39:59 +02001118 cur_arg += 2;
1119 }
1120 else if (!strcmp(args[cur_arg], "port")) {
1121 newsrv->check.port = atol(args[cur_arg + 1]);
1122 cur_arg += 2;
1123 }
1124 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001125 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001126 cur_arg ++;
1127 }
1128 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001129 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001130 cur_arg ++;
1131 }
1132 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001133 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001134 cur_arg ++;
1135 }
David Safb76832014-05-08 23:42:08 -04001136 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1137 newsrv->pp_opts |= SRV_PP_V2;
1138 cur_arg ++;
1139 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001140 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1141 newsrv->check.send_proxy = 1;
1142 cur_arg ++;
1143 }
1144 else if (!strcmp(args[cur_arg], "weight")) {
1145 int w;
1146 w = atol(args[cur_arg + 1]);
1147 if (w < 0 || w > SRV_UWGHT_MAX) {
1148 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1149 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1150 err_code |= ERR_ALERT | ERR_FATAL;
1151 goto out;
1152 }
1153 newsrv->uweight = newsrv->iweight = w;
1154 cur_arg += 2;
1155 }
1156 else if (!strcmp(args[cur_arg], "minconn")) {
1157 newsrv->minconn = atol(args[cur_arg + 1]);
1158 cur_arg += 2;
1159 }
1160 else if (!strcmp(args[cur_arg], "maxconn")) {
1161 newsrv->maxconn = atol(args[cur_arg + 1]);
1162 cur_arg += 2;
1163 }
1164 else if (!strcmp(args[cur_arg], "maxqueue")) {
1165 newsrv->maxqueue = atol(args[cur_arg + 1]);
1166 cur_arg += 2;
1167 }
1168 else if (!strcmp(args[cur_arg], "slowstart")) {
1169 /* slowstart is stored in seconds */
1170 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1171 if (err) {
1172 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1173 file, linenum, *err, newsrv->id);
1174 err_code |= ERR_ALERT | ERR_FATAL;
1175 goto out;
1176 }
1177 newsrv->slowstart = (val + 999) / 1000;
1178 cur_arg += 2;
1179 }
1180 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1181
1182 if (!*args[cur_arg + 1]) {
1183 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1184 file, linenum);
1185 err_code |= ERR_ALERT | ERR_FATAL;
1186 goto out;
1187 }
1188
1189 newsrv->trackit = strdup(args[cur_arg + 1]);
1190
1191 cur_arg += 2;
1192 }
1193 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1194 global.maxsock++;
1195 do_check = 1;
1196 cur_arg += 1;
1197 }
1198 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Willy Tarreau20125212014-05-13 19:44:56 +02001199 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001200 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001201 newsrv->check.state |= CHK_ST_PAUSED;
1202 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001203 cur_arg += 1;
1204 }
1205 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1206 if (!strcmp(args[cur_arg + 1], "none"))
1207 newsrv->observe = HANA_OBS_NONE;
1208 else if (!strcmp(args[cur_arg + 1], "layer4"))
1209 newsrv->observe = HANA_OBS_LAYER4;
1210 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1211 if (curproxy->mode != PR_MODE_HTTP) {
1212 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1213 file, linenum, args[cur_arg + 1]);
1214 err_code |= ERR_ALERT;
1215 }
1216 newsrv->observe = HANA_OBS_LAYER7;
1217 }
1218 else {
1219 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1220 "'layer4', 'layer7' but got '%s'\n",
1221 file, linenum, args[cur_arg], args[cur_arg + 1]);
1222 err_code |= ERR_ALERT | ERR_FATAL;
1223 goto out;
1224 }
1225
1226 cur_arg += 2;
1227 }
1228 else if (!strcmp(args[cur_arg], "on-error")) {
1229 if (!strcmp(args[cur_arg + 1], "fastinter"))
1230 newsrv->onerror = HANA_ONERR_FASTINTER;
1231 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1232 newsrv->onerror = HANA_ONERR_FAILCHK;
1233 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1234 newsrv->onerror = HANA_ONERR_SUDDTH;
1235 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1236 newsrv->onerror = HANA_ONERR_MARKDWN;
1237 else {
1238 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1239 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1240 file, linenum, args[cur_arg], args[cur_arg + 1]);
1241 err_code |= ERR_ALERT | ERR_FATAL;
1242 goto out;
1243 }
1244
1245 cur_arg += 2;
1246 }
1247 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1248 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1249 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1250 else {
1251 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1252 file, linenum, args[cur_arg], args[cur_arg + 1]);
1253 err_code |= ERR_ALERT | ERR_FATAL;
1254 goto out;
1255 }
1256
1257 cur_arg += 2;
1258 }
1259 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1260 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1261 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1262 else {
1263 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1264 file, linenum, args[cur_arg], args[cur_arg + 1]);
1265 err_code |= ERR_ALERT | ERR_FATAL;
1266 goto out;
1267 }
1268
1269 cur_arg += 2;
1270 }
1271 else if (!strcmp(args[cur_arg], "error-limit")) {
1272 if (!*args[cur_arg + 1]) {
1273 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1274 file, linenum, args[cur_arg]);
1275 err_code |= ERR_ALERT | ERR_FATAL;
1276 goto out;
1277 }
1278
1279 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1280
1281 if (newsrv->consecutive_errors_limit <= 0) {
1282 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1283 file, linenum, args[cur_arg]);
1284 err_code |= ERR_ALERT | ERR_FATAL;
1285 goto out;
1286 }
1287 cur_arg += 2;
1288 }
1289 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1290 int port_low, port_high;
1291 struct sockaddr_storage *sk;
1292 struct protocol *proto;
1293
1294 if (!*args[cur_arg + 1]) {
1295 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1296 file, linenum, "source", "usesrc", "interface");
1297 err_code |= ERR_ALERT | ERR_FATAL;
1298 goto out;
1299 }
1300
1301 newsrv->conn_src.opts |= CO_SRC_BIND;
1302 sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL);
1303 if (!sk) {
1304 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1305 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1306 err_code |= ERR_ALERT | ERR_FATAL;
1307 goto out;
1308 }
1309
1310 proto = protocol_by_family(sk->ss_family);
1311 if (!proto || !proto->connect) {
1312 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1313 file, linenum, args[cur_arg], args[cur_arg+1]);
1314 err_code |= ERR_ALERT | ERR_FATAL;
1315 goto out;
1316 }
1317
1318 newsrv->conn_src.source_addr = *sk;
1319
1320 if (port_low != port_high) {
1321 int i;
1322
1323 if (!port_low || !port_high) {
1324 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1325 file, linenum, args[cur_arg], args[cur_arg + 1]);
1326 err_code |= ERR_ALERT | ERR_FATAL;
1327 goto out;
1328 }
1329
1330 if (port_low <= 0 || port_low > 65535 ||
1331 port_high <= 0 || port_high > 65535 ||
1332 port_low > port_high) {
1333 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1334 file, linenum, port_low, port_high);
1335 err_code |= ERR_ALERT | ERR_FATAL;
1336 goto out;
1337 }
1338 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1339 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1340 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1341 }
1342
1343 cur_arg += 2;
1344 while (*(args[cur_arg])) {
1345 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
1346#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
1347#if !defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +02001348 if (!is_inet_addr(&newsrv->conn_src.source_addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001349 Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n",
1350 file, linenum, "usesrc", "source");
1351 err_code |= ERR_ALERT | ERR_FATAL;
1352 goto out;
1353 }
1354#endif
1355 if (!*args[cur_arg + 1]) {
1356 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1357 file, linenum, "usesrc");
1358 err_code |= ERR_ALERT | ERR_FATAL;
1359 goto out;
1360 }
1361 if (!strcmp(args[cur_arg + 1], "client")) {
1362 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1363 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1364 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1365 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1366 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1367 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1368 char *name, *end;
1369
1370 name = args[cur_arg+1] + 7;
1371 while (isspace(*name))
1372 name++;
1373
1374 end = name;
1375 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1376 end++;
1377
1378 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1379 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1380 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1381 newsrv->conn_src.bind_hdr_len = end - name;
1382 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1383 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1384 newsrv->conn_src.bind_hdr_occ = -1;
1385
1386 /* now look for an occurrence number */
1387 while (isspace(*end))
1388 end++;
1389 if (*end == ',') {
1390 end++;
1391 name = end;
1392 if (*end == '-')
1393 end++;
1394 while (isdigit((int)*end))
1395 end++;
1396 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1397 }
1398
1399 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1400 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1401 " occurrences values smaller than %d.\n",
1402 file, linenum, MAX_HDR_HISTORY);
1403 err_code |= ERR_ALERT | ERR_FATAL;
1404 goto out;
1405 }
1406 } else {
1407 struct sockaddr_storage *sk;
1408 int port1, port2;
1409
1410 sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL);
1411 if (!sk) {
1412 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1413 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1414 err_code |= ERR_ALERT | ERR_FATAL;
1415 goto out;
1416 }
1417
1418 proto = protocol_by_family(sk->ss_family);
1419 if (!proto || !proto->connect) {
1420 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1421 file, linenum, args[cur_arg], args[cur_arg+1]);
1422 err_code |= ERR_ALERT | ERR_FATAL;
1423 goto out;
1424 }
1425
1426 if (port1 != port2) {
1427 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1428 file, linenum, args[cur_arg], args[cur_arg + 1]);
1429 err_code |= ERR_ALERT | ERR_FATAL;
1430 goto out;
1431 }
1432 newsrv->conn_src.tproxy_addr = *sk;
1433 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1434 }
1435 global.last_checks |= LSTCHK_NETADM;
1436#if !defined(CONFIG_HAP_TRANSPARENT)
1437 global.last_checks |= LSTCHK_CTTPROXY;
1438#endif
1439 cur_arg += 2;
1440 continue;
1441#else /* no TPROXY support */
1442 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1443 file, linenum, "usesrc");
1444 err_code |= ERR_ALERT | ERR_FATAL;
1445 goto out;
1446#endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */
1447 } /* "usesrc" */
1448
1449 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1450#ifdef SO_BINDTODEVICE
1451 if (!*args[cur_arg + 1]) {
1452 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1453 file, linenum, args[0]);
1454 err_code |= ERR_ALERT | ERR_FATAL;
1455 goto out;
1456 }
1457 free(newsrv->conn_src.iface_name);
1458 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1459 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1460 global.last_checks |= LSTCHK_NETADM;
1461#else
1462 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1463 file, linenum, args[0], args[cur_arg]);
1464 err_code |= ERR_ALERT | ERR_FATAL;
1465 goto out;
1466#endif
1467 cur_arg += 2;
1468 continue;
1469 }
1470 /* this keyword in not an option of "source" */
1471 break;
1472 } /* while */
1473 }
1474 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1475 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1476 file, linenum, "usesrc", "source");
1477 err_code |= ERR_ALERT | ERR_FATAL;
1478 goto out;
1479 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001480 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1481#ifdef CONFIG_HAP_NS
1482 char *arg = args[cur_arg + 1];
1483 if (!strcmp(arg, "*")) {
1484 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1485 } else {
1486 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1487
1488 if (newsrv->netns == NULL)
1489 newsrv->netns = netns_store_insert(arg);
1490
1491 if (newsrv->netns == NULL) {
1492 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1493 err_code |= ERR_ALERT | ERR_FATAL;
1494 goto out;
1495 }
1496 }
1497#else
1498 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1499 file, linenum, args[0], args[cur_arg]);
1500 err_code |= ERR_ALERT | ERR_FATAL;
1501 goto out;
1502#endif
1503 cur_arg += 2;
1504 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001505 else {
1506 static int srv_dumped;
1507 struct srv_kw *kw;
1508 char *err;
1509
1510 kw = srv_find_kw(args[cur_arg]);
1511 if (kw) {
1512 char *err = NULL;
1513 int code;
1514
1515 if (!kw->parse) {
1516 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1517 file, linenum, args[0], args[1], args[cur_arg]);
1518 cur_arg += 1 + kw->skip ;
1519 err_code |= ERR_ALERT | ERR_FATAL;
1520 goto out;
1521 }
1522
1523 if (defsrv && !kw->default_ok) {
1524 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1525 file, linenum, args[0], args[1], args[cur_arg]);
1526 cur_arg += 1 + kw->skip ;
1527 err_code |= ERR_ALERT;
1528 continue;
1529 }
1530
1531 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1532 err_code |= code;
1533
1534 if (code) {
1535 if (err && *err) {
1536 indent_msg(&err, 2);
1537 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1538 }
1539 else
1540 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1541 file, linenum, args[0], args[1], args[cur_arg]);
1542 if (code & ERR_FATAL) {
1543 free(err);
1544 cur_arg += 1 + kw->skip;
1545 goto out;
1546 }
1547 }
1548 free(err);
1549 cur_arg += 1 + kw->skip;
1550 continue;
1551 }
1552
1553 err = NULL;
1554 if (!srv_dumped) {
1555 srv_dump_kws(&err);
1556 indent_msg(&err, 4);
1557 srv_dumped = 1;
1558 }
1559
1560 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1561 file, linenum, args[0], args[1], args[cur_arg],
1562 err ? " Registered keywords :" : "", err ? err : "");
1563 free(err);
1564
1565 err_code |= ERR_ALERT | ERR_FATAL;
1566 goto out;
1567 }
1568 }
1569
Willy Tarreau272adea2014-03-31 10:39:59 +02001570 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001571 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001572
1573 if (newsrv->trackit) {
1574 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1575 file, linenum);
1576 err_code |= ERR_ALERT | ERR_FATAL;
1577 goto out;
1578 }
1579
1580 /* If neither a port nor an addr was specified and no check transport
1581 * layer is forced, then the transport layer used by the checks is the
1582 * same as for the production traffic. Otherwise we use raw_sock by
1583 * default, unless one is specified.
1584 */
Simon Horman41f58762015-01-30 11:22:56 +09001585 if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001586#ifdef USE_OPENSSL
1587 newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL));
1588#endif
David Safb76832014-05-08 23:42:08 -04001589 newsrv->check.send_proxy |= (newsrv->pp_opts);
Willy Tarreau272adea2014-03-31 10:39:59 +02001590 }
1591 /* try to get the port from check_core.addr if check.port not set */
1592 if (!newsrv->check.port)
Simon Horman41f58762015-01-30 11:22:56 +09001593 newsrv->check.port = get_host_port(&newsrv->check.addr);
Willy Tarreau272adea2014-03-31 10:39:59 +02001594
1595 if (!newsrv->check.port)
1596 newsrv->check.port = realport; /* by default */
1597
1598 if (!newsrv->check.port) {
1599 /* not yet valid, because no port was set on
1600 * the server either. We'll check if we have
1601 * a known port on the first listener.
1602 */
1603 struct listener *l;
1604
1605 list_for_each_entry(l, &curproxy->conf.listeners, by_fe) {
1606 newsrv->check.port = get_host_port(&l->addr);
1607 if (newsrv->check.port)
1608 break;
1609 }
1610 }
1611 /*
1612 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001613 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001614 */
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001615 if (!newsrv->check.port &&
Simon Horman41f58762015-01-30 11:22:56 +09001616 (is_inet_addr(&newsrv->check.addr) ||
1617 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001618 struct tcpcheck_rule *n = NULL, *r = NULL;
1619 struct list *l;
1620
1621 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1622 if (!r) {
1623 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1624 file, linenum, newsrv->id);
1625 err_code |= ERR_ALERT | ERR_FATAL;
1626 goto out;
1627 }
1628 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1629 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",
1630 file, linenum, newsrv->id);
1631 err_code |= ERR_ALERT | ERR_FATAL;
1632 goto out;
1633 }
1634 else {
1635 /* scan the tcp-check ruleset to ensure a port has been configured */
1636 l = &newsrv->proxy->tcpcheck_rules;
1637 list_for_each_entry(n, l, list) {
1638 r = (struct tcpcheck_rule *)n->list.p;
1639 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1640 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",
1641 file, linenum, newsrv->id);
1642 err_code |= ERR_ALERT | ERR_FATAL;
1643 goto out;
1644 }
1645 }
1646 }
1647 }
1648
1649 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001650 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001651 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001652 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1653 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001654 goto out;
1655 }
1656
1657 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1658 }
1659
1660 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001661 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001662
1663 if (!newsrv->agent.port) {
1664 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1665 file, linenum, newsrv->id);
1666 err_code |= ERR_ALERT | ERR_FATAL;
1667 goto out;
1668 }
1669
1670 if (!newsrv->agent.inter)
1671 newsrv->agent.inter = newsrv->check.inter;
1672
Simon Hormanb1900d52015-01-30 11:22:54 +09001673 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02001674 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001675 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1676 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001677 goto out;
1678 }
1679
1680 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1681 }
1682
1683 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001684 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02001685 curproxy->srv_bck++;
1686 else
1687 curproxy->srv_act++;
1688
Willy Tarreauc5150da2014-05-13 19:27:31 +02001689 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02001690 }
1691 }
1692 return 0;
1693
1694 out:
1695 free(errmsg);
1696 return err_code;
1697}
1698
Simon Horman7d09b9a2013-02-12 10:45:51 +09001699/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001700 * Local variables:
1701 * c-indent-level: 8
1702 * c-basic-offset: 8
1703 * End:
1704 */