blob: 468814c408beaaae7b458579c1389b42321357a6 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Olivier Houchard4e694042017-03-14 20:01:29 +010018#include <import/xxhash.h>
19
Willy Tarreau272adea2014-03-31 10:39:59 +020020#include <common/cfgparse.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020021#include <haproxy/dict-t.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020022#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020023#include <haproxy/errors.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020024#include <haproxy/namespace.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020025#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020026
William Lallemand222baf22016-11-19 02:00:33 +010027#include <types/applet.h>
28#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010030#include <types/cli.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020036#include <proto/connection.h>
Willy Tarreaufc8f6a82020-06-03 19:20:59 +020037#include <haproxy/port_range.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020038#include <haproxy/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020039#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010040#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020041#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020042#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010043#include <proto/stream_interface.h>
44#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020045#include <proto/task.h>
David Carlier6f182082017-04-03 21:58:04 +010046#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020047
Willy Tarreau8d2b7772020-05-27 10:58:19 +020048#include <import/ebsttree.h>
Baptiste Assmannda29fe22019-06-13 13:24:29 +020049
Willy Tarreau3ff577e2018-08-02 11:48:52 +020050static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020051static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010052static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010053static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020054static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
55static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020056static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau21faa912012-10-10 08:27:36 +020058/* List head of all known server keywords */
59static struct srv_kw_list srv_keywords = {
60 .list = LIST_HEAD_INIT(srv_keywords.list)
61};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062
Willy Tarreauaf613e82020-06-05 08:40:51 +020063__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010064struct eb_root idle_conn_srv = EB_ROOT;
65struct task *idle_conn_task = NULL;
66struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020067struct mt_list toremove_connections[MAX_THREADS];
Willy Tarreauaf613e82020-06-05 08:40:51 +020068__decl_thread(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010069
Frédéric Lécaille7da71292019-05-20 09:47:07 +020070/* The server names dictionary */
71struct dict server_name_dict = {
72 .name = "server names",
73 .values = EB_ROOT_UNIQUE,
74};
75
Baptiste Assmannda29fe22019-06-13 13:24:29 +020076/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010077struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020078
Simon Hormana3608442013-11-01 16:46:15 +090079int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020080{
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020082 return s->down_time;
83
84 return now.tv_sec - s->last_change + s->down_time;
85}
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050087int srv_lastsession(const struct server *s)
88{
89 if (s->counters.last_sess)
90 return now.tv_sec - s->counters.last_sess;
91
92 return -1;
93}
94
Simon Horman4a741432013-02-23 15:35:38 +090095int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020096{
Simon Horman4a741432013-02-23 15:35:38 +090097 const struct server *s = check->server;
98
Willy Tarreauff5ae352013-12-11 20:36:34 +010099 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900100 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100101
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900103 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100104
Simon Horman4a741432013-02-23 15:35:38 +0900105 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100106}
107
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100108/*
109 * Check that we did not get a hash collision.
110 * Unlikely, but it can happen.
111 */
112static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100113{
114 struct proxy *p = s->proxy;
115 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100116
117 for (tmpserv = p->srv; tmpserv != NULL;
118 tmpserv = tmpserv->next) {
119 if (tmpserv == s)
120 continue;
121 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
122 continue;
123 if (tmpserv->cookie &&
124 strcmp(tmpserv->cookie, s->cookie) == 0) {
125 ha_warning("We generated two equal cookies for two different servers.\n"
126 "Please change the secret key for '%s'.\n",
127 s->proxy->id);
128 }
129 }
130
131}
132
Willy Tarreau46b7f532018-08-21 11:54:26 +0200133/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200134 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200135 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100136void srv_set_dyncookie(struct server *s)
137{
138 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100139 char *tmpbuf;
140 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100141 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100142 size_t buffer_len;
143 int addr_len;
144 int port;
145
Willy Tarreau5e83d992019-07-30 11:59:34 +0200146 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
147
Olivier Houchard4e694042017-03-14 20:01:29 +0100148 if ((s->flags & SRV_F_COOKIESET) ||
149 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
150 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200151 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100152 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100153
154 if (s->addr.ss_family != AF_INET &&
155 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200156 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100157 /*
158 * Buffer to calculate the cookie value.
159 * The buffer contains the secret key + the server IP address
160 * + the TCP port.
161 */
162 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
163 /*
164 * The TCP port should use only 2 bytes, but is stored in
165 * an unsigned int in struct server, so let's use 4, to be
166 * on the safe side.
167 */
168 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200169 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100170 memcpy(tmpbuf, p->dyncookie_key, key_len);
171 memcpy(&(tmpbuf[key_len]),
172 s->addr.ss_family == AF_INET ?
173 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
174 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
175 addr_len);
176 /*
177 * Make sure it's the same across all the load balancers,
178 * no matter their endianness.
179 */
180 port = htonl(s->svc_port);
181 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
182 hash_value = XXH64(tmpbuf, buffer_len, 0);
183 memprintf(&s->cookie, "%016llx", hash_value);
184 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200185 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100186 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100187
188 /* Don't bother checking if the dyncookie is duplicated if
189 * the server is marked as "disabled", maybe it doesn't have
190 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100191 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100192 if (!(s->next_admin & SRV_ADMF_FMAINT))
193 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200194 out:
195 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100196}
197
Willy Tarreau21faa912012-10-10 08:27:36 +0200198/*
199 * Registers the server keyword list <kwl> as a list of valid keywords for next
200 * parsing sessions.
201 */
202void srv_register_keywords(struct srv_kw_list *kwl)
203{
204 LIST_ADDQ(&srv_keywords.list, &kwl->list);
205}
206
207/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
208 * keyword is found with a NULL ->parse() function, then an attempt is made to
209 * find one with a valid ->parse() function. This way it is possible to declare
210 * platform-dependant, known keywords as NULL, then only declare them as valid
211 * if some options are met. Note that if the requested keyword contains an
212 * opening parenthesis, everything from this point is ignored.
213 */
214struct srv_kw *srv_find_kw(const char *kw)
215{
216 int index;
217 const char *kwend;
218 struct srv_kw_list *kwl;
219 struct srv_kw *ret = NULL;
220
221 kwend = strchr(kw, '(');
222 if (!kwend)
223 kwend = kw + strlen(kw);
224
225 list_for_each_entry(kwl, &srv_keywords.list, list) {
226 for (index = 0; kwl->kw[index].kw != NULL; index++) {
227 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
228 kwl->kw[index].kw[kwend-kw] == 0) {
229 if (kwl->kw[index].parse)
230 return &kwl->kw[index]; /* found it !*/
231 else
232 ret = &kwl->kw[index]; /* may be OK */
233 }
234 }
235 }
236 return ret;
237}
238
239/* Dumps all registered "server" keywords to the <out> string pointer. The
240 * unsupported keywords are only dumped if their supported form was not
241 * found.
242 */
243void srv_dump_kws(char **out)
244{
245 struct srv_kw_list *kwl;
246 int index;
247
Christopher Faulet784063e2020-05-18 12:14:18 +0200248 if (!out)
249 return;
250
Willy Tarreau21faa912012-10-10 08:27:36 +0200251 *out = NULL;
252 list_for_each_entry(kwl, &srv_keywords.list, list) {
253 for (index = 0; kwl->kw[index].kw != NULL; index++) {
254 if (kwl->kw[index].parse ||
255 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
256 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
257 kwl->scope,
258 kwl->kw[index].kw,
259 kwl->kw[index].skip ? " <arg>" : "",
260 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
261 kwl->kw[index].parse ? "" : " (not supported)");
262 }
263 }
264 }
265}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100266
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100267/* Parse the "backup" server keyword */
268static int srv_parse_backup(char **args, int *cur_arg,
269 struct proxy *curproxy, struct server *newsrv, char **err)
270{
271 newsrv->flags |= SRV_F_BACKUP;
272 return 0;
273}
274
Alexander Liu2a54bb72019-05-22 19:44:48 +0800275
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100276/* Parse the "cookie" server keyword */
277static int srv_parse_cookie(char **args, int *cur_arg,
278 struct proxy *curproxy, struct server *newsrv, char **err)
279{
280 char *arg;
281
282 arg = args[*cur_arg + 1];
283 if (!*arg) {
284 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
285 return ERR_ALERT | ERR_FATAL;
286 }
287
288 free(newsrv->cookie);
289 newsrv->cookie = strdup(arg);
290 newsrv->cklen = strlen(arg);
291 newsrv->flags |= SRV_F_COOKIESET;
292 return 0;
293}
294
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100295/* Parse the "disabled" server keyword */
296static int srv_parse_disabled(char **args, int *cur_arg,
297 struct proxy *curproxy, struct server *newsrv, char **err)
298{
Emeric Brun52a91d32017-08-31 14:41:55 +0200299 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
300 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100301 newsrv->check.state |= CHK_ST_PAUSED;
302 newsrv->check.health = 0;
303 return 0;
304}
305
306/* Parse the "enabled" server keyword */
307static int srv_parse_enabled(char **args, int *cur_arg,
308 struct proxy *curproxy, struct server *newsrv, char **err)
309{
Emeric Brun52a91d32017-08-31 14:41:55 +0200310 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
311 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100312 newsrv->check.state &= ~CHK_ST_PAUSED;
313 newsrv->check.health = newsrv->check.rise;
314 return 0;
315}
316
Willy Tarreau9c538e02019-01-23 10:21:49 +0100317static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
318{
319 char *arg;
320
321 arg = args[*cur_arg + 1];
322 if (!*arg) {
323 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
324 return ERR_ALERT | ERR_FATAL;
325 }
326 newsrv->max_reuse = atoi(arg);
327
328 return 0;
329}
330
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100331static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100332{
333 const char *res;
334 char *arg;
335 unsigned int time;
336
337 arg = args[*cur_arg + 1];
338 if (!*arg) {
339 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
340 return ERR_ALERT | ERR_FATAL;
341 }
342 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200343 if (res == PARSE_TIME_OVER) {
344 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
345 args[*cur_arg+1], args[*cur_arg]);
346 return ERR_ALERT | ERR_FATAL;
347 }
348 else if (res == PARSE_TIME_UNDER) {
349 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
350 args[*cur_arg+1], args[*cur_arg]);
351 return ERR_ALERT | ERR_FATAL;
352 }
353 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100354 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
355 *res, args[*cur_arg]);
356 return ERR_ALERT | ERR_FATAL;
357 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100358 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100359
360 return 0;
361}
362
Olivier Houchard006e3102018-12-10 18:30:32 +0100363static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
364{
365 char *arg;
366
367 arg = args[*cur_arg + 1];
368 if (!*arg) {
369 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
370 return ERR_ALERT | ERR_FATAL;
371 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100372
Olivier Houchard006e3102018-12-10 18:30:32 +0100373 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100374 if ((int)newsrv->max_idle_conns < -1) {
375 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
376 return ERR_ALERT | ERR_FATAL;
377 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100378
379 return 0;
380}
381
Willy Tarreaudff55432012-10-10 17:51:05 +0200382/* parse the "id" server keyword */
383static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
384{
385 struct eb32_node *node;
386
387 if (!*args[*cur_arg + 1]) {
388 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
389 return ERR_ALERT | ERR_FATAL;
390 }
391
392 newsrv->puid = atol(args[*cur_arg + 1]);
393 newsrv->conf.id.key = newsrv->puid;
394
395 if (newsrv->puid <= 0) {
396 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
397 return ERR_ALERT | ERR_FATAL;
398 }
399
400 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
401 if (node) {
402 struct server *target = container_of(node, struct server, conf.id);
403 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
404 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
405 target->id);
406 return ERR_ALERT | ERR_FATAL;
407 }
408
409 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200410 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200411 return 0;
412}
413
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100414/* Parse the "namespace" server keyword */
415static int srv_parse_namespace(char **args, int *cur_arg,
416 struct proxy *curproxy, struct server *newsrv, char **err)
417{
Willy Tarreaue5733232019-05-22 19:24:06 +0200418#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100419 char *arg;
420
421 arg = args[*cur_arg + 1];
422 if (!*arg) {
423 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
424 return ERR_ALERT | ERR_FATAL;
425 }
426
427 if (!strcmp(arg, "*")) {
428 /* Use the namespace associated with the connection (if present). */
429 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
430 return 0;
431 }
432
433 /*
434 * As this parser may be called several times for the same 'default-server'
435 * object, or for a new 'server' instance deriving from a 'default-server'
436 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
437 */
438 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
439
440 newsrv->netns = netns_store_lookup(arg, strlen(arg));
441 if (!newsrv->netns)
442 newsrv->netns = netns_store_insert(arg);
443
444 if (!newsrv->netns) {
445 memprintf(err, "Cannot open namespace '%s'", arg);
446 return ERR_ALERT | ERR_FATAL;
447 }
448
449 return 0;
450#else
451 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
452 return ERR_ALERT | ERR_FATAL;
453#endif
454}
455
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100456/* Parse the "no-backup" server keyword */
457static int srv_parse_no_backup(char **args, int *cur_arg,
458 struct proxy *curproxy, struct server *newsrv, char **err)
459{
460 newsrv->flags &= ~SRV_F_BACKUP;
461 return 0;
462}
463
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100464
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100465/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200466static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100467{
468 srv->pp_opts &= ~flags;
469 return 0;
470}
471
472/* Parse the "no-send-proxy" server keyword */
473static int srv_parse_no_send_proxy(char **args, int *cur_arg,
474 struct proxy *curproxy, struct server *newsrv, char **err)
475{
476 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
477}
478
479/* Parse the "no-send-proxy-v2" server keyword */
480static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
481 struct proxy *curproxy, struct server *newsrv, char **err)
482{
483 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
484}
485
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200486/* Parse the "no-tfo" server keyword */
487static int srv_parse_no_tfo(char **args, int *cur_arg,
488 struct proxy *curproxy, struct server *newsrv, char **err)
489{
490 newsrv->flags &= ~SRV_F_FASTOPEN;
491 return 0;
492}
493
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100494/* Parse the "non-stick" server keyword */
495static int srv_parse_non_stick(char **args, int *cur_arg,
496 struct proxy *curproxy, struct server *newsrv, char **err)
497{
498 newsrv->flags |= SRV_F_NON_STICK;
499 return 0;
500}
501
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100502/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200503static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100504{
505 srv->pp_opts |= flags;
506 return 0;
507}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200508/* parse the "proto" server keyword */
509static int srv_parse_proto(char **args, int *cur_arg,
510 struct proxy *px, struct server *newsrv, char **err)
511{
512 struct ist proto;
513
514 if (!*args[*cur_arg + 1]) {
515 memprintf(err, "'%s' : missing value", args[*cur_arg]);
516 return ERR_ALERT | ERR_FATAL;
517 }
518 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
519 newsrv->mux_proto = get_mux_proto(proto);
520 if (!newsrv->mux_proto) {
521 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
522 return ERR_ALERT | ERR_FATAL;
523 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200524 return 0;
525}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100526
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100527/* parse the "proxy-v2-options" */
528static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
529 struct proxy *px, struct server *newsrv, char **err)
530{
531 char *p, *n;
532 for (p = args[*cur_arg+1]; p; p = n) {
533 n = strchr(p, ',');
534 if (n)
535 *n++ = '\0';
536 if (!strcmp(p, "ssl")) {
537 newsrv->pp_opts |= SRV_PP_V2_SSL;
538 } else if (!strcmp(p, "cert-cn")) {
539 newsrv->pp_opts |= SRV_PP_V2_SSL;
540 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100541 } else if (!strcmp(p, "cert-key")) {
542 newsrv->pp_opts |= SRV_PP_V2_SSL;
543 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
544 } else if (!strcmp(p, "cert-sig")) {
545 newsrv->pp_opts |= SRV_PP_V2_SSL;
546 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
547 } else if (!strcmp(p, "ssl-cipher")) {
548 newsrv->pp_opts |= SRV_PP_V2_SSL;
549 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100550 } else if (!strcmp(p, "authority")) {
551 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100552 } else if (!strcmp(p, "crc32c")) {
553 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100554 } else if (!strcmp(p, "unique-id")) {
555 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100556 } else
557 goto fail;
558 }
559 return 0;
560 fail:
561 if (err)
562 memprintf(err, "'%s' : proxy v2 option not implemented", p);
563 return ERR_ALERT | ERR_FATAL;
564}
565
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100566/* Parse the "observe" server keyword */
567static int srv_parse_observe(char **args, int *cur_arg,
568 struct proxy *curproxy, struct server *newsrv, char **err)
569{
570 char *arg;
571
572 arg = args[*cur_arg + 1];
573 if (!*arg) {
574 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
575 return ERR_ALERT | ERR_FATAL;
576 }
577
578 if (!strcmp(arg, "none")) {
579 newsrv->observe = HANA_OBS_NONE;
580 }
581 else if (!strcmp(arg, "layer4")) {
582 newsrv->observe = HANA_OBS_LAYER4;
583 }
584 else if (!strcmp(arg, "layer7")) {
585 if (curproxy->mode != PR_MODE_HTTP) {
586 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
587 return ERR_ALERT;
588 }
589 newsrv->observe = HANA_OBS_LAYER7;
590 }
591 else {
592 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
593 "but got '%s'\n", args[*cur_arg], arg);
594 return ERR_ALERT | ERR_FATAL;
595 }
596
597 return 0;
598}
599
Frédéric Lécaille16186232017-03-14 16:42:49 +0100600/* Parse the "redir" server keyword */
601static int srv_parse_redir(char **args, int *cur_arg,
602 struct proxy *curproxy, struct server *newsrv, char **err)
603{
604 char *arg;
605
606 arg = args[*cur_arg + 1];
607 if (!*arg) {
608 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
609 return ERR_ALERT | ERR_FATAL;
610 }
611
612 free(newsrv->rdr_pfx);
613 newsrv->rdr_pfx = strdup(arg);
614 newsrv->rdr_len = strlen(arg);
615
616 return 0;
617}
618
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100619/* Parse the "send-proxy" server keyword */
620static int srv_parse_send_proxy(char **args, int *cur_arg,
621 struct proxy *curproxy, struct server *newsrv, char **err)
622{
623 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
624}
625
626/* Parse the "send-proxy-v2" server keyword */
627static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
628 struct proxy *curproxy, struct server *newsrv, char **err)
629{
630 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
631}
632
Frédéric Lécailledba97072017-03-17 15:33:50 +0100633
634/* Parse the "source" server keyword */
635static int srv_parse_source(char **args, int *cur_arg,
636 struct proxy *curproxy, struct server *newsrv, char **err)
637{
638 char *errmsg;
639 int port_low, port_high;
640 struct sockaddr_storage *sk;
641 struct protocol *proto;
642
643 errmsg = NULL;
644
645 if (!*args[*cur_arg + 1]) {
646 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
647 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
648 goto err;
649 }
650
651 /* 'sk' is statically allocated (no need to be freed). */
652 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
653 if (!sk) {
654 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
655 goto err;
656 }
657
658 proto = protocol_by_family(sk->ss_family);
659 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100660 ha_alert("'%s %s' : connect() not supported for this address family.\n",
661 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100662 goto err;
663 }
664
665 newsrv->conn_src.opts |= CO_SRC_BIND;
666 newsrv->conn_src.source_addr = *sk;
667
668 if (port_low != port_high) {
669 int i;
670
671 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100672 ha_alert("'%s' does not support port offsets (found '%s').\n",
673 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100674 goto err;
675 }
676
677 if (port_low <= 0 || port_low > 65535 ||
678 port_high <= 0 || port_high > 65535 ||
679 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100680 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100681 goto err;
682 }
683 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
684 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
685 newsrv->conn_src.sport_range->ports[i] = port_low + i;
686 }
687
688 *cur_arg += 2;
689 while (*(args[*cur_arg])) {
690 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
691#if defined(CONFIG_HAP_TRANSPARENT)
692 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100693 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
694 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100695 goto err;
696 }
697 if (!strcmp(args[*cur_arg + 1], "client")) {
698 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
699 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
700 }
701 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
702 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
703 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
704 }
705 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
706 char *name, *end;
707
708 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100709 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100710 name++;
711
712 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100713 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100714 end++;
715
716 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
717 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
718 free(newsrv->conn_src.bind_hdr_name);
719 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
720 newsrv->conn_src.bind_hdr_len = end - name;
721 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
722 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
723 newsrv->conn_src.bind_hdr_occ = -1;
724
725 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100726 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100727 end++;
728 if (*end == ',') {
729 end++;
730 name = end;
731 if (*end == '-')
732 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100733 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100734 end++;
735 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
736 }
737
738 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100739 ha_alert("usesrc hdr_ip(name,num) does not support negative"
740 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100741 goto err;
742 }
743 }
744 else {
745 struct sockaddr_storage *sk;
746 int port1, port2;
747
748 /* 'sk' is statically allocated (no need to be freed). */
749 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
750 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100751 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100752 goto err;
753 }
754
755 proto = protocol_by_family(sk->ss_family);
756 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100757 ha_alert("'%s %s' : connect() not supported for this address family.\n",
758 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100759 goto err;
760 }
761
762 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100763 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
764 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100765 goto err;
766 }
767 newsrv->conn_src.tproxy_addr = *sk;
768 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
769 }
770 global.last_checks |= LSTCHK_NETADM;
771 *cur_arg += 2;
772 continue;
773#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100774 ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100775 goto err;
776#endif /* defined(CONFIG_HAP_TRANSPARENT) */
777 } /* "usesrc" */
778
779 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
780#ifdef SO_BINDTODEVICE
781 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100782 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100783 goto err;
784 }
785 free(newsrv->conn_src.iface_name);
786 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
787 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
788 global.last_checks |= LSTCHK_NETADM;
789#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100790 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100791 goto err;
792#endif
793 *cur_arg += 2;
794 continue;
795 }
796 /* this keyword in not an option of "source" */
797 break;
798 } /* while */
799
800 return 0;
801
802 err:
803 free(errmsg);
804 return ERR_ALERT | ERR_FATAL;
805}
806
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100807/* Parse the "stick" server keyword */
808static int srv_parse_stick(char **args, int *cur_arg,
809 struct proxy *curproxy, struct server *newsrv, char **err)
810{
811 newsrv->flags &= ~SRV_F_NON_STICK;
812 return 0;
813}
814
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100815/* Parse the "track" server keyword */
816static int srv_parse_track(char **args, int *cur_arg,
817 struct proxy *curproxy, struct server *newsrv, char **err)
818{
819 char *arg;
820
821 arg = args[*cur_arg + 1];
822 if (!*arg) {
823 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
824 return ERR_ALERT | ERR_FATAL;
825 }
826
827 free(newsrv->trackit);
828 newsrv->trackit = strdup(arg);
829
830 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800831}
832
833/* Parse the "socks4" server keyword */
834static int srv_parse_socks4(char **args, int *cur_arg,
835 struct proxy *curproxy, struct server *newsrv, char **err)
836{
837 char *errmsg;
838 int port_low, port_high;
839 struct sockaddr_storage *sk;
840 struct protocol *proto;
841
842 errmsg = NULL;
843
844 if (!*args[*cur_arg + 1]) {
845 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
846 goto err;
847 }
848
849 /* 'sk' is statically allocated (no need to be freed). */
850 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
851 if (!sk) {
852 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
853 goto err;
854 }
855
856 proto = protocol_by_family(sk->ss_family);
857 if (!proto || !proto->connect) {
858 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
859 goto err;
860 }
861
862 newsrv->flags |= SRV_F_SOCKS4_PROXY;
863 newsrv->socks4_addr = *sk;
864
865 if (port_low != port_high) {
866 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
867 goto err;
868 }
869
870 if (!port_low) {
871 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
872 goto err;
873 }
874
875 return 0;
876
877 err:
878 free(errmsg);
879 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100880}
881
Frédéric Lécailledba97072017-03-17 15:33:50 +0100882
Willy Tarreau034c88c2017-01-23 23:36:45 +0100883/* parse the "tfo" server keyword */
884static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
885{
886 newsrv->flags |= SRV_F_FASTOPEN;
887 return 0;
888}
889
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200890/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200891 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200892 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200893 *
894 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200895 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200896void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200897{
Willy Tarreau87b09662015-04-03 00:22:06 +0200898 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200899
Willy Tarreau87b09662015-04-03 00:22:06 +0200900 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
901 if (stream->srv_conn == srv)
902 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200903}
904
905/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200906 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200907 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200908 *
909 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200910 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200911void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200912{
913 struct server *srv;
914
915 for (srv = px->srv; srv != NULL; srv = srv->next)
916 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200917 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200918}
919
Willy Tarreaubda92272014-05-20 21:55:30 +0200920/* Appends some information to a message string related to a server going UP or
921 * DOWN. If both <forced> and <reason> are null and the server tracks another
922 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200923 * If <check> is non-null, an entire string describing the check result will be
924 * appended after a comma and a space (eg: to report some information from the
925 * check that changed the state). In the other case, the string will be built
926 * using the check results stored into the struct server if present.
927 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200928 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200929 *
930 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200931 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200932void srv_append_status(struct buffer *msg, struct server *s,
933 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200934{
Emeric Brun5a133512017-10-19 14:42:30 +0200935 short status = s->op_st_chg.status;
936 short code = s->op_st_chg.code;
937 long duration = s->op_st_chg.duration;
938 char *desc = s->op_st_chg.reason;
939
940 if (check) {
941 status = check->status;
942 code = check->code;
943 duration = check->duration;
944 desc = check->desc;
945 }
946
947 if (status != -1) {
948 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
949
950 if (status >= HCHK_STATUS_L57DATA)
951 chunk_appendf(msg, ", code: %d", code);
952
953 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200954 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200955
956 chunk_appendf(msg, ", info: \"");
957
958 chunk_initlen(&src, desc, 0, strlen(desc));
959 chunk_asciiencode(msg, &src, '"');
960
961 chunk_appendf(msg, "\"");
962 }
963
964 if (duration >= 0)
965 chunk_appendf(msg, ", check duration: %ldms", duration);
966 }
967 else if (desc && *desc) {
968 chunk_appendf(msg, ", %s", desc);
969 }
970 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200971 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200972 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200973
974 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200975 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200976 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
977 " %d sessions active, %d requeued, %d remaining in queue",
978 s->proxy->srv_act, s->proxy->srv_bck,
979 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
980 s->cur_sess, xferred, s->nbpend);
981 else
982 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
983 " %d sessions requeued, %d total in queue",
984 s->proxy->srv_act, s->proxy->srv_bck,
985 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
986 xferred, s->nbpend);
987 }
988}
989
Emeric Brun5a133512017-10-19 14:42:30 +0200990/* Marks server <s> down, regardless of its checks' statuses. The server is
991 * registered in a list to postpone the counting of the remaining servers on
992 * the proxy and transfers queued streams whenever possible to other servers at
993 * a sync point. Maintenance servers are ignored. It stores the <reason> if
994 * non-null as the reason for going down or the available data from the check
995 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200996 *
997 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200998 */
Emeric Brun5a133512017-10-19 14:42:30 +0200999void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001000{
1001 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001002
Emeric Brun64cc49c2017-10-03 14:46:45 +02001003 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001004 return;
1005
Emeric Brun52a91d32017-08-31 14:41:55 +02001006 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001007 *s->op_st_chg.reason = 0;
1008 s->op_st_chg.status = -1;
1009 if (reason) {
1010 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1011 }
1012 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001013 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001014 s->op_st_chg.code = check->code;
1015 s->op_st_chg.status = check->status;
1016 s->op_st_chg.duration = check->duration;
1017 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001018
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001019 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001020 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001021
Emeric Brun9f0b4582017-10-23 14:39:51 +02001022 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001023 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001024 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001025 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001026 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001027}
1028
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001029/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001030 * in maintenance. The server is registered in a list to postpone the counting
1031 * of the remaining servers on the proxy and tries to grab requests from the
1032 * proxy at a sync point. Maintenance servers are ignored. It stores the
1033 * <reason> if non-null as the reason for going down or the available data
1034 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001035 *
1036 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001037 */
Emeric Brun5a133512017-10-19 14:42:30 +02001038void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001039{
1040 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001041
Emeric Brun64cc49c2017-10-03 14:46:45 +02001042 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001043 return;
1044
Emeric Brun52a91d32017-08-31 14:41:55 +02001045 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001046 return;
1047
Emeric Brun52a91d32017-08-31 14:41:55 +02001048 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001049 *s->op_st_chg.reason = 0;
1050 s->op_st_chg.status = -1;
1051 if (reason) {
1052 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1053 }
1054 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001055 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001056 s->op_st_chg.code = check->code;
1057 s->op_st_chg.status = check->status;
1058 s->op_st_chg.duration = check->duration;
1059 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001060
Emeric Brun64cc49c2017-10-03 14:46:45 +02001061 if (s->slowstart <= 0)
1062 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001063
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001064 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001065 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001066
Emeric Brun9f0b4582017-10-23 14:39:51 +02001067 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001068 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001069 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001070 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001071 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001072}
1073
Willy Tarreau8eb77842014-05-21 13:54:57 +02001074/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001075 * isn't in maintenance. The server is registered in a list to postpone the
1076 * counting of the remaining servers on the proxy and tries to grab requests
1077 * from the proxy. Maintenance servers are ignored. It stores the
1078 * <reason> if non-null as the reason for going down or the available data
1079 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001080 * up. Note that it makes use of the trash to build the log strings, so <reason>
1081 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001082 *
1083 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001084 */
Emeric Brun5a133512017-10-19 14:42:30 +02001085void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001086{
1087 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001088
Emeric Brun64cc49c2017-10-03 14:46:45 +02001089 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001090 return;
1091
Emeric Brun52a91d32017-08-31 14:41:55 +02001092 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001093 return;
1094
Emeric Brun52a91d32017-08-31 14:41:55 +02001095 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001096 *s->op_st_chg.reason = 0;
1097 s->op_st_chg.status = -1;
1098 if (reason) {
1099 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1100 }
1101 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001102 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001103 s->op_st_chg.code = check->code;
1104 s->op_st_chg.status = check->status;
1105 s->op_st_chg.duration = check->duration;
1106 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001107
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001108 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001109 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001110
Emeric Brun9f0b4582017-10-23 14:39:51 +02001111 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001112 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001113 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001114 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001115 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001116}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001117
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001118/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1119 * enforce either maint mode or drain mode. It is not allowed to set more than
1120 * one flag at once. The equivalent "inherited" flag is propagated to all
1121 * tracking servers. Maintenance mode disables health checks (but not agent
1122 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001123 * is done. If <cause> is non-null, it will be displayed at the end of the log
1124 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001125 *
1126 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001127 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001128void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001130 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001131
1132 if (!mode)
1133 return;
1134
1135 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001136 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001137 return;
1138
Emeric Brun52a91d32017-08-31 14:41:55 +02001139 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001140 if (cause)
1141 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1142
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001143 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001144 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001145
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001146 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001147 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1148 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001149 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001150
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001151 /* compute the inherited flag to propagate */
1152 if (mode & SRV_ADMF_MAINT)
1153 mode = SRV_ADMF_IMAINT;
1154 else if (mode & SRV_ADMF_DRAIN)
1155 mode = SRV_ADMF_IDRAIN;
1156
Emeric Brun9f0b4582017-10-23 14:39:51 +02001157 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001158 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001159 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001160 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001161 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001162}
1163
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001164/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1165 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1166 * than one flag at once. The equivalent "inherited" flag is propagated to all
1167 * tracking servers. Leaving maintenance mode re-enables health checks. When
1168 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001169 *
1170 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001171 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001172void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001173{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001174 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001175
1176 if (!mode)
1177 return;
1178
1179 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001180 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001181 return;
1182
Emeric Brun52a91d32017-08-31 14:41:55 +02001183 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001184
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001185 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001186 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001187
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001188 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001189 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1190 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001191 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001192
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001193 if (mode & SRV_ADMF_MAINT)
1194 mode = SRV_ADMF_IMAINT;
1195 else if (mode & SRV_ADMF_DRAIN)
1196 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001197
Emeric Brun9f0b4582017-10-23 14:39:51 +02001198 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001199 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001200 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001201 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001202 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001203}
1204
Willy Tarreau757478e2016-11-03 19:22:19 +01001205/* principle: propagate maint and drain to tracking servers. This is useful
1206 * upon startup so that inherited states are correct.
1207 */
1208static void srv_propagate_admin_state(struct server *srv)
1209{
1210 struct server *srv2;
1211
1212 if (!srv->trackers)
1213 return;
1214
1215 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001216 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001217 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001218 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001219
Emeric Brun52a91d32017-08-31 14:41:55 +02001220 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001221 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001223 }
1224}
1225
1226/* Compute and propagate the admin states for all servers in proxy <px>.
1227 * Only servers *not* tracking another one are considered, because other
1228 * ones will be handled when the server they track is visited.
1229 */
1230void srv_compute_all_admin_states(struct proxy *px)
1231{
1232 struct server *srv;
1233
1234 for (srv = px->srv; srv; srv = srv->next) {
1235 if (srv->track)
1236 continue;
1237 srv_propagate_admin_state(srv);
1238 }
1239}
1240
Willy Tarreaudff55432012-10-10 17:51:05 +02001241/* Note: must not be declared <const> as its list will be overwritten.
1242 * Please take care of keeping this list alphabetically sorted, doing so helps
1243 * all code contributors.
1244 * Optional keywords are also declared with a NULL ->parse() function so that
1245 * the config parser can report an appropriate error when a known keyword was
1246 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001247 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001248 */
1249static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001250 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001251 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001252 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1253 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001254 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001255 { "max-reuse", srv_parse_max_reuse, 1, 1 }, /* Set the max number of requests on a connection, -1 means unlimited */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001256 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001257 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001258 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1259 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001260 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001261 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001262 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001263 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1 }, /* Set the max number of orphan idle connections, 0 means unlimited */
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001264 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001265 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001266 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001267 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001268 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1269 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001270 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001271 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001272 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001273 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001274 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001275 { NULL, NULL, 0 },
1276}};
1277
Willy Tarreau0108d902018-11-25 19:14:37 +01001278INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001279
Willy Tarreau004e0452013-11-21 11:22:01 +01001280/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001281 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001282 * state is automatically disabled if the time is elapsed. If <must_update> is
1283 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001284 *
1285 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001286 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001287void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001288{
1289 struct proxy *px = sv->proxy;
1290 unsigned w;
1291
1292 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1293 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001294 if (sv->next_state == SRV_ST_STARTING)
1295 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001296 }
1297
1298 /* We must take care of not pushing the server to full throttle during slow starts.
1299 * It must also start immediately, at least at the minimal step when leaving maintenance.
1300 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001301 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001302 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1303 else
1304 w = px->lbprm.wdiv;
1305
Emeric Brun52a91d32017-08-31 14:41:55 +02001306 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001307
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001308 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001309 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001310 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001311}
1312
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001314 * Parses weight_str and configures sv accordingly.
1315 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001316 *
1317 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001318 */
1319const char *server_parse_weight_change_request(struct server *sv,
1320 const char *weight_str)
1321{
1322 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001323 long int w;
1324 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001325
1326 px = sv->proxy;
1327
1328 /* if the weight is terminated with '%', it is set relative to
1329 * the initial weight, otherwise it is absolute.
1330 */
1331 if (!*weight_str)
1332 return "Require <weight> or <weight%>.\n";
1333
Simon Hormanb796afa2013-02-12 10:45:53 +09001334 w = strtol(weight_str, &end, 10);
1335 if (end == weight_str)
1336 return "Empty weight string empty or preceded by garbage";
1337 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001338 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001339 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001340 /* Avoid integer overflow */
1341 if (w > 25600)
1342 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001343 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001344 if (w > 256)
1345 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001346 }
1347 else if (w < 0 || w > 256)
1348 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001349 else if (end[0] != '\0')
1350 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001351
1352 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1353 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1354
1355 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001356 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001357
1358 return NULL;
1359}
1360
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001361/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001362 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1363 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001364 * Returns:
1365 * - error string on error
1366 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001367 *
1368 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001369 */
1370const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001371 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001372{
1373 unsigned char ip[INET6_ADDRSTRLEN];
1374
1375 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001376 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001377 return NULL;
1378 }
1379 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001380 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001381 return NULL;
1382 }
1383
1384 return "Could not understand IP address format.\n";
1385}
1386
Willy Tarreau46b7f532018-08-21 11:54:26 +02001387/*
1388 * Must be called with the server lock held.
1389 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001390const char *server_parse_maxconn_change_request(struct server *sv,
1391 const char *maxconn_str)
1392{
1393 long int v;
1394 char *end;
1395
1396 if (!*maxconn_str)
1397 return "Require <maxconn>.\n";
1398
1399 v = strtol(maxconn_str, &end, 10);
1400 if (end == maxconn_str)
1401 return "maxconn string empty or preceded by garbage";
1402 else if (end[0] != '\0')
1403 return "Trailing garbage in maxconn string";
1404
1405 if (sv->maxconn == sv->minconn) { // static maxconn
1406 sv->maxconn = sv->minconn = v;
1407 } else { // dynamic maxconn
1408 sv->maxconn = v;
1409 }
1410
1411 if (may_dequeue_tasks(sv, sv->proxy))
1412 process_srv_queue(sv);
1413
1414 return NULL;
1415}
1416
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001417#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001418static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1419 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001420{
1421 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001422 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001423 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001424 NULL,
1425 };
1426
1427 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001428 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001429
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001430 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001431}
1432
1433static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1434{
1435 struct sample_expr *expr;
1436
1437 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001438 if (!expr) {
1439 memprintf(err, "error detected while parsing sni expression : %s", *err);
1440 return ERR_ALERT | ERR_FATAL;
1441 }
1442
1443 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1444 memprintf(err, "error detected while parsing sni expression : "
1445 " fetch method '%s' extracts information from '%s', "
1446 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001447 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001448 return ERR_ALERT | ERR_FATAL;
1449 }
1450
1451 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1452 release_sample_expr(newsrv->ssl_ctx.sni);
1453 newsrv->ssl_ctx.sni = expr;
1454
1455 return 0;
1456}
1457#endif
1458
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001459static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, int err_code, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001460{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001461 char *msg = "error encountered while processing ";
1462 char *quote = "'";
1463 char *token = args[cur_arg];
1464
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001465 if (err && *err) {
1466 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001467 msg = *err;
1468 quote = "";
1469 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001470 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001471
1472 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1473 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1474 file, linenum, args[0], args[1],
1475 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001476 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001477 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1478 file, linenum, args[0], args[1],
1479 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001480}
1481
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001482static void srv_conn_src_sport_range_cpy(struct server *srv,
1483 struct server *src)
1484{
1485 int range_sz;
1486
1487 range_sz = src->conn_src.sport_range->size;
1488 if (range_sz > 0) {
1489 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1490 if (srv->conn_src.sport_range != NULL) {
1491 int i;
1492
1493 for (i = 0; i < range_sz; i++) {
1494 srv->conn_src.sport_range->ports[i] =
1495 src->conn_src.sport_range->ports[i];
1496 }
1497 }
1498 }
1499}
1500
1501/*
1502 * Copy <src> server connection source settings to <srv> server everything needed.
1503 */
1504static void srv_conn_src_cpy(struct server *srv, struct server *src)
1505{
1506 srv->conn_src.opts = src->conn_src.opts;
1507 srv->conn_src.source_addr = src->conn_src.source_addr;
1508
1509 /* Source port range copy. */
1510 if (src->conn_src.sport_range != NULL)
1511 srv_conn_src_sport_range_cpy(srv, src);
1512
1513#ifdef CONFIG_HAP_TRANSPARENT
1514 if (src->conn_src.bind_hdr_name != NULL) {
1515 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1516 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1517 }
1518 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1519 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1520#endif
1521 if (src->conn_src.iface_name != NULL)
1522 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1523}
1524
1525/*
1526 * Copy <src> server SSL settings to <srv> server allocating
1527 * everything needed.
1528 */
1529#if defined(USE_OPENSSL)
1530static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1531{
1532 if (src->ssl_ctx.ca_file != NULL)
1533 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1534 if (src->ssl_ctx.crl_file != NULL)
1535 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1536 if (src->ssl_ctx.client_crt != NULL)
1537 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1538
1539 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1540
1541 if (src->ssl_ctx.verify_host != NULL)
1542 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1543 if (src->ssl_ctx.ciphers != NULL)
1544 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001545 if (src->ssl_ctx.options)
1546 srv->ssl_ctx.options = src->ssl_ctx.options;
1547 if (src->ssl_ctx.methods.flags)
1548 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1549 if (src->ssl_ctx.methods.min)
1550 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1551 if (src->ssl_ctx.methods.max)
1552 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1553
Willy Tarreau5db847a2019-05-09 14:13:35 +02001554#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001555 if (src->ssl_ctx.ciphersuites != NULL)
1556 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1557#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001558 if (src->sni_expr != NULL)
1559 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001560
1561#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1562 if (src->ssl_ctx.alpn_str) {
1563 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1564 if (srv->ssl_ctx.alpn_str) {
1565 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1566 src->ssl_ctx.alpn_len);
1567 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1568 }
1569 }
1570#endif
1571#ifdef OPENSSL_NPN_NEGOTIATED
1572 if (src->ssl_ctx.npn_str) {
1573 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1574 if (srv->ssl_ctx.npn_str) {
1575 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1576 src->ssl_ctx.npn_len);
1577 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1578 }
1579 }
1580#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001581}
1582#endif
1583
1584/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001585 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001586 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001587 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001588 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001589static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001590{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001591 char *hostname_dn;
1592 int hostname_len, hostname_dn_len;
1593
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001594 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001595 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001596
Christopher Faulet67957bd2017-09-27 11:00:59 +02001597 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001598 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001599 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1600 hostname_dn, trash.size);
1601 if (hostname_dn_len == -1)
1602 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001603
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001604
Christopher Faulet67957bd2017-09-27 11:00:59 +02001605 free(srv->hostname);
1606 free(srv->hostname_dn);
1607 srv->hostname = strdup(hostname);
1608 srv->hostname_dn = strdup(hostname_dn);
1609 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001610 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001611 goto err;
1612
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001613 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001614
1615 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001616 free(srv->hostname); srv->hostname = NULL;
1617 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001618 return -1;
1619}
1620
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001621/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001622 * Copy <src> server settings to <srv> server allocating
1623 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001624 * This function is not supposed to be called at any time, but only
1625 * during server settings parsing or during server allocations from
1626 * a server template, and just after having calloc()'ed a new server.
1627 * So, <src> may only be a default server (when parsing server settings)
1628 * or a server template (during server allocations from a server template).
1629 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1630 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001631 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001632static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001633{
1634 /* Connection source settings copy */
1635 srv_conn_src_cpy(srv, src);
1636
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001637 if (srv_tmpl) {
1638 srv->addr = src->addr;
1639 srv->svc_port = src->svc_port;
1640 }
1641
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001642 srv->pp_opts = src->pp_opts;
1643 if (src->rdr_pfx != NULL) {
1644 srv->rdr_pfx = strdup(src->rdr_pfx);
1645 srv->rdr_len = src->rdr_len;
1646 }
1647 if (src->cookie != NULL) {
1648 srv->cookie = strdup(src->cookie);
1649 srv->cklen = src->cklen;
1650 }
1651 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001652 srv->check.addr = src->check.addr;
1653 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001654 srv->check.use_ssl = src->check.use_ssl;
1655 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001656 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001657 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001658 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001659 /* Note: 'flags' field has potentially been already initialized. */
1660 srv->flags |= src->flags;
1661 srv->do_check = src->do_check;
1662 srv->do_agent = src->do_agent;
1663 if (srv->check.port)
1664 srv->flags |= SRV_F_CHECKPORT;
1665 srv->check.inter = src->check.inter;
1666 srv->check.fastinter = src->check.fastinter;
1667 srv->check.downinter = src->check.downinter;
1668 srv->agent.use_ssl = src->agent.use_ssl;
1669 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001670
1671 if (src->agent.tcpcheck_rules) {
1672 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1673 if (srv->agent.tcpcheck_rules) {
1674 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1675 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1676 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1677 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1678 &src->agent.tcpcheck_rules->preset_vars);
1679 }
1680 }
1681
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001682 srv->agent.inter = src->agent.inter;
1683 srv->agent.fastinter = src->agent.fastinter;
1684 srv->agent.downinter = src->agent.downinter;
1685 srv->maxqueue = src->maxqueue;
1686 srv->minconn = src->minconn;
1687 srv->maxconn = src->maxconn;
1688 srv->slowstart = src->slowstart;
1689 srv->observe = src->observe;
1690 srv->onerror = src->onerror;
1691 srv->onmarkeddown = src->onmarkeddown;
1692 srv->onmarkedup = src->onmarkedup;
1693 if (src->trackit != NULL)
1694 srv->trackit = strdup(src->trackit);
1695 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1696 srv->uweight = srv->iweight = src->iweight;
1697
1698 srv->check.send_proxy = src->check.send_proxy;
1699 /* health: up, but will fall down at first failure */
1700 srv->check.rise = srv->check.health = src->check.rise;
1701 srv->check.fall = src->check.fall;
1702
1703 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1705 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1706 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001707 srv->check.state |= CHK_ST_PAUSED;
1708 srv->check.health = 0;
1709 }
1710
1711 /* health: up but will fall down at first failure */
1712 srv->agent.rise = srv->agent.health = src->agent.rise;
1713 srv->agent.fall = src->agent.fall;
1714
1715 if (src->resolvers_id != NULL)
1716 srv->resolvers_id = strdup(src->resolvers_id);
1717 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001718 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001719 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001720 if (srv->dns_opts.family_prio == AF_UNSPEC)
1721 srv->dns_opts.family_prio = AF_INET6;
1722 memcpy(srv->dns_opts.pref_net,
1723 src->dns_opts.pref_net,
1724 sizeof srv->dns_opts.pref_net);
1725 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1726
1727 srv->init_addr_methods = src->init_addr_methods;
1728 srv->init_addr = src->init_addr;
1729#if defined(USE_OPENSSL)
1730 srv_ssl_settings_cpy(srv, src);
1731#endif
1732#ifdef TCP_USER_TIMEOUT
1733 srv->tcp_ut = src->tcp_ut;
1734#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001735 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001736 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001737 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001738 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001739
Olivier Houchard8da5f982017-08-04 18:35:36 +02001740 if (srv_tmpl)
1741 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001742
1743 srv->check.via_socks4 = src->check.via_socks4;
1744 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001745}
1746
William Lallemand313bfd12018-10-26 14:47:32 +02001747struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001748{
1749 struct server *srv;
1750
1751 srv = calloc(1, sizeof *srv);
1752 if (!srv)
1753 return NULL;
1754
1755 srv->obj_type = OBJ_TYPE_SERVER;
1756 srv->proxy = proxy;
1757 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001758 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001759
Emeric Brun52a91d32017-08-31 14:41:55 +02001760 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001761 srv->last_change = now.tv_sec;
1762
Christopher Faulet38290462020-04-21 11:46:40 +02001763 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001764 srv->check.status = HCHK_STATUS_INI;
1765 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001766 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001767 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001768
Christopher Faulet38290462020-04-21 11:46:40 +02001769 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001770 srv->agent.status = HCHK_STATUS_INI;
1771 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001772 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001773 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1774
Willy Tarreau975b1552019-06-06 16:25:55 +02001775 /* please don't put default server settings here, they are set in
1776 * init_default_instance().
1777 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001778 return srv;
1779}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001780
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001781#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1782static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1783 struct server *srv, struct proxy *proxy)
1784{
1785 int ret;
1786 char *err = NULL;
1787
1788 if (!srv->sni_expr)
1789 return 0;
1790
1791 ret = server_parse_sni_expr(srv, proxy, &err);
1792 if (!ret)
1793 return 0;
1794
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001795 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001796 free(err);
1797
1798 return ret;
1799}
1800#endif
1801
1802/*
1803 * Server initializations finalization.
1804 * Initialize health check, agent check and SNI expression if enabled.
1805 * Must not be called for a default server instance.
1806 */
1807static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1808 struct server *srv, struct proxy *px)
1809{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001810#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001811 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001812#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001813
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001814 if (srv->do_check && srv->trackit) {
1815 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1816 file, linenum);
1817 return ERR_ALERT | ERR_FATAL;
1818 }
1819
1820 if (srv->do_agent && !srv->agent.port) {
1821 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1822 file, linenum, srv->id);
1823 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001824 }
1825
1826#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1827 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1828 return ret;
1829#endif
1830
1831 if (srv->flags & SRV_F_BACKUP)
1832 px->srv_bck++;
1833 else
1834 px->srv_act++;
1835 srv_lb_commit_status(srv);
1836
1837 return 0;
1838}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001839
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001840/*
1841 * Parse as much as possible such a range string argument: low[-high]
1842 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1843 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1844 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1845 * Returns 0 if succeeded, -1 if not.
1846 */
1847static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1848{
1849 char *nb_high_arg;
1850
1851 *nb_high = 0;
1852 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001854
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001855 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001856 *nb_high_arg++ = '\0';
1857 *nb_high = atoi(nb_high_arg);
1858 }
1859 else {
1860 *nb_high += *nb_low;
1861 *nb_low = 1;
1862 }
1863
1864 if (*nb_low < 0 || *nb_high < *nb_low)
1865 return -1;
1866
1867 return 0;
1868}
1869
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001870static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1871{
1872 chunk_printf(&trash, "%s%d", prefix, nb);
1873 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001874 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001875}
1876
1877/*
1878 * Initialize as much as possible servers from <srv> server template.
1879 * Note that a server template is a special server with
1880 * a few different parameters than a server which has
1881 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001882 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001883 * 'srv' template included.
1884 */
1885static int server_template_init(struct server *srv, struct proxy *px)
1886{
1887 int i;
1888 struct server *newsrv;
1889
1890 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001891 newsrv = new_server(px);
1892 if (!newsrv)
1893 goto err;
1894
1895 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001896 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001897#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1898 if (newsrv->sni_expr) {
1899 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1900 if (!newsrv->ssl_ctx.sni)
1901 goto err;
1902 }
1903#endif
1904 /* Set this new server ID. */
1905 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1906
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001907 /* Linked backwards first. This will be restablished after parsing. */
1908 newsrv->next = px->srv;
1909 px->srv = newsrv;
1910 }
1911 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1912
1913 return i - srv->tmpl_info.nb_low;
1914
1915 err:
1916 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1917 if (newsrv) {
1918#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1919 release_sample_expr(newsrv->ssl_ctx.sni);
1920#endif
1921 free_check(&newsrv->agent);
1922 free_check(&newsrv->check);
1923 }
1924 free(newsrv);
1925 return i - srv->tmpl_info.nb_low;
1926}
1927
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001928int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr, int in_peers_section)
Willy Tarreau272adea2014-03-31 10:39:59 +02001929{
1930 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001931 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001932 char *errmsg = NULL;
1933 int err_code = 0;
1934 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001935 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001936
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001937 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001938 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001939 !strcmp(args[0], "default-server") ||
1940 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001941 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001942 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001943 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001944 int srv_tmpl = !defsrv && !srv;
1945 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001946
1947 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001948 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 err_code |= ERR_ALERT | ERR_FATAL;
1950 goto out;
1951 }
1952 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001953 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001954
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001955 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001956 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001957 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001958 if (in_peers_section) {
1959 return 0;
1960 }
1961 else {
1962 /* 'server' line number of argument check. */
1963 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1964 file, linenum, args[0]);
1965 err_code |= ERR_ALERT | ERR_FATAL;
1966 goto out;
1967 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001968 }
1969
1970 err = invalid_char(args[1]);
1971 }
1972 else if (srv_tmpl) {
1973 if (!*args[3]) {
1974 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001975 ha_alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001976 file, linenum, args[0]);
1977 err_code |= ERR_ALERT | ERR_FATAL;
1978 goto out;
1979 }
1980
1981 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001982 }
1983
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001984 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001985 ha_alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001986 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001987 err_code |= ERR_ALERT | ERR_FATAL;
1988 goto out;
1989 }
1990
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001991 cur_arg = 2;
1992 if (srv_tmpl) {
1993 /* Parse server-template <nb | range> arg. */
1994 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001995 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001996 file, linenum, args[0], args[cur_arg]);
1997 err_code |= ERR_ALERT | ERR_FATAL;
1998 goto out;
1999 }
2000 cur_arg++;
2001 }
2002
Willy Tarreau272adea2014-03-31 10:39:59 +02002003 if (!defsrv) {
2004 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002005 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 struct protocol *proto;
2007
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002008 newsrv = new_server(curproxy);
2009 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002010 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002011 err_code |= ERR_ALERT | ERR_ABORT;
2012 goto out;
2013 }
2014
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002015 if (srv_tmpl) {
2016 newsrv->tmpl_info.nb_low = tmpl_range_low;
2017 newsrv->tmpl_info.nb_high = tmpl_range_high;
2018 }
2019
Willy Tarreau272adea2014-03-31 10:39:59 +02002020 /* the servers are linked backwards first */
2021 newsrv->next = curproxy->srv;
2022 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002023 newsrv->conf.file = strdup(file);
2024 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002025 /* Note: for a server template, its id is its prefix.
2026 * This is a temporary id which will be used for server allocations to come
2027 * after parsing.
2028 */
2029 if (srv)
2030 newsrv->id = strdup(args[1]);
2031 else
2032 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002033
2034 /* several ways to check the port component :
2035 * - IP => port=+0, relative (IPv4 only)
2036 * - IP: => port=+0, relative
2037 * - IP:N => port=N, absolute
2038 * - IP:+N => port=+N, relative
2039 * - IP:-N => port=-N, relative
2040 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002041 if (!parse_addr)
2042 goto skip_addr;
2043
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002044 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002045 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002046 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002047 err_code |= ERR_ALERT | ERR_FATAL;
2048 goto out;
2049 }
2050
2051 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002052 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002053 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002054 file, linenum, args[0], args[1]);
2055 err_code |= ERR_ALERT | ERR_FATAL;
2056 goto out;
2057 }
2058
2059 if (!port1 || !port2) {
2060 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002061 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002062 }
2063 else if (port1 != port2) {
2064 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002065 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002066 file, linenum, args[0], args[1], args[2]);
2067 err_code |= ERR_ALERT | ERR_FATAL;
2068 goto out;
2069 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002070
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002071 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002072 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002073 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002074 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002075 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2076 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2077 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002078 err_code |= ERR_ALERT | ERR_FATAL;
2079 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002080 }
2081 }
2082 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002083 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002084 file, linenum, newsrv->id);
2085 err_code |= ERR_ALERT | ERR_FATAL;
2086 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002087 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002088 }
2089
Willy Tarreau272adea2014-03-31 10:39:59 +02002090 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002091 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002092
Olivier Houchard8da5f982017-08-04 18:35:36 +02002093 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002094 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002095 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002096 err_code |= ERR_ALERT | ERR_FATAL;
2097 goto out;
2098 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002099
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002100 cur_arg++;
2101 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002102 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002103 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002104 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002105 } else {
2106 newsrv = &curproxy->defsrv;
2107 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002108 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002109 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002110 }
2111
2112 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002113 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002114 char *p, *end;
2115 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002116 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002117
2118 newsrv->init_addr_methods = 0;
2119 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2120
2121 for (p = args[cur_arg + 1]; *p; p = end) {
2122 /* cut on next comma */
2123 for (end = p; *end && *end != ','; end++);
2124 if (*end)
2125 *(end++) = 0;
2126
Willy Tarreau4310d362016-11-02 15:05:56 +01002127 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002128 if (!strcmp(p, "libc")) {
2129 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2130 }
2131 else if (!strcmp(p, "last")) {
2132 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2133 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002134 else if (!strcmp(p, "none")) {
2135 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2136 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002137 else if (str2ip2(p, &sa, 0)) {
2138 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002139 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002140 file, linenum, args[cur_arg], p);
2141 err_code |= ERR_ALERT | ERR_FATAL;
2142 goto out;
2143 }
2144 newsrv->init_addr = sa;
2145 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2146 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002147 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002148 ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002149 file, linenum, args[cur_arg], p);
2150 err_code |= ERR_ALERT | ERR_FATAL;
2151 goto out;
2152 }
2153 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002154 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002155 file, linenum, args[cur_arg], p);
2156 err_code |= ERR_ALERT | ERR_FATAL;
2157 goto out;
2158 }
2159 }
2160 cur_arg += 2;
2161 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002162 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002163 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002164 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2165 cur_arg += 2;
2166 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002167 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2168 char *p, *end;
2169
2170 for (p = args[cur_arg + 1]; *p; p = end) {
2171 /* cut on next comma */
2172 for (end = p; *end && *end != ','; end++);
2173 if (*end)
2174 *(end++) = 0;
2175
2176 if (!strcmp(p, "allow-dup-ip")) {
2177 newsrv->dns_opts.accept_duplicate_ip = 1;
2178 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002179 else if (!strcmp(p, "ignore-weight")) {
2180 newsrv->dns_opts.ignore_weight = 1;
2181 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002182 else if (!strcmp(p, "prevent-dup-ip")) {
2183 newsrv->dns_opts.accept_duplicate_ip = 0;
2184 }
2185 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002186 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.\n",
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002187 file, linenum, args[cur_arg], p);
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
2191 }
2192
2193 cur_arg += 2;
2194 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002195 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2196 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002197 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002198 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002199 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002200 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002201 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002202 file, linenum, args[cur_arg]);
2203 err_code |= ERR_ALERT | ERR_FATAL;
2204 goto out;
2205 }
2206 cur_arg += 2;
2207 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002208 else if (!strcmp(args[cur_arg], "resolve-net")) {
2209 char *p, *e;
2210 unsigned char mask;
2211 struct dns_options *opt;
2212
2213 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002214 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002215 file, linenum, args[cur_arg]);
2216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
2219
2220 opt = &newsrv->dns_opts;
2221
2222 /* Split arguments by comma, and convert it from ipv4 or ipv6
2223 * string network in in_addr or in6_addr.
2224 */
2225 p = args[cur_arg + 1];
2226 e = p;
2227 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002228 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002229 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002230 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002231 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2232 err_code |= ERR_ALERT | ERR_FATAL;
2233 goto out;
2234 }
2235 /* look for end or comma. */
2236 while (*e != ',' && *e != '\0')
2237 e++;
2238 if (*e == ',') {
2239 *e = '\0';
2240 e++;
2241 }
2242 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2243 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2244 /* Try to convert input string from ipv4 or ipv6 network. */
2245 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2246 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2247 &mask)) {
2248 /* Try to convert input string from ipv6 network. */
2249 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2250 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2251 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002252 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002253 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002254 file, linenum, args[cur_arg], p);
2255 err_code |= ERR_ALERT | ERR_FATAL;
2256 goto out;
2257 }
2258 opt->pref_net_nb++;
2259 p = e;
2260 }
2261
2262 cur_arg += 2;
2263 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002264 else if (!strcmp(args[cur_arg], "weight")) {
2265 int w;
2266 w = atol(args[cur_arg + 1]);
2267 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002268 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002269 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2270 err_code |= ERR_ALERT | ERR_FATAL;
2271 goto out;
2272 }
2273 newsrv->uweight = newsrv->iweight = w;
2274 cur_arg += 2;
2275 }
Emeric Brun97556472020-05-30 01:42:45 +02002276 else if (!strcmp(args[cur_arg], "log-proto")) {
2277 if (!strcmp(args[cur_arg + 1], "legacy"))
2278 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2279 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2280 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2281 else {
2282 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2283 "'octet-count' but got '%s'\n",
2284 file, linenum, args[cur_arg], args[cur_arg + 1]);
2285 err_code |= ERR_ALERT | ERR_FATAL;
2286 goto out;
2287 }
2288 cur_arg += 2;
2289 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002290 else if (!strcmp(args[cur_arg], "minconn")) {
2291 newsrv->minconn = atol(args[cur_arg + 1]);
2292 cur_arg += 2;
2293 }
2294 else if (!strcmp(args[cur_arg], "maxconn")) {
2295 newsrv->maxconn = atol(args[cur_arg + 1]);
2296 cur_arg += 2;
2297 }
2298 else if (!strcmp(args[cur_arg], "maxqueue")) {
2299 newsrv->maxqueue = atol(args[cur_arg + 1]);
2300 cur_arg += 2;
2301 }
2302 else if (!strcmp(args[cur_arg], "slowstart")) {
2303 /* slowstart is stored in seconds */
2304 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002305
2306 if (err == PARSE_TIME_OVER) {
2307 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2308 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2309 err_code |= ERR_ALERT | ERR_FATAL;
2310 goto out;
2311 }
2312 else if (err == PARSE_TIME_UNDER) {
2313 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2314 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2315 err_code |= ERR_ALERT | ERR_FATAL;
2316 goto out;
2317 }
2318 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002319 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002320 file, linenum, *err, newsrv->id);
2321 err_code |= ERR_ALERT | ERR_FATAL;
2322 goto out;
2323 }
2324 newsrv->slowstart = (val + 999) / 1000;
2325 cur_arg += 2;
2326 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002327 else if (!strcmp(args[cur_arg], "on-error")) {
2328 if (!strcmp(args[cur_arg + 1], "fastinter"))
2329 newsrv->onerror = HANA_ONERR_FASTINTER;
2330 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2331 newsrv->onerror = HANA_ONERR_FAILCHK;
2332 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2333 newsrv->onerror = HANA_ONERR_SUDDTH;
2334 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2335 newsrv->onerror = HANA_ONERR_MARKDWN;
2336 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002337 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002338 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2339 file, linenum, args[cur_arg], args[cur_arg + 1]);
2340 err_code |= ERR_ALERT | ERR_FATAL;
2341 goto out;
2342 }
2343
2344 cur_arg += 2;
2345 }
2346 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2347 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2348 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2349 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002350 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002351 file, linenum, args[cur_arg], args[cur_arg + 1]);
2352 err_code |= ERR_ALERT | ERR_FATAL;
2353 goto out;
2354 }
2355
2356 cur_arg += 2;
2357 }
2358 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2359 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2360 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2361 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002362 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002363 file, linenum, args[cur_arg], args[cur_arg + 1]);
2364 err_code |= ERR_ALERT | ERR_FATAL;
2365 goto out;
2366 }
2367
2368 cur_arg += 2;
2369 }
2370 else if (!strcmp(args[cur_arg], "error-limit")) {
2371 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002372 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002373 file, linenum, args[cur_arg]);
2374 err_code |= ERR_ALERT | ERR_FATAL;
2375 goto out;
2376 }
2377
2378 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2379
2380 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002381 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002382 file, linenum, args[cur_arg]);
2383 err_code |= ERR_ALERT | ERR_FATAL;
2384 goto out;
2385 }
2386 cur_arg += 2;
2387 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002388 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002389 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002390 file, linenum, "usesrc", "source");
2391 err_code |= ERR_ALERT | ERR_FATAL;
2392 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002393 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002394 else {
2395 static int srv_dumped;
2396 struct srv_kw *kw;
2397 char *err;
2398
2399 kw = srv_find_kw(args[cur_arg]);
2400 if (kw) {
2401 char *err = NULL;
2402 int code;
2403
2404 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002405 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002406 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002407 if (kw->skip != -1)
2408 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002409 err_code |= ERR_ALERT | ERR_FATAL;
2410 goto out;
2411 }
2412
2413 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002414 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002416 if (kw->skip != -1)
2417 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002418 err_code |= ERR_ALERT;
2419 continue;
2420 }
2421
2422 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2423 err_code |= code;
2424
2425 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002426 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002427 if (code & ERR_FATAL) {
2428 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002429 if (kw->skip != -1)
2430 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002431 goto out;
2432 }
2433 }
2434 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002435 if (kw->skip != -1)
2436 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002437 continue;
2438 }
2439
2440 err = NULL;
2441 if (!srv_dumped) {
2442 srv_dump_kws(&err);
2443 indent_msg(&err, 4);
2444 srv_dumped = 1;
2445 }
2446
Christopher Faulet767a84b2017-11-24 16:50:31 +01002447 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002448 file, linenum, args[0], args[1], args[cur_arg],
2449 err ? " Registered keywords :" : "", err ? err : "");
2450 free(err);
2451
2452 err_code |= ERR_ALERT | ERR_FATAL;
2453 goto out;
2454 }
2455 }
2456
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002457 if (!defsrv)
2458 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2459 if (err_code & ERR_FATAL)
2460 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002461 if (srv_tmpl)
2462 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002463 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002464 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002465 return 0;
2466
2467 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002468 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002469 free(errmsg);
2470 return err_code;
2471}
2472
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002473/* Returns a pointer to the first server matching either id <id>.
2474 * NULL is returned if no match is found.
2475 * the lookup is performed in the backend <bk>
2476 */
2477struct server *server_find_by_id(struct proxy *bk, int id)
2478{
2479 struct eb32_node *eb32;
2480 struct server *curserver;
2481
2482 if (!bk || (id ==0))
2483 return NULL;
2484
2485 /* <bk> has no backend capabilities, so it can't have a server */
2486 if (!(bk->cap & PR_CAP_BE))
2487 return NULL;
2488
2489 curserver = NULL;
2490
2491 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2492 if (eb32)
2493 curserver = container_of(eb32, struct server, conf.id);
2494
2495 return curserver;
2496}
2497
2498/* Returns a pointer to the first server matching either name <name>, or id
2499 * if <name> starts with a '#'. NULL is returned if no match is found.
2500 * the lookup is performed in the backend <bk>
2501 */
2502struct server *server_find_by_name(struct proxy *bk, const char *name)
2503{
2504 struct server *curserver;
2505
2506 if (!bk || !name)
2507 return NULL;
2508
2509 /* <bk> has no backend capabilities, so it can't have a server */
2510 if (!(bk->cap & PR_CAP_BE))
2511 return NULL;
2512
2513 curserver = NULL;
2514 if (*name == '#') {
2515 curserver = server_find_by_id(bk, atoi(name + 1));
2516 if (curserver)
2517 return curserver;
2518 }
2519 else {
2520 curserver = bk->srv;
2521
2522 while (curserver && (strcmp(curserver->id, name) != 0))
2523 curserver = curserver->next;
2524
2525 if (curserver)
2526 return curserver;
2527 }
2528
2529 return NULL;
2530}
2531
2532struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2533{
2534 struct server *byname;
2535 struct server *byid;
2536
2537 if (!name && !id)
2538 return NULL;
2539
2540 if (diff)
2541 *diff = 0;
2542
2543 byname = byid = NULL;
2544
2545 if (name) {
2546 byname = server_find_by_name(bk, name);
2547 if (byname && (!id || byname->puid == id))
2548 return byname;
2549 }
2550
2551 /* remaining possibilities :
2552 * - name not set
2553 * - name set but not found
2554 * - name found but ID doesn't match
2555 */
2556 if (id) {
2557 byid = server_find_by_id(bk, id);
2558 if (byid) {
2559 if (byname) {
2560 /* use id only if forced by configuration */
2561 if (byid->flags & SRV_F_FORCED_ID) {
2562 if (diff)
2563 *diff |= 2;
2564 return byid;
2565 }
2566 else {
2567 if (diff)
2568 *diff |= 1;
2569 return byname;
2570 }
2571 }
2572
2573 /* remaining possibilities:
2574 * - name not set
2575 * - name set but not found
2576 */
2577 if (name && diff)
2578 *diff |= 2;
2579 return byid;
2580 }
2581
2582 /* id bot found */
2583 if (byname) {
2584 if (diff)
2585 *diff |= 1;
2586 return byname;
2587 }
2588 }
2589
2590 return NULL;
2591}
2592
Willy Tarreau46b7f532018-08-21 11:54:26 +02002593/* Update a server state using the parameters available in the params list.
2594 *
2595 * Grabs the server lock during operation.
2596 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002597static void srv_update_state(struct server *srv, int version, char **params)
2598{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002599 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002600 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002601
2602 /* fields since version 1
2603 * and common to all other upcoming versions
2604 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002605 enum srv_state srv_op_state;
2606 enum srv_admin srv_admin_state;
2607 unsigned srv_uweight, srv_iweight;
2608 unsigned long srv_last_time_change;
2609 short srv_check_status;
2610 enum chk_result srv_check_result;
2611 int srv_check_health;
2612 int srv_check_state, srv_agent_state;
2613 int bk_f_forced_id;
2614 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002615 int fqdn_set_by_cli;
2616 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002617 const char *port_str;
2618 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002619 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002620
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002621 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002622 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002623 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002624 switch (version) {
2625 case 1:
2626 /*
2627 * now we can proceed with server's state update:
2628 * srv_addr: params[0]
2629 * srv_op_state: params[1]
2630 * srv_admin_state: params[2]
2631 * srv_uweight: params[3]
2632 * srv_iweight: params[4]
2633 * srv_last_time_change: params[5]
2634 * srv_check_status: params[6]
2635 * srv_check_result: params[7]
2636 * srv_check_health: params[8]
2637 * srv_check_state: params[9]
2638 * srv_agent_state: params[10]
2639 * bk_f_forced_id: params[11]
2640 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002641 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002642 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002643 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002644 */
2645
2646 /* validating srv_op_state */
2647 p = NULL;
2648 errno = 0;
2649 srv_op_state = strtol(params[1], &p, 10);
2650 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2651 (srv_op_state != SRV_ST_STOPPED &&
2652 srv_op_state != SRV_ST_STARTING &&
2653 srv_op_state != SRV_ST_RUNNING &&
2654 srv_op_state != SRV_ST_STOPPING)) {
2655 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2656 }
2657
2658 /* validating srv_admin_state */
2659 p = NULL;
2660 errno = 0;
2661 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002662 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002663
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002664 /* inherited statuses will be recomputed later.
2665 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2666 */
2667 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002668
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002669 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2670 (srv_admin_state != 0 &&
2671 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002672 srv_admin_state != SRV_ADMF_CMAINT &&
2673 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002674 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002675 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002676 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2677 }
2678
2679 /* validating srv_uweight */
2680 p = NULL;
2681 errno = 0;
2682 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002683 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002684 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2685
2686 /* validating srv_iweight */
2687 p = NULL;
2688 errno = 0;
2689 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002690 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002691 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2692
2693 /* validating srv_last_time_change */
2694 p = NULL;
2695 errno = 0;
2696 srv_last_time_change = strtol(params[5], &p, 10);
2697 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2698 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2699
2700 /* validating srv_check_status */
2701 p = NULL;
2702 errno = 0;
2703 srv_check_status = strtol(params[6], &p, 10);
2704 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2705 (srv_check_status >= HCHK_STATUS_SIZE))
2706 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2707
2708 /* validating srv_check_result */
2709 p = NULL;
2710 errno = 0;
2711 srv_check_result = strtol(params[7], &p, 10);
2712 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2713 (srv_check_result != CHK_RES_UNKNOWN &&
2714 srv_check_result != CHK_RES_NEUTRAL &&
2715 srv_check_result != CHK_RES_FAILED &&
2716 srv_check_result != CHK_RES_PASSED &&
2717 srv_check_result != CHK_RES_CONDPASS)) {
2718 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2719 }
2720
2721 /* validating srv_check_health */
2722 p = NULL;
2723 errno = 0;
2724 srv_check_health = strtol(params[8], &p, 10);
2725 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2726 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2727
2728 /* validating srv_check_state */
2729 p = NULL;
2730 errno = 0;
2731 srv_check_state = strtol(params[9], &p, 10);
2732 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2733 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2734 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2735
2736 /* validating srv_agent_state */
2737 p = NULL;
2738 errno = 0;
2739 srv_agent_state = strtol(params[10], &p, 10);
2740 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2741 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2742 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2743
2744 /* validating bk_f_forced_id */
2745 p = NULL;
2746 errno = 0;
2747 bk_f_forced_id = strtol(params[11], &p, 10);
2748 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2749 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2750
2751 /* validating srv_f_forced_id */
2752 p = NULL;
2753 errno = 0;
2754 srv_f_forced_id = strtol(params[12], &p, 10);
2755 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2756 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2757
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002758 /* validating srv_fqdn */
2759 fqdn = params[13];
2760 if (fqdn && *fqdn == '-')
2761 fqdn = NULL;
2762 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2763 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2764 fqdn = NULL;
2765 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002766
Frédéric Lécaille31694712017-08-01 08:47:19 +02002767 port_str = params[14];
2768 if (port_str) {
2769 port = strl2uic(port_str, strlen(port_str));
2770 if (port > USHRT_MAX) {
2771 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2772 port_str = NULL;
2773 }
2774 }
2775
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002776 /* SRV record
2777 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2778 */
2779 srvrecord = params[15];
2780 if (srvrecord && *srvrecord != '_')
2781 srvrecord = NULL;
2782
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002783 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002784 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002785 goto out;
2786
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002787 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002788 /* recover operational state and apply it to this server
2789 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002790 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002791 switch (srv_op_state) {
2792 case SRV_ST_STOPPED:
2793 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002794 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002795 break;
2796 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002797 /* If rise == 1 there is no STARTING state, let's switch to
2798 * RUNNING
2799 */
2800 if (srv->check.rise == 1) {
2801 srv->check.health = srv->check.rise + srv->check.fall - 1;
2802 srv_set_running(srv, "", NULL);
2803 break;
2804 }
2805 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2806 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002807 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002808 break;
2809 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002810 /* If fall == 1 there is no STOPPING state, let's switch to
2811 * STOPPED
2812 */
2813 if (srv->check.fall == 1) {
2814 srv->check.health = 0;
2815 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2816 break;
2817 }
2818 if (srv->check.health < srv->check.rise ||
2819 srv->check.health > srv->check.rise + srv->check.fall - 2)
2820 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002821 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002822 break;
2823 case SRV_ST_RUNNING:
2824 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002825 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002826 break;
2827 }
2828
2829 /* When applying server state, the following rules apply:
2830 * - in case of a configuration change, we apply the setting from the new
2831 * configuration, regardless of old running state
2832 * - if no configuration change, we apply old running state only if old running
2833 * state is different from new configuration state
2834 */
2835 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002836 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2837 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002838 srv_adm_set_maint(srv);
2839 else
2840 srv_adm_set_ready(srv);
2841 }
2842 /* configuration is the same, let's compate old running state and new conf state */
2843 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002844 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002845 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002846 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002847 srv_adm_set_ready(srv);
2848 }
2849 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002850 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002851 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002852 * (srv->iweight is the weight set up in configuration).
2853 * There are two possible reasons for FDRAIN to have been present :
2854 * - previous config weight was zero
2855 * - "set server b/s drain" was sent to the CLI
2856 *
2857 * In the first case, we simply want to drop this drain state
2858 * if the new weight is not zero anymore, meaning the administrator
2859 * has intentionally turned the weight back to a positive value to
2860 * enable the server again after an operation. In the second case,
2861 * the drain state was forced on the CLI regardless of the config's
2862 * weight so we don't want a change to the config weight to lose this
2863 * status. What this means is :
2864 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2865 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002866 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002867 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002868 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002869 }
2870
2871 srv->last_change = date.tv_sec - srv_last_time_change;
2872 srv->check.status = srv_check_status;
2873 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002874
2875 /* Only case we want to apply is removing ENABLED flag which could have been
2876 * done by the "disable health" command over the stats socket
2877 */
2878 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2879 (srv_check_state & CHK_ST_CONFIGURED) &&
2880 !(srv_check_state & CHK_ST_ENABLED))
2881 srv->check.state &= ~CHK_ST_ENABLED;
2882
2883 /* Only case we want to apply is removing ENABLED flag which could have been
2884 * done by the "disable agent" command over the stats socket
2885 */
2886 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2887 (srv_agent_state & CHK_ST_CONFIGURED) &&
2888 !(srv_agent_state & CHK_ST_ENABLED))
2889 srv->agent.state &= ~CHK_ST_ENABLED;
2890
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002891 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2892 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002893 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002894 * It means that a configuration file change has precedence over a unix socket change
2895 * for server's weight
2896 *
2897 * by default, HAProxy applies the following weight when parsing the configuration
2898 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002899 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002900 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002901 srv->uweight = srv_uweight;
2902 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002903 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002904
Willy Tarreaue5a60682016-11-09 14:54:53 +01002905 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002906 if (strcmp(params[0], "-"))
2907 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002908
2909 if (fqdn && srv->hostname) {
2910 if (!strcmp(srv->hostname, fqdn)) {
2911 /* Here we reset the 'set from stats socket FQDN' flag
2912 * to support such transitions:
2913 * Let's say initial FQDN value is foo1 (in configuration file).
2914 * - FQDN changed from stats socket, from foo1 to foo2 value,
2915 * - FQDN changed again from file configuration (with the same previous value
2916 set from stats socket, from foo1 to foo2 value),
2917 * - reload for any other reason than a FQDN modification,
2918 * the configuration file FQDN matches the fqdn server state file value.
2919 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002920 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002921 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002922 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002923 }
2924 else {
2925 /* If the FDQN has been changed from stats socket,
2926 * apply fqdn state file value (which is the value set
2927 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002928 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002929 */
2930 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002931 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002932 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002933 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002934 }
2935 }
2936 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002937 /* If all the conditions below are validated, this means
2938 * we're evaluating a server managed by SRV resolution
2939 */
2940 else if (fqdn && !srv->hostname && srvrecord) {
2941 int res;
2942
2943 /* we can't apply previous state if SRV record has changed */
2944 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2945 chunk_appendf(msg, ", SRV record mismatch between configuration ('%s') and state file ('%s) for server '%s'. Previous state not applied", srv->srvrq->name, srvrecord, srv->id);
2946 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2947 goto out;
2948 }
2949
2950 /* create or find a SRV resolution for this srv record */
2951 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2952 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2953 if (srv->srvrq == NULL) {
2954 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2955 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2956 goto out;
2957 }
2958
2959 /* prepare DNS resolution for this server */
2960 res = srv_prepare_for_resolution(srv, fqdn);
2961 if (res == -1) {
2962 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2963 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2964 goto out;
2965 }
2966
2967 /* configure check.port accordingly */
2968 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2969 !(srv->flags & SRV_F_CHECKPORT))
2970 srv->check.port = port;
2971
2972 /* Unset SRV_F_MAPPORTS for SRV records.
2973 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2974 * because no ports are provided in the configuration file.
2975 * This is because HAProxy will use the port found into the SRV record.
2976 */
2977 srv->flags &= ~SRV_F_MAPPORTS;
2978 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002979
Frédéric Lécaille31694712017-08-01 08:47:19 +02002980 if (port_str)
2981 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002982 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002983
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002984 break;
2985 default:
2986 chunk_appendf(msg, ", version '%d' not supported", version);
2987 }
2988
2989 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002990 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002991 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002992 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002993 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002994 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002995}
2996
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002997
2998/*
2999 * read next line from file <f> and return the server state version if one found.
3000 * If no version is found, then 0 is returned
3001 * Note that this should be the first read on <f>
3002 */
3003static int srv_state_get_version(FILE *f) {
3004 char buf[2];
3005 int ret;
3006
3007 /* first character of first line of the file must contain the version of the export */
3008 if (fgets(buf, 2, f) == NULL) {
3009 return 0;
3010 }
3011
3012 ret = atoi(buf);
3013 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3014 (ret > SRV_STATE_FILE_VERSION_MAX))
3015 return 0;
3016
3017 return ret;
3018}
3019
3020
3021/*
3022 * parses server state line stored in <buf> and supposedly in version <version>.
3023 * Set <params> and <srv_params> accordingly.
3024 * In case of error, params[0] is set to NULL.
3025 */
3026static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3027{
3028 int buflen, arg, srv_arg;
3029 char *cur, *end;
3030
3031 buflen = strlen(buf);
3032 cur = buf;
3033 end = cur + buflen;
3034
3035 /* we need at least one character */
3036 if (buflen == 0) {
3037 params[0] = NULL;
3038 return;
3039 }
3040
3041 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003042 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003043 ++cur;
3044
3045 /* Ignore empty or commented lines */
3046 if (cur == end || *cur == '#') {
3047 params[0] = NULL;
3048 return;
3049 }
3050
3051 /* truncated lines */
3052 if (buf[buflen - 1] != '\n') {
3053 //ha_warning("server-state file '%s': truncated line\n", filepath);
3054 params[0] = NULL;
3055 return;
3056 }
3057
3058 /* Removes trailing '\n' */
3059 buf[buflen - 1] = '\0';
3060
3061 /* we're now ready to move the line into *srv_params[] */
3062 params[0] = cur;
3063 arg = 1;
3064 srv_arg = 0;
3065 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003066 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003067 *cur = '\0';
3068 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003069 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003070 ++cur;
3071 switch (version) {
3072 case 1:
3073 /*
3074 * srv_addr: params[4] => srv_params[0]
3075 * srv_op_state: params[5] => srv_params[1]
3076 * srv_admin_state: params[6] => srv_params[2]
3077 * srv_uweight: params[7] => srv_params[3]
3078 * srv_iweight: params[8] => srv_params[4]
3079 * srv_last_time_change: params[9] => srv_params[5]
3080 * srv_check_status: params[10] => srv_params[6]
3081 * srv_check_result: params[11] => srv_params[7]
3082 * srv_check_health: params[12] => srv_params[8]
3083 * srv_check_state: params[13] => srv_params[9]
3084 * srv_agent_state: params[14] => srv_params[10]
3085 * bk_f_forced_id: params[15] => srv_params[11]
3086 * srv_f_forced_id: params[16] => srv_params[12]
3087 * srv_fqdn: params[17] => srv_params[13]
3088 * srv_port: params[18] => srv_params[14]
3089 * srvrecord: params[19] => srv_params[15]
3090 */
3091 if (arg >= 4) {
3092 srv_params[srv_arg] = cur;
3093 ++srv_arg;
3094 }
3095 break;
3096 }
3097
3098 params[arg] = cur;
3099 ++arg;
3100 }
3101 else {
3102 ++cur;
3103 }
3104 }
3105
3106 /* if line is incomplete line, then ignore it.
3107 * otherwise, update useful flags */
3108 switch (version) {
3109 case 1:
3110 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3111 params[0] = NULL;
3112 return;
3113 }
3114 break;
3115 }
3116
3117 return;
3118}
3119
3120
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003121/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3122 * For each proxy, it does the following:
3123 * - opens its server state file (either one or local one)
3124 * - read whole file, line by line
3125 * - analyse each line to check if it matches our current backend:
3126 * - backend name matches
3127 * - backend id matches if id is forced and name doesn't match
3128 * - if the server pointed by the line is found, then state is applied
3129 *
3130 * If the running backend uuid or id differs from the state file, then HAProxy reports
3131 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003132 *
3133 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003134 */
3135void apply_server_state(void)
3136{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003137 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003138 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3139 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003140 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003141 FILE *f;
3142 char *filepath;
3143 char globalfilepath[MAXPATHLEN + 1];
3144 char localfilepath[MAXPATHLEN + 1];
3145 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003146 struct proxy *curproxy, *bk;
3147 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003148 char *line;
3149 char *bkname, *srvname;
3150 struct state_line *st;
3151 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003152
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003153
3154 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003155 globalfilepathlen = 0;
3156 /* create the globalfilepath variable */
3157 if (global.server_state_file) {
3158 /* absolute path or no base directory provided */
3159 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3160 len = strlen(global.server_state_file);
3161 if (len > MAXPATHLEN) {
3162 globalfilepathlen = 0;
3163 goto globalfileerror;
3164 }
3165 memcpy(globalfilepath, global.server_state_file, len);
3166 globalfilepath[len] = '\0';
3167 globalfilepathlen = len;
3168 }
3169 else if (global.server_state_base) {
3170 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003171 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003172 globalfilepathlen = 0;
3173 goto globalfileerror;
3174 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003175 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003176 globalfilepath[len] = 0;
3177 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003178
3179 /* append a slash if needed */
3180 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3181 if (globalfilepathlen + 1 > MAXPATHLEN) {
3182 globalfilepathlen = 0;
3183 goto globalfileerror;
3184 }
3185 globalfilepath[globalfilepathlen++] = '/';
3186 }
3187
3188 len = strlen(global.server_state_file);
3189 if (globalfilepathlen + len > MAXPATHLEN) {
3190 globalfilepathlen = 0;
3191 goto globalfileerror;
3192 }
3193 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3194 globalfilepathlen += len;
3195 globalfilepath[globalfilepathlen++] = 0;
3196 }
3197 }
3198 globalfileerror:
3199 if (globalfilepathlen == 0)
3200 globalfilepath[0] = '\0';
3201
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003202 /* Load global server state in a tree */
3203 if (globalfilepathlen > 0) {
3204 errno = 0;
3205 f = fopen(globalfilepath, "r");
3206 if (errno)
3207 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003208 if (!f)
3209 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003210
3211 global_file_version = srv_state_get_version(f);
3212 if (global_file_version == 0)
3213 goto out_load_server_state_in_tree;
3214
3215 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3216 line = NULL;
3217
3218 line = strdup(mybuf);
3219 if (line == NULL)
3220 continue;
3221
3222 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3223 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003224 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003225
3226 /* bkname */
3227 bkname = params[1];
3228 /* srvname */
3229 srvname = params[3];
3230
3231 /* key */
3232 chunk_printf(&trash, "%s %s", bkname, srvname);
3233
3234 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003235 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003236 if (st == NULL) {
3237 goto nextline;
3238 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003239 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003240 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3241 /* this is a duplicate key, probably a hand-crafted file,
3242 * drop it!
3243 */
3244 goto nextline;
3245 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003246
3247 /* save line */
3248 st->line = line;
3249
3250 continue;
3251
3252 nextline:
3253 /* free up memory in case of error during the processing of the line */
3254 free(line);
3255 }
3256 }
3257 out_load_server_state_in_tree:
3258
3259 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003260 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003261 /* servers are only in backends */
3262 if (!(curproxy->cap & PR_CAP_BE))
3263 continue;
3264 fileopenerr = 0;
3265 filepath = NULL;
3266
3267 /* search server state file path and name */
3268 switch (curproxy->load_server_state_from_file) {
3269 /* read servers state from global file */
3270 case PR_SRV_STATE_FILE_GLOBAL:
3271 /* there was an error while generating global server state file path */
3272 if (globalfilepathlen == 0)
3273 continue;
3274 filepath = globalfilepath;
3275 fileopenerr = 1;
3276 break;
3277 /* this backend has its own file */
3278 case PR_SRV_STATE_FILE_LOCAL:
3279 localfilepathlen = 0;
3280 localfilepath[0] = '\0';
3281 len = 0;
3282 /* create the localfilepath variable */
3283 /* absolute path or no base directory provided */
3284 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3285 len = strlen(curproxy->server_state_file_name);
3286 if (len > MAXPATHLEN) {
3287 localfilepathlen = 0;
3288 goto localfileerror;
3289 }
3290 memcpy(localfilepath, curproxy->server_state_file_name, len);
3291 localfilepath[len] = '\0';
3292 localfilepathlen = len;
3293 }
3294 else if (global.server_state_base) {
3295 len = strlen(global.server_state_base);
3296 localfilepathlen += len;
3297
3298 if (localfilepathlen > MAXPATHLEN) {
3299 localfilepathlen = 0;
3300 goto localfileerror;
3301 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003302 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003303 localfilepath[localfilepathlen] = 0;
3304
3305 /* append a slash if needed */
3306 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3307 if (localfilepathlen + 1 > MAXPATHLEN) {
3308 localfilepathlen = 0;
3309 goto localfileerror;
3310 }
3311 localfilepath[localfilepathlen++] = '/';
3312 }
3313
3314 len = strlen(curproxy->server_state_file_name);
3315 if (localfilepathlen + len > MAXPATHLEN) {
3316 localfilepathlen = 0;
3317 goto localfileerror;
3318 }
3319 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3320 localfilepathlen += len;
3321 localfilepath[localfilepathlen++] = 0;
3322 }
3323 filepath = localfilepath;
3324 localfileerror:
3325 if (localfilepathlen == 0)
3326 localfilepath[0] = '\0';
3327
3328 break;
3329 case PR_SRV_STATE_FILE_NONE:
3330 default:
3331 continue;
3332 }
3333
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003334 /* when global file is used, we get data from the tree
3335 * Note that in such case we don't check backend name neither uuid.
3336 * Backend name can't be wrong since it's used as a key to retrieve the server state
3337 * line from the tree.
3338 */
3339 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3340 struct server *srv = curproxy->srv;
3341 while (srv) {
3342 struct ebmb_node *node;
3343 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003344
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003345 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3346 node = ebst_lookup(&state_file, trash.area);
3347 if (!node)
3348 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003349
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003350 st = container_of(node, struct state_line, name_name);
3351 memcpy(mybuf, st->line, strlen(st->line));
3352 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003353
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003354 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3355 if (params[0] == NULL)
3356 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003357
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003358 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003359
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003360 next:
3361 srv = srv->next;
3362 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003363
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003364 continue; /* next proxy in list */
3365 }
3366 else {
3367 /* load 'local' state file */
3368 errno = 0;
3369 f = fopen(filepath, "r");
3370 if (errno && fileopenerr)
3371 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3372 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003373 continue;
3374
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003375 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003376
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003377 /* first character of first line of the file must contain the version of the export */
3378 version = srv_state_get_version(f);
3379 if (version == 0) {
3380 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3381 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003382 }
3383
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003384 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3385 int bk_f_forced_id = 0;
3386 int check_id = 0;
3387 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003388
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003389 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003390
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003391 if (params[0] == NULL) {
3392 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003393 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003394
3395 /* if line is incomplete line, then ignore it.
3396 * otherwise, update useful flags */
3397 switch (version) {
3398 case 1:
3399 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3400 check_id = (atoi(params[0]) == curproxy->uuid);
3401 check_name = (strcmp(curproxy->id, params[1]) == 0);
3402 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003403 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003404
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003405 bk = curproxy;
3406
3407 /* if backend can't be found, let's continue */
3408 if (!check_id && !check_name)
3409 continue;
3410 else if (!check_id && check_name) {
3411 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3412 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3413 }
3414 else if (check_id && !check_name) {
3415 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3416 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3417 /* if name doesn't match, we still want to update curproxy if the backend id
3418 * was forced in previous the previous configuration */
3419 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003420 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003421 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003422
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003423 /* look for the server by its name: param[3] */
3424 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003425
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003426 if (!srv) {
3427 /* if no server found, then warning and continue with next line */
3428 ha_warning("can't find server '%s' in backend '%s'\n",
3429 params[3], params[1]);
3430 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3431 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003432 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003433 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003434
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003435 /* now we can proceed with server's state update */
3436 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003437 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003438 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003439fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003440 fclose(f);
3441 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003442
3443 /* now free memory allocated for the tree */
3444 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3445 node;
3446 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3447 st = container_of(node, struct state_line, name_name);
3448 ebmb_delete(&st->name_name);
3449 free(st->line);
3450 free(st);
3451 }
3452
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003453}
3454
Simon Horman7d09b9a2013-02-12 10:45:51 +09003455/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003456 * update a server's current IP address.
3457 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3458 * ip is in network format.
3459 * updater is a string which contains an information about the requester of the update.
3460 * updater is used if not NULL.
3461 *
3462 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003463 *
3464 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003465 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003466int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003467{
3468 /* generates a log line and a warning on stderr */
3469 if (1) {
3470 /* book enough space for both IPv4 and IPv6 */
3471 char oldip[INET6_ADDRSTRLEN];
3472 char newip[INET6_ADDRSTRLEN];
3473
3474 memset(oldip, '\0', INET6_ADDRSTRLEN);
3475 memset(newip, '\0', INET6_ADDRSTRLEN);
3476
3477 /* copy old IP address in a string */
3478 switch (s->addr.ss_family) {
3479 case AF_INET:
3480 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3481 break;
3482 case AF_INET6:
3483 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3484 break;
3485 };
3486
3487 /* copy new IP address in a string */
3488 switch (ip_sin_family) {
3489 case AF_INET:
3490 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3491 break;
3492 case AF_INET6:
3493 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3494 break;
3495 };
3496
3497 /* save log line into a buffer */
3498 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3499 s->proxy->id, s->id, oldip, newip, updater);
3500
3501 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003502 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003503
3504 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003505 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003506 }
3507
3508 /* save the new IP family */
3509 s->addr.ss_family = ip_sin_family;
3510 /* save the new IP address */
3511 switch (ip_sin_family) {
3512 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003513 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003514 break;
3515 case AF_INET6:
3516 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3517 break;
3518 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003519 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003520
3521 return 0;
3522}
3523
3524/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003525 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3526 *
3527 * Caller can pass its name through <updater> to get it integrated in the response message
3528 * returned by the function.
3529 *
3530 * The function first does the following, in that order:
3531 * - validates the new addr and/or port
3532 * - checks if an update is required (new IP or port is different than current ones)
3533 * - checks the update is allowed:
3534 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3535 * - allow all changes if no CHECKS are configured
3536 * - if CHECK is configured:
3537 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3538 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3539 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003540 *
3541 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003542 */
3543const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3544{
3545 struct sockaddr_storage sa;
3546 int ret, port_change_required;
3547 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003548 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003549 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003550 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003551
3552 msg = get_trash_chunk();
3553 chunk_reset(msg);
3554
3555 if (addr) {
3556 memset(&sa, 0, sizeof(struct sockaddr_storage));
3557 if (str2ip2(addr, &sa, 0) == NULL) {
3558 chunk_printf(msg, "Invalid addr '%s'", addr);
3559 goto out;
3560 }
3561
3562 /* changes are allowed on AF_INET* families only */
3563 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3564 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3565 goto out;
3566 }
3567
3568 /* collecting data currently setup */
3569 memset(current_addr, '\0', sizeof(current_addr));
3570 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3571 /* changes are allowed on AF_INET* families only */
3572 if ((ret != AF_INET) && (ret != AF_INET6)) {
3573 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3574 goto out;
3575 }
3576
3577 /* applying ADDR changes if required and allowed
3578 * ipcmp returns 0 when both ADDR are the same
3579 */
3580 if (ipcmp(&s->addr, &sa) == 0) {
3581 chunk_appendf(msg, "no need to change the addr");
3582 goto port;
3583 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003584 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003585 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003586
3587 /* we also need to update check's ADDR only if it uses the server's one */
3588 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003589 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003590 }
3591
3592 /* we also need to update agent ADDR only if it use the server's one */
3593 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003594 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003595 }
3596
3597 /* update report for caller */
3598 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3599 }
3600
3601 port:
3602 if (port) {
3603 char sign = '\0';
3604 char *endptr;
3605
3606 if (addr)
3607 chunk_appendf(msg, ", ");
3608
3609 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003610 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003611
3612 /* check if PORT change is required */
3613 port_change_required = 0;
3614
3615 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003616 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003617 new_port = strtol(port, &endptr, 10);
3618 if ((errno != 0) || (port == endptr)) {
3619 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3620 goto out;
3621 }
3622
3623 /* check if caller triggers a port mapped or offset */
3624 if (sign == '-' || (sign == '+')) {
3625 /* check if server currently uses port map */
3626 if (!(s->flags & SRV_F_MAPPORTS)) {
3627 /* switch from fixed port to port map mandatorily triggers
3628 * a port change */
3629 port_change_required = 1;
3630 /* check is configured
3631 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3632 * prevent PORT change if check doesn't have it's dedicated port while switching
3633 * to port mapping */
3634 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3635 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.");
3636 goto out;
3637 }
3638 }
3639 /* we're already using port maps */
3640 else {
3641 port_change_required = current_port != new_port;
3642 }
3643 }
3644 /* fixed port */
3645 else {
3646 port_change_required = current_port != new_port;
3647 }
3648
3649 /* applying PORT changes if required and update response message */
3650 if (port_change_required) {
3651 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003652 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003653 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003654
3655 /* prepare message */
3656 chunk_appendf(msg, "port changed from '");
3657 if (s->flags & SRV_F_MAPPORTS)
3658 chunk_appendf(msg, "+");
3659 chunk_appendf(msg, "%d' to '", current_port);
3660
3661 if (sign == '-') {
3662 s->flags |= SRV_F_MAPPORTS;
3663 chunk_appendf(msg, "%c", sign);
3664 /* just use for result output */
3665 new_port = -new_port;
3666 }
3667 else if (sign == '+') {
3668 s->flags |= SRV_F_MAPPORTS;
3669 chunk_appendf(msg, "%c", sign);
3670 }
3671 else {
3672 s->flags &= ~SRV_F_MAPPORTS;
3673 }
3674
3675 chunk_appendf(msg, "%d'", new_port);
3676
3677 /* we also need to update health checks port only if it uses server's realport */
3678 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3679 s->check.port = new_port;
3680 }
3681 }
3682 else {
3683 chunk_appendf(msg, "no need to change the port");
3684 }
3685 }
3686
3687out:
William Dauchy6318d332020-05-02 21:52:36 +02003688 if (changed) {
3689 /* force connection cleanup on the given server */
3690 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003691 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003692 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003693 if (updater)
3694 chunk_appendf(msg, " by '%s'", updater);
3695 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003696 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003697}
3698
3699
3700/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003701 * update server status based on result of name resolution
3702 * returns:
3703 * 0 if server status is updated
3704 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003705 *
3706 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003707 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003708int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003709{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003710 struct dns_resolvers *resolvers = s->resolvers;
3711 struct dns_resolution *resolution = s->dns_requester->resolution;
3712 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003713
3714 switch (resolution->status) {
3715 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003716 /* status when HAProxy has just (re)started.
3717 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003718 break;
3719
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003720 case RSLV_STATUS_VALID:
3721 /*
3722 * resume health checks
3723 * server will be turned back on if health check is safe
3724 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003725 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003726 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003727 return 1;
3728 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3729 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003730 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003731 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003732
Emeric Brun52a91d32017-08-31 14:41:55 +02003733 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003734 return 1;
3735 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3736 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3737 s->proxy->id, s->id);
3738
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003739 ha_warning("%s.\n", trash.area);
3740 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003741 return 0;
3742
3743 case RSLV_STATUS_NX:
3744 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003745 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3746 if (!tick_is_expired(exp, now_ms))
3747 break;
3748
3749 if (s->next_admin & SRV_ADMF_RMAINT)
3750 return 1;
3751 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3752 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003753
3754 case RSLV_STATUS_TIMEOUT:
3755 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003756 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3757 if (!tick_is_expired(exp, now_ms))
3758 break;
3759
3760 if (s->next_admin & SRV_ADMF_RMAINT)
3761 return 1;
3762 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3763 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003764
3765 case RSLV_STATUS_REFUSED:
3766 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003767 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3768 if (!tick_is_expired(exp, now_ms))
3769 break;
3770
3771 if (s->next_admin & SRV_ADMF_RMAINT)
3772 return 1;
3773 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3774 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003775
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003776 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003777 /* stop server if resolution failed for a long enough period */
3778 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3779 if (!tick_is_expired(exp, now_ms))
3780 break;
3781
3782 if (s->next_admin & SRV_ADMF_RMAINT)
3783 return 1;
3784 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3785 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003786 }
3787
3788 return 1;
3789}
3790
3791/*
3792 * Server Name Resolution valid response callback
3793 * It expects:
3794 * - <nameserver>: the name server which answered the valid response
3795 * - <response>: buffer containing a valid DNS response
3796 * - <response_len>: size of <response>
3797 * It performs the following actions:
3798 * - ignore response if current ip found and server family not met
3799 * - update with first new ip found if family is met and current IP is not found
3800 * returns:
3801 * 0 on error
3802 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003803 *
3804 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003805 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003806int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003807{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003808 struct server *s = NULL;
3809 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003810 void *serverip, *firstip;
3811 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003812 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003813 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003814 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003815
Christopher Faulet67957bd2017-09-27 11:00:59 +02003816 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003817 if (!s)
3818 return 1;
3819
Christopher Faulet67957bd2017-09-27 11:00:59 +02003820 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003821
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003822 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003823 firstip = NULL; /* pointer to the first valid response found */
3824 /* it will be used as the new IP if a change is required */
3825 firstip_sin_family = AF_UNSPEC;
3826 serverip = NULL; /* current server IP address */
3827
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003828 /* initializing server IP pointer */
3829 server_sin_family = s->addr.ss_family;
3830 switch (server_sin_family) {
3831 case AF_INET:
3832 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3833 break;
3834
3835 case AF_INET6:
3836 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3837 break;
3838
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003839 case AF_UNSPEC:
3840 break;
3841
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003842 default:
3843 goto invalid;
3844 }
3845
Baptiste Assmann729c9012017-05-22 15:13:10 +02003846 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003847 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003848 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003849
3850 switch (ret) {
3851 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003852 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003853
3854 case DNS_UPD_SRVIP_NOT_FOUND:
3855 goto save_ip;
3856
3857 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003858 goto invalid;
3859
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003860 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003861 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003862 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003863
Baptiste Assmannfad03182015-10-28 02:03:32 +01003864 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003865 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003866 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003867 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003868
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003869 default:
3870 goto invalid;
3871
3872 }
3873
3874 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003875 if (nameserver) {
3876 nameserver->counters.update++;
3877 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003878 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003879 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003880 else
3881 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003882 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003883
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003884 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003885 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003886 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003887
3888 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003889 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003890 nameserver->counters.invalid++;
3891 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003892 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003893 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003894 return 0;
3895}
3896
3897/*
3898 * Server Name Resolution error management callback
3899 * returns:
3900 * 0 on error
3901 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003902 *
3903 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003904 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003905int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003906{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003907 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003908
Christopher Faulet67957bd2017-09-27 11:00:59 +02003909 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003910 if (!s)
3911 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003912 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003913 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003914 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003915 return 1;
3916}
3917
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003918/*
3919 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003920 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003921 * It returns a pointer to the first server found or NULL if <ip> is not yet
3922 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003923 *
3924 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003925 */
3926struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3927{
3928 struct server *tmpsrv;
3929 struct proxy *be;
3930
3931 if (!srv)
3932 return NULL;
3933
3934 be = srv->proxy;
3935 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003936 /* we found the current server is the same, ignore it */
3937 if (srv == tmpsrv)
3938 continue;
3939
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003940 /* We want to compare the IP in the record with the IP of the servers in the
3941 * same backend, only if:
3942 * * DNS resolution is enabled on the server
3943 * * the hostname used for the resolution by our server is the same than the
3944 * one used for the server found in the backend
3945 * * the server found in the backend is not our current server
3946 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003947 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003948 if ((tmpsrv->hostname_dn == NULL) ||
3949 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3950 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003951 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003952 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003953 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003954 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003955
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003956 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003957 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003958 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003959 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003960 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003961
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003962 /* At this point, we have 2 different servers using the same DNS hostname
3963 * for their respective resolution.
3964 */
3965 if (*ip_family == tmpsrv->addr.ss_family &&
3966 ((tmpsrv->addr.ss_family == AF_INET &&
3967 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3968 (tmpsrv->addr.ss_family == AF_INET6 &&
3969 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003970 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003971 return tmpsrv;
3972 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003973 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003974 }
3975
Emeric Brune9fd6b52017-11-02 17:20:39 +01003976
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003977 return NULL;
3978}
3979
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003980/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3981 * resolver. This is suited for initial address configuration. Returns 0 on
3982 * success otherwise a non-zero error code. In case of error, *err_code, if
3983 * not NULL, is filled up.
3984 */
3985int srv_set_addr_via_libc(struct server *srv, int *err_code)
3986{
3987 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003988 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003989 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003990 return 1;
3991 }
3992 return 0;
3993}
3994
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003995/* Set the server's FDQN (->hostname) from <hostname>.
3996 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003997 *
3998 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003999 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004000int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004001{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004002 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004003 char *hostname_dn;
4004 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004005
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004006 /* Note that the server lock is already held. */
4007 if (!srv->resolvers)
4008 return -1;
4009
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004010 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004011 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004012 /* run time DNS resolution was not active for this server
4013 * and we can't enable it at run time for now.
4014 */
4015 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004016 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004017
4018 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004019 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004020 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004021 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4022 hostname_dn, trash.size);
4023 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004024 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004025
Christopher Faulet67957bd2017-09-27 11:00:59 +02004026 resolution = srv->dns_requester->resolution;
4027 if (resolution &&
4028 resolution->hostname_dn &&
4029 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004030 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004031
Christopher Faulet67957bd2017-09-27 11:00:59 +02004032 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004033
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004034 free(srv->hostname);
4035 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004036 srv->hostname = strdup(hostname);
4037 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004038 srv->hostname_dn_len = hostname_dn_len;
4039 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004040 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004041
Baptiste Assmann13a92322019-06-07 09:40:55 +02004042 if (srv->flags & SRV_F_NO_RESOLUTION)
4043 goto end;
4044
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004045 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004046 goto err;
4047
4048 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004049 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004050 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004051 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004052
4053 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004054 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004055 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004056 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004057}
4058
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004059/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4060 * from the state file. This is suited for initial address configuration.
4061 * Returns 0 on success otherwise a non-zero error code. In case of error,
4062 * *err_code, if not NULL, is filled up.
4063 */
4064static int srv_apply_lastaddr(struct server *srv, int *err_code)
4065{
4066 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4067 if (err_code)
4068 *err_code |= ERR_WARN;
4069 return 1;
4070 }
4071 return 0;
4072}
4073
Willy Tarreau25e51522016-11-04 15:10:17 +01004074/* returns 0 if no error, otherwise a combination of ERR_* flags */
4075static int srv_iterate_initaddr(struct server *srv)
4076{
4077 int return_code = 0;
4078 int err_code;
4079 unsigned int methods;
4080
4081 methods = srv->init_addr_methods;
4082 if (!methods) { // default to "last,libc"
4083 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4084 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4085 }
4086
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004087 /* "-dr" : always append "none" so that server addresses resolution
4088 * failures are silently ignored, this is convenient to validate some
4089 * configs out of their environment.
4090 */
4091 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4092 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4093
Willy Tarreau25e51522016-11-04 15:10:17 +01004094 while (methods) {
4095 err_code = 0;
4096 switch (srv_get_next_initaddr(&methods)) {
4097 case SRV_IADDR_LAST:
4098 if (!srv->lastaddr)
4099 continue;
4100 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004101 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004102 return_code |= err_code;
4103 break;
4104
4105 case SRV_IADDR_LIBC:
4106 if (!srv->hostname)
4107 continue;
4108 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004109 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004110 return_code |= err_code;
4111 break;
4112
Willy Tarreau37ebe122016-11-04 15:17:58 +01004113 case SRV_IADDR_NONE:
4114 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004115 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004116 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4117 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004118 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004119 return return_code;
4120
Willy Tarreau4310d362016-11-02 15:05:56 +01004121 case SRV_IADDR_IP:
4122 ipcpy(&srv->init_addr, &srv->addr);
4123 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004124 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4125 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004126 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004127 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004128
Willy Tarreau25e51522016-11-04 15:10:17 +01004129 default: /* unhandled method */
4130 break;
4131 }
4132 }
4133
4134 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004135 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004136 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4137 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004138 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004139 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004140 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4141 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004142
4143 return_code |= ERR_ALERT | ERR_FATAL;
4144 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004145out:
4146 srv_set_dyncookie(srv);
4147 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004148}
4149
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004150/*
4151 * This function parses all backends and all servers within each backend
4152 * and performs servers' addr resolution based on information provided by:
4153 * - configuration file
4154 * - server-state file (states provided by an 'old' haproxy process)
4155 *
4156 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4157 */
4158int srv_init_addr(void)
4159{
4160 struct proxy *curproxy;
4161 int return_code = 0;
4162
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004163 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004164 while (curproxy) {
4165 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004166
4167 /* servers are in backend only */
4168 if (!(curproxy->cap & PR_CAP_BE))
4169 goto srv_init_addr_next;
4170
Willy Tarreau25e51522016-11-04 15:10:17 +01004171 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004172 if (srv->hostname)
4173 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004174
4175 srv_init_addr_next:
4176 curproxy = curproxy->next;
4177 }
4178
4179 return return_code;
4180}
4181
Willy Tarreau46b7f532018-08-21 11:54:26 +02004182/*
4183 * Must be called with the server lock held.
4184 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004185const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004186{
4187
Willy Tarreau83061a82018-07-13 11:56:34 +02004188 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004189
4190 msg = get_trash_chunk();
4191 chunk_reset(msg);
4192
Olivier Houchard8da5f982017-08-04 18:35:36 +02004193 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004194 chunk_appendf(msg, "no need to change the FDQN");
4195 goto out;
4196 }
4197
4198 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4199 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4200 goto out;
4201 }
4202
4203 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4204 server->proxy->id, server->id, server->hostname, fqdn);
4205
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004206 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004207 chunk_reset(msg);
4208 chunk_appendf(msg, "could not update %s/%s FQDN",
4209 server->proxy->id, server->id);
4210 goto out;
4211 }
4212
4213 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004214 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004215
4216 out:
4217 if (updater)
4218 chunk_appendf(msg, " by '%s'", updater);
4219 chunk_appendf(msg, "\n");
4220
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004221 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004222}
4223
4224
Willy Tarreau21b069d2016-11-23 17:15:08 +01004225/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4226 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004227 * 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 +01004228 * used for CLI commands requiring a server name.
4229 * Important: the <arg> is modified to remove the '/'.
4230 */
4231struct server *cli_find_server(struct appctx *appctx, char *arg)
4232{
4233 struct proxy *px;
4234 struct server *sv;
4235 char *line;
4236
4237 /* split "backend/server" and make <line> point to server */
4238 for (line = arg; *line; line++)
4239 if (*line == '/') {
4240 *line++ = '\0';
4241 break;
4242 }
4243
4244 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004245 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004246 return NULL;
4247 }
4248
4249 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004250 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004251 return NULL;
4252 }
4253
4254 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004255 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004256 return NULL;
4257 }
4258
4259 return sv;
4260}
4261
William Lallemand222baf22016-11-19 02:00:33 +01004262
Willy Tarreau46b7f532018-08-21 11:54:26 +02004263/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004264static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004265{
4266 struct server *sv;
4267 const char *warning;
4268
4269 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4270 return 1;
4271
4272 sv = cli_find_server(appctx, args[2]);
4273 if (!sv)
4274 return 1;
4275
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004276 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004277
William Lallemand222baf22016-11-19 02:00:33 +01004278 if (strcmp(args[3], "weight") == 0) {
4279 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004280 if (warning)
4281 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004282 }
4283 else if (strcmp(args[3], "state") == 0) {
4284 if (strcmp(args[4], "ready") == 0)
4285 srv_adm_set_ready(sv);
4286 else if (strcmp(args[4], "drain") == 0)
4287 srv_adm_set_drain(sv);
4288 else if (strcmp(args[4], "maint") == 0)
4289 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004290 else
4291 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004292 }
4293 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004294 if (sv->track)
4295 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004296 else if (strcmp(args[4], "up") == 0) {
4297 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004298 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004299 }
4300 else if (strcmp(args[4], "stopping") == 0) {
4301 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004302 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004303 }
4304 else if (strcmp(args[4], "down") == 0) {
4305 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004306 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004307 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004308 else
4309 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004310 }
4311 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004312 if (!(sv->agent.state & CHK_ST_ENABLED))
4313 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004314 else if (strcmp(args[4], "up") == 0) {
4315 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004316 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004317 }
4318 else if (strcmp(args[4], "down") == 0) {
4319 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004320 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004321 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004322 else
4323 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004324 }
Misiek2da082d2017-01-09 09:40:42 +01004325 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004326 if (!(sv->agent.state & CHK_ST_ENABLED))
4327 cli_err(appctx, "agent checks are not enabled on this server.\n");
4328 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4329 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004330 }
4331 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004332 if (!(sv->agent.state & CHK_ST_ENABLED))
4333 cli_err(appctx, "agent checks are not enabled on this server.\n");
4334 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004335 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004336 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004337 }
4338 }
William Lallemand222baf22016-11-19 02:00:33 +01004339 else if (strcmp(args[3], "check-port") == 0) {
4340 int i = 0;
4341 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004342 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004343 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004344 }
4345 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004346 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004347 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004348 }
4349 /* prevent the update of port to 0 if MAPPORTS are in use */
4350 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004351 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004352 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004353 }
4354 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004355 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004356 }
4357 else if (strcmp(args[3], "addr") == 0) {
4358 char *addr = NULL;
4359 char *port = NULL;
4360 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004361 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004362 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004363 }
4364 else {
4365 addr = args[4];
4366 }
4367 if (strcmp(args[5], "port") == 0) {
4368 port = args[6];
4369 }
4370 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004371 if (warning)
4372 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004373 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4374 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004375 else if (strcmp(args[3], "fqdn") == 0) {
4376 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004377 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004378 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004379 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004380 /* ensure runtime resolver will process this new fqdn */
4381 if (sv->flags & SRV_F_NO_RESOLUTION) {
4382 sv->flags &= ~SRV_F_NO_RESOLUTION;
4383 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004384 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004385 if (warning)
4386 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004387 }
William Lallemand222baf22016-11-19 02:00:33 +01004388 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004389 cli_err(appctx,
4390 "'set server <srv>' only supports 'agent', 'health', 'state',"
4391 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004392 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004393 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004394 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004395 return 1;
4396}
4397
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004398static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004399{
4400 struct stream_interface *si = appctx->owner;
4401 struct proxy *px;
4402 struct server *sv;
4403 char *line;
4404
4405
4406 /* split "backend/server" and make <line> point to server */
4407 for (line = args[2]; *line; line++)
4408 if (*line == '/') {
4409 *line++ = '\0';
4410 break;
4411 }
4412
Willy Tarreau9d008692019-08-09 11:21:01 +02004413 if (!*line)
4414 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004415
Willy Tarreau9d008692019-08-09 11:21:01 +02004416 if (!get_backend_server(args[2], line, &px, &sv))
4417 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004418
4419 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004420 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4421 sv->iweight);
4422 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004423 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004424 return 0;
4425 }
William Lallemand6b160942016-11-22 12:34:35 +01004426 return 1;
4427}
4428
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004429/* Parse a "set weight" command.
4430 *
4431 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004432 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004433static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004434{
4435 struct server *sv;
4436 const char *warning;
4437
4438 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4439 return 1;
4440
4441 sv = cli_find_server(appctx, args[2]);
4442 if (!sv)
4443 return 1;
4444
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004445 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4446
William Lallemand6b160942016-11-22 12:34:35 +01004447 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004448 if (warning)
4449 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004450
4451 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4452
William Lallemand6b160942016-11-22 12:34:35 +01004453 return 1;
4454}
4455
Willy Tarreau46b7f532018-08-21 11:54:26 +02004456/* parse a "set maxconn server" command. It always returns 1.
4457 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004458 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004459 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004460static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004461{
4462 struct server *sv;
4463 const char *warning;
4464
4465 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4466 return 1;
4467
4468 sv = cli_find_server(appctx, args[3]);
4469 if (!sv)
4470 return 1;
4471
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004472 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4473
Willy Tarreaub8026272016-11-23 11:26:56 +01004474 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004475 if (warning)
4476 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004477
4478 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4479
Willy Tarreaub8026272016-11-23 11:26:56 +01004480 return 1;
4481}
William Lallemand6b160942016-11-22 12:34:35 +01004482
Willy Tarreau46b7f532018-08-21 11:54:26 +02004483/* parse a "disable agent" command. It always returns 1.
4484 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004485 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004486 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004487static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004488{
4489 struct server *sv;
4490
4491 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4492 return 1;
4493
4494 sv = cli_find_server(appctx, args[2]);
4495 if (!sv)
4496 return 1;
4497
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004498 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004499 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004500 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004501 return 1;
4502}
4503
Willy Tarreau46b7f532018-08-21 11:54:26 +02004504/* parse a "disable health" command. It always returns 1.
4505 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004506 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004507 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004508static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004509{
4510 struct server *sv;
4511
4512 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4513 return 1;
4514
4515 sv = cli_find_server(appctx, args[2]);
4516 if (!sv)
4517 return 1;
4518
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004519 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004520 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004521 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004522 return 1;
4523}
4524
Willy Tarreau46b7f532018-08-21 11:54:26 +02004525/* parse a "disable server" command. It always returns 1.
4526 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004527 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004528 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004529static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004530{
4531 struct server *sv;
4532
4533 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4534 return 1;
4535
4536 sv = cli_find_server(appctx, args[2]);
4537 if (!sv)
4538 return 1;
4539
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004540 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004541 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004542 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004543 return 1;
4544}
4545
Willy Tarreau46b7f532018-08-21 11:54:26 +02004546/* parse a "enable agent" command. It always returns 1.
4547 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004548 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004549 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004550static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004551{
4552 struct server *sv;
4553
4554 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4555 return 1;
4556
4557 sv = cli_find_server(appctx, args[2]);
4558 if (!sv)
4559 return 1;
4560
Willy Tarreau9d008692019-08-09 11:21:01 +02004561 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4562 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004563
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004564 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004565 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004566 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004567 return 1;
4568}
4569
Willy Tarreau46b7f532018-08-21 11:54:26 +02004570/* parse a "enable health" command. It always returns 1.
4571 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004572 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004573 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004574static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004575{
4576 struct server *sv;
4577
4578 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4579 return 1;
4580
4581 sv = cli_find_server(appctx, args[2]);
4582 if (!sv)
4583 return 1;
4584
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004585 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004586 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004587 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004588 return 1;
4589}
4590
Willy Tarreau46b7f532018-08-21 11:54:26 +02004591/* parse a "enable server" command. It always returns 1.
4592 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004593 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004594 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004595static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004596{
4597 struct server *sv;
4598
4599 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4600 return 1;
4601
4602 sv = cli_find_server(appctx, args[2]);
4603 if (!sv)
4604 return 1;
4605
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004606 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004607 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004608 if (!(sv->flags & SRV_F_COOKIESET)
4609 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4610 sv->cookie)
4611 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004612 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004613 return 1;
4614}
4615
William Lallemand222baf22016-11-19 02:00:33 +01004616/* register cli keywords */
4617static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004618 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004619 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004620 { { "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 +01004621 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004622 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004623 { { "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 +01004624 { { "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 +01004625 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004626 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4627 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4628
William Lallemand222baf22016-11-19 02:00:33 +01004629 {{},}
4630}};
4631
Willy Tarreau0108d902018-11-25 19:14:37 +01004632INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004633
Emeric Brun64cc49c2017-10-03 14:46:45 +02004634/*
4635 * This function applies server's status changes, it is
4636 * is designed to be called asynchronously.
4637 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004638 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004639 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004640static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004641{
4642 struct check *check = &s->check;
4643 int xferred;
4644 struct proxy *px = s->proxy;
4645 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4646 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4647 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004648 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004649
Emeric Brun64cc49c2017-10-03 14:46:45 +02004650 /* If currently main is not set we try to apply pending state changes */
4651 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4652 int next_admin;
4653
4654 /* Backup next admin */
4655 next_admin = s->next_admin;
4656
4657 /* restore current admin state */
4658 s->next_admin = s->cur_admin;
4659
4660 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4661 s->last_change = now.tv_sec;
4662 if (s->proxy->lbprm.set_server_status_down)
4663 s->proxy->lbprm.set_server_status_down(s);
4664
4665 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4666 srv_shutdown_streams(s, SF_ERR_DOWN);
4667
4668 /* we might have streams queued on this server and waiting for
4669 * a connection. Those which are redispatchable will be queued
4670 * to another server or to the proxy itself.
4671 */
4672 xferred = pendconn_redistribute(s);
4673
4674 tmptrash = alloc_trash_chunk();
4675 if (tmptrash) {
4676 chunk_printf(tmptrash,
4677 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4678 s->proxy->id, s->id);
4679
Emeric Brun5a133512017-10-19 14:42:30 +02004680 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004681 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004682
4683 /* we don't send an alert if the server was previously paused */
4684 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004685 send_log(s->proxy, log_level, "%s.\n",
4686 tmptrash->area);
4687 send_email_alert(s, log_level, "%s",
4688 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004689 free_trash_chunk(tmptrash);
4690 tmptrash = NULL;
4691 }
4692 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4693 set_backend_down(s->proxy);
4694
4695 s->counters.down_trans++;
4696 }
4697 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4698 s->last_change = now.tv_sec;
4699 if (s->proxy->lbprm.set_server_status_down)
4700 s->proxy->lbprm.set_server_status_down(s);
4701
4702 /* we might have streams queued on this server and waiting for
4703 * a connection. Those which are redispatchable will be queued
4704 * to another server or to the proxy itself.
4705 */
4706 xferred = pendconn_redistribute(s);
4707
4708 tmptrash = alloc_trash_chunk();
4709 if (tmptrash) {
4710 chunk_printf(tmptrash,
4711 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4712 s->proxy->id, s->id);
4713
Emeric Brun5a133512017-10-19 14:42:30 +02004714 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004715
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004716 ha_warning("%s.\n", tmptrash->area);
4717 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4718 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004719 free_trash_chunk(tmptrash);
4720 tmptrash = NULL;
4721 }
4722
4723 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4724 set_backend_down(s->proxy);
4725 }
4726 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4727 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4728 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4729 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4730 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4731 s->proxy->last_change = now.tv_sec;
4732 }
4733
4734 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4735 s->down_time += now.tv_sec - s->last_change;
4736
4737 s->last_change = now.tv_sec;
4738 if (s->next_state == SRV_ST_STARTING)
4739 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4740
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004741 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004742 /* now propagate the status change to any LB algorithms */
4743 if (px->lbprm.update_server_eweight)
4744 px->lbprm.update_server_eweight(s);
4745 else if (srv_willbe_usable(s)) {
4746 if (px->lbprm.set_server_status_up)
4747 px->lbprm.set_server_status_up(s);
4748 }
4749 else {
4750 if (px->lbprm.set_server_status_down)
4751 px->lbprm.set_server_status_down(s);
4752 }
4753
4754 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4755 * and it's not a backup server and its effective weight is > 0,
4756 * then it can accept new connections, so we shut down all streams
4757 * on all backup servers.
4758 */
4759 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4760 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4761 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4762
4763 /* check if we can handle some connections queued at the proxy. We
4764 * will take as many as we can handle.
4765 */
4766 xferred = pendconn_grab_from_px(s);
4767
4768 tmptrash = alloc_trash_chunk();
4769 if (tmptrash) {
4770 chunk_printf(tmptrash,
4771 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4772 s->proxy->id, s->id);
4773
Emeric Brun5a133512017-10-19 14:42:30 +02004774 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004775 ha_warning("%s.\n", tmptrash->area);
4776 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4777 tmptrash->area);
4778 send_email_alert(s, LOG_NOTICE, "%s",
4779 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004780 free_trash_chunk(tmptrash);
4781 tmptrash = NULL;
4782 }
4783
4784 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4785 set_backend_down(s->proxy);
4786 }
4787 else if (s->cur_eweight != s->next_eweight) {
4788 /* now propagate the status change to any LB algorithms */
4789 if (px->lbprm.update_server_eweight)
4790 px->lbprm.update_server_eweight(s);
4791 else if (srv_willbe_usable(s)) {
4792 if (px->lbprm.set_server_status_up)
4793 px->lbprm.set_server_status_up(s);
4794 }
4795 else {
4796 if (px->lbprm.set_server_status_down)
4797 px->lbprm.set_server_status_down(s);
4798 }
4799
4800 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4801 set_backend_down(s->proxy);
4802 }
4803
4804 s->next_admin = next_admin;
4805 }
4806
Emeric Brun5a133512017-10-19 14:42:30 +02004807 /* reset operational state change */
4808 *s->op_st_chg.reason = 0;
4809 s->op_st_chg.status = s->op_st_chg.code = -1;
4810 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004811
4812 /* Now we try to apply pending admin changes */
4813
4814 /* Maintenance must also disable health checks */
4815 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4816 if (s->check.state & CHK_ST_ENABLED) {
4817 s->check.state |= CHK_ST_PAUSED;
4818 check->health = 0;
4819 }
4820
4821 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004822 tmptrash = alloc_trash_chunk();
4823 if (tmptrash) {
4824 chunk_printf(tmptrash,
4825 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4826 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4827 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004828
Olivier Houchard796a2b32017-10-24 17:42:47 +02004829 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004830
Olivier Houchard796a2b32017-10-24 17:42:47 +02004831 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004832 ha_warning("%s.\n", tmptrash->area);
4833 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4834 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004835 }
4836 free_trash_chunk(tmptrash);
4837 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004838 }
Emeric Brun8f298292017-12-06 16:47:17 +01004839 /* commit new admin status */
4840
4841 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004842 }
4843 else { /* server was still running */
4844 check->health = 0; /* failure */
4845 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004846
4847 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004848 if (s->proxy->lbprm.set_server_status_down)
4849 s->proxy->lbprm.set_server_status_down(s);
4850
Emeric Brun64cc49c2017-10-03 14:46:45 +02004851 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4852 srv_shutdown_streams(s, SF_ERR_DOWN);
4853
William Dauchy6318d332020-05-02 21:52:36 +02004854 /* force connection cleanup on the given server */
4855 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004856 /* we might have streams queued on this server and waiting for
4857 * a connection. Those which are redispatchable will be queued
4858 * to another server or to the proxy itself.
4859 */
4860 xferred = pendconn_redistribute(s);
4861
4862 tmptrash = alloc_trash_chunk();
4863 if (tmptrash) {
4864 chunk_printf(tmptrash,
4865 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4866 s->flags & SRV_F_BACKUP ? "Backup " : "",
4867 s->proxy->id, s->id,
4868 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4869
4870 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4871
4872 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004873 ha_warning("%s.\n", tmptrash->area);
4874 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4875 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004876 }
4877 free_trash_chunk(tmptrash);
4878 tmptrash = NULL;
4879 }
4880 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4881 set_backend_down(s->proxy);
4882
4883 s->counters.down_trans++;
4884 }
4885 }
4886 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4887 /* OK here we're leaving maintenance, we have many things to check,
4888 * because the server might possibly be coming back up depending on
4889 * its state. In practice, leaving maintenance means that we should
4890 * immediately turn to UP (more or less the slowstart) under the
4891 * following conditions :
4892 * - server is neither checked nor tracked
4893 * - server tracks another server which is not checked
4894 * - server tracks another server which is already up
4895 * Which sums up as something simpler :
4896 * "either the tracking server is up or the server's checks are disabled
4897 * or up". Otherwise we only re-enable health checks. There's a special
4898 * case associated to the stopping state which can be inherited. Note
4899 * that the server might still be in drain mode, which is naturally dealt
4900 * with by the lower level functions.
4901 */
4902
4903 if (s->check.state & CHK_ST_ENABLED) {
4904 s->check.state &= ~CHK_ST_PAUSED;
4905 check->health = check->rise; /* start OK but check immediately */
4906 }
4907
4908 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4909 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4910 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4911 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4912 s->next_state = SRV_ST_STOPPING;
4913 }
4914 else {
4915 s->next_state = SRV_ST_STARTING;
4916 if (s->slowstart > 0)
4917 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4918 else
4919 s->next_state = SRV_ST_RUNNING;
4920 }
4921
4922 }
4923
4924 tmptrash = alloc_trash_chunk();
4925 if (tmptrash) {
4926 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4927 chunk_printf(tmptrash,
4928 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4929 s->flags & SRV_F_BACKUP ? "Backup " : "",
4930 s->proxy->id, s->id,
4931 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4932 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4933 }
4934 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4935 chunk_printf(tmptrash,
4936 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4937 s->flags & SRV_F_BACKUP ? "Backup " : "",
4938 s->proxy->id, s->id, s->hostname,
4939 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4940 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4941 }
4942 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4943 chunk_printf(tmptrash,
4944 "%sServer %s/%s is %s/%s (leaving maintenance)",
4945 s->flags & SRV_F_BACKUP ? "Backup " : "",
4946 s->proxy->id, s->id,
4947 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4948 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4949 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004950 ha_warning("%s.\n", tmptrash->area);
4951 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4952 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004953 free_trash_chunk(tmptrash);
4954 tmptrash = NULL;
4955 }
4956
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004957 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004958 /* now propagate the status change to any LB algorithms */
4959 if (px->lbprm.update_server_eweight)
4960 px->lbprm.update_server_eweight(s);
4961 else if (srv_willbe_usable(s)) {
4962 if (px->lbprm.set_server_status_up)
4963 px->lbprm.set_server_status_up(s);
4964 }
4965 else {
4966 if (px->lbprm.set_server_status_down)
4967 px->lbprm.set_server_status_down(s);
4968 }
4969
4970 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4971 set_backend_down(s->proxy);
4972
Willy Tarreau6a78e612018-08-07 10:14:53 +02004973 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4974 * and it's not a backup server and its effective weight is > 0,
4975 * then it can accept new connections, so we shut down all streams
4976 * on all backup servers.
4977 */
4978 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4979 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4980 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4981
4982 /* check if we can handle some connections queued at the proxy. We
4983 * will take as many as we can handle.
4984 */
4985 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004986 }
4987 else if (s->next_admin & SRV_ADMF_MAINT) {
4988 /* remaining in maintenance mode, let's inform precisely about the
4989 * situation.
4990 */
4991 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4992 tmptrash = alloc_trash_chunk();
4993 if (tmptrash) {
4994 chunk_printf(tmptrash,
4995 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4996 s->flags & SRV_F_BACKUP ? "Backup " : "",
4997 s->proxy->id, s->id);
4998
4999 if (s->track) /* normally it's mandatory here */
5000 chunk_appendf(tmptrash, " via %s/%s",
5001 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005002 ha_warning("%s.\n", tmptrash->area);
5003 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5004 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005005 free_trash_chunk(tmptrash);
5006 tmptrash = NULL;
5007 }
5008 }
5009 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5010 tmptrash = alloc_trash_chunk();
5011 if (tmptrash) {
5012 chunk_printf(tmptrash,
5013 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5014 s->flags & SRV_F_BACKUP ? "Backup " : "",
5015 s->proxy->id, s->id, s->hostname);
5016
5017 if (s->track) /* normally it's mandatory here */
5018 chunk_appendf(tmptrash, " via %s/%s",
5019 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005020 ha_warning("%s.\n", tmptrash->area);
5021 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5022 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005023 free_trash_chunk(tmptrash);
5024 tmptrash = NULL;
5025 }
5026 }
5027 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5028 tmptrash = alloc_trash_chunk();
5029 if (tmptrash) {
5030 chunk_printf(tmptrash,
5031 "%sServer %s/%s remains in forced maintenance",
5032 s->flags & SRV_F_BACKUP ? "Backup " : "",
5033 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005034 ha_warning("%s.\n", tmptrash->area);
5035 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5036 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005037 free_trash_chunk(tmptrash);
5038 tmptrash = NULL;
5039 }
5040 }
5041 /* don't report anything when leaving drain mode and remaining in maintenance */
5042
5043 s->cur_admin = s->next_admin;
5044 }
5045
5046 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5047 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5048 /* drain state is applied only if not yet in maint */
5049
5050 s->last_change = now.tv_sec;
5051 if (px->lbprm.set_server_status_down)
5052 px->lbprm.set_server_status_down(s);
5053
5054 /* we might have streams queued on this server and waiting for
5055 * a connection. Those which are redispatchable will be queued
5056 * to another server or to the proxy itself.
5057 */
5058 xferred = pendconn_redistribute(s);
5059
5060 tmptrash = alloc_trash_chunk();
5061 if (tmptrash) {
5062 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5063 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5064 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5065
5066 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5067
5068 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005069 ha_warning("%s.\n", tmptrash->area);
5070 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5071 tmptrash->area);
5072 send_email_alert(s, LOG_NOTICE, "%s",
5073 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005074 }
5075 free_trash_chunk(tmptrash);
5076 tmptrash = NULL;
5077 }
5078
5079 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5080 set_backend_down(s->proxy);
5081 }
5082 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5083 /* OK completely leaving drain mode */
5084 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5085 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5086 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5087 s->proxy->last_change = now.tv_sec;
5088 }
5089
5090 if (s->last_change < now.tv_sec) // ignore negative times
5091 s->down_time += now.tv_sec - s->last_change;
5092 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005093 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005094
5095 tmptrash = alloc_trash_chunk();
5096 if (tmptrash) {
5097 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5098 chunk_printf(tmptrash,
5099 "%sServer %s/%s is %s (leaving forced drain)",
5100 s->flags & SRV_F_BACKUP ? "Backup " : "",
5101 s->proxy->id, s->id,
5102 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5103 }
5104 else {
5105 chunk_printf(tmptrash,
5106 "%sServer %s/%s is %s (leaving drain)",
5107 s->flags & SRV_F_BACKUP ? "Backup " : "",
5108 s->proxy->id, s->id,
5109 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5110 if (s->track) /* normally it's mandatory here */
5111 chunk_appendf(tmptrash, " via %s/%s",
5112 s->track->proxy->id, s->track->id);
5113 }
5114
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005115 ha_warning("%s.\n", tmptrash->area);
5116 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5117 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005118 free_trash_chunk(tmptrash);
5119 tmptrash = NULL;
5120 }
5121
5122 /* now propagate the status change to any LB algorithms */
5123 if (px->lbprm.update_server_eweight)
5124 px->lbprm.update_server_eweight(s);
5125 else if (srv_willbe_usable(s)) {
5126 if (px->lbprm.set_server_status_up)
5127 px->lbprm.set_server_status_up(s);
5128 }
5129 else {
5130 if (px->lbprm.set_server_status_down)
5131 px->lbprm.set_server_status_down(s);
5132 }
5133 }
5134 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5135 /* remaining in drain mode after removing one of its flags */
5136
5137 tmptrash = alloc_trash_chunk();
5138 if (tmptrash) {
5139 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5140 chunk_printf(tmptrash,
5141 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5142 s->flags & SRV_F_BACKUP ? "Backup " : "",
5143 s->proxy->id, s->id);
5144
5145 if (s->track) /* normally it's mandatory here */
5146 chunk_appendf(tmptrash, " via %s/%s",
5147 s->track->proxy->id, s->track->id);
5148 }
5149 else {
5150 chunk_printf(tmptrash,
5151 "%sServer %s/%s remains in forced drain mode",
5152 s->flags & SRV_F_BACKUP ? "Backup " : "",
5153 s->proxy->id, s->id);
5154 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005155 ha_warning("%s.\n", tmptrash->area);
5156 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5157 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005158 free_trash_chunk(tmptrash);
5159 tmptrash = NULL;
5160 }
5161
5162 /* commit new admin status */
5163
5164 s->cur_admin = s->next_admin;
5165 }
5166 }
5167
5168 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005169 *s->adm_st_chg_cause = 0;
5170}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005171
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005172struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5173{
5174 struct connection *conn;
5175
Olivier Houchard859dc802019-08-08 15:47:21 +02005176 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005177 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005178 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005179 }
5180
5181 return task;
5182}
5183
William Dauchy6318d332020-05-02 21:52:36 +02005184/* cleanup connections for a given server
5185 * might be useful when going on forced maintenance or live changing ip/port
5186 */
William Dauchy707ad322020-05-04 13:52:40 +02005187static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005188{
5189 struct connection *conn;
5190 int did_remove;
5191 int i;
5192 int j;
5193
5194 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5195 for (i = 0; i < global.nbthread; i++) {
5196 did_remove = 0;
5197 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
5198 for (j = 0; j < srv->curr_idle_conns; j++) {
5199 conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5200 if (!conn)
5201 conn = MT_LIST_POP(&srv->safe_conns[i],
5202 struct connection *, list);
5203 if (!conn)
5204 break;
5205 did_remove = 1;
5206 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
5207 }
5208 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
5209 if (did_remove)
5210 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5211 }
5212 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5213}
5214
Willy Tarreau980855b2019-02-07 14:59:29 +01005215struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005216{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005217 struct server *srv;
5218 struct eb32_node *eb;
5219 int i;
5220 unsigned int next_wakeup;
5221 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005222
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005223 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5224 while (1) {
5225 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005226 int exceed_conns;
5227 int to_kill;
5228 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005229
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005230 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5231 if (!eb) {
5232 /* we might have reached the end of the tree, typically because
5233 * <now_ms> is in the first half and we're first scanning the last
5234 * half. Let's loop back to the beginning of the tree now.
5235 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005236
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005237 eb = eb32_first(&idle_conn_srv);
5238 if (likely(!eb))
5239 break;
5240 }
5241 if (tick_is_lt(now_ms, eb->key)) {
5242 /* timer not expired yet, revisit it later */
5243 next_wakeup = eb->key;
5244 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005245 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005246 }
5247 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005248
5249 /* Calculate how many idle connections we want to kill :
5250 * we want to remove half the difference between the total
5251 * of established connections (used or idle) and the max
5252 * number of used connections.
5253 */
5254 curr_idle = srv->curr_idle_conns;
5255 if (curr_idle == 0)
5256 goto remove;
5257 exceed_conns = srv->curr_used_conns + curr_idle -
5258 srv->max_used_conns;
5259 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5260 srv->max_used_conns = srv->curr_used_conns;
5261
5262 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5263 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005264 int j;
5265 int did_remove = 0;
5266
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005267 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5268 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005269 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005270 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005271 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5272 if (!conn)
5273 conn = MT_LIST_POP(&srv->safe_conns[i],
5274 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005275 if (!conn)
5276 break;
5277 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005278 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005279 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005280 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005281 if (did_remove && max_conn < srv->curr_idle_thr[i])
5282 srv_is_empty = 0;
5283 if (did_remove)
5284 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5285 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005286remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005287 eb32_delete(&srv->idle_node);
5288 if (!srv_is_empty) {
5289 /* There are still more idle connections, add the
5290 * server back in the tree.
5291 */
5292 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5293 now_ms);
5294 eb32_insert(&idle_conn_srv, &srv->idle_node);
5295 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005296 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005297 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5298
5299 if (need_wakeup)
5300 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005301 else
5302 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005303
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005304 return task;
5305}
Olivier Houchard88698d92019-04-16 19:07:22 +02005306
5307/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5308static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5309 struct proxy *defpx, const char *file, int line,
5310 char **err)
5311{
5312 int arg = -1;
5313
5314 if (too_many_args(1, args, err, NULL))
5315 return -1;
5316
5317 if (*(args[1]) != 0)
5318 arg = atoi(args[1]);
5319
5320 if (arg < 0 || arg > 100) {
5321 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5322 return -1;
5323 }
5324
5325 if (args[0][10] == 'h')
5326 global.tune.pool_high_ratio = arg;
5327 else
5328 global.tune.pool_low_ratio = arg;
5329 return 0;
5330}
5331
5332/* config keyword parsers */
5333static struct cfg_kw_list cfg_kws = {ILH, {
5334 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5335 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5336 { 0, NULL, NULL }
5337}};
5338
5339INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5340
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005341/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005342 * Local variables:
5343 * c-indent-level: 8
5344 * c-basic-offset: 8
5345 * End:
5346 */