blob: 5c457186f449fbccadbf2fee67904819a697ebd8 [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 Tarreau0a4b0ab2020-06-11 11:22:44 +020014#include <sys/types.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <netinet/tcp.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020017#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020018
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <import/ebsttree.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <import/xxhash.h>
21
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020023#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020024#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020025#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020026#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020028#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020029#include <haproxy/dict-t.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020030#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020032#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020033#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020034#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020035#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/port_range.h>
37#include <haproxy/protocol.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020038#include <haproxy/queue.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020040#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020041#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020044#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020045#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020046#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020047
Baptiste Assmannda29fe22019-06-13 13:24:29 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020050static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010052static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020053static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
54static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020055static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau21faa912012-10-10 08:27:36 +020057/* List head of all known server keywords */
58static struct srv_kw_list srv_keywords = {
59 .list = LIST_HEAD_INIT(srv_keywords.list)
60};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061
Willy Tarreauaf613e82020-06-05 08:40:51 +020062__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010063struct eb_root idle_conn_srv = EB_ROOT;
64struct task *idle_conn_task = NULL;
Olivier Houchard9ea5d362019-02-14 18:29:09 +010065
Frédéric Lécaille7da71292019-05-20 09:47:07 +020066/* The server names dictionary */
67struct dict server_name_dict = {
68 .name = "server names",
69 .values = EB_ROOT_UNIQUE,
70};
71
Baptiste Assmannda29fe22019-06-13 13:24:29 +020072/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010073struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020074
Simon Hormana3608442013-11-01 16:46:15 +090075int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020076{
Emeric Brun52a91d32017-08-31 14:41:55 +020077 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020078 return s->down_time;
79
80 return now.tv_sec - s->last_change + s->down_time;
81}
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050083int srv_lastsession(const struct server *s)
84{
85 if (s->counters.last_sess)
86 return now.tv_sec - s->counters.last_sess;
87
88 return -1;
89}
90
Simon Horman4a741432013-02-23 15:35:38 +090091int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020092{
Simon Horman4a741432013-02-23 15:35:38 +090093 const struct server *s = check->server;
94
Willy Tarreauff5ae352013-12-11 20:36:34 +010095 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090096 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010097
Emeric Brun52a91d32017-08-31 14:41:55 +020098 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090099 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100100
Simon Horman4a741432013-02-23 15:35:38 +0900101 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100102}
103
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100104/*
105 * Check that we did not get a hash collision.
106 * Unlikely, but it can happen.
107 */
108static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100109{
110 struct proxy *p = s->proxy;
111 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100112
113 for (tmpserv = p->srv; tmpserv != NULL;
114 tmpserv = tmpserv->next) {
115 if (tmpserv == s)
116 continue;
117 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
118 continue;
119 if (tmpserv->cookie &&
120 strcmp(tmpserv->cookie, s->cookie) == 0) {
121 ha_warning("We generated two equal cookies for two different servers.\n"
122 "Please change the secret key for '%s'.\n",
123 s->proxy->id);
124 }
125 }
126
127}
128
Willy Tarreau46b7f532018-08-21 11:54:26 +0200129/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200130 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200131 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100132void srv_set_dyncookie(struct server *s)
133{
134 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100135 char *tmpbuf;
136 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100137 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100138 size_t buffer_len;
139 int addr_len;
140 int port;
141
Willy Tarreau5e83d992019-07-30 11:59:34 +0200142 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
143
Olivier Houchard4e694042017-03-14 20:01:29 +0100144 if ((s->flags & SRV_F_COOKIESET) ||
145 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
146 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200147 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100148 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100149
150 if (s->addr.ss_family != AF_INET &&
151 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200152 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100153 /*
154 * Buffer to calculate the cookie value.
155 * The buffer contains the secret key + the server IP address
156 * + the TCP port.
157 */
158 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
159 /*
160 * The TCP port should use only 2 bytes, but is stored in
161 * an unsigned int in struct server, so let's use 4, to be
162 * on the safe side.
163 */
164 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200165 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100166 memcpy(tmpbuf, p->dyncookie_key, key_len);
167 memcpy(&(tmpbuf[key_len]),
168 s->addr.ss_family == AF_INET ?
169 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
170 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
171 addr_len);
172 /*
173 * Make sure it's the same across all the load balancers,
174 * no matter their endianness.
175 */
176 port = htonl(s->svc_port);
177 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
178 hash_value = XXH64(tmpbuf, buffer_len, 0);
179 memprintf(&s->cookie, "%016llx", hash_value);
180 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200181 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100182 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100183
184 /* Don't bother checking if the dyncookie is duplicated if
185 * the server is marked as "disabled", maybe it doesn't have
186 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100187 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100188 if (!(s->next_admin & SRV_ADMF_FMAINT))
189 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200190 out:
191 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100192}
193
Willy Tarreau21faa912012-10-10 08:27:36 +0200194/*
195 * Registers the server keyword list <kwl> as a list of valid keywords for next
196 * parsing sessions.
197 */
198void srv_register_keywords(struct srv_kw_list *kwl)
199{
200 LIST_ADDQ(&srv_keywords.list, &kwl->list);
201}
202
203/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
204 * keyword is found with a NULL ->parse() function, then an attempt is made to
205 * find one with a valid ->parse() function. This way it is possible to declare
206 * platform-dependant, known keywords as NULL, then only declare them as valid
207 * if some options are met. Note that if the requested keyword contains an
208 * opening parenthesis, everything from this point is ignored.
209 */
210struct srv_kw *srv_find_kw(const char *kw)
211{
212 int index;
213 const char *kwend;
214 struct srv_kw_list *kwl;
215 struct srv_kw *ret = NULL;
216
217 kwend = strchr(kw, '(');
218 if (!kwend)
219 kwend = kw + strlen(kw);
220
221 list_for_each_entry(kwl, &srv_keywords.list, list) {
222 for (index = 0; kwl->kw[index].kw != NULL; index++) {
223 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
224 kwl->kw[index].kw[kwend-kw] == 0) {
225 if (kwl->kw[index].parse)
226 return &kwl->kw[index]; /* found it !*/
227 else
228 ret = &kwl->kw[index]; /* may be OK */
229 }
230 }
231 }
232 return ret;
233}
234
235/* Dumps all registered "server" keywords to the <out> string pointer. The
236 * unsupported keywords are only dumped if their supported form was not
237 * found.
238 */
239void srv_dump_kws(char **out)
240{
241 struct srv_kw_list *kwl;
242 int index;
243
Christopher Faulet784063e2020-05-18 12:14:18 +0200244 if (!out)
245 return;
246
Willy Tarreau21faa912012-10-10 08:27:36 +0200247 *out = NULL;
248 list_for_each_entry(kwl, &srv_keywords.list, list) {
249 for (index = 0; kwl->kw[index].kw != NULL; index++) {
250 if (kwl->kw[index].parse ||
251 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
252 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
253 kwl->scope,
254 kwl->kw[index].kw,
255 kwl->kw[index].skip ? " <arg>" : "",
256 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
257 kwl->kw[index].parse ? "" : " (not supported)");
258 }
259 }
260 }
261}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100262
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100263/* Parse the "backup" server keyword */
264static int srv_parse_backup(char **args, int *cur_arg,
265 struct proxy *curproxy, struct server *newsrv, char **err)
266{
267 newsrv->flags |= SRV_F_BACKUP;
268 return 0;
269}
270
Alexander Liu2a54bb72019-05-22 19:44:48 +0800271
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100272/* Parse the "cookie" server keyword */
273static int srv_parse_cookie(char **args, int *cur_arg,
274 struct proxy *curproxy, struct server *newsrv, char **err)
275{
276 char *arg;
277
278 arg = args[*cur_arg + 1];
279 if (!*arg) {
280 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
281 return ERR_ALERT | ERR_FATAL;
282 }
283
284 free(newsrv->cookie);
285 newsrv->cookie = strdup(arg);
286 newsrv->cklen = strlen(arg);
287 newsrv->flags |= SRV_F_COOKIESET;
288 return 0;
289}
290
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100291/* Parse the "disabled" server keyword */
292static int srv_parse_disabled(char **args, int *cur_arg,
293 struct proxy *curproxy, struct server *newsrv, char **err)
294{
Emeric Brun52a91d32017-08-31 14:41:55 +0200295 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
296 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100297 newsrv->check.state |= CHK_ST_PAUSED;
298 newsrv->check.health = 0;
299 return 0;
300}
301
302/* Parse the "enabled" server keyword */
303static int srv_parse_enabled(char **args, int *cur_arg,
304 struct proxy *curproxy, struct server *newsrv, char **err)
305{
Emeric Brun52a91d32017-08-31 14:41:55 +0200306 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
307 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100308 newsrv->check.state &= ~CHK_ST_PAUSED;
309 newsrv->check.health = newsrv->check.rise;
310 return 0;
311}
312
Willy Tarreau9c538e02019-01-23 10:21:49 +0100313static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
314{
315 char *arg;
316
317 arg = args[*cur_arg + 1];
318 if (!*arg) {
319 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
320 return ERR_ALERT | ERR_FATAL;
321 }
322 newsrv->max_reuse = atoi(arg);
323
324 return 0;
325}
326
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100327static 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 +0100328{
329 const char *res;
330 char *arg;
331 unsigned int time;
332
333 arg = args[*cur_arg + 1];
334 if (!*arg) {
335 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
336 return ERR_ALERT | ERR_FATAL;
337 }
338 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200339 if (res == PARSE_TIME_OVER) {
340 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
341 args[*cur_arg+1], args[*cur_arg]);
342 return ERR_ALERT | ERR_FATAL;
343 }
344 else if (res == PARSE_TIME_UNDER) {
345 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
346 args[*cur_arg+1], args[*cur_arg]);
347 return ERR_ALERT | ERR_FATAL;
348 }
349 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100350 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
351 *res, args[*cur_arg]);
352 return ERR_ALERT | ERR_FATAL;
353 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100354 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100355
356 return 0;
357}
358
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200359static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
360{
361 char *arg;
362
363 arg = args[*cur_arg + 1];
364 if (!*arg) {
365 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
366 return ERR_ALERT | ERR_FATAL;
367 }
368
369 newsrv->low_idle_conns = atoi(arg);
370 return 0;
371}
372
Olivier Houchard006e3102018-12-10 18:30:32 +0100373static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
374{
375 char *arg;
376
377 arg = args[*cur_arg + 1];
378 if (!*arg) {
379 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
380 return ERR_ALERT | ERR_FATAL;
381 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100382
Olivier Houchard006e3102018-12-10 18:30:32 +0100383 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100384 if ((int)newsrv->max_idle_conns < -1) {
385 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
386 return ERR_ALERT | ERR_FATAL;
387 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100388
389 return 0;
390}
391
Willy Tarreaudff55432012-10-10 17:51:05 +0200392/* parse the "id" server keyword */
393static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
394{
395 struct eb32_node *node;
396
397 if (!*args[*cur_arg + 1]) {
398 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
399 return ERR_ALERT | ERR_FATAL;
400 }
401
402 newsrv->puid = atol(args[*cur_arg + 1]);
403 newsrv->conf.id.key = newsrv->puid;
404
405 if (newsrv->puid <= 0) {
406 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
407 return ERR_ALERT | ERR_FATAL;
408 }
409
410 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
411 if (node) {
412 struct server *target = container_of(node, struct server, conf.id);
413 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
414 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
415 target->id);
416 return ERR_ALERT | ERR_FATAL;
417 }
418
419 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200420 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200421 return 0;
422}
423
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100424/* Parse the "namespace" server keyword */
425static int srv_parse_namespace(char **args, int *cur_arg,
426 struct proxy *curproxy, struct server *newsrv, char **err)
427{
Willy Tarreaue5733232019-05-22 19:24:06 +0200428#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100429 char *arg;
430
431 arg = args[*cur_arg + 1];
432 if (!*arg) {
433 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
434 return ERR_ALERT | ERR_FATAL;
435 }
436
437 if (!strcmp(arg, "*")) {
438 /* Use the namespace associated with the connection (if present). */
439 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
440 return 0;
441 }
442
443 /*
444 * As this parser may be called several times for the same 'default-server'
445 * object, or for a new 'server' instance deriving from a 'default-server'
446 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
447 */
448 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
449
450 newsrv->netns = netns_store_lookup(arg, strlen(arg));
451 if (!newsrv->netns)
452 newsrv->netns = netns_store_insert(arg);
453
454 if (!newsrv->netns) {
455 memprintf(err, "Cannot open namespace '%s'", arg);
456 return ERR_ALERT | ERR_FATAL;
457 }
458
459 return 0;
460#else
461 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
462 return ERR_ALERT | ERR_FATAL;
463#endif
464}
465
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100466/* Parse the "no-backup" server keyword */
467static int srv_parse_no_backup(char **args, int *cur_arg,
468 struct proxy *curproxy, struct server *newsrv, char **err)
469{
470 newsrv->flags &= ~SRV_F_BACKUP;
471 return 0;
472}
473
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100474
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100475/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200476static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100477{
478 srv->pp_opts &= ~flags;
479 return 0;
480}
481
482/* Parse the "no-send-proxy" server keyword */
483static int srv_parse_no_send_proxy(char **args, int *cur_arg,
484 struct proxy *curproxy, struct server *newsrv, char **err)
485{
486 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
487}
488
489/* Parse the "no-send-proxy-v2" server keyword */
490static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
491 struct proxy *curproxy, struct server *newsrv, char **err)
492{
493 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
494}
495
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200496/* Parse the "no-tfo" server keyword */
497static int srv_parse_no_tfo(char **args, int *cur_arg,
498 struct proxy *curproxy, struct server *newsrv, char **err)
499{
500 newsrv->flags &= ~SRV_F_FASTOPEN;
501 return 0;
502}
503
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100504/* Parse the "non-stick" server keyword */
505static int srv_parse_non_stick(char **args, int *cur_arg,
506 struct proxy *curproxy, struct server *newsrv, char **err)
507{
508 newsrv->flags |= SRV_F_NON_STICK;
509 return 0;
510}
511
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100512/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200513static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100514{
515 srv->pp_opts |= flags;
516 return 0;
517}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200518/* parse the "proto" server keyword */
519static int srv_parse_proto(char **args, int *cur_arg,
520 struct proxy *px, struct server *newsrv, char **err)
521{
522 struct ist proto;
523
524 if (!*args[*cur_arg + 1]) {
525 memprintf(err, "'%s' : missing value", args[*cur_arg]);
526 return ERR_ALERT | ERR_FATAL;
527 }
528 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
529 newsrv->mux_proto = get_mux_proto(proto);
530 if (!newsrv->mux_proto) {
531 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
532 return ERR_ALERT | ERR_FATAL;
533 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200534 return 0;
535}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100536
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100537/* parse the "proxy-v2-options" */
538static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
539 struct proxy *px, struct server *newsrv, char **err)
540{
541 char *p, *n;
542 for (p = args[*cur_arg+1]; p; p = n) {
543 n = strchr(p, ',');
544 if (n)
545 *n++ = '\0';
546 if (!strcmp(p, "ssl")) {
547 newsrv->pp_opts |= SRV_PP_V2_SSL;
548 } else if (!strcmp(p, "cert-cn")) {
549 newsrv->pp_opts |= SRV_PP_V2_SSL;
550 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100551 } else if (!strcmp(p, "cert-key")) {
552 newsrv->pp_opts |= SRV_PP_V2_SSL;
553 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
554 } else if (!strcmp(p, "cert-sig")) {
555 newsrv->pp_opts |= SRV_PP_V2_SSL;
556 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
557 } else if (!strcmp(p, "ssl-cipher")) {
558 newsrv->pp_opts |= SRV_PP_V2_SSL;
559 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100560 } else if (!strcmp(p, "authority")) {
561 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100562 } else if (!strcmp(p, "crc32c")) {
563 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100564 } else if (!strcmp(p, "unique-id")) {
565 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100566 } else
567 goto fail;
568 }
569 return 0;
570 fail:
571 if (err)
572 memprintf(err, "'%s' : proxy v2 option not implemented", p);
573 return ERR_ALERT | ERR_FATAL;
574}
575
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100576/* Parse the "observe" server keyword */
577static int srv_parse_observe(char **args, int *cur_arg,
578 struct proxy *curproxy, struct server *newsrv, char **err)
579{
580 char *arg;
581
582 arg = args[*cur_arg + 1];
583 if (!*arg) {
584 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
585 return ERR_ALERT | ERR_FATAL;
586 }
587
588 if (!strcmp(arg, "none")) {
589 newsrv->observe = HANA_OBS_NONE;
590 }
591 else if (!strcmp(arg, "layer4")) {
592 newsrv->observe = HANA_OBS_LAYER4;
593 }
594 else if (!strcmp(arg, "layer7")) {
595 if (curproxy->mode != PR_MODE_HTTP) {
596 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
597 return ERR_ALERT;
598 }
599 newsrv->observe = HANA_OBS_LAYER7;
600 }
601 else {
602 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
603 "but got '%s'\n", args[*cur_arg], arg);
604 return ERR_ALERT | ERR_FATAL;
605 }
606
607 return 0;
608}
609
Frédéric Lécaille16186232017-03-14 16:42:49 +0100610/* Parse the "redir" server keyword */
611static int srv_parse_redir(char **args, int *cur_arg,
612 struct proxy *curproxy, struct server *newsrv, char **err)
613{
614 char *arg;
615
616 arg = args[*cur_arg + 1];
617 if (!*arg) {
618 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
619 return ERR_ALERT | ERR_FATAL;
620 }
621
622 free(newsrv->rdr_pfx);
623 newsrv->rdr_pfx = strdup(arg);
624 newsrv->rdr_len = strlen(arg);
625
626 return 0;
627}
628
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100629/* Parse the "send-proxy" server keyword */
630static int srv_parse_send_proxy(char **args, int *cur_arg,
631 struct proxy *curproxy, struct server *newsrv, char **err)
632{
633 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
634}
635
636/* Parse the "send-proxy-v2" server keyword */
637static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
638 struct proxy *curproxy, struct server *newsrv, char **err)
639{
640 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
641}
642
Frédéric Lécailledba97072017-03-17 15:33:50 +0100643
644/* Parse the "source" server keyword */
645static int srv_parse_source(char **args, int *cur_arg,
646 struct proxy *curproxy, struct server *newsrv, char **err)
647{
648 char *errmsg;
649 int port_low, port_high;
650 struct sockaddr_storage *sk;
651 struct protocol *proto;
652
653 errmsg = NULL;
654
655 if (!*args[*cur_arg + 1]) {
656 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
657 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
658 goto err;
659 }
660
661 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau32819932020-09-04 15:53:16 +0200662 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100663 if (!sk) {
664 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
665 goto err;
666 }
667
668 proto = protocol_by_family(sk->ss_family);
669 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100670 ha_alert("'%s %s' : connect() not supported for this address family.\n",
671 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100672 goto err;
673 }
674
675 newsrv->conn_src.opts |= CO_SRC_BIND;
676 newsrv->conn_src.source_addr = *sk;
677
678 if (port_low != port_high) {
679 int i;
680
Frédéric Lécailledba97072017-03-17 15:33:50 +0100681 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
682 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
683 newsrv->conn_src.sport_range->ports[i] = port_low + i;
684 }
685
686 *cur_arg += 2;
687 while (*(args[*cur_arg])) {
688 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
689#if defined(CONFIG_HAP_TRANSPARENT)
690 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100691 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
692 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100693 goto err;
694 }
695 if (!strcmp(args[*cur_arg + 1], "client")) {
696 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
697 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
698 }
699 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
700 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
701 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
702 }
703 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
704 char *name, *end;
705
706 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100707 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100708 name++;
709
710 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100711 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100712 end++;
713
714 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
715 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
716 free(newsrv->conn_src.bind_hdr_name);
717 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
718 newsrv->conn_src.bind_hdr_len = end - name;
719 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
720 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
721 newsrv->conn_src.bind_hdr_occ = -1;
722
723 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100724 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100725 end++;
726 if (*end == ',') {
727 end++;
728 name = end;
729 if (*end == '-')
730 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100731 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100732 end++;
733 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
734 }
735
736 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100737 ha_alert("usesrc hdr_ip(name,num) does not support negative"
738 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100739 goto err;
740 }
741 }
742 else {
743 struct sockaddr_storage *sk;
744 int port1, port2;
745
746 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau32819932020-09-04 15:53:16 +0200747 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100748 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100749 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100750 goto err;
751 }
752
753 proto = protocol_by_family(sk->ss_family);
754 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100755 ha_alert("'%s %s' : connect() not supported for this address family.\n",
756 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100757 goto err;
758 }
759
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 newsrv->conn_src.tproxy_addr = *sk;
761 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
762 }
763 global.last_checks |= LSTCHK_NETADM;
764 *cur_arg += 2;
765 continue;
766#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100767 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 +0100768 goto err;
769#endif /* defined(CONFIG_HAP_TRANSPARENT) */
770 } /* "usesrc" */
771
772 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
773#ifdef SO_BINDTODEVICE
774 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100775 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100776 goto err;
777 }
778 free(newsrv->conn_src.iface_name);
779 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
780 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
781 global.last_checks |= LSTCHK_NETADM;
782#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100784 goto err;
785#endif
786 *cur_arg += 2;
787 continue;
788 }
789 /* this keyword in not an option of "source" */
790 break;
791 } /* while */
792
793 return 0;
794
795 err:
796 free(errmsg);
797 return ERR_ALERT | ERR_FATAL;
798}
799
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100800/* Parse the "stick" server keyword */
801static int srv_parse_stick(char **args, int *cur_arg,
802 struct proxy *curproxy, struct server *newsrv, char **err)
803{
804 newsrv->flags &= ~SRV_F_NON_STICK;
805 return 0;
806}
807
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100808/* Parse the "track" server keyword */
809static int srv_parse_track(char **args, int *cur_arg,
810 struct proxy *curproxy, struct server *newsrv, char **err)
811{
812 char *arg;
813
814 arg = args[*cur_arg + 1];
815 if (!*arg) {
816 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
817 return ERR_ALERT | ERR_FATAL;
818 }
819
820 free(newsrv->trackit);
821 newsrv->trackit = strdup(arg);
822
823 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800824}
825
826/* Parse the "socks4" server keyword */
827static int srv_parse_socks4(char **args, int *cur_arg,
828 struct proxy *curproxy, struct server *newsrv, char **err)
829{
830 char *errmsg;
831 int port_low, port_high;
832 struct sockaddr_storage *sk;
833 struct protocol *proto;
834
835 errmsg = NULL;
836
837 if (!*args[*cur_arg + 1]) {
838 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
839 goto err;
840 }
841
842 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau32819932020-09-04 15:53:16 +0200843 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM);
Alexander Liu2a54bb72019-05-22 19:44:48 +0800844 if (!sk) {
845 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
846 goto err;
847 }
848
849 proto = protocol_by_family(sk->ss_family);
850 if (!proto || !proto->connect) {
851 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
852 goto err;
853 }
854
855 newsrv->flags |= SRV_F_SOCKS4_PROXY;
856 newsrv->socks4_addr = *sk;
857
Alexander Liu2a54bb72019-05-22 19:44:48 +0800858 return 0;
859
860 err:
861 free(errmsg);
862 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100863}
864
Frédéric Lécailledba97072017-03-17 15:33:50 +0100865
Willy Tarreau034c88c2017-01-23 23:36:45 +0100866/* parse the "tfo" server keyword */
867static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
868{
869 newsrv->flags |= SRV_F_FASTOPEN;
870 return 0;
871}
872
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200873/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200874 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200875 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200876 *
877 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200878 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200879void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200880{
Willy Tarreau87b09662015-04-03 00:22:06 +0200881 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200882
Willy Tarreau87b09662015-04-03 00:22:06 +0200883 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
884 if (stream->srv_conn == srv)
885 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200886}
887
888/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200889 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200890 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200891 *
892 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200893 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200894void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200895{
896 struct server *srv;
897
898 for (srv = px->srv; srv != NULL; srv = srv->next)
899 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200900 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200901}
902
Willy Tarreaubda92272014-05-20 21:55:30 +0200903/* Appends some information to a message string related to a server going UP or
904 * DOWN. If both <forced> and <reason> are null and the server tracks another
905 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200906 * If <check> is non-null, an entire string describing the check result will be
907 * appended after a comma and a space (eg: to report some information from the
908 * check that changed the state). In the other case, the string will be built
909 * using the check results stored into the struct server if present.
910 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200911 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200912 *
913 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200914 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200915void srv_append_status(struct buffer *msg, struct server *s,
916 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200917{
Emeric Brun5a133512017-10-19 14:42:30 +0200918 short status = s->op_st_chg.status;
919 short code = s->op_st_chg.code;
920 long duration = s->op_st_chg.duration;
921 char *desc = s->op_st_chg.reason;
922
923 if (check) {
924 status = check->status;
925 code = check->code;
926 duration = check->duration;
927 desc = check->desc;
928 }
929
930 if (status != -1) {
931 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
932
933 if (status >= HCHK_STATUS_L57DATA)
934 chunk_appendf(msg, ", code: %d", code);
935
936 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200937 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200938
939 chunk_appendf(msg, ", info: \"");
940
941 chunk_initlen(&src, desc, 0, strlen(desc));
942 chunk_asciiencode(msg, &src, '"');
943
944 chunk_appendf(msg, "\"");
945 }
946
947 if (duration >= 0)
948 chunk_appendf(msg, ", check duration: %ldms", duration);
949 }
950 else if (desc && *desc) {
951 chunk_appendf(msg, ", %s", desc);
952 }
953 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200954 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200955 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200956
957 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200958 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200959 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
960 " %d sessions active, %d requeued, %d remaining in queue",
961 s->proxy->srv_act, s->proxy->srv_bck,
962 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
963 s->cur_sess, xferred, s->nbpend);
964 else
965 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
966 " %d sessions requeued, %d total in queue",
967 s->proxy->srv_act, s->proxy->srv_bck,
968 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
969 xferred, s->nbpend);
970 }
971}
972
Emeric Brun5a133512017-10-19 14:42:30 +0200973/* Marks server <s> down, regardless of its checks' statuses. The server is
974 * registered in a list to postpone the counting of the remaining servers on
975 * the proxy and transfers queued streams whenever possible to other servers at
976 * a sync point. Maintenance servers are ignored. It stores the <reason> if
977 * non-null as the reason for going down or the available data from the check
978 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200979 *
980 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200981 */
Emeric Brun5a133512017-10-19 14:42:30 +0200982void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200983{
984 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200985
Emeric Brun64cc49c2017-10-03 14:46:45 +0200986 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200987 return;
988
Emeric Brun52a91d32017-08-31 14:41:55 +0200989 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200990 *s->op_st_chg.reason = 0;
991 s->op_st_chg.status = -1;
992 if (reason) {
993 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
994 }
995 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100996 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200997 s->op_st_chg.code = check->code;
998 s->op_st_chg.status = check->status;
999 s->op_st_chg.duration = check->duration;
1000 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001001
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001002 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001003 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001004
Emeric Brun9f0b4582017-10-23 14:39:51 +02001005 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001006 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001007 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001008 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001009 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001010}
1011
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001012/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001013 * in maintenance. The server is registered in a list to postpone the counting
1014 * of the remaining servers on the proxy and tries to grab requests from the
1015 * proxy at a sync point. Maintenance servers are ignored. It stores the
1016 * <reason> if non-null as the reason for going down or the available data
1017 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001018 *
1019 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001020 */
Emeric Brun5a133512017-10-19 14:42:30 +02001021void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001022{
1023 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001024
Emeric Brun64cc49c2017-10-03 14:46:45 +02001025 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001026 return;
1027
Emeric Brun52a91d32017-08-31 14:41:55 +02001028 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001029 return;
1030
Emeric Brun52a91d32017-08-31 14:41:55 +02001031 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001032 *s->op_st_chg.reason = 0;
1033 s->op_st_chg.status = -1;
1034 if (reason) {
1035 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1036 }
1037 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001038 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001039 s->op_st_chg.code = check->code;
1040 s->op_st_chg.status = check->status;
1041 s->op_st_chg.duration = check->duration;
1042 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001043
Emeric Brun64cc49c2017-10-03 14:46:45 +02001044 if (s->slowstart <= 0)
1045 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001046
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001047 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001048 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001049
Emeric Brun9f0b4582017-10-23 14:39:51 +02001050 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001051 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001052 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001053 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001054 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001055}
1056
Willy Tarreau8eb77842014-05-21 13:54:57 +02001057/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001058 * isn't in maintenance. The server is registered in a list to postpone the
1059 * counting of the remaining servers on the proxy and tries to grab requests
1060 * from the proxy. Maintenance servers are ignored. It stores the
1061 * <reason> if non-null as the reason for going down or the available data
1062 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001063 * up. Note that it makes use of the trash to build the log strings, so <reason>
1064 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001065 *
1066 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001067 */
Emeric Brun5a133512017-10-19 14:42:30 +02001068void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001069{
1070 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001071
Emeric Brun64cc49c2017-10-03 14:46:45 +02001072 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001073 return;
1074
Emeric Brun52a91d32017-08-31 14:41:55 +02001075 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001076 return;
1077
Emeric Brun52a91d32017-08-31 14:41:55 +02001078 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001079 *s->op_st_chg.reason = 0;
1080 s->op_st_chg.status = -1;
1081 if (reason) {
1082 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1083 }
1084 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001085 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001086 s->op_st_chg.code = check->code;
1087 s->op_st_chg.status = check->status;
1088 s->op_st_chg.duration = check->duration;
1089 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001090
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001091 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001092 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001093
Emeric Brun9f0b4582017-10-23 14:39:51 +02001094 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001095 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001096 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001097 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001098 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001099}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001100
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001101/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1102 * enforce either maint mode or drain mode. It is not allowed to set more than
1103 * one flag at once. The equivalent "inherited" flag is propagated to all
1104 * tracking servers. Maintenance mode disables health checks (but not agent
1105 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001106 * is done. If <cause> is non-null, it will be displayed at the end of the log
1107 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001108 *
1109 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001110 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001111void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001112{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001113 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001114
1115 if (!mode)
1116 return;
1117
1118 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001119 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001120 return;
1121
Emeric Brun52a91d32017-08-31 14:41:55 +02001122 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001123 if (cause)
1124 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1125
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001126 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001127 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001128
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001129 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001130 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1131 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001132 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001133
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001134 /* compute the inherited flag to propagate */
1135 if (mode & SRV_ADMF_MAINT)
1136 mode = SRV_ADMF_IMAINT;
1137 else if (mode & SRV_ADMF_DRAIN)
1138 mode = SRV_ADMF_IDRAIN;
1139
Emeric Brun9f0b4582017-10-23 14:39:51 +02001140 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001141 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001142 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001143 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001144 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001145}
1146
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001147/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1148 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1149 * than one flag at once. The equivalent "inherited" flag is propagated to all
1150 * tracking servers. Leaving maintenance mode re-enables health checks. When
1151 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001152 *
1153 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001154 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001155void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001156{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001157 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001158
1159 if (!mode)
1160 return;
1161
1162 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001163 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001164 return;
1165
Emeric Brun52a91d32017-08-31 14:41:55 +02001166 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001167
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001168 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001169 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001170
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001171 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001172 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1173 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001174 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001175
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001176 if (mode & SRV_ADMF_MAINT)
1177 mode = SRV_ADMF_IMAINT;
1178 else if (mode & SRV_ADMF_DRAIN)
1179 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001180
Emeric Brun9f0b4582017-10-23 14:39:51 +02001181 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001182 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001183 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001184 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001185 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001186}
1187
Willy Tarreau757478e2016-11-03 19:22:19 +01001188/* principle: propagate maint and drain to tracking servers. This is useful
1189 * upon startup so that inherited states are correct.
1190 */
1191static void srv_propagate_admin_state(struct server *srv)
1192{
1193 struct server *srv2;
1194
1195 if (!srv->trackers)
1196 return;
1197
1198 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001199 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001200 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001201 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001202
Emeric Brun52a91d32017-08-31 14:41:55 +02001203 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001204 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001205 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001206 }
1207}
1208
1209/* Compute and propagate the admin states for all servers in proxy <px>.
1210 * Only servers *not* tracking another one are considered, because other
1211 * ones will be handled when the server they track is visited.
1212 */
1213void srv_compute_all_admin_states(struct proxy *px)
1214{
1215 struct server *srv;
1216
1217 for (srv = px->srv; srv; srv = srv->next) {
1218 if (srv->track)
1219 continue;
1220 srv_propagate_admin_state(srv);
1221 }
1222}
1223
Willy Tarreaudff55432012-10-10 17:51:05 +02001224/* Note: must not be declared <const> as its list will be overwritten.
1225 * Please take care of keeping this list alphabetically sorted, doing so helps
1226 * all code contributors.
1227 * Optional keywords are also declared with a NULL ->parse() function so that
1228 * the config parser can report an appropriate error when a known keyword was
1229 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001230 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001231 */
1232static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001233 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001234 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001235 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1236 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001237 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001238 { "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 +01001239 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001240 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001241 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1242 { "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 +02001243 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001244 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001245 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001246 { "pool-low-conn", srv_parse_pool_low_conn, 1, 1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */
Olivier Houchard006e3102018-12-10 18:30:32 +01001247 { "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 +01001248 { "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 +02001249 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001250 { "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 +01001251 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001252 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1253 { "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 +02001254 { "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 +01001255 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001256 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001257 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001258 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001259 { NULL, NULL, 0 },
1260}};
1261
Willy Tarreau0108d902018-11-25 19:14:37 +01001262INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001263
Willy Tarreau004e0452013-11-21 11:22:01 +01001264/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001265 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001266 * state is automatically disabled if the time is elapsed. If <must_update> is
1267 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001268 *
1269 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001270 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001271void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001272{
1273 struct proxy *px = sv->proxy;
1274 unsigned w;
1275
1276 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1277 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001278 if (sv->next_state == SRV_ST_STARTING)
1279 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001280 }
1281
1282 /* We must take care of not pushing the server to full throttle during slow starts.
1283 * It must also start immediately, at least at the minimal step when leaving maintenance.
1284 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001285 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001286 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1287 else
1288 w = px->lbprm.wdiv;
1289
Emeric Brun52a91d32017-08-31 14:41:55 +02001290 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001291
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001292 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001293 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001294 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001295}
1296
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001298 * Parses weight_str and configures sv accordingly.
1299 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001300 *
1301 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001302 */
1303const char *server_parse_weight_change_request(struct server *sv,
1304 const char *weight_str)
1305{
1306 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001307 long int w;
1308 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001309
1310 px = sv->proxy;
1311
1312 /* if the weight is terminated with '%', it is set relative to
1313 * the initial weight, otherwise it is absolute.
1314 */
1315 if (!*weight_str)
1316 return "Require <weight> or <weight%>.\n";
1317
Simon Hormanb796afa2013-02-12 10:45:53 +09001318 w = strtol(weight_str, &end, 10);
1319 if (end == weight_str)
1320 return "Empty weight string empty or preceded by garbage";
1321 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001322 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001323 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001324 /* Avoid integer overflow */
1325 if (w > 25600)
1326 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001327 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001328 if (w > 256)
1329 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001330 }
1331 else if (w < 0 || w > 256)
1332 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001333 else if (end[0] != '\0')
1334 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001335
1336 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1337 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1338
1339 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001340 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001341
1342 return NULL;
1343}
1344
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001345/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001346 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1347 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001348 * Returns:
1349 * - error string on error
1350 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001351 *
1352 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001353 */
1354const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001355 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001356{
1357 unsigned char ip[INET6_ADDRSTRLEN];
1358
1359 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001360 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001361 return NULL;
1362 }
1363 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001364 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001365 return NULL;
1366 }
1367
1368 return "Could not understand IP address format.\n";
1369}
1370
Willy Tarreau46b7f532018-08-21 11:54:26 +02001371/*
1372 * Must be called with the server lock held.
1373 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001374const char *server_parse_maxconn_change_request(struct server *sv,
1375 const char *maxconn_str)
1376{
1377 long int v;
1378 char *end;
1379
1380 if (!*maxconn_str)
1381 return "Require <maxconn>.\n";
1382
1383 v = strtol(maxconn_str, &end, 10);
1384 if (end == maxconn_str)
1385 return "maxconn string empty or preceded by garbage";
1386 else if (end[0] != '\0')
1387 return "Trailing garbage in maxconn string";
1388
1389 if (sv->maxconn == sv->minconn) { // static maxconn
1390 sv->maxconn = sv->minconn = v;
1391 } else { // dynamic maxconn
1392 sv->maxconn = v;
1393 }
1394
1395 if (may_dequeue_tasks(sv, sv->proxy))
1396 process_srv_queue(sv);
1397
1398 return NULL;
1399}
1400
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001401#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001402static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1403 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001404{
1405 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001406 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001407 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001408 NULL,
1409 };
1410
1411 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001412 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001413
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001414 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001415}
1416
1417static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1418{
1419 struct sample_expr *expr;
1420
1421 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 +01001422 if (!expr) {
1423 memprintf(err, "error detected while parsing sni expression : %s", *err);
1424 return ERR_ALERT | ERR_FATAL;
1425 }
1426
1427 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1428 memprintf(err, "error detected while parsing sni expression : "
1429 " fetch method '%s' extracts information from '%s', "
1430 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001431 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001432 return ERR_ALERT | ERR_FATAL;
1433 }
1434
1435 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1436 release_sample_expr(newsrv->ssl_ctx.sni);
1437 newsrv->ssl_ctx.sni = expr;
1438
1439 return 0;
1440}
1441#endif
1442
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001443static 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 +01001444{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001445 char *msg = "error encountered while processing ";
1446 char *quote = "'";
1447 char *token = args[cur_arg];
1448
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001449 if (err && *err) {
1450 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001451 msg = *err;
1452 quote = "";
1453 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001454 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001455
1456 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1457 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1458 file, linenum, args[0], args[1],
1459 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001460 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001461 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1462 file, linenum, args[0], args[1],
1463 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001464}
1465
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001466static void srv_conn_src_sport_range_cpy(struct server *srv,
1467 struct server *src)
1468{
1469 int range_sz;
1470
1471 range_sz = src->conn_src.sport_range->size;
1472 if (range_sz > 0) {
1473 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1474 if (srv->conn_src.sport_range != NULL) {
1475 int i;
1476
1477 for (i = 0; i < range_sz; i++) {
1478 srv->conn_src.sport_range->ports[i] =
1479 src->conn_src.sport_range->ports[i];
1480 }
1481 }
1482 }
1483}
1484
1485/*
1486 * Copy <src> server connection source settings to <srv> server everything needed.
1487 */
1488static void srv_conn_src_cpy(struct server *srv, struct server *src)
1489{
1490 srv->conn_src.opts = src->conn_src.opts;
1491 srv->conn_src.source_addr = src->conn_src.source_addr;
1492
1493 /* Source port range copy. */
1494 if (src->conn_src.sport_range != NULL)
1495 srv_conn_src_sport_range_cpy(srv, src);
1496
1497#ifdef CONFIG_HAP_TRANSPARENT
1498 if (src->conn_src.bind_hdr_name != NULL) {
1499 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1500 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1501 }
1502 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1503 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1504#endif
1505 if (src->conn_src.iface_name != NULL)
1506 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1507}
1508
1509/*
1510 * Copy <src> server SSL settings to <srv> server allocating
1511 * everything needed.
1512 */
1513#if defined(USE_OPENSSL)
1514static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1515{
1516 if (src->ssl_ctx.ca_file != NULL)
1517 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1518 if (src->ssl_ctx.crl_file != NULL)
1519 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1520 if (src->ssl_ctx.client_crt != NULL)
1521 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1522
1523 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1524
1525 if (src->ssl_ctx.verify_host != NULL)
1526 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1527 if (src->ssl_ctx.ciphers != NULL)
1528 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001529 if (src->ssl_ctx.options)
1530 srv->ssl_ctx.options = src->ssl_ctx.options;
1531 if (src->ssl_ctx.methods.flags)
1532 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1533 if (src->ssl_ctx.methods.min)
1534 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1535 if (src->ssl_ctx.methods.max)
1536 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1537
Willy Tarreau5db847a2019-05-09 14:13:35 +02001538#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001539 if (src->ssl_ctx.ciphersuites != NULL)
1540 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1541#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001542 if (src->sni_expr != NULL)
1543 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001544
1545#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1546 if (src->ssl_ctx.alpn_str) {
1547 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1548 if (srv->ssl_ctx.alpn_str) {
1549 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1550 src->ssl_ctx.alpn_len);
1551 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1552 }
1553 }
1554#endif
1555#ifdef OPENSSL_NPN_NEGOTIATED
1556 if (src->ssl_ctx.npn_str) {
1557 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1558 if (srv->ssl_ctx.npn_str) {
1559 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1560 src->ssl_ctx.npn_len);
1561 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1562 }
1563 }
1564#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001565}
1566#endif
1567
1568/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001569 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001570 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001571 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001572 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001573static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001574{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001575 char *hostname_dn;
1576 int hostname_len, hostname_dn_len;
1577
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001578 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001579 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001580
Christopher Faulet67957bd2017-09-27 11:00:59 +02001581 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001582 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001583 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1584 hostname_dn, trash.size);
1585 if (hostname_dn_len == -1)
1586 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001587
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001588
Christopher Faulet67957bd2017-09-27 11:00:59 +02001589 free(srv->hostname);
1590 free(srv->hostname_dn);
1591 srv->hostname = strdup(hostname);
1592 srv->hostname_dn = strdup(hostname_dn);
1593 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001594 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001595 goto err;
1596
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001597 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001598
1599 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001600 free(srv->hostname); srv->hostname = NULL;
1601 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001602 return -1;
1603}
1604
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001605/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001606 * Copy <src> server settings to <srv> server allocating
1607 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001608 * This function is not supposed to be called at any time, but only
1609 * during server settings parsing or during server allocations from
1610 * a server template, and just after having calloc()'ed a new server.
1611 * So, <src> may only be a default server (when parsing server settings)
1612 * or a server template (during server allocations from a server template).
1613 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1614 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001615 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001616static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001617{
1618 /* Connection source settings copy */
1619 srv_conn_src_cpy(srv, src);
1620
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001621 if (srv_tmpl) {
1622 srv->addr = src->addr;
1623 srv->svc_port = src->svc_port;
1624 }
1625
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001626 srv->pp_opts = src->pp_opts;
1627 if (src->rdr_pfx != NULL) {
1628 srv->rdr_pfx = strdup(src->rdr_pfx);
1629 srv->rdr_len = src->rdr_len;
1630 }
1631 if (src->cookie != NULL) {
1632 srv->cookie = strdup(src->cookie);
1633 srv->cklen = src->cklen;
1634 }
1635 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001636 srv->check.addr = src->check.addr;
1637 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001638 srv->check.use_ssl = src->check.use_ssl;
1639 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001640 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001641 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001642 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001643 /* Note: 'flags' field has potentially been already initialized. */
1644 srv->flags |= src->flags;
1645 srv->do_check = src->do_check;
1646 srv->do_agent = src->do_agent;
1647 if (srv->check.port)
1648 srv->flags |= SRV_F_CHECKPORT;
1649 srv->check.inter = src->check.inter;
1650 srv->check.fastinter = src->check.fastinter;
1651 srv->check.downinter = src->check.downinter;
1652 srv->agent.use_ssl = src->agent.use_ssl;
1653 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001654
1655 if (src->agent.tcpcheck_rules) {
1656 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1657 if (srv->agent.tcpcheck_rules) {
1658 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1659 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1660 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1661 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1662 &src->agent.tcpcheck_rules->preset_vars);
1663 }
1664 }
1665
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001666 srv->agent.inter = src->agent.inter;
1667 srv->agent.fastinter = src->agent.fastinter;
1668 srv->agent.downinter = src->agent.downinter;
1669 srv->maxqueue = src->maxqueue;
1670 srv->minconn = src->minconn;
1671 srv->maxconn = src->maxconn;
1672 srv->slowstart = src->slowstart;
1673 srv->observe = src->observe;
1674 srv->onerror = src->onerror;
1675 srv->onmarkeddown = src->onmarkeddown;
1676 srv->onmarkedup = src->onmarkedup;
1677 if (src->trackit != NULL)
1678 srv->trackit = strdup(src->trackit);
1679 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1680 srv->uweight = srv->iweight = src->iweight;
1681
1682 srv->check.send_proxy = src->check.send_proxy;
1683 /* health: up, but will fall down at first failure */
1684 srv->check.rise = srv->check.health = src->check.rise;
1685 srv->check.fall = src->check.fall;
1686
1687 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001688 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1689 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1690 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001691 srv->check.state |= CHK_ST_PAUSED;
1692 srv->check.health = 0;
1693 }
1694
1695 /* health: up but will fall down at first failure */
1696 srv->agent.rise = srv->agent.health = src->agent.rise;
1697 srv->agent.fall = src->agent.fall;
1698
1699 if (src->resolvers_id != NULL)
1700 srv->resolvers_id = strdup(src->resolvers_id);
1701 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001702 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001703 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001704 if (srv->dns_opts.family_prio == AF_UNSPEC)
1705 srv->dns_opts.family_prio = AF_INET6;
1706 memcpy(srv->dns_opts.pref_net,
1707 src->dns_opts.pref_net,
1708 sizeof srv->dns_opts.pref_net);
1709 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1710
1711 srv->init_addr_methods = src->init_addr_methods;
1712 srv->init_addr = src->init_addr;
1713#if defined(USE_OPENSSL)
1714 srv_ssl_settings_cpy(srv, src);
1715#endif
1716#ifdef TCP_USER_TIMEOUT
1717 srv->tcp_ut = src->tcp_ut;
1718#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001719 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001720 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001721 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01001722 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001723 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001724
Olivier Houchard8da5f982017-08-04 18:35:36 +02001725 if (srv_tmpl)
1726 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001727
1728 srv->check.via_socks4 = src->check.via_socks4;
1729 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001730}
1731
William Lallemand313bfd12018-10-26 14:47:32 +02001732struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001733{
1734 struct server *srv;
1735
1736 srv = calloc(1, sizeof *srv);
1737 if (!srv)
1738 return NULL;
1739
1740 srv->obj_type = OBJ_TYPE_SERVER;
1741 srv->proxy = proxy;
1742 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001743 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001744
Emeric Brun52a91d32017-08-31 14:41:55 +02001745 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001746 srv->last_change = now.tv_sec;
1747
Christopher Faulet38290462020-04-21 11:46:40 +02001748 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749 srv->check.status = HCHK_STATUS_INI;
1750 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001751 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001752 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001753
Christopher Faulet38290462020-04-21 11:46:40 +02001754 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001755 srv->agent.status = HCHK_STATUS_INI;
1756 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001757 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001758 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1759
Willy Tarreau975b1552019-06-06 16:25:55 +02001760 /* please don't put default server settings here, they are set in
1761 * init_default_instance().
1762 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001763 return srv;
1764}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001765
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001766#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1767static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1768 struct server *srv, struct proxy *proxy)
1769{
1770 int ret;
1771 char *err = NULL;
1772
1773 if (!srv->sni_expr)
1774 return 0;
1775
1776 ret = server_parse_sni_expr(srv, proxy, &err);
1777 if (!ret)
1778 return 0;
1779
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001780 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001781 free(err);
1782
1783 return ret;
1784}
1785#endif
1786
1787/*
1788 * Server initializations finalization.
1789 * Initialize health check, agent check and SNI expression if enabled.
1790 * Must not be called for a default server instance.
1791 */
1792static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1793 struct server *srv, struct proxy *px)
1794{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001795#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001796 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001797#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001798
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001799 if (srv->do_check && srv->trackit) {
1800 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1801 file, linenum);
1802 return ERR_ALERT | ERR_FATAL;
1803 }
1804
1805 if (srv->do_agent && !srv->agent.port) {
1806 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1807 file, linenum, srv->id);
1808 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001809 }
1810
1811#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1812 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1813 return ret;
1814#endif
1815
1816 if (srv->flags & SRV_F_BACKUP)
1817 px->srv_bck++;
1818 else
1819 px->srv_act++;
1820 srv_lb_commit_status(srv);
1821
1822 return 0;
1823}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001824
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001825/*
1826 * Parse as much as possible such a range string argument: low[-high]
1827 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1828 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1829 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1830 * Returns 0 if succeeded, -1 if not.
1831 */
1832static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1833{
1834 char *nb_high_arg;
1835
1836 *nb_high = 0;
1837 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001838 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001839
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001840 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001841 *nb_high_arg++ = '\0';
1842 *nb_high = atoi(nb_high_arg);
1843 }
1844 else {
1845 *nb_high += *nb_low;
1846 *nb_low = 1;
1847 }
1848
1849 if (*nb_low < 0 || *nb_high < *nb_low)
1850 return -1;
1851
1852 return 0;
1853}
1854
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001855static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1856{
1857 chunk_printf(&trash, "%s%d", prefix, nb);
1858 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001859 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001860}
1861
1862/*
1863 * Initialize as much as possible servers from <srv> server template.
1864 * Note that a server template is a special server with
1865 * a few different parameters than a server which has
1866 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001867 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001868 * 'srv' template included.
1869 */
1870static int server_template_init(struct server *srv, struct proxy *px)
1871{
1872 int i;
1873 struct server *newsrv;
1874
1875 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 +02001876 newsrv = new_server(px);
1877 if (!newsrv)
1878 goto err;
1879
1880 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001881 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001882#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1883 if (newsrv->sni_expr) {
1884 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1885 if (!newsrv->ssl_ctx.sni)
1886 goto err;
1887 }
1888#endif
1889 /* Set this new server ID. */
1890 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1891
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001892 /* Linked backwards first. This will be restablished after parsing. */
1893 newsrv->next = px->srv;
1894 px->srv = newsrv;
1895 }
1896 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1897
1898 return i - srv->tmpl_info.nb_low;
1899
1900 err:
1901 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1902 if (newsrv) {
1903#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1904 release_sample_expr(newsrv->ssl_ctx.sni);
1905#endif
1906 free_check(&newsrv->agent);
1907 free_check(&newsrv->check);
1908 }
1909 free(newsrv);
1910 return i - srv->tmpl_info.nb_low;
1911}
1912
Emeric Brund3db3842020-07-21 16:54:36 +02001913int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy,
1914 struct proxy *defproxy, int parse_addr, int in_peers_section, int initial_resolve)
Willy Tarreau272adea2014-03-31 10:39:59 +02001915{
1916 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001917 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001918 char *errmsg = NULL;
1919 int err_code = 0;
1920 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001921 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001922
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001923 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001924 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001925 !strcmp(args[0], "default-server") ||
1926 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001927 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001928 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001929 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001930 int srv_tmpl = !defsrv && !srv;
1931 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001932
1933 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001934 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001935 err_code |= ERR_ALERT | ERR_FATAL;
1936 goto out;
1937 }
1938 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001939 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001940
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001941 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001942 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001943 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001944 if (in_peers_section) {
1945 return 0;
1946 }
1947 else {
1948 /* 'server' line number of argument check. */
1949 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1950 file, linenum, args[0]);
1951 err_code |= ERR_ALERT | ERR_FATAL;
1952 goto out;
1953 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954 }
1955
1956 err = invalid_char(args[1]);
1957 }
1958 else if (srv_tmpl) {
1959 if (!*args[3]) {
1960 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001961 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 +02001962 file, linenum, args[0]);
1963 err_code |= ERR_ALERT | ERR_FATAL;
1964 goto out;
1965 }
1966
1967 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001968 }
1969
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001970 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001971 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 +02001972 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001973 err_code |= ERR_ALERT | ERR_FATAL;
1974 goto out;
1975 }
1976
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001977 cur_arg = 2;
1978 if (srv_tmpl) {
1979 /* Parse server-template <nb | range> arg. */
1980 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001981 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001982 file, linenum, args[0], args[cur_arg]);
1983 err_code |= ERR_ALERT | ERR_FATAL;
1984 goto out;
1985 }
1986 cur_arg++;
1987 }
1988
Willy Tarreau272adea2014-03-31 10:39:59 +02001989 if (!defsrv) {
1990 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001991 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001992 struct protocol *proto;
1993
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001994 newsrv = new_server(curproxy);
1995 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001996 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02001997 err_code |= ERR_ALERT | ERR_ABORT;
1998 goto out;
1999 }
2000
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002001 if (srv_tmpl) {
2002 newsrv->tmpl_info.nb_low = tmpl_range_low;
2003 newsrv->tmpl_info.nb_high = tmpl_range_high;
2004 }
2005
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 /* the servers are linked backwards first */
2007 newsrv->next = curproxy->srv;
2008 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002009 newsrv->conf.file = strdup(file);
2010 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002011 /* Note: for a server template, its id is its prefix.
2012 * This is a temporary id which will be used for server allocations to come
2013 * after parsing.
2014 */
2015 if (srv)
2016 newsrv->id = strdup(args[1]);
2017 else
2018 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002019
2020 /* several ways to check the port component :
2021 * - IP => port=+0, relative (IPv4 only)
2022 * - IP: => port=+0, relative
2023 * - IP:N => port=N, absolute
2024 * - IP:+N => port=+N, relative
2025 * - IP:-N => port=-N, relative
2026 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002027 if (!parse_addr)
2028 goto skip_addr;
2029
Willy Tarreau32819932020-09-04 15:53:16 +02002030 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, (initial_resolve ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | PA_O_PORT_OFS | PA_O_STREAM | PA_O_XPRT);
Willy Tarreau272adea2014-03-31 10:39:59 +02002031 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002032 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002033 err_code |= ERR_ALERT | ERR_FATAL;
2034 goto out;
2035 }
2036
2037 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002038 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002039 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002040 file, linenum, args[0], args[1]);
2041 err_code |= ERR_ALERT | ERR_FATAL;
2042 goto out;
2043 }
2044
2045 if (!port1 || !port2) {
2046 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002047 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002048 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002049
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002050 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002051 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002052 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002053 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002054 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2055 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2056 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002057 err_code |= ERR_ALERT | ERR_FATAL;
2058 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002059 }
2060 }
2061 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002062 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002063 file, linenum, newsrv->id);
2064 err_code |= ERR_ALERT | ERR_FATAL;
2065 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002066 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002067 }
2068
Willy Tarreau272adea2014-03-31 10:39:59 +02002069 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002070 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002071
Olivier Houchard8da5f982017-08-04 18:35:36 +02002072 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002073 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002074 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002075 err_code |= ERR_ALERT | ERR_FATAL;
2076 goto out;
2077 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002078
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002079 cur_arg++;
2080 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002081 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002082 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002083 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002084 } else {
2085 newsrv = &curproxy->defsrv;
2086 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002087 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002088 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 }
2090
2091 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002092 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002093 char *p, *end;
2094 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002095 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002096
2097 newsrv->init_addr_methods = 0;
2098 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2099
2100 for (p = args[cur_arg + 1]; *p; p = end) {
2101 /* cut on next comma */
2102 for (end = p; *end && *end != ','; end++);
2103 if (*end)
2104 *(end++) = 0;
2105
Willy Tarreau4310d362016-11-02 15:05:56 +01002106 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002107 if (!strcmp(p, "libc")) {
2108 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2109 }
2110 else if (!strcmp(p, "last")) {
2111 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2112 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002113 else if (!strcmp(p, "none")) {
2114 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2115 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002116 else if (str2ip2(p, &sa, 0)) {
2117 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002118 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002119 file, linenum, args[cur_arg], p);
2120 err_code |= ERR_ALERT | ERR_FATAL;
2121 goto out;
2122 }
2123 newsrv->init_addr = sa;
2124 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2125 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002126 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002127 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 +02002128 file, linenum, args[cur_arg], p);
2129 err_code |= ERR_ALERT | ERR_FATAL;
2130 goto out;
2131 }
2132 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002133 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002134 file, linenum, args[cur_arg], p);
2135 err_code |= ERR_ALERT | ERR_FATAL;
2136 goto out;
2137 }
2138 }
2139 cur_arg += 2;
2140 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002141 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002142 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002143 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2144 cur_arg += 2;
2145 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002146 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2147 char *p, *end;
2148
2149 for (p = args[cur_arg + 1]; *p; p = end) {
2150 /* cut on next comma */
2151 for (end = p; *end && *end != ','; end++);
2152 if (*end)
2153 *(end++) = 0;
2154
2155 if (!strcmp(p, "allow-dup-ip")) {
2156 newsrv->dns_opts.accept_duplicate_ip = 1;
2157 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002158 else if (!strcmp(p, "ignore-weight")) {
2159 newsrv->dns_opts.ignore_weight = 1;
2160 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002161 else if (!strcmp(p, "prevent-dup-ip")) {
2162 newsrv->dns_opts.accept_duplicate_ip = 0;
2163 }
2164 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002165 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 +02002166 file, linenum, args[cur_arg], p);
2167 err_code |= ERR_ALERT | ERR_FATAL;
2168 goto out;
2169 }
2170 }
2171
2172 cur_arg += 2;
2173 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002174 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2175 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002176 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002177 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002178 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002179 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002180 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002181 file, linenum, args[cur_arg]);
2182 err_code |= ERR_ALERT | ERR_FATAL;
2183 goto out;
2184 }
2185 cur_arg += 2;
2186 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002187 else if (!strcmp(args[cur_arg], "resolve-net")) {
2188 char *p, *e;
2189 unsigned char mask;
2190 struct dns_options *opt;
2191
2192 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002193 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002194 file, linenum, args[cur_arg]);
2195 err_code |= ERR_ALERT | ERR_FATAL;
2196 goto out;
2197 }
2198
2199 opt = &newsrv->dns_opts;
2200
2201 /* Split arguments by comma, and convert it from ipv4 or ipv6
2202 * string network in in_addr or in6_addr.
2203 */
2204 p = args[cur_arg + 1];
2205 e = p;
2206 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002207 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002208 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002209 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002210 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2211 err_code |= ERR_ALERT | ERR_FATAL;
2212 goto out;
2213 }
2214 /* look for end or comma. */
2215 while (*e != ',' && *e != '\0')
2216 e++;
2217 if (*e == ',') {
2218 *e = '\0';
2219 e++;
2220 }
2221 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2222 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2223 /* Try to convert input string from ipv4 or ipv6 network. */
2224 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2225 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2226 &mask)) {
2227 /* Try to convert input string from ipv6 network. */
2228 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2229 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2230 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002231 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002232 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002233 file, linenum, args[cur_arg], p);
2234 err_code |= ERR_ALERT | ERR_FATAL;
2235 goto out;
2236 }
2237 opt->pref_net_nb++;
2238 p = e;
2239 }
2240
2241 cur_arg += 2;
2242 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002243 else if (!strcmp(args[cur_arg], "weight")) {
2244 int w;
2245 w = atol(args[cur_arg + 1]);
2246 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002247 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002248 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2249 err_code |= ERR_ALERT | ERR_FATAL;
2250 goto out;
2251 }
2252 newsrv->uweight = newsrv->iweight = w;
2253 cur_arg += 2;
2254 }
Emeric Brun97556472020-05-30 01:42:45 +02002255 else if (!strcmp(args[cur_arg], "log-proto")) {
2256 if (!strcmp(args[cur_arg + 1], "legacy"))
2257 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2258 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2259 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2260 else {
2261 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2262 "'octet-count' but got '%s'\n",
2263 file, linenum, args[cur_arg], args[cur_arg + 1]);
2264 err_code |= ERR_ALERT | ERR_FATAL;
2265 goto out;
2266 }
2267 cur_arg += 2;
2268 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002269 else if (!strcmp(args[cur_arg], "minconn")) {
2270 newsrv->minconn = atol(args[cur_arg + 1]);
2271 cur_arg += 2;
2272 }
2273 else if (!strcmp(args[cur_arg], "maxconn")) {
2274 newsrv->maxconn = atol(args[cur_arg + 1]);
2275 cur_arg += 2;
2276 }
2277 else if (!strcmp(args[cur_arg], "maxqueue")) {
2278 newsrv->maxqueue = atol(args[cur_arg + 1]);
2279 cur_arg += 2;
2280 }
2281 else if (!strcmp(args[cur_arg], "slowstart")) {
2282 /* slowstart is stored in seconds */
2283 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002284
2285 if (err == PARSE_TIME_OVER) {
2286 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2287 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2288 err_code |= ERR_ALERT | ERR_FATAL;
2289 goto out;
2290 }
2291 else if (err == PARSE_TIME_UNDER) {
2292 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2293 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2294 err_code |= ERR_ALERT | ERR_FATAL;
2295 goto out;
2296 }
2297 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002298 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002299 file, linenum, *err, newsrv->id);
2300 err_code |= ERR_ALERT | ERR_FATAL;
2301 goto out;
2302 }
2303 newsrv->slowstart = (val + 999) / 1000;
2304 cur_arg += 2;
2305 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002306 else if (!strcmp(args[cur_arg], "on-error")) {
2307 if (!strcmp(args[cur_arg + 1], "fastinter"))
2308 newsrv->onerror = HANA_ONERR_FASTINTER;
2309 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2310 newsrv->onerror = HANA_ONERR_FAILCHK;
2311 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2312 newsrv->onerror = HANA_ONERR_SUDDTH;
2313 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2314 newsrv->onerror = HANA_ONERR_MARKDWN;
2315 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002316 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002317 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2318 file, linenum, args[cur_arg], args[cur_arg + 1]);
2319 err_code |= ERR_ALERT | ERR_FATAL;
2320 goto out;
2321 }
2322
2323 cur_arg += 2;
2324 }
2325 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2326 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2327 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2328 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002329 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 file, linenum, args[cur_arg], args[cur_arg + 1]);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334
2335 cur_arg += 2;
2336 }
2337 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2338 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2339 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2340 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002341 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002342 file, linenum, args[cur_arg], args[cur_arg + 1]);
2343 err_code |= ERR_ALERT | ERR_FATAL;
2344 goto out;
2345 }
2346
2347 cur_arg += 2;
2348 }
2349 else if (!strcmp(args[cur_arg], "error-limit")) {
2350 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002351 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002352 file, linenum, args[cur_arg]);
2353 err_code |= ERR_ALERT | ERR_FATAL;
2354 goto out;
2355 }
2356
2357 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2358
2359 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002360 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002361 file, linenum, args[cur_arg]);
2362 err_code |= ERR_ALERT | ERR_FATAL;
2363 goto out;
2364 }
2365 cur_arg += 2;
2366 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002367 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002368 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002369 file, linenum, "usesrc", "source");
2370 err_code |= ERR_ALERT | ERR_FATAL;
2371 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002372 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002373 else {
2374 static int srv_dumped;
2375 struct srv_kw *kw;
2376 char *err;
2377
2378 kw = srv_find_kw(args[cur_arg]);
2379 if (kw) {
2380 char *err = NULL;
2381 int code;
2382
2383 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002384 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 +02002385 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002386 if (kw->skip != -1)
2387 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002388 err_code |= ERR_ALERT | ERR_FATAL;
2389 goto out;
2390 }
2391
2392 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002393 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002394 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002395 if (kw->skip != -1)
2396 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002397 err_code |= ERR_ALERT;
2398 continue;
2399 }
2400
2401 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2402 err_code |= code;
2403
2404 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002405 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002406 if (code & ERR_FATAL) {
2407 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002408 if (kw->skip != -1)
2409 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002410 goto out;
2411 }
2412 }
2413 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002414 if (kw->skip != -1)
2415 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002416 continue;
2417 }
2418
2419 err = NULL;
2420 if (!srv_dumped) {
2421 srv_dump_kws(&err);
2422 indent_msg(&err, 4);
2423 srv_dumped = 1;
2424 }
2425
Christopher Faulet767a84b2017-11-24 16:50:31 +01002426 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002427 file, linenum, args[0], args[1], args[cur_arg],
2428 err ? " Registered keywords :" : "", err ? err : "");
2429 free(err);
2430
2431 err_code |= ERR_ALERT | ERR_FATAL;
2432 goto out;
2433 }
2434 }
2435
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002436 if (!defsrv)
2437 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2438 if (err_code & ERR_FATAL)
2439 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002440 if (srv_tmpl)
2441 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002442 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002443 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002444 return 0;
2445
2446 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002447 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002448 free(errmsg);
2449 return err_code;
2450}
2451
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002452/* Returns a pointer to the first server matching either id <id>.
2453 * NULL is returned if no match is found.
2454 * the lookup is performed in the backend <bk>
2455 */
2456struct server *server_find_by_id(struct proxy *bk, int id)
2457{
2458 struct eb32_node *eb32;
2459 struct server *curserver;
2460
2461 if (!bk || (id ==0))
2462 return NULL;
2463
2464 /* <bk> has no backend capabilities, so it can't have a server */
2465 if (!(bk->cap & PR_CAP_BE))
2466 return NULL;
2467
2468 curserver = NULL;
2469
2470 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2471 if (eb32)
2472 curserver = container_of(eb32, struct server, conf.id);
2473
2474 return curserver;
2475}
2476
2477/* Returns a pointer to the first server matching either name <name>, or id
2478 * if <name> starts with a '#'. NULL is returned if no match is found.
2479 * the lookup is performed in the backend <bk>
2480 */
2481struct server *server_find_by_name(struct proxy *bk, const char *name)
2482{
2483 struct server *curserver;
2484
2485 if (!bk || !name)
2486 return NULL;
2487
2488 /* <bk> has no backend capabilities, so it can't have a server */
2489 if (!(bk->cap & PR_CAP_BE))
2490 return NULL;
2491
2492 curserver = NULL;
2493 if (*name == '#') {
2494 curserver = server_find_by_id(bk, atoi(name + 1));
2495 if (curserver)
2496 return curserver;
2497 }
2498 else {
2499 curserver = bk->srv;
2500
2501 while (curserver && (strcmp(curserver->id, name) != 0))
2502 curserver = curserver->next;
2503
2504 if (curserver)
2505 return curserver;
2506 }
2507
2508 return NULL;
2509}
2510
2511struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2512{
2513 struct server *byname;
2514 struct server *byid;
2515
2516 if (!name && !id)
2517 return NULL;
2518
2519 if (diff)
2520 *diff = 0;
2521
2522 byname = byid = NULL;
2523
2524 if (name) {
2525 byname = server_find_by_name(bk, name);
2526 if (byname && (!id || byname->puid == id))
2527 return byname;
2528 }
2529
2530 /* remaining possibilities :
2531 * - name not set
2532 * - name set but not found
2533 * - name found but ID doesn't match
2534 */
2535 if (id) {
2536 byid = server_find_by_id(bk, id);
2537 if (byid) {
2538 if (byname) {
2539 /* use id only if forced by configuration */
2540 if (byid->flags & SRV_F_FORCED_ID) {
2541 if (diff)
2542 *diff |= 2;
2543 return byid;
2544 }
2545 else {
2546 if (diff)
2547 *diff |= 1;
2548 return byname;
2549 }
2550 }
2551
2552 /* remaining possibilities:
2553 * - name not set
2554 * - name set but not found
2555 */
2556 if (name && diff)
2557 *diff |= 2;
2558 return byid;
2559 }
2560
2561 /* id bot found */
2562 if (byname) {
2563 if (diff)
2564 *diff |= 1;
2565 return byname;
2566 }
2567 }
2568
2569 return NULL;
2570}
2571
Willy Tarreau46b7f532018-08-21 11:54:26 +02002572/* Update a server state using the parameters available in the params list.
2573 *
2574 * Grabs the server lock during operation.
2575 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002576static void srv_update_state(struct server *srv, int version, char **params)
2577{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002578 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002579 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002580
2581 /* fields since version 1
2582 * and common to all other upcoming versions
2583 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002584 enum srv_state srv_op_state;
2585 enum srv_admin srv_admin_state;
2586 unsigned srv_uweight, srv_iweight;
2587 unsigned long srv_last_time_change;
2588 short srv_check_status;
2589 enum chk_result srv_check_result;
2590 int srv_check_health;
2591 int srv_check_state, srv_agent_state;
2592 int bk_f_forced_id;
2593 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002594 int fqdn_set_by_cli;
2595 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002596 const char *port_str;
2597 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002598 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002599
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002600 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002601 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002602 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002603 switch (version) {
2604 case 1:
2605 /*
2606 * now we can proceed with server's state update:
2607 * srv_addr: params[0]
2608 * srv_op_state: params[1]
2609 * srv_admin_state: params[2]
2610 * srv_uweight: params[3]
2611 * srv_iweight: params[4]
2612 * srv_last_time_change: params[5]
2613 * srv_check_status: params[6]
2614 * srv_check_result: params[7]
2615 * srv_check_health: params[8]
2616 * srv_check_state: params[9]
2617 * srv_agent_state: params[10]
2618 * bk_f_forced_id: params[11]
2619 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002620 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002621 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002622 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002623 */
2624
2625 /* validating srv_op_state */
2626 p = NULL;
2627 errno = 0;
2628 srv_op_state = strtol(params[1], &p, 10);
2629 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2630 (srv_op_state != SRV_ST_STOPPED &&
2631 srv_op_state != SRV_ST_STARTING &&
2632 srv_op_state != SRV_ST_RUNNING &&
2633 srv_op_state != SRV_ST_STOPPING)) {
2634 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2635 }
2636
2637 /* validating srv_admin_state */
2638 p = NULL;
2639 errno = 0;
2640 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002641 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002642
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002643 /* inherited statuses will be recomputed later.
2644 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2645 */
2646 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002647
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002648 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2649 (srv_admin_state != 0 &&
2650 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002651 srv_admin_state != SRV_ADMF_CMAINT &&
2652 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002653 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002654 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002655 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2656 }
2657
2658 /* validating srv_uweight */
2659 p = NULL;
2660 errno = 0;
2661 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002662 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002663 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2664
2665 /* validating srv_iweight */
2666 p = NULL;
2667 errno = 0;
2668 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002669 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002670 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2671
2672 /* validating srv_last_time_change */
2673 p = NULL;
2674 errno = 0;
2675 srv_last_time_change = strtol(params[5], &p, 10);
2676 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2677 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2678
2679 /* validating srv_check_status */
2680 p = NULL;
2681 errno = 0;
2682 srv_check_status = strtol(params[6], &p, 10);
2683 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2684 (srv_check_status >= HCHK_STATUS_SIZE))
2685 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2686
2687 /* validating srv_check_result */
2688 p = NULL;
2689 errno = 0;
2690 srv_check_result = strtol(params[7], &p, 10);
2691 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2692 (srv_check_result != CHK_RES_UNKNOWN &&
2693 srv_check_result != CHK_RES_NEUTRAL &&
2694 srv_check_result != CHK_RES_FAILED &&
2695 srv_check_result != CHK_RES_PASSED &&
2696 srv_check_result != CHK_RES_CONDPASS)) {
2697 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2698 }
2699
2700 /* validating srv_check_health */
2701 p = NULL;
2702 errno = 0;
2703 srv_check_health = strtol(params[8], &p, 10);
2704 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2705 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2706
2707 /* validating srv_check_state */
2708 p = NULL;
2709 errno = 0;
2710 srv_check_state = strtol(params[9], &p, 10);
2711 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2712 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2713 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2714
2715 /* validating srv_agent_state */
2716 p = NULL;
2717 errno = 0;
2718 srv_agent_state = strtol(params[10], &p, 10);
2719 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2720 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2721 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2722
2723 /* validating bk_f_forced_id */
2724 p = NULL;
2725 errno = 0;
2726 bk_f_forced_id = strtol(params[11], &p, 10);
2727 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2728 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2729
2730 /* validating srv_f_forced_id */
2731 p = NULL;
2732 errno = 0;
2733 srv_f_forced_id = strtol(params[12], &p, 10);
2734 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2735 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2736
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002737 /* validating srv_fqdn */
2738 fqdn = params[13];
2739 if (fqdn && *fqdn == '-')
2740 fqdn = NULL;
2741 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2742 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2743 fqdn = NULL;
2744 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002745
Frédéric Lécaille31694712017-08-01 08:47:19 +02002746 port_str = params[14];
2747 if (port_str) {
2748 port = strl2uic(port_str, strlen(port_str));
2749 if (port > USHRT_MAX) {
2750 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2751 port_str = NULL;
2752 }
2753 }
2754
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002755 /* SRV record
2756 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2757 */
2758 srvrecord = params[15];
2759 if (srvrecord && *srvrecord != '_')
2760 srvrecord = NULL;
2761
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002762 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002763 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002764 goto out;
2765
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002766 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002767 /* recover operational state and apply it to this server
2768 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002769 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002770 switch (srv_op_state) {
2771 case SRV_ST_STOPPED:
2772 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002773 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002774 break;
2775 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002776 /* If rise == 1 there is no STARTING state, let's switch to
2777 * RUNNING
2778 */
2779 if (srv->check.rise == 1) {
2780 srv->check.health = srv->check.rise + srv->check.fall - 1;
2781 srv_set_running(srv, "", NULL);
2782 break;
2783 }
2784 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2785 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002786 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787 break;
2788 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002789 /* If fall == 1 there is no STOPPING state, let's switch to
2790 * STOPPED
2791 */
2792 if (srv->check.fall == 1) {
2793 srv->check.health = 0;
2794 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2795 break;
2796 }
2797 if (srv->check.health < srv->check.rise ||
2798 srv->check.health > srv->check.rise + srv->check.fall - 2)
2799 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002800 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002801 break;
2802 case SRV_ST_RUNNING:
2803 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002804 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002805 break;
2806 }
2807
2808 /* When applying server state, the following rules apply:
2809 * - in case of a configuration change, we apply the setting from the new
2810 * configuration, regardless of old running state
2811 * - if no configuration change, we apply old running state only if old running
2812 * state is different from new configuration state
2813 */
2814 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002815 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2816 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002817 srv_adm_set_maint(srv);
2818 else
2819 srv_adm_set_ready(srv);
2820 }
2821 /* configuration is the same, let's compate old running state and new conf state */
2822 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002823 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002824 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002825 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002826 srv_adm_set_ready(srv);
2827 }
2828 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002829 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002830 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002831 * (srv->iweight is the weight set up in configuration).
2832 * There are two possible reasons for FDRAIN to have been present :
2833 * - previous config weight was zero
2834 * - "set server b/s drain" was sent to the CLI
2835 *
2836 * In the first case, we simply want to drop this drain state
2837 * if the new weight is not zero anymore, meaning the administrator
2838 * has intentionally turned the weight back to a positive value to
2839 * enable the server again after an operation. In the second case,
2840 * the drain state was forced on the CLI regardless of the config's
2841 * weight so we don't want a change to the config weight to lose this
2842 * status. What this means is :
2843 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2844 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002845 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002846 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002847 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002848 }
2849
2850 srv->last_change = date.tv_sec - srv_last_time_change;
2851 srv->check.status = srv_check_status;
2852 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002853
2854 /* Only case we want to apply is removing ENABLED flag which could have been
2855 * done by the "disable health" command over the stats socket
2856 */
2857 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2858 (srv_check_state & CHK_ST_CONFIGURED) &&
2859 !(srv_check_state & CHK_ST_ENABLED))
2860 srv->check.state &= ~CHK_ST_ENABLED;
2861
2862 /* Only case we want to apply is removing ENABLED flag which could have been
2863 * done by the "disable agent" command over the stats socket
2864 */
2865 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2866 (srv_agent_state & CHK_ST_CONFIGURED) &&
2867 !(srv_agent_state & CHK_ST_ENABLED))
2868 srv->agent.state &= ~CHK_ST_ENABLED;
2869
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002870 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2871 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002872 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002873 * It means that a configuration file change has precedence over a unix socket change
2874 * for server's weight
2875 *
2876 * by default, HAProxy applies the following weight when parsing the configuration
2877 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002879 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002880 srv->uweight = srv_uweight;
2881 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002882 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002883
Willy Tarreaue5a60682016-11-09 14:54:53 +01002884 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002885 if (strcmp(params[0], "-"))
2886 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002887
2888 if (fqdn && srv->hostname) {
2889 if (!strcmp(srv->hostname, fqdn)) {
2890 /* Here we reset the 'set from stats socket FQDN' flag
2891 * to support such transitions:
2892 * Let's say initial FQDN value is foo1 (in configuration file).
2893 * - FQDN changed from stats socket, from foo1 to foo2 value,
2894 * - FQDN changed again from file configuration (with the same previous value
2895 set from stats socket, from foo1 to foo2 value),
2896 * - reload for any other reason than a FQDN modification,
2897 * the configuration file FQDN matches the fqdn server state file value.
2898 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002899 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002900 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002901 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002902 }
2903 else {
2904 /* If the FDQN has been changed from stats socket,
2905 * apply fqdn state file value (which is the value set
2906 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002907 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002908 */
2909 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002910 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002911 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002912 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002913 }
2914 }
2915 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002916 /* If all the conditions below are validated, this means
2917 * we're evaluating a server managed by SRV resolution
2918 */
2919 else if (fqdn && !srv->hostname && srvrecord) {
2920 int res;
2921
2922 /* we can't apply previous state if SRV record has changed */
2923 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2924 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);
2925 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2926 goto out;
2927 }
2928
2929 /* create or find a SRV resolution for this srv record */
2930 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2931 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2932 if (srv->srvrq == NULL) {
2933 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2934 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2935 goto out;
2936 }
2937
2938 /* prepare DNS resolution for this server */
2939 res = srv_prepare_for_resolution(srv, fqdn);
2940 if (res == -1) {
2941 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2942 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2943 goto out;
2944 }
2945
2946 /* configure check.port accordingly */
2947 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2948 !(srv->flags & SRV_F_CHECKPORT))
2949 srv->check.port = port;
2950
2951 /* Unset SRV_F_MAPPORTS for SRV records.
2952 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2953 * because no ports are provided in the configuration file.
2954 * This is because HAProxy will use the port found into the SRV record.
2955 */
2956 srv->flags &= ~SRV_F_MAPPORTS;
2957 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002958
Frédéric Lécaille31694712017-08-01 08:47:19 +02002959 if (port_str)
2960 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002961 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002962
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002963 break;
2964 default:
2965 chunk_appendf(msg, ", version '%d' not supported", version);
2966 }
2967
2968 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002969 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002970 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002971 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002972 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002973 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002974}
2975
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002976
2977/*
2978 * read next line from file <f> and return the server state version if one found.
2979 * If no version is found, then 0 is returned
2980 * Note that this should be the first read on <f>
2981 */
2982static int srv_state_get_version(FILE *f) {
2983 char buf[2];
2984 int ret;
2985
2986 /* first character of first line of the file must contain the version of the export */
2987 if (fgets(buf, 2, f) == NULL) {
2988 return 0;
2989 }
2990
2991 ret = atoi(buf);
2992 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
2993 (ret > SRV_STATE_FILE_VERSION_MAX))
2994 return 0;
2995
2996 return ret;
2997}
2998
2999
3000/*
3001 * parses server state line stored in <buf> and supposedly in version <version>.
3002 * Set <params> and <srv_params> accordingly.
3003 * In case of error, params[0] is set to NULL.
3004 */
3005static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3006{
3007 int buflen, arg, srv_arg;
3008 char *cur, *end;
3009
3010 buflen = strlen(buf);
3011 cur = buf;
3012 end = cur + buflen;
3013
3014 /* we need at least one character */
3015 if (buflen == 0) {
3016 params[0] = NULL;
3017 return;
3018 }
3019
3020 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003021 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003022 ++cur;
3023
3024 /* Ignore empty or commented lines */
3025 if (cur == end || *cur == '#') {
3026 params[0] = NULL;
3027 return;
3028 }
3029
3030 /* truncated lines */
3031 if (buf[buflen - 1] != '\n') {
3032 //ha_warning("server-state file '%s': truncated line\n", filepath);
3033 params[0] = NULL;
3034 return;
3035 }
3036
3037 /* Removes trailing '\n' */
3038 buf[buflen - 1] = '\0';
3039
3040 /* we're now ready to move the line into *srv_params[] */
3041 params[0] = cur;
3042 arg = 1;
3043 srv_arg = 0;
3044 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003045 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003046 *cur = '\0';
3047 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003048 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003049 ++cur;
3050 switch (version) {
3051 case 1:
3052 /*
3053 * srv_addr: params[4] => srv_params[0]
3054 * srv_op_state: params[5] => srv_params[1]
3055 * srv_admin_state: params[6] => srv_params[2]
3056 * srv_uweight: params[7] => srv_params[3]
3057 * srv_iweight: params[8] => srv_params[4]
3058 * srv_last_time_change: params[9] => srv_params[5]
3059 * srv_check_status: params[10] => srv_params[6]
3060 * srv_check_result: params[11] => srv_params[7]
3061 * srv_check_health: params[12] => srv_params[8]
3062 * srv_check_state: params[13] => srv_params[9]
3063 * srv_agent_state: params[14] => srv_params[10]
3064 * bk_f_forced_id: params[15] => srv_params[11]
3065 * srv_f_forced_id: params[16] => srv_params[12]
3066 * srv_fqdn: params[17] => srv_params[13]
3067 * srv_port: params[18] => srv_params[14]
3068 * srvrecord: params[19] => srv_params[15]
3069 */
3070 if (arg >= 4) {
3071 srv_params[srv_arg] = cur;
3072 ++srv_arg;
3073 }
3074 break;
3075 }
3076
3077 params[arg] = cur;
3078 ++arg;
3079 }
3080 else {
3081 ++cur;
3082 }
3083 }
3084
3085 /* if line is incomplete line, then ignore it.
3086 * otherwise, update useful flags */
3087 switch (version) {
3088 case 1:
3089 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3090 params[0] = NULL;
3091 return;
3092 }
3093 break;
3094 }
3095
3096 return;
3097}
3098
3099
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003100/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3101 * For each proxy, it does the following:
3102 * - opens its server state file (either one or local one)
3103 * - read whole file, line by line
3104 * - analyse each line to check if it matches our current backend:
3105 * - backend name matches
3106 * - backend id matches if id is forced and name doesn't match
3107 * - if the server pointed by the line is found, then state is applied
3108 *
3109 * If the running backend uuid or id differs from the state file, then HAProxy reports
3110 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003111 *
3112 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003113 */
3114void apply_server_state(void)
3115{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003116 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003117 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3118 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003119 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003120 FILE *f;
3121 char *filepath;
3122 char globalfilepath[MAXPATHLEN + 1];
3123 char localfilepath[MAXPATHLEN + 1];
3124 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003125 struct proxy *curproxy, *bk;
3126 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003127 char *line;
3128 char *bkname, *srvname;
3129 struct state_line *st;
3130 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003131
Willy Tarreau2d067f92020-07-16 06:40:27 +02003132 f = NULL;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003133 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003134 globalfilepathlen = 0;
3135 /* create the globalfilepath variable */
3136 if (global.server_state_file) {
3137 /* absolute path or no base directory provided */
3138 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3139 len = strlen(global.server_state_file);
3140 if (len > MAXPATHLEN) {
3141 globalfilepathlen = 0;
3142 goto globalfileerror;
3143 }
3144 memcpy(globalfilepath, global.server_state_file, len);
3145 globalfilepath[len] = '\0';
3146 globalfilepathlen = len;
3147 }
3148 else if (global.server_state_base) {
3149 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003150 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003151 globalfilepathlen = 0;
3152 goto globalfileerror;
3153 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003154 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003155 globalfilepath[len] = 0;
3156 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003157
3158 /* append a slash if needed */
3159 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3160 if (globalfilepathlen + 1 > MAXPATHLEN) {
3161 globalfilepathlen = 0;
3162 goto globalfileerror;
3163 }
3164 globalfilepath[globalfilepathlen++] = '/';
3165 }
3166
3167 len = strlen(global.server_state_file);
3168 if (globalfilepathlen + len > MAXPATHLEN) {
3169 globalfilepathlen = 0;
3170 goto globalfileerror;
3171 }
3172 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3173 globalfilepathlen += len;
3174 globalfilepath[globalfilepathlen++] = 0;
3175 }
3176 }
3177 globalfileerror:
3178 if (globalfilepathlen == 0)
3179 globalfilepath[0] = '\0';
3180
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003181 /* Load global server state in a tree */
3182 if (globalfilepathlen > 0) {
3183 errno = 0;
3184 f = fopen(globalfilepath, "r");
3185 if (errno)
3186 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003187 if (!f)
3188 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003189
3190 global_file_version = srv_state_get_version(f);
3191 if (global_file_version == 0)
3192 goto out_load_server_state_in_tree;
3193
3194 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3195 line = NULL;
3196
3197 line = strdup(mybuf);
3198 if (line == NULL)
3199 continue;
3200
3201 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3202 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003203 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003204
3205 /* bkname */
3206 bkname = params[1];
3207 /* srvname */
3208 srvname = params[3];
3209
3210 /* key */
3211 chunk_printf(&trash, "%s %s", bkname, srvname);
3212
3213 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003214 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003215 if (st == NULL) {
3216 goto nextline;
3217 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003218 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003219 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3220 /* this is a duplicate key, probably a hand-crafted file,
3221 * drop it!
3222 */
3223 goto nextline;
3224 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003225
3226 /* save line */
3227 st->line = line;
3228
3229 continue;
3230
3231 nextline:
3232 /* free up memory in case of error during the processing of the line */
3233 free(line);
3234 }
3235 }
3236 out_load_server_state_in_tree:
3237
Ilya Shipitsindc6e8a92020-07-16 02:02:40 +05003238 if (f) {
3239 fclose(f);
3240 f = NULL;
3241 }
3242
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003243 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003244 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003245 /* servers are only in backends */
3246 if (!(curproxy->cap & PR_CAP_BE))
3247 continue;
3248 fileopenerr = 0;
3249 filepath = NULL;
3250
3251 /* search server state file path and name */
3252 switch (curproxy->load_server_state_from_file) {
3253 /* read servers state from global file */
3254 case PR_SRV_STATE_FILE_GLOBAL:
3255 /* there was an error while generating global server state file path */
3256 if (globalfilepathlen == 0)
3257 continue;
3258 filepath = globalfilepath;
3259 fileopenerr = 1;
3260 break;
3261 /* this backend has its own file */
3262 case PR_SRV_STATE_FILE_LOCAL:
3263 localfilepathlen = 0;
3264 localfilepath[0] = '\0';
3265 len = 0;
3266 /* create the localfilepath variable */
3267 /* absolute path or no base directory provided */
3268 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3269 len = strlen(curproxy->server_state_file_name);
3270 if (len > MAXPATHLEN) {
3271 localfilepathlen = 0;
3272 goto localfileerror;
3273 }
3274 memcpy(localfilepath, curproxy->server_state_file_name, len);
3275 localfilepath[len] = '\0';
3276 localfilepathlen = len;
3277 }
3278 else if (global.server_state_base) {
3279 len = strlen(global.server_state_base);
3280 localfilepathlen += len;
3281
3282 if (localfilepathlen > MAXPATHLEN) {
3283 localfilepathlen = 0;
3284 goto localfileerror;
3285 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003286 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003287 localfilepath[localfilepathlen] = 0;
3288
3289 /* append a slash if needed */
3290 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3291 if (localfilepathlen + 1 > MAXPATHLEN) {
3292 localfilepathlen = 0;
3293 goto localfileerror;
3294 }
3295 localfilepath[localfilepathlen++] = '/';
3296 }
3297
3298 len = strlen(curproxy->server_state_file_name);
3299 if (localfilepathlen + len > MAXPATHLEN) {
3300 localfilepathlen = 0;
3301 goto localfileerror;
3302 }
3303 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3304 localfilepathlen += len;
3305 localfilepath[localfilepathlen++] = 0;
3306 }
3307 filepath = localfilepath;
3308 localfileerror:
3309 if (localfilepathlen == 0)
3310 localfilepath[0] = '\0';
3311
3312 break;
3313 case PR_SRV_STATE_FILE_NONE:
3314 default:
3315 continue;
3316 }
3317
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003318 /* when global file is used, we get data from the tree
3319 * Note that in such case we don't check backend name neither uuid.
3320 * Backend name can't be wrong since it's used as a key to retrieve the server state
3321 * line from the tree.
3322 */
3323 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3324 struct server *srv = curproxy->srv;
3325 while (srv) {
3326 struct ebmb_node *node;
3327 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003328
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003329 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3330 node = ebst_lookup(&state_file, trash.area);
3331 if (!node)
3332 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003333
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003334 st = container_of(node, struct state_line, name_name);
3335 memcpy(mybuf, st->line, strlen(st->line));
3336 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003337
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003338 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3339 if (params[0] == NULL)
3340 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003341
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003342 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003343
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003344 next:
3345 srv = srv->next;
3346 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003347
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003348 continue; /* next proxy in list */
3349 }
3350 else {
3351 /* load 'local' state file */
3352 errno = 0;
3353 f = fopen(filepath, "r");
3354 if (errno && fileopenerr)
3355 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3356 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003357 continue;
3358
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003359 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003360
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003361 /* first character of first line of the file must contain the version of the export */
3362 version = srv_state_get_version(f);
3363 if (version == 0) {
3364 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3365 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003366 }
3367
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003368 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3369 int bk_f_forced_id = 0;
3370 int check_id = 0;
3371 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003372
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003373 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003374
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003375 if (params[0] == NULL) {
3376 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003377 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003378
3379 /* if line is incomplete line, then ignore it.
3380 * otherwise, update useful flags */
3381 switch (version) {
3382 case 1:
3383 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3384 check_id = (atoi(params[0]) == curproxy->uuid);
3385 check_name = (strcmp(curproxy->id, params[1]) == 0);
3386 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003387 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003388
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003389 bk = curproxy;
3390
3391 /* if backend can't be found, let's continue */
3392 if (!check_id && !check_name)
3393 continue;
3394 else if (!check_id && check_name) {
3395 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3396 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3397 }
3398 else if (check_id && !check_name) {
3399 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3400 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3401 /* if name doesn't match, we still want to update curproxy if the backend id
3402 * was forced in previous the previous configuration */
3403 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003404 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003405 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003406
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003407 /* look for the server by its name: param[3] */
3408 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003409
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003410 if (!srv) {
3411 /* if no server found, then warning and continue with next line */
3412 ha_warning("can't find server '%s' in backend '%s'\n",
3413 params[3], params[1]);
3414 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3415 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003416 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003417 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003418
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003419 /* now we can proceed with server's state update */
3420 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003421 }
Ilya Shipitsindc6e8a92020-07-16 02:02:40 +05003422
3423 fileclose:
3424 fclose(f);
3425
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003426 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003427 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003428
3429 /* now free memory allocated for the tree */
3430 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3431 node;
3432 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3433 st = container_of(node, struct state_line, name_name);
3434 ebmb_delete(&st->name_name);
3435 free(st->line);
3436 free(st);
3437 }
3438
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003439}
3440
Simon Horman7d09b9a2013-02-12 10:45:51 +09003441/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003442 * update a server's current IP address.
3443 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3444 * ip is in network format.
3445 * updater is a string which contains an information about the requester of the update.
3446 * updater is used if not NULL.
3447 *
3448 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003449 *
3450 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003451 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003452int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003453{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003454 /* save the new IP family & address if necessary */
3455 switch (ip_sin_family) {
3456 case AF_INET:
3457 if (s->addr.ss_family == ip_sin_family &&
3458 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3459 return 0;
3460 break;
3461 case AF_INET6:
3462 if (s->addr.ss_family == ip_sin_family &&
3463 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3464 return 0;
3465 break;
3466 };
3467
Baptiste Assmann14e40142015-04-14 01:13:07 +02003468 /* 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;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003485 default:
3486 strcpy(oldip, "(none)");
3487 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003488 };
3489
3490 /* copy new IP address in a string */
3491 switch (ip_sin_family) {
3492 case AF_INET:
3493 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3494 break;
3495 case AF_INET6:
3496 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3497 break;
3498 };
3499
3500 /* save log line into a buffer */
3501 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3502 s->proxy->id, s->id, oldip, newip, updater);
3503
3504 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003505 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003506
3507 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003508 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003509 }
3510
3511 /* save the new IP family */
3512 s->addr.ss_family = ip_sin_family;
3513 /* save the new IP address */
3514 switch (ip_sin_family) {
3515 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003516 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003517 break;
3518 case AF_INET6:
3519 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3520 break;
3521 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003522 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003523
3524 return 0;
3525}
3526
3527/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003528 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3529 *
3530 * Caller can pass its name through <updater> to get it integrated in the response message
3531 * returned by the function.
3532 *
3533 * The function first does the following, in that order:
3534 * - validates the new addr and/or port
3535 * - checks if an update is required (new IP or port is different than current ones)
3536 * - checks the update is allowed:
3537 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3538 * - allow all changes if no CHECKS are configured
3539 * - if CHECK is configured:
3540 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3541 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3542 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003543 *
3544 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003545 */
3546const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3547{
3548 struct sockaddr_storage sa;
3549 int ret, port_change_required;
3550 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003551 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003552 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003553 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003554
3555 msg = get_trash_chunk();
3556 chunk_reset(msg);
3557
3558 if (addr) {
3559 memset(&sa, 0, sizeof(struct sockaddr_storage));
3560 if (str2ip2(addr, &sa, 0) == NULL) {
3561 chunk_printf(msg, "Invalid addr '%s'", addr);
3562 goto out;
3563 }
3564
3565 /* changes are allowed on AF_INET* families only */
3566 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3567 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3568 goto out;
3569 }
3570
3571 /* collecting data currently setup */
3572 memset(current_addr, '\0', sizeof(current_addr));
3573 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3574 /* changes are allowed on AF_INET* families only */
3575 if ((ret != AF_INET) && (ret != AF_INET6)) {
3576 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3577 goto out;
3578 }
3579
3580 /* applying ADDR changes if required and allowed
3581 * ipcmp returns 0 when both ADDR are the same
3582 */
3583 if (ipcmp(&s->addr, &sa) == 0) {
3584 chunk_appendf(msg, "no need to change the addr");
3585 goto port;
3586 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003587 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003588 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003589
3590 /* we also need to update check's ADDR only if it uses the server's one */
3591 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003592 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003593 }
3594
3595 /* we also need to update agent ADDR only if it use the server's one */
3596 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003597 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003598 }
3599
3600 /* update report for caller */
3601 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3602 }
3603
3604 port:
3605 if (port) {
3606 char sign = '\0';
3607 char *endptr;
3608
3609 if (addr)
3610 chunk_appendf(msg, ", ");
3611
3612 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003613 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003614
3615 /* check if PORT change is required */
3616 port_change_required = 0;
3617
3618 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003619 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003620 new_port = strtol(port, &endptr, 10);
3621 if ((errno != 0) || (port == endptr)) {
3622 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3623 goto out;
3624 }
3625
3626 /* check if caller triggers a port mapped or offset */
3627 if (sign == '-' || (sign == '+')) {
3628 /* check if server currently uses port map */
3629 if (!(s->flags & SRV_F_MAPPORTS)) {
3630 /* switch from fixed port to port map mandatorily triggers
3631 * a port change */
3632 port_change_required = 1;
3633 /* check is configured
3634 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3635 * prevent PORT change if check doesn't have it's dedicated port while switching
3636 * to port mapping */
3637 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3638 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.");
3639 goto out;
3640 }
3641 }
3642 /* we're already using port maps */
3643 else {
3644 port_change_required = current_port != new_port;
3645 }
3646 }
3647 /* fixed port */
3648 else {
3649 port_change_required = current_port != new_port;
3650 }
3651
3652 /* applying PORT changes if required and update response message */
3653 if (port_change_required) {
3654 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003655 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003656 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003657
3658 /* prepare message */
3659 chunk_appendf(msg, "port changed from '");
3660 if (s->flags & SRV_F_MAPPORTS)
3661 chunk_appendf(msg, "+");
3662 chunk_appendf(msg, "%d' to '", current_port);
3663
3664 if (sign == '-') {
3665 s->flags |= SRV_F_MAPPORTS;
3666 chunk_appendf(msg, "%c", sign);
3667 /* just use for result output */
3668 new_port = -new_port;
3669 }
3670 else if (sign == '+') {
3671 s->flags |= SRV_F_MAPPORTS;
3672 chunk_appendf(msg, "%c", sign);
3673 }
3674 else {
3675 s->flags &= ~SRV_F_MAPPORTS;
3676 }
3677
3678 chunk_appendf(msg, "%d'", new_port);
3679
3680 /* we also need to update health checks port only if it uses server's realport */
3681 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3682 s->check.port = new_port;
3683 }
3684 }
3685 else {
3686 chunk_appendf(msg, "no need to change the port");
3687 }
3688 }
3689
3690out:
William Dauchy6318d332020-05-02 21:52:36 +02003691 if (changed) {
3692 /* force connection cleanup on the given server */
3693 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003694 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003695 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003696 if (updater)
3697 chunk_appendf(msg, " by '%s'", updater);
3698 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003699 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003700}
3701
3702
3703/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003704 * update server status based on result of name resolution
3705 * returns:
3706 * 0 if server status is updated
3707 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003708 *
3709 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003710 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003711int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003712{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003713 struct dns_resolvers *resolvers = s->resolvers;
3714 struct dns_resolution *resolution = s->dns_requester->resolution;
3715 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716
Jerome Magnin012261a2020-07-28 13:38:22 +02003717 /* If resolution is NULL we're dealing with SRV records Additional records */
3718 if (resolution == NULL) {
Baptiste Assmann87138c32020-08-04 10:57:21 +02003719 /* since this server has an IP, it can go back in production */
3720 if (has_no_ip == 0) {
3721 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3722 return 1;
3723 }
3724
Jerome Magnin012261a2020-07-28 13:38:22 +02003725 if (s->next_admin & SRV_ADMF_RMAINT)
3726 return 1;
3727
3728 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3729 return 0;
3730 }
3731
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003732 switch (resolution->status) {
3733 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003734 /* status when HAProxy has just (re)started.
3735 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003736 break;
3737
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003738 case RSLV_STATUS_VALID:
3739 /*
3740 * resume health checks
3741 * server will be turned back on if health check is safe
3742 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003743 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003744 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003745 return 1;
3746 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3747 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003748 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003749 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003750
Emeric Brun52a91d32017-08-31 14:41:55 +02003751 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003752 return 1;
3753 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3754 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3755 s->proxy->id, s->id);
3756
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003757 ha_warning("%s.\n", trash.area);
3758 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003759 return 0;
3760
3761 case RSLV_STATUS_NX:
3762 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003763 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3764 if (!tick_is_expired(exp, now_ms))
3765 break;
3766
3767 if (s->next_admin & SRV_ADMF_RMAINT)
3768 return 1;
3769 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3770 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003771
3772 case RSLV_STATUS_TIMEOUT:
3773 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003774 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3775 if (!tick_is_expired(exp, now_ms))
3776 break;
3777
3778 if (s->next_admin & SRV_ADMF_RMAINT)
3779 return 1;
3780 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3781 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003782
3783 case RSLV_STATUS_REFUSED:
3784 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003785 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3786 if (!tick_is_expired(exp, now_ms))
3787 break;
3788
3789 if (s->next_admin & SRV_ADMF_RMAINT)
3790 return 1;
3791 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3792 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003793
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003794 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003795 /* stop server if resolution failed for a long enough period */
3796 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3797 if (!tick_is_expired(exp, now_ms))
3798 break;
3799
3800 if (s->next_admin & SRV_ADMF_RMAINT)
3801 return 1;
3802 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3803 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003804 }
3805
3806 return 1;
3807}
3808
3809/*
3810 * Server Name Resolution valid response callback
3811 * It expects:
3812 * - <nameserver>: the name server which answered the valid response
3813 * - <response>: buffer containing a valid DNS response
3814 * - <response_len>: size of <response>
3815 * It performs the following actions:
3816 * - ignore response if current ip found and server family not met
3817 * - update with first new ip found if family is met and current IP is not found
3818 * returns:
3819 * 0 on error
3820 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003821 *
3822 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003823 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003824int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003825{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003826 struct server *s = NULL;
3827 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003828 void *serverip, *firstip;
3829 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003830 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003831 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003832 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003833
Christopher Faulet67957bd2017-09-27 11:00:59 +02003834 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003835 if (!s)
3836 return 1;
3837
Christopher Faulet67957bd2017-09-27 11:00:59 +02003838 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003839
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003840 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003841 firstip = NULL; /* pointer to the first valid response found */
3842 /* it will be used as the new IP if a change is required */
3843 firstip_sin_family = AF_UNSPEC;
3844 serverip = NULL; /* current server IP address */
3845
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003846 /* initializing server IP pointer */
3847 server_sin_family = s->addr.ss_family;
3848 switch (server_sin_family) {
3849 case AF_INET:
3850 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3851 break;
3852
3853 case AF_INET6:
3854 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3855 break;
3856
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003857 case AF_UNSPEC:
3858 break;
3859
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003860 default:
3861 goto invalid;
3862 }
3863
Baptiste Assmann729c9012017-05-22 15:13:10 +02003864 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003865 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003866 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003867
3868 switch (ret) {
3869 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003870 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003871
3872 case DNS_UPD_SRVIP_NOT_FOUND:
3873 goto save_ip;
3874
3875 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003876 goto invalid;
3877
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003878 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003879 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003880 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003881
Baptiste Assmannfad03182015-10-28 02:03:32 +01003882 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003883 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003884 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003886
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003887 default:
3888 goto invalid;
3889
3890 }
3891
3892 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003893 if (nameserver) {
3894 nameserver->counters.update++;
3895 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003896 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003897 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003898 else
3899 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003901
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003902 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003903 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003904 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003905
3906 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003907 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003908 nameserver->counters.invalid++;
3909 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003910 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003911 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003912 return 0;
3913}
3914
3915/*
3916 * Server Name Resolution error management callback
3917 * returns:
3918 * 0 on error
3919 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003920 *
3921 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003922 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003923int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003924{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003925 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926
Christopher Faulet67957bd2017-09-27 11:00:59 +02003927 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003928 if (!s)
3929 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003930 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003931 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003932 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003933 return 1;
3934}
3935
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003936/*
3937 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003938 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003939 * It returns a pointer to the first server found or NULL if <ip> is not yet
3940 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003941 *
3942 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003943 */
3944struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3945{
3946 struct server *tmpsrv;
3947 struct proxy *be;
3948
3949 if (!srv)
3950 return NULL;
3951
3952 be = srv->proxy;
3953 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003954 /* we found the current server is the same, ignore it */
3955 if (srv == tmpsrv)
3956 continue;
3957
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003958 /* We want to compare the IP in the record with the IP of the servers in the
3959 * same backend, only if:
3960 * * DNS resolution is enabled on the server
3961 * * the hostname used for the resolution by our server is the same than the
3962 * one used for the server found in the backend
3963 * * the server found in the backend is not our current server
3964 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003965 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003966 if ((tmpsrv->hostname_dn == NULL) ||
3967 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3968 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003969 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003970 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003971 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003972 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003973
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003974 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003975 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003976 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003977 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003978 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003979
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003980 /* At this point, we have 2 different servers using the same DNS hostname
3981 * for their respective resolution.
3982 */
3983 if (*ip_family == tmpsrv->addr.ss_family &&
3984 ((tmpsrv->addr.ss_family == AF_INET &&
3985 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3986 (tmpsrv->addr.ss_family == AF_INET6 &&
3987 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003988 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003989 return tmpsrv;
3990 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003991 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003992 }
3993
Emeric Brune9fd6b52017-11-02 17:20:39 +01003994
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003995 return NULL;
3996}
3997
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003998/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3999 * resolver. This is suited for initial address configuration. Returns 0 on
4000 * success otherwise a non-zero error code. In case of error, *err_code, if
4001 * not NULL, is filled up.
4002 */
4003int srv_set_addr_via_libc(struct server *srv, int *err_code)
4004{
4005 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004006 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004007 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004008 return 1;
4009 }
4010 return 0;
4011}
4012
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004013/* Set the server's FDQN (->hostname) from <hostname>.
4014 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004015 *
4016 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004017 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004018int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004019{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004020 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004021 char *hostname_dn;
4022 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004023
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004024 /* Note that the server lock is already held. */
4025 if (!srv->resolvers)
4026 return -1;
4027
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004028 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004029 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004030 /* run time DNS resolution was not active for this server
4031 * and we can't enable it at run time for now.
4032 */
4033 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004034 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004035
4036 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004037 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004038 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004039 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4040 hostname_dn, trash.size);
4041 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004042 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004043
Christopher Faulet67957bd2017-09-27 11:00:59 +02004044 resolution = srv->dns_requester->resolution;
4045 if (resolution &&
4046 resolution->hostname_dn &&
4047 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004048 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004049
Christopher Faulet67957bd2017-09-27 11:00:59 +02004050 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004051
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004052 free(srv->hostname);
4053 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004054 srv->hostname = strdup(hostname);
4055 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004056 srv->hostname_dn_len = hostname_dn_len;
4057 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004058 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004059
Baptiste Assmann13a92322019-06-07 09:40:55 +02004060 if (srv->flags & SRV_F_NO_RESOLUTION)
4061 goto end;
4062
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004063 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004064 goto err;
4065
4066 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004067 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004068 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004069 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004070
4071 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004072 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004073 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004074 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004075}
4076
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004077/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4078 * from the state file. This is suited for initial address configuration.
4079 * Returns 0 on success otherwise a non-zero error code. In case of error,
4080 * *err_code, if not NULL, is filled up.
4081 */
4082static int srv_apply_lastaddr(struct server *srv, int *err_code)
4083{
4084 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4085 if (err_code)
4086 *err_code |= ERR_WARN;
4087 return 1;
4088 }
4089 return 0;
4090}
4091
Willy Tarreau25e51522016-11-04 15:10:17 +01004092/* returns 0 if no error, otherwise a combination of ERR_* flags */
4093static int srv_iterate_initaddr(struct server *srv)
4094{
4095 int return_code = 0;
4096 int err_code;
4097 unsigned int methods;
4098
4099 methods = srv->init_addr_methods;
4100 if (!methods) { // default to "last,libc"
4101 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4102 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4103 }
4104
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004105 /* "-dr" : always append "none" so that server addresses resolution
4106 * failures are silently ignored, this is convenient to validate some
4107 * configs out of their environment.
4108 */
4109 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4110 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4111
Willy Tarreau25e51522016-11-04 15:10:17 +01004112 while (methods) {
4113 err_code = 0;
4114 switch (srv_get_next_initaddr(&methods)) {
4115 case SRV_IADDR_LAST:
4116 if (!srv->lastaddr)
4117 continue;
4118 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004119 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004120 return_code |= err_code;
4121 break;
4122
4123 case SRV_IADDR_LIBC:
4124 if (!srv->hostname)
4125 continue;
4126 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004127 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004128 return_code |= err_code;
4129 break;
4130
Willy Tarreau37ebe122016-11-04 15:17:58 +01004131 case SRV_IADDR_NONE:
4132 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004133 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004134 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4135 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004136 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004137 return return_code;
4138
Willy Tarreau4310d362016-11-02 15:05:56 +01004139 case SRV_IADDR_IP:
4140 ipcpy(&srv->init_addr, &srv->addr);
4141 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004142 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4143 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004144 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004145 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004146
Willy Tarreau25e51522016-11-04 15:10:17 +01004147 default: /* unhandled method */
4148 break;
4149 }
4150 }
4151
4152 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004153 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004154 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4155 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004156 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004157 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004158 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4159 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004160
4161 return_code |= ERR_ALERT | ERR_FATAL;
4162 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004163out:
4164 srv_set_dyncookie(srv);
4165 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004166}
4167
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004168/*
4169 * This function parses all backends and all servers within each backend
4170 * and performs servers' addr resolution based on information provided by:
4171 * - configuration file
4172 * - server-state file (states provided by an 'old' haproxy process)
4173 *
4174 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4175 */
4176int srv_init_addr(void)
4177{
4178 struct proxy *curproxy;
4179 int return_code = 0;
4180
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004181 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004182 while (curproxy) {
4183 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004184
4185 /* servers are in backend only */
4186 if (!(curproxy->cap & PR_CAP_BE))
4187 goto srv_init_addr_next;
4188
Willy Tarreau25e51522016-11-04 15:10:17 +01004189 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004190 if (srv->hostname)
4191 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004192
4193 srv_init_addr_next:
4194 curproxy = curproxy->next;
4195 }
4196
4197 return return_code;
4198}
4199
Willy Tarreau46b7f532018-08-21 11:54:26 +02004200/*
4201 * Must be called with the server lock held.
4202 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004203const 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 +02004204{
4205
Willy Tarreau83061a82018-07-13 11:56:34 +02004206 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004207
4208 msg = get_trash_chunk();
4209 chunk_reset(msg);
4210
Olivier Houchard8da5f982017-08-04 18:35:36 +02004211 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004212 chunk_appendf(msg, "no need to change the FDQN");
4213 goto out;
4214 }
4215
4216 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4217 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4218 goto out;
4219 }
4220
4221 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4222 server->proxy->id, server->id, server->hostname, fqdn);
4223
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004224 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004225 chunk_reset(msg);
4226 chunk_appendf(msg, "could not update %s/%s FQDN",
4227 server->proxy->id, server->id);
4228 goto out;
4229 }
4230
4231 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004232 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004233
4234 out:
4235 if (updater)
4236 chunk_appendf(msg, " by '%s'", updater);
4237 chunk_appendf(msg, "\n");
4238
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004239 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004240}
4241
4242
Willy Tarreau21b069d2016-11-23 17:15:08 +01004243/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4244 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004245 * 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 +01004246 * used for CLI commands requiring a server name.
4247 * Important: the <arg> is modified to remove the '/'.
4248 */
4249struct server *cli_find_server(struct appctx *appctx, char *arg)
4250{
4251 struct proxy *px;
4252 struct server *sv;
4253 char *line;
4254
4255 /* split "backend/server" and make <line> point to server */
4256 for (line = arg; *line; line++)
4257 if (*line == '/') {
4258 *line++ = '\0';
4259 break;
4260 }
4261
4262 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004263 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004264 return NULL;
4265 }
4266
4267 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004268 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004269 return NULL;
4270 }
4271
4272 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004273 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004274 return NULL;
4275 }
4276
4277 return sv;
4278}
4279
William Lallemand222baf22016-11-19 02:00:33 +01004280
Willy Tarreau46b7f532018-08-21 11:54:26 +02004281/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004282static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004283{
4284 struct server *sv;
4285 const char *warning;
4286
4287 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4288 return 1;
4289
4290 sv = cli_find_server(appctx, args[2]);
4291 if (!sv)
4292 return 1;
4293
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004294 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004295
William Lallemand222baf22016-11-19 02:00:33 +01004296 if (strcmp(args[3], "weight") == 0) {
4297 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004298 if (warning)
4299 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004300 }
4301 else if (strcmp(args[3], "state") == 0) {
4302 if (strcmp(args[4], "ready") == 0)
4303 srv_adm_set_ready(sv);
4304 else if (strcmp(args[4], "drain") == 0)
4305 srv_adm_set_drain(sv);
4306 else if (strcmp(args[4], "maint") == 0)
4307 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004308 else
4309 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004310 }
4311 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004312 if (sv->track)
4313 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004314 else if (strcmp(args[4], "up") == 0) {
4315 sv->check.health = sv->check.rise + sv->check.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], "stopping") == 0) {
4319 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004320 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004321 }
4322 else if (strcmp(args[4], "down") == 0) {
4323 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004324 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004325 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004326 else
4327 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004328 }
4329 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004330 if (!(sv->agent.state & CHK_ST_ENABLED))
4331 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004332 else if (strcmp(args[4], "up") == 0) {
4333 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004334 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004335 }
4336 else if (strcmp(args[4], "down") == 0) {
4337 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004338 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004339 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004340 else
4341 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004342 }
Misiek2da082d2017-01-09 09:40:42 +01004343 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004344 if (!(sv->agent.state & CHK_ST_ENABLED))
4345 cli_err(appctx, "agent checks are not enabled on this server.\n");
4346 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4347 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004348 }
4349 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004350 if (!(sv->agent.state & CHK_ST_ENABLED))
4351 cli_err(appctx, "agent checks are not enabled on this server.\n");
4352 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004353 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004354 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004355 }
4356 }
William Lallemand222baf22016-11-19 02:00:33 +01004357 else if (strcmp(args[3], "check-port") == 0) {
4358 int i = 0;
4359 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004360 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004361 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004362 }
4363 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004364 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004365 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004366 }
4367 /* prevent the update of port to 0 if MAPPORTS are in use */
4368 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004369 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004370 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004371 }
4372 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004373 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004374 }
4375 else if (strcmp(args[3], "addr") == 0) {
4376 char *addr = NULL;
4377 char *port = NULL;
4378 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004379 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004380 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004381 }
4382 else {
4383 addr = args[4];
4384 }
4385 if (strcmp(args[5], "port") == 0) {
4386 port = args[6];
4387 }
4388 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004389 if (warning)
4390 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004391 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4392 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004393 else if (strcmp(args[3], "fqdn") == 0) {
4394 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004395 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004396 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004397 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004398 /* ensure runtime resolver will process this new fqdn */
4399 if (sv->flags & SRV_F_NO_RESOLUTION) {
4400 sv->flags &= ~SRV_F_NO_RESOLUTION;
4401 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004402 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004403 if (warning)
4404 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004405 }
William Lallemand222baf22016-11-19 02:00:33 +01004406 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004407 cli_err(appctx,
4408 "'set server <srv>' only supports 'agent', 'health', 'state',"
4409 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004410 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004411 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004412 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004413 return 1;
4414}
4415
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004416static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004417{
4418 struct stream_interface *si = appctx->owner;
4419 struct proxy *px;
4420 struct server *sv;
4421 char *line;
4422
4423
4424 /* split "backend/server" and make <line> point to server */
4425 for (line = args[2]; *line; line++)
4426 if (*line == '/') {
4427 *line++ = '\0';
4428 break;
4429 }
4430
Willy Tarreau9d008692019-08-09 11:21:01 +02004431 if (!*line)
4432 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004433
Willy Tarreau9d008692019-08-09 11:21:01 +02004434 if (!get_backend_server(args[2], line, &px, &sv))
4435 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004436
4437 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004438 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4439 sv->iweight);
4440 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004441 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004442 return 0;
4443 }
William Lallemand6b160942016-11-22 12:34:35 +01004444 return 1;
4445}
4446
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004447/* Parse a "set weight" command.
4448 *
4449 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004450 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004451static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004452{
4453 struct server *sv;
4454 const char *warning;
4455
4456 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4457 return 1;
4458
4459 sv = cli_find_server(appctx, args[2]);
4460 if (!sv)
4461 return 1;
4462
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004463 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4464
William Lallemand6b160942016-11-22 12:34:35 +01004465 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004466 if (warning)
4467 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004468
4469 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4470
William Lallemand6b160942016-11-22 12:34:35 +01004471 return 1;
4472}
4473
Willy Tarreau46b7f532018-08-21 11:54:26 +02004474/* parse a "set maxconn server" command. It always returns 1.
4475 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004476 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004477 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004478static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004479{
4480 struct server *sv;
4481 const char *warning;
4482
4483 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4484 return 1;
4485
4486 sv = cli_find_server(appctx, args[3]);
4487 if (!sv)
4488 return 1;
4489
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004490 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4491
Willy Tarreaub8026272016-11-23 11:26:56 +01004492 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004493 if (warning)
4494 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004495
4496 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4497
Willy Tarreaub8026272016-11-23 11:26:56 +01004498 return 1;
4499}
William Lallemand6b160942016-11-22 12:34:35 +01004500
Willy Tarreau46b7f532018-08-21 11:54:26 +02004501/* parse a "disable agent" command. It always returns 1.
4502 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004503 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004504 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004505static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004506{
4507 struct server *sv;
4508
4509 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4510 return 1;
4511
4512 sv = cli_find_server(appctx, args[2]);
4513 if (!sv)
4514 return 1;
4515
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004516 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004517 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004518 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004519 return 1;
4520}
4521
Willy Tarreau46b7f532018-08-21 11:54:26 +02004522/* parse a "disable health" command. It always returns 1.
4523 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004524 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004525 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004526static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004527{
4528 struct server *sv;
4529
4530 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4531 return 1;
4532
4533 sv = cli_find_server(appctx, args[2]);
4534 if (!sv)
4535 return 1;
4536
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004537 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004538 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004539 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004540 return 1;
4541}
4542
Willy Tarreau46b7f532018-08-21 11:54:26 +02004543/* parse a "disable server" command. It always returns 1.
4544 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004545 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004546 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004547static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004548{
4549 struct server *sv;
4550
4551 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4552 return 1;
4553
4554 sv = cli_find_server(appctx, args[2]);
4555 if (!sv)
4556 return 1;
4557
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004558 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004559 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004560 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004561 return 1;
4562}
4563
Willy Tarreau46b7f532018-08-21 11:54:26 +02004564/* parse a "enable agent" command. It always returns 1.
4565 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004566 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004567 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004568static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004569{
4570 struct server *sv;
4571
4572 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4573 return 1;
4574
4575 sv = cli_find_server(appctx, args[2]);
4576 if (!sv)
4577 return 1;
4578
Willy Tarreau9d008692019-08-09 11:21:01 +02004579 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4580 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004581
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004582 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004583 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004584 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004585 return 1;
4586}
4587
Willy Tarreau46b7f532018-08-21 11:54:26 +02004588/* parse a "enable health" command. It always returns 1.
4589 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004590 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004591 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004592static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004593{
4594 struct server *sv;
4595
4596 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4597 return 1;
4598
4599 sv = cli_find_server(appctx, args[2]);
4600 if (!sv)
4601 return 1;
4602
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004603 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004604 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004605 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004606 return 1;
4607}
4608
Willy Tarreau46b7f532018-08-21 11:54:26 +02004609/* parse a "enable server" command. It always returns 1.
4610 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004611 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004612 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004613static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004614{
4615 struct server *sv;
4616
4617 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4618 return 1;
4619
4620 sv = cli_find_server(appctx, args[2]);
4621 if (!sv)
4622 return 1;
4623
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004624 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004625 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004626 if (!(sv->flags & SRV_F_COOKIESET)
4627 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4628 sv->cookie)
4629 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004630 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004631 return 1;
4632}
4633
William Lallemand222baf22016-11-19 02:00:33 +01004634/* register cli keywords */
4635static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004636 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004637 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004638 { { "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 +01004639 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004640 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004641 { { "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 +01004642 { { "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 +01004643 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004644 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4645 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4646
William Lallemand222baf22016-11-19 02:00:33 +01004647 {{},}
4648}};
4649
Willy Tarreau0108d902018-11-25 19:14:37 +01004650INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004651
Emeric Brun64cc49c2017-10-03 14:46:45 +02004652/*
4653 * This function applies server's status changes, it is
4654 * is designed to be called asynchronously.
4655 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004656 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004657 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004658static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004659{
4660 struct check *check = &s->check;
4661 int xferred;
4662 struct proxy *px = s->proxy;
4663 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4664 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4665 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004666 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004667
Emeric Brun64cc49c2017-10-03 14:46:45 +02004668 /* If currently main is not set we try to apply pending state changes */
4669 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4670 int next_admin;
4671
4672 /* Backup next admin */
4673 next_admin = s->next_admin;
4674
4675 /* restore current admin state */
4676 s->next_admin = s->cur_admin;
4677
4678 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4679 s->last_change = now.tv_sec;
4680 if (s->proxy->lbprm.set_server_status_down)
4681 s->proxy->lbprm.set_server_status_down(s);
4682
4683 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4684 srv_shutdown_streams(s, SF_ERR_DOWN);
4685
4686 /* we might have streams queued on this server and waiting for
4687 * a connection. Those which are redispatchable will be queued
4688 * to another server or to the proxy itself.
4689 */
4690 xferred = pendconn_redistribute(s);
4691
4692 tmptrash = alloc_trash_chunk();
4693 if (tmptrash) {
4694 chunk_printf(tmptrash,
4695 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4696 s->proxy->id, s->id);
4697
Emeric Brun5a133512017-10-19 14:42:30 +02004698 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004699 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004700
4701 /* we don't send an alert if the server was previously paused */
4702 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004703 send_log(s->proxy, log_level, "%s.\n",
4704 tmptrash->area);
4705 send_email_alert(s, log_level, "%s",
4706 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004707 free_trash_chunk(tmptrash);
4708 tmptrash = NULL;
4709 }
4710 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4711 set_backend_down(s->proxy);
4712
4713 s->counters.down_trans++;
4714 }
4715 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4716 s->last_change = now.tv_sec;
4717 if (s->proxy->lbprm.set_server_status_down)
4718 s->proxy->lbprm.set_server_status_down(s);
4719
4720 /* we might have streams queued on this server and waiting for
4721 * a connection. Those which are redispatchable will be queued
4722 * to another server or to the proxy itself.
4723 */
4724 xferred = pendconn_redistribute(s);
4725
4726 tmptrash = alloc_trash_chunk();
4727 if (tmptrash) {
4728 chunk_printf(tmptrash,
4729 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4730 s->proxy->id, s->id);
4731
Emeric Brun5a133512017-10-19 14:42:30 +02004732 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004733
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004734 ha_warning("%s.\n", tmptrash->area);
4735 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4736 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004737 free_trash_chunk(tmptrash);
4738 tmptrash = NULL;
4739 }
4740
4741 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4742 set_backend_down(s->proxy);
4743 }
4744 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4745 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4746 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4747 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4748 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4749 s->proxy->last_change = now.tv_sec;
4750 }
4751
4752 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4753 s->down_time += now.tv_sec - s->last_change;
4754
4755 s->last_change = now.tv_sec;
4756 if (s->next_state == SRV_ST_STARTING)
4757 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4758
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004759 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004760 /* now propagate the status change to any LB algorithms */
4761 if (px->lbprm.update_server_eweight)
4762 px->lbprm.update_server_eweight(s);
4763 else if (srv_willbe_usable(s)) {
4764 if (px->lbprm.set_server_status_up)
4765 px->lbprm.set_server_status_up(s);
4766 }
4767 else {
4768 if (px->lbprm.set_server_status_down)
4769 px->lbprm.set_server_status_down(s);
4770 }
4771
4772 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4773 * and it's not a backup server and its effective weight is > 0,
4774 * then it can accept new connections, so we shut down all streams
4775 * on all backup servers.
4776 */
4777 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4778 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4779 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4780
4781 /* check if we can handle some connections queued at the proxy. We
4782 * will take as many as we can handle.
4783 */
4784 xferred = pendconn_grab_from_px(s);
4785
4786 tmptrash = alloc_trash_chunk();
4787 if (tmptrash) {
4788 chunk_printf(tmptrash,
4789 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4790 s->proxy->id, s->id);
4791
Emeric Brun5a133512017-10-19 14:42:30 +02004792 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004793 ha_warning("%s.\n", tmptrash->area);
4794 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4795 tmptrash->area);
4796 send_email_alert(s, LOG_NOTICE, "%s",
4797 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004798 free_trash_chunk(tmptrash);
4799 tmptrash = NULL;
4800 }
4801
4802 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4803 set_backend_down(s->proxy);
4804 }
4805 else if (s->cur_eweight != s->next_eweight) {
4806 /* now propagate the status change to any LB algorithms */
4807 if (px->lbprm.update_server_eweight)
4808 px->lbprm.update_server_eweight(s);
4809 else if (srv_willbe_usable(s)) {
4810 if (px->lbprm.set_server_status_up)
4811 px->lbprm.set_server_status_up(s);
4812 }
4813 else {
4814 if (px->lbprm.set_server_status_down)
4815 px->lbprm.set_server_status_down(s);
4816 }
4817
4818 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4819 set_backend_down(s->proxy);
4820 }
4821
4822 s->next_admin = next_admin;
4823 }
4824
Emeric Brun5a133512017-10-19 14:42:30 +02004825 /* reset operational state change */
4826 *s->op_st_chg.reason = 0;
4827 s->op_st_chg.status = s->op_st_chg.code = -1;
4828 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004829
4830 /* Now we try to apply pending admin changes */
4831
4832 /* Maintenance must also disable health checks */
4833 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4834 if (s->check.state & CHK_ST_ENABLED) {
4835 s->check.state |= CHK_ST_PAUSED;
4836 check->health = 0;
4837 }
4838
4839 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004840 tmptrash = alloc_trash_chunk();
4841 if (tmptrash) {
4842 chunk_printf(tmptrash,
4843 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4844 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4845 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004846
Olivier Houchard796a2b32017-10-24 17:42:47 +02004847 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004848
Olivier Houchard796a2b32017-10-24 17:42:47 +02004849 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004850 ha_warning("%s.\n", tmptrash->area);
4851 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4852 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004853 }
4854 free_trash_chunk(tmptrash);
4855 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004856 }
Emeric Brun8f298292017-12-06 16:47:17 +01004857 /* commit new admin status */
4858
4859 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004860 }
4861 else { /* server was still running */
4862 check->health = 0; /* failure */
4863 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004864
4865 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004866 if (s->proxy->lbprm.set_server_status_down)
4867 s->proxy->lbprm.set_server_status_down(s);
4868
Emeric Brun64cc49c2017-10-03 14:46:45 +02004869 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4870 srv_shutdown_streams(s, SF_ERR_DOWN);
4871
William Dauchy6318d332020-05-02 21:52:36 +02004872 /* force connection cleanup on the given server */
4873 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004874 /* we might have streams queued on this server and waiting for
4875 * a connection. Those which are redispatchable will be queued
4876 * to another server or to the proxy itself.
4877 */
4878 xferred = pendconn_redistribute(s);
4879
4880 tmptrash = alloc_trash_chunk();
4881 if (tmptrash) {
4882 chunk_printf(tmptrash,
4883 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4884 s->flags & SRV_F_BACKUP ? "Backup " : "",
4885 s->proxy->id, s->id,
4886 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4887
4888 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4889
4890 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004891 ha_warning("%s.\n", tmptrash->area);
4892 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4893 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004894 }
4895 free_trash_chunk(tmptrash);
4896 tmptrash = NULL;
4897 }
4898 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4899 set_backend_down(s->proxy);
4900
4901 s->counters.down_trans++;
4902 }
4903 }
4904 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4905 /* OK here we're leaving maintenance, we have many things to check,
4906 * because the server might possibly be coming back up depending on
4907 * its state. In practice, leaving maintenance means that we should
4908 * immediately turn to UP (more or less the slowstart) under the
4909 * following conditions :
4910 * - server is neither checked nor tracked
4911 * - server tracks another server which is not checked
4912 * - server tracks another server which is already up
4913 * Which sums up as something simpler :
4914 * "either the tracking server is up or the server's checks are disabled
4915 * or up". Otherwise we only re-enable health checks. There's a special
4916 * case associated to the stopping state which can be inherited. Note
4917 * that the server might still be in drain mode, which is naturally dealt
4918 * with by the lower level functions.
4919 */
4920
4921 if (s->check.state & CHK_ST_ENABLED) {
4922 s->check.state &= ~CHK_ST_PAUSED;
4923 check->health = check->rise; /* start OK but check immediately */
4924 }
4925
4926 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4927 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4928 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4929 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4930 s->next_state = SRV_ST_STOPPING;
4931 }
4932 else {
4933 s->next_state = SRV_ST_STARTING;
4934 if (s->slowstart > 0)
4935 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4936 else
4937 s->next_state = SRV_ST_RUNNING;
4938 }
4939
4940 }
4941
4942 tmptrash = alloc_trash_chunk();
4943 if (tmptrash) {
4944 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4945 chunk_printf(tmptrash,
4946 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4947 s->flags & SRV_F_BACKUP ? "Backup " : "",
4948 s->proxy->id, s->id,
4949 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4950 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4951 }
4952 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4953 chunk_printf(tmptrash,
4954 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4955 s->flags & SRV_F_BACKUP ? "Backup " : "",
4956 s->proxy->id, s->id, s->hostname,
4957 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4958 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4959 }
4960 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4961 chunk_printf(tmptrash,
4962 "%sServer %s/%s is %s/%s (leaving maintenance)",
4963 s->flags & SRV_F_BACKUP ? "Backup " : "",
4964 s->proxy->id, s->id,
4965 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4966 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4967 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004968 ha_warning("%s.\n", tmptrash->area);
4969 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4970 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004971 free_trash_chunk(tmptrash);
4972 tmptrash = NULL;
4973 }
4974
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004975 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004976 /* now propagate the status change to any LB algorithms */
4977 if (px->lbprm.update_server_eweight)
4978 px->lbprm.update_server_eweight(s);
4979 else if (srv_willbe_usable(s)) {
4980 if (px->lbprm.set_server_status_up)
4981 px->lbprm.set_server_status_up(s);
4982 }
4983 else {
4984 if (px->lbprm.set_server_status_down)
4985 px->lbprm.set_server_status_down(s);
4986 }
4987
4988 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4989 set_backend_down(s->proxy);
4990
Willy Tarreau6a78e612018-08-07 10:14:53 +02004991 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4992 * and it's not a backup server and its effective weight is > 0,
4993 * then it can accept new connections, so we shut down all streams
4994 * on all backup servers.
4995 */
4996 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4997 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4998 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4999
5000 /* check if we can handle some connections queued at the proxy. We
5001 * will take as many as we can handle.
5002 */
5003 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005004 }
5005 else if (s->next_admin & SRV_ADMF_MAINT) {
5006 /* remaining in maintenance mode, let's inform precisely about the
5007 * situation.
5008 */
5009 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5010 tmptrash = alloc_trash_chunk();
5011 if (tmptrash) {
5012 chunk_printf(tmptrash,
5013 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5014 s->flags & SRV_F_BACKUP ? "Backup " : "",
5015 s->proxy->id, s->id);
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 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5028 tmptrash = alloc_trash_chunk();
5029 if (tmptrash) {
5030 chunk_printf(tmptrash,
5031 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5032 s->flags & SRV_F_BACKUP ? "Backup " : "",
5033 s->proxy->id, s->id, s->hostname);
5034
5035 if (s->track) /* normally it's mandatory here */
5036 chunk_appendf(tmptrash, " via %s/%s",
5037 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005038 ha_warning("%s.\n", tmptrash->area);
5039 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5040 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005041 free_trash_chunk(tmptrash);
5042 tmptrash = NULL;
5043 }
5044 }
5045 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5046 tmptrash = alloc_trash_chunk();
5047 if (tmptrash) {
5048 chunk_printf(tmptrash,
5049 "%sServer %s/%s remains in forced maintenance",
5050 s->flags & SRV_F_BACKUP ? "Backup " : "",
5051 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005052 ha_warning("%s.\n", tmptrash->area);
5053 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5054 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005055 free_trash_chunk(tmptrash);
5056 tmptrash = NULL;
5057 }
5058 }
5059 /* don't report anything when leaving drain mode and remaining in maintenance */
5060
5061 s->cur_admin = s->next_admin;
5062 }
5063
5064 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5065 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5066 /* drain state is applied only if not yet in maint */
5067
5068 s->last_change = now.tv_sec;
5069 if (px->lbprm.set_server_status_down)
5070 px->lbprm.set_server_status_down(s);
5071
5072 /* we might have streams queued on this server and waiting for
5073 * a connection. Those which are redispatchable will be queued
5074 * to another server or to the proxy itself.
5075 */
5076 xferred = pendconn_redistribute(s);
5077
5078 tmptrash = alloc_trash_chunk();
5079 if (tmptrash) {
5080 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5081 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5082 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5083
5084 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5085
5086 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005087 ha_warning("%s.\n", tmptrash->area);
5088 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5089 tmptrash->area);
5090 send_email_alert(s, LOG_NOTICE, "%s",
5091 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005092 }
5093 free_trash_chunk(tmptrash);
5094 tmptrash = NULL;
5095 }
5096
5097 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5098 set_backend_down(s->proxy);
5099 }
5100 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5101 /* OK completely leaving drain mode */
5102 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5103 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5104 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5105 s->proxy->last_change = now.tv_sec;
5106 }
5107
5108 if (s->last_change < now.tv_sec) // ignore negative times
5109 s->down_time += now.tv_sec - s->last_change;
5110 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005111 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005112
5113 tmptrash = alloc_trash_chunk();
5114 if (tmptrash) {
5115 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5116 chunk_printf(tmptrash,
5117 "%sServer %s/%s is %s (leaving forced drain)",
5118 s->flags & SRV_F_BACKUP ? "Backup " : "",
5119 s->proxy->id, s->id,
5120 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5121 }
5122 else {
5123 chunk_printf(tmptrash,
5124 "%sServer %s/%s is %s (leaving drain)",
5125 s->flags & SRV_F_BACKUP ? "Backup " : "",
5126 s->proxy->id, s->id,
5127 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5128 if (s->track) /* normally it's mandatory here */
5129 chunk_appendf(tmptrash, " via %s/%s",
5130 s->track->proxy->id, s->track->id);
5131 }
5132
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005133 ha_warning("%s.\n", tmptrash->area);
5134 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5135 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005136 free_trash_chunk(tmptrash);
5137 tmptrash = NULL;
5138 }
5139
5140 /* now propagate the status change to any LB algorithms */
5141 if (px->lbprm.update_server_eweight)
5142 px->lbprm.update_server_eweight(s);
5143 else if (srv_willbe_usable(s)) {
5144 if (px->lbprm.set_server_status_up)
5145 px->lbprm.set_server_status_up(s);
5146 }
5147 else {
5148 if (px->lbprm.set_server_status_down)
5149 px->lbprm.set_server_status_down(s);
5150 }
5151 }
5152 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5153 /* remaining in drain mode after removing one of its flags */
5154
5155 tmptrash = alloc_trash_chunk();
5156 if (tmptrash) {
5157 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5158 chunk_printf(tmptrash,
5159 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5160 s->flags & SRV_F_BACKUP ? "Backup " : "",
5161 s->proxy->id, s->id);
5162
5163 if (s->track) /* normally it's mandatory here */
5164 chunk_appendf(tmptrash, " via %s/%s",
5165 s->track->proxy->id, s->track->id);
5166 }
5167 else {
5168 chunk_printf(tmptrash,
5169 "%sServer %s/%s remains in forced drain mode",
5170 s->flags & SRV_F_BACKUP ? "Backup " : "",
5171 s->proxy->id, s->id);
5172 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005173 ha_warning("%s.\n", tmptrash->area);
5174 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5175 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005176 free_trash_chunk(tmptrash);
5177 tmptrash = NULL;
5178 }
5179
5180 /* commit new admin status */
5181
5182 s->cur_admin = s->next_admin;
5183 }
5184 }
5185
5186 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005187 *s->adm_st_chg_cause = 0;
5188}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005189
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005190struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5191{
5192 struct connection *conn;
5193
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005194 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005195 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005196 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005197 }
5198
5199 return task;
5200}
5201
Olivier Houchardff1d0922020-06-29 20:15:59 +02005202/* Move toremove_nb connections from idle_list to toremove_list, -1 means
5203 * moving them all.
5204 * Returns the number of connections moved.
5205 */
5206static int srv_migrate_conns_to_remove(struct mt_list *idle_list, struct mt_list *toremove_list, int toremove_nb)
5207{
5208 struct mt_list *elt1, elt2;
5209 struct connection *conn;
5210 int i = 0;
5211
5212 mt_list_for_each_entry_safe(conn, idle_list, list, elt1, elt2) {
5213 if (toremove_nb != -1 && i >= toremove_nb)
5214 break;
5215 MT_LIST_DEL_SAFE_NOINIT(elt1);
Willy Tarreaude4db172020-07-10 08:10:29 +02005216 MT_LIST_ADDQ(toremove_list, &conn->list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005217 i++;
5218 }
5219 return i;
5220}
William Dauchy6318d332020-05-02 21:52:36 +02005221/* cleanup connections for a given server
5222 * might be useful when going on forced maintenance or live changing ip/port
5223 */
William Dauchy707ad322020-05-04 13:52:40 +02005224static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005225{
William Dauchy6318d332020-05-02 21:52:36 +02005226 int did_remove;
5227 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005228
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005229 /* check all threads starting with ours */
William Dauchy6318d332020-05-02 21:52:36 +02005230 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005231 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005232 did_remove = 0;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005233 if (srv_migrate_conns_to_remove(&srv->idle_conns[i], &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02005234 did_remove = 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005235 if (srv_migrate_conns_to_remove(&srv->safe_conns[i], &idle_conns[i].toremove_conns, -1) > 0)
5236 did_remove = 1;
William Dauchy6318d332020-05-02 21:52:36 +02005237 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005238 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005239
5240 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5241 break;
William Dauchy6318d332020-05-02 21:52:36 +02005242 }
5243 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5244}
5245
Willy Tarreau980855b2019-02-07 14:59:29 +01005246struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005247{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005248 struct server *srv;
5249 struct eb32_node *eb;
5250 int i;
5251 unsigned int next_wakeup;
5252 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005253
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005254 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5255 while (1) {
5256 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005257 int exceed_conns;
5258 int to_kill;
5259 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005260
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005261 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5262 if (!eb) {
5263 /* we might have reached the end of the tree, typically because
5264 * <now_ms> is in the first half and we're first scanning the last
5265 * half. Let's loop back to the beginning of the tree now.
5266 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005267
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005268 eb = eb32_first(&idle_conn_srv);
5269 if (likely(!eb))
5270 break;
5271 }
5272 if (tick_is_lt(now_ms, eb->key)) {
5273 /* timer not expired yet, revisit it later */
5274 next_wakeup = eb->key;
5275 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005276 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005277 }
5278 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005279
5280 /* Calculate how many idle connections we want to kill :
5281 * we want to remove half the difference between the total
5282 * of established connections (used or idle) and the max
5283 * number of used connections.
5284 */
5285 curr_idle = srv->curr_idle_conns;
5286 if (curr_idle == 0)
5287 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005288 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005289 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005290
5291 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns + 1) / 2;
5292 if (srv->est_need_conns < srv->max_used_conns)
5293 srv->est_need_conns = srv->max_used_conns;
5294
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005295 srv->max_used_conns = srv->curr_used_conns;
5296
Willy Tarreau18ed7892020-07-02 19:05:30 +02005297 if (exceed_conns <= 0)
5298 goto remove;
5299
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005300 /* check all threads starting with ours */
5301 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005302 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005303 int j;
5304 int did_remove = 0;
5305
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005306 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5307 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005308
5309 j = srv_migrate_conns_to_remove(&srv->idle_conns[i], &idle_conns[i].toremove_conns, max_conn);
5310 if (j > 0)
5311 did_remove = 1;
5312 if (max_conn - j > 0 &&
5313 srv_migrate_conns_to_remove(&srv->safe_conns[i], &idle_conns[i].toremove_conns, max_conn - j) > 0)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005314 did_remove = 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005315
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005316 if (did_remove && max_conn < srv->curr_idle_thr[i])
5317 srv_is_empty = 0;
5318 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005319 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005320
5321 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5322 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005323 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005324remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005325 eb32_delete(&srv->idle_node);
5326 if (!srv_is_empty) {
5327 /* There are still more idle connections, add the
5328 * server back in the tree.
5329 */
5330 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5331 now_ms);
5332 eb32_insert(&idle_conn_srv, &srv->idle_node);
5333 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005334 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005335 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5336
5337 if (need_wakeup)
5338 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005339 else
5340 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005341
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005342 return task;
5343}
Olivier Houchard88698d92019-04-16 19:07:22 +02005344
Willy Tarreau76cc6992020-07-01 18:49:24 +02005345/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
5346static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
5347 struct proxy *defpx, const char *file, int line,
5348 char **err)
5349{
5350 if (too_many_args(1, args, err, NULL))
5351 return -1;
5352
5353 if (strcmp(args[1], "on") == 0)
5354 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
5355 else if (strcmp(args[1], "off") == 0)
5356 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
5357 else {
5358 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
5359 return -1;
5360 }
5361 return 0;
5362}
5363
Olivier Houchard88698d92019-04-16 19:07:22 +02005364/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5365static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5366 struct proxy *defpx, const char *file, int line,
5367 char **err)
5368{
5369 int arg = -1;
5370
5371 if (too_many_args(1, args, err, NULL))
5372 return -1;
5373
5374 if (*(args[1]) != 0)
5375 arg = atoi(args[1]);
5376
5377 if (arg < 0 || arg > 100) {
5378 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5379 return -1;
5380 }
5381
5382 if (args[0][10] == 'h')
5383 global.tune.pool_high_ratio = arg;
5384 else
5385 global.tune.pool_low_ratio = arg;
5386 return 0;
5387}
5388
5389/* config keyword parsers */
5390static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02005391 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02005392 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5393 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5394 { 0, NULL, NULL }
5395}};
5396
5397INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5398
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005399/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005400 * Local variables:
5401 * c-indent-level: 8
5402 * c-basic-offset: 8
5403 * End:
5404 */