blob: 7ac890513f6ca84c04e1324df34fd78b3df8052b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020017#include <haproxy/applet-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Olivier Houchard4e694042017-03-14 20:01:29 +010019#include <import/xxhash.h>
20
Willy Tarreau272adea2014-03-31 10:39:59 +020021#include <common/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020022#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020023#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020024#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020025#include <haproxy/dict-t.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020026#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020027#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020028#include <haproxy/global.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020029#include <haproxy/namespace.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020030#include <haproxy/sample.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020031#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020032#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020033#include <haproxy/task.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020034#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020035
Willy Tarreaufc8f6a82020-06-03 19:20:59 +020036#include <haproxy/port_range.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020037#include <haproxy/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020038#include <proto/queue.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020039#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
David Carlier6f182082017-04-03 21:58:04 +010041#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020042
Willy Tarreau8d2b7772020-05-27 10:58:19 +020043#include <import/ebsttree.h>
Baptiste Assmannda29fe22019-06-13 13:24:29 +020044
Willy Tarreau3ff577e2018-08-02 11:48:52 +020045static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020046static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010047static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010048static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020049static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
50static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020051static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau21faa912012-10-10 08:27:36 +020053/* List head of all known server keywords */
54static struct srv_kw_list srv_keywords = {
55 .list = LIST_HEAD_INIT(srv_keywords.list)
56};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057
Willy Tarreauaf613e82020-06-05 08:40:51 +020058__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010059struct eb_root idle_conn_srv = EB_ROOT;
60struct task *idle_conn_task = NULL;
61struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020062struct mt_list toremove_connections[MAX_THREADS];
Willy Tarreauaf613e82020-06-05 08:40:51 +020063__decl_thread(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010064
Frédéric Lécaille7da71292019-05-20 09:47:07 +020065/* The server names dictionary */
66struct dict server_name_dict = {
67 .name = "server names",
68 .values = EB_ROOT_UNIQUE,
69};
70
Baptiste Assmannda29fe22019-06-13 13:24:29 +020071/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010072struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020073
Simon Hormana3608442013-11-01 16:46:15 +090074int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020075{
Emeric Brun52a91d32017-08-31 14:41:55 +020076 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020077 return s->down_time;
78
79 return now.tv_sec - s->last_change + s->down_time;
80}
Willy Tarreaubaaee002006-06-26 02:48:02 +020081
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050082int srv_lastsession(const struct server *s)
83{
84 if (s->counters.last_sess)
85 return now.tv_sec - s->counters.last_sess;
86
87 return -1;
88}
89
Simon Horman4a741432013-02-23 15:35:38 +090090int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020091{
Simon Horman4a741432013-02-23 15:35:38 +090092 const struct server *s = check->server;
93
Willy Tarreauff5ae352013-12-11 20:36:34 +010094 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090095 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010096
Emeric Brun52a91d32017-08-31 14:41:55 +020097 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090098 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010099
Simon Horman4a741432013-02-23 15:35:38 +0900100 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100101}
102
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100103/*
104 * Check that we did not get a hash collision.
105 * Unlikely, but it can happen.
106 */
107static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100108{
109 struct proxy *p = s->proxy;
110 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100111
112 for (tmpserv = p->srv; tmpserv != NULL;
113 tmpserv = tmpserv->next) {
114 if (tmpserv == s)
115 continue;
116 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
117 continue;
118 if (tmpserv->cookie &&
119 strcmp(tmpserv->cookie, s->cookie) == 0) {
120 ha_warning("We generated two equal cookies for two different servers.\n"
121 "Please change the secret key for '%s'.\n",
122 s->proxy->id);
123 }
124 }
125
126}
127
Willy Tarreau46b7f532018-08-21 11:54:26 +0200128/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200129 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200130 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100131void srv_set_dyncookie(struct server *s)
132{
133 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100134 char *tmpbuf;
135 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100136 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100137 size_t buffer_len;
138 int addr_len;
139 int port;
140
Willy Tarreau5e83d992019-07-30 11:59:34 +0200141 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
142
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 if ((s->flags & SRV_F_COOKIESET) ||
144 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
145 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200146 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100147 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100148
149 if (s->addr.ss_family != AF_INET &&
150 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200151 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100152 /*
153 * Buffer to calculate the cookie value.
154 * The buffer contains the secret key + the server IP address
155 * + the TCP port.
156 */
157 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
158 /*
159 * The TCP port should use only 2 bytes, but is stored in
160 * an unsigned int in struct server, so let's use 4, to be
161 * on the safe side.
162 */
163 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200164 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100165 memcpy(tmpbuf, p->dyncookie_key, key_len);
166 memcpy(&(tmpbuf[key_len]),
167 s->addr.ss_family == AF_INET ?
168 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
169 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
170 addr_len);
171 /*
172 * Make sure it's the same across all the load balancers,
173 * no matter their endianness.
174 */
175 port = htonl(s->svc_port);
176 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
177 hash_value = XXH64(tmpbuf, buffer_len, 0);
178 memprintf(&s->cookie, "%016llx", hash_value);
179 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200180 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100181 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100182
183 /* Don't bother checking if the dyncookie is duplicated if
184 * the server is marked as "disabled", maybe it doesn't have
185 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100186 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100187 if (!(s->next_admin & SRV_ADMF_FMAINT))
188 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200189 out:
190 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100191}
192
Willy Tarreau21faa912012-10-10 08:27:36 +0200193/*
194 * Registers the server keyword list <kwl> as a list of valid keywords for next
195 * parsing sessions.
196 */
197void srv_register_keywords(struct srv_kw_list *kwl)
198{
199 LIST_ADDQ(&srv_keywords.list, &kwl->list);
200}
201
202/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
203 * keyword is found with a NULL ->parse() function, then an attempt is made to
204 * find one with a valid ->parse() function. This way it is possible to declare
205 * platform-dependant, known keywords as NULL, then only declare them as valid
206 * if some options are met. Note that if the requested keyword contains an
207 * opening parenthesis, everything from this point is ignored.
208 */
209struct srv_kw *srv_find_kw(const char *kw)
210{
211 int index;
212 const char *kwend;
213 struct srv_kw_list *kwl;
214 struct srv_kw *ret = NULL;
215
216 kwend = strchr(kw, '(');
217 if (!kwend)
218 kwend = kw + strlen(kw);
219
220 list_for_each_entry(kwl, &srv_keywords.list, list) {
221 for (index = 0; kwl->kw[index].kw != NULL; index++) {
222 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
223 kwl->kw[index].kw[kwend-kw] == 0) {
224 if (kwl->kw[index].parse)
225 return &kwl->kw[index]; /* found it !*/
226 else
227 ret = &kwl->kw[index]; /* may be OK */
228 }
229 }
230 }
231 return ret;
232}
233
234/* Dumps all registered "server" keywords to the <out> string pointer. The
235 * unsupported keywords are only dumped if their supported form was not
236 * found.
237 */
238void srv_dump_kws(char **out)
239{
240 struct srv_kw_list *kwl;
241 int index;
242
Christopher Faulet784063e2020-05-18 12:14:18 +0200243 if (!out)
244 return;
245
Willy Tarreau21faa912012-10-10 08:27:36 +0200246 *out = NULL;
247 list_for_each_entry(kwl, &srv_keywords.list, list) {
248 for (index = 0; kwl->kw[index].kw != NULL; index++) {
249 if (kwl->kw[index].parse ||
250 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
251 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
252 kwl->scope,
253 kwl->kw[index].kw,
254 kwl->kw[index].skip ? " <arg>" : "",
255 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
256 kwl->kw[index].parse ? "" : " (not supported)");
257 }
258 }
259 }
260}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100261
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100262/* Parse the "backup" server keyword */
263static int srv_parse_backup(char **args, int *cur_arg,
264 struct proxy *curproxy, struct server *newsrv, char **err)
265{
266 newsrv->flags |= SRV_F_BACKUP;
267 return 0;
268}
269
Alexander Liu2a54bb72019-05-22 19:44:48 +0800270
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100271/* Parse the "cookie" server keyword */
272static int srv_parse_cookie(char **args, int *cur_arg,
273 struct proxy *curproxy, struct server *newsrv, char **err)
274{
275 char *arg;
276
277 arg = args[*cur_arg + 1];
278 if (!*arg) {
279 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
280 return ERR_ALERT | ERR_FATAL;
281 }
282
283 free(newsrv->cookie);
284 newsrv->cookie = strdup(arg);
285 newsrv->cklen = strlen(arg);
286 newsrv->flags |= SRV_F_COOKIESET;
287 return 0;
288}
289
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100290/* Parse the "disabled" server keyword */
291static int srv_parse_disabled(char **args, int *cur_arg,
292 struct proxy *curproxy, struct server *newsrv, char **err)
293{
Emeric Brun52a91d32017-08-31 14:41:55 +0200294 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
295 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100296 newsrv->check.state |= CHK_ST_PAUSED;
297 newsrv->check.health = 0;
298 return 0;
299}
300
301/* Parse the "enabled" server keyword */
302static int srv_parse_enabled(char **args, int *cur_arg,
303 struct proxy *curproxy, struct server *newsrv, char **err)
304{
Emeric Brun52a91d32017-08-31 14:41:55 +0200305 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
306 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100307 newsrv->check.state &= ~CHK_ST_PAUSED;
308 newsrv->check.health = newsrv->check.rise;
309 return 0;
310}
311
Willy Tarreau9c538e02019-01-23 10:21:49 +0100312static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
313{
314 char *arg;
315
316 arg = args[*cur_arg + 1];
317 if (!*arg) {
318 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
319 return ERR_ALERT | ERR_FATAL;
320 }
321 newsrv->max_reuse = atoi(arg);
322
323 return 0;
324}
325
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100326static 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 +0100327{
328 const char *res;
329 char *arg;
330 unsigned int time;
331
332 arg = args[*cur_arg + 1];
333 if (!*arg) {
334 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
335 return ERR_ALERT | ERR_FATAL;
336 }
337 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200338 if (res == PARSE_TIME_OVER) {
339 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
340 args[*cur_arg+1], args[*cur_arg]);
341 return ERR_ALERT | ERR_FATAL;
342 }
343 else if (res == PARSE_TIME_UNDER) {
344 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
345 args[*cur_arg+1], args[*cur_arg]);
346 return ERR_ALERT | ERR_FATAL;
347 }
348 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100349 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
350 *res, args[*cur_arg]);
351 return ERR_ALERT | ERR_FATAL;
352 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100353 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100354
355 return 0;
356}
357
Olivier Houchard006e3102018-12-10 18:30:32 +0100358static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
359{
360 char *arg;
361
362 arg = args[*cur_arg + 1];
363 if (!*arg) {
364 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
365 return ERR_ALERT | ERR_FATAL;
366 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100367
Olivier Houchard006e3102018-12-10 18:30:32 +0100368 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100369 if ((int)newsrv->max_idle_conns < -1) {
370 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
371 return ERR_ALERT | ERR_FATAL;
372 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100373
374 return 0;
375}
376
Willy Tarreaudff55432012-10-10 17:51:05 +0200377/* parse the "id" server keyword */
378static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
379{
380 struct eb32_node *node;
381
382 if (!*args[*cur_arg + 1]) {
383 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
384 return ERR_ALERT | ERR_FATAL;
385 }
386
387 newsrv->puid = atol(args[*cur_arg + 1]);
388 newsrv->conf.id.key = newsrv->puid;
389
390 if (newsrv->puid <= 0) {
391 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
392 return ERR_ALERT | ERR_FATAL;
393 }
394
395 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
396 if (node) {
397 struct server *target = container_of(node, struct server, conf.id);
398 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
399 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
400 target->id);
401 return ERR_ALERT | ERR_FATAL;
402 }
403
404 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200405 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200406 return 0;
407}
408
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100409/* Parse the "namespace" server keyword */
410static int srv_parse_namespace(char **args, int *cur_arg,
411 struct proxy *curproxy, struct server *newsrv, char **err)
412{
Willy Tarreaue5733232019-05-22 19:24:06 +0200413#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100414 char *arg;
415
416 arg = args[*cur_arg + 1];
417 if (!*arg) {
418 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
419 return ERR_ALERT | ERR_FATAL;
420 }
421
422 if (!strcmp(arg, "*")) {
423 /* Use the namespace associated with the connection (if present). */
424 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
425 return 0;
426 }
427
428 /*
429 * As this parser may be called several times for the same 'default-server'
430 * object, or for a new 'server' instance deriving from a 'default-server'
431 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
432 */
433 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
434
435 newsrv->netns = netns_store_lookup(arg, strlen(arg));
436 if (!newsrv->netns)
437 newsrv->netns = netns_store_insert(arg);
438
439 if (!newsrv->netns) {
440 memprintf(err, "Cannot open namespace '%s'", arg);
441 return ERR_ALERT | ERR_FATAL;
442 }
443
444 return 0;
445#else
446 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
447 return ERR_ALERT | ERR_FATAL;
448#endif
449}
450
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100451/* Parse the "no-backup" server keyword */
452static int srv_parse_no_backup(char **args, int *cur_arg,
453 struct proxy *curproxy, struct server *newsrv, char **err)
454{
455 newsrv->flags &= ~SRV_F_BACKUP;
456 return 0;
457}
458
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100459
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100460/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200461static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100462{
463 srv->pp_opts &= ~flags;
464 return 0;
465}
466
467/* Parse the "no-send-proxy" server keyword */
468static int srv_parse_no_send_proxy(char **args, int *cur_arg,
469 struct proxy *curproxy, struct server *newsrv, char **err)
470{
471 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
472}
473
474/* Parse the "no-send-proxy-v2" server keyword */
475static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
476 struct proxy *curproxy, struct server *newsrv, char **err)
477{
478 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
479}
480
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200481/* Parse the "no-tfo" server keyword */
482static int srv_parse_no_tfo(char **args, int *cur_arg,
483 struct proxy *curproxy, struct server *newsrv, char **err)
484{
485 newsrv->flags &= ~SRV_F_FASTOPEN;
486 return 0;
487}
488
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100489/* Parse the "non-stick" server keyword */
490static int srv_parse_non_stick(char **args, int *cur_arg,
491 struct proxy *curproxy, struct server *newsrv, char **err)
492{
493 newsrv->flags |= SRV_F_NON_STICK;
494 return 0;
495}
496
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100497/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200498static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100499{
500 srv->pp_opts |= flags;
501 return 0;
502}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200503/* parse the "proto" server keyword */
504static int srv_parse_proto(char **args, int *cur_arg,
505 struct proxy *px, struct server *newsrv, char **err)
506{
507 struct ist proto;
508
509 if (!*args[*cur_arg + 1]) {
510 memprintf(err, "'%s' : missing value", args[*cur_arg]);
511 return ERR_ALERT | ERR_FATAL;
512 }
513 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
514 newsrv->mux_proto = get_mux_proto(proto);
515 if (!newsrv->mux_proto) {
516 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
517 return ERR_ALERT | ERR_FATAL;
518 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200519 return 0;
520}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100521
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100522/* parse the "proxy-v2-options" */
523static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
524 struct proxy *px, struct server *newsrv, char **err)
525{
526 char *p, *n;
527 for (p = args[*cur_arg+1]; p; p = n) {
528 n = strchr(p, ',');
529 if (n)
530 *n++ = '\0';
531 if (!strcmp(p, "ssl")) {
532 newsrv->pp_opts |= SRV_PP_V2_SSL;
533 } else if (!strcmp(p, "cert-cn")) {
534 newsrv->pp_opts |= SRV_PP_V2_SSL;
535 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100536 } else if (!strcmp(p, "cert-key")) {
537 newsrv->pp_opts |= SRV_PP_V2_SSL;
538 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
539 } else if (!strcmp(p, "cert-sig")) {
540 newsrv->pp_opts |= SRV_PP_V2_SSL;
541 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
542 } else if (!strcmp(p, "ssl-cipher")) {
543 newsrv->pp_opts |= SRV_PP_V2_SSL;
544 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100545 } else if (!strcmp(p, "authority")) {
546 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100547 } else if (!strcmp(p, "crc32c")) {
548 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100549 } else if (!strcmp(p, "unique-id")) {
550 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100551 } else
552 goto fail;
553 }
554 return 0;
555 fail:
556 if (err)
557 memprintf(err, "'%s' : proxy v2 option not implemented", p);
558 return ERR_ALERT | ERR_FATAL;
559}
560
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100561/* Parse the "observe" server keyword */
562static int srv_parse_observe(char **args, int *cur_arg,
563 struct proxy *curproxy, struct server *newsrv, char **err)
564{
565 char *arg;
566
567 arg = args[*cur_arg + 1];
568 if (!*arg) {
569 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
570 return ERR_ALERT | ERR_FATAL;
571 }
572
573 if (!strcmp(arg, "none")) {
574 newsrv->observe = HANA_OBS_NONE;
575 }
576 else if (!strcmp(arg, "layer4")) {
577 newsrv->observe = HANA_OBS_LAYER4;
578 }
579 else if (!strcmp(arg, "layer7")) {
580 if (curproxy->mode != PR_MODE_HTTP) {
581 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
582 return ERR_ALERT;
583 }
584 newsrv->observe = HANA_OBS_LAYER7;
585 }
586 else {
587 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
588 "but got '%s'\n", args[*cur_arg], arg);
589 return ERR_ALERT | ERR_FATAL;
590 }
591
592 return 0;
593}
594
Frédéric Lécaille16186232017-03-14 16:42:49 +0100595/* Parse the "redir" server keyword */
596static int srv_parse_redir(char **args, int *cur_arg,
597 struct proxy *curproxy, struct server *newsrv, char **err)
598{
599 char *arg;
600
601 arg = args[*cur_arg + 1];
602 if (!*arg) {
603 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
604 return ERR_ALERT | ERR_FATAL;
605 }
606
607 free(newsrv->rdr_pfx);
608 newsrv->rdr_pfx = strdup(arg);
609 newsrv->rdr_len = strlen(arg);
610
611 return 0;
612}
613
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100614/* Parse the "send-proxy" server keyword */
615static int srv_parse_send_proxy(char **args, int *cur_arg,
616 struct proxy *curproxy, struct server *newsrv, char **err)
617{
618 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
619}
620
621/* Parse the "send-proxy-v2" server keyword */
622static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
623 struct proxy *curproxy, struct server *newsrv, char **err)
624{
625 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
626}
627
Frédéric Lécailledba97072017-03-17 15:33:50 +0100628
629/* Parse the "source" server keyword */
630static int srv_parse_source(char **args, int *cur_arg,
631 struct proxy *curproxy, struct server *newsrv, char **err)
632{
633 char *errmsg;
634 int port_low, port_high;
635 struct sockaddr_storage *sk;
636 struct protocol *proto;
637
638 errmsg = NULL;
639
640 if (!*args[*cur_arg + 1]) {
641 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
642 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
643 goto err;
644 }
645
646 /* 'sk' is statically allocated (no need to be freed). */
647 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
648 if (!sk) {
649 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
650 goto err;
651 }
652
653 proto = protocol_by_family(sk->ss_family);
654 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100655 ha_alert("'%s %s' : connect() not supported for this address family.\n",
656 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100657 goto err;
658 }
659
660 newsrv->conn_src.opts |= CO_SRC_BIND;
661 newsrv->conn_src.source_addr = *sk;
662
663 if (port_low != port_high) {
664 int i;
665
666 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100667 ha_alert("'%s' does not support port offsets (found '%s').\n",
668 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100669 goto err;
670 }
671
672 if (port_low <= 0 || port_low > 65535 ||
673 port_high <= 0 || port_high > 65535 ||
674 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100675 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100676 goto err;
677 }
678 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
679 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
680 newsrv->conn_src.sport_range->ports[i] = port_low + i;
681 }
682
683 *cur_arg += 2;
684 while (*(args[*cur_arg])) {
685 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
686#if defined(CONFIG_HAP_TRANSPARENT)
687 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100688 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
689 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100690 goto err;
691 }
692 if (!strcmp(args[*cur_arg + 1], "client")) {
693 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
694 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
695 }
696 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
697 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
698 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
699 }
700 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
701 char *name, *end;
702
703 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100704 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100705 name++;
706
707 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100708 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100709 end++;
710
711 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
712 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
713 free(newsrv->conn_src.bind_hdr_name);
714 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
715 newsrv->conn_src.bind_hdr_len = end - name;
716 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
717 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
718 newsrv->conn_src.bind_hdr_occ = -1;
719
720 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100721 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100722 end++;
723 if (*end == ',') {
724 end++;
725 name = end;
726 if (*end == '-')
727 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100728 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100729 end++;
730 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
731 }
732
733 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100734 ha_alert("usesrc hdr_ip(name,num) does not support negative"
735 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100736 goto err;
737 }
738 }
739 else {
740 struct sockaddr_storage *sk;
741 int port1, port2;
742
743 /* 'sk' is statically allocated (no need to be freed). */
744 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
745 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100746 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100747 goto err;
748 }
749
750 proto = protocol_by_family(sk->ss_family);
751 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100752 ha_alert("'%s %s' : connect() not supported for this address family.\n",
753 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100754 goto err;
755 }
756
757 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
759 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 goto err;
761 }
762 newsrv->conn_src.tproxy_addr = *sk;
763 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
764 }
765 global.last_checks |= LSTCHK_NETADM;
766 *cur_arg += 2;
767 continue;
768#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100769 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 +0100770 goto err;
771#endif /* defined(CONFIG_HAP_TRANSPARENT) */
772 } /* "usesrc" */
773
774 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
775#ifdef SO_BINDTODEVICE
776 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100777 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100778 goto err;
779 }
780 free(newsrv->conn_src.iface_name);
781 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
782 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
783 global.last_checks |= LSTCHK_NETADM;
784#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100785 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100786 goto err;
787#endif
788 *cur_arg += 2;
789 continue;
790 }
791 /* this keyword in not an option of "source" */
792 break;
793 } /* while */
794
795 return 0;
796
797 err:
798 free(errmsg);
799 return ERR_ALERT | ERR_FATAL;
800}
801
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100802/* Parse the "stick" server keyword */
803static int srv_parse_stick(char **args, int *cur_arg,
804 struct proxy *curproxy, struct server *newsrv, char **err)
805{
806 newsrv->flags &= ~SRV_F_NON_STICK;
807 return 0;
808}
809
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100810/* Parse the "track" server keyword */
811static int srv_parse_track(char **args, int *cur_arg,
812 struct proxy *curproxy, struct server *newsrv, char **err)
813{
814 char *arg;
815
816 arg = args[*cur_arg + 1];
817 if (!*arg) {
818 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
819 return ERR_ALERT | ERR_FATAL;
820 }
821
822 free(newsrv->trackit);
823 newsrv->trackit = strdup(arg);
824
825 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800826}
827
828/* Parse the "socks4" server keyword */
829static int srv_parse_socks4(char **args, int *cur_arg,
830 struct proxy *curproxy, struct server *newsrv, char **err)
831{
832 char *errmsg;
833 int port_low, port_high;
834 struct sockaddr_storage *sk;
835 struct protocol *proto;
836
837 errmsg = NULL;
838
839 if (!*args[*cur_arg + 1]) {
840 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
841 goto err;
842 }
843
844 /* 'sk' is statically allocated (no need to be freed). */
845 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
846 if (!sk) {
847 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
848 goto err;
849 }
850
851 proto = protocol_by_family(sk->ss_family);
852 if (!proto || !proto->connect) {
853 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
854 goto err;
855 }
856
857 newsrv->flags |= SRV_F_SOCKS4_PROXY;
858 newsrv->socks4_addr = *sk;
859
860 if (port_low != port_high) {
861 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
862 goto err;
863 }
864
865 if (!port_low) {
866 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
867 goto err;
868 }
869
870 return 0;
871
872 err:
873 free(errmsg);
874 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100875}
876
Frédéric Lécailledba97072017-03-17 15:33:50 +0100877
Willy Tarreau034c88c2017-01-23 23:36:45 +0100878/* parse the "tfo" server keyword */
879static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
880{
881 newsrv->flags |= SRV_F_FASTOPEN;
882 return 0;
883}
884
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200885/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200886 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200887 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200888 *
889 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200890 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200891void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200892{
Willy Tarreau87b09662015-04-03 00:22:06 +0200893 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200894
Willy Tarreau87b09662015-04-03 00:22:06 +0200895 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
896 if (stream->srv_conn == srv)
897 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200898}
899
900/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200901 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200902 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200903 *
904 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200905 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200906void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200907{
908 struct server *srv;
909
910 for (srv = px->srv; srv != NULL; srv = srv->next)
911 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200912 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200913}
914
Willy Tarreaubda92272014-05-20 21:55:30 +0200915/* Appends some information to a message string related to a server going UP or
916 * DOWN. If both <forced> and <reason> are null and the server tracks another
917 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200918 * If <check> is non-null, an entire string describing the check result will be
919 * appended after a comma and a space (eg: to report some information from the
920 * check that changed the state). In the other case, the string will be built
921 * using the check results stored into the struct server if present.
922 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200923 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200924 *
925 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200926 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200927void srv_append_status(struct buffer *msg, struct server *s,
928 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200929{
Emeric Brun5a133512017-10-19 14:42:30 +0200930 short status = s->op_st_chg.status;
931 short code = s->op_st_chg.code;
932 long duration = s->op_st_chg.duration;
933 char *desc = s->op_st_chg.reason;
934
935 if (check) {
936 status = check->status;
937 code = check->code;
938 duration = check->duration;
939 desc = check->desc;
940 }
941
942 if (status != -1) {
943 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
944
945 if (status >= HCHK_STATUS_L57DATA)
946 chunk_appendf(msg, ", code: %d", code);
947
948 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200949 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200950
951 chunk_appendf(msg, ", info: \"");
952
953 chunk_initlen(&src, desc, 0, strlen(desc));
954 chunk_asciiencode(msg, &src, '"');
955
956 chunk_appendf(msg, "\"");
957 }
958
959 if (duration >= 0)
960 chunk_appendf(msg, ", check duration: %ldms", duration);
961 }
962 else if (desc && *desc) {
963 chunk_appendf(msg, ", %s", desc);
964 }
965 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200966 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200967 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200968
969 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200970 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200971 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
972 " %d sessions active, %d requeued, %d remaining in queue",
973 s->proxy->srv_act, s->proxy->srv_bck,
974 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
975 s->cur_sess, xferred, s->nbpend);
976 else
977 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
978 " %d sessions requeued, %d total in queue",
979 s->proxy->srv_act, s->proxy->srv_bck,
980 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
981 xferred, s->nbpend);
982 }
983}
984
Emeric Brun5a133512017-10-19 14:42:30 +0200985/* Marks server <s> down, regardless of its checks' statuses. The server is
986 * registered in a list to postpone the counting of the remaining servers on
987 * the proxy and transfers queued streams whenever possible to other servers at
988 * a sync point. Maintenance servers are ignored. It stores the <reason> if
989 * non-null as the reason for going down or the available data from the check
990 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200991 *
992 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200993 */
Emeric Brun5a133512017-10-19 14:42:30 +0200994void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200995{
996 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200997
Emeric Brun64cc49c2017-10-03 14:46:45 +0200998 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200999 return;
1000
Emeric Brun52a91d32017-08-31 14:41:55 +02001001 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001002 *s->op_st_chg.reason = 0;
1003 s->op_st_chg.status = -1;
1004 if (reason) {
1005 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1006 }
1007 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001008 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001009 s->op_st_chg.code = check->code;
1010 s->op_st_chg.status = check->status;
1011 s->op_st_chg.duration = check->duration;
1012 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001013
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001014 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001015 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001016
Emeric Brun9f0b4582017-10-23 14:39:51 +02001017 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001018 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001019 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001020 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001021 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001022}
1023
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001024/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001025 * in maintenance. The server is registered in a list to postpone the counting
1026 * of the remaining servers on the proxy and tries to grab requests from the
1027 * proxy at a sync point. Maintenance servers are ignored. It stores the
1028 * <reason> if non-null as the reason for going down or the available data
1029 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001030 *
1031 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001032 */
Emeric Brun5a133512017-10-19 14:42:30 +02001033void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001034{
1035 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001036
Emeric Brun64cc49c2017-10-03 14:46:45 +02001037 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038 return;
1039
Emeric Brun52a91d32017-08-31 14:41:55 +02001040 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001041 return;
1042
Emeric Brun52a91d32017-08-31 14:41:55 +02001043 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001044 *s->op_st_chg.reason = 0;
1045 s->op_st_chg.status = -1;
1046 if (reason) {
1047 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1048 }
1049 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001050 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001051 s->op_st_chg.code = check->code;
1052 s->op_st_chg.status = check->status;
1053 s->op_st_chg.duration = check->duration;
1054 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001055
Emeric Brun64cc49c2017-10-03 14:46:45 +02001056 if (s->slowstart <= 0)
1057 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001058
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001059 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001060 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001061
Emeric Brun9f0b4582017-10-23 14:39:51 +02001062 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001063 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001064 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001066 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001067}
1068
Willy Tarreau8eb77842014-05-21 13:54:57 +02001069/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001070 * isn't in maintenance. The server is registered in a list to postpone the
1071 * counting of the remaining servers on the proxy and tries to grab requests
1072 * from the proxy. Maintenance servers are ignored. It stores the
1073 * <reason> if non-null as the reason for going down or the available data
1074 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001075 * up. Note that it makes use of the trash to build the log strings, so <reason>
1076 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001077 *
1078 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001079 */
Emeric Brun5a133512017-10-19 14:42:30 +02001080void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001081{
1082 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001083
Emeric Brun64cc49c2017-10-03 14:46:45 +02001084 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085 return;
1086
Emeric Brun52a91d32017-08-31 14:41:55 +02001087 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001088 return;
1089
Emeric Brun52a91d32017-08-31 14:41:55 +02001090 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001091 *s->op_st_chg.reason = 0;
1092 s->op_st_chg.status = -1;
1093 if (reason) {
1094 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1095 }
1096 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001097 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001098 s->op_st_chg.code = check->code;
1099 s->op_st_chg.status = check->status;
1100 s->op_st_chg.duration = check->duration;
1101 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001102
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001103 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001104 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001105
Emeric Brun9f0b4582017-10-23 14:39:51 +02001106 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001107 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001108 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001109 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001110 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001111}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001112
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001113/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1114 * enforce either maint mode or drain mode. It is not allowed to set more than
1115 * one flag at once. The equivalent "inherited" flag is propagated to all
1116 * tracking servers. Maintenance mode disables health checks (but not agent
1117 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001118 * is done. If <cause> is non-null, it will be displayed at the end of the log
1119 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001120 *
1121 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001122 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001123void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001124{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001125 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126
1127 if (!mode)
1128 return;
1129
1130 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001131 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132 return;
1133
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001135 if (cause)
1136 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1137
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001138 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001139 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001140
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001141 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001142 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1143 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001144 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001145
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001146 /* compute the inherited flag to propagate */
1147 if (mode & SRV_ADMF_MAINT)
1148 mode = SRV_ADMF_IMAINT;
1149 else if (mode & SRV_ADMF_DRAIN)
1150 mode = SRV_ADMF_IDRAIN;
1151
Emeric Brun9f0b4582017-10-23 14:39:51 +02001152 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001153 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001154 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001155 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001156 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001157}
1158
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001159/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1160 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1161 * than one flag at once. The equivalent "inherited" flag is propagated to all
1162 * tracking servers. Leaving maintenance mode re-enables health checks. When
1163 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001164 *
1165 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001166 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001167void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001168{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001169 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170
1171 if (!mode)
1172 return;
1173
1174 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001175 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001176 return;
1177
Emeric Brun52a91d32017-08-31 14:41:55 +02001178 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001179
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001180 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001181 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001182
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001183 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001184 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1185 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001186 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001187
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001188 if (mode & SRV_ADMF_MAINT)
1189 mode = SRV_ADMF_IMAINT;
1190 else if (mode & SRV_ADMF_DRAIN)
1191 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001192
Emeric Brun9f0b4582017-10-23 14:39:51 +02001193 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001194 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001195 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001196 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001197 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001198}
1199
Willy Tarreau757478e2016-11-03 19:22:19 +01001200/* principle: propagate maint and drain to tracking servers. This is useful
1201 * upon startup so that inherited states are correct.
1202 */
1203static void srv_propagate_admin_state(struct server *srv)
1204{
1205 struct server *srv2;
1206
1207 if (!srv->trackers)
1208 return;
1209
1210 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001211 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001212 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001213 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001214
Emeric Brun52a91d32017-08-31 14:41:55 +02001215 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001216 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001217 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001218 }
1219}
1220
1221/* Compute and propagate the admin states for all servers in proxy <px>.
1222 * Only servers *not* tracking another one are considered, because other
1223 * ones will be handled when the server they track is visited.
1224 */
1225void srv_compute_all_admin_states(struct proxy *px)
1226{
1227 struct server *srv;
1228
1229 for (srv = px->srv; srv; srv = srv->next) {
1230 if (srv->track)
1231 continue;
1232 srv_propagate_admin_state(srv);
1233 }
1234}
1235
Willy Tarreaudff55432012-10-10 17:51:05 +02001236/* Note: must not be declared <const> as its list will be overwritten.
1237 * Please take care of keeping this list alphabetically sorted, doing so helps
1238 * all code contributors.
1239 * Optional keywords are also declared with a NULL ->parse() function so that
1240 * the config parser can report an appropriate error when a known keyword was
1241 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001242 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001243 */
1244static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001245 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001246 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001247 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1248 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001249 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001250 { "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 +01001251 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001252 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001253 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1254 { "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 +02001255 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001256 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001257 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001258 { "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 +01001259 { "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 +02001260 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001261 { "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 +01001262 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001263 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1264 { "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 +02001265 { "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 +01001266 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001267 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001268 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001269 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001270 { NULL, NULL, 0 },
1271}};
1272
Willy Tarreau0108d902018-11-25 19:14:37 +01001273INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001274
Willy Tarreau004e0452013-11-21 11:22:01 +01001275/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001276 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001277 * state is automatically disabled if the time is elapsed. If <must_update> is
1278 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001279 *
1280 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001281 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001282void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001283{
1284 struct proxy *px = sv->proxy;
1285 unsigned w;
1286
1287 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1288 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001289 if (sv->next_state == SRV_ST_STARTING)
1290 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001291 }
1292
1293 /* We must take care of not pushing the server to full throttle during slow starts.
1294 * It must also start immediately, at least at the minimal step when leaving maintenance.
1295 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001296 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001297 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1298 else
1299 w = px->lbprm.wdiv;
1300
Emeric Brun52a91d32017-08-31 14:41:55 +02001301 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001302
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001303 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001304 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001305 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001306}
1307
Willy Tarreaubaaee002006-06-26 02:48:02 +02001308/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001309 * Parses weight_str and configures sv accordingly.
1310 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001311 *
1312 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001313 */
1314const char *server_parse_weight_change_request(struct server *sv,
1315 const char *weight_str)
1316{
1317 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001318 long int w;
1319 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001320
1321 px = sv->proxy;
1322
1323 /* if the weight is terminated with '%', it is set relative to
1324 * the initial weight, otherwise it is absolute.
1325 */
1326 if (!*weight_str)
1327 return "Require <weight> or <weight%>.\n";
1328
Simon Hormanb796afa2013-02-12 10:45:53 +09001329 w = strtol(weight_str, &end, 10);
1330 if (end == weight_str)
1331 return "Empty weight string empty or preceded by garbage";
1332 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001333 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001334 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001335 /* Avoid integer overflow */
1336 if (w > 25600)
1337 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001338 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001339 if (w > 256)
1340 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001341 }
1342 else if (w < 0 || w > 256)
1343 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001344 else if (end[0] != '\0')
1345 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001346
1347 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1348 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1349
1350 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001351 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001352
1353 return NULL;
1354}
1355
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001356/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001357 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1358 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001359 * Returns:
1360 * - error string on error
1361 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001362 *
1363 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001364 */
1365const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001366 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001367{
1368 unsigned char ip[INET6_ADDRSTRLEN];
1369
1370 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001371 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001372 return NULL;
1373 }
1374 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001375 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001376 return NULL;
1377 }
1378
1379 return "Could not understand IP address format.\n";
1380}
1381
Willy Tarreau46b7f532018-08-21 11:54:26 +02001382/*
1383 * Must be called with the server lock held.
1384 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001385const char *server_parse_maxconn_change_request(struct server *sv,
1386 const char *maxconn_str)
1387{
1388 long int v;
1389 char *end;
1390
1391 if (!*maxconn_str)
1392 return "Require <maxconn>.\n";
1393
1394 v = strtol(maxconn_str, &end, 10);
1395 if (end == maxconn_str)
1396 return "maxconn string empty or preceded by garbage";
1397 else if (end[0] != '\0')
1398 return "Trailing garbage in maxconn string";
1399
1400 if (sv->maxconn == sv->minconn) { // static maxconn
1401 sv->maxconn = sv->minconn = v;
1402 } else { // dynamic maxconn
1403 sv->maxconn = v;
1404 }
1405
1406 if (may_dequeue_tasks(sv, sv->proxy))
1407 process_srv_queue(sv);
1408
1409 return NULL;
1410}
1411
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001412#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001413static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1414 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001415{
1416 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001417 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001418 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001419 NULL,
1420 };
1421
1422 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001423 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001424
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001425 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001426}
1427
1428static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1429{
1430 struct sample_expr *expr;
1431
1432 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 +01001433 if (!expr) {
1434 memprintf(err, "error detected while parsing sni expression : %s", *err);
1435 return ERR_ALERT | ERR_FATAL;
1436 }
1437
1438 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1439 memprintf(err, "error detected while parsing sni expression : "
1440 " fetch method '%s' extracts information from '%s', "
1441 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001442 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001443 return ERR_ALERT | ERR_FATAL;
1444 }
1445
1446 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1447 release_sample_expr(newsrv->ssl_ctx.sni);
1448 newsrv->ssl_ctx.sni = expr;
1449
1450 return 0;
1451}
1452#endif
1453
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001454static 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 +01001455{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001456 char *msg = "error encountered while processing ";
1457 char *quote = "'";
1458 char *token = args[cur_arg];
1459
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001460 if (err && *err) {
1461 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001462 msg = *err;
1463 quote = "";
1464 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001465 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001466
1467 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1468 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1469 file, linenum, args[0], args[1],
1470 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001471 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001472 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1473 file, linenum, args[0], args[1],
1474 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001475}
1476
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001477static void srv_conn_src_sport_range_cpy(struct server *srv,
1478 struct server *src)
1479{
1480 int range_sz;
1481
1482 range_sz = src->conn_src.sport_range->size;
1483 if (range_sz > 0) {
1484 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1485 if (srv->conn_src.sport_range != NULL) {
1486 int i;
1487
1488 for (i = 0; i < range_sz; i++) {
1489 srv->conn_src.sport_range->ports[i] =
1490 src->conn_src.sport_range->ports[i];
1491 }
1492 }
1493 }
1494}
1495
1496/*
1497 * Copy <src> server connection source settings to <srv> server everything needed.
1498 */
1499static void srv_conn_src_cpy(struct server *srv, struct server *src)
1500{
1501 srv->conn_src.opts = src->conn_src.opts;
1502 srv->conn_src.source_addr = src->conn_src.source_addr;
1503
1504 /* Source port range copy. */
1505 if (src->conn_src.sport_range != NULL)
1506 srv_conn_src_sport_range_cpy(srv, src);
1507
1508#ifdef CONFIG_HAP_TRANSPARENT
1509 if (src->conn_src.bind_hdr_name != NULL) {
1510 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1511 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1512 }
1513 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1514 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1515#endif
1516 if (src->conn_src.iface_name != NULL)
1517 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1518}
1519
1520/*
1521 * Copy <src> server SSL settings to <srv> server allocating
1522 * everything needed.
1523 */
1524#if defined(USE_OPENSSL)
1525static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1526{
1527 if (src->ssl_ctx.ca_file != NULL)
1528 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1529 if (src->ssl_ctx.crl_file != NULL)
1530 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1531 if (src->ssl_ctx.client_crt != NULL)
1532 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1533
1534 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1535
1536 if (src->ssl_ctx.verify_host != NULL)
1537 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1538 if (src->ssl_ctx.ciphers != NULL)
1539 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001540 if (src->ssl_ctx.options)
1541 srv->ssl_ctx.options = src->ssl_ctx.options;
1542 if (src->ssl_ctx.methods.flags)
1543 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1544 if (src->ssl_ctx.methods.min)
1545 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1546 if (src->ssl_ctx.methods.max)
1547 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1548
Willy Tarreau5db847a2019-05-09 14:13:35 +02001549#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001550 if (src->ssl_ctx.ciphersuites != NULL)
1551 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1552#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001553 if (src->sni_expr != NULL)
1554 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001555
1556#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1557 if (src->ssl_ctx.alpn_str) {
1558 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1559 if (srv->ssl_ctx.alpn_str) {
1560 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1561 src->ssl_ctx.alpn_len);
1562 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1563 }
1564 }
1565#endif
1566#ifdef OPENSSL_NPN_NEGOTIATED
1567 if (src->ssl_ctx.npn_str) {
1568 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1569 if (srv->ssl_ctx.npn_str) {
1570 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1571 src->ssl_ctx.npn_len);
1572 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1573 }
1574 }
1575#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001576}
1577#endif
1578
1579/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001580 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001581 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001582 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001583 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001584static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001585{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001586 char *hostname_dn;
1587 int hostname_len, hostname_dn_len;
1588
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001589 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001590 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001591
Christopher Faulet67957bd2017-09-27 11:00:59 +02001592 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001593 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001594 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1595 hostname_dn, trash.size);
1596 if (hostname_dn_len == -1)
1597 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001598
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001599
Christopher Faulet67957bd2017-09-27 11:00:59 +02001600 free(srv->hostname);
1601 free(srv->hostname_dn);
1602 srv->hostname = strdup(hostname);
1603 srv->hostname_dn = strdup(hostname_dn);
1604 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001605 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001606 goto err;
1607
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001608 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001609
1610 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001611 free(srv->hostname); srv->hostname = NULL;
1612 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001613 return -1;
1614}
1615
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001616/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001617 * Copy <src> server settings to <srv> server allocating
1618 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001619 * This function is not supposed to be called at any time, but only
1620 * during server settings parsing or during server allocations from
1621 * a server template, and just after having calloc()'ed a new server.
1622 * So, <src> may only be a default server (when parsing server settings)
1623 * or a server template (during server allocations from a server template).
1624 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1625 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001626 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001627static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001628{
1629 /* Connection source settings copy */
1630 srv_conn_src_cpy(srv, src);
1631
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001632 if (srv_tmpl) {
1633 srv->addr = src->addr;
1634 srv->svc_port = src->svc_port;
1635 }
1636
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001637 srv->pp_opts = src->pp_opts;
1638 if (src->rdr_pfx != NULL) {
1639 srv->rdr_pfx = strdup(src->rdr_pfx);
1640 srv->rdr_len = src->rdr_len;
1641 }
1642 if (src->cookie != NULL) {
1643 srv->cookie = strdup(src->cookie);
1644 srv->cklen = src->cklen;
1645 }
1646 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001647 srv->check.addr = src->check.addr;
1648 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001649 srv->check.use_ssl = src->check.use_ssl;
1650 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001651 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001652 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001653 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001654 /* Note: 'flags' field has potentially been already initialized. */
1655 srv->flags |= src->flags;
1656 srv->do_check = src->do_check;
1657 srv->do_agent = src->do_agent;
1658 if (srv->check.port)
1659 srv->flags |= SRV_F_CHECKPORT;
1660 srv->check.inter = src->check.inter;
1661 srv->check.fastinter = src->check.fastinter;
1662 srv->check.downinter = src->check.downinter;
1663 srv->agent.use_ssl = src->agent.use_ssl;
1664 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001665
1666 if (src->agent.tcpcheck_rules) {
1667 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1668 if (srv->agent.tcpcheck_rules) {
1669 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1670 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1671 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1672 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1673 &src->agent.tcpcheck_rules->preset_vars);
1674 }
1675 }
1676
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001677 srv->agent.inter = src->agent.inter;
1678 srv->agent.fastinter = src->agent.fastinter;
1679 srv->agent.downinter = src->agent.downinter;
1680 srv->maxqueue = src->maxqueue;
1681 srv->minconn = src->minconn;
1682 srv->maxconn = src->maxconn;
1683 srv->slowstart = src->slowstart;
1684 srv->observe = src->observe;
1685 srv->onerror = src->onerror;
1686 srv->onmarkeddown = src->onmarkeddown;
1687 srv->onmarkedup = src->onmarkedup;
1688 if (src->trackit != NULL)
1689 srv->trackit = strdup(src->trackit);
1690 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1691 srv->uweight = srv->iweight = src->iweight;
1692
1693 srv->check.send_proxy = src->check.send_proxy;
1694 /* health: up, but will fall down at first failure */
1695 srv->check.rise = srv->check.health = src->check.rise;
1696 srv->check.fall = src->check.fall;
1697
1698 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001699 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1700 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1701 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001702 srv->check.state |= CHK_ST_PAUSED;
1703 srv->check.health = 0;
1704 }
1705
1706 /* health: up but will fall down at first failure */
1707 srv->agent.rise = srv->agent.health = src->agent.rise;
1708 srv->agent.fall = src->agent.fall;
1709
1710 if (src->resolvers_id != NULL)
1711 srv->resolvers_id = strdup(src->resolvers_id);
1712 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001713 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001714 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001715 if (srv->dns_opts.family_prio == AF_UNSPEC)
1716 srv->dns_opts.family_prio = AF_INET6;
1717 memcpy(srv->dns_opts.pref_net,
1718 src->dns_opts.pref_net,
1719 sizeof srv->dns_opts.pref_net);
1720 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1721
1722 srv->init_addr_methods = src->init_addr_methods;
1723 srv->init_addr = src->init_addr;
1724#if defined(USE_OPENSSL)
1725 srv_ssl_settings_cpy(srv, src);
1726#endif
1727#ifdef TCP_USER_TIMEOUT
1728 srv->tcp_ut = src->tcp_ut;
1729#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001730 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001731 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001732 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001733 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001734
Olivier Houchard8da5f982017-08-04 18:35:36 +02001735 if (srv_tmpl)
1736 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001737
1738 srv->check.via_socks4 = src->check.via_socks4;
1739 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001740}
1741
William Lallemand313bfd12018-10-26 14:47:32 +02001742struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001743{
1744 struct server *srv;
1745
1746 srv = calloc(1, sizeof *srv);
1747 if (!srv)
1748 return NULL;
1749
1750 srv->obj_type = OBJ_TYPE_SERVER;
1751 srv->proxy = proxy;
1752 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001753 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001754
Emeric Brun52a91d32017-08-31 14:41:55 +02001755 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001756 srv->last_change = now.tv_sec;
1757
Christopher Faulet38290462020-04-21 11:46:40 +02001758 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001759 srv->check.status = HCHK_STATUS_INI;
1760 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001761 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001762 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001763
Christopher Faulet38290462020-04-21 11:46:40 +02001764 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001765 srv->agent.status = HCHK_STATUS_INI;
1766 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001767 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001768 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1769
Willy Tarreau975b1552019-06-06 16:25:55 +02001770 /* please don't put default server settings here, they are set in
1771 * init_default_instance().
1772 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001773 return srv;
1774}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001775
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001776#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1777static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1778 struct server *srv, struct proxy *proxy)
1779{
1780 int ret;
1781 char *err = NULL;
1782
1783 if (!srv->sni_expr)
1784 return 0;
1785
1786 ret = server_parse_sni_expr(srv, proxy, &err);
1787 if (!ret)
1788 return 0;
1789
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001790 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001791 free(err);
1792
1793 return ret;
1794}
1795#endif
1796
1797/*
1798 * Server initializations finalization.
1799 * Initialize health check, agent check and SNI expression if enabled.
1800 * Must not be called for a default server instance.
1801 */
1802static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1803 struct server *srv, struct proxy *px)
1804{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001805#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001806 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001807#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001808
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001809 if (srv->do_check && srv->trackit) {
1810 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1811 file, linenum);
1812 return ERR_ALERT | ERR_FATAL;
1813 }
1814
1815 if (srv->do_agent && !srv->agent.port) {
1816 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1817 file, linenum, srv->id);
1818 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001819 }
1820
1821#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1822 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1823 return ret;
1824#endif
1825
1826 if (srv->flags & SRV_F_BACKUP)
1827 px->srv_bck++;
1828 else
1829 px->srv_act++;
1830 srv_lb_commit_status(srv);
1831
1832 return 0;
1833}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001834
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001835/*
1836 * Parse as much as possible such a range string argument: low[-high]
1837 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1838 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1839 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1840 * Returns 0 if succeeded, -1 if not.
1841 */
1842static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1843{
1844 char *nb_high_arg;
1845
1846 *nb_high = 0;
1847 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001848 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001849
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001850 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001851 *nb_high_arg++ = '\0';
1852 *nb_high = atoi(nb_high_arg);
1853 }
1854 else {
1855 *nb_high += *nb_low;
1856 *nb_low = 1;
1857 }
1858
1859 if (*nb_low < 0 || *nb_high < *nb_low)
1860 return -1;
1861
1862 return 0;
1863}
1864
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001865static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1866{
1867 chunk_printf(&trash, "%s%d", prefix, nb);
1868 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001869 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001870}
1871
1872/*
1873 * Initialize as much as possible servers from <srv> server template.
1874 * Note that a server template is a special server with
1875 * a few different parameters than a server which has
1876 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001877 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001878 * 'srv' template included.
1879 */
1880static int server_template_init(struct server *srv, struct proxy *px)
1881{
1882 int i;
1883 struct server *newsrv;
1884
1885 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 +02001886 newsrv = new_server(px);
1887 if (!newsrv)
1888 goto err;
1889
1890 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001891 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001892#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1893 if (newsrv->sni_expr) {
1894 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1895 if (!newsrv->ssl_ctx.sni)
1896 goto err;
1897 }
1898#endif
1899 /* Set this new server ID. */
1900 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1901
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001902 /* Linked backwards first. This will be restablished after parsing. */
1903 newsrv->next = px->srv;
1904 px->srv = newsrv;
1905 }
1906 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1907
1908 return i - srv->tmpl_info.nb_low;
1909
1910 err:
1911 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1912 if (newsrv) {
1913#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1914 release_sample_expr(newsrv->ssl_ctx.sni);
1915#endif
1916 free_check(&newsrv->agent);
1917 free_check(&newsrv->check);
1918 }
1919 free(newsrv);
1920 return i - srv->tmpl_info.nb_low;
1921}
1922
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001923int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr, int in_peers_section)
Willy Tarreau272adea2014-03-31 10:39:59 +02001924{
1925 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001926 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001927 char *errmsg = NULL;
1928 int err_code = 0;
1929 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001930 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001931
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001932 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001933 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001934 !strcmp(args[0], "default-server") ||
1935 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001936 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001937 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001938 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001939 int srv_tmpl = !defsrv && !srv;
1940 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001941
1942 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001943 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001944 err_code |= ERR_ALERT | ERR_FATAL;
1945 goto out;
1946 }
1947 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001948 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001949
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001950 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001951 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001952 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001953 if (in_peers_section) {
1954 return 0;
1955 }
1956 else {
1957 /* 'server' line number of argument check. */
1958 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1959 file, linenum, args[0]);
1960 err_code |= ERR_ALERT | ERR_FATAL;
1961 goto out;
1962 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001963 }
1964
1965 err = invalid_char(args[1]);
1966 }
1967 else if (srv_tmpl) {
1968 if (!*args[3]) {
1969 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001970 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 +02001971 file, linenum, args[0]);
1972 err_code |= ERR_ALERT | ERR_FATAL;
1973 goto out;
1974 }
1975
1976 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001977 }
1978
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001979 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001980 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 +02001981 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001982 err_code |= ERR_ALERT | ERR_FATAL;
1983 goto out;
1984 }
1985
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001986 cur_arg = 2;
1987 if (srv_tmpl) {
1988 /* Parse server-template <nb | range> arg. */
1989 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001990 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001991 file, linenum, args[0], args[cur_arg]);
1992 err_code |= ERR_ALERT | ERR_FATAL;
1993 goto out;
1994 }
1995 cur_arg++;
1996 }
1997
Willy Tarreau272adea2014-03-31 10:39:59 +02001998 if (!defsrv) {
1999 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002000 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002001 struct protocol *proto;
2002
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002003 newsrv = new_server(curproxy);
2004 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002005 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 err_code |= ERR_ALERT | ERR_ABORT;
2007 goto out;
2008 }
2009
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002010 if (srv_tmpl) {
2011 newsrv->tmpl_info.nb_low = tmpl_range_low;
2012 newsrv->tmpl_info.nb_high = tmpl_range_high;
2013 }
2014
Willy Tarreau272adea2014-03-31 10:39:59 +02002015 /* the servers are linked backwards first */
2016 newsrv->next = curproxy->srv;
2017 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002018 newsrv->conf.file = strdup(file);
2019 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002020 /* Note: for a server template, its id is its prefix.
2021 * This is a temporary id which will be used for server allocations to come
2022 * after parsing.
2023 */
2024 if (srv)
2025 newsrv->id = strdup(args[1]);
2026 else
2027 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002028
2029 /* several ways to check the port component :
2030 * - IP => port=+0, relative (IPv4 only)
2031 * - IP: => port=+0, relative
2032 * - IP:N => port=N, absolute
2033 * - IP:+N => port=+N, relative
2034 * - IP:-N => port=-N, relative
2035 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002036 if (!parse_addr)
2037 goto skip_addr;
2038
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002039 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002040 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002041 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002042 err_code |= ERR_ALERT | ERR_FATAL;
2043 goto out;
2044 }
2045
2046 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002047 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002048 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002049 file, linenum, args[0], args[1]);
2050 err_code |= ERR_ALERT | ERR_FATAL;
2051 goto out;
2052 }
2053
2054 if (!port1 || !port2) {
2055 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002056 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002057 }
2058 else if (port1 != port2) {
2059 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002060 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002061 file, linenum, args[0], args[1], args[2]);
2062 err_code |= ERR_ALERT | ERR_FATAL;
2063 goto out;
2064 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002065
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002066 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002067 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002068 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002069 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002070 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2071 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2072 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002073 err_code |= ERR_ALERT | ERR_FATAL;
2074 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002075 }
2076 }
2077 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002078 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002079 file, linenum, newsrv->id);
2080 err_code |= ERR_ALERT | ERR_FATAL;
2081 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002082 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002083 }
2084
Willy Tarreau272adea2014-03-31 10:39:59 +02002085 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002086 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002087
Olivier Houchard8da5f982017-08-04 18:35:36 +02002088 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002089 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002090 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002091 err_code |= ERR_ALERT | ERR_FATAL;
2092 goto out;
2093 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002094
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002095 cur_arg++;
2096 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002097 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002098 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002099 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002100 } else {
2101 newsrv = &curproxy->defsrv;
2102 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002103 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002104 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002105 }
2106
2107 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002108 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002109 char *p, *end;
2110 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002111 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002112
2113 newsrv->init_addr_methods = 0;
2114 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2115
2116 for (p = args[cur_arg + 1]; *p; p = end) {
2117 /* cut on next comma */
2118 for (end = p; *end && *end != ','; end++);
2119 if (*end)
2120 *(end++) = 0;
2121
Willy Tarreau4310d362016-11-02 15:05:56 +01002122 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002123 if (!strcmp(p, "libc")) {
2124 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2125 }
2126 else if (!strcmp(p, "last")) {
2127 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2128 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002129 else if (!strcmp(p, "none")) {
2130 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2131 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002132 else if (str2ip2(p, &sa, 0)) {
2133 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002134 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002135 file, linenum, args[cur_arg], p);
2136 err_code |= ERR_ALERT | ERR_FATAL;
2137 goto out;
2138 }
2139 newsrv->init_addr = sa;
2140 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2141 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002142 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002143 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 +02002144 file, linenum, args[cur_arg], p);
2145 err_code |= ERR_ALERT | ERR_FATAL;
2146 goto out;
2147 }
2148 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002149 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002150 file, linenum, args[cur_arg], p);
2151 err_code |= ERR_ALERT | ERR_FATAL;
2152 goto out;
2153 }
2154 }
2155 cur_arg += 2;
2156 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002157 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002158 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002159 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2160 cur_arg += 2;
2161 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002162 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2163 char *p, *end;
2164
2165 for (p = args[cur_arg + 1]; *p; p = end) {
2166 /* cut on next comma */
2167 for (end = p; *end && *end != ','; end++);
2168 if (*end)
2169 *(end++) = 0;
2170
2171 if (!strcmp(p, "allow-dup-ip")) {
2172 newsrv->dns_opts.accept_duplicate_ip = 1;
2173 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002174 else if (!strcmp(p, "ignore-weight")) {
2175 newsrv->dns_opts.ignore_weight = 1;
2176 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002177 else if (!strcmp(p, "prevent-dup-ip")) {
2178 newsrv->dns_opts.accept_duplicate_ip = 0;
2179 }
2180 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002181 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 +02002182 file, linenum, args[cur_arg], p);
2183 err_code |= ERR_ALERT | ERR_FATAL;
2184 goto out;
2185 }
2186 }
2187
2188 cur_arg += 2;
2189 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002190 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2191 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002192 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002193 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002194 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002195 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002196 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002197 file, linenum, args[cur_arg]);
2198 err_code |= ERR_ALERT | ERR_FATAL;
2199 goto out;
2200 }
2201 cur_arg += 2;
2202 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002203 else if (!strcmp(args[cur_arg], "resolve-net")) {
2204 char *p, *e;
2205 unsigned char mask;
2206 struct dns_options *opt;
2207
2208 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002209 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002210 file, linenum, args[cur_arg]);
2211 err_code |= ERR_ALERT | ERR_FATAL;
2212 goto out;
2213 }
2214
2215 opt = &newsrv->dns_opts;
2216
2217 /* Split arguments by comma, and convert it from ipv4 or ipv6
2218 * string network in in_addr or in6_addr.
2219 */
2220 p = args[cur_arg + 1];
2221 e = p;
2222 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002223 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002224 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002225 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002226 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
2229 }
2230 /* look for end or comma. */
2231 while (*e != ',' && *e != '\0')
2232 e++;
2233 if (*e == ',') {
2234 *e = '\0';
2235 e++;
2236 }
2237 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2238 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2239 /* Try to convert input string from ipv4 or ipv6 network. */
2240 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2241 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2242 &mask)) {
2243 /* Try to convert input string from ipv6 network. */
2244 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2245 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2246 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002247 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002248 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002249 file, linenum, args[cur_arg], p);
2250 err_code |= ERR_ALERT | ERR_FATAL;
2251 goto out;
2252 }
2253 opt->pref_net_nb++;
2254 p = e;
2255 }
2256
2257 cur_arg += 2;
2258 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002259 else if (!strcmp(args[cur_arg], "weight")) {
2260 int w;
2261 w = atol(args[cur_arg + 1]);
2262 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002263 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002264 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2265 err_code |= ERR_ALERT | ERR_FATAL;
2266 goto out;
2267 }
2268 newsrv->uweight = newsrv->iweight = w;
2269 cur_arg += 2;
2270 }
Emeric Brun97556472020-05-30 01:42:45 +02002271 else if (!strcmp(args[cur_arg], "log-proto")) {
2272 if (!strcmp(args[cur_arg + 1], "legacy"))
2273 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2274 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2275 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2276 else {
2277 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2278 "'octet-count' but got '%s'\n",
2279 file, linenum, args[cur_arg], args[cur_arg + 1]);
2280 err_code |= ERR_ALERT | ERR_FATAL;
2281 goto out;
2282 }
2283 cur_arg += 2;
2284 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 else if (!strcmp(args[cur_arg], "minconn")) {
2286 newsrv->minconn = atol(args[cur_arg + 1]);
2287 cur_arg += 2;
2288 }
2289 else if (!strcmp(args[cur_arg], "maxconn")) {
2290 newsrv->maxconn = atol(args[cur_arg + 1]);
2291 cur_arg += 2;
2292 }
2293 else if (!strcmp(args[cur_arg], "maxqueue")) {
2294 newsrv->maxqueue = atol(args[cur_arg + 1]);
2295 cur_arg += 2;
2296 }
2297 else if (!strcmp(args[cur_arg], "slowstart")) {
2298 /* slowstart is stored in seconds */
2299 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002300
2301 if (err == PARSE_TIME_OVER) {
2302 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2303 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2304 err_code |= ERR_ALERT | ERR_FATAL;
2305 goto out;
2306 }
2307 else if (err == PARSE_TIME_UNDER) {
2308 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2309 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2310 err_code |= ERR_ALERT | ERR_FATAL;
2311 goto out;
2312 }
2313 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002314 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002315 file, linenum, *err, newsrv->id);
2316 err_code |= ERR_ALERT | ERR_FATAL;
2317 goto out;
2318 }
2319 newsrv->slowstart = (val + 999) / 1000;
2320 cur_arg += 2;
2321 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002322 else if (!strcmp(args[cur_arg], "on-error")) {
2323 if (!strcmp(args[cur_arg + 1], "fastinter"))
2324 newsrv->onerror = HANA_ONERR_FASTINTER;
2325 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2326 newsrv->onerror = HANA_ONERR_FAILCHK;
2327 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2328 newsrv->onerror = HANA_ONERR_SUDDTH;
2329 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2330 newsrv->onerror = HANA_ONERR_MARKDWN;
2331 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002332 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002333 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2334 file, linenum, args[cur_arg], args[cur_arg + 1]);
2335 err_code |= ERR_ALERT | ERR_FATAL;
2336 goto out;
2337 }
2338
2339 cur_arg += 2;
2340 }
2341 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2342 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2343 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2344 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002345 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002346 file, linenum, args[cur_arg], args[cur_arg + 1]);
2347 err_code |= ERR_ALERT | ERR_FATAL;
2348 goto out;
2349 }
2350
2351 cur_arg += 2;
2352 }
2353 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2354 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2355 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2356 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002357 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002358 file, linenum, args[cur_arg], args[cur_arg + 1]);
2359 err_code |= ERR_ALERT | ERR_FATAL;
2360 goto out;
2361 }
2362
2363 cur_arg += 2;
2364 }
2365 else if (!strcmp(args[cur_arg], "error-limit")) {
2366 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002367 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002368 file, linenum, args[cur_arg]);
2369 err_code |= ERR_ALERT | ERR_FATAL;
2370 goto out;
2371 }
2372
2373 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2374
2375 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002376 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002377 file, linenum, args[cur_arg]);
2378 err_code |= ERR_ALERT | ERR_FATAL;
2379 goto out;
2380 }
2381 cur_arg += 2;
2382 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002383 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002384 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002385 file, linenum, "usesrc", "source");
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002388 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002389 else {
2390 static int srv_dumped;
2391 struct srv_kw *kw;
2392 char *err;
2393
2394 kw = srv_find_kw(args[cur_arg]);
2395 if (kw) {
2396 char *err = NULL;
2397 int code;
2398
2399 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002400 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 +02002401 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002402 if (kw->skip != -1)
2403 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002404 err_code |= ERR_ALERT | ERR_FATAL;
2405 goto out;
2406 }
2407
2408 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002409 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002410 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002411 if (kw->skip != -1)
2412 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002413 err_code |= ERR_ALERT;
2414 continue;
2415 }
2416
2417 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2418 err_code |= code;
2419
2420 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002421 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002422 if (code & ERR_FATAL) {
2423 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002424 if (kw->skip != -1)
2425 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 goto out;
2427 }
2428 }
2429 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002430 if (kw->skip != -1)
2431 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002432 continue;
2433 }
2434
2435 err = NULL;
2436 if (!srv_dumped) {
2437 srv_dump_kws(&err);
2438 indent_msg(&err, 4);
2439 srv_dumped = 1;
2440 }
2441
Christopher Faulet767a84b2017-11-24 16:50:31 +01002442 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002443 file, linenum, args[0], args[1], args[cur_arg],
2444 err ? " Registered keywords :" : "", err ? err : "");
2445 free(err);
2446
2447 err_code |= ERR_ALERT | ERR_FATAL;
2448 goto out;
2449 }
2450 }
2451
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002452 if (!defsrv)
2453 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2454 if (err_code & ERR_FATAL)
2455 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002456 if (srv_tmpl)
2457 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002458 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002459 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002460 return 0;
2461
2462 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002463 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 free(errmsg);
2465 return err_code;
2466}
2467
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002468/* Returns a pointer to the first server matching either id <id>.
2469 * NULL is returned if no match is found.
2470 * the lookup is performed in the backend <bk>
2471 */
2472struct server *server_find_by_id(struct proxy *bk, int id)
2473{
2474 struct eb32_node *eb32;
2475 struct server *curserver;
2476
2477 if (!bk || (id ==0))
2478 return NULL;
2479
2480 /* <bk> has no backend capabilities, so it can't have a server */
2481 if (!(bk->cap & PR_CAP_BE))
2482 return NULL;
2483
2484 curserver = NULL;
2485
2486 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2487 if (eb32)
2488 curserver = container_of(eb32, struct server, conf.id);
2489
2490 return curserver;
2491}
2492
2493/* Returns a pointer to the first server matching either name <name>, or id
2494 * if <name> starts with a '#'. NULL is returned if no match is found.
2495 * the lookup is performed in the backend <bk>
2496 */
2497struct server *server_find_by_name(struct proxy *bk, const char *name)
2498{
2499 struct server *curserver;
2500
2501 if (!bk || !name)
2502 return NULL;
2503
2504 /* <bk> has no backend capabilities, so it can't have a server */
2505 if (!(bk->cap & PR_CAP_BE))
2506 return NULL;
2507
2508 curserver = NULL;
2509 if (*name == '#') {
2510 curserver = server_find_by_id(bk, atoi(name + 1));
2511 if (curserver)
2512 return curserver;
2513 }
2514 else {
2515 curserver = bk->srv;
2516
2517 while (curserver && (strcmp(curserver->id, name) != 0))
2518 curserver = curserver->next;
2519
2520 if (curserver)
2521 return curserver;
2522 }
2523
2524 return NULL;
2525}
2526
2527struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2528{
2529 struct server *byname;
2530 struct server *byid;
2531
2532 if (!name && !id)
2533 return NULL;
2534
2535 if (diff)
2536 *diff = 0;
2537
2538 byname = byid = NULL;
2539
2540 if (name) {
2541 byname = server_find_by_name(bk, name);
2542 if (byname && (!id || byname->puid == id))
2543 return byname;
2544 }
2545
2546 /* remaining possibilities :
2547 * - name not set
2548 * - name set but not found
2549 * - name found but ID doesn't match
2550 */
2551 if (id) {
2552 byid = server_find_by_id(bk, id);
2553 if (byid) {
2554 if (byname) {
2555 /* use id only if forced by configuration */
2556 if (byid->flags & SRV_F_FORCED_ID) {
2557 if (diff)
2558 *diff |= 2;
2559 return byid;
2560 }
2561 else {
2562 if (diff)
2563 *diff |= 1;
2564 return byname;
2565 }
2566 }
2567
2568 /* remaining possibilities:
2569 * - name not set
2570 * - name set but not found
2571 */
2572 if (name && diff)
2573 *diff |= 2;
2574 return byid;
2575 }
2576
2577 /* id bot found */
2578 if (byname) {
2579 if (diff)
2580 *diff |= 1;
2581 return byname;
2582 }
2583 }
2584
2585 return NULL;
2586}
2587
Willy Tarreau46b7f532018-08-21 11:54:26 +02002588/* Update a server state using the parameters available in the params list.
2589 *
2590 * Grabs the server lock during operation.
2591 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002592static void srv_update_state(struct server *srv, int version, char **params)
2593{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002594 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002595 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002596
2597 /* fields since version 1
2598 * and common to all other upcoming versions
2599 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002600 enum srv_state srv_op_state;
2601 enum srv_admin srv_admin_state;
2602 unsigned srv_uweight, srv_iweight;
2603 unsigned long srv_last_time_change;
2604 short srv_check_status;
2605 enum chk_result srv_check_result;
2606 int srv_check_health;
2607 int srv_check_state, srv_agent_state;
2608 int bk_f_forced_id;
2609 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002610 int fqdn_set_by_cli;
2611 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002612 const char *port_str;
2613 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002614 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002615
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002616 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002617 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002618 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002619 switch (version) {
2620 case 1:
2621 /*
2622 * now we can proceed with server's state update:
2623 * srv_addr: params[0]
2624 * srv_op_state: params[1]
2625 * srv_admin_state: params[2]
2626 * srv_uweight: params[3]
2627 * srv_iweight: params[4]
2628 * srv_last_time_change: params[5]
2629 * srv_check_status: params[6]
2630 * srv_check_result: params[7]
2631 * srv_check_health: params[8]
2632 * srv_check_state: params[9]
2633 * srv_agent_state: params[10]
2634 * bk_f_forced_id: params[11]
2635 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002636 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002637 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002638 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002639 */
2640
2641 /* validating srv_op_state */
2642 p = NULL;
2643 errno = 0;
2644 srv_op_state = strtol(params[1], &p, 10);
2645 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2646 (srv_op_state != SRV_ST_STOPPED &&
2647 srv_op_state != SRV_ST_STARTING &&
2648 srv_op_state != SRV_ST_RUNNING &&
2649 srv_op_state != SRV_ST_STOPPING)) {
2650 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2651 }
2652
2653 /* validating srv_admin_state */
2654 p = NULL;
2655 errno = 0;
2656 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002657 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002658
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002659 /* inherited statuses will be recomputed later.
2660 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2661 */
2662 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002663
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002664 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2665 (srv_admin_state != 0 &&
2666 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002667 srv_admin_state != SRV_ADMF_CMAINT &&
2668 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002669 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002670 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002671 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2672 }
2673
2674 /* validating srv_uweight */
2675 p = NULL;
2676 errno = 0;
2677 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002678 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002679 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2680
2681 /* validating srv_iweight */
2682 p = NULL;
2683 errno = 0;
2684 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002685 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002686 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2687
2688 /* validating srv_last_time_change */
2689 p = NULL;
2690 errno = 0;
2691 srv_last_time_change = strtol(params[5], &p, 10);
2692 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2693 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2694
2695 /* validating srv_check_status */
2696 p = NULL;
2697 errno = 0;
2698 srv_check_status = strtol(params[6], &p, 10);
2699 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2700 (srv_check_status >= HCHK_STATUS_SIZE))
2701 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2702
2703 /* validating srv_check_result */
2704 p = NULL;
2705 errno = 0;
2706 srv_check_result = strtol(params[7], &p, 10);
2707 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2708 (srv_check_result != CHK_RES_UNKNOWN &&
2709 srv_check_result != CHK_RES_NEUTRAL &&
2710 srv_check_result != CHK_RES_FAILED &&
2711 srv_check_result != CHK_RES_PASSED &&
2712 srv_check_result != CHK_RES_CONDPASS)) {
2713 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2714 }
2715
2716 /* validating srv_check_health */
2717 p = NULL;
2718 errno = 0;
2719 srv_check_health = strtol(params[8], &p, 10);
2720 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2721 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2722
2723 /* validating srv_check_state */
2724 p = NULL;
2725 errno = 0;
2726 srv_check_state = strtol(params[9], &p, 10);
2727 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2728 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2729 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2730
2731 /* validating srv_agent_state */
2732 p = NULL;
2733 errno = 0;
2734 srv_agent_state = strtol(params[10], &p, 10);
2735 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2736 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2737 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2738
2739 /* validating bk_f_forced_id */
2740 p = NULL;
2741 errno = 0;
2742 bk_f_forced_id = strtol(params[11], &p, 10);
2743 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2744 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2745
2746 /* validating srv_f_forced_id */
2747 p = NULL;
2748 errno = 0;
2749 srv_f_forced_id = strtol(params[12], &p, 10);
2750 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2751 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2752
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002753 /* validating srv_fqdn */
2754 fqdn = params[13];
2755 if (fqdn && *fqdn == '-')
2756 fqdn = NULL;
2757 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2758 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2759 fqdn = NULL;
2760 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002761
Frédéric Lécaille31694712017-08-01 08:47:19 +02002762 port_str = params[14];
2763 if (port_str) {
2764 port = strl2uic(port_str, strlen(port_str));
2765 if (port > USHRT_MAX) {
2766 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2767 port_str = NULL;
2768 }
2769 }
2770
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002771 /* SRV record
2772 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2773 */
2774 srvrecord = params[15];
2775 if (srvrecord && *srvrecord != '_')
2776 srvrecord = NULL;
2777
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002778 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002779 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002780 goto out;
2781
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002782 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002783 /* recover operational state and apply it to this server
2784 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002785 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002786 switch (srv_op_state) {
2787 case SRV_ST_STOPPED:
2788 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002789 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002790 break;
2791 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002792 /* If rise == 1 there is no STARTING state, let's switch to
2793 * RUNNING
2794 */
2795 if (srv->check.rise == 1) {
2796 srv->check.health = srv->check.rise + srv->check.fall - 1;
2797 srv_set_running(srv, "", NULL);
2798 break;
2799 }
2800 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2801 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002802 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002803 break;
2804 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002805 /* If fall == 1 there is no STOPPING state, let's switch to
2806 * STOPPED
2807 */
2808 if (srv->check.fall == 1) {
2809 srv->check.health = 0;
2810 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2811 break;
2812 }
2813 if (srv->check.health < srv->check.rise ||
2814 srv->check.health > srv->check.rise + srv->check.fall - 2)
2815 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002816 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002817 break;
2818 case SRV_ST_RUNNING:
2819 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002820 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002821 break;
2822 }
2823
2824 /* When applying server state, the following rules apply:
2825 * - in case of a configuration change, we apply the setting from the new
2826 * configuration, regardless of old running state
2827 * - if no configuration change, we apply old running state only if old running
2828 * state is different from new configuration state
2829 */
2830 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002831 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2832 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002833 srv_adm_set_maint(srv);
2834 else
2835 srv_adm_set_ready(srv);
2836 }
2837 /* configuration is the same, let's compate old running state and new conf state */
2838 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002839 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002840 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002841 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002842 srv_adm_set_ready(srv);
2843 }
2844 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002845 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002846 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002847 * (srv->iweight is the weight set up in configuration).
2848 * There are two possible reasons for FDRAIN to have been present :
2849 * - previous config weight was zero
2850 * - "set server b/s drain" was sent to the CLI
2851 *
2852 * In the first case, we simply want to drop this drain state
2853 * if the new weight is not zero anymore, meaning the administrator
2854 * has intentionally turned the weight back to a positive value to
2855 * enable the server again after an operation. In the second case,
2856 * the drain state was forced on the CLI regardless of the config's
2857 * weight so we don't want a change to the config weight to lose this
2858 * status. What this means is :
2859 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2860 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002861 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002862 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002863 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002864 }
2865
2866 srv->last_change = date.tv_sec - srv_last_time_change;
2867 srv->check.status = srv_check_status;
2868 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002869
2870 /* Only case we want to apply is removing ENABLED flag which could have been
2871 * done by the "disable health" command over the stats socket
2872 */
2873 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2874 (srv_check_state & CHK_ST_CONFIGURED) &&
2875 !(srv_check_state & CHK_ST_ENABLED))
2876 srv->check.state &= ~CHK_ST_ENABLED;
2877
2878 /* Only case we want to apply is removing ENABLED flag which could have been
2879 * done by the "disable agent" command over the stats socket
2880 */
2881 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2882 (srv_agent_state & CHK_ST_CONFIGURED) &&
2883 !(srv_agent_state & CHK_ST_ENABLED))
2884 srv->agent.state &= ~CHK_ST_ENABLED;
2885
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002886 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2887 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002888 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002889 * It means that a configuration file change has precedence over a unix socket change
2890 * for server's weight
2891 *
2892 * by default, HAProxy applies the following weight when parsing the configuration
2893 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002894 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002895 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002896 srv->uweight = srv_uweight;
2897 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002898 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002899
Willy Tarreaue5a60682016-11-09 14:54:53 +01002900 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002901 if (strcmp(params[0], "-"))
2902 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002903
2904 if (fqdn && srv->hostname) {
2905 if (!strcmp(srv->hostname, fqdn)) {
2906 /* Here we reset the 'set from stats socket FQDN' flag
2907 * to support such transitions:
2908 * Let's say initial FQDN value is foo1 (in configuration file).
2909 * - FQDN changed from stats socket, from foo1 to foo2 value,
2910 * - FQDN changed again from file configuration (with the same previous value
2911 set from stats socket, from foo1 to foo2 value),
2912 * - reload for any other reason than a FQDN modification,
2913 * the configuration file FQDN matches the fqdn server state file value.
2914 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002915 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002916 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002917 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002918 }
2919 else {
2920 /* If the FDQN has been changed from stats socket,
2921 * apply fqdn state file value (which is the value set
2922 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002923 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002924 */
2925 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002926 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002927 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002928 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002929 }
2930 }
2931 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002932 /* If all the conditions below are validated, this means
2933 * we're evaluating a server managed by SRV resolution
2934 */
2935 else if (fqdn && !srv->hostname && srvrecord) {
2936 int res;
2937
2938 /* we can't apply previous state if SRV record has changed */
2939 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2940 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);
2941 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2942 goto out;
2943 }
2944
2945 /* create or find a SRV resolution for this srv record */
2946 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2947 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2948 if (srv->srvrq == NULL) {
2949 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2950 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2951 goto out;
2952 }
2953
2954 /* prepare DNS resolution for this server */
2955 res = srv_prepare_for_resolution(srv, fqdn);
2956 if (res == -1) {
2957 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2958 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2959 goto out;
2960 }
2961
2962 /* configure check.port accordingly */
2963 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2964 !(srv->flags & SRV_F_CHECKPORT))
2965 srv->check.port = port;
2966
2967 /* Unset SRV_F_MAPPORTS for SRV records.
2968 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2969 * because no ports are provided in the configuration file.
2970 * This is because HAProxy will use the port found into the SRV record.
2971 */
2972 srv->flags &= ~SRV_F_MAPPORTS;
2973 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002974
Frédéric Lécaille31694712017-08-01 08:47:19 +02002975 if (port_str)
2976 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002977 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002978
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002979 break;
2980 default:
2981 chunk_appendf(msg, ", version '%d' not supported", version);
2982 }
2983
2984 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002985 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002986 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002987 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002988 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002989 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002990}
2991
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002992
2993/*
2994 * read next line from file <f> and return the server state version if one found.
2995 * If no version is found, then 0 is returned
2996 * Note that this should be the first read on <f>
2997 */
2998static int srv_state_get_version(FILE *f) {
2999 char buf[2];
3000 int ret;
3001
3002 /* first character of first line of the file must contain the version of the export */
3003 if (fgets(buf, 2, f) == NULL) {
3004 return 0;
3005 }
3006
3007 ret = atoi(buf);
3008 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3009 (ret > SRV_STATE_FILE_VERSION_MAX))
3010 return 0;
3011
3012 return ret;
3013}
3014
3015
3016/*
3017 * parses server state line stored in <buf> and supposedly in version <version>.
3018 * Set <params> and <srv_params> accordingly.
3019 * In case of error, params[0] is set to NULL.
3020 */
3021static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3022{
3023 int buflen, arg, srv_arg;
3024 char *cur, *end;
3025
3026 buflen = strlen(buf);
3027 cur = buf;
3028 end = cur + buflen;
3029
3030 /* we need at least one character */
3031 if (buflen == 0) {
3032 params[0] = NULL;
3033 return;
3034 }
3035
3036 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003037 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003038 ++cur;
3039
3040 /* Ignore empty or commented lines */
3041 if (cur == end || *cur == '#') {
3042 params[0] = NULL;
3043 return;
3044 }
3045
3046 /* truncated lines */
3047 if (buf[buflen - 1] != '\n') {
3048 //ha_warning("server-state file '%s': truncated line\n", filepath);
3049 params[0] = NULL;
3050 return;
3051 }
3052
3053 /* Removes trailing '\n' */
3054 buf[buflen - 1] = '\0';
3055
3056 /* we're now ready to move the line into *srv_params[] */
3057 params[0] = cur;
3058 arg = 1;
3059 srv_arg = 0;
3060 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003061 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003062 *cur = '\0';
3063 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003064 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003065 ++cur;
3066 switch (version) {
3067 case 1:
3068 /*
3069 * srv_addr: params[4] => srv_params[0]
3070 * srv_op_state: params[5] => srv_params[1]
3071 * srv_admin_state: params[6] => srv_params[2]
3072 * srv_uweight: params[7] => srv_params[3]
3073 * srv_iweight: params[8] => srv_params[4]
3074 * srv_last_time_change: params[9] => srv_params[5]
3075 * srv_check_status: params[10] => srv_params[6]
3076 * srv_check_result: params[11] => srv_params[7]
3077 * srv_check_health: params[12] => srv_params[8]
3078 * srv_check_state: params[13] => srv_params[9]
3079 * srv_agent_state: params[14] => srv_params[10]
3080 * bk_f_forced_id: params[15] => srv_params[11]
3081 * srv_f_forced_id: params[16] => srv_params[12]
3082 * srv_fqdn: params[17] => srv_params[13]
3083 * srv_port: params[18] => srv_params[14]
3084 * srvrecord: params[19] => srv_params[15]
3085 */
3086 if (arg >= 4) {
3087 srv_params[srv_arg] = cur;
3088 ++srv_arg;
3089 }
3090 break;
3091 }
3092
3093 params[arg] = cur;
3094 ++arg;
3095 }
3096 else {
3097 ++cur;
3098 }
3099 }
3100
3101 /* if line is incomplete line, then ignore it.
3102 * otherwise, update useful flags */
3103 switch (version) {
3104 case 1:
3105 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3106 params[0] = NULL;
3107 return;
3108 }
3109 break;
3110 }
3111
3112 return;
3113}
3114
3115
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003116/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3117 * For each proxy, it does the following:
3118 * - opens its server state file (either one or local one)
3119 * - read whole file, line by line
3120 * - analyse each line to check if it matches our current backend:
3121 * - backend name matches
3122 * - backend id matches if id is forced and name doesn't match
3123 * - if the server pointed by the line is found, then state is applied
3124 *
3125 * If the running backend uuid or id differs from the state file, then HAProxy reports
3126 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003127 *
3128 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003129 */
3130void apply_server_state(void)
3131{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003132 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003133 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3134 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003135 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003136 FILE *f;
3137 char *filepath;
3138 char globalfilepath[MAXPATHLEN + 1];
3139 char localfilepath[MAXPATHLEN + 1];
3140 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003141 struct proxy *curproxy, *bk;
3142 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003143 char *line;
3144 char *bkname, *srvname;
3145 struct state_line *st;
3146 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003147
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003148
3149 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003150 globalfilepathlen = 0;
3151 /* create the globalfilepath variable */
3152 if (global.server_state_file) {
3153 /* absolute path or no base directory provided */
3154 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3155 len = strlen(global.server_state_file);
3156 if (len > MAXPATHLEN) {
3157 globalfilepathlen = 0;
3158 goto globalfileerror;
3159 }
3160 memcpy(globalfilepath, global.server_state_file, len);
3161 globalfilepath[len] = '\0';
3162 globalfilepathlen = len;
3163 }
3164 else if (global.server_state_base) {
3165 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003166 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003167 globalfilepathlen = 0;
3168 goto globalfileerror;
3169 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003170 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003171 globalfilepath[len] = 0;
3172 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003173
3174 /* append a slash if needed */
3175 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3176 if (globalfilepathlen + 1 > MAXPATHLEN) {
3177 globalfilepathlen = 0;
3178 goto globalfileerror;
3179 }
3180 globalfilepath[globalfilepathlen++] = '/';
3181 }
3182
3183 len = strlen(global.server_state_file);
3184 if (globalfilepathlen + len > MAXPATHLEN) {
3185 globalfilepathlen = 0;
3186 goto globalfileerror;
3187 }
3188 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3189 globalfilepathlen += len;
3190 globalfilepath[globalfilepathlen++] = 0;
3191 }
3192 }
3193 globalfileerror:
3194 if (globalfilepathlen == 0)
3195 globalfilepath[0] = '\0';
3196
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003197 /* Load global server state in a tree */
3198 if (globalfilepathlen > 0) {
3199 errno = 0;
3200 f = fopen(globalfilepath, "r");
3201 if (errno)
3202 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003203 if (!f)
3204 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003205
3206 global_file_version = srv_state_get_version(f);
3207 if (global_file_version == 0)
3208 goto out_load_server_state_in_tree;
3209
3210 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3211 line = NULL;
3212
3213 line = strdup(mybuf);
3214 if (line == NULL)
3215 continue;
3216
3217 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3218 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003219 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003220
3221 /* bkname */
3222 bkname = params[1];
3223 /* srvname */
3224 srvname = params[3];
3225
3226 /* key */
3227 chunk_printf(&trash, "%s %s", bkname, srvname);
3228
3229 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003230 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003231 if (st == NULL) {
3232 goto nextline;
3233 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003234 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003235 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3236 /* this is a duplicate key, probably a hand-crafted file,
3237 * drop it!
3238 */
3239 goto nextline;
3240 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003241
3242 /* save line */
3243 st->line = line;
3244
3245 continue;
3246
3247 nextline:
3248 /* free up memory in case of error during the processing of the line */
3249 free(line);
3250 }
3251 }
3252 out_load_server_state_in_tree:
3253
3254 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003255 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003256 /* servers are only in backends */
3257 if (!(curproxy->cap & PR_CAP_BE))
3258 continue;
3259 fileopenerr = 0;
3260 filepath = NULL;
3261
3262 /* search server state file path and name */
3263 switch (curproxy->load_server_state_from_file) {
3264 /* read servers state from global file */
3265 case PR_SRV_STATE_FILE_GLOBAL:
3266 /* there was an error while generating global server state file path */
3267 if (globalfilepathlen == 0)
3268 continue;
3269 filepath = globalfilepath;
3270 fileopenerr = 1;
3271 break;
3272 /* this backend has its own file */
3273 case PR_SRV_STATE_FILE_LOCAL:
3274 localfilepathlen = 0;
3275 localfilepath[0] = '\0';
3276 len = 0;
3277 /* create the localfilepath variable */
3278 /* absolute path or no base directory provided */
3279 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3280 len = strlen(curproxy->server_state_file_name);
3281 if (len > MAXPATHLEN) {
3282 localfilepathlen = 0;
3283 goto localfileerror;
3284 }
3285 memcpy(localfilepath, curproxy->server_state_file_name, len);
3286 localfilepath[len] = '\0';
3287 localfilepathlen = len;
3288 }
3289 else if (global.server_state_base) {
3290 len = strlen(global.server_state_base);
3291 localfilepathlen += len;
3292
3293 if (localfilepathlen > MAXPATHLEN) {
3294 localfilepathlen = 0;
3295 goto localfileerror;
3296 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003297 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003298 localfilepath[localfilepathlen] = 0;
3299
3300 /* append a slash if needed */
3301 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3302 if (localfilepathlen + 1 > MAXPATHLEN) {
3303 localfilepathlen = 0;
3304 goto localfileerror;
3305 }
3306 localfilepath[localfilepathlen++] = '/';
3307 }
3308
3309 len = strlen(curproxy->server_state_file_name);
3310 if (localfilepathlen + len > MAXPATHLEN) {
3311 localfilepathlen = 0;
3312 goto localfileerror;
3313 }
3314 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3315 localfilepathlen += len;
3316 localfilepath[localfilepathlen++] = 0;
3317 }
3318 filepath = localfilepath;
3319 localfileerror:
3320 if (localfilepathlen == 0)
3321 localfilepath[0] = '\0';
3322
3323 break;
3324 case PR_SRV_STATE_FILE_NONE:
3325 default:
3326 continue;
3327 }
3328
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003329 /* when global file is used, we get data from the tree
3330 * Note that in such case we don't check backend name neither uuid.
3331 * Backend name can't be wrong since it's used as a key to retrieve the server state
3332 * line from the tree.
3333 */
3334 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3335 struct server *srv = curproxy->srv;
3336 while (srv) {
3337 struct ebmb_node *node;
3338 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003339
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003340 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3341 node = ebst_lookup(&state_file, trash.area);
3342 if (!node)
3343 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003344
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003345 st = container_of(node, struct state_line, name_name);
3346 memcpy(mybuf, st->line, strlen(st->line));
3347 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003348
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003349 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3350 if (params[0] == NULL)
3351 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003352
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003353 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003354
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003355 next:
3356 srv = srv->next;
3357 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003358
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003359 continue; /* next proxy in list */
3360 }
3361 else {
3362 /* load 'local' state file */
3363 errno = 0;
3364 f = fopen(filepath, "r");
3365 if (errno && fileopenerr)
3366 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3367 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003368 continue;
3369
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003370 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003371
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003372 /* first character of first line of the file must contain the version of the export */
3373 version = srv_state_get_version(f);
3374 if (version == 0) {
3375 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3376 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003377 }
3378
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003379 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3380 int bk_f_forced_id = 0;
3381 int check_id = 0;
3382 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003383
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003384 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003385
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003386 if (params[0] == NULL) {
3387 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003388 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003389
3390 /* if line is incomplete line, then ignore it.
3391 * otherwise, update useful flags */
3392 switch (version) {
3393 case 1:
3394 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3395 check_id = (atoi(params[0]) == curproxy->uuid);
3396 check_name = (strcmp(curproxy->id, params[1]) == 0);
3397 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003398 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003399
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003400 bk = curproxy;
3401
3402 /* if backend can't be found, let's continue */
3403 if (!check_id && !check_name)
3404 continue;
3405 else if (!check_id && check_name) {
3406 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3407 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3408 }
3409 else if (check_id && !check_name) {
3410 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3411 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3412 /* if name doesn't match, we still want to update curproxy if the backend id
3413 * was forced in previous the previous configuration */
3414 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003415 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003416 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003417
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003418 /* look for the server by its name: param[3] */
3419 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003420
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003421 if (!srv) {
3422 /* if no server found, then warning and continue with next line */
3423 ha_warning("can't find server '%s' in backend '%s'\n",
3424 params[3], params[1]);
3425 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3426 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003427 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003428 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003429
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003430 /* now we can proceed with server's state update */
3431 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003432 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003433 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003434fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003435 fclose(f);
3436 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003437
3438 /* now free memory allocated for the tree */
3439 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3440 node;
3441 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3442 st = container_of(node, struct state_line, name_name);
3443 ebmb_delete(&st->name_name);
3444 free(st->line);
3445 free(st);
3446 }
3447
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003448}
3449
Simon Horman7d09b9a2013-02-12 10:45:51 +09003450/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003451 * update a server's current IP address.
3452 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3453 * ip is in network format.
3454 * updater is a string which contains an information about the requester of the update.
3455 * updater is used if not NULL.
3456 *
3457 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003458 *
3459 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003460 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003461int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003462{
3463 /* generates a log line and a warning on stderr */
3464 if (1) {
3465 /* book enough space for both IPv4 and IPv6 */
3466 char oldip[INET6_ADDRSTRLEN];
3467 char newip[INET6_ADDRSTRLEN];
3468
3469 memset(oldip, '\0', INET6_ADDRSTRLEN);
3470 memset(newip, '\0', INET6_ADDRSTRLEN);
3471
3472 /* copy old IP address in a string */
3473 switch (s->addr.ss_family) {
3474 case AF_INET:
3475 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3476 break;
3477 case AF_INET6:
3478 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3479 break;
3480 };
3481
3482 /* copy new IP address in a string */
3483 switch (ip_sin_family) {
3484 case AF_INET:
3485 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3486 break;
3487 case AF_INET6:
3488 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3489 break;
3490 };
3491
3492 /* save log line into a buffer */
3493 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3494 s->proxy->id, s->id, oldip, newip, updater);
3495
3496 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003497 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003498
3499 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003500 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003501 }
3502
3503 /* save the new IP family */
3504 s->addr.ss_family = ip_sin_family;
3505 /* save the new IP address */
3506 switch (ip_sin_family) {
3507 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003508 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003509 break;
3510 case AF_INET6:
3511 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3512 break;
3513 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003514 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003515
3516 return 0;
3517}
3518
3519/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003520 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3521 *
3522 * Caller can pass its name through <updater> to get it integrated in the response message
3523 * returned by the function.
3524 *
3525 * The function first does the following, in that order:
3526 * - validates the new addr and/or port
3527 * - checks if an update is required (new IP or port is different than current ones)
3528 * - checks the update is allowed:
3529 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3530 * - allow all changes if no CHECKS are configured
3531 * - if CHECK is configured:
3532 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3533 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3534 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003535 *
3536 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003537 */
3538const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3539{
3540 struct sockaddr_storage sa;
3541 int ret, port_change_required;
3542 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003543 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003544 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003545 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003546
3547 msg = get_trash_chunk();
3548 chunk_reset(msg);
3549
3550 if (addr) {
3551 memset(&sa, 0, sizeof(struct sockaddr_storage));
3552 if (str2ip2(addr, &sa, 0) == NULL) {
3553 chunk_printf(msg, "Invalid addr '%s'", addr);
3554 goto out;
3555 }
3556
3557 /* changes are allowed on AF_INET* families only */
3558 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3559 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3560 goto out;
3561 }
3562
3563 /* collecting data currently setup */
3564 memset(current_addr, '\0', sizeof(current_addr));
3565 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3566 /* changes are allowed on AF_INET* families only */
3567 if ((ret != AF_INET) && (ret != AF_INET6)) {
3568 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3569 goto out;
3570 }
3571
3572 /* applying ADDR changes if required and allowed
3573 * ipcmp returns 0 when both ADDR are the same
3574 */
3575 if (ipcmp(&s->addr, &sa) == 0) {
3576 chunk_appendf(msg, "no need to change the addr");
3577 goto port;
3578 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003579 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003580 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003581
3582 /* we also need to update check's ADDR only if it uses the server's one */
3583 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003584 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003585 }
3586
3587 /* we also need to update agent ADDR only if it use the server's one */
3588 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003589 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003590 }
3591
3592 /* update report for caller */
3593 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3594 }
3595
3596 port:
3597 if (port) {
3598 char sign = '\0';
3599 char *endptr;
3600
3601 if (addr)
3602 chunk_appendf(msg, ", ");
3603
3604 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003605 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003606
3607 /* check if PORT change is required */
3608 port_change_required = 0;
3609
3610 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003611 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003612 new_port = strtol(port, &endptr, 10);
3613 if ((errno != 0) || (port == endptr)) {
3614 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3615 goto out;
3616 }
3617
3618 /* check if caller triggers a port mapped or offset */
3619 if (sign == '-' || (sign == '+')) {
3620 /* check if server currently uses port map */
3621 if (!(s->flags & SRV_F_MAPPORTS)) {
3622 /* switch from fixed port to port map mandatorily triggers
3623 * a port change */
3624 port_change_required = 1;
3625 /* check is configured
3626 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3627 * prevent PORT change if check doesn't have it's dedicated port while switching
3628 * to port mapping */
3629 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3630 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.");
3631 goto out;
3632 }
3633 }
3634 /* we're already using port maps */
3635 else {
3636 port_change_required = current_port != new_port;
3637 }
3638 }
3639 /* fixed port */
3640 else {
3641 port_change_required = current_port != new_port;
3642 }
3643
3644 /* applying PORT changes if required and update response message */
3645 if (port_change_required) {
3646 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003647 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003648 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003649
3650 /* prepare message */
3651 chunk_appendf(msg, "port changed from '");
3652 if (s->flags & SRV_F_MAPPORTS)
3653 chunk_appendf(msg, "+");
3654 chunk_appendf(msg, "%d' to '", current_port);
3655
3656 if (sign == '-') {
3657 s->flags |= SRV_F_MAPPORTS;
3658 chunk_appendf(msg, "%c", sign);
3659 /* just use for result output */
3660 new_port = -new_port;
3661 }
3662 else if (sign == '+') {
3663 s->flags |= SRV_F_MAPPORTS;
3664 chunk_appendf(msg, "%c", sign);
3665 }
3666 else {
3667 s->flags &= ~SRV_F_MAPPORTS;
3668 }
3669
3670 chunk_appendf(msg, "%d'", new_port);
3671
3672 /* we also need to update health checks port only if it uses server's realport */
3673 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3674 s->check.port = new_port;
3675 }
3676 }
3677 else {
3678 chunk_appendf(msg, "no need to change the port");
3679 }
3680 }
3681
3682out:
William Dauchy6318d332020-05-02 21:52:36 +02003683 if (changed) {
3684 /* force connection cleanup on the given server */
3685 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003686 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003687 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003688 if (updater)
3689 chunk_appendf(msg, " by '%s'", updater);
3690 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003691 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003692}
3693
3694
3695/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003696 * update server status based on result of name resolution
3697 * returns:
3698 * 0 if server status is updated
3699 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003700 *
3701 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003702 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003703int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003704{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003705 struct dns_resolvers *resolvers = s->resolvers;
3706 struct dns_resolution *resolution = s->dns_requester->resolution;
3707 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003708
3709 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) {
3871 nameserver->counters.update++;
3872 /* 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) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003885 nameserver->counters.invalid++;
3886 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
4249 if (px->state == PR_STSTOPPED) {
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
Olivier Houchard859dc802019-08-08 15:47:21 +02005171 while ((conn = MT_LIST_POP(&toremove_connections[tid],
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
William Dauchy6318d332020-05-02 21:52:36 +02005179/* cleanup connections for a given server
5180 * might be useful when going on forced maintenance or live changing ip/port
5181 */
William Dauchy707ad322020-05-04 13:52:40 +02005182static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005183{
5184 struct connection *conn;
5185 int did_remove;
5186 int i;
5187 int j;
5188
5189 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5190 for (i = 0; i < global.nbthread; i++) {
5191 did_remove = 0;
5192 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
5193 for (j = 0; j < srv->curr_idle_conns; j++) {
5194 conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5195 if (!conn)
5196 conn = MT_LIST_POP(&srv->safe_conns[i],
5197 struct connection *, list);
5198 if (!conn)
5199 break;
5200 did_remove = 1;
5201 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
5202 }
5203 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
5204 if (did_remove)
5205 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5206 }
5207 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5208}
5209
Willy Tarreau980855b2019-02-07 14:59:29 +01005210struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005211{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005212 struct server *srv;
5213 struct eb32_node *eb;
5214 int i;
5215 unsigned int next_wakeup;
5216 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005217
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005218 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5219 while (1) {
5220 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005221 int exceed_conns;
5222 int to_kill;
5223 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005224
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005225 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5226 if (!eb) {
5227 /* we might have reached the end of the tree, typically because
5228 * <now_ms> is in the first half and we're first scanning the last
5229 * half. Let's loop back to the beginning of the tree now.
5230 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005231
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005232 eb = eb32_first(&idle_conn_srv);
5233 if (likely(!eb))
5234 break;
5235 }
5236 if (tick_is_lt(now_ms, eb->key)) {
5237 /* timer not expired yet, revisit it later */
5238 next_wakeup = eb->key;
5239 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005240 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005241 }
5242 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005243
5244 /* Calculate how many idle connections we want to kill :
5245 * we want to remove half the difference between the total
5246 * of established connections (used or idle) and the max
5247 * number of used connections.
5248 */
5249 curr_idle = srv->curr_idle_conns;
5250 if (curr_idle == 0)
5251 goto remove;
5252 exceed_conns = srv->curr_used_conns + curr_idle -
5253 srv->max_used_conns;
5254 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5255 srv->max_used_conns = srv->curr_used_conns;
5256
5257 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5258 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005259 int j;
5260 int did_remove = 0;
5261
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005262 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5263 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005264 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005265 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005266 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5267 if (!conn)
5268 conn = MT_LIST_POP(&srv->safe_conns[i],
5269 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005270 if (!conn)
5271 break;
5272 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005273 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005274 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005275 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005276 if (did_remove && max_conn < srv->curr_idle_thr[i])
5277 srv_is_empty = 0;
5278 if (did_remove)
5279 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5280 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005281remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005282 eb32_delete(&srv->idle_node);
5283 if (!srv_is_empty) {
5284 /* There are still more idle connections, add the
5285 * server back in the tree.
5286 */
5287 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5288 now_ms);
5289 eb32_insert(&idle_conn_srv, &srv->idle_node);
5290 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005291 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005292 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5293
5294 if (need_wakeup)
5295 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005296 else
5297 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005298
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005299 return task;
5300}
Olivier Houchard88698d92019-04-16 19:07:22 +02005301
5302/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5303static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5304 struct proxy *defpx, const char *file, int line,
5305 char **err)
5306{
5307 int arg = -1;
5308
5309 if (too_many_args(1, args, err, NULL))
5310 return -1;
5311
5312 if (*(args[1]) != 0)
5313 arg = atoi(args[1]);
5314
5315 if (arg < 0 || arg > 100) {
5316 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5317 return -1;
5318 }
5319
5320 if (args[0][10] == 'h')
5321 global.tune.pool_high_ratio = arg;
5322 else
5323 global.tune.pool_low_ratio = arg;
5324 return 0;
5325}
5326
5327/* config keyword parsers */
5328static struct cfg_kw_list cfg_kws = {ILH, {
5329 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5330 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5331 { 0, NULL, NULL }
5332}};
5333
5334INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5335
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005336/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005337 * Local variables:
5338 * c-indent-level: 8
5339 * c-basic-offset: 8
5340 * End:
5341 */