blob: 45b0f41216771ae3d89d48a8dcac2223c939362c [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;
Frédéric Lécailledba97072017-03-17 15:33:50 +0100651
652 errmsg = NULL;
653
654 if (!*args[*cur_arg + 1]) {
655 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
656 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
657 goto err;
658 }
659
660 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200661 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
662 &errmsg, NULL, NULL,
663 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100664 if (!sk) {
665 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
666 goto err;
667 }
668
Frédéric Lécailledba97072017-03-17 15:33:50 +0100669 newsrv->conn_src.opts |= CO_SRC_BIND;
670 newsrv->conn_src.source_addr = *sk;
671
672 if (port_low != port_high) {
673 int i;
674
Frédéric Lécailledba97072017-03-17 15:33:50 +0100675 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
676 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
677 newsrv->conn_src.sport_range->ports[i] = port_low + i;
678 }
679
680 *cur_arg += 2;
681 while (*(args[*cur_arg])) {
682 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
683#if defined(CONFIG_HAP_TRANSPARENT)
684 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100685 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
686 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100687 goto err;
688 }
689 if (!strcmp(args[*cur_arg + 1], "client")) {
690 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
691 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
692 }
693 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
694 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
695 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
696 }
697 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
698 char *name, *end;
699
700 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100701 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100702 name++;
703
704 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100705 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100706 end++;
707
708 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
709 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
710 free(newsrv->conn_src.bind_hdr_name);
711 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
712 newsrv->conn_src.bind_hdr_len = end - name;
713 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
714 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
715 newsrv->conn_src.bind_hdr_occ = -1;
716
717 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100718 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100719 end++;
720 if (*end == ',') {
721 end++;
722 name = end;
723 if (*end == '-')
724 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100725 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100726 end++;
727 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
728 }
729
730 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100731 ha_alert("usesrc hdr_ip(name,num) does not support negative"
732 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100733 goto err;
734 }
735 }
736 else {
737 struct sockaddr_storage *sk;
738 int port1, port2;
739
740 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200741 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
742 &errmsg, NULL, NULL,
743 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100744 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100745 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100746 goto err;
747 }
748
Frédéric Lécailledba97072017-03-17 15:33:50 +0100749 newsrv->conn_src.tproxy_addr = *sk;
750 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
751 }
752 global.last_checks |= LSTCHK_NETADM;
753 *cur_arg += 2;
754 continue;
755#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100756 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 +0100757 goto err;
758#endif /* defined(CONFIG_HAP_TRANSPARENT) */
759 } /* "usesrc" */
760
761 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
762#ifdef SO_BINDTODEVICE
763 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100764 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100765 goto err;
766 }
767 free(newsrv->conn_src.iface_name);
768 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
769 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
770 global.last_checks |= LSTCHK_NETADM;
771#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100772 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100773 goto err;
774#endif
775 *cur_arg += 2;
776 continue;
777 }
778 /* this keyword in not an option of "source" */
779 break;
780 } /* while */
781
782 return 0;
783
784 err:
785 free(errmsg);
786 return ERR_ALERT | ERR_FATAL;
787}
788
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100789/* Parse the "stick" server keyword */
790static int srv_parse_stick(char **args, int *cur_arg,
791 struct proxy *curproxy, struct server *newsrv, char **err)
792{
793 newsrv->flags &= ~SRV_F_NON_STICK;
794 return 0;
795}
796
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100797/* Parse the "track" server keyword */
798static int srv_parse_track(char **args, int *cur_arg,
799 struct proxy *curproxy, struct server *newsrv, char **err)
800{
801 char *arg;
802
803 arg = args[*cur_arg + 1];
804 if (!*arg) {
805 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
806 return ERR_ALERT | ERR_FATAL;
807 }
808
809 free(newsrv->trackit);
810 newsrv->trackit = strdup(arg);
811
812 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800813}
814
815/* Parse the "socks4" server keyword */
816static int srv_parse_socks4(char **args, int *cur_arg,
817 struct proxy *curproxy, struct server *newsrv, char **err)
818{
819 char *errmsg;
820 int port_low, port_high;
821 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800822
823 errmsg = NULL;
824
825 if (!*args[*cur_arg + 1]) {
826 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
827 goto err;
828 }
829
830 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200831 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
832 &errmsg, NULL, NULL,
833 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +0800834 if (!sk) {
835 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
836 goto err;
837 }
838
Alexander Liu2a54bb72019-05-22 19:44:48 +0800839 newsrv->flags |= SRV_F_SOCKS4_PROXY;
840 newsrv->socks4_addr = *sk;
841
Alexander Liu2a54bb72019-05-22 19:44:48 +0800842 return 0;
843
844 err:
845 free(errmsg);
846 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100847}
848
Frédéric Lécailledba97072017-03-17 15:33:50 +0100849
Willy Tarreau034c88c2017-01-23 23:36:45 +0100850/* parse the "tfo" server keyword */
851static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
852{
853 newsrv->flags |= SRV_F_FASTOPEN;
854 return 0;
855}
856
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200857/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200858 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200859 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200860 *
861 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200862 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200863void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200864{
Willy Tarreau87b09662015-04-03 00:22:06 +0200865 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200866
Willy Tarreau87b09662015-04-03 00:22:06 +0200867 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
868 if (stream->srv_conn == srv)
869 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200870}
871
872/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200873 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200874 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200875 *
876 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200877 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200878void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200879{
880 struct server *srv;
881
882 for (srv = px->srv; srv != NULL; srv = srv->next)
883 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200884 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200885}
886
Willy Tarreaubda92272014-05-20 21:55:30 +0200887/* Appends some information to a message string related to a server going UP or
888 * DOWN. If both <forced> and <reason> are null and the server tracks another
889 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200890 * If <check> is non-null, an entire string describing the check result will be
891 * appended after a comma and a space (eg: to report some information from the
892 * check that changed the state). In the other case, the string will be built
893 * using the check results stored into the struct server if present.
894 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200895 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200896 *
897 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200898 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200899void srv_append_status(struct buffer *msg, struct server *s,
900 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200901{
Emeric Brun5a133512017-10-19 14:42:30 +0200902 short status = s->op_st_chg.status;
903 short code = s->op_st_chg.code;
904 long duration = s->op_st_chg.duration;
905 char *desc = s->op_st_chg.reason;
906
907 if (check) {
908 status = check->status;
909 code = check->code;
910 duration = check->duration;
911 desc = check->desc;
912 }
913
914 if (status != -1) {
915 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
916
917 if (status >= HCHK_STATUS_L57DATA)
918 chunk_appendf(msg, ", code: %d", code);
919
920 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200921 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200922
923 chunk_appendf(msg, ", info: \"");
924
925 chunk_initlen(&src, desc, 0, strlen(desc));
926 chunk_asciiencode(msg, &src, '"');
927
928 chunk_appendf(msg, "\"");
929 }
930
931 if (duration >= 0)
932 chunk_appendf(msg, ", check duration: %ldms", duration);
933 }
934 else if (desc && *desc) {
935 chunk_appendf(msg, ", %s", desc);
936 }
937 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200938 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200939 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200940
941 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200942 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200943 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
944 " %d sessions active, %d requeued, %d remaining in queue",
945 s->proxy->srv_act, s->proxy->srv_bck,
946 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
947 s->cur_sess, xferred, s->nbpend);
948 else
949 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
950 " %d sessions requeued, %d total in queue",
951 s->proxy->srv_act, s->proxy->srv_bck,
952 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
953 xferred, s->nbpend);
954 }
955}
956
Emeric Brun5a133512017-10-19 14:42:30 +0200957/* Marks server <s> down, regardless of its checks' statuses. The server is
958 * registered in a list to postpone the counting of the remaining servers on
959 * the proxy and transfers queued streams whenever possible to other servers at
960 * a sync point. Maintenance servers are ignored. It stores the <reason> if
961 * non-null as the reason for going down or the available data from the check
962 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200963 *
964 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200965 */
Emeric Brun5a133512017-10-19 14:42:30 +0200966void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200967{
968 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200969
Emeric Brun64cc49c2017-10-03 14:46:45 +0200970 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200971 return;
972
Emeric Brun52a91d32017-08-31 14:41:55 +0200973 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200974 *s->op_st_chg.reason = 0;
975 s->op_st_chg.status = -1;
976 if (reason) {
977 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
978 }
979 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100980 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200981 s->op_st_chg.code = check->code;
982 s->op_st_chg.status = check->status;
983 s->op_st_chg.duration = check->duration;
984 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200985
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200986 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200987 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200988
Emeric Brun9f0b4582017-10-23 14:39:51 +0200989 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100990 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200991 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100992 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200993 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200994}
995
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200996/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200997 * in maintenance. The server is registered in a list to postpone the counting
998 * of the remaining servers on the proxy and tries to grab requests from the
999 * proxy at a sync point. Maintenance servers are ignored. It stores the
1000 * <reason> if non-null as the reason for going down or the available data
1001 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001002 *
1003 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001004 */
Emeric Brun5a133512017-10-19 14:42:30 +02001005void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001006{
1007 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001008
Emeric Brun64cc49c2017-10-03 14:46:45 +02001009 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001010 return;
1011
Emeric Brun52a91d32017-08-31 14:41:55 +02001012 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001013 return;
1014
Emeric Brun52a91d32017-08-31 14:41:55 +02001015 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001016 *s->op_st_chg.reason = 0;
1017 s->op_st_chg.status = -1;
1018 if (reason) {
1019 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1020 }
1021 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001022 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001023 s->op_st_chg.code = check->code;
1024 s->op_st_chg.status = check->status;
1025 s->op_st_chg.duration = check->duration;
1026 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001027
Emeric Brun64cc49c2017-10-03 14:46:45 +02001028 if (s->slowstart <= 0)
1029 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001030
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001031 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001032 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001033
Emeric Brun9f0b4582017-10-23 14:39:51 +02001034 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001035 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001036 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001037 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001038 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001039}
1040
Willy Tarreau8eb77842014-05-21 13:54:57 +02001041/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001042 * isn't in maintenance. The server is registered in a list to postpone the
1043 * counting of the remaining servers on the proxy and tries to grab requests
1044 * from the proxy. Maintenance servers are ignored. It stores the
1045 * <reason> if non-null as the reason for going down or the available data
1046 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001047 * up. Note that it makes use of the trash to build the log strings, so <reason>
1048 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001049 *
1050 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001051 */
Emeric Brun5a133512017-10-19 14:42:30 +02001052void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001053{
1054 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001055
Emeric Brun64cc49c2017-10-03 14:46:45 +02001056 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001057 return;
1058
Emeric Brun52a91d32017-08-31 14:41:55 +02001059 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001060 return;
1061
Emeric Brun52a91d32017-08-31 14:41:55 +02001062 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001063 *s->op_st_chg.reason = 0;
1064 s->op_st_chg.status = -1;
1065 if (reason) {
1066 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1067 }
1068 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001069 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001070 s->op_st_chg.code = check->code;
1071 s->op_st_chg.status = check->status;
1072 s->op_st_chg.duration = check->duration;
1073 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001074
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001075 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001076 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001077
Emeric Brun9f0b4582017-10-23 14:39:51 +02001078 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001079 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001080 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001081 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001082 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001083}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001084
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001085/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1086 * enforce either maint mode or drain mode. It is not allowed to set more than
1087 * one flag at once. The equivalent "inherited" flag is propagated to all
1088 * tracking servers. Maintenance mode disables health checks (but not agent
1089 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001090 * is done. If <cause> is non-null, it will be displayed at the end of the log
1091 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001092 *
1093 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001094 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001095void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001096{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001097 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001098
1099 if (!mode)
1100 return;
1101
1102 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001103 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001104 return;
1105
Emeric Brun52a91d32017-08-31 14:41:55 +02001106 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001107 if (cause)
1108 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1109
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001110 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001111 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001112
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001113 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001114 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1115 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001116 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001117
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001118 /* compute the inherited flag to propagate */
1119 if (mode & SRV_ADMF_MAINT)
1120 mode = SRV_ADMF_IMAINT;
1121 else if (mode & SRV_ADMF_DRAIN)
1122 mode = SRV_ADMF_IDRAIN;
1123
Emeric Brun9f0b4582017-10-23 14:39:51 +02001124 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001125 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001126 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001127 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001128 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129}
1130
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001131/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1132 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1133 * than one flag at once. The equivalent "inherited" flag is propagated to all
1134 * tracking servers. Leaving maintenance mode re-enables health checks. When
1135 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001136 *
1137 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001138 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001139void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001140{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001141 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001142
1143 if (!mode)
1144 return;
1145
1146 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001147 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001148 return;
1149
Emeric Brun52a91d32017-08-31 14:41:55 +02001150 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001151
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001152 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001153 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001154
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001155 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001156 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1157 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001158 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001159
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001160 if (mode & SRV_ADMF_MAINT)
1161 mode = SRV_ADMF_IMAINT;
1162 else if (mode & SRV_ADMF_DRAIN)
1163 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001164
Emeric Brun9f0b4582017-10-23 14:39:51 +02001165 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001166 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001167 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001168 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001169 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170}
1171
Willy Tarreau757478e2016-11-03 19:22:19 +01001172/* principle: propagate maint and drain to tracking servers. This is useful
1173 * upon startup so that inherited states are correct.
1174 */
1175static void srv_propagate_admin_state(struct server *srv)
1176{
1177 struct server *srv2;
1178
1179 if (!srv->trackers)
1180 return;
1181
1182 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001183 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001184 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001185 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001186
Emeric Brun52a91d32017-08-31 14:41:55 +02001187 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001188 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001189 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001190 }
1191}
1192
1193/* Compute and propagate the admin states for all servers in proxy <px>.
1194 * Only servers *not* tracking another one are considered, because other
1195 * ones will be handled when the server they track is visited.
1196 */
1197void srv_compute_all_admin_states(struct proxy *px)
1198{
1199 struct server *srv;
1200
1201 for (srv = px->srv; srv; srv = srv->next) {
1202 if (srv->track)
1203 continue;
1204 srv_propagate_admin_state(srv);
1205 }
1206}
1207
Willy Tarreaudff55432012-10-10 17:51:05 +02001208/* Note: must not be declared <const> as its list will be overwritten.
1209 * Please take care of keeping this list alphabetically sorted, doing so helps
1210 * all code contributors.
1211 * Optional keywords are also declared with a NULL ->parse() function so that
1212 * the config parser can report an appropriate error when a known keyword was
1213 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001214 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001215 */
1216static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001217 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001218 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001219 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1220 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001221 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001222 { "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 +01001223 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001224 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001225 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1226 { "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 +02001227 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001228 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001229 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001230 { "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 +01001231 { "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 +01001232 { "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 +02001233 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001234 { "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 +01001235 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001236 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1237 { "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 +02001238 { "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 +01001239 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001240 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001241 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001242 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001243 { NULL, NULL, 0 },
1244}};
1245
Willy Tarreau0108d902018-11-25 19:14:37 +01001246INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001247
Willy Tarreau004e0452013-11-21 11:22:01 +01001248/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001249 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001250 * state is automatically disabled if the time is elapsed. If <must_update> is
1251 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001252 *
1253 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001254 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001255void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001256{
1257 struct proxy *px = sv->proxy;
1258 unsigned w;
1259
1260 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1261 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001262 if (sv->next_state == SRV_ST_STARTING)
1263 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001264 }
1265
1266 /* We must take care of not pushing the server to full throttle during slow starts.
1267 * It must also start immediately, at least at the minimal step when leaving maintenance.
1268 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001269 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001270 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1271 else
1272 w = px->lbprm.wdiv;
1273
Emeric Brun52a91d32017-08-31 14:41:55 +02001274 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001275
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001276 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001277 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001278 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001279}
1280
Willy Tarreaubaaee002006-06-26 02:48:02 +02001281/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001282 * Parses weight_str and configures sv accordingly.
1283 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001284 *
1285 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001286 */
1287const char *server_parse_weight_change_request(struct server *sv,
1288 const char *weight_str)
1289{
1290 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001291 long int w;
1292 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001293
1294 px = sv->proxy;
1295
1296 /* if the weight is terminated with '%', it is set relative to
1297 * the initial weight, otherwise it is absolute.
1298 */
1299 if (!*weight_str)
1300 return "Require <weight> or <weight%>.\n";
1301
Simon Hormanb796afa2013-02-12 10:45:53 +09001302 w = strtol(weight_str, &end, 10);
1303 if (end == weight_str)
1304 return "Empty weight string empty or preceded by garbage";
1305 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001306 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001307 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001308 /* Avoid integer overflow */
1309 if (w > 25600)
1310 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001311 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001312 if (w > 256)
1313 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001314 }
1315 else if (w < 0 || w > 256)
1316 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001317 else if (end[0] != '\0')
1318 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001319
1320 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1321 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1322
1323 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001324 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001325
1326 return NULL;
1327}
1328
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001329/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001330 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1331 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001332 * Returns:
1333 * - error string on error
1334 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001335 *
1336 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001337 */
1338const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001339 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001340{
1341 unsigned char ip[INET6_ADDRSTRLEN];
1342
1343 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001344 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001345 return NULL;
1346 }
1347 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001348 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001349 return NULL;
1350 }
1351
1352 return "Could not understand IP address format.\n";
1353}
1354
Willy Tarreau46b7f532018-08-21 11:54:26 +02001355/*
1356 * Must be called with the server lock held.
1357 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001358const char *server_parse_maxconn_change_request(struct server *sv,
1359 const char *maxconn_str)
1360{
1361 long int v;
1362 char *end;
1363
1364 if (!*maxconn_str)
1365 return "Require <maxconn>.\n";
1366
1367 v = strtol(maxconn_str, &end, 10);
1368 if (end == maxconn_str)
1369 return "maxconn string empty or preceded by garbage";
1370 else if (end[0] != '\0')
1371 return "Trailing garbage in maxconn string";
1372
1373 if (sv->maxconn == sv->minconn) { // static maxconn
1374 sv->maxconn = sv->minconn = v;
1375 } else { // dynamic maxconn
1376 sv->maxconn = v;
1377 }
1378
1379 if (may_dequeue_tasks(sv, sv->proxy))
1380 process_srv_queue(sv);
1381
1382 return NULL;
1383}
1384
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001385#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001386static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1387 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001388{
1389 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001390 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001391 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001392 NULL,
1393 };
1394
1395 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001396 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001397
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001398 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001399}
1400
1401static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1402{
1403 struct sample_expr *expr;
1404
1405 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 +01001406 if (!expr) {
1407 memprintf(err, "error detected while parsing sni expression : %s", *err);
1408 return ERR_ALERT | ERR_FATAL;
1409 }
1410
1411 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1412 memprintf(err, "error detected while parsing sni expression : "
1413 " fetch method '%s' extracts information from '%s', "
1414 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001415 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001416 return ERR_ALERT | ERR_FATAL;
1417 }
1418
1419 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1420 release_sample_expr(newsrv->ssl_ctx.sni);
1421 newsrv->ssl_ctx.sni = expr;
1422
1423 return 0;
1424}
1425#endif
1426
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001427static 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 +01001428{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001429 char *msg = "error encountered while processing ";
1430 char *quote = "'";
1431 char *token = args[cur_arg];
1432
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001433 if (err && *err) {
1434 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001435 msg = *err;
1436 quote = "";
1437 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001438 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001439
1440 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1441 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1442 file, linenum, args[0], args[1],
1443 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001444 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001445 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1446 file, linenum, args[0], args[1],
1447 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001448}
1449
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001450static void srv_conn_src_sport_range_cpy(struct server *srv,
1451 struct server *src)
1452{
1453 int range_sz;
1454
1455 range_sz = src->conn_src.sport_range->size;
1456 if (range_sz > 0) {
1457 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1458 if (srv->conn_src.sport_range != NULL) {
1459 int i;
1460
1461 for (i = 0; i < range_sz; i++) {
1462 srv->conn_src.sport_range->ports[i] =
1463 src->conn_src.sport_range->ports[i];
1464 }
1465 }
1466 }
1467}
1468
1469/*
1470 * Copy <src> server connection source settings to <srv> server everything needed.
1471 */
1472static void srv_conn_src_cpy(struct server *srv, struct server *src)
1473{
1474 srv->conn_src.opts = src->conn_src.opts;
1475 srv->conn_src.source_addr = src->conn_src.source_addr;
1476
1477 /* Source port range copy. */
1478 if (src->conn_src.sport_range != NULL)
1479 srv_conn_src_sport_range_cpy(srv, src);
1480
1481#ifdef CONFIG_HAP_TRANSPARENT
1482 if (src->conn_src.bind_hdr_name != NULL) {
1483 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1484 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1485 }
1486 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1487 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1488#endif
1489 if (src->conn_src.iface_name != NULL)
1490 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1491}
1492
1493/*
1494 * Copy <src> server SSL settings to <srv> server allocating
1495 * everything needed.
1496 */
1497#if defined(USE_OPENSSL)
1498static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1499{
1500 if (src->ssl_ctx.ca_file != NULL)
1501 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1502 if (src->ssl_ctx.crl_file != NULL)
1503 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1504 if (src->ssl_ctx.client_crt != NULL)
1505 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1506
1507 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1508
1509 if (src->ssl_ctx.verify_host != NULL)
1510 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1511 if (src->ssl_ctx.ciphers != NULL)
1512 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001513 if (src->ssl_ctx.options)
1514 srv->ssl_ctx.options = src->ssl_ctx.options;
1515 if (src->ssl_ctx.methods.flags)
1516 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1517 if (src->ssl_ctx.methods.min)
1518 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1519 if (src->ssl_ctx.methods.max)
1520 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1521
Willy Tarreau5db847a2019-05-09 14:13:35 +02001522#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001523 if (src->ssl_ctx.ciphersuites != NULL)
1524 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1525#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001526 if (src->sni_expr != NULL)
1527 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001528
1529#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1530 if (src->ssl_ctx.alpn_str) {
1531 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1532 if (srv->ssl_ctx.alpn_str) {
1533 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1534 src->ssl_ctx.alpn_len);
1535 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1536 }
1537 }
1538#endif
1539#ifdef OPENSSL_NPN_NEGOTIATED
1540 if (src->ssl_ctx.npn_str) {
1541 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1542 if (srv->ssl_ctx.npn_str) {
1543 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1544 src->ssl_ctx.npn_len);
1545 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1546 }
1547 }
1548#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001549}
1550#endif
1551
1552/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001553 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001554 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001555 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001556 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001557static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001558{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001559 char *hostname_dn;
1560 int hostname_len, hostname_dn_len;
1561
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001562 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001563 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001564
Christopher Faulet67957bd2017-09-27 11:00:59 +02001565 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001567 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1568 hostname_dn, trash.size);
1569 if (hostname_dn_len == -1)
1570 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001571
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001572
Christopher Faulet67957bd2017-09-27 11:00:59 +02001573 free(srv->hostname);
1574 free(srv->hostname_dn);
1575 srv->hostname = strdup(hostname);
1576 srv->hostname_dn = strdup(hostname_dn);
1577 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001578 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001579 goto err;
1580
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001581 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001582
1583 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001584 free(srv->hostname); srv->hostname = NULL;
1585 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001586 return -1;
1587}
1588
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001589/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001590 * Copy <src> server settings to <srv> server allocating
1591 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001592 * This function is not supposed to be called at any time, but only
1593 * during server settings parsing or during server allocations from
1594 * a server template, and just after having calloc()'ed a new server.
1595 * So, <src> may only be a default server (when parsing server settings)
1596 * or a server template (during server allocations from a server template).
1597 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1598 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001599 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001600static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001601{
1602 /* Connection source settings copy */
1603 srv_conn_src_cpy(srv, src);
1604
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001605 if (srv_tmpl) {
1606 srv->addr = src->addr;
1607 srv->svc_port = src->svc_port;
1608 }
1609
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001610 srv->pp_opts = src->pp_opts;
1611 if (src->rdr_pfx != NULL) {
1612 srv->rdr_pfx = strdup(src->rdr_pfx);
1613 srv->rdr_len = src->rdr_len;
1614 }
1615 if (src->cookie != NULL) {
1616 srv->cookie = strdup(src->cookie);
1617 srv->cklen = src->cklen;
1618 }
1619 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001620 srv->check.addr = src->check.addr;
1621 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001622 srv->check.use_ssl = src->check.use_ssl;
1623 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001624 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001625 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001626 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001627 /* Note: 'flags' field has potentially been already initialized. */
1628 srv->flags |= src->flags;
1629 srv->do_check = src->do_check;
1630 srv->do_agent = src->do_agent;
1631 if (srv->check.port)
1632 srv->flags |= SRV_F_CHECKPORT;
1633 srv->check.inter = src->check.inter;
1634 srv->check.fastinter = src->check.fastinter;
1635 srv->check.downinter = src->check.downinter;
1636 srv->agent.use_ssl = src->agent.use_ssl;
1637 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001638
1639 if (src->agent.tcpcheck_rules) {
1640 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1641 if (srv->agent.tcpcheck_rules) {
1642 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1643 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1644 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1645 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1646 &src->agent.tcpcheck_rules->preset_vars);
1647 }
1648 }
1649
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001650 srv->agent.inter = src->agent.inter;
1651 srv->agent.fastinter = src->agent.fastinter;
1652 srv->agent.downinter = src->agent.downinter;
1653 srv->maxqueue = src->maxqueue;
1654 srv->minconn = src->minconn;
1655 srv->maxconn = src->maxconn;
1656 srv->slowstart = src->slowstart;
1657 srv->observe = src->observe;
1658 srv->onerror = src->onerror;
1659 srv->onmarkeddown = src->onmarkeddown;
1660 srv->onmarkedup = src->onmarkedup;
1661 if (src->trackit != NULL)
1662 srv->trackit = strdup(src->trackit);
1663 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1664 srv->uweight = srv->iweight = src->iweight;
1665
1666 srv->check.send_proxy = src->check.send_proxy;
1667 /* health: up, but will fall down at first failure */
1668 srv->check.rise = srv->check.health = src->check.rise;
1669 srv->check.fall = src->check.fall;
1670
1671 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001672 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1673 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1674 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001675 srv->check.state |= CHK_ST_PAUSED;
1676 srv->check.health = 0;
1677 }
1678
1679 /* health: up but will fall down at first failure */
1680 srv->agent.rise = srv->agent.health = src->agent.rise;
1681 srv->agent.fall = src->agent.fall;
1682
1683 if (src->resolvers_id != NULL)
1684 srv->resolvers_id = strdup(src->resolvers_id);
1685 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001686 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001687 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001688 if (srv->dns_opts.family_prio == AF_UNSPEC)
1689 srv->dns_opts.family_prio = AF_INET6;
1690 memcpy(srv->dns_opts.pref_net,
1691 src->dns_opts.pref_net,
1692 sizeof srv->dns_opts.pref_net);
1693 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1694
1695 srv->init_addr_methods = src->init_addr_methods;
1696 srv->init_addr = src->init_addr;
1697#if defined(USE_OPENSSL)
1698 srv_ssl_settings_cpy(srv, src);
1699#endif
1700#ifdef TCP_USER_TIMEOUT
1701 srv->tcp_ut = src->tcp_ut;
1702#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001703 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001704 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001705 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01001706 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001707 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001708
Olivier Houchard8da5f982017-08-04 18:35:36 +02001709 if (srv_tmpl)
1710 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001711
1712 srv->check.via_socks4 = src->check.via_socks4;
1713 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001714}
1715
William Lallemand313bfd12018-10-26 14:47:32 +02001716struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001717{
1718 struct server *srv;
1719
1720 srv = calloc(1, sizeof *srv);
1721 if (!srv)
1722 return NULL;
1723
1724 srv->obj_type = OBJ_TYPE_SERVER;
1725 srv->proxy = proxy;
1726 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001727 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001728
Emeric Brun52a91d32017-08-31 14:41:55 +02001729 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001730 srv->last_change = now.tv_sec;
1731
Christopher Faulet38290462020-04-21 11:46:40 +02001732 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001733 srv->check.status = HCHK_STATUS_INI;
1734 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001735 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001736 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001737
Christopher Faulet38290462020-04-21 11:46:40 +02001738 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001739 srv->agent.status = HCHK_STATUS_INI;
1740 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001741 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001742 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1743
Willy Tarreau975b1552019-06-06 16:25:55 +02001744 /* please don't put default server settings here, they are set in
1745 * init_default_instance().
1746 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001747 return srv;
1748}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001749
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001750#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1751static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1752 struct server *srv, struct proxy *proxy)
1753{
1754 int ret;
1755 char *err = NULL;
1756
1757 if (!srv->sni_expr)
1758 return 0;
1759
1760 ret = server_parse_sni_expr(srv, proxy, &err);
1761 if (!ret)
1762 return 0;
1763
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001764 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001765 free(err);
1766
1767 return ret;
1768}
1769#endif
1770
1771/*
1772 * Server initializations finalization.
1773 * Initialize health check, agent check and SNI expression if enabled.
1774 * Must not be called for a default server instance.
1775 */
1776static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1777 struct server *srv, struct proxy *px)
1778{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001779#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001780 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001781#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001782
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001783 if (srv->do_check && srv->trackit) {
1784 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1785 file, linenum);
1786 return ERR_ALERT | ERR_FATAL;
1787 }
1788
1789 if (srv->do_agent && !srv->agent.port) {
1790 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1791 file, linenum, srv->id);
1792 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001793 }
1794
1795#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1796 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1797 return ret;
1798#endif
1799
1800 if (srv->flags & SRV_F_BACKUP)
1801 px->srv_bck++;
1802 else
1803 px->srv_act++;
1804 srv_lb_commit_status(srv);
1805
1806 return 0;
1807}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001808
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001809/*
1810 * Parse as much as possible such a range string argument: low[-high]
1811 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1812 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1813 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1814 * Returns 0 if succeeded, -1 if not.
1815 */
1816static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1817{
1818 char *nb_high_arg;
1819
1820 *nb_high = 0;
1821 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001822 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001823
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001824 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001825 *nb_high_arg++ = '\0';
1826 *nb_high = atoi(nb_high_arg);
1827 }
1828 else {
1829 *nb_high += *nb_low;
1830 *nb_low = 1;
1831 }
1832
1833 if (*nb_low < 0 || *nb_high < *nb_low)
1834 return -1;
1835
1836 return 0;
1837}
1838
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001839static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1840{
1841 chunk_printf(&trash, "%s%d", prefix, nb);
1842 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001843 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001844}
1845
1846/*
1847 * Initialize as much as possible servers from <srv> server template.
1848 * Note that a server template is a special server with
1849 * a few different parameters than a server which has
1850 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001851 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001852 * 'srv' template included.
1853 */
1854static int server_template_init(struct server *srv, struct proxy *px)
1855{
1856 int i;
1857 struct server *newsrv;
1858
1859 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 +02001860 newsrv = new_server(px);
1861 if (!newsrv)
1862 goto err;
1863
1864 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001865 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001866#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1867 if (newsrv->sni_expr) {
1868 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1869 if (!newsrv->ssl_ctx.sni)
1870 goto err;
1871 }
1872#endif
1873 /* Set this new server ID. */
1874 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1875
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001876 /* Linked backwards first. This will be restablished after parsing. */
1877 newsrv->next = px->srv;
1878 px->srv = newsrv;
1879 }
1880 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1881
1882 return i - srv->tmpl_info.nb_low;
1883
1884 err:
1885 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1886 if (newsrv) {
1887#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1888 release_sample_expr(newsrv->ssl_ctx.sni);
1889#endif
1890 free_check(&newsrv->agent);
1891 free_check(&newsrv->check);
1892 }
1893 free(newsrv);
1894 return i - srv->tmpl_info.nb_low;
1895}
1896
Emeric Brund3db3842020-07-21 16:54:36 +02001897int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy,
1898 struct proxy *defproxy, int parse_addr, int in_peers_section, int initial_resolve)
Willy Tarreau272adea2014-03-31 10:39:59 +02001899{
1900 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001901 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001902 char *errmsg = NULL;
1903 int err_code = 0;
1904 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001905 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001906
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001907 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001908 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001909 !strcmp(args[0], "default-server") ||
1910 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001911 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001912 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001913 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001914 int srv_tmpl = !defsrv && !srv;
1915 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001916
1917 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001918 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001919 err_code |= ERR_ALERT | ERR_FATAL;
1920 goto out;
1921 }
1922 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001923 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001924
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001925 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001926 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001927 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001928 if (in_peers_section) {
1929 return 0;
1930 }
1931 else {
1932 /* 'server' line number of argument check. */
1933 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1934 file, linenum, args[0]);
1935 err_code |= ERR_ALERT | ERR_FATAL;
1936 goto out;
1937 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001938 }
1939
1940 err = invalid_char(args[1]);
1941 }
1942 else if (srv_tmpl) {
1943 if (!*args[3]) {
1944 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001945 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 +02001946 file, linenum, args[0]);
1947 err_code |= ERR_ALERT | ERR_FATAL;
1948 goto out;
1949 }
1950
1951 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001952 }
1953
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001955 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 +02001956 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001957 err_code |= ERR_ALERT | ERR_FATAL;
1958 goto out;
1959 }
1960
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001961 cur_arg = 2;
1962 if (srv_tmpl) {
1963 /* Parse server-template <nb | range> arg. */
1964 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001965 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001966 file, linenum, args[0], args[cur_arg]);
1967 err_code |= ERR_ALERT | ERR_FATAL;
1968 goto out;
1969 }
1970 cur_arg++;
1971 }
1972
Willy Tarreau272adea2014-03-31 10:39:59 +02001973 if (!defsrv) {
1974 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001975 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001976
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001977 newsrv = new_server(curproxy);
1978 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001979 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02001980 err_code |= ERR_ALERT | ERR_ABORT;
1981 goto out;
1982 }
1983
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001984 if (srv_tmpl) {
1985 newsrv->tmpl_info.nb_low = tmpl_range_low;
1986 newsrv->tmpl_info.nb_high = tmpl_range_high;
1987 }
1988
Willy Tarreau272adea2014-03-31 10:39:59 +02001989 /* the servers are linked backwards first */
1990 newsrv->next = curproxy->srv;
1991 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001992 newsrv->conf.file = strdup(file);
1993 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001994 /* Note: for a server template, its id is its prefix.
1995 * This is a temporary id which will be used for server allocations to come
1996 * after parsing.
1997 */
1998 if (srv)
1999 newsrv->id = strdup(args[1]);
2000 else
2001 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002002
2003 /* several ways to check the port component :
2004 * - IP => port=+0, relative (IPv4 only)
2005 * - IP: => port=+0, relative
2006 * - IP:N => port=N, absolute
2007 * - IP:+N => port=+N, relative
2008 * - IP:-N => port=-N, relative
2009 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002010 if (!parse_addr)
2011 goto skip_addr;
2012
Willy Tarreau65ec4e32020-09-16 19:17:08 +02002013 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, NULL, NULL,
2014 &errmsg, NULL, &fqdn,
2015 (initial_resolve ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | PA_O_PORT_OFS | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
Willy Tarreau272adea2014-03-31 10:39:59 +02002016 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002017 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002018 err_code |= ERR_ALERT | ERR_FATAL;
2019 goto out;
2020 }
2021
Willy Tarreau272adea2014-03-31 10:39:59 +02002022 if (!port1 || !port2) {
2023 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002024 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002025 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002026
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002027 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002028 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002029 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002030 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002031 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2032 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2033 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002034 err_code |= ERR_ALERT | ERR_FATAL;
2035 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002036 }
2037 }
2038 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002039 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002040 file, linenum, newsrv->id);
2041 err_code |= ERR_ALERT | ERR_FATAL;
2042 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002043 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002044 }
2045
Willy Tarreau272adea2014-03-31 10:39:59 +02002046 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002047 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002048
Olivier Houchard8da5f982017-08-04 18:35:36 +02002049 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002050 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002051 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002052 err_code |= ERR_ALERT | ERR_FATAL;
2053 goto out;
2054 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002055
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002056 cur_arg++;
2057 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002058 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002059 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002060 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002061 } else {
2062 newsrv = &curproxy->defsrv;
2063 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002064 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002065 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002066 }
2067
2068 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002069 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002070 char *p, *end;
2071 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002072 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002073
2074 newsrv->init_addr_methods = 0;
2075 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2076
2077 for (p = args[cur_arg + 1]; *p; p = end) {
2078 /* cut on next comma */
2079 for (end = p; *end && *end != ','; end++);
2080 if (*end)
2081 *(end++) = 0;
2082
Willy Tarreau4310d362016-11-02 15:05:56 +01002083 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002084 if (!strcmp(p, "libc")) {
2085 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2086 }
2087 else if (!strcmp(p, "last")) {
2088 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2089 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002090 else if (!strcmp(p, "none")) {
2091 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2092 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002093 else if (str2ip2(p, &sa, 0)) {
2094 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002095 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002096 file, linenum, args[cur_arg], p);
2097 err_code |= ERR_ALERT | ERR_FATAL;
2098 goto out;
2099 }
2100 newsrv->init_addr = sa;
2101 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2102 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002103 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002104 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 +02002105 file, linenum, args[cur_arg], p);
2106 err_code |= ERR_ALERT | ERR_FATAL;
2107 goto out;
2108 }
2109 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002110 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002111 file, linenum, args[cur_arg], p);
2112 err_code |= ERR_ALERT | ERR_FATAL;
2113 goto out;
2114 }
2115 }
2116 cur_arg += 2;
2117 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002118 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002119 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002120 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2121 cur_arg += 2;
2122 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002123 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2124 char *p, *end;
2125
2126 for (p = args[cur_arg + 1]; *p; p = end) {
2127 /* cut on next comma */
2128 for (end = p; *end && *end != ','; end++);
2129 if (*end)
2130 *(end++) = 0;
2131
2132 if (!strcmp(p, "allow-dup-ip")) {
2133 newsrv->dns_opts.accept_duplicate_ip = 1;
2134 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002135 else if (!strcmp(p, "ignore-weight")) {
2136 newsrv->dns_opts.ignore_weight = 1;
2137 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002138 else if (!strcmp(p, "prevent-dup-ip")) {
2139 newsrv->dns_opts.accept_duplicate_ip = 0;
2140 }
2141 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002142 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 +02002143 file, linenum, args[cur_arg], p);
2144 err_code |= ERR_ALERT | ERR_FATAL;
2145 goto out;
2146 }
2147 }
2148
2149 cur_arg += 2;
2150 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002151 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2152 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002153 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002154 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002155 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002156 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002157 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002158 file, linenum, args[cur_arg]);
2159 err_code |= ERR_ALERT | ERR_FATAL;
2160 goto out;
2161 }
2162 cur_arg += 2;
2163 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002164 else if (!strcmp(args[cur_arg], "resolve-net")) {
2165 char *p, *e;
2166 unsigned char mask;
2167 struct dns_options *opt;
2168
2169 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002170 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002171 file, linenum, args[cur_arg]);
2172 err_code |= ERR_ALERT | ERR_FATAL;
2173 goto out;
2174 }
2175
2176 opt = &newsrv->dns_opts;
2177
2178 /* Split arguments by comma, and convert it from ipv4 or ipv6
2179 * string network in in_addr or in6_addr.
2180 */
2181 p = args[cur_arg + 1];
2182 e = p;
2183 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002184 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002185 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002186 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002187 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
2191 /* look for end or comma. */
2192 while (*e != ',' && *e != '\0')
2193 e++;
2194 if (*e == ',') {
2195 *e = '\0';
2196 e++;
2197 }
2198 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2199 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2200 /* Try to convert input string from ipv4 or ipv6 network. */
2201 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2202 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2203 &mask)) {
2204 /* Try to convert input string from ipv6 network. */
2205 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2206 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2207 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002208 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002209 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002210 file, linenum, args[cur_arg], p);
2211 err_code |= ERR_ALERT | ERR_FATAL;
2212 goto out;
2213 }
2214 opt->pref_net_nb++;
2215 p = e;
2216 }
2217
2218 cur_arg += 2;
2219 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002220 else if (!strcmp(args[cur_arg], "weight")) {
2221 int w;
2222 w = atol(args[cur_arg + 1]);
2223 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002224 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002225 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2226 err_code |= ERR_ALERT | ERR_FATAL;
2227 goto out;
2228 }
2229 newsrv->uweight = newsrv->iweight = w;
2230 cur_arg += 2;
2231 }
Emeric Brun97556472020-05-30 01:42:45 +02002232 else if (!strcmp(args[cur_arg], "log-proto")) {
2233 if (!strcmp(args[cur_arg + 1], "legacy"))
2234 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2235 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2236 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2237 else {
2238 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2239 "'octet-count' but got '%s'\n",
2240 file, linenum, args[cur_arg], args[cur_arg + 1]);
2241 err_code |= ERR_ALERT | ERR_FATAL;
2242 goto out;
2243 }
2244 cur_arg += 2;
2245 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002246 else if (!strcmp(args[cur_arg], "minconn")) {
2247 newsrv->minconn = atol(args[cur_arg + 1]);
2248 cur_arg += 2;
2249 }
2250 else if (!strcmp(args[cur_arg], "maxconn")) {
2251 newsrv->maxconn = atol(args[cur_arg + 1]);
2252 cur_arg += 2;
2253 }
2254 else if (!strcmp(args[cur_arg], "maxqueue")) {
2255 newsrv->maxqueue = atol(args[cur_arg + 1]);
2256 cur_arg += 2;
2257 }
2258 else if (!strcmp(args[cur_arg], "slowstart")) {
2259 /* slowstart is stored in seconds */
2260 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002261
2262 if (err == PARSE_TIME_OVER) {
2263 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2264 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2265 err_code |= ERR_ALERT | ERR_FATAL;
2266 goto out;
2267 }
2268 else if (err == PARSE_TIME_UNDER) {
2269 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2270 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2271 err_code |= ERR_ALERT | ERR_FATAL;
2272 goto out;
2273 }
2274 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002275 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002276 file, linenum, *err, newsrv->id);
2277 err_code |= ERR_ALERT | ERR_FATAL;
2278 goto out;
2279 }
2280 newsrv->slowstart = (val + 999) / 1000;
2281 cur_arg += 2;
2282 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002283 else if (!strcmp(args[cur_arg], "on-error")) {
2284 if (!strcmp(args[cur_arg + 1], "fastinter"))
2285 newsrv->onerror = HANA_ONERR_FASTINTER;
2286 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2287 newsrv->onerror = HANA_ONERR_FAILCHK;
2288 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2289 newsrv->onerror = HANA_ONERR_SUDDTH;
2290 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2291 newsrv->onerror = HANA_ONERR_MARKDWN;
2292 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002293 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002294 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2295 file, linenum, args[cur_arg], args[cur_arg + 1]);
2296 err_code |= ERR_ALERT | ERR_FATAL;
2297 goto out;
2298 }
2299
2300 cur_arg += 2;
2301 }
2302 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2303 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2304 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2305 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002306 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002307 file, linenum, args[cur_arg], args[cur_arg + 1]);
2308 err_code |= ERR_ALERT | ERR_FATAL;
2309 goto out;
2310 }
2311
2312 cur_arg += 2;
2313 }
2314 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2315 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2316 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2317 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002318 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002319 file, linenum, args[cur_arg], args[cur_arg + 1]);
2320 err_code |= ERR_ALERT | ERR_FATAL;
2321 goto out;
2322 }
2323
2324 cur_arg += 2;
2325 }
2326 else if (!strcmp(args[cur_arg], "error-limit")) {
2327 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002328 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002329 file, linenum, args[cur_arg]);
2330 err_code |= ERR_ALERT | ERR_FATAL;
2331 goto out;
2332 }
2333
2334 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2335
2336 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002337 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002338 file, linenum, args[cur_arg]);
2339 err_code |= ERR_ALERT | ERR_FATAL;
2340 goto out;
2341 }
2342 cur_arg += 2;
2343 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002344 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002345 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002346 file, linenum, "usesrc", "source");
2347 err_code |= ERR_ALERT | ERR_FATAL;
2348 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002349 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 else {
2351 static int srv_dumped;
2352 struct srv_kw *kw;
2353 char *err;
2354
2355 kw = srv_find_kw(args[cur_arg]);
2356 if (kw) {
2357 char *err = NULL;
2358 int code;
2359
2360 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002361 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 +02002362 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002363 if (kw->skip != -1)
2364 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368
2369 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002370 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002371 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002372 if (kw->skip != -1)
2373 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002374 err_code |= ERR_ALERT;
2375 continue;
2376 }
2377
2378 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2379 err_code |= code;
2380
2381 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002382 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002383 if (code & ERR_FATAL) {
2384 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002385 if (kw->skip != -1)
2386 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002387 goto out;
2388 }
2389 }
2390 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002391 if (kw->skip != -1)
2392 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 continue;
2394 }
2395
2396 err = NULL;
2397 if (!srv_dumped) {
2398 srv_dump_kws(&err);
2399 indent_msg(&err, 4);
2400 srv_dumped = 1;
2401 }
2402
Christopher Faulet767a84b2017-11-24 16:50:31 +01002403 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002404 file, linenum, args[0], args[1], args[cur_arg],
2405 err ? " Registered keywords :" : "", err ? err : "");
2406 free(err);
2407
2408 err_code |= ERR_ALERT | ERR_FATAL;
2409 goto out;
2410 }
2411 }
2412
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002413 if (!defsrv)
2414 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2415 if (err_code & ERR_FATAL)
2416 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002417 if (srv_tmpl)
2418 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002420 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002421 return 0;
2422
2423 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002424 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002425 free(errmsg);
2426 return err_code;
2427}
2428
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002429/* Returns a pointer to the first server matching either id <id>.
2430 * NULL is returned if no match is found.
2431 * the lookup is performed in the backend <bk>
2432 */
2433struct server *server_find_by_id(struct proxy *bk, int id)
2434{
2435 struct eb32_node *eb32;
2436 struct server *curserver;
2437
2438 if (!bk || (id ==0))
2439 return NULL;
2440
2441 /* <bk> has no backend capabilities, so it can't have a server */
2442 if (!(bk->cap & PR_CAP_BE))
2443 return NULL;
2444
2445 curserver = NULL;
2446
2447 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2448 if (eb32)
2449 curserver = container_of(eb32, struct server, conf.id);
2450
2451 return curserver;
2452}
2453
2454/* Returns a pointer to the first server matching either name <name>, or id
2455 * if <name> starts with a '#'. NULL is returned if no match is found.
2456 * the lookup is performed in the backend <bk>
2457 */
2458struct server *server_find_by_name(struct proxy *bk, const char *name)
2459{
2460 struct server *curserver;
2461
2462 if (!bk || !name)
2463 return NULL;
2464
2465 /* <bk> has no backend capabilities, so it can't have a server */
2466 if (!(bk->cap & PR_CAP_BE))
2467 return NULL;
2468
2469 curserver = NULL;
2470 if (*name == '#') {
2471 curserver = server_find_by_id(bk, atoi(name + 1));
2472 if (curserver)
2473 return curserver;
2474 }
2475 else {
2476 curserver = bk->srv;
2477
2478 while (curserver && (strcmp(curserver->id, name) != 0))
2479 curserver = curserver->next;
2480
2481 if (curserver)
2482 return curserver;
2483 }
2484
2485 return NULL;
2486}
2487
2488struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2489{
2490 struct server *byname;
2491 struct server *byid;
2492
2493 if (!name && !id)
2494 return NULL;
2495
2496 if (diff)
2497 *diff = 0;
2498
2499 byname = byid = NULL;
2500
2501 if (name) {
2502 byname = server_find_by_name(bk, name);
2503 if (byname && (!id || byname->puid == id))
2504 return byname;
2505 }
2506
2507 /* remaining possibilities :
2508 * - name not set
2509 * - name set but not found
2510 * - name found but ID doesn't match
2511 */
2512 if (id) {
2513 byid = server_find_by_id(bk, id);
2514 if (byid) {
2515 if (byname) {
2516 /* use id only if forced by configuration */
2517 if (byid->flags & SRV_F_FORCED_ID) {
2518 if (diff)
2519 *diff |= 2;
2520 return byid;
2521 }
2522 else {
2523 if (diff)
2524 *diff |= 1;
2525 return byname;
2526 }
2527 }
2528
2529 /* remaining possibilities:
2530 * - name not set
2531 * - name set but not found
2532 */
2533 if (name && diff)
2534 *diff |= 2;
2535 return byid;
2536 }
2537
2538 /* id bot found */
2539 if (byname) {
2540 if (diff)
2541 *diff |= 1;
2542 return byname;
2543 }
2544 }
2545
2546 return NULL;
2547}
2548
Willy Tarreau46b7f532018-08-21 11:54:26 +02002549/* Update a server state using the parameters available in the params list.
2550 *
2551 * Grabs the server lock during operation.
2552 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002553static void srv_update_state(struct server *srv, int version, char **params)
2554{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002555 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002556 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002557
2558 /* fields since version 1
2559 * and common to all other upcoming versions
2560 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002561 enum srv_state srv_op_state;
2562 enum srv_admin srv_admin_state;
2563 unsigned srv_uweight, srv_iweight;
2564 unsigned long srv_last_time_change;
2565 short srv_check_status;
2566 enum chk_result srv_check_result;
2567 int srv_check_health;
2568 int srv_check_state, srv_agent_state;
2569 int bk_f_forced_id;
2570 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002571 int fqdn_set_by_cli;
2572 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002573 const char *port_str;
2574 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002575 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002576
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002577 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002578 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002579 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002580 switch (version) {
2581 case 1:
2582 /*
2583 * now we can proceed with server's state update:
2584 * srv_addr: params[0]
2585 * srv_op_state: params[1]
2586 * srv_admin_state: params[2]
2587 * srv_uweight: params[3]
2588 * srv_iweight: params[4]
2589 * srv_last_time_change: params[5]
2590 * srv_check_status: params[6]
2591 * srv_check_result: params[7]
2592 * srv_check_health: params[8]
2593 * srv_check_state: params[9]
2594 * srv_agent_state: params[10]
2595 * bk_f_forced_id: params[11]
2596 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002597 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002598 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002599 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002600 */
2601
2602 /* validating srv_op_state */
2603 p = NULL;
2604 errno = 0;
2605 srv_op_state = strtol(params[1], &p, 10);
2606 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2607 (srv_op_state != SRV_ST_STOPPED &&
2608 srv_op_state != SRV_ST_STARTING &&
2609 srv_op_state != SRV_ST_RUNNING &&
2610 srv_op_state != SRV_ST_STOPPING)) {
2611 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2612 }
2613
2614 /* validating srv_admin_state */
2615 p = NULL;
2616 errno = 0;
2617 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002618 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002619
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002620 /* inherited statuses will be recomputed later.
2621 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2622 */
2623 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002624
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002625 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2626 (srv_admin_state != 0 &&
2627 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002628 srv_admin_state != SRV_ADMF_CMAINT &&
2629 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002630 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002631 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002632 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2633 }
2634
2635 /* validating srv_uweight */
2636 p = NULL;
2637 errno = 0;
2638 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002639 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002640 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2641
2642 /* validating srv_iweight */
2643 p = NULL;
2644 errno = 0;
2645 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002646 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002647 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2648
2649 /* validating srv_last_time_change */
2650 p = NULL;
2651 errno = 0;
2652 srv_last_time_change = strtol(params[5], &p, 10);
2653 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2654 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2655
2656 /* validating srv_check_status */
2657 p = NULL;
2658 errno = 0;
2659 srv_check_status = strtol(params[6], &p, 10);
2660 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2661 (srv_check_status >= HCHK_STATUS_SIZE))
2662 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2663
2664 /* validating srv_check_result */
2665 p = NULL;
2666 errno = 0;
2667 srv_check_result = strtol(params[7], &p, 10);
2668 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2669 (srv_check_result != CHK_RES_UNKNOWN &&
2670 srv_check_result != CHK_RES_NEUTRAL &&
2671 srv_check_result != CHK_RES_FAILED &&
2672 srv_check_result != CHK_RES_PASSED &&
2673 srv_check_result != CHK_RES_CONDPASS)) {
2674 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2675 }
2676
2677 /* validating srv_check_health */
2678 p = NULL;
2679 errno = 0;
2680 srv_check_health = strtol(params[8], &p, 10);
2681 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2682 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2683
2684 /* validating srv_check_state */
2685 p = NULL;
2686 errno = 0;
2687 srv_check_state = strtol(params[9], &p, 10);
2688 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2689 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2690 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2691
2692 /* validating srv_agent_state */
2693 p = NULL;
2694 errno = 0;
2695 srv_agent_state = strtol(params[10], &p, 10);
2696 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2697 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2698 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2699
2700 /* validating bk_f_forced_id */
2701 p = NULL;
2702 errno = 0;
2703 bk_f_forced_id = strtol(params[11], &p, 10);
2704 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2705 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2706
2707 /* validating srv_f_forced_id */
2708 p = NULL;
2709 errno = 0;
2710 srv_f_forced_id = strtol(params[12], &p, 10);
2711 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2712 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2713
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002714 /* validating srv_fqdn */
2715 fqdn = params[13];
2716 if (fqdn && *fqdn == '-')
2717 fqdn = NULL;
2718 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2719 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2720 fqdn = NULL;
2721 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002722
Frédéric Lécaille31694712017-08-01 08:47:19 +02002723 port_str = params[14];
2724 if (port_str) {
2725 port = strl2uic(port_str, strlen(port_str));
2726 if (port > USHRT_MAX) {
2727 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2728 port_str = NULL;
2729 }
2730 }
2731
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002732 /* SRV record
2733 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2734 */
2735 srvrecord = params[15];
2736 if (srvrecord && *srvrecord != '_')
2737 srvrecord = NULL;
2738
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002739 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002741 goto out;
2742
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002743 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002744 /* recover operational state and apply it to this server
2745 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002746 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002747 switch (srv_op_state) {
2748 case SRV_ST_STOPPED:
2749 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002750 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002751 break;
2752 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002753 /* If rise == 1 there is no STARTING state, let's switch to
2754 * RUNNING
2755 */
2756 if (srv->check.rise == 1) {
2757 srv->check.health = srv->check.rise + srv->check.fall - 1;
2758 srv_set_running(srv, "", NULL);
2759 break;
2760 }
2761 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2762 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002763 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002764 break;
2765 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002766 /* If fall == 1 there is no STOPPING state, let's switch to
2767 * STOPPED
2768 */
2769 if (srv->check.fall == 1) {
2770 srv->check.health = 0;
2771 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2772 break;
2773 }
2774 if (srv->check.health < srv->check.rise ||
2775 srv->check.health > srv->check.rise + srv->check.fall - 2)
2776 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002777 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002778 break;
2779 case SRV_ST_RUNNING:
2780 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002781 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002782 break;
2783 }
2784
2785 /* When applying server state, the following rules apply:
2786 * - in case of a configuration change, we apply the setting from the new
2787 * configuration, regardless of old running state
2788 * - if no configuration change, we apply old running state only if old running
2789 * state is different from new configuration state
2790 */
2791 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002792 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2793 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002794 srv_adm_set_maint(srv);
2795 else
2796 srv_adm_set_ready(srv);
2797 }
2798 /* configuration is the same, let's compate old running state and new conf state */
2799 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002800 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002801 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002802 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002803 srv_adm_set_ready(srv);
2804 }
2805 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002806 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002807 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002808 * (srv->iweight is the weight set up in configuration).
2809 * There are two possible reasons for FDRAIN to have been present :
2810 * - previous config weight was zero
2811 * - "set server b/s drain" was sent to the CLI
2812 *
2813 * In the first case, we simply want to drop this drain state
2814 * if the new weight is not zero anymore, meaning the administrator
2815 * has intentionally turned the weight back to a positive value to
2816 * enable the server again after an operation. In the second case,
2817 * the drain state was forced on the CLI regardless of the config's
2818 * weight so we don't want a change to the config weight to lose this
2819 * status. What this means is :
2820 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2821 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002822 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002823 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002824 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002825 }
2826
2827 srv->last_change = date.tv_sec - srv_last_time_change;
2828 srv->check.status = srv_check_status;
2829 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002830
2831 /* Only case we want to apply is removing ENABLED flag which could have been
2832 * done by the "disable health" command over the stats socket
2833 */
2834 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2835 (srv_check_state & CHK_ST_CONFIGURED) &&
2836 !(srv_check_state & CHK_ST_ENABLED))
2837 srv->check.state &= ~CHK_ST_ENABLED;
2838
2839 /* Only case we want to apply is removing ENABLED flag which could have been
2840 * done by the "disable agent" command over the stats socket
2841 */
2842 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2843 (srv_agent_state & CHK_ST_CONFIGURED) &&
2844 !(srv_agent_state & CHK_ST_ENABLED))
2845 srv->agent.state &= ~CHK_ST_ENABLED;
2846
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002847 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2848 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002849 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002850 * It means that a configuration file change has precedence over a unix socket change
2851 * for server's weight
2852 *
2853 * by default, HAProxy applies the following weight when parsing the configuration
2854 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002855 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002856 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002857 srv->uweight = srv_uweight;
2858 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002859 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002860
Willy Tarreaue5a60682016-11-09 14:54:53 +01002861 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002862 if (strcmp(params[0], "-"))
2863 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002864
2865 if (fqdn && srv->hostname) {
2866 if (!strcmp(srv->hostname, fqdn)) {
2867 /* Here we reset the 'set from stats socket FQDN' flag
2868 * to support such transitions:
2869 * Let's say initial FQDN value is foo1 (in configuration file).
2870 * - FQDN changed from stats socket, from foo1 to foo2 value,
2871 * - FQDN changed again from file configuration (with the same previous value
2872 set from stats socket, from foo1 to foo2 value),
2873 * - reload for any other reason than a FQDN modification,
2874 * the configuration file FQDN matches the fqdn server state file value.
2875 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002876 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002877 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002878 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002879 }
2880 else {
2881 /* If the FDQN has been changed from stats socket,
2882 * apply fqdn state file value (which is the value set
2883 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002884 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002885 */
2886 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002887 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002888 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002889 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002890 }
2891 }
2892 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002893 /* If all the conditions below are validated, this means
2894 * we're evaluating a server managed by SRV resolution
2895 */
2896 else if (fqdn && !srv->hostname && srvrecord) {
2897 int res;
2898
2899 /* we can't apply previous state if SRV record has changed */
2900 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2901 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);
2902 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2903 goto out;
2904 }
2905
2906 /* create or find a SRV resolution for this srv record */
2907 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2908 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2909 if (srv->srvrq == NULL) {
2910 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2911 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2912 goto out;
2913 }
2914
2915 /* prepare DNS resolution for this server */
2916 res = srv_prepare_for_resolution(srv, fqdn);
2917 if (res == -1) {
2918 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2919 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2920 goto out;
2921 }
2922
2923 /* configure check.port accordingly */
2924 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2925 !(srv->flags & SRV_F_CHECKPORT))
2926 srv->check.port = port;
2927
2928 /* Unset SRV_F_MAPPORTS for SRV records.
2929 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2930 * because no ports are provided in the configuration file.
2931 * This is because HAProxy will use the port found into the SRV record.
2932 */
2933 srv->flags &= ~SRV_F_MAPPORTS;
2934 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002935
Frédéric Lécaille31694712017-08-01 08:47:19 +02002936 if (port_str)
2937 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002938 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002939
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002940 break;
2941 default:
2942 chunk_appendf(msg, ", version '%d' not supported", version);
2943 }
2944
2945 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002946 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002947 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002948 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002949 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002950 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002951}
2952
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002953
2954/*
2955 * read next line from file <f> and return the server state version if one found.
2956 * If no version is found, then 0 is returned
2957 * Note that this should be the first read on <f>
2958 */
2959static int srv_state_get_version(FILE *f) {
2960 char buf[2];
2961 int ret;
2962
2963 /* first character of first line of the file must contain the version of the export */
2964 if (fgets(buf, 2, f) == NULL) {
2965 return 0;
2966 }
2967
2968 ret = atoi(buf);
2969 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
2970 (ret > SRV_STATE_FILE_VERSION_MAX))
2971 return 0;
2972
2973 return ret;
2974}
2975
2976
2977/*
2978 * parses server state line stored in <buf> and supposedly in version <version>.
2979 * Set <params> and <srv_params> accordingly.
2980 * In case of error, params[0] is set to NULL.
2981 */
2982static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
2983{
2984 int buflen, arg, srv_arg;
2985 char *cur, *end;
2986
2987 buflen = strlen(buf);
2988 cur = buf;
2989 end = cur + buflen;
2990
2991 /* we need at least one character */
2992 if (buflen == 0) {
2993 params[0] = NULL;
2994 return;
2995 }
2996
2997 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01002998 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002999 ++cur;
3000
3001 /* Ignore empty or commented lines */
3002 if (cur == end || *cur == '#') {
3003 params[0] = NULL;
3004 return;
3005 }
3006
3007 /* truncated lines */
3008 if (buf[buflen - 1] != '\n') {
3009 //ha_warning("server-state file '%s': truncated line\n", filepath);
3010 params[0] = NULL;
3011 return;
3012 }
3013
3014 /* Removes trailing '\n' */
3015 buf[buflen - 1] = '\0';
3016
3017 /* we're now ready to move the line into *srv_params[] */
3018 params[0] = cur;
3019 arg = 1;
3020 srv_arg = 0;
3021 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003022 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003023 *cur = '\0';
3024 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003025 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003026 ++cur;
3027 switch (version) {
3028 case 1:
3029 /*
3030 * srv_addr: params[4] => srv_params[0]
3031 * srv_op_state: params[5] => srv_params[1]
3032 * srv_admin_state: params[6] => srv_params[2]
3033 * srv_uweight: params[7] => srv_params[3]
3034 * srv_iweight: params[8] => srv_params[4]
3035 * srv_last_time_change: params[9] => srv_params[5]
3036 * srv_check_status: params[10] => srv_params[6]
3037 * srv_check_result: params[11] => srv_params[7]
3038 * srv_check_health: params[12] => srv_params[8]
3039 * srv_check_state: params[13] => srv_params[9]
3040 * srv_agent_state: params[14] => srv_params[10]
3041 * bk_f_forced_id: params[15] => srv_params[11]
3042 * srv_f_forced_id: params[16] => srv_params[12]
3043 * srv_fqdn: params[17] => srv_params[13]
3044 * srv_port: params[18] => srv_params[14]
3045 * srvrecord: params[19] => srv_params[15]
3046 */
3047 if (arg >= 4) {
3048 srv_params[srv_arg] = cur;
3049 ++srv_arg;
3050 }
3051 break;
3052 }
3053
3054 params[arg] = cur;
3055 ++arg;
3056 }
3057 else {
3058 ++cur;
3059 }
3060 }
3061
3062 /* if line is incomplete line, then ignore it.
3063 * otherwise, update useful flags */
3064 switch (version) {
3065 case 1:
3066 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3067 params[0] = NULL;
3068 return;
3069 }
3070 break;
3071 }
3072
3073 return;
3074}
3075
3076
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003077/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3078 * For each proxy, it does the following:
3079 * - opens its server state file (either one or local one)
3080 * - read whole file, line by line
3081 * - analyse each line to check if it matches our current backend:
3082 * - backend name matches
3083 * - backend id matches if id is forced and name doesn't match
3084 * - if the server pointed by the line is found, then state is applied
3085 *
3086 * If the running backend uuid or id differs from the state file, then HAProxy reports
3087 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003088 *
3089 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003090 */
3091void apply_server_state(void)
3092{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003093 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003094 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3095 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003096 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003097 FILE *f;
3098 char *filepath;
3099 char globalfilepath[MAXPATHLEN + 1];
3100 char localfilepath[MAXPATHLEN + 1];
3101 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003102 struct proxy *curproxy, *bk;
3103 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003104 char *line;
3105 char *bkname, *srvname;
3106 struct state_line *st;
3107 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003108
Willy Tarreau2d067f92020-07-16 06:40:27 +02003109 f = NULL;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003110 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003111 globalfilepathlen = 0;
3112 /* create the globalfilepath variable */
3113 if (global.server_state_file) {
3114 /* absolute path or no base directory provided */
3115 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3116 len = strlen(global.server_state_file);
3117 if (len > MAXPATHLEN) {
3118 globalfilepathlen = 0;
3119 goto globalfileerror;
3120 }
3121 memcpy(globalfilepath, global.server_state_file, len);
3122 globalfilepath[len] = '\0';
3123 globalfilepathlen = len;
3124 }
3125 else if (global.server_state_base) {
3126 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003127 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003128 globalfilepathlen = 0;
3129 goto globalfileerror;
3130 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003131 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003132 globalfilepath[len] = 0;
3133 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003134
3135 /* append a slash if needed */
3136 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3137 if (globalfilepathlen + 1 > MAXPATHLEN) {
3138 globalfilepathlen = 0;
3139 goto globalfileerror;
3140 }
3141 globalfilepath[globalfilepathlen++] = '/';
3142 }
3143
3144 len = strlen(global.server_state_file);
3145 if (globalfilepathlen + len > MAXPATHLEN) {
3146 globalfilepathlen = 0;
3147 goto globalfileerror;
3148 }
3149 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3150 globalfilepathlen += len;
3151 globalfilepath[globalfilepathlen++] = 0;
3152 }
3153 }
3154 globalfileerror:
3155 if (globalfilepathlen == 0)
3156 globalfilepath[0] = '\0';
3157
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003158 /* Load global server state in a tree */
3159 if (globalfilepathlen > 0) {
3160 errno = 0;
3161 f = fopen(globalfilepath, "r");
3162 if (errno)
3163 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003164 if (!f)
3165 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003166
3167 global_file_version = srv_state_get_version(f);
3168 if (global_file_version == 0)
3169 goto out_load_server_state_in_tree;
3170
3171 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3172 line = NULL;
3173
3174 line = strdup(mybuf);
3175 if (line == NULL)
3176 continue;
3177
3178 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3179 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003180 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003181
3182 /* bkname */
3183 bkname = params[1];
3184 /* srvname */
3185 srvname = params[3];
3186
3187 /* key */
3188 chunk_printf(&trash, "%s %s", bkname, srvname);
3189
3190 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003191 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003192 if (st == NULL) {
3193 goto nextline;
3194 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003195 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003196 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3197 /* this is a duplicate key, probably a hand-crafted file,
3198 * drop it!
3199 */
3200 goto nextline;
3201 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003202
3203 /* save line */
3204 st->line = line;
3205
3206 continue;
3207
3208 nextline:
3209 /* free up memory in case of error during the processing of the line */
3210 free(line);
3211 }
3212 }
3213 out_load_server_state_in_tree:
3214
Ilya Shipitsindc6e8a92020-07-16 02:02:40 +05003215 if (f) {
3216 fclose(f);
3217 f = NULL;
3218 }
3219
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003220 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003221 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003222 /* servers are only in backends */
3223 if (!(curproxy->cap & PR_CAP_BE))
3224 continue;
3225 fileopenerr = 0;
3226 filepath = NULL;
3227
3228 /* search server state file path and name */
3229 switch (curproxy->load_server_state_from_file) {
3230 /* read servers state from global file */
3231 case PR_SRV_STATE_FILE_GLOBAL:
3232 /* there was an error while generating global server state file path */
3233 if (globalfilepathlen == 0)
3234 continue;
3235 filepath = globalfilepath;
3236 fileopenerr = 1;
3237 break;
3238 /* this backend has its own file */
3239 case PR_SRV_STATE_FILE_LOCAL:
3240 localfilepathlen = 0;
3241 localfilepath[0] = '\0';
3242 len = 0;
3243 /* create the localfilepath variable */
3244 /* absolute path or no base directory provided */
3245 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3246 len = strlen(curproxy->server_state_file_name);
3247 if (len > MAXPATHLEN) {
3248 localfilepathlen = 0;
3249 goto localfileerror;
3250 }
3251 memcpy(localfilepath, curproxy->server_state_file_name, len);
3252 localfilepath[len] = '\0';
3253 localfilepathlen = len;
3254 }
3255 else if (global.server_state_base) {
3256 len = strlen(global.server_state_base);
3257 localfilepathlen += len;
3258
3259 if (localfilepathlen > MAXPATHLEN) {
3260 localfilepathlen = 0;
3261 goto localfileerror;
3262 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003263 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003264 localfilepath[localfilepathlen] = 0;
3265
3266 /* append a slash if needed */
3267 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3268 if (localfilepathlen + 1 > MAXPATHLEN) {
3269 localfilepathlen = 0;
3270 goto localfileerror;
3271 }
3272 localfilepath[localfilepathlen++] = '/';
3273 }
3274
3275 len = strlen(curproxy->server_state_file_name);
3276 if (localfilepathlen + len > MAXPATHLEN) {
3277 localfilepathlen = 0;
3278 goto localfileerror;
3279 }
3280 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3281 localfilepathlen += len;
3282 localfilepath[localfilepathlen++] = 0;
3283 }
3284 filepath = localfilepath;
3285 localfileerror:
3286 if (localfilepathlen == 0)
3287 localfilepath[0] = '\0';
3288
3289 break;
3290 case PR_SRV_STATE_FILE_NONE:
3291 default:
3292 continue;
3293 }
3294
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003295 /* when global file is used, we get data from the tree
3296 * Note that in such case we don't check backend name neither uuid.
3297 * Backend name can't be wrong since it's used as a key to retrieve the server state
3298 * line from the tree.
3299 */
3300 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3301 struct server *srv = curproxy->srv;
3302 while (srv) {
3303 struct ebmb_node *node;
3304 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003305
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003306 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3307 node = ebst_lookup(&state_file, trash.area);
3308 if (!node)
3309 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003310
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003311 st = container_of(node, struct state_line, name_name);
3312 memcpy(mybuf, st->line, strlen(st->line));
3313 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003314
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003315 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3316 if (params[0] == NULL)
3317 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003318
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003319 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003320
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003321 next:
3322 srv = srv->next;
3323 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003324
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003325 continue; /* next proxy in list */
3326 }
3327 else {
3328 /* load 'local' state file */
3329 errno = 0;
3330 f = fopen(filepath, "r");
3331 if (errno && fileopenerr)
3332 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3333 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003334 continue;
3335
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003336 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003337
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003338 /* first character of first line of the file must contain the version of the export */
3339 version = srv_state_get_version(f);
3340 if (version == 0) {
3341 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3342 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003343 }
3344
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003345 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3346 int bk_f_forced_id = 0;
3347 int check_id = 0;
3348 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003349
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003350 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003351
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003352 if (params[0] == NULL) {
3353 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003354 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003355
3356 /* if line is incomplete line, then ignore it.
3357 * otherwise, update useful flags */
3358 switch (version) {
3359 case 1:
3360 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3361 check_id = (atoi(params[0]) == curproxy->uuid);
3362 check_name = (strcmp(curproxy->id, params[1]) == 0);
3363 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003364 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003365
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003366 bk = curproxy;
3367
3368 /* if backend can't be found, let's continue */
3369 if (!check_id && !check_name)
3370 continue;
3371 else if (!check_id && check_name) {
3372 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3373 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3374 }
3375 else if (check_id && !check_name) {
3376 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3377 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3378 /* if name doesn't match, we still want to update curproxy if the backend id
3379 * was forced in previous the previous configuration */
3380 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003381 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003382 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003383
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003384 /* look for the server by its name: param[3] */
3385 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003386
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003387 if (!srv) {
3388 /* if no server found, then warning and continue with next line */
3389 ha_warning("can't find server '%s' in backend '%s'\n",
3390 params[3], params[1]);
3391 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3392 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003393 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003394 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003395
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003396 /* now we can proceed with server's state update */
3397 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003398 }
Ilya Shipitsindc6e8a92020-07-16 02:02:40 +05003399
3400 fileclose:
3401 fclose(f);
3402
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003403 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003404 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003405
3406 /* now free memory allocated for the tree */
3407 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3408 node;
3409 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3410 st = container_of(node, struct state_line, name_name);
3411 ebmb_delete(&st->name_name);
3412 free(st->line);
3413 free(st);
3414 }
3415
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003416}
3417
Simon Horman7d09b9a2013-02-12 10:45:51 +09003418/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003419 * update a server's current IP address.
3420 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3421 * ip is in network format.
3422 * updater is a string which contains an information about the requester of the update.
3423 * updater is used if not NULL.
3424 *
3425 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003426 *
3427 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003428 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003429int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003430{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003431 /* save the new IP family & address if necessary */
3432 switch (ip_sin_family) {
3433 case AF_INET:
3434 if (s->addr.ss_family == ip_sin_family &&
3435 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3436 return 0;
3437 break;
3438 case AF_INET6:
3439 if (s->addr.ss_family == ip_sin_family &&
3440 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3441 return 0;
3442 break;
3443 };
3444
Baptiste Assmann14e40142015-04-14 01:13:07 +02003445 /* generates a log line and a warning on stderr */
3446 if (1) {
3447 /* book enough space for both IPv4 and IPv6 */
3448 char oldip[INET6_ADDRSTRLEN];
3449 char newip[INET6_ADDRSTRLEN];
3450
3451 memset(oldip, '\0', INET6_ADDRSTRLEN);
3452 memset(newip, '\0', INET6_ADDRSTRLEN);
3453
3454 /* copy old IP address in a string */
3455 switch (s->addr.ss_family) {
3456 case AF_INET:
3457 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3458 break;
3459 case AF_INET6:
3460 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3461 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003462 default:
3463 strcpy(oldip, "(none)");
3464 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003465 };
3466
3467 /* copy new IP address in a string */
3468 switch (ip_sin_family) {
3469 case AF_INET:
3470 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3471 break;
3472 case AF_INET6:
3473 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3474 break;
3475 };
3476
3477 /* save log line into a buffer */
3478 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3479 s->proxy->id, s->id, oldip, newip, updater);
3480
3481 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003482 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003483
3484 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003485 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003486 }
3487
3488 /* save the new IP family */
3489 s->addr.ss_family = ip_sin_family;
3490 /* save the new IP address */
3491 switch (ip_sin_family) {
3492 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003493 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003494 break;
3495 case AF_INET6:
3496 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3497 break;
3498 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003499 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003500
3501 return 0;
3502}
3503
3504/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003505 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3506 *
3507 * Caller can pass its name through <updater> to get it integrated in the response message
3508 * returned by the function.
3509 *
3510 * The function first does the following, in that order:
3511 * - validates the new addr and/or port
3512 * - checks if an update is required (new IP or port is different than current ones)
3513 * - checks the update is allowed:
3514 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3515 * - allow all changes if no CHECKS are configured
3516 * - if CHECK is configured:
3517 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3518 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3519 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003520 *
3521 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003522 */
3523const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3524{
3525 struct sockaddr_storage sa;
3526 int ret, port_change_required;
3527 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003528 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003529 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003530 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003531
3532 msg = get_trash_chunk();
3533 chunk_reset(msg);
3534
3535 if (addr) {
3536 memset(&sa, 0, sizeof(struct sockaddr_storage));
3537 if (str2ip2(addr, &sa, 0) == NULL) {
3538 chunk_printf(msg, "Invalid addr '%s'", addr);
3539 goto out;
3540 }
3541
3542 /* changes are allowed on AF_INET* families only */
3543 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3544 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3545 goto out;
3546 }
3547
3548 /* collecting data currently setup */
3549 memset(current_addr, '\0', sizeof(current_addr));
3550 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3551 /* changes are allowed on AF_INET* families only */
3552 if ((ret != AF_INET) && (ret != AF_INET6)) {
3553 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3554 goto out;
3555 }
3556
3557 /* applying ADDR changes if required and allowed
3558 * ipcmp returns 0 when both ADDR are the same
3559 */
3560 if (ipcmp(&s->addr, &sa) == 0) {
3561 chunk_appendf(msg, "no need to change the addr");
3562 goto port;
3563 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003564 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003565 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003566
3567 /* we also need to update check's ADDR only if it uses the server's one */
3568 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003569 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003570 }
3571
3572 /* we also need to update agent ADDR only if it use the server's one */
3573 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003574 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003575 }
3576
3577 /* update report for caller */
3578 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3579 }
3580
3581 port:
3582 if (port) {
3583 char sign = '\0';
3584 char *endptr;
3585
3586 if (addr)
3587 chunk_appendf(msg, ", ");
3588
3589 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003590 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003591
3592 /* check if PORT change is required */
3593 port_change_required = 0;
3594
3595 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003596 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003597 new_port = strtol(port, &endptr, 10);
3598 if ((errno != 0) || (port == endptr)) {
3599 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3600 goto out;
3601 }
3602
3603 /* check if caller triggers a port mapped or offset */
3604 if (sign == '-' || (sign == '+')) {
3605 /* check if server currently uses port map */
3606 if (!(s->flags & SRV_F_MAPPORTS)) {
3607 /* switch from fixed port to port map mandatorily triggers
3608 * a port change */
3609 port_change_required = 1;
3610 /* check is configured
3611 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3612 * prevent PORT change if check doesn't have it's dedicated port while switching
3613 * to port mapping */
3614 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3615 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.");
3616 goto out;
3617 }
3618 }
3619 /* we're already using port maps */
3620 else {
3621 port_change_required = current_port != new_port;
3622 }
3623 }
3624 /* fixed port */
3625 else {
3626 port_change_required = current_port != new_port;
3627 }
3628
3629 /* applying PORT changes if required and update response message */
3630 if (port_change_required) {
3631 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003632 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003633 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003634
3635 /* prepare message */
3636 chunk_appendf(msg, "port changed from '");
3637 if (s->flags & SRV_F_MAPPORTS)
3638 chunk_appendf(msg, "+");
3639 chunk_appendf(msg, "%d' to '", current_port);
3640
3641 if (sign == '-') {
3642 s->flags |= SRV_F_MAPPORTS;
3643 chunk_appendf(msg, "%c", sign);
3644 /* just use for result output */
3645 new_port = -new_port;
3646 }
3647 else if (sign == '+') {
3648 s->flags |= SRV_F_MAPPORTS;
3649 chunk_appendf(msg, "%c", sign);
3650 }
3651 else {
3652 s->flags &= ~SRV_F_MAPPORTS;
3653 }
3654
3655 chunk_appendf(msg, "%d'", new_port);
3656
3657 /* we also need to update health checks port only if it uses server's realport */
3658 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3659 s->check.port = new_port;
3660 }
3661 }
3662 else {
3663 chunk_appendf(msg, "no need to change the port");
3664 }
3665 }
3666
3667out:
William Dauchy6318d332020-05-02 21:52:36 +02003668 if (changed) {
3669 /* force connection cleanup on the given server */
3670 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003671 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003672 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003673 if (updater)
3674 chunk_appendf(msg, " by '%s'", updater);
3675 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003676 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003677}
3678
3679
3680/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003681 * update server status based on result of name resolution
3682 * returns:
3683 * 0 if server status is updated
3684 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003685 *
3686 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003687 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003688int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003689{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003690 struct dns_resolvers *resolvers = s->resolvers;
3691 struct dns_resolution *resolution = s->dns_requester->resolution;
3692 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003693
Jerome Magnin012261a2020-07-28 13:38:22 +02003694 /* If resolution is NULL we're dealing with SRV records Additional records */
3695 if (resolution == NULL) {
Baptiste Assmann87138c32020-08-04 10:57:21 +02003696 /* since this server has an IP, it can go back in production */
3697 if (has_no_ip == 0) {
3698 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3699 return 1;
3700 }
3701
Jerome Magnin012261a2020-07-28 13:38:22 +02003702 if (s->next_admin & SRV_ADMF_RMAINT)
3703 return 1;
3704
3705 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3706 return 0;
3707 }
3708
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003709 switch (resolution->status) {
3710 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003711 /* status when HAProxy has just (re)started.
3712 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003713 break;
3714
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003715 case RSLV_STATUS_VALID:
3716 /*
3717 * resume health checks
3718 * server will be turned back on if health check is safe
3719 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003720 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003721 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003722 return 1;
3723 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3724 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003725 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003726 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003727
Emeric Brun52a91d32017-08-31 14:41:55 +02003728 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003729 return 1;
3730 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3731 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3732 s->proxy->id, s->id);
3733
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003734 ha_warning("%s.\n", trash.area);
3735 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003736 return 0;
3737
3738 case RSLV_STATUS_NX:
3739 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003740 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3741 if (!tick_is_expired(exp, now_ms))
3742 break;
3743
3744 if (s->next_admin & SRV_ADMF_RMAINT)
3745 return 1;
3746 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3747 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003748
3749 case RSLV_STATUS_TIMEOUT:
3750 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003751 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3752 if (!tick_is_expired(exp, now_ms))
3753 break;
3754
3755 if (s->next_admin & SRV_ADMF_RMAINT)
3756 return 1;
3757 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3758 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003759
3760 case RSLV_STATUS_REFUSED:
3761 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003762 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3763 if (!tick_is_expired(exp, now_ms))
3764 break;
3765
3766 if (s->next_admin & SRV_ADMF_RMAINT)
3767 return 1;
3768 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3769 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003770
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003771 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003772 /* stop server if resolution failed for a long enough period */
3773 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3774 if (!tick_is_expired(exp, now_ms))
3775 break;
3776
3777 if (s->next_admin & SRV_ADMF_RMAINT)
3778 return 1;
3779 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3780 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003781 }
3782
3783 return 1;
3784}
3785
3786/*
3787 * Server Name Resolution valid response callback
3788 * It expects:
3789 * - <nameserver>: the name server which answered the valid response
3790 * - <response>: buffer containing a valid DNS response
3791 * - <response_len>: size of <response>
3792 * It performs the following actions:
3793 * - ignore response if current ip found and server family not met
3794 * - update with first new ip found if family is met and current IP is not found
3795 * returns:
3796 * 0 on error
3797 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003798 *
3799 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003800 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003801int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003802{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003803 struct server *s = NULL;
3804 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003805 void *serverip, *firstip;
3806 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003807 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003808 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003809 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003810
Christopher Faulet67957bd2017-09-27 11:00:59 +02003811 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003812 if (!s)
3813 return 1;
3814
Christopher Faulet67957bd2017-09-27 11:00:59 +02003815 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003816
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003817 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003818 firstip = NULL; /* pointer to the first valid response found */
3819 /* it will be used as the new IP if a change is required */
3820 firstip_sin_family = AF_UNSPEC;
3821 serverip = NULL; /* current server IP address */
3822
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003823 /* initializing server IP pointer */
3824 server_sin_family = s->addr.ss_family;
3825 switch (server_sin_family) {
3826 case AF_INET:
3827 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3828 break;
3829
3830 case AF_INET6:
3831 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3832 break;
3833
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003834 case AF_UNSPEC:
3835 break;
3836
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003837 default:
3838 goto invalid;
3839 }
3840
Baptiste Assmann729c9012017-05-22 15:13:10 +02003841 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003842 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003843 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003844
3845 switch (ret) {
3846 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003847 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003848
3849 case DNS_UPD_SRVIP_NOT_FOUND:
3850 goto save_ip;
3851
3852 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003853 goto invalid;
3854
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003855 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003856 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003857 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003858
Baptiste Assmannfad03182015-10-28 02:03:32 +01003859 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003860 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003861 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003862 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003863
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003864 default:
3865 goto invalid;
3866
3867 }
3868
3869 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003870 if (nameserver) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003871 nameserver->counters->update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003872 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003873 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003874 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003875 else
3876 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003877 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003878
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003879 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003880 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003881 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003882
3883 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003884 if (nameserver) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003885 nameserver->counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003886 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003887 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003888 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003889 return 0;
3890}
3891
3892/*
3893 * Server Name Resolution error management callback
3894 * returns:
3895 * 0 on error
3896 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003897 *
3898 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003899 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003900int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003901{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003902 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003903
Christopher Faulet67957bd2017-09-27 11:00:59 +02003904 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003905 if (!s)
3906 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003907 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003908 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003909 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003910 return 1;
3911}
3912
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003913/*
3914 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003915 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003916 * It returns a pointer to the first server found or NULL if <ip> is not yet
3917 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003918 *
3919 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003920 */
3921struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3922{
3923 struct server *tmpsrv;
3924 struct proxy *be;
3925
3926 if (!srv)
3927 return NULL;
3928
3929 be = srv->proxy;
3930 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003931 /* we found the current server is the same, ignore it */
3932 if (srv == tmpsrv)
3933 continue;
3934
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003935 /* We want to compare the IP in the record with the IP of the servers in the
3936 * same backend, only if:
3937 * * DNS resolution is enabled on the server
3938 * * the hostname used for the resolution by our server is the same than the
3939 * one used for the server found in the backend
3940 * * the server found in the backend is not our current server
3941 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003942 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003943 if ((tmpsrv->hostname_dn == NULL) ||
3944 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3945 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003946 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003947 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003948 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003949 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003950
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003951 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003952 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003953 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003954 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003955 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003956
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003957 /* At this point, we have 2 different servers using the same DNS hostname
3958 * for their respective resolution.
3959 */
3960 if (*ip_family == tmpsrv->addr.ss_family &&
3961 ((tmpsrv->addr.ss_family == AF_INET &&
3962 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3963 (tmpsrv->addr.ss_family == AF_INET6 &&
3964 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003965 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003966 return tmpsrv;
3967 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003968 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003969 }
3970
Emeric Brune9fd6b52017-11-02 17:20:39 +01003971
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003972 return NULL;
3973}
3974
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003975/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3976 * resolver. This is suited for initial address configuration. Returns 0 on
3977 * success otherwise a non-zero error code. In case of error, *err_code, if
3978 * not NULL, is filled up.
3979 */
3980int srv_set_addr_via_libc(struct server *srv, int *err_code)
3981{
3982 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003983 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003984 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003985 return 1;
3986 }
3987 return 0;
3988}
3989
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003990/* Set the server's FDQN (->hostname) from <hostname>.
3991 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003992 *
3993 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003994 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003995int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003996{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003997 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003998 char *hostname_dn;
3999 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004000
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004001 /* Note that the server lock is already held. */
4002 if (!srv->resolvers)
4003 return -1;
4004
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004005 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004006 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004007 /* run time DNS resolution was not active for this server
4008 * and we can't enable it at run time for now.
4009 */
4010 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004011 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004012
4013 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004014 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004015 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004016 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4017 hostname_dn, trash.size);
4018 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004019 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004020
Christopher Faulet67957bd2017-09-27 11:00:59 +02004021 resolution = srv->dns_requester->resolution;
4022 if (resolution &&
4023 resolution->hostname_dn &&
4024 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004025 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004026
Christopher Faulet67957bd2017-09-27 11:00:59 +02004027 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004028
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004029 free(srv->hostname);
4030 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004031 srv->hostname = strdup(hostname);
4032 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004033 srv->hostname_dn_len = hostname_dn_len;
4034 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004035 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004036
Baptiste Assmann13a92322019-06-07 09:40:55 +02004037 if (srv->flags & SRV_F_NO_RESOLUTION)
4038 goto end;
4039
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004040 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004041 goto err;
4042
4043 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004044 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004045 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004046 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004047
4048 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004049 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004050 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004051 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004052}
4053
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004054/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4055 * from the state file. This is suited for initial address configuration.
4056 * Returns 0 on success otherwise a non-zero error code. In case of error,
4057 * *err_code, if not NULL, is filled up.
4058 */
4059static int srv_apply_lastaddr(struct server *srv, int *err_code)
4060{
4061 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4062 if (err_code)
4063 *err_code |= ERR_WARN;
4064 return 1;
4065 }
4066 return 0;
4067}
4068
Willy Tarreau25e51522016-11-04 15:10:17 +01004069/* returns 0 if no error, otherwise a combination of ERR_* flags */
4070static int srv_iterate_initaddr(struct server *srv)
4071{
4072 int return_code = 0;
4073 int err_code;
4074 unsigned int methods;
4075
4076 methods = srv->init_addr_methods;
4077 if (!methods) { // default to "last,libc"
4078 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4079 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4080 }
4081
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004082 /* "-dr" : always append "none" so that server addresses resolution
4083 * failures are silently ignored, this is convenient to validate some
4084 * configs out of their environment.
4085 */
4086 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4087 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4088
Willy Tarreau25e51522016-11-04 15:10:17 +01004089 while (methods) {
4090 err_code = 0;
4091 switch (srv_get_next_initaddr(&methods)) {
4092 case SRV_IADDR_LAST:
4093 if (!srv->lastaddr)
4094 continue;
4095 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004096 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004097 return_code |= err_code;
4098 break;
4099
4100 case SRV_IADDR_LIBC:
4101 if (!srv->hostname)
4102 continue;
4103 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004104 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004105 return_code |= err_code;
4106 break;
4107
Willy Tarreau37ebe122016-11-04 15:17:58 +01004108 case SRV_IADDR_NONE:
4109 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004110 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004111 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4112 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004113 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004114 return return_code;
4115
Willy Tarreau4310d362016-11-02 15:05:56 +01004116 case SRV_IADDR_IP:
4117 ipcpy(&srv->init_addr, &srv->addr);
4118 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004119 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4120 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004121 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004122 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004123
Willy Tarreau25e51522016-11-04 15:10:17 +01004124 default: /* unhandled method */
4125 break;
4126 }
4127 }
4128
4129 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004130 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004131 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4132 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004133 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004134 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004135 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4136 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004137
4138 return_code |= ERR_ALERT | ERR_FATAL;
4139 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004140out:
4141 srv_set_dyncookie(srv);
4142 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004143}
4144
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004145/*
4146 * This function parses all backends and all servers within each backend
4147 * and performs servers' addr resolution based on information provided by:
4148 * - configuration file
4149 * - server-state file (states provided by an 'old' haproxy process)
4150 *
4151 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4152 */
4153int srv_init_addr(void)
4154{
4155 struct proxy *curproxy;
4156 int return_code = 0;
4157
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004158 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004159 while (curproxy) {
4160 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004161
4162 /* servers are in backend only */
4163 if (!(curproxy->cap & PR_CAP_BE))
4164 goto srv_init_addr_next;
4165
Willy Tarreau25e51522016-11-04 15:10:17 +01004166 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004167 if (srv->hostname)
4168 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004169
4170 srv_init_addr_next:
4171 curproxy = curproxy->next;
4172 }
4173
4174 return return_code;
4175}
4176
Willy Tarreau46b7f532018-08-21 11:54:26 +02004177/*
4178 * Must be called with the server lock held.
4179 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004180const 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 +02004181{
4182
Willy Tarreau83061a82018-07-13 11:56:34 +02004183 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004184
4185 msg = get_trash_chunk();
4186 chunk_reset(msg);
4187
Olivier Houchard8da5f982017-08-04 18:35:36 +02004188 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004189 chunk_appendf(msg, "no need to change the FDQN");
4190 goto out;
4191 }
4192
4193 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4194 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4195 goto out;
4196 }
4197
4198 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4199 server->proxy->id, server->id, server->hostname, fqdn);
4200
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004201 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004202 chunk_reset(msg);
4203 chunk_appendf(msg, "could not update %s/%s FQDN",
4204 server->proxy->id, server->id);
4205 goto out;
4206 }
4207
4208 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004209 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004210
4211 out:
4212 if (updater)
4213 chunk_appendf(msg, " by '%s'", updater);
4214 chunk_appendf(msg, "\n");
4215
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004216 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004217}
4218
4219
Willy Tarreau21b069d2016-11-23 17:15:08 +01004220/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4221 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004222 * 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 +01004223 * used for CLI commands requiring a server name.
4224 * Important: the <arg> is modified to remove the '/'.
4225 */
4226struct server *cli_find_server(struct appctx *appctx, char *arg)
4227{
4228 struct proxy *px;
4229 struct server *sv;
4230 char *line;
4231
4232 /* split "backend/server" and make <line> point to server */
4233 for (line = arg; *line; line++)
4234 if (*line == '/') {
4235 *line++ = '\0';
4236 break;
4237 }
4238
4239 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004240 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004241 return NULL;
4242 }
4243
4244 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004245 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004246 return NULL;
4247 }
4248
Willy Tarreauc3914d42020-09-24 08:39:22 +02004249 if (px->disabled) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004250 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004251 return NULL;
4252 }
4253
4254 return sv;
4255}
4256
William Lallemand222baf22016-11-19 02:00:33 +01004257
Willy Tarreau46b7f532018-08-21 11:54:26 +02004258/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004259static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004260{
4261 struct server *sv;
4262 const char *warning;
4263
4264 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4265 return 1;
4266
4267 sv = cli_find_server(appctx, args[2]);
4268 if (!sv)
4269 return 1;
4270
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004271 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004272
William Lallemand222baf22016-11-19 02:00:33 +01004273 if (strcmp(args[3], "weight") == 0) {
4274 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004275 if (warning)
4276 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004277 }
4278 else if (strcmp(args[3], "state") == 0) {
4279 if (strcmp(args[4], "ready") == 0)
4280 srv_adm_set_ready(sv);
4281 else if (strcmp(args[4], "drain") == 0)
4282 srv_adm_set_drain(sv);
4283 else if (strcmp(args[4], "maint") == 0)
4284 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004285 else
4286 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004287 }
4288 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004289 if (sv->track)
4290 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004291 else if (strcmp(args[4], "up") == 0) {
4292 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004293 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004294 }
4295 else if (strcmp(args[4], "stopping") == 0) {
4296 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004297 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004298 }
4299 else if (strcmp(args[4], "down") == 0) {
4300 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004301 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004302 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004303 else
4304 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004305 }
4306 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004307 if (!(sv->agent.state & CHK_ST_ENABLED))
4308 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004309 else if (strcmp(args[4], "up") == 0) {
4310 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004311 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004312 }
4313 else if (strcmp(args[4], "down") == 0) {
4314 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004315 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004316 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004317 else
4318 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004319 }
Misiek2da082d2017-01-09 09:40:42 +01004320 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004321 if (!(sv->agent.state & CHK_ST_ENABLED))
4322 cli_err(appctx, "agent checks are not enabled on this server.\n");
4323 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4324 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004325 }
4326 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004327 if (!(sv->agent.state & CHK_ST_ENABLED))
4328 cli_err(appctx, "agent checks are not enabled on this server.\n");
4329 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004330 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004331 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004332 }
4333 }
William Lallemand222baf22016-11-19 02:00:33 +01004334 else if (strcmp(args[3], "check-port") == 0) {
4335 int i = 0;
4336 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004337 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004338 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004339 }
4340 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004341 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004342 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004343 }
4344 /* prevent the update of port to 0 if MAPPORTS are in use */
4345 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004346 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004347 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004348 }
4349 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004350 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004351 }
4352 else if (strcmp(args[3], "addr") == 0) {
4353 char *addr = NULL;
4354 char *port = NULL;
4355 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004356 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004357 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004358 }
4359 else {
4360 addr = args[4];
4361 }
4362 if (strcmp(args[5], "port") == 0) {
4363 port = args[6];
4364 }
4365 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004366 if (warning)
4367 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004368 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4369 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004370 else if (strcmp(args[3], "fqdn") == 0) {
4371 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004372 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004373 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004374 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004375 /* ensure runtime resolver will process this new fqdn */
4376 if (sv->flags & SRV_F_NO_RESOLUTION) {
4377 sv->flags &= ~SRV_F_NO_RESOLUTION;
4378 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004379 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004380 if (warning)
4381 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004382 }
William Lallemand222baf22016-11-19 02:00:33 +01004383 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004384 cli_err(appctx,
4385 "'set server <srv>' only supports 'agent', 'health', 'state',"
4386 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004387 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004388 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004389 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004390 return 1;
4391}
4392
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004393static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004394{
4395 struct stream_interface *si = appctx->owner;
4396 struct proxy *px;
4397 struct server *sv;
4398 char *line;
4399
4400
4401 /* split "backend/server" and make <line> point to server */
4402 for (line = args[2]; *line; line++)
4403 if (*line == '/') {
4404 *line++ = '\0';
4405 break;
4406 }
4407
Willy Tarreau9d008692019-08-09 11:21:01 +02004408 if (!*line)
4409 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004410
Willy Tarreau9d008692019-08-09 11:21:01 +02004411 if (!get_backend_server(args[2], line, &px, &sv))
4412 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004413
4414 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004415 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4416 sv->iweight);
4417 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004418 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004419 return 0;
4420 }
William Lallemand6b160942016-11-22 12:34:35 +01004421 return 1;
4422}
4423
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004424/* Parse a "set weight" command.
4425 *
4426 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004427 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004428static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004429{
4430 struct server *sv;
4431 const char *warning;
4432
4433 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4434 return 1;
4435
4436 sv = cli_find_server(appctx, args[2]);
4437 if (!sv)
4438 return 1;
4439
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004440 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4441
William Lallemand6b160942016-11-22 12:34:35 +01004442 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004443 if (warning)
4444 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004445
4446 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4447
William Lallemand6b160942016-11-22 12:34:35 +01004448 return 1;
4449}
4450
Willy Tarreau46b7f532018-08-21 11:54:26 +02004451/* parse a "set maxconn server" command. It always returns 1.
4452 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004453 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004454 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004455static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004456{
4457 struct server *sv;
4458 const char *warning;
4459
4460 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4461 return 1;
4462
4463 sv = cli_find_server(appctx, args[3]);
4464 if (!sv)
4465 return 1;
4466
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004467 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4468
Willy Tarreaub8026272016-11-23 11:26:56 +01004469 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004470 if (warning)
4471 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004472
4473 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4474
Willy Tarreaub8026272016-11-23 11:26:56 +01004475 return 1;
4476}
William Lallemand6b160942016-11-22 12:34:35 +01004477
Willy Tarreau46b7f532018-08-21 11:54:26 +02004478/* parse a "disable agent" command. It always returns 1.
4479 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004480 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004481 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004482static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004483{
4484 struct server *sv;
4485
4486 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4487 return 1;
4488
4489 sv = cli_find_server(appctx, args[2]);
4490 if (!sv)
4491 return 1;
4492
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004493 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004494 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004495 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004496 return 1;
4497}
4498
Willy Tarreau46b7f532018-08-21 11:54:26 +02004499/* parse a "disable health" command. It always returns 1.
4500 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004501 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004502 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004503static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004504{
4505 struct server *sv;
4506
4507 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4508 return 1;
4509
4510 sv = cli_find_server(appctx, args[2]);
4511 if (!sv)
4512 return 1;
4513
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004514 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004515 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004516 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004517 return 1;
4518}
4519
Willy Tarreau46b7f532018-08-21 11:54:26 +02004520/* parse a "disable server" command. It always returns 1.
4521 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004522 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004523 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004524static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004525{
4526 struct server *sv;
4527
4528 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4529 return 1;
4530
4531 sv = cli_find_server(appctx, args[2]);
4532 if (!sv)
4533 return 1;
4534
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004535 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004536 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004537 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004538 return 1;
4539}
4540
Willy Tarreau46b7f532018-08-21 11:54:26 +02004541/* parse a "enable agent" command. It always returns 1.
4542 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004543 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004544 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004545static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004546{
4547 struct server *sv;
4548
4549 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4550 return 1;
4551
4552 sv = cli_find_server(appctx, args[2]);
4553 if (!sv)
4554 return 1;
4555
Willy Tarreau9d008692019-08-09 11:21:01 +02004556 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4557 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004558
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004559 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004560 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004561 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004562 return 1;
4563}
4564
Willy Tarreau46b7f532018-08-21 11:54:26 +02004565/* parse a "enable health" command. It always returns 1.
4566 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004567 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004568 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004569static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004570{
4571 struct server *sv;
4572
4573 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4574 return 1;
4575
4576 sv = cli_find_server(appctx, args[2]);
4577 if (!sv)
4578 return 1;
4579
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004580 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004581 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004582 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004583 return 1;
4584}
4585
Willy Tarreau46b7f532018-08-21 11:54:26 +02004586/* parse a "enable server" command. It always returns 1.
4587 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004588 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004589 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004590static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004591{
4592 struct server *sv;
4593
4594 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4595 return 1;
4596
4597 sv = cli_find_server(appctx, args[2]);
4598 if (!sv)
4599 return 1;
4600
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004601 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004602 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004603 if (!(sv->flags & SRV_F_COOKIESET)
4604 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4605 sv->cookie)
4606 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004607 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004608 return 1;
4609}
4610
William Lallemand222baf22016-11-19 02:00:33 +01004611/* register cli keywords */
4612static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004613 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004614 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004615 { { "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 +01004616 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004617 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004618 { { "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 +01004619 { { "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 +01004620 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004621 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4622 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4623
William Lallemand222baf22016-11-19 02:00:33 +01004624 {{},}
4625}};
4626
Willy Tarreau0108d902018-11-25 19:14:37 +01004627INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004628
Emeric Brun64cc49c2017-10-03 14:46:45 +02004629/*
4630 * This function applies server's status changes, it is
4631 * is designed to be called asynchronously.
4632 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004633 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004634 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004635static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004636{
4637 struct check *check = &s->check;
4638 int xferred;
4639 struct proxy *px = s->proxy;
4640 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4641 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4642 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004643 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004644
Emeric Brun64cc49c2017-10-03 14:46:45 +02004645 /* If currently main is not set we try to apply pending state changes */
4646 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4647 int next_admin;
4648
4649 /* Backup next admin */
4650 next_admin = s->next_admin;
4651
4652 /* restore current admin state */
4653 s->next_admin = s->cur_admin;
4654
4655 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4656 s->last_change = now.tv_sec;
4657 if (s->proxy->lbprm.set_server_status_down)
4658 s->proxy->lbprm.set_server_status_down(s);
4659
4660 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4661 srv_shutdown_streams(s, SF_ERR_DOWN);
4662
4663 /* we might have streams queued on this server and waiting for
4664 * a connection. Those which are redispatchable will be queued
4665 * to another server or to the proxy itself.
4666 */
4667 xferred = pendconn_redistribute(s);
4668
4669 tmptrash = alloc_trash_chunk();
4670 if (tmptrash) {
4671 chunk_printf(tmptrash,
4672 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4673 s->proxy->id, s->id);
4674
Emeric Brun5a133512017-10-19 14:42:30 +02004675 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004676 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004677
4678 /* we don't send an alert if the server was previously paused */
4679 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004680 send_log(s->proxy, log_level, "%s.\n",
4681 tmptrash->area);
4682 send_email_alert(s, log_level, "%s",
4683 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004684 free_trash_chunk(tmptrash);
4685 tmptrash = NULL;
4686 }
4687 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4688 set_backend_down(s->proxy);
4689
4690 s->counters.down_trans++;
4691 }
4692 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4693 s->last_change = now.tv_sec;
4694 if (s->proxy->lbprm.set_server_status_down)
4695 s->proxy->lbprm.set_server_status_down(s);
4696
4697 /* we might have streams queued on this server and waiting for
4698 * a connection. Those which are redispatchable will be queued
4699 * to another server or to the proxy itself.
4700 */
4701 xferred = pendconn_redistribute(s);
4702
4703 tmptrash = alloc_trash_chunk();
4704 if (tmptrash) {
4705 chunk_printf(tmptrash,
4706 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4707 s->proxy->id, s->id);
4708
Emeric Brun5a133512017-10-19 14:42:30 +02004709 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004710
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004711 ha_warning("%s.\n", tmptrash->area);
4712 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4713 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004714 free_trash_chunk(tmptrash);
4715 tmptrash = NULL;
4716 }
4717
4718 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4719 set_backend_down(s->proxy);
4720 }
4721 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4722 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4723 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4724 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4725 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4726 s->proxy->last_change = now.tv_sec;
4727 }
4728
4729 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4730 s->down_time += now.tv_sec - s->last_change;
4731
4732 s->last_change = now.tv_sec;
4733 if (s->next_state == SRV_ST_STARTING)
4734 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4735
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004736 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004737 /* now propagate the status change to any LB algorithms */
4738 if (px->lbprm.update_server_eweight)
4739 px->lbprm.update_server_eweight(s);
4740 else if (srv_willbe_usable(s)) {
4741 if (px->lbprm.set_server_status_up)
4742 px->lbprm.set_server_status_up(s);
4743 }
4744 else {
4745 if (px->lbprm.set_server_status_down)
4746 px->lbprm.set_server_status_down(s);
4747 }
4748
4749 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4750 * and it's not a backup server and its effective weight is > 0,
4751 * then it can accept new connections, so we shut down all streams
4752 * on all backup servers.
4753 */
4754 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4755 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4756 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4757
4758 /* check if we can handle some connections queued at the proxy. We
4759 * will take as many as we can handle.
4760 */
4761 xferred = pendconn_grab_from_px(s);
4762
4763 tmptrash = alloc_trash_chunk();
4764 if (tmptrash) {
4765 chunk_printf(tmptrash,
4766 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4767 s->proxy->id, s->id);
4768
Emeric Brun5a133512017-10-19 14:42:30 +02004769 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004770 ha_warning("%s.\n", tmptrash->area);
4771 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4772 tmptrash->area);
4773 send_email_alert(s, LOG_NOTICE, "%s",
4774 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004775 free_trash_chunk(tmptrash);
4776 tmptrash = NULL;
4777 }
4778
4779 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4780 set_backend_down(s->proxy);
4781 }
4782 else if (s->cur_eweight != s->next_eweight) {
4783 /* now propagate the status change to any LB algorithms */
4784 if (px->lbprm.update_server_eweight)
4785 px->lbprm.update_server_eweight(s);
4786 else if (srv_willbe_usable(s)) {
4787 if (px->lbprm.set_server_status_up)
4788 px->lbprm.set_server_status_up(s);
4789 }
4790 else {
4791 if (px->lbprm.set_server_status_down)
4792 px->lbprm.set_server_status_down(s);
4793 }
4794
4795 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4796 set_backend_down(s->proxy);
4797 }
4798
4799 s->next_admin = next_admin;
4800 }
4801
Emeric Brun5a133512017-10-19 14:42:30 +02004802 /* reset operational state change */
4803 *s->op_st_chg.reason = 0;
4804 s->op_st_chg.status = s->op_st_chg.code = -1;
4805 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004806
4807 /* Now we try to apply pending admin changes */
4808
4809 /* Maintenance must also disable health checks */
4810 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4811 if (s->check.state & CHK_ST_ENABLED) {
4812 s->check.state |= CHK_ST_PAUSED;
4813 check->health = 0;
4814 }
4815
4816 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004817 tmptrash = alloc_trash_chunk();
4818 if (tmptrash) {
4819 chunk_printf(tmptrash,
4820 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4821 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4822 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004823
Olivier Houchard796a2b32017-10-24 17:42:47 +02004824 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004825
Olivier Houchard796a2b32017-10-24 17:42:47 +02004826 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004827 ha_warning("%s.\n", tmptrash->area);
4828 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4829 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004830 }
4831 free_trash_chunk(tmptrash);
4832 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004833 }
Emeric Brun8f298292017-12-06 16:47:17 +01004834 /* commit new admin status */
4835
4836 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004837 }
4838 else { /* server was still running */
4839 check->health = 0; /* failure */
4840 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004841
4842 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004843 if (s->proxy->lbprm.set_server_status_down)
4844 s->proxy->lbprm.set_server_status_down(s);
4845
Emeric Brun64cc49c2017-10-03 14:46:45 +02004846 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4847 srv_shutdown_streams(s, SF_ERR_DOWN);
4848
William Dauchy6318d332020-05-02 21:52:36 +02004849 /* force connection cleanup on the given server */
4850 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004851 /* we might have streams queued on this server and waiting for
4852 * a connection. Those which are redispatchable will be queued
4853 * to another server or to the proxy itself.
4854 */
4855 xferred = pendconn_redistribute(s);
4856
4857 tmptrash = alloc_trash_chunk();
4858 if (tmptrash) {
4859 chunk_printf(tmptrash,
4860 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4861 s->flags & SRV_F_BACKUP ? "Backup " : "",
4862 s->proxy->id, s->id,
4863 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4864
4865 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4866
4867 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004868 ha_warning("%s.\n", tmptrash->area);
4869 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4870 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004871 }
4872 free_trash_chunk(tmptrash);
4873 tmptrash = NULL;
4874 }
4875 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4876 set_backend_down(s->proxy);
4877
4878 s->counters.down_trans++;
4879 }
4880 }
4881 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4882 /* OK here we're leaving maintenance, we have many things to check,
4883 * because the server might possibly be coming back up depending on
4884 * its state. In practice, leaving maintenance means that we should
4885 * immediately turn to UP (more or less the slowstart) under the
4886 * following conditions :
4887 * - server is neither checked nor tracked
4888 * - server tracks another server which is not checked
4889 * - server tracks another server which is already up
4890 * Which sums up as something simpler :
4891 * "either the tracking server is up or the server's checks are disabled
4892 * or up". Otherwise we only re-enable health checks. There's a special
4893 * case associated to the stopping state which can be inherited. Note
4894 * that the server might still be in drain mode, which is naturally dealt
4895 * with by the lower level functions.
4896 */
4897
4898 if (s->check.state & CHK_ST_ENABLED) {
4899 s->check.state &= ~CHK_ST_PAUSED;
4900 check->health = check->rise; /* start OK but check immediately */
4901 }
4902
4903 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4904 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4905 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4906 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4907 s->next_state = SRV_ST_STOPPING;
4908 }
4909 else {
4910 s->next_state = SRV_ST_STARTING;
4911 if (s->slowstart > 0)
4912 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4913 else
4914 s->next_state = SRV_ST_RUNNING;
4915 }
4916
4917 }
4918
4919 tmptrash = alloc_trash_chunk();
4920 if (tmptrash) {
4921 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4922 chunk_printf(tmptrash,
4923 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4924 s->flags & SRV_F_BACKUP ? "Backup " : "",
4925 s->proxy->id, s->id,
4926 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4927 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4928 }
4929 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4930 chunk_printf(tmptrash,
4931 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4932 s->flags & SRV_F_BACKUP ? "Backup " : "",
4933 s->proxy->id, s->id, s->hostname,
4934 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4935 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4936 }
4937 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4938 chunk_printf(tmptrash,
4939 "%sServer %s/%s is %s/%s (leaving maintenance)",
4940 s->flags & SRV_F_BACKUP ? "Backup " : "",
4941 s->proxy->id, s->id,
4942 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4943 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4944 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004945 ha_warning("%s.\n", tmptrash->area);
4946 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4947 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004948 free_trash_chunk(tmptrash);
4949 tmptrash = NULL;
4950 }
4951
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004952 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004953 /* now propagate the status change to any LB algorithms */
4954 if (px->lbprm.update_server_eweight)
4955 px->lbprm.update_server_eweight(s);
4956 else if (srv_willbe_usable(s)) {
4957 if (px->lbprm.set_server_status_up)
4958 px->lbprm.set_server_status_up(s);
4959 }
4960 else {
4961 if (px->lbprm.set_server_status_down)
4962 px->lbprm.set_server_status_down(s);
4963 }
4964
4965 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4966 set_backend_down(s->proxy);
4967
Willy Tarreau6a78e612018-08-07 10:14:53 +02004968 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4969 * and it's not a backup server and its effective weight is > 0,
4970 * then it can accept new connections, so we shut down all streams
4971 * on all backup servers.
4972 */
4973 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4974 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4975 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4976
4977 /* check if we can handle some connections queued at the proxy. We
4978 * will take as many as we can handle.
4979 */
4980 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004981 }
4982 else if (s->next_admin & SRV_ADMF_MAINT) {
4983 /* remaining in maintenance mode, let's inform precisely about the
4984 * situation.
4985 */
4986 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4987 tmptrash = alloc_trash_chunk();
4988 if (tmptrash) {
4989 chunk_printf(tmptrash,
4990 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4991 s->flags & SRV_F_BACKUP ? "Backup " : "",
4992 s->proxy->id, s->id);
4993
4994 if (s->track) /* normally it's mandatory here */
4995 chunk_appendf(tmptrash, " via %s/%s",
4996 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004997 ha_warning("%s.\n", tmptrash->area);
4998 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4999 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005000 free_trash_chunk(tmptrash);
5001 tmptrash = NULL;
5002 }
5003 }
5004 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5005 tmptrash = alloc_trash_chunk();
5006 if (tmptrash) {
5007 chunk_printf(tmptrash,
5008 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5009 s->flags & SRV_F_BACKUP ? "Backup " : "",
5010 s->proxy->id, s->id, s->hostname);
5011
5012 if (s->track) /* normally it's mandatory here */
5013 chunk_appendf(tmptrash, " via %s/%s",
5014 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005015 ha_warning("%s.\n", tmptrash->area);
5016 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5017 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005018 free_trash_chunk(tmptrash);
5019 tmptrash = NULL;
5020 }
5021 }
5022 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5023 tmptrash = alloc_trash_chunk();
5024 if (tmptrash) {
5025 chunk_printf(tmptrash,
5026 "%sServer %s/%s remains in forced maintenance",
5027 s->flags & SRV_F_BACKUP ? "Backup " : "",
5028 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005029 ha_warning("%s.\n", tmptrash->area);
5030 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5031 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005032 free_trash_chunk(tmptrash);
5033 tmptrash = NULL;
5034 }
5035 }
5036 /* don't report anything when leaving drain mode and remaining in maintenance */
5037
5038 s->cur_admin = s->next_admin;
5039 }
5040
5041 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5042 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5043 /* drain state is applied only if not yet in maint */
5044
5045 s->last_change = now.tv_sec;
5046 if (px->lbprm.set_server_status_down)
5047 px->lbprm.set_server_status_down(s);
5048
5049 /* we might have streams queued on this server and waiting for
5050 * a connection. Those which are redispatchable will be queued
5051 * to another server or to the proxy itself.
5052 */
5053 xferred = pendconn_redistribute(s);
5054
5055 tmptrash = alloc_trash_chunk();
5056 if (tmptrash) {
5057 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5058 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5059 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5060
5061 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5062
5063 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005064 ha_warning("%s.\n", tmptrash->area);
5065 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5066 tmptrash->area);
5067 send_email_alert(s, LOG_NOTICE, "%s",
5068 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005069 }
5070 free_trash_chunk(tmptrash);
5071 tmptrash = NULL;
5072 }
5073
5074 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5075 set_backend_down(s->proxy);
5076 }
5077 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5078 /* OK completely leaving drain mode */
5079 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5080 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5081 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5082 s->proxy->last_change = now.tv_sec;
5083 }
5084
5085 if (s->last_change < now.tv_sec) // ignore negative times
5086 s->down_time += now.tv_sec - s->last_change;
5087 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005088 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005089
5090 tmptrash = alloc_trash_chunk();
5091 if (tmptrash) {
5092 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5093 chunk_printf(tmptrash,
5094 "%sServer %s/%s is %s (leaving forced drain)",
5095 s->flags & SRV_F_BACKUP ? "Backup " : "",
5096 s->proxy->id, s->id,
5097 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5098 }
5099 else {
5100 chunk_printf(tmptrash,
5101 "%sServer %s/%s is %s (leaving drain)",
5102 s->flags & SRV_F_BACKUP ? "Backup " : "",
5103 s->proxy->id, s->id,
5104 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5105 if (s->track) /* normally it's mandatory here */
5106 chunk_appendf(tmptrash, " via %s/%s",
5107 s->track->proxy->id, s->track->id);
5108 }
5109
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005110 ha_warning("%s.\n", tmptrash->area);
5111 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5112 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005113 free_trash_chunk(tmptrash);
5114 tmptrash = NULL;
5115 }
5116
5117 /* now propagate the status change to any LB algorithms */
5118 if (px->lbprm.update_server_eweight)
5119 px->lbprm.update_server_eweight(s);
5120 else if (srv_willbe_usable(s)) {
5121 if (px->lbprm.set_server_status_up)
5122 px->lbprm.set_server_status_up(s);
5123 }
5124 else {
5125 if (px->lbprm.set_server_status_down)
5126 px->lbprm.set_server_status_down(s);
5127 }
5128 }
5129 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5130 /* remaining in drain mode after removing one of its flags */
5131
5132 tmptrash = alloc_trash_chunk();
5133 if (tmptrash) {
5134 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5135 chunk_printf(tmptrash,
5136 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5137 s->flags & SRV_F_BACKUP ? "Backup " : "",
5138 s->proxy->id, s->id);
5139
5140 if (s->track) /* normally it's mandatory here */
5141 chunk_appendf(tmptrash, " via %s/%s",
5142 s->track->proxy->id, s->track->id);
5143 }
5144 else {
5145 chunk_printf(tmptrash,
5146 "%sServer %s/%s remains in forced drain mode",
5147 s->flags & SRV_F_BACKUP ? "Backup " : "",
5148 s->proxy->id, s->id);
5149 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005150 ha_warning("%s.\n", tmptrash->area);
5151 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5152 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005153 free_trash_chunk(tmptrash);
5154 tmptrash = NULL;
5155 }
5156
5157 /* commit new admin status */
5158
5159 s->cur_admin = s->next_admin;
5160 }
5161 }
5162
5163 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005164 *s->adm_st_chg_cause = 0;
5165}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005166
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005167struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5168{
5169 struct connection *conn;
5170
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005171 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005172 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005173 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005174 }
5175
5176 return task;
5177}
5178
Olivier Houchardff1d0922020-06-29 20:15:59 +02005179/* Move toremove_nb connections from idle_list to toremove_list, -1 means
5180 * moving them all.
5181 * Returns the number of connections moved.
5182 */
5183static int srv_migrate_conns_to_remove(struct mt_list *idle_list, struct mt_list *toremove_list, int toremove_nb)
5184{
5185 struct mt_list *elt1, elt2;
5186 struct connection *conn;
5187 int i = 0;
5188
5189 mt_list_for_each_entry_safe(conn, idle_list, list, elt1, elt2) {
5190 if (toremove_nb != -1 && i >= toremove_nb)
5191 break;
5192 MT_LIST_DEL_SAFE_NOINIT(elt1);
Willy Tarreaude4db172020-07-10 08:10:29 +02005193 MT_LIST_ADDQ(toremove_list, &conn->list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005194 i++;
5195 }
5196 return i;
5197}
William Dauchy6318d332020-05-02 21:52:36 +02005198/* cleanup connections for a given server
5199 * might be useful when going on forced maintenance or live changing ip/port
5200 */
William Dauchy707ad322020-05-04 13:52:40 +02005201static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005202{
William Dauchy6318d332020-05-02 21:52:36 +02005203 int did_remove;
5204 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005205
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005206 /* check all threads starting with ours */
William Dauchy6318d332020-05-02 21:52:36 +02005207 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005208 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005209 did_remove = 0;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005210 if (srv_migrate_conns_to_remove(&srv->idle_conns[i], &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02005211 did_remove = 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005212 if (srv_migrate_conns_to_remove(&srv->safe_conns[i], &idle_conns[i].toremove_conns, -1) > 0)
5213 did_remove = 1;
William Dauchy6318d332020-05-02 21:52:36 +02005214 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005215 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005216
5217 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5218 break;
William Dauchy6318d332020-05-02 21:52:36 +02005219 }
5220 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5221}
5222
Willy Tarreau980855b2019-02-07 14:59:29 +01005223struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005224{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005225 struct server *srv;
5226 struct eb32_node *eb;
5227 int i;
5228 unsigned int next_wakeup;
5229 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005230
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005231 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5232 while (1) {
5233 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005234 int exceed_conns;
5235 int to_kill;
5236 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005237
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005238 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5239 if (!eb) {
5240 /* we might have reached the end of the tree, typically because
5241 * <now_ms> is in the first half and we're first scanning the last
5242 * half. Let's loop back to the beginning of the tree now.
5243 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005244
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005245 eb = eb32_first(&idle_conn_srv);
5246 if (likely(!eb))
5247 break;
5248 }
5249 if (tick_is_lt(now_ms, eb->key)) {
5250 /* timer not expired yet, revisit it later */
5251 next_wakeup = eb->key;
5252 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005253 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005254 }
5255 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005256
5257 /* Calculate how many idle connections we want to kill :
5258 * we want to remove half the difference between the total
5259 * of established connections (used or idle) and the max
5260 * number of used connections.
5261 */
5262 curr_idle = srv->curr_idle_conns;
5263 if (curr_idle == 0)
5264 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005265 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005266 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005267
5268 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns + 1) / 2;
5269 if (srv->est_need_conns < srv->max_used_conns)
5270 srv->est_need_conns = srv->max_used_conns;
5271
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005272 srv->max_used_conns = srv->curr_used_conns;
5273
Willy Tarreau18ed7892020-07-02 19:05:30 +02005274 if (exceed_conns <= 0)
5275 goto remove;
5276
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005277 /* check all threads starting with ours */
5278 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005279 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005280 int j;
5281 int did_remove = 0;
5282
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005283 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5284 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005285
5286 j = srv_migrate_conns_to_remove(&srv->idle_conns[i], &idle_conns[i].toremove_conns, max_conn);
5287 if (j > 0)
5288 did_remove = 1;
5289 if (max_conn - j > 0 &&
5290 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 +01005291 did_remove = 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005292
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005293 if (did_remove && max_conn < srv->curr_idle_thr[i])
5294 srv_is_empty = 0;
5295 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005296 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005297
5298 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5299 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005300 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005301remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005302 eb32_delete(&srv->idle_node);
5303 if (!srv_is_empty) {
5304 /* There are still more idle connections, add the
5305 * server back in the tree.
5306 */
5307 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5308 now_ms);
5309 eb32_insert(&idle_conn_srv, &srv->idle_node);
5310 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005311 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005312 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5313
5314 if (need_wakeup)
5315 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005316 else
5317 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005318
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005319 return task;
5320}
Olivier Houchard88698d92019-04-16 19:07:22 +02005321
Willy Tarreau76cc6992020-07-01 18:49:24 +02005322/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
5323static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
5324 struct proxy *defpx, const char *file, int line,
5325 char **err)
5326{
5327 if (too_many_args(1, args, err, NULL))
5328 return -1;
5329
5330 if (strcmp(args[1], "on") == 0)
5331 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
5332 else if (strcmp(args[1], "off") == 0)
5333 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
5334 else {
5335 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
5336 return -1;
5337 }
5338 return 0;
5339}
5340
Olivier Houchard88698d92019-04-16 19:07:22 +02005341/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5342static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5343 struct proxy *defpx, const char *file, int line,
5344 char **err)
5345{
5346 int arg = -1;
5347
5348 if (too_many_args(1, args, err, NULL))
5349 return -1;
5350
5351 if (*(args[1]) != 0)
5352 arg = atoi(args[1]);
5353
5354 if (arg < 0 || arg > 100) {
5355 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5356 return -1;
5357 }
5358
5359 if (args[0][10] == 'h')
5360 global.tune.pool_high_ratio = arg;
5361 else
5362 global.tune.pool_low_ratio = arg;
5363 return 0;
5364}
5365
5366/* config keyword parsers */
5367static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02005368 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02005369 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5370 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5371 { 0, NULL, NULL }
5372}};
5373
5374INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5375
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005376/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005377 * Local variables:
5378 * c-indent-level: 8
5379 * c-basic-offset: 8
5380 * End:
5381 */