blob: 5589723efb26c2655b5ab467900bf499c6f51fea [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>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020035#include <proto/port_range.h>
36#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020037#include <proto/queue.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020038#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020039#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010040#include <proto/stream_interface.h>
41#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020042#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020043#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020044
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020045static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010046static int srv_apply_lastaddr(struct server *srv, int *err_code);
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau21faa912012-10-10 08:27:36 +020048/* List head of all known server keywords */
49static struct srv_kw_list srv_keywords = {
50 .list = LIST_HEAD_INIT(srv_keywords.list)
51};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020052
Simon Hormana3608442013-11-01 16:46:15 +090053int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020054{
Willy Tarreau892337c2014-05-13 23:41:20 +020055 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020056 return s->down_time;
57
58 return now.tv_sec - s->last_change + s->down_time;
59}
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050061int srv_lastsession(const struct server *s)
62{
63 if (s->counters.last_sess)
64 return now.tv_sec - s->counters.last_sess;
65
66 return -1;
67}
68
Simon Horman4a741432013-02-23 15:35:38 +090069int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020070{
Simon Horman4a741432013-02-23 15:35:38 +090071 const struct server *s = check->server;
72
Willy Tarreauff5ae352013-12-11 20:36:34 +010073 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090074 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010075
Willy Tarreau892337c2014-05-13 23:41:20 +020076 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090077 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010078
Simon Horman4a741432013-02-23 15:35:38 +090079 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080}
81
Olivier Houchard4e694042017-03-14 20:01:29 +010082void srv_set_dyncookie(struct server *s)
83{
84 struct proxy *p = s->proxy;
85 struct server *tmpserv;
86 char *tmpbuf;
87 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010088 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010089 size_t buffer_len;
90 int addr_len;
91 int port;
92
93 if ((s->flags & SRV_F_COOKIESET) ||
94 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
95 s->proxy->dyncookie_key == NULL)
96 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010097 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +010098
99 if (s->addr.ss_family != AF_INET &&
100 s->addr.ss_family != AF_INET6)
101 return;
102 /*
103 * Buffer to calculate the cookie value.
104 * The buffer contains the secret key + the server IP address
105 * + the TCP port.
106 */
107 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
108 /*
109 * The TCP port should use only 2 bytes, but is stored in
110 * an unsigned int in struct server, so let's use 4, to be
111 * on the safe side.
112 */
113 buffer_len = key_len + addr_len + 4;
114 tmpbuf = trash.str;
115 memcpy(tmpbuf, p->dyncookie_key, key_len);
116 memcpy(&(tmpbuf[key_len]),
117 s->addr.ss_family == AF_INET ?
118 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
119 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
120 addr_len);
121 /*
122 * Make sure it's the same across all the load balancers,
123 * no matter their endianness.
124 */
125 port = htonl(s->svc_port);
126 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
127 hash_value = XXH64(tmpbuf, buffer_len, 0);
128 memprintf(&s->cookie, "%016llx", hash_value);
129 if (!s->cookie)
130 return;
131 s->cklen = 16;
132 /*
133 * Check that we did not get a hash collision.
134 * Unlikely, but it can happen.
135 */
136 for (p = proxy; p != NULL; p = p->next)
137 for (tmpserv = proxy->srv; tmpserv != NULL;
138 tmpserv = tmpserv->next) {
139 if (tmpserv == s)
140 continue;
141 if (tmpserv->cookie &&
142 strcmp(tmpserv->cookie, s->cookie) == 0) {
143 Warning("We generated two equal cookies for two different servers.\n"
144 "Please change the secret key for '%s'.\n",
145 s->proxy->id);
146 }
147 }
148}
149
Willy Tarreau21faa912012-10-10 08:27:36 +0200150/*
151 * Registers the server keyword list <kwl> as a list of valid keywords for next
152 * parsing sessions.
153 */
154void srv_register_keywords(struct srv_kw_list *kwl)
155{
156 LIST_ADDQ(&srv_keywords.list, &kwl->list);
157}
158
159/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
160 * keyword is found with a NULL ->parse() function, then an attempt is made to
161 * find one with a valid ->parse() function. This way it is possible to declare
162 * platform-dependant, known keywords as NULL, then only declare them as valid
163 * if some options are met. Note that if the requested keyword contains an
164 * opening parenthesis, everything from this point is ignored.
165 */
166struct srv_kw *srv_find_kw(const char *kw)
167{
168 int index;
169 const char *kwend;
170 struct srv_kw_list *kwl;
171 struct srv_kw *ret = NULL;
172
173 kwend = strchr(kw, '(');
174 if (!kwend)
175 kwend = kw + strlen(kw);
176
177 list_for_each_entry(kwl, &srv_keywords.list, list) {
178 for (index = 0; kwl->kw[index].kw != NULL; index++) {
179 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
180 kwl->kw[index].kw[kwend-kw] == 0) {
181 if (kwl->kw[index].parse)
182 return &kwl->kw[index]; /* found it !*/
183 else
184 ret = &kwl->kw[index]; /* may be OK */
185 }
186 }
187 }
188 return ret;
189}
190
191/* Dumps all registered "server" keywords to the <out> string pointer. The
192 * unsupported keywords are only dumped if their supported form was not
193 * found.
194 */
195void srv_dump_kws(char **out)
196{
197 struct srv_kw_list *kwl;
198 int index;
199
200 *out = NULL;
201 list_for_each_entry(kwl, &srv_keywords.list, list) {
202 for (index = 0; kwl->kw[index].kw != NULL; index++) {
203 if (kwl->kw[index].parse ||
204 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
205 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
206 kwl->scope,
207 kwl->kw[index].kw,
208 kwl->kw[index].skip ? " <arg>" : "",
209 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
210 kwl->kw[index].parse ? "" : " (not supported)");
211 }
212 }
213 }
214}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100215
Willy Tarreaudff55432012-10-10 17:51:05 +0200216/* parse the "id" server keyword */
217static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
218{
219 struct eb32_node *node;
220
221 if (!*args[*cur_arg + 1]) {
222 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
223 return ERR_ALERT | ERR_FATAL;
224 }
225
226 newsrv->puid = atol(args[*cur_arg + 1]);
227 newsrv->conf.id.key = newsrv->puid;
228
229 if (newsrv->puid <= 0) {
230 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
231 return ERR_ALERT | ERR_FATAL;
232 }
233
234 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
235 if (node) {
236 struct server *target = container_of(node, struct server, conf.id);
237 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
238 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
239 target->id);
240 return ERR_ALERT | ERR_FATAL;
241 }
242
243 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200244 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200245 return 0;
246}
247
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200248/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200249 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200250 * shutdown.
251 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200252void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200253{
Willy Tarreau87b09662015-04-03 00:22:06 +0200254 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200255
Willy Tarreau87b09662015-04-03 00:22:06 +0200256 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
257 if (stream->srv_conn == srv)
258 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200259}
260
261/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200262 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200263 * the reason for the shutdown.
264 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200265void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200266{
267 struct server *srv;
268
269 for (srv = px->srv; srv != NULL; srv = srv->next)
270 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200271 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200272}
273
Willy Tarreaubda92272014-05-20 21:55:30 +0200274/* Appends some information to a message string related to a server going UP or
275 * DOWN. If both <forced> and <reason> are null and the server tracks another
276 * one, a "via" information will be provided to know where the status came from.
277 * If <reason> is non-null, the entire string will be appended after a comma and
278 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200279 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200280 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200281 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200282void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200283{
Willy Tarreaubda92272014-05-20 21:55:30 +0200284 if (reason)
285 chunk_appendf(msg, ", %s", reason);
286 else if (!forced && s->track)
287 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200288
289 if (xferred >= 0) {
290 if (s->state == SRV_ST_STOPPED)
291 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
292 " %d sessions active, %d requeued, %d remaining in queue",
293 s->proxy->srv_act, s->proxy->srv_bck,
294 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
295 s->cur_sess, xferred, s->nbpend);
296 else
297 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
298 " %d sessions requeued, %d total in queue",
299 s->proxy->srv_act, s->proxy->srv_bck,
300 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
301 xferred, s->nbpend);
302 }
303}
304
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200305/* Marks server <s> down, regardless of its checks' statuses, notifies by all
306 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200307 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200308 * recomputes the number of servers, but not the map. Maintenance servers are
309 * ignored. It reports <reason> if non-null as the reason for going down. Note
310 * that it makes use of the trash to build the log strings, so <reason> must
311 * not be placed there.
312 */
313void srv_set_stopped(struct server *s, const char *reason)
314{
315 struct server *srv;
316 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
317 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900318 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200319 int xferred;
320
321 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
322 return;
323
324 s->last_change = now.tv_sec;
325 s->state = SRV_ST_STOPPED;
326 if (s->proxy->lbprm.set_server_status_down)
327 s->proxy->lbprm.set_server_status_down(s);
328
329 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200330 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200331
Willy Tarreau87b09662015-04-03 00:22:06 +0200332 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200333 * a connection. Those which are redispatchable will be queued
334 * to another server or to the proxy itself.
335 */
336 xferred = pendconn_redistribute(s);
337
338 chunk_printf(&trash,
339 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
340 s->proxy->id, s->id);
341
342 srv_append_status(&trash, s, reason, xferred, 0);
343 Warning("%s.\n", trash.str);
344
345 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900346 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
347 send_log(s->proxy, log_level, "%s.\n", trash.str);
348 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200349
350 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
351 set_backend_down(s->proxy);
352
353 s->counters.down_trans++;
354
355 for (srv = s->trackers; srv; srv = srv->tracknext)
356 srv_set_stopped(srv, NULL);
357}
358
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200359/* Marks server <s> up regardless of its checks' statuses and provided it isn't
360 * in maintenance. Notifies by all available means, recounts the remaining
361 * servers on the proxy and tries to grab requests from the proxy. It
362 * automatically recomputes the number of servers, but not the map. Maintenance
363 * servers are ignored. It reports <reason> if non-null as the reason for going
364 * up. Note that it makes use of the trash to build the log strings, so <reason>
365 * must not be placed there.
366 */
367void srv_set_running(struct server *s, const char *reason)
368{
369 struct server *srv;
370 int xferred;
371
372 if (s->admin & SRV_ADMF_MAINT)
373 return;
374
375 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
376 return;
377
378 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
379 if (s->proxy->last_change < now.tv_sec) // ignore negative times
380 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
381 s->proxy->last_change = now.tv_sec;
382 }
383
384 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
385 s->down_time += now.tv_sec - s->last_change;
386
387 s->last_change = now.tv_sec;
388
389 s->state = SRV_ST_STARTING;
390 if (s->slowstart > 0)
391 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
392 else
393 s->state = SRV_ST_RUNNING;
394
395 server_recalc_eweight(s);
396
397 /* If the server is set with "on-marked-up shutdown-backup-sessions",
398 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200399 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200400 * on all backup servers.
401 */
402 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
403 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200404 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200405
406 /* check if we can handle some connections queued at the proxy. We
407 * will take as many as we can handle.
408 */
409 xferred = pendconn_grab_from_px(s);
410
411 chunk_printf(&trash,
412 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
413 s->proxy->id, s->id);
414
415 srv_append_status(&trash, s, reason, xferred, 0);
416 Warning("%s.\n", trash.str);
417 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900418 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200419
420 for (srv = s->trackers; srv; srv = srv->tracknext)
421 srv_set_running(srv, NULL);
422}
423
Willy Tarreau8eb77842014-05-21 13:54:57 +0200424/* Marks server <s> stopping regardless of its checks' statuses and provided it
425 * isn't in maintenance. Notifies by all available means, recounts the remaining
426 * servers on the proxy and tries to grab requests from the proxy. It
427 * automatically recomputes the number of servers, but not the map. Maintenance
428 * servers are ignored. It reports <reason> if non-null as the reason for going
429 * up. Note that it makes use of the trash to build the log strings, so <reason>
430 * must not be placed there.
431 */
432void srv_set_stopping(struct server *s, const char *reason)
433{
434 struct server *srv;
435 int xferred;
436
437 if (s->admin & SRV_ADMF_MAINT)
438 return;
439
440 if (s->state == SRV_ST_STOPPING)
441 return;
442
443 s->last_change = now.tv_sec;
444 s->state = SRV_ST_STOPPING;
445 if (s->proxy->lbprm.set_server_status_down)
446 s->proxy->lbprm.set_server_status_down(s);
447
Willy Tarreau87b09662015-04-03 00:22:06 +0200448 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200449 * a connection. Those which are redispatchable will be queued
450 * to another server or to the proxy itself.
451 */
452 xferred = pendconn_redistribute(s);
453
454 chunk_printf(&trash,
455 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
456 s->proxy->id, s->id);
457
458 srv_append_status(&trash, s, reason, xferred, 0);
459
460 Warning("%s.\n", trash.str);
461 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
462
463 if (!s->proxy->srv_bck && !s->proxy->srv_act)
464 set_backend_down(s->proxy);
465
466 for (srv = s->trackers; srv; srv = srv->tracknext)
467 srv_set_stopping(srv, NULL);
468}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200469
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200470/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
471 * enforce either maint mode or drain mode. It is not allowed to set more than
472 * one flag at once. The equivalent "inherited" flag is propagated to all
473 * tracking servers. Maintenance mode disables health checks (but not agent
474 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100475 * is done. If <cause> is non-null, it will be displayed at the end of the log
476 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200477 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100478void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200479{
480 struct check *check = &s->check;
481 struct server *srv;
482 int xferred;
483
484 if (!mode)
485 return;
486
487 /* stop going down as soon as we meet a server already in the same state */
488 if (s->admin & mode)
489 return;
490
491 s->admin |= mode;
492
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200493 /* stop going down if the equivalent flag was already present (forced or inherited) */
494 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
495 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
496 return;
497
498 /* Maintenance must also disable health checks */
499 if (mode & SRV_ADMF_MAINT) {
500 if (s->check.state & CHK_ST_ENABLED) {
501 s->check.state |= CHK_ST_PAUSED;
502 check->health = 0;
503 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200504
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200505 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200506 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100507 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
508 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
509 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200510
Willy Tarreaubda92272014-05-20 21:55:30 +0200511 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200512
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100513 if (!(global.mode & MODE_STARTING)) {
514 Warning("%s.\n", trash.str);
515 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
516 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200517 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200518 else { /* server was still running */
519 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
520 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
521
522 check->health = 0; /* failure */
523 s->last_change = now.tv_sec;
524 s->state = SRV_ST_STOPPED;
525 if (s->proxy->lbprm.set_server_status_down)
526 s->proxy->lbprm.set_server_status_down(s);
527
528 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200529 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200530
Willy Tarreau87b09662015-04-03 00:22:06 +0200531 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200532 * a connection. Those which are redispatchable will be queued
533 * to another server or to the proxy itself.
534 */
535 xferred = pendconn_redistribute(s);
536
537 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100538 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200539 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +0100540 s->proxy->id, s->id,
541 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200542
543 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
544
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100545 if (!(global.mode & MODE_STARTING)) {
546 Warning("%s.\n", trash.str);
547 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
548 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200549
550 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
551 set_backend_down(s->proxy);
552
553 s->counters.down_trans++;
554 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200555 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200556
557 /* drain state is applied only if not yet in maint */
558 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200559 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
560
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200561 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200562 if (s->proxy->lbprm.set_server_status_down)
563 s->proxy->lbprm.set_server_status_down(s);
564
Willy Tarreau87b09662015-04-03 00:22:06 +0200565 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200566 * a connection. Those which are redispatchable will be queued
567 * to another server or to the proxy itself.
568 */
569 xferred = pendconn_redistribute(s);
570
Willy Tarreau8b428482016-11-07 15:53:43 +0100571 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
572 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
573 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200574
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200575 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200576
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100577 if (!(global.mode & MODE_STARTING)) {
578 Warning("%s.\n", trash.str);
579 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
580 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
581 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200582 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
583 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200584 }
585
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200586 /* compute the inherited flag to propagate */
587 if (mode & SRV_ADMF_MAINT)
588 mode = SRV_ADMF_IMAINT;
589 else if (mode & SRV_ADMF_DRAIN)
590 mode = SRV_ADMF_IDRAIN;
591
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200592 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +0100593 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200594}
595
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200596/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
597 * stop enforcing either maint mode or drain mode. It is not allowed to set more
598 * than one flag at once. The equivalent "inherited" flag is propagated to all
599 * tracking servers. Leaving maintenance mode re-enables health checks. When
600 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200601 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200602void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200603{
604 struct check *check = &s->check;
605 struct server *srv;
606 int xferred = -1;
607
608 if (!mode)
609 return;
610
611 /* stop going down as soon as we see the flag is not there anymore */
612 if (!(s->admin & mode))
613 return;
614
615 s->admin &= ~mode;
616
617 if (s->admin & SRV_ADMF_MAINT) {
618 /* remaining in maintenance mode, let's inform precisely about the
619 * situation.
620 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200621 if (mode & SRV_ADMF_FMAINT) {
622 chunk_printf(&trash,
623 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
624 s->flags & SRV_F_BACKUP ? "Backup " : "",
625 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200626
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200627 if (s->track) /* normally it's mandatory here */
628 chunk_appendf(&trash, " via %s/%s",
629 s->track->proxy->id, s->track->id);
630 Warning("%s.\n", trash.str);
631 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
632 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100633 if (mode & SRV_ADMF_RMAINT) {
634 chunk_printf(&trash,
635 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
636 s->flags & SRV_F_BACKUP ? "Backup " : "",
637 s->proxy->id, s->id, s->hostname);
638
639 if (s->track) /* normally it's mandatory here */
640 chunk_appendf(&trash, " via %s/%s",
641 s->track->proxy->id, s->track->id);
642 Warning("%s.\n", trash.str);
643 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
644 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200645 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200646 chunk_printf(&trash,
647 "%sServer %s/%s remains in forced maintenance",
648 s->flags & SRV_F_BACKUP ? "Backup " : "",
649 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200650 Warning("%s.\n", trash.str);
651 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
652 }
653 /* don't report anything when leaving drain mode and remaining in maintenance */
654 }
655 else if (mode & SRV_ADMF_MAINT) {
656 /* OK here we're leaving maintenance, we have many things to check,
657 * because the server might possibly be coming back up depending on
658 * its state. In practice, leaving maintenance means that we should
659 * immediately turn to UP (more or less the slowstart) under the
660 * following conditions :
661 * - server is neither checked nor tracked
662 * - server tracks another server which is not checked
663 * - server tracks another server which is already up
664 * Which sums up as something simpler :
665 * "either the tracking server is up or the server's checks are disabled
666 * or up". Otherwise we only re-enable health checks. There's a special
667 * case associated to the stopping state which can be inherited. Note
668 * that the server might still be in drain mode, which is naturally dealt
669 * with by the lower level functions.
670 */
671
672 if (s->check.state & CHK_ST_ENABLED) {
673 s->check.state &= ~CHK_ST_PAUSED;
674 check->health = check->rise; /* start OK but check immediately */
675 }
676
677 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
678 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
679 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
680 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
681 if (s->proxy->last_change < now.tv_sec) // ignore negative times
682 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
683 s->proxy->last_change = now.tv_sec;
684 }
685
686 if (s->last_change < now.tv_sec) // ignore negative times
687 s->down_time += now.tv_sec - s->last_change;
688 s->last_change = now.tv_sec;
689
690 if (s->track && s->track->state == SRV_ST_STOPPING)
691 s->state = SRV_ST_STOPPING;
692 else {
693 s->state = SRV_ST_STARTING;
694 if (s->slowstart > 0)
695 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
696 else
697 s->state = SRV_ST_RUNNING;
698 }
699
700 server_recalc_eweight(s);
701
702 /* If the server is set with "on-marked-up shutdown-backup-sessions",
703 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200704 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200705 * on all backup servers.
706 */
707 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
708 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200709 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200710
711 /* check if we can handle some connections queued at the proxy. We
712 * will take as many as we can handle.
713 */
714 xferred = pendconn_grab_from_px(s);
715 }
716
717 if (mode & SRV_ADMF_FMAINT) {
718 chunk_printf(&trash,
719 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
720 s->flags & SRV_F_BACKUP ? "Backup " : "",
721 s->proxy->id, s->id,
722 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
723 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200724 }
Willy Tarreaue6599732016-11-07 15:42:33 +0100725 else if (mode & SRV_ADMF_RMAINT) {
726 chunk_printf(&trash,
727 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
728 s->flags & SRV_F_BACKUP ? "Backup " : "",
729 s->proxy->id, s->id, s->hostname,
730 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
731 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
732 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200733 else {
734 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200735 "%sServer %s/%s is %s/%s (leaving maintenance)",
736 s->flags & SRV_F_BACKUP ? "Backup " : "",
737 s->proxy->id, s->id,
738 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
739 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
740 srv_append_status(&trash, s, NULL, xferred, 0);
741 }
742 Warning("%s.\n", trash.str);
743 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
744 }
745 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
746 /* remaining in drain mode after removing one of its flags */
747
748 if (mode & SRV_ADMF_FDRAIN) {
749 chunk_printf(&trash,
750 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200751 s->flags & SRV_F_BACKUP ? "Backup " : "",
752 s->proxy->id, s->id);
753
754 if (s->track) /* normally it's mandatory here */
755 chunk_appendf(&trash, " via %s/%s",
756 s->track->proxy->id, s->track->id);
757 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200758 else {
759 chunk_printf(&trash,
760 "%sServer %s/%s remains in forced drain mode",
761 s->flags & SRV_F_BACKUP ? "Backup " : "",
762 s->proxy->id, s->id);
763 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200764 Warning("%s.\n", trash.str);
765 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200766 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200767 else if (mode & SRV_ADMF_DRAIN) {
768 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200769 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
770 if (s->proxy->last_change < now.tv_sec) // ignore negative times
771 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
772 s->proxy->last_change = now.tv_sec;
773 }
774
775 if (s->last_change < now.tv_sec) // ignore negative times
776 s->down_time += now.tv_sec - s->last_change;
777 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200778 server_recalc_eweight(s);
779
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200780 if (mode & SRV_ADMF_FDRAIN) {
781 chunk_printf(&trash,
782 "%sServer %s/%s is %s (leaving forced drain)",
783 s->flags & SRV_F_BACKUP ? "Backup " : "",
784 s->proxy->id, s->id,
785 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
786 }
787 else {
788 chunk_printf(&trash,
789 "%sServer %s/%s is %s (leaving drain)",
790 s->flags & SRV_F_BACKUP ? "Backup " : "",
791 s->proxy->id, s->id,
792 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
793 if (s->track) /* normally it's mandatory here */
794 chunk_appendf(&trash, " via %s/%s",
795 s->track->proxy->id, s->track->id);
796 }
797 Warning("%s.\n", trash.str);
798 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200799 }
800
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200801 /* stop going down if the equivalent flag is still present (forced or inherited) */
802 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
803 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
804 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200805
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200806 if (mode & SRV_ADMF_MAINT)
807 mode = SRV_ADMF_IMAINT;
808 else if (mode & SRV_ADMF_DRAIN)
809 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200810
811 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200812 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200813}
814
Willy Tarreau757478e2016-11-03 19:22:19 +0100815/* principle: propagate maint and drain to tracking servers. This is useful
816 * upon startup so that inherited states are correct.
817 */
818static void srv_propagate_admin_state(struct server *srv)
819{
820 struct server *srv2;
821
822 if (!srv->trackers)
823 return;
824
825 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
826 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +0100827 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100828
829 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +0100830 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100831 }
832}
833
834/* Compute and propagate the admin states for all servers in proxy <px>.
835 * Only servers *not* tracking another one are considered, because other
836 * ones will be handled when the server they track is visited.
837 */
838void srv_compute_all_admin_states(struct proxy *px)
839{
840 struct server *srv;
841
842 for (srv = px->srv; srv; srv = srv->next) {
843 if (srv->track)
844 continue;
845 srv_propagate_admin_state(srv);
846 }
847}
848
Willy Tarreaudff55432012-10-10 17:51:05 +0200849/* Note: must not be declared <const> as its list will be overwritten.
850 * Please take care of keeping this list alphabetically sorted, doing so helps
851 * all code contributors.
852 * Optional keywords are also declared with a NULL ->parse() function so that
853 * the config parser can report an appropriate error when a known keyword was
854 * not enabled.
855 */
856static struct srv_kw_list srv_kws = { "ALL", { }, {
857 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
858 { NULL, NULL, 0 },
859}};
860
861__attribute__((constructor))
862static void __listener_init(void)
863{
864 srv_register_keywords(&srv_kws);
865}
866
Willy Tarreau004e0452013-11-21 11:22:01 +0100867/* Recomputes the server's eweight based on its state, uweight, the current time,
868 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
869 * state is automatically disabled if the time is elapsed.
870 */
871void server_recalc_eweight(struct server *sv)
872{
873 struct proxy *px = sv->proxy;
874 unsigned w;
875
876 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
877 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +0200878 if (sv->state == SRV_ST_STARTING)
879 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +0100880 }
881
882 /* We must take care of not pushing the server to full throttle during slow starts.
883 * It must also start immediately, at least at the minimal step when leaving maintenance.
884 */
Willy Tarreau892337c2014-05-13 23:41:20 +0200885 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +0100886 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
887 else
888 w = px->lbprm.wdiv;
889
890 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
891
892 /* now propagate the status change to any LB algorithms */
893 if (px->lbprm.update_server_eweight)
894 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +0200895 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100896 if (px->lbprm.set_server_status_up)
897 px->lbprm.set_server_status_up(sv);
898 }
899 else {
900 if (px->lbprm.set_server_status_down)
901 px->lbprm.set_server_status_down(sv);
902 }
903}
904
Willy Tarreaubaaee002006-06-26 02:48:02 +0200905/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900906 * Parses weight_str and configures sv accordingly.
907 * Returns NULL on success, error message string otherwise.
908 */
909const char *server_parse_weight_change_request(struct server *sv,
910 const char *weight_str)
911{
912 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900913 long int w;
914 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900915
916 px = sv->proxy;
917
918 /* if the weight is terminated with '%', it is set relative to
919 * the initial weight, otherwise it is absolute.
920 */
921 if (!*weight_str)
922 return "Require <weight> or <weight%>.\n";
923
Simon Hormanb796afa2013-02-12 10:45:53 +0900924 w = strtol(weight_str, &end, 10);
925 if (end == weight_str)
926 return "Empty weight string empty or preceded by garbage";
927 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900928 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900929 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900930 /* Avoid integer overflow */
931 if (w > 25600)
932 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900933 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900934 if (w > 256)
935 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900936 }
937 else if (w < 0 || w > 256)
938 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900939 else if (end[0] != '\0')
940 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900941
942 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
943 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
944
945 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +0100946 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900947
948 return NULL;
949}
950
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200951/*
Thierry Fournier09a91782016-02-24 08:25:39 +0100952 * Parses <addr_str> and configures <sv> accordingly. <from> precise
953 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200954 * Returns:
955 * - error string on error
956 * - NULL on success
957 */
958const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +0100959 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200960{
961 unsigned char ip[INET6_ADDRSTRLEN];
962
963 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100964 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200965 return NULL;
966 }
967 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +0100968 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200969 return NULL;
970 }
971
972 return "Could not understand IP address format.\n";
973}
974
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200975const char *server_parse_maxconn_change_request(struct server *sv,
976 const char *maxconn_str)
977{
978 long int v;
979 char *end;
980
981 if (!*maxconn_str)
982 return "Require <maxconn>.\n";
983
984 v = strtol(maxconn_str, &end, 10);
985 if (end == maxconn_str)
986 return "maxconn string empty or preceded by garbage";
987 else if (end[0] != '\0')
988 return "Trailing garbage in maxconn string";
989
990 if (sv->maxconn == sv->minconn) { // static maxconn
991 sv->maxconn = sv->minconn = v;
992 } else { // dynamic maxconn
993 sv->maxconn = v;
994 }
995
996 if (may_dequeue_tasks(sv, sv->proxy))
997 process_srv_queue(sv);
998
999 return NULL;
1000}
1001
Willy Tarreau272adea2014-03-31 10:39:59 +02001002int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1003{
1004 struct server *newsrv = NULL;
1005 const char *err;
1006 char *errmsg = NULL;
1007 int err_code = 0;
1008 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001009 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001010
1011 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
1012 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02001013 int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd');
1014
1015 if (!defsrv && curproxy == defproxy) {
1016 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1017 err_code |= ERR_ALERT | ERR_FATAL;
1018 goto out;
1019 }
1020 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1021 err_code |= ERR_ALERT | ERR_FATAL;
1022
1023 if (!*args[2]) {
1024 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1025 file, linenum, args[0]);
1026 err_code |= ERR_ALERT | ERR_FATAL;
1027 goto out;
1028 }
1029
1030 err = invalid_char(args[1]);
1031 if (err && !defsrv) {
1032 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1033 file, linenum, *err, args[1]);
1034 err_code |= ERR_ALERT | ERR_FATAL;
1035 goto out;
1036 }
1037
1038 if (!defsrv) {
1039 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001040 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001041 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001042 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02001043
Vincent Bernat02779b62016-04-03 13:48:43 +02001044 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001045 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1046 err_code |= ERR_ALERT | ERR_ABORT;
1047 goto out;
1048 }
1049
1050 /* the servers are linked backwards first */
1051 newsrv->next = curproxy->srv;
1052 curproxy->srv = newsrv;
1053 newsrv->proxy = curproxy;
1054 newsrv->conf.file = strdup(file);
1055 newsrv->conf.line = linenum;
1056
1057 newsrv->obj_type = OBJ_TYPE_SERVER;
1058 LIST_INIT(&newsrv->actconns);
1059 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02001060 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02001061 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02001062 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +02001063 do_check = 0;
1064 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +02001065 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +02001066 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +02001067 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +02001068 newsrv->last_change = now.tv_sec;
1069 newsrv->id = strdup(args[1]);
1070
1071 /* several ways to check the port component :
1072 * - IP => port=+0, relative (IPv4 only)
1073 * - IP: => port=+0, relative
1074 * - IP:N => port=N, absolute
1075 * - IP:+N => port=+N, relative
1076 * - IP:-N => port=-N, relative
1077 */
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001078 sk = str2sa_range(args[2], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001079 if (!sk) {
1080 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1081 err_code |= ERR_ALERT | ERR_FATAL;
1082 goto out;
1083 }
1084
1085 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001086 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001087 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1088 file, linenum, args[0], args[1]);
1089 err_code |= ERR_ALERT | ERR_FATAL;
1090 goto out;
1091 }
1092
1093 if (!port1 || !port2) {
1094 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001095 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001096 }
1097 else if (port1 != port2) {
1098 /* port range */
1099 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1100 file, linenum, args[0], args[1], args[2]);
1101 err_code |= ERR_ALERT | ERR_FATAL;
1102 goto out;
1103 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001104
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001105 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001106 newsrv->hostname = fqdn;
1107 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001108 goto skip_name_resolution;
1109
Willy Tarreau07101d52015-09-08 16:16:35 +02001110 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001111 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001112 goto skip_name_resolution;
1113
1114 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1115 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1116 goto skip_name_resolution;
1117 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1118 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1119 file, linenum, args[2]);
1120 err_code |= ERR_ALERT | ERR_FATAL;
1121 goto out;
1122 }
1123
1124 curr_resolution->requester = newsrv;
1125 curr_resolution->requester_cb = snr_resolution_cb;
1126 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1127 curr_resolution->status = RSLV_STATUS_NONE;
1128 curr_resolution->step = RSLV_STEP_NONE;
1129 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001130 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001131 newsrv->resolution = curr_resolution;
1132
1133 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001134 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001135 newsrv->svc_port = port;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001136 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = xprt_get(XPRT_RAW);
Willy Tarreau272adea2014-03-31 10:39:59 +02001137
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001138 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001139 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1140 file, linenum, newsrv->addr.ss_family, args[2]);
1141 err_code |= ERR_ALERT | ERR_FATAL;
1142 goto out;
1143 }
1144
Willy Tarreau141ad852016-12-22 18:38:00 +01001145 newsrv->use_ssl = curproxy->defsrv.use_ssl;
Willy Tarreau272adea2014-03-31 10:39:59 +02001146 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1147 newsrv->check.port = curproxy->defsrv.check.port;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001148 if (newsrv->check.port)
1149 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001150 newsrv->check.inter = curproxy->defsrv.check.inter;
1151 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1152 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1153 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1154 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001155 if (curproxy->defsrv.agent.send_string != NULL)
1156 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1157 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001158 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1159 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1160 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1161 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1162 newsrv->minconn = curproxy->defsrv.minconn;
1163 newsrv->maxconn = curproxy->defsrv.maxconn;
1164 newsrv->slowstart = curproxy->defsrv.slowstart;
1165 newsrv->onerror = curproxy->defsrv.onerror;
1166 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1167 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
1168 newsrv->consecutive_errors_limit
1169 = curproxy->defsrv.consecutive_errors_limit;
Willy Tarreau272adea2014-03-31 10:39:59 +02001170 newsrv->uweight = newsrv->iweight
1171 = curproxy->defsrv.iweight;
1172
1173 newsrv->check.status = HCHK_STATUS_INI;
1174 newsrv->check.rise = curproxy->defsrv.check.rise;
1175 newsrv->check.fall = curproxy->defsrv.check.fall;
1176 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1177 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001178 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001179
1180 newsrv->agent.status = HCHK_STATUS_INI;
1181 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1182 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1183 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1184 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001185 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1186 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1187 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001188 memcpy(newsrv->dns_opts.pref_net,
1189 curproxy->defsrv.dns_opts.pref_net,
1190 sizeof(newsrv->dns_opts.pref_net));
1191 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001192 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1193 newsrv->init_addr = curproxy->defsrv.init_addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001194
1195 cur_arg = 3;
1196 } else {
1197 newsrv = &curproxy->defsrv;
1198 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001199 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001200 }
1201
1202 while (*args[cur_arg]) {
1203 if (!strcmp(args[cur_arg], "agent-check")) {
1204 global.maxsock++;
1205 do_agent = 1;
1206 cur_arg += 1;
1207 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1208 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1209 if (err) {
1210 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1211 file, linenum, *err, newsrv->id);
1212 err_code |= ERR_ALERT | ERR_FATAL;
1213 goto out;
1214 }
1215 if (val <= 0) {
1216 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1217 file, linenum, val, args[cur_arg], newsrv->id);
1218 err_code |= ERR_ALERT | ERR_FATAL;
1219 goto out;
1220 }
1221 newsrv->agent.inter = val;
1222 cur_arg += 2;
1223 }
Misiekea849332017-01-09 09:39:51 +01001224 else if (!strcmp(args[cur_arg], "agent-addr")) {
1225 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1226 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1227 goto out;
1228 }
1229
1230 cur_arg += 2;
1231 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001232 else if (!strcmp(args[cur_arg], "agent-port")) {
1233 global.maxsock++;
1234 newsrv->agent.port = atol(args[cur_arg + 1]);
1235 cur_arg += 2;
1236 }
James Brown55f9ff12015-10-21 18:19:05 -07001237 else if (!strcmp(args[cur_arg], "agent-send")) {
1238 global.maxsock++;
1239 free(newsrv->agent.send_string);
1240 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1241 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1242 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1243 cur_arg += 2;
1244 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001245 else if (!defsrv && !strcmp(args[cur_arg], "cookie")) {
1246 newsrv->cookie = strdup(args[cur_arg + 1]);
1247 newsrv->cklen = strlen(args[cur_arg + 1]);
Olivier Houchard4e694042017-03-14 20:01:29 +01001248 newsrv->flags |= SRV_F_COOKIESET;
Willy Tarreau272adea2014-03-31 10:39:59 +02001249 cur_arg += 2;
1250 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001251 else if (!strcmp(args[cur_arg], "init-addr")) {
1252 char *p, *end;
1253 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001254 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001255
1256 newsrv->init_addr_methods = 0;
1257 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1258
1259 for (p = args[cur_arg + 1]; *p; p = end) {
1260 /* cut on next comma */
1261 for (end = p; *end && *end != ','; end++);
1262 if (*end)
1263 *(end++) = 0;
1264
Willy Tarreau4310d362016-11-02 15:05:56 +01001265 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001266 if (!strcmp(p, "libc")) {
1267 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1268 }
1269 else if (!strcmp(p, "last")) {
1270 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1271 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001272 else if (!strcmp(p, "none")) {
1273 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1274 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001275 else if (str2ip2(p, &sa, 0)) {
1276 if (is_addr(&newsrv->init_addr)) {
1277 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1278 file, linenum, args[cur_arg], p);
1279 err_code |= ERR_ALERT | ERR_FATAL;
1280 goto out;
1281 }
1282 newsrv->init_addr = sa;
1283 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1284 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001285 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001286 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001287 file, linenum, args[cur_arg], p);
1288 err_code |= ERR_ALERT | ERR_FATAL;
1289 goto out;
1290 }
1291 if (!done) {
1292 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1293 file, linenum, args[cur_arg], p);
1294 err_code |= ERR_ALERT | ERR_FATAL;
1295 goto out;
1296 }
1297 }
1298 cur_arg += 2;
1299 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001300 else if (!defsrv && !strcmp(args[cur_arg], "redir")) {
1301 newsrv->rdr_pfx = strdup(args[cur_arg + 1]);
1302 newsrv->rdr_len = strlen(args[cur_arg + 1]);
1303 cur_arg += 2;
1304 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001305 else if (!strcmp(args[cur_arg], "resolvers")) {
1306 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1307 cur_arg += 2;
1308 }
1309 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1310 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001311 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001312 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001313 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001314 else {
1315 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1316 file, linenum, args[cur_arg]);
1317 err_code |= ERR_ALERT | ERR_FATAL;
1318 goto out;
1319 }
1320 cur_arg += 2;
1321 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001322 else if (!strcmp(args[cur_arg], "resolve-net")) {
1323 char *p, *e;
1324 unsigned char mask;
1325 struct dns_options *opt;
1326
1327 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1328 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1329 file, linenum, args[cur_arg]);
1330 err_code |= ERR_ALERT | ERR_FATAL;
1331 goto out;
1332 }
1333
1334 opt = &newsrv->dns_opts;
1335
1336 /* Split arguments by comma, and convert it from ipv4 or ipv6
1337 * string network in in_addr or in6_addr.
1338 */
1339 p = args[cur_arg + 1];
1340 e = p;
1341 while (*p != '\0') {
1342 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001343 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001344 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1345 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1346 err_code |= ERR_ALERT | ERR_FATAL;
1347 goto out;
1348 }
1349 /* look for end or comma. */
1350 while (*e != ',' && *e != '\0')
1351 e++;
1352 if (*e == ',') {
1353 *e = '\0';
1354 e++;
1355 }
1356 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1357 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1358 /* Try to convert input string from ipv4 or ipv6 network. */
1359 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1360 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1361 &mask)) {
1362 /* Try to convert input string from ipv6 network. */
1363 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1364 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1365 } else {
1366 /* All network conversions fail, retrun error. */
1367 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1368 file, linenum, args[cur_arg], p);
1369 err_code |= ERR_ALERT | ERR_FATAL;
1370 goto out;
1371 }
1372 opt->pref_net_nb++;
1373 p = e;
1374 }
1375
1376 cur_arg += 2;
1377 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001378 else if (!strcmp(args[cur_arg], "rise")) {
1379 if (!*args[cur_arg + 1]) {
1380 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1381 file, linenum, args[cur_arg]);
1382 err_code |= ERR_ALERT | ERR_FATAL;
1383 goto out;
1384 }
1385
1386 newsrv->check.rise = atol(args[cur_arg + 1]);
1387 if (newsrv->check.rise <= 0) {
1388 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1389 file, linenum, args[cur_arg]);
1390 err_code |= ERR_ALERT | ERR_FATAL;
1391 goto out;
1392 }
1393
1394 if (newsrv->check.health)
1395 newsrv->check.health = newsrv->check.rise;
1396 cur_arg += 2;
1397 }
1398 else if (!strcmp(args[cur_arg], "fall")) {
1399 newsrv->check.fall = atol(args[cur_arg + 1]);
1400
1401 if (!*args[cur_arg + 1]) {
1402 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1403 file, linenum, args[cur_arg]);
1404 err_code |= ERR_ALERT | ERR_FATAL;
1405 goto out;
1406 }
1407
1408 if (newsrv->check.fall <= 0) {
1409 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1410 file, linenum, args[cur_arg]);
1411 err_code |= ERR_ALERT | ERR_FATAL;
1412 goto out;
1413 }
1414
1415 cur_arg += 2;
1416 }
1417 else if (!strcmp(args[cur_arg], "inter")) {
1418 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1419 if (err) {
1420 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
1421 file, linenum, *err, newsrv->id);
1422 err_code |= ERR_ALERT | ERR_FATAL;
1423 goto out;
1424 }
1425 if (val <= 0) {
1426 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1427 file, linenum, val, args[cur_arg], newsrv->id);
1428 err_code |= ERR_ALERT | ERR_FATAL;
1429 goto out;
1430 }
1431 newsrv->check.inter = val;
1432 cur_arg += 2;
1433 }
1434 else if (!strcmp(args[cur_arg], "fastinter")) {
1435 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1436 if (err) {
1437 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
1438 file, linenum, *err, newsrv->id);
1439 err_code |= ERR_ALERT | ERR_FATAL;
1440 goto out;
1441 }
1442 if (val <= 0) {
1443 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1444 file, linenum, val, args[cur_arg], newsrv->id);
1445 err_code |= ERR_ALERT | ERR_FATAL;
1446 goto out;
1447 }
1448 newsrv->check.fastinter = val;
1449 cur_arg += 2;
1450 }
1451 else if (!strcmp(args[cur_arg], "downinter")) {
1452 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1453 if (err) {
1454 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
1455 file, linenum, *err, newsrv->id);
1456 err_code |= ERR_ALERT | ERR_FATAL;
1457 goto out;
1458 }
1459 if (val <= 0) {
1460 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1461 file, linenum, val, args[cur_arg], newsrv->id);
1462 err_code |= ERR_ALERT | ERR_FATAL;
1463 goto out;
1464 }
1465 newsrv->check.downinter = val;
1466 cur_arg += 2;
1467 }
1468 else if (!defsrv && !strcmp(args[cur_arg], "addr")) {
1469 struct sockaddr_storage *sk;
1470 int port1, port2;
1471 struct protocol *proto;
1472
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001473 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001474 if (!sk) {
1475 Alert("parsing [%s:%d] : '%s' : %s\n",
1476 file, linenum, args[cur_arg], errmsg);
1477 err_code |= ERR_ALERT | ERR_FATAL;
1478 goto out;
1479 }
1480
1481 proto = protocol_by_family(sk->ss_family);
1482 if (!proto || !proto->connect) {
1483 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1484 file, linenum, args[cur_arg], args[cur_arg + 1]);
1485 err_code |= ERR_ALERT | ERR_FATAL;
1486 goto out;
1487 }
1488
1489 if (port1 != port2) {
1490 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1491 file, linenum, args[cur_arg], args[cur_arg + 1]);
1492 err_code |= ERR_ALERT | ERR_FATAL;
1493 goto out;
1494 }
1495
Simon Horman41f58762015-01-30 11:22:56 +09001496 newsrv->check.addr = newsrv->agent.addr = *sk;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001497 newsrv->flags |= SRV_F_CHECKADDR;
1498 newsrv->flags |= SRV_F_AGENTADDR;
Willy Tarreau272adea2014-03-31 10:39:59 +02001499 cur_arg += 2;
1500 }
1501 else if (!strcmp(args[cur_arg], "port")) {
1502 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001503 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001504 cur_arg += 2;
1505 }
1506 else if (!defsrv && !strcmp(args[cur_arg], "backup")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001507 newsrv->flags |= SRV_F_BACKUP;
Willy Tarreau272adea2014-03-31 10:39:59 +02001508 cur_arg ++;
1509 }
1510 else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02001511 newsrv->flags |= SRV_F_NON_STICK;
Willy Tarreau272adea2014-03-31 10:39:59 +02001512 cur_arg ++;
1513 }
1514 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) {
David Safb76832014-05-08 23:42:08 -04001515 newsrv->pp_opts |= SRV_PP_V1;
Willy Tarreau272adea2014-03-31 10:39:59 +02001516 cur_arg ++;
1517 }
David Safb76832014-05-08 23:42:08 -04001518 else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) {
1519 newsrv->pp_opts |= SRV_PP_V2;
1520 cur_arg ++;
1521 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001522 else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) {
1523 newsrv->check.send_proxy = 1;
1524 cur_arg ++;
1525 }
1526 else if (!strcmp(args[cur_arg], "weight")) {
1527 int w;
1528 w = atol(args[cur_arg + 1]);
1529 if (w < 0 || w > SRV_UWGHT_MAX) {
1530 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
1531 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
1532 err_code |= ERR_ALERT | ERR_FATAL;
1533 goto out;
1534 }
1535 newsrv->uweight = newsrv->iweight = w;
1536 cur_arg += 2;
1537 }
1538 else if (!strcmp(args[cur_arg], "minconn")) {
1539 newsrv->minconn = atol(args[cur_arg + 1]);
1540 cur_arg += 2;
1541 }
1542 else if (!strcmp(args[cur_arg], "maxconn")) {
1543 newsrv->maxconn = atol(args[cur_arg + 1]);
1544 cur_arg += 2;
1545 }
1546 else if (!strcmp(args[cur_arg], "maxqueue")) {
1547 newsrv->maxqueue = atol(args[cur_arg + 1]);
1548 cur_arg += 2;
1549 }
1550 else if (!strcmp(args[cur_arg], "slowstart")) {
1551 /* slowstart is stored in seconds */
1552 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1553 if (err) {
1554 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
1555 file, linenum, *err, newsrv->id);
1556 err_code |= ERR_ALERT | ERR_FATAL;
1557 goto out;
1558 }
1559 newsrv->slowstart = (val + 999) / 1000;
1560 cur_arg += 2;
1561 }
1562 else if (!defsrv && !strcmp(args[cur_arg], "track")) {
1563
1564 if (!*args[cur_arg + 1]) {
1565 Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n",
1566 file, linenum);
1567 err_code |= ERR_ALERT | ERR_FATAL;
1568 goto out;
1569 }
1570
1571 newsrv->trackit = strdup(args[cur_arg + 1]);
1572
1573 cur_arg += 2;
1574 }
1575 else if (!defsrv && !strcmp(args[cur_arg], "check")) {
1576 global.maxsock++;
1577 do_check = 1;
1578 cur_arg += 1;
1579 }
1580 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02001581 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02001582 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02001583 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02001584 newsrv->check.state |= CHK_ST_PAUSED;
1585 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001586 cur_arg += 1;
1587 }
1588 else if (!defsrv && !strcmp(args[cur_arg], "observe")) {
1589 if (!strcmp(args[cur_arg + 1], "none"))
1590 newsrv->observe = HANA_OBS_NONE;
1591 else if (!strcmp(args[cur_arg + 1], "layer4"))
1592 newsrv->observe = HANA_OBS_LAYER4;
1593 else if (!strcmp(args[cur_arg + 1], "layer7")) {
1594 if (curproxy->mode != PR_MODE_HTTP) {
1595 Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n",
1596 file, linenum, args[cur_arg + 1]);
1597 err_code |= ERR_ALERT;
1598 }
1599 newsrv->observe = HANA_OBS_LAYER7;
1600 }
1601 else {
1602 Alert("parsing [%s:%d]: '%s' expects one of 'none', "
1603 "'layer4', 'layer7' but got '%s'\n",
1604 file, linenum, args[cur_arg], args[cur_arg + 1]);
1605 err_code |= ERR_ALERT | ERR_FATAL;
1606 goto out;
1607 }
1608
1609 cur_arg += 2;
1610 }
1611 else if (!strcmp(args[cur_arg], "on-error")) {
1612 if (!strcmp(args[cur_arg + 1], "fastinter"))
1613 newsrv->onerror = HANA_ONERR_FASTINTER;
1614 else if (!strcmp(args[cur_arg + 1], "fail-check"))
1615 newsrv->onerror = HANA_ONERR_FAILCHK;
1616 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
1617 newsrv->onerror = HANA_ONERR_SUDDTH;
1618 else if (!strcmp(args[cur_arg + 1], "mark-down"))
1619 newsrv->onerror = HANA_ONERR_MARKDWN;
1620 else {
1621 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
1622 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
1623 file, linenum, args[cur_arg], args[cur_arg + 1]);
1624 err_code |= ERR_ALERT | ERR_FATAL;
1625 goto out;
1626 }
1627
1628 cur_arg += 2;
1629 }
1630 else if (!strcmp(args[cur_arg], "on-marked-down")) {
1631 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
1632 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1633 else {
1634 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
1635 file, linenum, args[cur_arg], args[cur_arg + 1]);
1636 err_code |= ERR_ALERT | ERR_FATAL;
1637 goto out;
1638 }
1639
1640 cur_arg += 2;
1641 }
1642 else if (!strcmp(args[cur_arg], "on-marked-up")) {
1643 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
1644 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1645 else {
1646 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
1647 file, linenum, args[cur_arg], args[cur_arg + 1]);
1648 err_code |= ERR_ALERT | ERR_FATAL;
1649 goto out;
1650 }
1651
1652 cur_arg += 2;
1653 }
1654 else if (!strcmp(args[cur_arg], "error-limit")) {
1655 if (!*args[cur_arg + 1]) {
1656 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1657 file, linenum, args[cur_arg]);
1658 err_code |= ERR_ALERT | ERR_FATAL;
1659 goto out;
1660 }
1661
1662 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
1663
1664 if (newsrv->consecutive_errors_limit <= 0) {
1665 Alert("parsing [%s:%d]: %s has to be > 0.\n",
1666 file, linenum, args[cur_arg]);
1667 err_code |= ERR_ALERT | ERR_FATAL;
1668 goto out;
1669 }
1670 cur_arg += 2;
1671 }
1672 else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */
1673 int port_low, port_high;
1674 struct sockaddr_storage *sk;
1675 struct protocol *proto;
1676
1677 if (!*args[cur_arg + 1]) {
1678 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n",
1679 file, linenum, "source", "usesrc", "interface");
1680 err_code |= ERR_ALERT | ERR_FATAL;
1681 goto out;
1682 }
1683
1684 newsrv->conn_src.opts |= CO_SRC_BIND;
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001685 sk = str2sa_range(args[cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001686 if (!sk) {
1687 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1688 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1689 err_code |= ERR_ALERT | ERR_FATAL;
1690 goto out;
1691 }
1692
1693 proto = protocol_by_family(sk->ss_family);
1694 if (!proto || !proto->connect) {
1695 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1696 file, linenum, args[cur_arg], args[cur_arg+1]);
1697 err_code |= ERR_ALERT | ERR_FATAL;
1698 goto out;
1699 }
1700
1701 newsrv->conn_src.source_addr = *sk;
1702
1703 if (port_low != port_high) {
1704 int i;
1705
1706 if (!port_low || !port_high) {
1707 Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n",
1708 file, linenum, args[cur_arg], args[cur_arg + 1]);
1709 err_code |= ERR_ALERT | ERR_FATAL;
1710 goto out;
1711 }
1712
1713 if (port_low <= 0 || port_low > 65535 ||
1714 port_high <= 0 || port_high > 65535 ||
1715 port_low > port_high) {
1716 Alert("parsing [%s:%d] : invalid source port range %d-%d.\n",
1717 file, linenum, port_low, port_high);
1718 err_code |= ERR_ALERT | ERR_FATAL;
1719 goto out;
1720 }
1721 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1722 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1723 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1724 }
1725
1726 cur_arg += 2;
1727 while (*(args[cur_arg])) {
1728 if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */
Willy Tarreau29fbe512015-08-20 19:35:14 +02001729#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau272adea2014-03-31 10:39:59 +02001730 if (!*args[cur_arg + 1]) {
1731 Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n",
1732 file, linenum, "usesrc");
1733 err_code |= ERR_ALERT | ERR_FATAL;
1734 goto out;
1735 }
1736 if (!strcmp(args[cur_arg + 1], "client")) {
1737 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1738 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1739 } else if (!strcmp(args[cur_arg + 1], "clientip")) {
1740 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1741 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1742 } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) {
1743 char *name, *end;
1744
1745 name = args[cur_arg+1] + 7;
1746 while (isspace(*name))
1747 name++;
1748
1749 end = name;
1750 while (*end && !isspace(*end) && *end != ',' && *end != ')')
1751 end++;
1752
1753 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1754 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1755 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1756 newsrv->conn_src.bind_hdr_len = end - name;
1757 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1758 newsrv->conn_src.bind_hdr_name[end-name] = '\0';
1759 newsrv->conn_src.bind_hdr_occ = -1;
1760
1761 /* now look for an occurrence number */
1762 while (isspace(*end))
1763 end++;
1764 if (*end == ',') {
1765 end++;
1766 name = end;
1767 if (*end == '-')
1768 end++;
1769 while (isdigit((int)*end))
1770 end++;
1771 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name);
1772 }
1773
1774 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1775 Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative"
1776 " occurrences values smaller than %d.\n",
1777 file, linenum, MAX_HDR_HISTORY);
1778 err_code |= ERR_ALERT | ERR_FATAL;
1779 goto out;
1780 }
1781 } else {
1782 struct sockaddr_storage *sk;
1783 int port1, port2;
1784
Willy Tarreau48ef4c92017-01-06 18:32:38 +01001785 sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
Willy Tarreau272adea2014-03-31 10:39:59 +02001786 if (!sk) {
1787 Alert("parsing [%s:%d] : '%s %s' : %s\n",
1788 file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
1789 err_code |= ERR_ALERT | ERR_FATAL;
1790 goto out;
1791 }
1792
1793 proto = protocol_by_family(sk->ss_family);
1794 if (!proto || !proto->connect) {
1795 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1796 file, linenum, args[cur_arg], args[cur_arg+1]);
1797 err_code |= ERR_ALERT | ERR_FATAL;
1798 goto out;
1799 }
1800
1801 if (port1 != port2) {
1802 Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n",
1803 file, linenum, args[cur_arg], args[cur_arg + 1]);
1804 err_code |= ERR_ALERT | ERR_FATAL;
1805 goto out;
1806 }
1807 newsrv->conn_src.tproxy_addr = *sk;
1808 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1809 }
1810 global.last_checks |= LSTCHK_NETADM;
Willy Tarreau272adea2014-03-31 10:39:59 +02001811 cur_arg += 2;
1812 continue;
1813#else /* no TPROXY support */
1814 Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n",
1815 file, linenum, "usesrc");
1816 err_code |= ERR_ALERT | ERR_FATAL;
1817 goto out;
Willy Tarreau29fbe512015-08-20 19:35:14 +02001818#endif /* defined(CONFIG_HAP_TRANSPARENT) */
Willy Tarreau272adea2014-03-31 10:39:59 +02001819 } /* "usesrc" */
1820
1821 if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */
1822#ifdef SO_BINDTODEVICE
1823 if (!*args[cur_arg + 1]) {
1824 Alert("parsing [%s:%d] : '%s' : missing interface name.\n",
1825 file, linenum, args[0]);
1826 err_code |= ERR_ALERT | ERR_FATAL;
1827 goto out;
1828 }
1829 free(newsrv->conn_src.iface_name);
1830 newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]);
1831 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1832 global.last_checks |= LSTCHK_NETADM;
1833#else
1834 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1835 file, linenum, args[0], args[cur_arg]);
1836 err_code |= ERR_ALERT | ERR_FATAL;
1837 goto out;
1838#endif
1839 cur_arg += 2;
1840 continue;
1841 }
1842 /* this keyword in not an option of "source" */
1843 break;
1844 } /* while */
1845 }
1846 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
1847 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
1848 file, linenum, "usesrc", "source");
1849 err_code |= ERR_ALERT | ERR_FATAL;
1850 goto out;
1851 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001852 else if (!defsrv && !strcmp(args[cur_arg], "namespace")) {
1853#ifdef CONFIG_HAP_NS
1854 char *arg = args[cur_arg + 1];
1855 if (!strcmp(arg, "*")) {
1856 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1857 } else {
1858 newsrv->netns = netns_store_lookup(arg, strlen(arg));
1859
1860 if (newsrv->netns == NULL)
1861 newsrv->netns = netns_store_insert(arg);
1862
1863 if (newsrv->netns == NULL) {
1864 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1865 err_code |= ERR_ALERT | ERR_FATAL;
1866 goto out;
1867 }
1868 }
1869#else
1870 Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n",
1871 file, linenum, args[0], args[cur_arg]);
1872 err_code |= ERR_ALERT | ERR_FATAL;
1873 goto out;
1874#endif
1875 cur_arg += 2;
1876 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001877 else {
1878 static int srv_dumped;
1879 struct srv_kw *kw;
1880 char *err;
1881
1882 kw = srv_find_kw(args[cur_arg]);
1883 if (kw) {
1884 char *err = NULL;
1885 int code;
1886
1887 if (!kw->parse) {
1888 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
1889 file, linenum, args[0], args[1], args[cur_arg]);
1890 cur_arg += 1 + kw->skip ;
1891 err_code |= ERR_ALERT | ERR_FATAL;
1892 goto out;
1893 }
1894
1895 if (defsrv && !kw->default_ok) {
1896 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
1897 file, linenum, args[0], args[1], args[cur_arg]);
1898 cur_arg += 1 + kw->skip ;
1899 err_code |= ERR_ALERT;
1900 continue;
1901 }
1902
1903 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
1904 err_code |= code;
1905
1906 if (code) {
1907 if (err && *err) {
1908 indent_msg(&err, 2);
1909 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err);
1910 }
1911 else
1912 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1913 file, linenum, args[0], args[1], args[cur_arg]);
1914 if (code & ERR_FATAL) {
1915 free(err);
1916 cur_arg += 1 + kw->skip;
1917 goto out;
1918 }
1919 }
1920 free(err);
1921 cur_arg += 1 + kw->skip;
1922 continue;
1923 }
1924
1925 err = NULL;
1926 if (!srv_dumped) {
1927 srv_dump_kws(&err);
1928 indent_msg(&err, 4);
1929 srv_dumped = 1;
1930 }
1931
1932 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
1933 file, linenum, args[0], args[1], args[cur_arg],
1934 err ? " Registered keywords :" : "", err ? err : "");
1935 free(err);
1936
1937 err_code |= ERR_ALERT | ERR_FATAL;
1938 goto out;
1939 }
1940 }
1941
Willy Tarreau272adea2014-03-31 10:39:59 +02001942 if (do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001943 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02001944
1945 if (newsrv->trackit) {
1946 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1947 file, linenum);
1948 err_code |= ERR_ALERT | ERR_FATAL;
1949 goto out;
1950 }
1951
Willy Tarreau272adea2014-03-31 10:39:59 +02001952 /*
1953 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02001954 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02001955 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001956 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09001957 (is_inet_addr(&newsrv->check.addr) ||
1958 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01001959 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001960 struct list *l;
1961
1962 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
1963 if (!r) {
1964 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
1965 file, linenum, newsrv->id);
1966 err_code |= ERR_ALERT | ERR_FATAL;
1967 goto out;
1968 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001969 /* search the first action (connect / send / expect) in the list */
1970 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001971 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01001972 if (r->action != TCPCHK_ACT_COMMENT)
1973 break;
1974 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001975 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1976 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",
1977 file, linenum, newsrv->id);
1978 err_code |= ERR_ALERT | ERR_FATAL;
1979 goto out;
1980 }
1981 else {
1982 /* scan the tcp-check ruleset to ensure a port has been configured */
1983 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01001984 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1986 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",
1987 file, linenum, newsrv->id);
1988 err_code |= ERR_ALERT | ERR_FATAL;
1989 goto out;
1990 }
1991 }
1992 }
1993 }
1994
1995 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09001996 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001997 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09001998 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1999 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002000 goto out;
2001 }
2002
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002003 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01002004 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002005
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2007 }
2008
2009 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002010 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002011
2012 if (!newsrv->agent.port) {
2013 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2014 file, linenum, newsrv->id);
2015 err_code |= ERR_ALERT | ERR_FATAL;
2016 goto out;
2017 }
2018
2019 if (!newsrv->agent.inter)
2020 newsrv->agent.inter = newsrv->check.inter;
2021
Simon Hormanb1900d52015-01-30 11:22:54 +09002022 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02002023 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002024 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2025 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002026 goto out;
2027 }
2028
2029 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2030 }
2031
2032 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02002033 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02002034 curproxy->srv_bck++;
2035 else
2036 curproxy->srv_act++;
2037
Willy Tarreauc5150da2014-05-13 19:27:31 +02002038 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002039 }
2040 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002041 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002042 return 0;
2043
2044 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002045 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002046 free(errmsg);
2047 return err_code;
2048}
2049
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002050/* Returns a pointer to the first server matching either id <id>.
2051 * NULL is returned if no match is found.
2052 * the lookup is performed in the backend <bk>
2053 */
2054struct server *server_find_by_id(struct proxy *bk, int id)
2055{
2056 struct eb32_node *eb32;
2057 struct server *curserver;
2058
2059 if (!bk || (id ==0))
2060 return NULL;
2061
2062 /* <bk> has no backend capabilities, so it can't have a server */
2063 if (!(bk->cap & PR_CAP_BE))
2064 return NULL;
2065
2066 curserver = NULL;
2067
2068 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2069 if (eb32)
2070 curserver = container_of(eb32, struct server, conf.id);
2071
2072 return curserver;
2073}
2074
2075/* Returns a pointer to the first server matching either name <name>, or id
2076 * if <name> starts with a '#'. NULL is returned if no match is found.
2077 * the lookup is performed in the backend <bk>
2078 */
2079struct server *server_find_by_name(struct proxy *bk, const char *name)
2080{
2081 struct server *curserver;
2082
2083 if (!bk || !name)
2084 return NULL;
2085
2086 /* <bk> has no backend capabilities, so it can't have a server */
2087 if (!(bk->cap & PR_CAP_BE))
2088 return NULL;
2089
2090 curserver = NULL;
2091 if (*name == '#') {
2092 curserver = server_find_by_id(bk, atoi(name + 1));
2093 if (curserver)
2094 return curserver;
2095 }
2096 else {
2097 curserver = bk->srv;
2098
2099 while (curserver && (strcmp(curserver->id, name) != 0))
2100 curserver = curserver->next;
2101
2102 if (curserver)
2103 return curserver;
2104 }
2105
2106 return NULL;
2107}
2108
2109struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2110{
2111 struct server *byname;
2112 struct server *byid;
2113
2114 if (!name && !id)
2115 return NULL;
2116
2117 if (diff)
2118 *diff = 0;
2119
2120 byname = byid = NULL;
2121
2122 if (name) {
2123 byname = server_find_by_name(bk, name);
2124 if (byname && (!id || byname->puid == id))
2125 return byname;
2126 }
2127
2128 /* remaining possibilities :
2129 * - name not set
2130 * - name set but not found
2131 * - name found but ID doesn't match
2132 */
2133 if (id) {
2134 byid = server_find_by_id(bk, id);
2135 if (byid) {
2136 if (byname) {
2137 /* use id only if forced by configuration */
2138 if (byid->flags & SRV_F_FORCED_ID) {
2139 if (diff)
2140 *diff |= 2;
2141 return byid;
2142 }
2143 else {
2144 if (diff)
2145 *diff |= 1;
2146 return byname;
2147 }
2148 }
2149
2150 /* remaining possibilities:
2151 * - name not set
2152 * - name set but not found
2153 */
2154 if (name && diff)
2155 *diff |= 2;
2156 return byid;
2157 }
2158
2159 /* id bot found */
2160 if (byname) {
2161 if (diff)
2162 *diff |= 1;
2163 return byname;
2164 }
2165 }
2166
2167 return NULL;
2168}
2169
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002170/* Update a server state using the parameters available in the params list */
2171static void srv_update_state(struct server *srv, int version, char **params)
2172{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002173 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002174 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002175
2176 /* fields since version 1
2177 * and common to all other upcoming versions
2178 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002179 enum srv_state srv_op_state;
2180 enum srv_admin srv_admin_state;
2181 unsigned srv_uweight, srv_iweight;
2182 unsigned long srv_last_time_change;
2183 short srv_check_status;
2184 enum chk_result srv_check_result;
2185 int srv_check_health;
2186 int srv_check_state, srv_agent_state;
2187 int bk_f_forced_id;
2188 int srv_f_forced_id;
2189
Willy Tarreau31138fa2015-09-29 18:38:47 +02002190 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002191 switch (version) {
2192 case 1:
2193 /*
2194 * now we can proceed with server's state update:
2195 * srv_addr: params[0]
2196 * srv_op_state: params[1]
2197 * srv_admin_state: params[2]
2198 * srv_uweight: params[3]
2199 * srv_iweight: params[4]
2200 * srv_last_time_change: params[5]
2201 * srv_check_status: params[6]
2202 * srv_check_result: params[7]
2203 * srv_check_health: params[8]
2204 * srv_check_state: params[9]
2205 * srv_agent_state: params[10]
2206 * bk_f_forced_id: params[11]
2207 * srv_f_forced_id: params[12]
2208 */
2209
2210 /* validating srv_op_state */
2211 p = NULL;
2212 errno = 0;
2213 srv_op_state = strtol(params[1], &p, 10);
2214 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2215 (srv_op_state != SRV_ST_STOPPED &&
2216 srv_op_state != SRV_ST_STARTING &&
2217 srv_op_state != SRV_ST_RUNNING &&
2218 srv_op_state != SRV_ST_STOPPING)) {
2219 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2220 }
2221
2222 /* validating srv_admin_state */
2223 p = NULL;
2224 errno = 0;
2225 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002226
2227 /* inherited statuses will be recomputed later */
2228 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2229
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002230 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2231 (srv_admin_state != 0 &&
2232 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002233 srv_admin_state != SRV_ADMF_CMAINT &&
2234 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002235 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002236 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002237 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2238 }
2239
2240 /* validating srv_uweight */
2241 p = NULL;
2242 errno = 0;
2243 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002244 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002245 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2246
2247 /* validating srv_iweight */
2248 p = NULL;
2249 errno = 0;
2250 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002251 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002252 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2253
2254 /* validating srv_last_time_change */
2255 p = NULL;
2256 errno = 0;
2257 srv_last_time_change = strtol(params[5], &p, 10);
2258 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2259 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2260
2261 /* validating srv_check_status */
2262 p = NULL;
2263 errno = 0;
2264 srv_check_status = strtol(params[6], &p, 10);
2265 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2266 (srv_check_status >= HCHK_STATUS_SIZE))
2267 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2268
2269 /* validating srv_check_result */
2270 p = NULL;
2271 errno = 0;
2272 srv_check_result = strtol(params[7], &p, 10);
2273 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2274 (srv_check_result != CHK_RES_UNKNOWN &&
2275 srv_check_result != CHK_RES_NEUTRAL &&
2276 srv_check_result != CHK_RES_FAILED &&
2277 srv_check_result != CHK_RES_PASSED &&
2278 srv_check_result != CHK_RES_CONDPASS)) {
2279 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2280 }
2281
2282 /* validating srv_check_health */
2283 p = NULL;
2284 errno = 0;
2285 srv_check_health = strtol(params[8], &p, 10);
2286 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2287 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2288
2289 /* validating srv_check_state */
2290 p = NULL;
2291 errno = 0;
2292 srv_check_state = strtol(params[9], &p, 10);
2293 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2294 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2295 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2296
2297 /* validating srv_agent_state */
2298 p = NULL;
2299 errno = 0;
2300 srv_agent_state = strtol(params[10], &p, 10);
2301 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2302 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2303 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2304
2305 /* validating bk_f_forced_id */
2306 p = NULL;
2307 errno = 0;
2308 bk_f_forced_id = strtol(params[11], &p, 10);
2309 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2310 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2311
2312 /* validating srv_f_forced_id */
2313 p = NULL;
2314 errno = 0;
2315 srv_f_forced_id = strtol(params[12], &p, 10);
2316 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2317 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2318
2319
2320 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002321 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002322 goto out;
2323
2324 /* recover operational state and apply it to this server
2325 * and all servers tracking this one */
2326 switch (srv_op_state) {
2327 case SRV_ST_STOPPED:
2328 srv->check.health = 0;
2329 srv_set_stopped(srv, "changed from server-state after a reload");
2330 break;
2331 case SRV_ST_STARTING:
2332 srv->state = srv_op_state;
2333 break;
2334 case SRV_ST_STOPPING:
2335 srv->check.health = srv->check.rise + srv->check.fall - 1;
2336 srv_set_stopping(srv, "changed from server-state after a reload");
2337 break;
2338 case SRV_ST_RUNNING:
2339 srv->check.health = srv->check.rise + srv->check.fall - 1;
2340 srv_set_running(srv, "");
2341 break;
2342 }
2343
2344 /* When applying server state, the following rules apply:
2345 * - in case of a configuration change, we apply the setting from the new
2346 * configuration, regardless of old running state
2347 * - if no configuration change, we apply old running state only if old running
2348 * state is different from new configuration state
2349 */
2350 /* configuration has changed */
2351 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2352 if (srv->admin & SRV_ADMF_CMAINT)
2353 srv_adm_set_maint(srv);
2354 else
2355 srv_adm_set_ready(srv);
2356 }
2357 /* configuration is the same, let's compate old running state and new conf state */
2358 else {
2359 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2360 srv_adm_set_maint(srv);
2361 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2362 srv_adm_set_ready(srv);
2363 }
2364 /* apply drain mode if server is currently enabled */
2365 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2366 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002367 * (srv->iweight is the weight set up in configuration).
2368 * There are two possible reasons for FDRAIN to have been present :
2369 * - previous config weight was zero
2370 * - "set server b/s drain" was sent to the CLI
2371 *
2372 * In the first case, we simply want to drop this drain state
2373 * if the new weight is not zero anymore, meaning the administrator
2374 * has intentionally turned the weight back to a positive value to
2375 * enable the server again after an operation. In the second case,
2376 * the drain state was forced on the CLI regardless of the config's
2377 * weight so we don't want a change to the config weight to lose this
2378 * status. What this means is :
2379 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2380 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002381 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002382 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002383 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002384 }
2385
2386 srv->last_change = date.tv_sec - srv_last_time_change;
2387 srv->check.status = srv_check_status;
2388 srv->check.result = srv_check_result;
2389 srv->check.health = srv_check_health;
2390
2391 /* Only case we want to apply is removing ENABLED flag which could have been
2392 * done by the "disable health" command over the stats socket
2393 */
2394 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2395 (srv_check_state & CHK_ST_CONFIGURED) &&
2396 !(srv_check_state & CHK_ST_ENABLED))
2397 srv->check.state &= ~CHK_ST_ENABLED;
2398
2399 /* Only case we want to apply is removing ENABLED flag which could have been
2400 * done by the "disable agent" command over the stats socket
2401 */
2402 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2403 (srv_agent_state & CHK_ST_CONFIGURED) &&
2404 !(srv_agent_state & CHK_ST_ENABLED))
2405 srv->agent.state &= ~CHK_ST_ENABLED;
2406
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002407 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2408 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002409 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002410 * It means that a configuration file change has precedence over a unix socket change
2411 * for server's weight
2412 *
2413 * by default, HAProxy applies the following weight when parsing the configuration
2414 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002415 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002416 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002417 srv->uweight = srv_uweight;
2418 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002419 server_recalc_eweight(srv);
2420
Willy Tarreaue5a60682016-11-09 14:54:53 +01002421 /* load server IP address */
2422 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002423 break;
2424 default:
2425 chunk_appendf(msg, ", version '%d' not supported", version);
2426 }
2427
2428 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002429 if (msg->len) {
2430 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002431 Warning("server-state application failed for server '%s/%s'%s",
2432 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002433 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002434}
2435
2436/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2437 * For each proxy, it does the following:
2438 * - opens its server state file (either one or local one)
2439 * - read whole file, line by line
2440 * - analyse each line to check if it matches our current backend:
2441 * - backend name matches
2442 * - backend id matches if id is forced and name doesn't match
2443 * - if the server pointed by the line is found, then state is applied
2444 *
2445 * If the running backend uuid or id differs from the state file, then HAProxy reports
2446 * a warning.
2447 */
2448void apply_server_state(void)
2449{
2450 char *cur, *end;
2451 char mybuf[SRV_STATE_LINE_MAXLEN];
2452 int mybuflen;
2453 char *params[SRV_STATE_FILE_MAX_FIELDS];
2454 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2455 int arg, srv_arg, version, diff;
2456 FILE *f;
2457 char *filepath;
2458 char globalfilepath[MAXPATHLEN + 1];
2459 char localfilepath[MAXPATHLEN + 1];
2460 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2461 extern struct proxy *proxy;
2462 struct proxy *curproxy, *bk;
2463 struct server *srv;
2464
2465 globalfilepathlen = 0;
2466 /* create the globalfilepath variable */
2467 if (global.server_state_file) {
2468 /* absolute path or no base directory provided */
2469 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2470 len = strlen(global.server_state_file);
2471 if (len > MAXPATHLEN) {
2472 globalfilepathlen = 0;
2473 goto globalfileerror;
2474 }
2475 memcpy(globalfilepath, global.server_state_file, len);
2476 globalfilepath[len] = '\0';
2477 globalfilepathlen = len;
2478 }
2479 else if (global.server_state_base) {
2480 len = strlen(global.server_state_base);
2481 globalfilepathlen += len;
2482
2483 if (globalfilepathlen > MAXPATHLEN) {
2484 globalfilepathlen = 0;
2485 goto globalfileerror;
2486 }
2487 strncpy(globalfilepath, global.server_state_base, len);
2488 globalfilepath[globalfilepathlen] = 0;
2489
2490 /* append a slash if needed */
2491 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2492 if (globalfilepathlen + 1 > MAXPATHLEN) {
2493 globalfilepathlen = 0;
2494 goto globalfileerror;
2495 }
2496 globalfilepath[globalfilepathlen++] = '/';
2497 }
2498
2499 len = strlen(global.server_state_file);
2500 if (globalfilepathlen + len > MAXPATHLEN) {
2501 globalfilepathlen = 0;
2502 goto globalfileerror;
2503 }
2504 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2505 globalfilepathlen += len;
2506 globalfilepath[globalfilepathlen++] = 0;
2507 }
2508 }
2509 globalfileerror:
2510 if (globalfilepathlen == 0)
2511 globalfilepath[0] = '\0';
2512
2513 /* read servers state from local file */
2514 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2515 /* servers are only in backends */
2516 if (!(curproxy->cap & PR_CAP_BE))
2517 continue;
2518 fileopenerr = 0;
2519 filepath = NULL;
2520
2521 /* search server state file path and name */
2522 switch (curproxy->load_server_state_from_file) {
2523 /* read servers state from global file */
2524 case PR_SRV_STATE_FILE_GLOBAL:
2525 /* there was an error while generating global server state file path */
2526 if (globalfilepathlen == 0)
2527 continue;
2528 filepath = globalfilepath;
2529 fileopenerr = 1;
2530 break;
2531 /* this backend has its own file */
2532 case PR_SRV_STATE_FILE_LOCAL:
2533 localfilepathlen = 0;
2534 localfilepath[0] = '\0';
2535 len = 0;
2536 /* create the localfilepath variable */
2537 /* absolute path or no base directory provided */
2538 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2539 len = strlen(curproxy->server_state_file_name);
2540 if (len > MAXPATHLEN) {
2541 localfilepathlen = 0;
2542 goto localfileerror;
2543 }
2544 memcpy(localfilepath, curproxy->server_state_file_name, len);
2545 localfilepath[len] = '\0';
2546 localfilepathlen = len;
2547 }
2548 else if (global.server_state_base) {
2549 len = strlen(global.server_state_base);
2550 localfilepathlen += len;
2551
2552 if (localfilepathlen > MAXPATHLEN) {
2553 localfilepathlen = 0;
2554 goto localfileerror;
2555 }
2556 strncpy(localfilepath, global.server_state_base, len);
2557 localfilepath[localfilepathlen] = 0;
2558
2559 /* append a slash if needed */
2560 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2561 if (localfilepathlen + 1 > MAXPATHLEN) {
2562 localfilepathlen = 0;
2563 goto localfileerror;
2564 }
2565 localfilepath[localfilepathlen++] = '/';
2566 }
2567
2568 len = strlen(curproxy->server_state_file_name);
2569 if (localfilepathlen + len > MAXPATHLEN) {
2570 localfilepathlen = 0;
2571 goto localfileerror;
2572 }
2573 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2574 localfilepathlen += len;
2575 localfilepath[localfilepathlen++] = 0;
2576 }
2577 filepath = localfilepath;
2578 localfileerror:
2579 if (localfilepathlen == 0)
2580 localfilepath[0] = '\0';
2581
2582 break;
2583 case PR_SRV_STATE_FILE_NONE:
2584 default:
2585 continue;
2586 }
2587
2588 /* preload global state file */
2589 errno = 0;
2590 f = fopen(filepath, "r");
2591 if (errno && fileopenerr)
2592 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2593 if (!f)
2594 continue;
2595
2596 mybuf[0] = '\0';
2597 mybuflen = 0;
2598 version = 0;
2599
2600 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002601 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2602 Warning("Can't read first line of the server state file '%s'\n", filepath);
2603 goto fileclose;
2604 }
2605
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002606 cur = mybuf;
2607 version = atoi(cur);
2608 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2609 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002610 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002611
2612 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2613 int bk_f_forced_id = 0;
2614 int check_id = 0;
2615 int check_name = 0;
2616
2617 mybuflen = strlen(mybuf);
2618 cur = mybuf;
2619 end = cur + mybuflen;
2620
2621 bk = NULL;
2622 srv = NULL;
2623
2624 /* we need at least one character */
2625 if (mybuflen == 0)
2626 continue;
2627
2628 /* ignore blank characters at the beginning of the line */
2629 while (isspace(*cur))
2630 ++cur;
2631
2632 if (cur == end)
2633 continue;
2634
2635 /* ignore comment line */
2636 if (*cur == '#')
2637 continue;
2638
2639 /* we're now ready to move the line into *srv_params[] */
2640 params[0] = cur;
2641 arg = 1;
2642 srv_arg = 0;
2643 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2644 if (isspace(*cur)) {
2645 *cur = '\0';
2646 ++cur;
2647 while (isspace(*cur))
2648 ++cur;
2649 switch (version) {
2650 case 1:
2651 /*
2652 * srv_addr: params[4] => srv_params[0]
2653 * srv_op_state: params[5] => srv_params[1]
2654 * srv_admin_state: params[6] => srv_params[2]
2655 * srv_uweight: params[7] => srv_params[3]
2656 * srv_iweight: params[8] => srv_params[4]
2657 * srv_last_time_change: params[9] => srv_params[5]
2658 * srv_check_status: params[10] => srv_params[6]
2659 * srv_check_result: params[11] => srv_params[7]
2660 * srv_check_health: params[12] => srv_params[8]
2661 * srv_check_state: params[13] => srv_params[9]
2662 * srv_agent_state: params[14] => srv_params[10]
2663 * bk_f_forced_id: params[15] => srv_params[11]
2664 * srv_f_forced_id: params[16] => srv_params[12]
2665 */
2666 if (arg >= 4) {
2667 srv_params[srv_arg] = cur;
2668 ++srv_arg;
2669 }
2670 break;
2671 }
2672
2673 params[arg] = cur;
2674 ++arg;
2675 }
2676 else {
2677 ++cur;
2678 }
2679 }
2680
2681 /* if line is incomplete line, then ignore it.
2682 * otherwise, update useful flags */
2683 switch (version) {
2684 case 1:
2685 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2686 continue;
2687 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2688 check_id = (atoi(params[0]) == curproxy->uuid);
2689 check_name = (strcmp(curproxy->id, params[1]) == 0);
2690 break;
2691 }
2692
2693 diff = 0;
2694 bk = curproxy;
2695
2696 /* if backend can't be found, let's continue */
2697 if (!check_id && !check_name)
2698 continue;
2699 else if (!check_id && check_name) {
2700 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2701 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
2702 }
2703 else if (check_id && !check_name) {
2704 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2705 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
2706 /* if name doesn't match, we still want to update curproxy if the backend id
2707 * was forced in previous the previous configuration */
2708 if (!bk_f_forced_id)
2709 continue;
2710 }
2711
2712 /* look for the server by its id: param[2] */
2713 /* else look for the server by its name: param[3] */
2714 diff = 0;
2715 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
2716
2717 if (!srv) {
2718 /* if no server found, then warning and continue with next line */
2719 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2720 params[3], params[2], params[0], params[1]);
2721 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
2722 params[3], params[2], params[0], params[1]);
2723 continue;
2724 }
2725 else if (diff & PR_FBM_MISMATCH_ID) {
2726 Warning("In backend '%s' (id: '%d'): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
2727 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
2728 }
2729 else if (diff & PR_FBM_MISMATCH_NAME) {
2730 Warning("In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
2731 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
2732 }
2733
2734 /* now we can proceed with server's state update */
2735 srv_update_state(srv, version, srv_params);
2736 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01002737fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002738 fclose(f);
2739 }
2740}
2741
Simon Horman7d09b9a2013-02-12 10:45:51 +09002742/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002743 * update a server's current IP address.
2744 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2745 * ip is in network format.
2746 * updater is a string which contains an information about the requester of the update.
2747 * updater is used if not NULL.
2748 *
2749 * A log line and a stderr warning message is generated based on server's backend options.
2750 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01002751int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002752{
2753 /* generates a log line and a warning on stderr */
2754 if (1) {
2755 /* book enough space for both IPv4 and IPv6 */
2756 char oldip[INET6_ADDRSTRLEN];
2757 char newip[INET6_ADDRSTRLEN];
2758
2759 memset(oldip, '\0', INET6_ADDRSTRLEN);
2760 memset(newip, '\0', INET6_ADDRSTRLEN);
2761
2762 /* copy old IP address in a string */
2763 switch (s->addr.ss_family) {
2764 case AF_INET:
2765 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2766 break;
2767 case AF_INET6:
2768 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2769 break;
2770 };
2771
2772 /* copy new IP address in a string */
2773 switch (ip_sin_family) {
2774 case AF_INET:
2775 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2776 break;
2777 case AF_INET6:
2778 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2779 break;
2780 };
2781
2782 /* save log line into a buffer */
2783 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2784 s->proxy->id, s->id, oldip, newip, updater);
2785
2786 /* write the buffer on stderr */
2787 Warning("%s.\n", trash.str);
2788
2789 /* send a log */
2790 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2791 }
2792
2793 /* save the new IP family */
2794 s->addr.ss_family = ip_sin_family;
2795 /* save the new IP address */
2796 switch (ip_sin_family) {
2797 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002798 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002799 break;
2800 case AF_INET6:
2801 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2802 break;
2803 };
Olivier Houchard4e694042017-03-14 20:01:29 +01002804 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002805
2806 return 0;
2807}
2808
2809/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002810 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2811 *
2812 * Caller can pass its name through <updater> to get it integrated in the response message
2813 * returned by the function.
2814 *
2815 * The function first does the following, in that order:
2816 * - validates the new addr and/or port
2817 * - checks if an update is required (new IP or port is different than current ones)
2818 * - checks the update is allowed:
2819 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2820 * - allow all changes if no CHECKS are configured
2821 * - if CHECK is configured:
2822 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2823 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2824 * conditions are met
2825 */
2826const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
2827{
2828 struct sockaddr_storage sa;
2829 int ret, port_change_required;
2830 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00002831 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002832 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01002833 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002834
2835 msg = get_trash_chunk();
2836 chunk_reset(msg);
2837
2838 if (addr) {
2839 memset(&sa, 0, sizeof(struct sockaddr_storage));
2840 if (str2ip2(addr, &sa, 0) == NULL) {
2841 chunk_printf(msg, "Invalid addr '%s'", addr);
2842 goto out;
2843 }
2844
2845 /* changes are allowed on AF_INET* families only */
2846 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2847 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2848 goto out;
2849 }
2850
2851 /* collecting data currently setup */
2852 memset(current_addr, '\0', sizeof(current_addr));
2853 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2854 /* changes are allowed on AF_INET* families only */
2855 if ((ret != AF_INET) && (ret != AF_INET6)) {
2856 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2857 goto out;
2858 }
2859
2860 /* applying ADDR changes if required and allowed
2861 * ipcmp returns 0 when both ADDR are the same
2862 */
2863 if (ipcmp(&s->addr, &sa) == 0) {
2864 chunk_appendf(msg, "no need to change the addr");
2865 goto port;
2866 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002867 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01002868 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002869
2870 /* we also need to update check's ADDR only if it uses the server's one */
2871 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002872 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002873 }
2874
2875 /* we also need to update agent ADDR only if it use the server's one */
2876 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002877 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002878 }
2879
2880 /* update report for caller */
2881 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2882 }
2883
2884 port:
2885 if (port) {
2886 char sign = '\0';
2887 char *endptr;
2888
2889 if (addr)
2890 chunk_appendf(msg, ", ");
2891
2892 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01002893 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002894
2895 /* check if PORT change is required */
2896 port_change_required = 0;
2897
2898 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04002899 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002900 new_port = strtol(port, &endptr, 10);
2901 if ((errno != 0) || (port == endptr)) {
2902 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2903 goto out;
2904 }
2905
2906 /* check if caller triggers a port mapped or offset */
2907 if (sign == '-' || (sign == '+')) {
2908 /* check if server currently uses port map */
2909 if (!(s->flags & SRV_F_MAPPORTS)) {
2910 /* switch from fixed port to port map mandatorily triggers
2911 * a port change */
2912 port_change_required = 1;
2913 /* check is configured
2914 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2915 * prevent PORT change if check doesn't have it's dedicated port while switching
2916 * to port mapping */
2917 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2918 chunk_appendf(msg, "can't change <port> to port map because it is incompatible with current health check port configuration (use 'port' statement from the 'server' directive.");
2919 goto out;
2920 }
2921 }
2922 /* we're already using port maps */
2923 else {
2924 port_change_required = current_port != new_port;
2925 }
2926 }
2927 /* fixed port */
2928 else {
2929 port_change_required = current_port != new_port;
2930 }
2931
2932 /* applying PORT changes if required and update response message */
2933 if (port_change_required) {
2934 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01002935 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01002936 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002937
2938 /* prepare message */
2939 chunk_appendf(msg, "port changed from '");
2940 if (s->flags & SRV_F_MAPPORTS)
2941 chunk_appendf(msg, "+");
2942 chunk_appendf(msg, "%d' to '", current_port);
2943
2944 if (sign == '-') {
2945 s->flags |= SRV_F_MAPPORTS;
2946 chunk_appendf(msg, "%c", sign);
2947 /* just use for result output */
2948 new_port = -new_port;
2949 }
2950 else if (sign == '+') {
2951 s->flags |= SRV_F_MAPPORTS;
2952 chunk_appendf(msg, "%c", sign);
2953 }
2954 else {
2955 s->flags &= ~SRV_F_MAPPORTS;
2956 }
2957
2958 chunk_appendf(msg, "%d'", new_port);
2959
2960 /* we also need to update health checks port only if it uses server's realport */
2961 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
2962 s->check.port = new_port;
2963 }
2964 }
2965 else {
2966 chunk_appendf(msg, "no need to change the port");
2967 }
2968 }
2969
2970out:
Olivier Houchard4e694042017-03-14 20:01:29 +01002971 if (changed)
2972 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002973 if (updater)
2974 chunk_appendf(msg, " by '%s'", updater);
2975 chunk_appendf(msg, "\n");
2976 return msg->str;
2977}
2978
2979
2980/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002981 * update server status based on result of name resolution
2982 * returns:
2983 * 0 if server status is updated
2984 * 1 if server status has not changed
2985 */
2986int snr_update_srv_status(struct server *s)
2987{
2988 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002989 struct dns_resolvers *resolvers;
2990
2991 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002992
2993 switch (resolution->status) {
2994 case RSLV_STATUS_NONE:
2995 /* status when HAProxy has just (re)started */
2996 trigger_resolution(s);
2997 break;
2998
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002999 case RSLV_STATUS_VALID:
3000 /*
3001 * resume health checks
3002 * server will be turned back on if health check is safe
3003 */
3004 if (!(s->admin & SRV_ADMF_RMAINT))
3005 return 1;
3006 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3007 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3008 s->proxy->id, s->id);
3009
3010 Warning("%s.\n", trash.str);
3011 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3012 return 0;
3013
3014 case RSLV_STATUS_NX:
3015 /* stop server if resolution is NX for a long enough period */
3016 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3017 if (s->admin & SRV_ADMF_RMAINT)
3018 return 1;
3019 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3020 return 0;
3021 }
3022 break;
3023
3024 case RSLV_STATUS_TIMEOUT:
3025 /* stop server if resolution is TIMEOUT for a long enough period */
3026 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3027 if (s->admin & SRV_ADMF_RMAINT)
3028 return 1;
3029 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3030 return 0;
3031 }
3032 break;
3033
3034 case RSLV_STATUS_REFUSED:
3035 /* stop server if resolution is REFUSED for a long enough period */
3036 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3037 if (s->admin & SRV_ADMF_RMAINT)
3038 return 1;
3039 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3040 return 0;
3041 }
3042 break;
3043
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003044 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003045 /* stop server if resolution is in unmatched error for a long enough period */
3046 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3047 if (s->admin & SRV_ADMF_RMAINT)
3048 return 1;
3049 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3050 return 0;
3051 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003052 break;
3053 }
3054
3055 return 1;
3056}
3057
3058/*
3059 * Server Name Resolution valid response callback
3060 * It expects:
3061 * - <nameserver>: the name server which answered the valid response
3062 * - <response>: buffer containing a valid DNS response
3063 * - <response_len>: size of <response>
3064 * It performs the following actions:
3065 * - ignore response if current ip found and server family not met
3066 * - update with first new ip found if family is met and current IP is not found
3067 * returns:
3068 * 0 on error
3069 * 1 when no error or safe ignore
3070 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003071int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003072{
3073 struct server *s;
3074 void *serverip, *firstip;
3075 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003076 int ret;
3077 struct chunk *chk = get_trash_chunk();
3078
3079 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003080 firstip = NULL; /* pointer to the first valid response found */
3081 /* it will be used as the new IP if a change is required */
3082 firstip_sin_family = AF_UNSPEC;
3083 serverip = NULL; /* current server IP address */
3084
3085 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003086 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003087
3088 /* initializing server IP pointer */
3089 server_sin_family = s->addr.ss_family;
3090 switch (server_sin_family) {
3091 case AF_INET:
3092 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3093 break;
3094
3095 case AF_INET6:
3096 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3097 break;
3098
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003099 case AF_UNSPEC:
3100 break;
3101
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003102 default:
3103 goto invalid;
3104 }
3105
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003106 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003107 serverip, server_sin_family, &firstip,
3108 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003109
3110 switch (ret) {
3111 case DNS_UPD_NO:
3112 if (resolution->status != RSLV_STATUS_VALID) {
3113 resolution->status = RSLV_STATUS_VALID;
3114 resolution->last_status_change = now_ms;
3115 }
3116 goto stop_resolution;
3117
3118 case DNS_UPD_SRVIP_NOT_FOUND:
3119 goto save_ip;
3120
3121 case DNS_UPD_CNAME:
3122 if (resolution->status != RSLV_STATUS_VALID) {
3123 resolution->status = RSLV_STATUS_VALID;
3124 resolution->last_status_change = now_ms;
3125 }
3126 goto invalid;
3127
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003128 case DNS_UPD_NO_IP_FOUND:
3129 if (resolution->status != RSLV_STATUS_OTHER) {
3130 resolution->status = RSLV_STATUS_OTHER;
3131 resolution->last_status_change = now_ms;
3132 }
3133 goto stop_resolution;
3134
Baptiste Assmannfad03182015-10-28 02:03:32 +01003135 case DNS_UPD_NAME_ERROR:
3136 /* if this is not the last expected response, we ignore it */
3137 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3138 return 0;
3139 /* update resolution status to OTHER error type */
3140 if (resolution->status != RSLV_STATUS_OTHER) {
3141 resolution->status = RSLV_STATUS_OTHER;
3142 resolution->last_status_change = now_ms;
3143 }
3144 goto stop_resolution;
3145
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003146 default:
3147 goto invalid;
3148
3149 }
3150
3151 save_ip:
3152 nameserver->counters.update += 1;
3153 if (resolution->status != RSLV_STATUS_VALID) {
3154 resolution->status = RSLV_STATUS_VALID;
3155 resolution->last_status_change = now_ms;
3156 }
3157
3158 /* save the first ip we found */
3159 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3160 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3161
3162 stop_resolution:
3163 /* update last resolution date and time */
3164 resolution->last_resolution = now_ms;
3165 /* reset current status flag */
3166 resolution->step = RSLV_STEP_NONE;
3167 /* reset values */
3168 dns_reset_resolution(resolution);
3169
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003170 dns_update_resolvers_timeout(nameserver->resolvers);
3171
3172 snr_update_srv_status(s);
3173 return 0;
3174
3175 invalid:
3176 nameserver->counters.invalid += 1;
3177 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3178 goto stop_resolution;
3179
3180 snr_update_srv_status(s);
3181 return 0;
3182}
3183
3184/*
3185 * Server Name Resolution error management callback
3186 * returns:
3187 * 0 on error
3188 * 1 when no error or safe ignore
3189 */
3190int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3191{
3192 struct server *s;
3193 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003194 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003195
3196 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003197 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003198 resolvers = resolution->resolvers;
3199
3200 /* can be ignored if this is not the last response */
3201 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3202 return 1;
3203 }
3204
3205 switch (error_code) {
3206 case DNS_RESP_INVALID:
3207 case DNS_RESP_WRONG_NAME:
3208 if (resolution->status != RSLV_STATUS_INVALID) {
3209 resolution->status = RSLV_STATUS_INVALID;
3210 resolution->last_status_change = now_ms;
3211 }
3212 break;
3213
3214 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003215 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003216 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003217 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003218 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003219 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3220 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003221
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003222 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003223 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003224 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003225 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003226 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003227 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003228 }
3229 else if (res_preferred_afinet) {
3230 /* fallback from A to AAAA */
3231 resolution->query_type = DNS_RTYPE_AAAA;
3232 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003233 else {
3234 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003235 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003236 resolution->query_type = DNS_RTYPE_A;
3237 } else {
3238 resolution->query_type = DNS_RTYPE_AAAA;
3239 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003240 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003241
3242 dns_send_query(resolution);
3243
3244 /*
3245 * move the resolution to the last element of the FIFO queue
3246 * and update timeout wakeup based on the new first entry
3247 */
3248 if (dns_check_resolution_queue(resolvers) > 1) {
3249 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003250 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003251 /* ex first resolution goes to the end of the queue */
3252 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3253 }
3254 dns_update_resolvers_timeout(resolvers);
3255 goto leave;
3256 }
3257 else {
3258 if (resolution->status != RSLV_STATUS_OTHER) {
3259 resolution->status = RSLV_STATUS_OTHER;
3260 resolution->last_status_change = now_ms;
3261 }
3262 }
3263 break;
3264
3265 case DNS_RESP_NX_DOMAIN:
3266 if (resolution->status != RSLV_STATUS_NX) {
3267 resolution->status = RSLV_STATUS_NX;
3268 resolution->last_status_change = now_ms;
3269 }
3270 break;
3271
3272 case DNS_RESP_REFUSED:
3273 if (resolution->status != RSLV_STATUS_REFUSED) {
3274 resolution->status = RSLV_STATUS_REFUSED;
3275 resolution->last_status_change = now_ms;
3276 }
3277 break;
3278
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003279 case DNS_RESP_TIMEOUT:
3280 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3281 resolution->status = RSLV_STATUS_TIMEOUT;
3282 resolution->last_status_change = now_ms;
3283 }
3284 break;
3285 }
3286
3287 /* update last resolution date and time */
3288 resolution->last_resolution = now_ms;
3289 /* reset current status flag */
3290 resolution->step = RSLV_STEP_NONE;
3291 /* reset values */
3292 dns_reset_resolution(resolution);
3293
3294 LIST_DEL(&resolution->list);
3295 dns_update_resolvers_timeout(resolvers);
3296
3297 leave:
3298 snr_update_srv_status(s);
3299 return 1;
3300}
3301
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003302/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3303 * resolver. This is suited for initial address configuration. Returns 0 on
3304 * success otherwise a non-zero error code. In case of error, *err_code, if
3305 * not NULL, is filled up.
3306 */
3307int srv_set_addr_via_libc(struct server *srv, int *err_code)
3308{
3309 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003310 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003311 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003312 return 1;
3313 }
3314 return 0;
3315}
3316
3317/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3318 * from the state file. This is suited for initial address configuration.
3319 * Returns 0 on success otherwise a non-zero error code. In case of error,
3320 * *err_code, if not NULL, is filled up.
3321 */
3322static int srv_apply_lastaddr(struct server *srv, int *err_code)
3323{
3324 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3325 if (err_code)
3326 *err_code |= ERR_WARN;
3327 return 1;
3328 }
3329 return 0;
3330}
3331
Willy Tarreau25e51522016-11-04 15:10:17 +01003332/* returns 0 if no error, otherwise a combination of ERR_* flags */
3333static int srv_iterate_initaddr(struct server *srv)
3334{
3335 int return_code = 0;
3336 int err_code;
3337 unsigned int methods;
3338
3339 methods = srv->init_addr_methods;
3340 if (!methods) { // default to "last,libc"
3341 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3342 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3343 }
3344
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003345 /* "-dr" : always append "none" so that server addresses resolution
3346 * failures are silently ignored, this is convenient to validate some
3347 * configs out of their environment.
3348 */
3349 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3350 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3351
Willy Tarreau25e51522016-11-04 15:10:17 +01003352 while (methods) {
3353 err_code = 0;
3354 switch (srv_get_next_initaddr(&methods)) {
3355 case SRV_IADDR_LAST:
3356 if (!srv->lastaddr)
3357 continue;
3358 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003359 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003360 return_code |= err_code;
3361 break;
3362
3363 case SRV_IADDR_LIBC:
3364 if (!srv->hostname)
3365 continue;
3366 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003367 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003368 return_code |= err_code;
3369 break;
3370
Willy Tarreau37ebe122016-11-04 15:17:58 +01003371 case SRV_IADDR_NONE:
3372 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003373 if (return_code) {
3374 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3375 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3376 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003377 return return_code;
3378
Willy Tarreau4310d362016-11-02 15:05:56 +01003379 case SRV_IADDR_IP:
3380 ipcpy(&srv->init_addr, &srv->addr);
3381 if (return_code) {
3382 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3383 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3384 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003385 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003386
Willy Tarreau25e51522016-11-04 15:10:17 +01003387 default: /* unhandled method */
3388 break;
3389 }
3390 }
3391
3392 if (!return_code) {
3393 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3394 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3395 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003396 else {
3397 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3398 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3399 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003400
3401 return_code |= ERR_ALERT | ERR_FATAL;
3402 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003403out:
3404 srv_set_dyncookie(srv);
3405 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003406}
3407
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003408/*
3409 * This function parses all backends and all servers within each backend
3410 * and performs servers' addr resolution based on information provided by:
3411 * - configuration file
3412 * - server-state file (states provided by an 'old' haproxy process)
3413 *
3414 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3415 */
3416int srv_init_addr(void)
3417{
3418 struct proxy *curproxy;
3419 int return_code = 0;
3420
3421 curproxy = proxy;
3422 while (curproxy) {
3423 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003424
3425 /* servers are in backend only */
3426 if (!(curproxy->cap & PR_CAP_BE))
3427 goto srv_init_addr_next;
3428
Willy Tarreau25e51522016-11-04 15:10:17 +01003429 for (srv = curproxy->srv; srv; srv = srv->next)
3430 if (srv->hostname)
3431 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003432
3433 srv_init_addr_next:
3434 curproxy = curproxy->next;
3435 }
3436
3437 return return_code;
3438}
3439
Willy Tarreau21b069d2016-11-23 17:15:08 +01003440/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3441 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003442 * on the CLI, sets the CLI's state to CLI_ST_PRINT and returns NULL. This is only
Willy Tarreau21b069d2016-11-23 17:15:08 +01003443 * used for CLI commands requiring a server name.
3444 * Important: the <arg> is modified to remove the '/'.
3445 */
3446struct server *cli_find_server(struct appctx *appctx, char *arg)
3447{
3448 struct proxy *px;
3449 struct server *sv;
3450 char *line;
3451
3452 /* split "backend/server" and make <line> point to server */
3453 for (line = arg; *line; line++)
3454 if (*line == '/') {
3455 *line++ = '\0';
3456 break;
3457 }
3458
3459 if (!*line || !*arg) {
3460 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003461 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003462 return NULL;
3463 }
3464
3465 if (!get_backend_server(arg, line, &px, &sv)) {
3466 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003467 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003468 return NULL;
3469 }
3470
3471 if (px->state == PR_STSTOPPED) {
3472 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003473 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003474 return NULL;
3475 }
3476
3477 return sv;
3478}
3479
William Lallemand222baf22016-11-19 02:00:33 +01003480
3481static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3482{
3483 struct server *sv;
3484 const char *warning;
3485
3486 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3487 return 1;
3488
3489 sv = cli_find_server(appctx, args[2]);
3490 if (!sv)
3491 return 1;
3492
3493 if (strcmp(args[3], "weight") == 0) {
3494 warning = server_parse_weight_change_request(sv, args[4]);
3495 if (warning) {
3496 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003497 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003498 }
3499 }
3500 else if (strcmp(args[3], "state") == 0) {
3501 if (strcmp(args[4], "ready") == 0)
3502 srv_adm_set_ready(sv);
3503 else if (strcmp(args[4], "drain") == 0)
3504 srv_adm_set_drain(sv);
3505 else if (strcmp(args[4], "maint") == 0)
3506 srv_adm_set_maint(sv);
3507 else {
3508 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003509 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003510 }
3511 }
3512 else if (strcmp(args[3], "health") == 0) {
3513 if (sv->track) {
3514 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003515 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003516 }
3517 else if (strcmp(args[4], "up") == 0) {
3518 sv->check.health = sv->check.rise + sv->check.fall - 1;
3519 srv_set_running(sv, "changed from CLI");
3520 }
3521 else if (strcmp(args[4], "stopping") == 0) {
3522 sv->check.health = sv->check.rise + sv->check.fall - 1;
3523 srv_set_stopping(sv, "changed from CLI");
3524 }
3525 else if (strcmp(args[4], "down") == 0) {
3526 sv->check.health = 0;
3527 srv_set_stopped(sv, "changed from CLI");
3528 }
3529 else {
3530 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003531 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003532 }
3533 }
3534 else if (strcmp(args[3], "agent") == 0) {
3535 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3536 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003537 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003538 }
3539 else if (strcmp(args[4], "up") == 0) {
3540 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3541 srv_set_running(sv, "changed from CLI");
3542 }
3543 else if (strcmp(args[4], "down") == 0) {
3544 sv->agent.health = 0;
3545 srv_set_stopped(sv, "changed from CLI");
3546 }
3547 else {
3548 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003549 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003550 }
3551 }
Misiek2da082d2017-01-09 09:40:42 +01003552 else if (strcmp(args[3], "agent-addr") == 0) {
3553 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3554 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3555 appctx->st0 = CLI_ST_PRINT;
3556 } else {
3557 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3558 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3559 appctx->st0 = CLI_ST_PRINT;
3560 }
3561 }
3562 }
3563 else if (strcmp(args[3], "agent-send") == 0) {
3564 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3565 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3566 appctx->st0 = CLI_ST_PRINT;
3567 } else {
3568 char *nss = strdup(args[4]);
3569 if (!nss) {
3570 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3571 appctx->st0 = CLI_ST_PRINT;
3572 } else {
3573 free(sv->agent.send_string);
3574 sv->agent.send_string = nss;
3575 sv->agent.send_string_len = strlen(args[4]);
3576 }
3577 }
3578 }
William Lallemand222baf22016-11-19 02:00:33 +01003579 else if (strcmp(args[3], "check-port") == 0) {
3580 int i = 0;
3581 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3582 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003583 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003584 }
3585 if ((i < 0) || (i > 65535)) {
3586 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003587 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003588 }
3589 /* prevent the update of port to 0 if MAPPORTS are in use */
3590 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3591 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003592 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003593 return 1;
3594 }
3595 sv->check.port = i;
3596 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003597 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003598 }
3599 else if (strcmp(args[3], "addr") == 0) {
3600 char *addr = NULL;
3601 char *port = NULL;
3602 if (strlen(args[4]) == 0) {
3603 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003604 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003605 return 1;
3606 }
3607 else {
3608 addr = args[4];
3609 }
3610 if (strcmp(args[5], "port") == 0) {
3611 port = args[6];
3612 }
3613 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3614 if (warning) {
3615 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003616 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003617 }
3618 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3619 }
3620 else {
3621 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr' and 'check-port'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003622 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003623 }
3624 return 1;
3625}
3626
William Lallemand6b160942016-11-22 12:34:35 +01003627static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3628{
3629 struct stream_interface *si = appctx->owner;
3630 struct proxy *px;
3631 struct server *sv;
3632 char *line;
3633
3634
3635 /* split "backend/server" and make <line> point to server */
3636 for (line = args[2]; *line; line++)
3637 if (*line == '/') {
3638 *line++ = '\0';
3639 break;
3640 }
3641
3642 if (!*line) {
3643 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003644 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003645 return 1;
3646 }
3647
3648 if (!get_backend_server(args[2], line, &px, &sv)) {
3649 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003650 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003651 return 1;
3652 }
3653
3654 /* return server's effective weight at the moment */
3655 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003656 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01003657 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003658 return 0;
3659 }
William Lallemand6b160942016-11-22 12:34:35 +01003660 return 1;
3661}
3662
3663static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
3664{
3665 struct server *sv;
3666 const char *warning;
3667
3668 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3669 return 1;
3670
3671 sv = cli_find_server(appctx, args[2]);
3672 if (!sv)
3673 return 1;
3674
3675 warning = server_parse_weight_change_request(sv, args[3]);
3676 if (warning) {
3677 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003678 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003679 }
3680 return 1;
3681}
3682
Willy Tarreaub8026272016-11-23 11:26:56 +01003683/* parse a "set maxconn server" command. It always returns 1. */
3684static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
3685{
3686 struct server *sv;
3687 const char *warning;
3688
3689 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3690 return 1;
3691
3692 sv = cli_find_server(appctx, args[3]);
3693 if (!sv)
3694 return 1;
3695
3696 warning = server_parse_maxconn_change_request(sv, args[4]);
3697 if (warning) {
3698 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003699 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01003700 }
3701 return 1;
3702}
William Lallemand6b160942016-11-22 12:34:35 +01003703
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003704/* parse a "disable agent" command. It always returns 1. */
3705static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
3706{
3707 struct server *sv;
3708
3709 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3710 return 1;
3711
3712 sv = cli_find_server(appctx, args[2]);
3713 if (!sv)
3714 return 1;
3715
3716 sv->agent.state &= ~CHK_ST_ENABLED;
3717 return 1;
3718}
3719
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003720/* parse a "disable health" command. It always returns 1. */
3721static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
3722{
3723 struct server *sv;
3724
3725 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3726 return 1;
3727
3728 sv = cli_find_server(appctx, args[2]);
3729 if (!sv)
3730 return 1;
3731
3732 sv->check.state &= ~CHK_ST_ENABLED;
3733 return 1;
3734}
3735
Willy Tarreauffb4d582016-11-24 12:47:00 +01003736/* parse a "disable server" command. It always returns 1. */
3737static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
3738{
3739 struct server *sv;
3740
3741 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3742 return 1;
3743
3744 sv = cli_find_server(appctx, args[2]);
3745 if (!sv)
3746 return 1;
3747
3748 srv_adm_set_maint(sv);
3749 return 1;
3750}
3751
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003752/* parse a "enable agent" command. It always returns 1. */
3753static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
3754{
3755 struct server *sv;
3756
3757 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3758 return 1;
3759
3760 sv = cli_find_server(appctx, args[2]);
3761 if (!sv)
3762 return 1;
3763
3764 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
3765 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003766 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003767 return 1;
3768 }
3769
3770 sv->agent.state |= CHK_ST_ENABLED;
3771 return 1;
3772}
3773
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003774/* parse a "enable health" command. It always returns 1. */
3775static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
3776{
3777 struct server *sv;
3778
3779 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3780 return 1;
3781
3782 sv = cli_find_server(appctx, args[2]);
3783 if (!sv)
3784 return 1;
3785
3786 sv->check.state |= CHK_ST_ENABLED;
3787 return 1;
3788}
3789
Willy Tarreauffb4d582016-11-24 12:47:00 +01003790/* parse a "enable server" command. It always returns 1. */
3791static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
3792{
3793 struct server *sv;
3794
3795 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3796 return 1;
3797
3798 sv = cli_find_server(appctx, args[2]);
3799 if (!sv)
3800 return 1;
3801
3802 srv_adm_set_ready(sv);
3803 return 1;
3804}
3805
William Lallemand222baf22016-11-19 02:00:33 +01003806/* register cli keywords */
3807static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003808 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003809 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003810 { { "disable", "server", NULL }, "disable server : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL },
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003811 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003812 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01003813 { { "enable", "server", NULL }, "enable server : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
Willy Tarreaub8026272016-11-23 11:26:56 +01003814 { { "set", "maxconn", "server", NULL }, "set maxconn server : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01003815 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01003816 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
3817 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
3818
William Lallemand222baf22016-11-19 02:00:33 +01003819 {{},}
3820}};
3821
3822__attribute__((constructor))
3823static void __server_init(void)
3824{
3825 cli_register_kw(&cli_kws);
3826}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003827
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003828/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003829 * Local variables:
3830 * c-indent-level: 8
3831 * c-basic-offset: 8
3832 * End:
3833 */