blob: 5c26de3b74971ed50c304a1dd8f5dc25f7391b0a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Olivier Houchard4e694042017-03-14 20:01:29 +010018#include <import/xxhash.h>
19
Willy Tarreau272adea2014-03-31 10:39:59 +020020#include <common/cfgparse.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020021#include <haproxy/dict-t.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020022#include <haproxy/errors.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020023#include <haproxy/namespace.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020024#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020025
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010029#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020030#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020036#include <proto/connection.h>
Willy Tarreaufc8f6a82020-06-03 19:20:59 +020037#include <haproxy/port_range.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020038#include <haproxy/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020039#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010040#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020041#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020042#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010043#include <proto/stream_interface.h>
44#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020045#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020046#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010047#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048
Willy Tarreau8d2b7772020-05-27 10:58:19 +020049#include <import/ebsttree.h>
Baptiste Assmannda29fe22019-06-13 13:24:29 +020050
Willy Tarreau3ff577e2018-08-02 11:48:52 +020051static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020052static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010053static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010054static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020055static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
56static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020057static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau21faa912012-10-10 08:27:36 +020059/* List head of all known server keywords */
60static struct srv_kw_list srv_keywords = {
61 .list = LIST_HEAD_INIT(srv_keywords.list)
62};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063
Willy Tarreauaf613e82020-06-05 08:40:51 +020064__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010065struct eb_root idle_conn_srv = EB_ROOT;
66struct task *idle_conn_task = NULL;
67struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020068struct mt_list toremove_connections[MAX_THREADS];
Willy Tarreauaf613e82020-06-05 08:40:51 +020069__decl_thread(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010070
Frédéric Lécaille7da71292019-05-20 09:47:07 +020071/* The server names dictionary */
72struct dict server_name_dict = {
73 .name = "server names",
74 .values = EB_ROOT_UNIQUE,
75};
76
Baptiste Assmannda29fe22019-06-13 13:24:29 +020077/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010078struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020079
Simon Hormana3608442013-11-01 16:46:15 +090080int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020081{
Emeric Brun52a91d32017-08-31 14:41:55 +020082 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020083 return s->down_time;
84
85 return now.tv_sec - s->last_change + s->down_time;
86}
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050088int srv_lastsession(const struct server *s)
89{
90 if (s->counters.last_sess)
91 return now.tv_sec - s->counters.last_sess;
92
93 return -1;
94}
95
Simon Horman4a741432013-02-23 15:35:38 +090096int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020097{
Simon Horman4a741432013-02-23 15:35:38 +090098 const struct server *s = check->server;
99
Willy Tarreauff5ae352013-12-11 20:36:34 +0100100 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900101 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100102
Emeric Brun52a91d32017-08-31 14:41:55 +0200103 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900104 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100105
Simon Horman4a741432013-02-23 15:35:38 +0900106 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100107}
108
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100109/*
110 * Check that we did not get a hash collision.
111 * Unlikely, but it can happen.
112 */
113static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100114{
115 struct proxy *p = s->proxy;
116 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117
118 for (tmpserv = p->srv; tmpserv != NULL;
119 tmpserv = tmpserv->next) {
120 if (tmpserv == s)
121 continue;
122 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
123 continue;
124 if (tmpserv->cookie &&
125 strcmp(tmpserv->cookie, s->cookie) == 0) {
126 ha_warning("We generated two equal cookies for two different servers.\n"
127 "Please change the secret key for '%s'.\n",
128 s->proxy->id);
129 }
130 }
131
132}
133
Willy Tarreau46b7f532018-08-21 11:54:26 +0200134/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200135 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200136 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137void srv_set_dyncookie(struct server *s)
138{
139 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100140 char *tmpbuf;
141 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100142 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 size_t buffer_len;
144 int addr_len;
145 int port;
146
Willy Tarreau5e83d992019-07-30 11:59:34 +0200147 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
148
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 if ((s->flags & SRV_F_COOKIESET) ||
150 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
151 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200152 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100153 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100154
155 if (s->addr.ss_family != AF_INET &&
156 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200157 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100158 /*
159 * Buffer to calculate the cookie value.
160 * The buffer contains the secret key + the server IP address
161 * + the TCP port.
162 */
163 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
164 /*
165 * The TCP port should use only 2 bytes, but is stored in
166 * an unsigned int in struct server, so let's use 4, to be
167 * on the safe side.
168 */
169 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200170 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100171 memcpy(tmpbuf, p->dyncookie_key, key_len);
172 memcpy(&(tmpbuf[key_len]),
173 s->addr.ss_family == AF_INET ?
174 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
175 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
176 addr_len);
177 /*
178 * Make sure it's the same across all the load balancers,
179 * no matter their endianness.
180 */
181 port = htonl(s->svc_port);
182 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
183 hash_value = XXH64(tmpbuf, buffer_len, 0);
184 memprintf(&s->cookie, "%016llx", hash_value);
185 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200186 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100187 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100188
189 /* Don't bother checking if the dyncookie is duplicated if
190 * the server is marked as "disabled", maybe it doesn't have
191 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100192 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100193 if (!(s->next_admin & SRV_ADMF_FMAINT))
194 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200195 out:
196 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100197}
198
Willy Tarreau21faa912012-10-10 08:27:36 +0200199/*
200 * Registers the server keyword list <kwl> as a list of valid keywords for next
201 * parsing sessions.
202 */
203void srv_register_keywords(struct srv_kw_list *kwl)
204{
205 LIST_ADDQ(&srv_keywords.list, &kwl->list);
206}
207
208/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
209 * keyword is found with a NULL ->parse() function, then an attempt is made to
210 * find one with a valid ->parse() function. This way it is possible to declare
211 * platform-dependant, known keywords as NULL, then only declare them as valid
212 * if some options are met. Note that if the requested keyword contains an
213 * opening parenthesis, everything from this point is ignored.
214 */
215struct srv_kw *srv_find_kw(const char *kw)
216{
217 int index;
218 const char *kwend;
219 struct srv_kw_list *kwl;
220 struct srv_kw *ret = NULL;
221
222 kwend = strchr(kw, '(');
223 if (!kwend)
224 kwend = kw + strlen(kw);
225
226 list_for_each_entry(kwl, &srv_keywords.list, list) {
227 for (index = 0; kwl->kw[index].kw != NULL; index++) {
228 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
229 kwl->kw[index].kw[kwend-kw] == 0) {
230 if (kwl->kw[index].parse)
231 return &kwl->kw[index]; /* found it !*/
232 else
233 ret = &kwl->kw[index]; /* may be OK */
234 }
235 }
236 }
237 return ret;
238}
239
240/* Dumps all registered "server" keywords to the <out> string pointer. The
241 * unsupported keywords are only dumped if their supported form was not
242 * found.
243 */
244void srv_dump_kws(char **out)
245{
246 struct srv_kw_list *kwl;
247 int index;
248
Christopher Faulet784063e2020-05-18 12:14:18 +0200249 if (!out)
250 return;
251
Willy Tarreau21faa912012-10-10 08:27:36 +0200252 *out = NULL;
253 list_for_each_entry(kwl, &srv_keywords.list, list) {
254 for (index = 0; kwl->kw[index].kw != NULL; index++) {
255 if (kwl->kw[index].parse ||
256 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
257 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
258 kwl->scope,
259 kwl->kw[index].kw,
260 kwl->kw[index].skip ? " <arg>" : "",
261 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
262 kwl->kw[index].parse ? "" : " (not supported)");
263 }
264 }
265 }
266}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100267
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100268/* Parse the "backup" server keyword */
269static int srv_parse_backup(char **args, int *cur_arg,
270 struct proxy *curproxy, struct server *newsrv, char **err)
271{
272 newsrv->flags |= SRV_F_BACKUP;
273 return 0;
274}
275
Alexander Liu2a54bb72019-05-22 19:44:48 +0800276
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100277/* Parse the "cookie" server keyword */
278static int srv_parse_cookie(char **args, int *cur_arg,
279 struct proxy *curproxy, struct server *newsrv, char **err)
280{
281 char *arg;
282
283 arg = args[*cur_arg + 1];
284 if (!*arg) {
285 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
286 return ERR_ALERT | ERR_FATAL;
287 }
288
289 free(newsrv->cookie);
290 newsrv->cookie = strdup(arg);
291 newsrv->cklen = strlen(arg);
292 newsrv->flags |= SRV_F_COOKIESET;
293 return 0;
294}
295
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100296/* Parse the "disabled" server keyword */
297static int srv_parse_disabled(char **args, int *cur_arg,
298 struct proxy *curproxy, struct server *newsrv, char **err)
299{
Emeric Brun52a91d32017-08-31 14:41:55 +0200300 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
301 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100302 newsrv->check.state |= CHK_ST_PAUSED;
303 newsrv->check.health = 0;
304 return 0;
305}
306
307/* Parse the "enabled" server keyword */
308static int srv_parse_enabled(char **args, int *cur_arg,
309 struct proxy *curproxy, struct server *newsrv, char **err)
310{
Emeric Brun52a91d32017-08-31 14:41:55 +0200311 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
312 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100313 newsrv->check.state &= ~CHK_ST_PAUSED;
314 newsrv->check.health = newsrv->check.rise;
315 return 0;
316}
317
Willy Tarreau9c538e02019-01-23 10:21:49 +0100318static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
319{
320 char *arg;
321
322 arg = args[*cur_arg + 1];
323 if (!*arg) {
324 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
325 return ERR_ALERT | ERR_FATAL;
326 }
327 newsrv->max_reuse = atoi(arg);
328
329 return 0;
330}
331
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100332static 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 +0100333{
334 const char *res;
335 char *arg;
336 unsigned int time;
337
338 arg = args[*cur_arg + 1];
339 if (!*arg) {
340 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
341 return ERR_ALERT | ERR_FATAL;
342 }
343 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200344 if (res == PARSE_TIME_OVER) {
345 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
346 args[*cur_arg+1], args[*cur_arg]);
347 return ERR_ALERT | ERR_FATAL;
348 }
349 else if (res == PARSE_TIME_UNDER) {
350 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
351 args[*cur_arg+1], args[*cur_arg]);
352 return ERR_ALERT | ERR_FATAL;
353 }
354 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100355 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
356 *res, args[*cur_arg]);
357 return ERR_ALERT | ERR_FATAL;
358 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100359 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100360
361 return 0;
362}
363
Olivier Houchard006e3102018-12-10 18:30:32 +0100364static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
365{
366 char *arg;
367
368 arg = args[*cur_arg + 1];
369 if (!*arg) {
370 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
371 return ERR_ALERT | ERR_FATAL;
372 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100373
Olivier Houchard006e3102018-12-10 18:30:32 +0100374 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100375 if ((int)newsrv->max_idle_conns < -1) {
376 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
377 return ERR_ALERT | ERR_FATAL;
378 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100379
380 return 0;
381}
382
Willy Tarreaudff55432012-10-10 17:51:05 +0200383/* parse the "id" server keyword */
384static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
385{
386 struct eb32_node *node;
387
388 if (!*args[*cur_arg + 1]) {
389 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
390 return ERR_ALERT | ERR_FATAL;
391 }
392
393 newsrv->puid = atol(args[*cur_arg + 1]);
394 newsrv->conf.id.key = newsrv->puid;
395
396 if (newsrv->puid <= 0) {
397 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
398 return ERR_ALERT | ERR_FATAL;
399 }
400
401 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
402 if (node) {
403 struct server *target = container_of(node, struct server, conf.id);
404 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
405 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
406 target->id);
407 return ERR_ALERT | ERR_FATAL;
408 }
409
410 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200411 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200412 return 0;
413}
414
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100415/* Parse the "namespace" server keyword */
416static int srv_parse_namespace(char **args, int *cur_arg,
417 struct proxy *curproxy, struct server *newsrv, char **err)
418{
Willy Tarreaue5733232019-05-22 19:24:06 +0200419#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100420 char *arg;
421
422 arg = args[*cur_arg + 1];
423 if (!*arg) {
424 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
425 return ERR_ALERT | ERR_FATAL;
426 }
427
428 if (!strcmp(arg, "*")) {
429 /* Use the namespace associated with the connection (if present). */
430 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
431 return 0;
432 }
433
434 /*
435 * As this parser may be called several times for the same 'default-server'
436 * object, or for a new 'server' instance deriving from a 'default-server'
437 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
438 */
439 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
440
441 newsrv->netns = netns_store_lookup(arg, strlen(arg));
442 if (!newsrv->netns)
443 newsrv->netns = netns_store_insert(arg);
444
445 if (!newsrv->netns) {
446 memprintf(err, "Cannot open namespace '%s'", arg);
447 return ERR_ALERT | ERR_FATAL;
448 }
449
450 return 0;
451#else
452 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
453 return ERR_ALERT | ERR_FATAL;
454#endif
455}
456
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100457/* Parse the "no-backup" server keyword */
458static int srv_parse_no_backup(char **args, int *cur_arg,
459 struct proxy *curproxy, struct server *newsrv, char **err)
460{
461 newsrv->flags &= ~SRV_F_BACKUP;
462 return 0;
463}
464
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100465
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100466/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200467static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100468{
469 srv->pp_opts &= ~flags;
470 return 0;
471}
472
473/* Parse the "no-send-proxy" server keyword */
474static int srv_parse_no_send_proxy(char **args, int *cur_arg,
475 struct proxy *curproxy, struct server *newsrv, char **err)
476{
477 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
478}
479
480/* Parse the "no-send-proxy-v2" server keyword */
481static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
482 struct proxy *curproxy, struct server *newsrv, char **err)
483{
484 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
485}
486
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200487/* Parse the "no-tfo" server keyword */
488static int srv_parse_no_tfo(char **args, int *cur_arg,
489 struct proxy *curproxy, struct server *newsrv, char **err)
490{
491 newsrv->flags &= ~SRV_F_FASTOPEN;
492 return 0;
493}
494
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100495/* Parse the "non-stick" server keyword */
496static int srv_parse_non_stick(char **args, int *cur_arg,
497 struct proxy *curproxy, struct server *newsrv, char **err)
498{
499 newsrv->flags |= SRV_F_NON_STICK;
500 return 0;
501}
502
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100503/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200504static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100505{
506 srv->pp_opts |= flags;
507 return 0;
508}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200509/* parse the "proto" server keyword */
510static int srv_parse_proto(char **args, int *cur_arg,
511 struct proxy *px, struct server *newsrv, char **err)
512{
513 struct ist proto;
514
515 if (!*args[*cur_arg + 1]) {
516 memprintf(err, "'%s' : missing value", args[*cur_arg]);
517 return ERR_ALERT | ERR_FATAL;
518 }
519 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
520 newsrv->mux_proto = get_mux_proto(proto);
521 if (!newsrv->mux_proto) {
522 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
523 return ERR_ALERT | ERR_FATAL;
524 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200525 return 0;
526}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100527
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100528/* parse the "proxy-v2-options" */
529static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
530 struct proxy *px, struct server *newsrv, char **err)
531{
532 char *p, *n;
533 for (p = args[*cur_arg+1]; p; p = n) {
534 n = strchr(p, ',');
535 if (n)
536 *n++ = '\0';
537 if (!strcmp(p, "ssl")) {
538 newsrv->pp_opts |= SRV_PP_V2_SSL;
539 } else if (!strcmp(p, "cert-cn")) {
540 newsrv->pp_opts |= SRV_PP_V2_SSL;
541 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100542 } else if (!strcmp(p, "cert-key")) {
543 newsrv->pp_opts |= SRV_PP_V2_SSL;
544 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
545 } else if (!strcmp(p, "cert-sig")) {
546 newsrv->pp_opts |= SRV_PP_V2_SSL;
547 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
548 } else if (!strcmp(p, "ssl-cipher")) {
549 newsrv->pp_opts |= SRV_PP_V2_SSL;
550 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100551 } else if (!strcmp(p, "authority")) {
552 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100553 } else if (!strcmp(p, "crc32c")) {
554 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100555 } else if (!strcmp(p, "unique-id")) {
556 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100557 } else
558 goto fail;
559 }
560 return 0;
561 fail:
562 if (err)
563 memprintf(err, "'%s' : proxy v2 option not implemented", p);
564 return ERR_ALERT | ERR_FATAL;
565}
566
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100567/* Parse the "observe" server keyword */
568static int srv_parse_observe(char **args, int *cur_arg,
569 struct proxy *curproxy, struct server *newsrv, char **err)
570{
571 char *arg;
572
573 arg = args[*cur_arg + 1];
574 if (!*arg) {
575 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
576 return ERR_ALERT | ERR_FATAL;
577 }
578
579 if (!strcmp(arg, "none")) {
580 newsrv->observe = HANA_OBS_NONE;
581 }
582 else if (!strcmp(arg, "layer4")) {
583 newsrv->observe = HANA_OBS_LAYER4;
584 }
585 else if (!strcmp(arg, "layer7")) {
586 if (curproxy->mode != PR_MODE_HTTP) {
587 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
588 return ERR_ALERT;
589 }
590 newsrv->observe = HANA_OBS_LAYER7;
591 }
592 else {
593 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
594 "but got '%s'\n", args[*cur_arg], arg);
595 return ERR_ALERT | ERR_FATAL;
596 }
597
598 return 0;
599}
600
Frédéric Lécaille16186232017-03-14 16:42:49 +0100601/* Parse the "redir" server keyword */
602static int srv_parse_redir(char **args, int *cur_arg,
603 struct proxy *curproxy, struct server *newsrv, char **err)
604{
605 char *arg;
606
607 arg = args[*cur_arg + 1];
608 if (!*arg) {
609 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
610 return ERR_ALERT | ERR_FATAL;
611 }
612
613 free(newsrv->rdr_pfx);
614 newsrv->rdr_pfx = strdup(arg);
615 newsrv->rdr_len = strlen(arg);
616
617 return 0;
618}
619
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100620/* Parse the "send-proxy" server keyword */
621static int srv_parse_send_proxy(char **args, int *cur_arg,
622 struct proxy *curproxy, struct server *newsrv, char **err)
623{
624 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
625}
626
627/* Parse the "send-proxy-v2" server keyword */
628static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
629 struct proxy *curproxy, struct server *newsrv, char **err)
630{
631 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
632}
633
Frédéric Lécailledba97072017-03-17 15:33:50 +0100634
635/* Parse the "source" server keyword */
636static int srv_parse_source(char **args, int *cur_arg,
637 struct proxy *curproxy, struct server *newsrv, char **err)
638{
639 char *errmsg;
640 int port_low, port_high;
641 struct sockaddr_storage *sk;
642 struct protocol *proto;
643
644 errmsg = NULL;
645
646 if (!*args[*cur_arg + 1]) {
647 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
648 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
649 goto err;
650 }
651
652 /* 'sk' is statically allocated (no need to be freed). */
653 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
654 if (!sk) {
655 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
656 goto err;
657 }
658
659 proto = protocol_by_family(sk->ss_family);
660 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100661 ha_alert("'%s %s' : connect() not supported for this address family.\n",
662 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100663 goto err;
664 }
665
666 newsrv->conn_src.opts |= CO_SRC_BIND;
667 newsrv->conn_src.source_addr = *sk;
668
669 if (port_low != port_high) {
670 int i;
671
672 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100673 ha_alert("'%s' does not support port offsets (found '%s').\n",
674 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100675 goto err;
676 }
677
678 if (port_low <= 0 || port_low > 65535 ||
679 port_high <= 0 || port_high > 65535 ||
680 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100681 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 +0100682 goto err;
683 }
684 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
685 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
686 newsrv->conn_src.sport_range->ports[i] = port_low + i;
687 }
688
689 *cur_arg += 2;
690 while (*(args[*cur_arg])) {
691 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
692#if defined(CONFIG_HAP_TRANSPARENT)
693 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100694 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
695 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100696 goto err;
697 }
698 if (!strcmp(args[*cur_arg + 1], "client")) {
699 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
700 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
701 }
702 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
703 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
704 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
705 }
706 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
707 char *name, *end;
708
709 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100710 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100711 name++;
712
713 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100714 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100715 end++;
716
717 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
718 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
719 free(newsrv->conn_src.bind_hdr_name);
720 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
721 newsrv->conn_src.bind_hdr_len = end - name;
722 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
723 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
724 newsrv->conn_src.bind_hdr_occ = -1;
725
726 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100727 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100728 end++;
729 if (*end == ',') {
730 end++;
731 name = end;
732 if (*end == '-')
733 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100734 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100735 end++;
736 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
737 }
738
739 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100740 ha_alert("usesrc hdr_ip(name,num) does not support negative"
741 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100742 goto err;
743 }
744 }
745 else {
746 struct sockaddr_storage *sk;
747 int port1, port2;
748
749 /* 'sk' is statically allocated (no need to be freed). */
750 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
751 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100752 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753 goto err;
754 }
755
756 proto = protocol_by_family(sk->ss_family);
757 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_alert("'%s %s' : connect() not supported for this address family.\n",
759 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 goto err;
761 }
762
763 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100764 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
765 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100766 goto err;
767 }
768 newsrv->conn_src.tproxy_addr = *sk;
769 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
770 }
771 global.last_checks |= LSTCHK_NETADM;
772 *cur_arg += 2;
773 continue;
774#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100775 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 +0100776 goto err;
777#endif /* defined(CONFIG_HAP_TRANSPARENT) */
778 } /* "usesrc" */
779
780 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
781#ifdef SO_BINDTODEVICE
782 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100784 goto err;
785 }
786 free(newsrv->conn_src.iface_name);
787 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
788 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
789 global.last_checks |= LSTCHK_NETADM;
790#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100792 goto err;
793#endif
794 *cur_arg += 2;
795 continue;
796 }
797 /* this keyword in not an option of "source" */
798 break;
799 } /* while */
800
801 return 0;
802
803 err:
804 free(errmsg);
805 return ERR_ALERT | ERR_FATAL;
806}
807
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100808/* Parse the "stick" server keyword */
809static int srv_parse_stick(char **args, int *cur_arg,
810 struct proxy *curproxy, struct server *newsrv, char **err)
811{
812 newsrv->flags &= ~SRV_F_NON_STICK;
813 return 0;
814}
815
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100816/* Parse the "track" server keyword */
817static int srv_parse_track(char **args, int *cur_arg,
818 struct proxy *curproxy, struct server *newsrv, char **err)
819{
820 char *arg;
821
822 arg = args[*cur_arg + 1];
823 if (!*arg) {
824 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
825 return ERR_ALERT | ERR_FATAL;
826 }
827
828 free(newsrv->trackit);
829 newsrv->trackit = strdup(arg);
830
831 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800832}
833
834/* Parse the "socks4" server keyword */
835static int srv_parse_socks4(char **args, int *cur_arg,
836 struct proxy *curproxy, struct server *newsrv, char **err)
837{
838 char *errmsg;
839 int port_low, port_high;
840 struct sockaddr_storage *sk;
841 struct protocol *proto;
842
843 errmsg = NULL;
844
845 if (!*args[*cur_arg + 1]) {
846 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
847 goto err;
848 }
849
850 /* 'sk' is statically allocated (no need to be freed). */
851 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
852 if (!sk) {
853 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
854 goto err;
855 }
856
857 proto = protocol_by_family(sk->ss_family);
858 if (!proto || !proto->connect) {
859 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
860 goto err;
861 }
862
863 newsrv->flags |= SRV_F_SOCKS4_PROXY;
864 newsrv->socks4_addr = *sk;
865
866 if (port_low != port_high) {
867 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
868 goto err;
869 }
870
871 if (!port_low) {
872 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
873 goto err;
874 }
875
876 return 0;
877
878 err:
879 free(errmsg);
880 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100881}
882
Frédéric Lécailledba97072017-03-17 15:33:50 +0100883
Willy Tarreau034c88c2017-01-23 23:36:45 +0100884/* parse the "tfo" server keyword */
885static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
886{
887 newsrv->flags |= SRV_F_FASTOPEN;
888 return 0;
889}
890
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200891/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200892 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200893 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200894 *
895 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200896 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200897void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200898{
Willy Tarreau87b09662015-04-03 00:22:06 +0200899 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200900
Willy Tarreau87b09662015-04-03 00:22:06 +0200901 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
902 if (stream->srv_conn == srv)
903 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200904}
905
906/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200907 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200908 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200909 *
910 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200911 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200912void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200913{
914 struct server *srv;
915
916 for (srv = px->srv; srv != NULL; srv = srv->next)
917 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200918 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200919}
920
Willy Tarreaubda92272014-05-20 21:55:30 +0200921/* Appends some information to a message string related to a server going UP or
922 * DOWN. If both <forced> and <reason> are null and the server tracks another
923 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200924 * If <check> is non-null, an entire string describing the check result will be
925 * appended after a comma and a space (eg: to report some information from the
926 * check that changed the state). In the other case, the string will be built
927 * using the check results stored into the struct server if present.
928 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200929 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200930 *
931 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200932 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200933void srv_append_status(struct buffer *msg, struct server *s,
934 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200935{
Emeric Brun5a133512017-10-19 14:42:30 +0200936 short status = s->op_st_chg.status;
937 short code = s->op_st_chg.code;
938 long duration = s->op_st_chg.duration;
939 char *desc = s->op_st_chg.reason;
940
941 if (check) {
942 status = check->status;
943 code = check->code;
944 duration = check->duration;
945 desc = check->desc;
946 }
947
948 if (status != -1) {
949 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
950
951 if (status >= HCHK_STATUS_L57DATA)
952 chunk_appendf(msg, ", code: %d", code);
953
954 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200955 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200956
957 chunk_appendf(msg, ", info: \"");
958
959 chunk_initlen(&src, desc, 0, strlen(desc));
960 chunk_asciiencode(msg, &src, '"');
961
962 chunk_appendf(msg, "\"");
963 }
964
965 if (duration >= 0)
966 chunk_appendf(msg, ", check duration: %ldms", duration);
967 }
968 else if (desc && *desc) {
969 chunk_appendf(msg, ", %s", desc);
970 }
971 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200972 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200973 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200974
975 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200976 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
978 " %d sessions active, %d requeued, %d remaining in queue",
979 s->proxy->srv_act, s->proxy->srv_bck,
980 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
981 s->cur_sess, xferred, s->nbpend);
982 else
983 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
984 " %d sessions requeued, %d total in queue",
985 s->proxy->srv_act, s->proxy->srv_bck,
986 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
987 xferred, s->nbpend);
988 }
989}
990
Emeric Brun5a133512017-10-19 14:42:30 +0200991/* Marks server <s> down, regardless of its checks' statuses. The server is
992 * registered in a list to postpone the counting of the remaining servers on
993 * the proxy and transfers queued streams whenever possible to other servers at
994 * a sync point. Maintenance servers are ignored. It stores the <reason> if
995 * non-null as the reason for going down or the available data from the check
996 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200997 *
998 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200999 */
Emeric Brun5a133512017-10-19 14:42:30 +02001000void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001001{
1002 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001003
Emeric Brun64cc49c2017-10-03 14:46:45 +02001004 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001005 return;
1006
Emeric Brun52a91d32017-08-31 14:41:55 +02001007 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001008 *s->op_st_chg.reason = 0;
1009 s->op_st_chg.status = -1;
1010 if (reason) {
1011 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1012 }
1013 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001014 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001015 s->op_st_chg.code = check->code;
1016 s->op_st_chg.status = check->status;
1017 s->op_st_chg.duration = check->duration;
1018 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001019
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001020 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001021 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001022
Emeric Brun9f0b4582017-10-23 14:39:51 +02001023 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001024 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001025 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001026 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001027 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001028}
1029
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001030/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001031 * in maintenance. The server is registered in a list to postpone the counting
1032 * of the remaining servers on the proxy and tries to grab requests from the
1033 * proxy at a sync point. Maintenance servers are ignored. It stores the
1034 * <reason> if non-null as the reason for going down or the available data
1035 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001036 *
1037 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038 */
Emeric Brun5a133512017-10-19 14:42:30 +02001039void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001040{
1041 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001042
Emeric Brun64cc49c2017-10-03 14:46:45 +02001043 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001044 return;
1045
Emeric Brun52a91d32017-08-31 14:41:55 +02001046 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001047 return;
1048
Emeric Brun52a91d32017-08-31 14:41:55 +02001049 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001050 *s->op_st_chg.reason = 0;
1051 s->op_st_chg.status = -1;
1052 if (reason) {
1053 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1054 }
1055 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001056 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001057 s->op_st_chg.code = check->code;
1058 s->op_st_chg.status = check->status;
1059 s->op_st_chg.duration = check->duration;
1060 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001061
Emeric Brun64cc49c2017-10-03 14:46:45 +02001062 if (s->slowstart <= 0)
1063 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001064
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001065 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001066 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001067
Emeric Brun9f0b4582017-10-23 14:39:51 +02001068 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001069 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001070 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001071 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001072 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001073}
1074
Willy Tarreau8eb77842014-05-21 13:54:57 +02001075/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001076 * isn't in maintenance. The server is registered in a list to postpone the
1077 * counting of the remaining servers on the proxy and tries to grab requests
1078 * from the proxy. Maintenance servers are ignored. It stores the
1079 * <reason> if non-null as the reason for going down or the available data
1080 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001081 * up. Note that it makes use of the trash to build the log strings, so <reason>
1082 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001083 *
1084 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085 */
Emeric Brun5a133512017-10-19 14:42:30 +02001086void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001087{
1088 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001089
Emeric Brun64cc49c2017-10-03 14:46:45 +02001090 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001091 return;
1092
Emeric Brun52a91d32017-08-31 14:41:55 +02001093 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001094 return;
1095
Emeric Brun52a91d32017-08-31 14:41:55 +02001096 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001097 *s->op_st_chg.reason = 0;
1098 s->op_st_chg.status = -1;
1099 if (reason) {
1100 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1101 }
1102 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001103 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001104 s->op_st_chg.code = check->code;
1105 s->op_st_chg.status = check->status;
1106 s->op_st_chg.duration = check->duration;
1107 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001108
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001109 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001110 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001111
Emeric Brun9f0b4582017-10-23 14:39:51 +02001112 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001113 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001114 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001115 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001116 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001117}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001118
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001119/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1120 * enforce either maint mode or drain mode. It is not allowed to set more than
1121 * one flag at once. The equivalent "inherited" flag is propagated to all
1122 * tracking servers. Maintenance mode disables health checks (but not agent
1123 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001124 * is done. If <cause> is non-null, it will be displayed at the end of the log
1125 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001126 *
1127 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001128 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001129void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001130{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001131 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132
1133 if (!mode)
1134 return;
1135
1136 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001137 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001138 return;
1139
Emeric Brun52a91d32017-08-31 14:41:55 +02001140 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001141 if (cause)
1142 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1143
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001144 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001145 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001146
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001147 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001148 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1149 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001150 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001151
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001152 /* compute the inherited flag to propagate */
1153 if (mode & SRV_ADMF_MAINT)
1154 mode = SRV_ADMF_IMAINT;
1155 else if (mode & SRV_ADMF_DRAIN)
1156 mode = SRV_ADMF_IDRAIN;
1157
Emeric Brun9f0b4582017-10-23 14:39:51 +02001158 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001159 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001160 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001161 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001162 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001163}
1164
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001165/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1166 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1167 * than one flag at once. The equivalent "inherited" flag is propagated to all
1168 * tracking servers. Leaving maintenance mode re-enables health checks. When
1169 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001170 *
1171 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001172 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001173void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001174{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001175 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001176
1177 if (!mode)
1178 return;
1179
1180 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001181 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001182 return;
1183
Emeric Brun52a91d32017-08-31 14:41:55 +02001184 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001185
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001186 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001187 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001188
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001189 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001190 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1191 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001192 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001193
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001194 if (mode & SRV_ADMF_MAINT)
1195 mode = SRV_ADMF_IMAINT;
1196 else if (mode & SRV_ADMF_DRAIN)
1197 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001198
Emeric Brun9f0b4582017-10-23 14:39:51 +02001199 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001200 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001201 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001202 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001203 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001204}
1205
Willy Tarreau757478e2016-11-03 19:22:19 +01001206/* principle: propagate maint and drain to tracking servers. This is useful
1207 * upon startup so that inherited states are correct.
1208 */
1209static void srv_propagate_admin_state(struct server *srv)
1210{
1211 struct server *srv2;
1212
1213 if (!srv->trackers)
1214 return;
1215
1216 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001217 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001218 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001219 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001220
Emeric Brun52a91d32017-08-31 14:41:55 +02001221 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001222 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001223 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001224 }
1225}
1226
1227/* Compute and propagate the admin states for all servers in proxy <px>.
1228 * Only servers *not* tracking another one are considered, because other
1229 * ones will be handled when the server they track is visited.
1230 */
1231void srv_compute_all_admin_states(struct proxy *px)
1232{
1233 struct server *srv;
1234
1235 for (srv = px->srv; srv; srv = srv->next) {
1236 if (srv->track)
1237 continue;
1238 srv_propagate_admin_state(srv);
1239 }
1240}
1241
Willy Tarreaudff55432012-10-10 17:51:05 +02001242/* Note: must not be declared <const> as its list will be overwritten.
1243 * Please take care of keeping this list alphabetically sorted, doing so helps
1244 * all code contributors.
1245 * Optional keywords are also declared with a NULL ->parse() function so that
1246 * the config parser can report an appropriate error when a known keyword was
1247 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001248 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001249 */
1250static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001251 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001252 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001253 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1254 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001255 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001256 { "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 +01001257 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001258 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001259 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1260 { "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 +02001261 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001262 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001263 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001264 { "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 +01001265 { "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 +02001266 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001267 { "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 +01001268 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001269 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1270 { "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 +02001271 { "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 +01001272 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001273 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001274 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001275 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001276 { NULL, NULL, 0 },
1277}};
1278
Willy Tarreau0108d902018-11-25 19:14:37 +01001279INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001280
Willy Tarreau004e0452013-11-21 11:22:01 +01001281/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001282 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001283 * state is automatically disabled if the time is elapsed. If <must_update> is
1284 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001285 *
1286 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001287 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001288void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001289{
1290 struct proxy *px = sv->proxy;
1291 unsigned w;
1292
1293 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1294 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001295 if (sv->next_state == SRV_ST_STARTING)
1296 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001297 }
1298
1299 /* We must take care of not pushing the server to full throttle during slow starts.
1300 * It must also start immediately, at least at the minimal step when leaving maintenance.
1301 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001302 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001303 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1304 else
1305 w = px->lbprm.wdiv;
1306
Emeric Brun52a91d32017-08-31 14:41:55 +02001307 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001308
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001309 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001310 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001311 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001312}
1313
Willy Tarreaubaaee002006-06-26 02:48:02 +02001314/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001315 * Parses weight_str and configures sv accordingly.
1316 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001317 *
1318 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001319 */
1320const char *server_parse_weight_change_request(struct server *sv,
1321 const char *weight_str)
1322{
1323 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001324 long int w;
1325 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001326
1327 px = sv->proxy;
1328
1329 /* if the weight is terminated with '%', it is set relative to
1330 * the initial weight, otherwise it is absolute.
1331 */
1332 if (!*weight_str)
1333 return "Require <weight> or <weight%>.\n";
1334
Simon Hormanb796afa2013-02-12 10:45:53 +09001335 w = strtol(weight_str, &end, 10);
1336 if (end == weight_str)
1337 return "Empty weight string empty or preceded by garbage";
1338 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001339 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001340 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001341 /* Avoid integer overflow */
1342 if (w > 25600)
1343 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001344 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001345 if (w > 256)
1346 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001347 }
1348 else if (w < 0 || w > 256)
1349 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001350 else if (end[0] != '\0')
1351 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001352
1353 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1354 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1355
1356 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001357 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001358
1359 return NULL;
1360}
1361
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001362/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001363 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1364 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001365 * Returns:
1366 * - error string on error
1367 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001368 *
1369 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001370 */
1371const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001372 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001373{
1374 unsigned char ip[INET6_ADDRSTRLEN];
1375
1376 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001377 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001378 return NULL;
1379 }
1380 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001381 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001382 return NULL;
1383 }
1384
1385 return "Could not understand IP address format.\n";
1386}
1387
Willy Tarreau46b7f532018-08-21 11:54:26 +02001388/*
1389 * Must be called with the server lock held.
1390 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001391const char *server_parse_maxconn_change_request(struct server *sv,
1392 const char *maxconn_str)
1393{
1394 long int v;
1395 char *end;
1396
1397 if (!*maxconn_str)
1398 return "Require <maxconn>.\n";
1399
1400 v = strtol(maxconn_str, &end, 10);
1401 if (end == maxconn_str)
1402 return "maxconn string empty or preceded by garbage";
1403 else if (end[0] != '\0')
1404 return "Trailing garbage in maxconn string";
1405
1406 if (sv->maxconn == sv->minconn) { // static maxconn
1407 sv->maxconn = sv->minconn = v;
1408 } else { // dynamic maxconn
1409 sv->maxconn = v;
1410 }
1411
1412 if (may_dequeue_tasks(sv, sv->proxy))
1413 process_srv_queue(sv);
1414
1415 return NULL;
1416}
1417
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001418#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001419static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1420 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001421{
1422 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001423 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001424 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001425 NULL,
1426 };
1427
1428 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001429 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001430
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001431 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001432}
1433
1434static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1435{
1436 struct sample_expr *expr;
1437
1438 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 +01001439 if (!expr) {
1440 memprintf(err, "error detected while parsing sni expression : %s", *err);
1441 return ERR_ALERT | ERR_FATAL;
1442 }
1443
1444 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1445 memprintf(err, "error detected while parsing sni expression : "
1446 " fetch method '%s' extracts information from '%s', "
1447 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001448 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001449 return ERR_ALERT | ERR_FATAL;
1450 }
1451
1452 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1453 release_sample_expr(newsrv->ssl_ctx.sni);
1454 newsrv->ssl_ctx.sni = expr;
1455
1456 return 0;
1457}
1458#endif
1459
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001460static 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 +01001461{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001462 char *msg = "error encountered while processing ";
1463 char *quote = "'";
1464 char *token = args[cur_arg];
1465
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001466 if (err && *err) {
1467 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001468 msg = *err;
1469 quote = "";
1470 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001471 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001472
1473 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1474 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1475 file, linenum, args[0], args[1],
1476 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001477 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001478 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1479 file, linenum, args[0], args[1],
1480 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001481}
1482
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001483static void srv_conn_src_sport_range_cpy(struct server *srv,
1484 struct server *src)
1485{
1486 int range_sz;
1487
1488 range_sz = src->conn_src.sport_range->size;
1489 if (range_sz > 0) {
1490 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1491 if (srv->conn_src.sport_range != NULL) {
1492 int i;
1493
1494 for (i = 0; i < range_sz; i++) {
1495 srv->conn_src.sport_range->ports[i] =
1496 src->conn_src.sport_range->ports[i];
1497 }
1498 }
1499 }
1500}
1501
1502/*
1503 * Copy <src> server connection source settings to <srv> server everything needed.
1504 */
1505static void srv_conn_src_cpy(struct server *srv, struct server *src)
1506{
1507 srv->conn_src.opts = src->conn_src.opts;
1508 srv->conn_src.source_addr = src->conn_src.source_addr;
1509
1510 /* Source port range copy. */
1511 if (src->conn_src.sport_range != NULL)
1512 srv_conn_src_sport_range_cpy(srv, src);
1513
1514#ifdef CONFIG_HAP_TRANSPARENT
1515 if (src->conn_src.bind_hdr_name != NULL) {
1516 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1517 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1518 }
1519 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1520 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1521#endif
1522 if (src->conn_src.iface_name != NULL)
1523 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1524}
1525
1526/*
1527 * Copy <src> server SSL settings to <srv> server allocating
1528 * everything needed.
1529 */
1530#if defined(USE_OPENSSL)
1531static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1532{
1533 if (src->ssl_ctx.ca_file != NULL)
1534 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1535 if (src->ssl_ctx.crl_file != NULL)
1536 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1537 if (src->ssl_ctx.client_crt != NULL)
1538 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1539
1540 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1541
1542 if (src->ssl_ctx.verify_host != NULL)
1543 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1544 if (src->ssl_ctx.ciphers != NULL)
1545 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001546 if (src->ssl_ctx.options)
1547 srv->ssl_ctx.options = src->ssl_ctx.options;
1548 if (src->ssl_ctx.methods.flags)
1549 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1550 if (src->ssl_ctx.methods.min)
1551 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1552 if (src->ssl_ctx.methods.max)
1553 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1554
Willy Tarreau5db847a2019-05-09 14:13:35 +02001555#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001556 if (src->ssl_ctx.ciphersuites != NULL)
1557 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1558#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001559 if (src->sni_expr != NULL)
1560 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001561
1562#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1563 if (src->ssl_ctx.alpn_str) {
1564 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1565 if (srv->ssl_ctx.alpn_str) {
1566 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1567 src->ssl_ctx.alpn_len);
1568 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1569 }
1570 }
1571#endif
1572#ifdef OPENSSL_NPN_NEGOTIATED
1573 if (src->ssl_ctx.npn_str) {
1574 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1575 if (srv->ssl_ctx.npn_str) {
1576 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1577 src->ssl_ctx.npn_len);
1578 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1579 }
1580 }
1581#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001582}
1583#endif
1584
1585/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001586 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001587 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001588 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001589 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001590static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001591{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001592 char *hostname_dn;
1593 int hostname_len, hostname_dn_len;
1594
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001595 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001596 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001597
Christopher Faulet67957bd2017-09-27 11:00:59 +02001598 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001599 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001600 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1601 hostname_dn, trash.size);
1602 if (hostname_dn_len == -1)
1603 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001604
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001605
Christopher Faulet67957bd2017-09-27 11:00:59 +02001606 free(srv->hostname);
1607 free(srv->hostname_dn);
1608 srv->hostname = strdup(hostname);
1609 srv->hostname_dn = strdup(hostname_dn);
1610 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001611 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001612 goto err;
1613
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001614 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001615
1616 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001617 free(srv->hostname); srv->hostname = NULL;
1618 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001619 return -1;
1620}
1621
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001622/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001623 * Copy <src> server settings to <srv> server allocating
1624 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001625 * This function is not supposed to be called at any time, but only
1626 * during server settings parsing or during server allocations from
1627 * a server template, and just after having calloc()'ed a new server.
1628 * So, <src> may only be a default server (when parsing server settings)
1629 * or a server template (during server allocations from a server template).
1630 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1631 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001632 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001633static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001634{
1635 /* Connection source settings copy */
1636 srv_conn_src_cpy(srv, src);
1637
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001638 if (srv_tmpl) {
1639 srv->addr = src->addr;
1640 srv->svc_port = src->svc_port;
1641 }
1642
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001643 srv->pp_opts = src->pp_opts;
1644 if (src->rdr_pfx != NULL) {
1645 srv->rdr_pfx = strdup(src->rdr_pfx);
1646 srv->rdr_len = src->rdr_len;
1647 }
1648 if (src->cookie != NULL) {
1649 srv->cookie = strdup(src->cookie);
1650 srv->cklen = src->cklen;
1651 }
1652 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001653 srv->check.addr = src->check.addr;
1654 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001655 srv->check.use_ssl = src->check.use_ssl;
1656 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001657 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001658 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001659 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001660 /* Note: 'flags' field has potentially been already initialized. */
1661 srv->flags |= src->flags;
1662 srv->do_check = src->do_check;
1663 srv->do_agent = src->do_agent;
1664 if (srv->check.port)
1665 srv->flags |= SRV_F_CHECKPORT;
1666 srv->check.inter = src->check.inter;
1667 srv->check.fastinter = src->check.fastinter;
1668 srv->check.downinter = src->check.downinter;
1669 srv->agent.use_ssl = src->agent.use_ssl;
1670 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001671
1672 if (src->agent.tcpcheck_rules) {
1673 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1674 if (srv->agent.tcpcheck_rules) {
1675 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1676 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1677 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1678 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1679 &src->agent.tcpcheck_rules->preset_vars);
1680 }
1681 }
1682
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001683 srv->agent.inter = src->agent.inter;
1684 srv->agent.fastinter = src->agent.fastinter;
1685 srv->agent.downinter = src->agent.downinter;
1686 srv->maxqueue = src->maxqueue;
1687 srv->minconn = src->minconn;
1688 srv->maxconn = src->maxconn;
1689 srv->slowstart = src->slowstart;
1690 srv->observe = src->observe;
1691 srv->onerror = src->onerror;
1692 srv->onmarkeddown = src->onmarkeddown;
1693 srv->onmarkedup = src->onmarkedup;
1694 if (src->trackit != NULL)
1695 srv->trackit = strdup(src->trackit);
1696 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1697 srv->uweight = srv->iweight = src->iweight;
1698
1699 srv->check.send_proxy = src->check.send_proxy;
1700 /* health: up, but will fall down at first failure */
1701 srv->check.rise = srv->check.health = src->check.rise;
1702 srv->check.fall = src->check.fall;
1703
1704 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001705 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1706 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1707 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001708 srv->check.state |= CHK_ST_PAUSED;
1709 srv->check.health = 0;
1710 }
1711
1712 /* health: up but will fall down at first failure */
1713 srv->agent.rise = srv->agent.health = src->agent.rise;
1714 srv->agent.fall = src->agent.fall;
1715
1716 if (src->resolvers_id != NULL)
1717 srv->resolvers_id = strdup(src->resolvers_id);
1718 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001719 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001720 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001721 if (srv->dns_opts.family_prio == AF_UNSPEC)
1722 srv->dns_opts.family_prio = AF_INET6;
1723 memcpy(srv->dns_opts.pref_net,
1724 src->dns_opts.pref_net,
1725 sizeof srv->dns_opts.pref_net);
1726 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1727
1728 srv->init_addr_methods = src->init_addr_methods;
1729 srv->init_addr = src->init_addr;
1730#if defined(USE_OPENSSL)
1731 srv_ssl_settings_cpy(srv, src);
1732#endif
1733#ifdef TCP_USER_TIMEOUT
1734 srv->tcp_ut = src->tcp_ut;
1735#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001736 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001737 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001738 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001739 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001740
Olivier Houchard8da5f982017-08-04 18:35:36 +02001741 if (srv_tmpl)
1742 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001743
1744 srv->check.via_socks4 = src->check.via_socks4;
1745 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001746}
1747
William Lallemand313bfd12018-10-26 14:47:32 +02001748struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749{
1750 struct server *srv;
1751
1752 srv = calloc(1, sizeof *srv);
1753 if (!srv)
1754 return NULL;
1755
1756 srv->obj_type = OBJ_TYPE_SERVER;
1757 srv->proxy = proxy;
1758 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001759 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001760
Emeric Brun52a91d32017-08-31 14:41:55 +02001761 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001762 srv->last_change = now.tv_sec;
1763
Christopher Faulet38290462020-04-21 11:46:40 +02001764 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001765 srv->check.status = HCHK_STATUS_INI;
1766 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001767 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001768 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001769
Christopher Faulet38290462020-04-21 11:46:40 +02001770 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001771 srv->agent.status = HCHK_STATUS_INI;
1772 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001773 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001774 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1775
Willy Tarreau975b1552019-06-06 16:25:55 +02001776 /* please don't put default server settings here, they are set in
1777 * init_default_instance().
1778 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001779 return srv;
1780}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001781
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001782#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1783static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1784 struct server *srv, struct proxy *proxy)
1785{
1786 int ret;
1787 char *err = NULL;
1788
1789 if (!srv->sni_expr)
1790 return 0;
1791
1792 ret = server_parse_sni_expr(srv, proxy, &err);
1793 if (!ret)
1794 return 0;
1795
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001796 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001797 free(err);
1798
1799 return ret;
1800}
1801#endif
1802
1803/*
1804 * Server initializations finalization.
1805 * Initialize health check, agent check and SNI expression if enabled.
1806 * Must not be called for a default server instance.
1807 */
1808static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1809 struct server *srv, struct proxy *px)
1810{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001811#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001812 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001813#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001814
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001815 if (srv->do_check && srv->trackit) {
1816 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1817 file, linenum);
1818 return ERR_ALERT | ERR_FATAL;
1819 }
1820
1821 if (srv->do_agent && !srv->agent.port) {
1822 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1823 file, linenum, srv->id);
1824 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001825 }
1826
1827#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1828 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1829 return ret;
1830#endif
1831
1832 if (srv->flags & SRV_F_BACKUP)
1833 px->srv_bck++;
1834 else
1835 px->srv_act++;
1836 srv_lb_commit_status(srv);
1837
1838 return 0;
1839}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001840
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001841/*
1842 * Parse as much as possible such a range string argument: low[-high]
1843 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1844 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1845 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1846 * Returns 0 if succeeded, -1 if not.
1847 */
1848static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1849{
1850 char *nb_high_arg;
1851
1852 *nb_high = 0;
1853 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001854 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001855
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001856 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001857 *nb_high_arg++ = '\0';
1858 *nb_high = atoi(nb_high_arg);
1859 }
1860 else {
1861 *nb_high += *nb_low;
1862 *nb_low = 1;
1863 }
1864
1865 if (*nb_low < 0 || *nb_high < *nb_low)
1866 return -1;
1867
1868 return 0;
1869}
1870
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001871static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1872{
1873 chunk_printf(&trash, "%s%d", prefix, nb);
1874 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001875 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001876}
1877
1878/*
1879 * Initialize as much as possible servers from <srv> server template.
1880 * Note that a server template is a special server with
1881 * a few different parameters than a server which has
1882 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001883 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001884 * 'srv' template included.
1885 */
1886static int server_template_init(struct server *srv, struct proxy *px)
1887{
1888 int i;
1889 struct server *newsrv;
1890
1891 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 +02001892 newsrv = new_server(px);
1893 if (!newsrv)
1894 goto err;
1895
1896 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001897 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001898#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1899 if (newsrv->sni_expr) {
1900 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1901 if (!newsrv->ssl_ctx.sni)
1902 goto err;
1903 }
1904#endif
1905 /* Set this new server ID. */
1906 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1907
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001908 /* Linked backwards first. This will be restablished after parsing. */
1909 newsrv->next = px->srv;
1910 px->srv = newsrv;
1911 }
1912 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1913
1914 return i - srv->tmpl_info.nb_low;
1915
1916 err:
1917 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1918 if (newsrv) {
1919#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1920 release_sample_expr(newsrv->ssl_ctx.sni);
1921#endif
1922 free_check(&newsrv->agent);
1923 free_check(&newsrv->check);
1924 }
1925 free(newsrv);
1926 return i - srv->tmpl_info.nb_low;
1927}
1928
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001929int 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 +02001930{
1931 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001932 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001933 char *errmsg = NULL;
1934 int err_code = 0;
1935 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001936 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001937
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001938 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001939 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001940 !strcmp(args[0], "default-server") ||
1941 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001942 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001943 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001944 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001945 int srv_tmpl = !defsrv && !srv;
1946 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001947
1948 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001949 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001950 err_code |= ERR_ALERT | ERR_FATAL;
1951 goto out;
1952 }
1953 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001954 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001955
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001956 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001957 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001958 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001959 if (in_peers_section) {
1960 return 0;
1961 }
1962 else {
1963 /* 'server' line number of argument check. */
1964 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1965 file, linenum, args[0]);
1966 err_code |= ERR_ALERT | ERR_FATAL;
1967 goto out;
1968 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001969 }
1970
1971 err = invalid_char(args[1]);
1972 }
1973 else if (srv_tmpl) {
1974 if (!*args[3]) {
1975 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001976 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 +02001977 file, linenum, args[0]);
1978 err_code |= ERR_ALERT | ERR_FATAL;
1979 goto out;
1980 }
1981
1982 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001983 }
1984
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001985 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001986 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 +02001987 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001988 err_code |= ERR_ALERT | ERR_FATAL;
1989 goto out;
1990 }
1991
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001992 cur_arg = 2;
1993 if (srv_tmpl) {
1994 /* Parse server-template <nb | range> arg. */
1995 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001996 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001997 file, linenum, args[0], args[cur_arg]);
1998 err_code |= ERR_ALERT | ERR_FATAL;
1999 goto out;
2000 }
2001 cur_arg++;
2002 }
2003
Willy Tarreau272adea2014-03-31 10:39:59 +02002004 if (!defsrv) {
2005 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002006 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002007 struct protocol *proto;
2008
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002009 newsrv = new_server(curproxy);
2010 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002011 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002012 err_code |= ERR_ALERT | ERR_ABORT;
2013 goto out;
2014 }
2015
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002016 if (srv_tmpl) {
2017 newsrv->tmpl_info.nb_low = tmpl_range_low;
2018 newsrv->tmpl_info.nb_high = tmpl_range_high;
2019 }
2020
Willy Tarreau272adea2014-03-31 10:39:59 +02002021 /* the servers are linked backwards first */
2022 newsrv->next = curproxy->srv;
2023 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002024 newsrv->conf.file = strdup(file);
2025 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002026 /* Note: for a server template, its id is its prefix.
2027 * This is a temporary id which will be used for server allocations to come
2028 * after parsing.
2029 */
2030 if (srv)
2031 newsrv->id = strdup(args[1]);
2032 else
2033 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002034
2035 /* several ways to check the port component :
2036 * - IP => port=+0, relative (IPv4 only)
2037 * - IP: => port=+0, relative
2038 * - IP:N => port=N, absolute
2039 * - IP:+N => port=+N, relative
2040 * - IP:-N => port=-N, relative
2041 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002042 if (!parse_addr)
2043 goto skip_addr;
2044
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002045 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002046 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002047 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002048 err_code |= ERR_ALERT | ERR_FATAL;
2049 goto out;
2050 }
2051
2052 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002053 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002054 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002055 file, linenum, args[0], args[1]);
2056 err_code |= ERR_ALERT | ERR_FATAL;
2057 goto out;
2058 }
2059
2060 if (!port1 || !port2) {
2061 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002062 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002063 }
2064 else if (port1 != port2) {
2065 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002066 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002067 file, linenum, args[0], args[1], args[2]);
2068 err_code |= ERR_ALERT | ERR_FATAL;
2069 goto out;
2070 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002071
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002072 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002073 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002074 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002075 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002076 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2077 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2078 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002079 err_code |= ERR_ALERT | ERR_FATAL;
2080 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002081 }
2082 }
2083 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002084 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002085 file, linenum, newsrv->id);
2086 err_code |= ERR_ALERT | ERR_FATAL;
2087 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002088 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002089 }
2090
Willy Tarreau272adea2014-03-31 10:39:59 +02002091 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002092 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002093
Olivier Houchard8da5f982017-08-04 18:35:36 +02002094 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002095 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002096 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002097 err_code |= ERR_ALERT | ERR_FATAL;
2098 goto out;
2099 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002100
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002101 cur_arg++;
2102 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002103 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002104 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002105 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 } else {
2107 newsrv = &curproxy->defsrv;
2108 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002109 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002110 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002111 }
2112
2113 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002114 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002115 char *p, *end;
2116 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002117 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002118
2119 newsrv->init_addr_methods = 0;
2120 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2121
2122 for (p = args[cur_arg + 1]; *p; p = end) {
2123 /* cut on next comma */
2124 for (end = p; *end && *end != ','; end++);
2125 if (*end)
2126 *(end++) = 0;
2127
Willy Tarreau4310d362016-11-02 15:05:56 +01002128 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002129 if (!strcmp(p, "libc")) {
2130 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2131 }
2132 else if (!strcmp(p, "last")) {
2133 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2134 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002135 else if (!strcmp(p, "none")) {
2136 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2137 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002138 else if (str2ip2(p, &sa, 0)) {
2139 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002140 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002141 file, linenum, args[cur_arg], p);
2142 err_code |= ERR_ALERT | ERR_FATAL;
2143 goto out;
2144 }
2145 newsrv->init_addr = sa;
2146 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2147 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002148 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002149 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 +02002150 file, linenum, args[cur_arg], p);
2151 err_code |= ERR_ALERT | ERR_FATAL;
2152 goto out;
2153 }
2154 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002155 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002156 file, linenum, args[cur_arg], p);
2157 err_code |= ERR_ALERT | ERR_FATAL;
2158 goto out;
2159 }
2160 }
2161 cur_arg += 2;
2162 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002163 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002164 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002165 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2166 cur_arg += 2;
2167 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002168 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2169 char *p, *end;
2170
2171 for (p = args[cur_arg + 1]; *p; p = end) {
2172 /* cut on next comma */
2173 for (end = p; *end && *end != ','; end++);
2174 if (*end)
2175 *(end++) = 0;
2176
2177 if (!strcmp(p, "allow-dup-ip")) {
2178 newsrv->dns_opts.accept_duplicate_ip = 1;
2179 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002180 else if (!strcmp(p, "ignore-weight")) {
2181 newsrv->dns_opts.ignore_weight = 1;
2182 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002183 else if (!strcmp(p, "prevent-dup-ip")) {
2184 newsrv->dns_opts.accept_duplicate_ip = 0;
2185 }
2186 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002187 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 +02002188 file, linenum, args[cur_arg], p);
2189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
2191 }
2192 }
2193
2194 cur_arg += 2;
2195 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002196 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2197 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002198 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002199 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002200 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002201 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002202 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002203 file, linenum, args[cur_arg]);
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207 cur_arg += 2;
2208 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002209 else if (!strcmp(args[cur_arg], "resolve-net")) {
2210 char *p, *e;
2211 unsigned char mask;
2212 struct dns_options *opt;
2213
2214 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002215 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002216 file, linenum, args[cur_arg]);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220
2221 opt = &newsrv->dns_opts;
2222
2223 /* Split arguments by comma, and convert it from ipv4 or ipv6
2224 * string network in in_addr or in6_addr.
2225 */
2226 p = args[cur_arg + 1];
2227 e = p;
2228 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002229 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002230 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002231 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002232 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2233 err_code |= ERR_ALERT | ERR_FATAL;
2234 goto out;
2235 }
2236 /* look for end or comma. */
2237 while (*e != ',' && *e != '\0')
2238 e++;
2239 if (*e == ',') {
2240 *e = '\0';
2241 e++;
2242 }
2243 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2244 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2245 /* Try to convert input string from ipv4 or ipv6 network. */
2246 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2247 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2248 &mask)) {
2249 /* Try to convert input string from ipv6 network. */
2250 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2251 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2252 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002253 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002254 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002255 file, linenum, args[cur_arg], p);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto out;
2258 }
2259 opt->pref_net_nb++;
2260 p = e;
2261 }
2262
2263 cur_arg += 2;
2264 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002265 else if (!strcmp(args[cur_arg], "weight")) {
2266 int w;
2267 w = atol(args[cur_arg + 1]);
2268 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002269 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002270 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2271 err_code |= ERR_ALERT | ERR_FATAL;
2272 goto out;
2273 }
2274 newsrv->uweight = newsrv->iweight = w;
2275 cur_arg += 2;
2276 }
Emeric Brun97556472020-05-30 01:42:45 +02002277 else if (!strcmp(args[cur_arg], "log-proto")) {
2278 if (!strcmp(args[cur_arg + 1], "legacy"))
2279 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2280 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2281 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2282 else {
2283 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2284 "'octet-count' but got '%s'\n",
2285 file, linenum, args[cur_arg], args[cur_arg + 1]);
2286 err_code |= ERR_ALERT | ERR_FATAL;
2287 goto out;
2288 }
2289 cur_arg += 2;
2290 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002291 else if (!strcmp(args[cur_arg], "minconn")) {
2292 newsrv->minconn = atol(args[cur_arg + 1]);
2293 cur_arg += 2;
2294 }
2295 else if (!strcmp(args[cur_arg], "maxconn")) {
2296 newsrv->maxconn = atol(args[cur_arg + 1]);
2297 cur_arg += 2;
2298 }
2299 else if (!strcmp(args[cur_arg], "maxqueue")) {
2300 newsrv->maxqueue = atol(args[cur_arg + 1]);
2301 cur_arg += 2;
2302 }
2303 else if (!strcmp(args[cur_arg], "slowstart")) {
2304 /* slowstart is stored in seconds */
2305 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002306
2307 if (err == PARSE_TIME_OVER) {
2308 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\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 == PARSE_TIME_UNDER) {
2314 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2315 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2316 err_code |= ERR_ALERT | ERR_FATAL;
2317 goto out;
2318 }
2319 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002320 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002321 file, linenum, *err, newsrv->id);
2322 err_code |= ERR_ALERT | ERR_FATAL;
2323 goto out;
2324 }
2325 newsrv->slowstart = (val + 999) / 1000;
2326 cur_arg += 2;
2327 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002328 else if (!strcmp(args[cur_arg], "on-error")) {
2329 if (!strcmp(args[cur_arg + 1], "fastinter"))
2330 newsrv->onerror = HANA_ONERR_FASTINTER;
2331 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2332 newsrv->onerror = HANA_ONERR_FAILCHK;
2333 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2334 newsrv->onerror = HANA_ONERR_SUDDTH;
2335 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2336 newsrv->onerror = HANA_ONERR_MARKDWN;
2337 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002338 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002339 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2340 file, linenum, args[cur_arg], args[cur_arg + 1]);
2341 err_code |= ERR_ALERT | ERR_FATAL;
2342 goto out;
2343 }
2344
2345 cur_arg += 2;
2346 }
2347 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2348 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2349 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2350 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002351 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002352 file, linenum, args[cur_arg], args[cur_arg + 1]);
2353 err_code |= ERR_ALERT | ERR_FATAL;
2354 goto out;
2355 }
2356
2357 cur_arg += 2;
2358 }
2359 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2360 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2361 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2362 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002363 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002364 file, linenum, args[cur_arg], args[cur_arg + 1]);
2365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368
2369 cur_arg += 2;
2370 }
2371 else if (!strcmp(args[cur_arg], "error-limit")) {
2372 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002373 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002374 file, linenum, args[cur_arg]);
2375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
2377 }
2378
2379 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2380
2381 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002382 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002383 file, linenum, args[cur_arg]);
2384 err_code |= ERR_ALERT | ERR_FATAL;
2385 goto out;
2386 }
2387 cur_arg += 2;
2388 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002389 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 file, linenum, "usesrc", "source");
2392 err_code |= ERR_ALERT | ERR_FATAL;
2393 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002394 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002395 else {
2396 static int srv_dumped;
2397 struct srv_kw *kw;
2398 char *err;
2399
2400 kw = srv_find_kw(args[cur_arg]);
2401 if (kw) {
2402 char *err = NULL;
2403 int code;
2404
2405 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002406 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 +02002407 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002408 if (kw->skip != -1)
2409 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002410 err_code |= ERR_ALERT | ERR_FATAL;
2411 goto out;
2412 }
2413
2414 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002415 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002416 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002417 if (kw->skip != -1)
2418 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 err_code |= ERR_ALERT;
2420 continue;
2421 }
2422
2423 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2424 err_code |= code;
2425
2426 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002427 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002428 if (code & ERR_FATAL) {
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 goto out;
2433 }
2434 }
2435 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002436 if (kw->skip != -1)
2437 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002438 continue;
2439 }
2440
2441 err = NULL;
2442 if (!srv_dumped) {
2443 srv_dump_kws(&err);
2444 indent_msg(&err, 4);
2445 srv_dumped = 1;
2446 }
2447
Christopher Faulet767a84b2017-11-24 16:50:31 +01002448 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002449 file, linenum, args[0], args[1], args[cur_arg],
2450 err ? " Registered keywords :" : "", err ? err : "");
2451 free(err);
2452
2453 err_code |= ERR_ALERT | ERR_FATAL;
2454 goto out;
2455 }
2456 }
2457
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002458 if (!defsrv)
2459 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2460 if (err_code & ERR_FATAL)
2461 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002462 if (srv_tmpl)
2463 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002465 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002466 return 0;
2467
2468 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002469 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002470 free(errmsg);
2471 return err_code;
2472}
2473
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002474/* Returns a pointer to the first server matching either id <id>.
2475 * NULL is returned if no match is found.
2476 * the lookup is performed in the backend <bk>
2477 */
2478struct server *server_find_by_id(struct proxy *bk, int id)
2479{
2480 struct eb32_node *eb32;
2481 struct server *curserver;
2482
2483 if (!bk || (id ==0))
2484 return NULL;
2485
2486 /* <bk> has no backend capabilities, so it can't have a server */
2487 if (!(bk->cap & PR_CAP_BE))
2488 return NULL;
2489
2490 curserver = NULL;
2491
2492 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2493 if (eb32)
2494 curserver = container_of(eb32, struct server, conf.id);
2495
2496 return curserver;
2497}
2498
2499/* Returns a pointer to the first server matching either name <name>, or id
2500 * if <name> starts with a '#'. NULL is returned if no match is found.
2501 * the lookup is performed in the backend <bk>
2502 */
2503struct server *server_find_by_name(struct proxy *bk, const char *name)
2504{
2505 struct server *curserver;
2506
2507 if (!bk || !name)
2508 return NULL;
2509
2510 /* <bk> has no backend capabilities, so it can't have a server */
2511 if (!(bk->cap & PR_CAP_BE))
2512 return NULL;
2513
2514 curserver = NULL;
2515 if (*name == '#') {
2516 curserver = server_find_by_id(bk, atoi(name + 1));
2517 if (curserver)
2518 return curserver;
2519 }
2520 else {
2521 curserver = bk->srv;
2522
2523 while (curserver && (strcmp(curserver->id, name) != 0))
2524 curserver = curserver->next;
2525
2526 if (curserver)
2527 return curserver;
2528 }
2529
2530 return NULL;
2531}
2532
2533struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2534{
2535 struct server *byname;
2536 struct server *byid;
2537
2538 if (!name && !id)
2539 return NULL;
2540
2541 if (diff)
2542 *diff = 0;
2543
2544 byname = byid = NULL;
2545
2546 if (name) {
2547 byname = server_find_by_name(bk, name);
2548 if (byname && (!id || byname->puid == id))
2549 return byname;
2550 }
2551
2552 /* remaining possibilities :
2553 * - name not set
2554 * - name set but not found
2555 * - name found but ID doesn't match
2556 */
2557 if (id) {
2558 byid = server_find_by_id(bk, id);
2559 if (byid) {
2560 if (byname) {
2561 /* use id only if forced by configuration */
2562 if (byid->flags & SRV_F_FORCED_ID) {
2563 if (diff)
2564 *diff |= 2;
2565 return byid;
2566 }
2567 else {
2568 if (diff)
2569 *diff |= 1;
2570 return byname;
2571 }
2572 }
2573
2574 /* remaining possibilities:
2575 * - name not set
2576 * - name set but not found
2577 */
2578 if (name && diff)
2579 *diff |= 2;
2580 return byid;
2581 }
2582
2583 /* id bot found */
2584 if (byname) {
2585 if (diff)
2586 *diff |= 1;
2587 return byname;
2588 }
2589 }
2590
2591 return NULL;
2592}
2593
Willy Tarreau46b7f532018-08-21 11:54:26 +02002594/* Update a server state using the parameters available in the params list.
2595 *
2596 * Grabs the server lock during operation.
2597 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002598static void srv_update_state(struct server *srv, int version, char **params)
2599{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002600 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002601 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002602
2603 /* fields since version 1
2604 * and common to all other upcoming versions
2605 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002606 enum srv_state srv_op_state;
2607 enum srv_admin srv_admin_state;
2608 unsigned srv_uweight, srv_iweight;
2609 unsigned long srv_last_time_change;
2610 short srv_check_status;
2611 enum chk_result srv_check_result;
2612 int srv_check_health;
2613 int srv_check_state, srv_agent_state;
2614 int bk_f_forced_id;
2615 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002616 int fqdn_set_by_cli;
2617 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002618 const char *port_str;
2619 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002620 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002621
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002622 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002623 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002624 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002625 switch (version) {
2626 case 1:
2627 /*
2628 * now we can proceed with server's state update:
2629 * srv_addr: params[0]
2630 * srv_op_state: params[1]
2631 * srv_admin_state: params[2]
2632 * srv_uweight: params[3]
2633 * srv_iweight: params[4]
2634 * srv_last_time_change: params[5]
2635 * srv_check_status: params[6]
2636 * srv_check_result: params[7]
2637 * srv_check_health: params[8]
2638 * srv_check_state: params[9]
2639 * srv_agent_state: params[10]
2640 * bk_f_forced_id: params[11]
2641 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002642 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002643 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002644 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002645 */
2646
2647 /* validating srv_op_state */
2648 p = NULL;
2649 errno = 0;
2650 srv_op_state = strtol(params[1], &p, 10);
2651 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2652 (srv_op_state != SRV_ST_STOPPED &&
2653 srv_op_state != SRV_ST_STARTING &&
2654 srv_op_state != SRV_ST_RUNNING &&
2655 srv_op_state != SRV_ST_STOPPING)) {
2656 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2657 }
2658
2659 /* validating srv_admin_state */
2660 p = NULL;
2661 errno = 0;
2662 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002663 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002664
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002665 /* inherited statuses will be recomputed later.
2666 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2667 */
2668 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002669
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002670 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2671 (srv_admin_state != 0 &&
2672 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002673 srv_admin_state != SRV_ADMF_CMAINT &&
2674 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002675 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002676 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002677 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2678 }
2679
2680 /* validating srv_uweight */
2681 p = NULL;
2682 errno = 0;
2683 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002684 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002685 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2686
2687 /* validating srv_iweight */
2688 p = NULL;
2689 errno = 0;
2690 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002691 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002692 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2693
2694 /* validating srv_last_time_change */
2695 p = NULL;
2696 errno = 0;
2697 srv_last_time_change = strtol(params[5], &p, 10);
2698 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2699 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2700
2701 /* validating srv_check_status */
2702 p = NULL;
2703 errno = 0;
2704 srv_check_status = strtol(params[6], &p, 10);
2705 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2706 (srv_check_status >= HCHK_STATUS_SIZE))
2707 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2708
2709 /* validating srv_check_result */
2710 p = NULL;
2711 errno = 0;
2712 srv_check_result = strtol(params[7], &p, 10);
2713 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2714 (srv_check_result != CHK_RES_UNKNOWN &&
2715 srv_check_result != CHK_RES_NEUTRAL &&
2716 srv_check_result != CHK_RES_FAILED &&
2717 srv_check_result != CHK_RES_PASSED &&
2718 srv_check_result != CHK_RES_CONDPASS)) {
2719 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2720 }
2721
2722 /* validating srv_check_health */
2723 p = NULL;
2724 errno = 0;
2725 srv_check_health = strtol(params[8], &p, 10);
2726 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2727 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2728
2729 /* validating srv_check_state */
2730 p = NULL;
2731 errno = 0;
2732 srv_check_state = strtol(params[9], &p, 10);
2733 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2734 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2735 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2736
2737 /* validating srv_agent_state */
2738 p = NULL;
2739 errno = 0;
2740 srv_agent_state = strtol(params[10], &p, 10);
2741 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2742 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2743 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2744
2745 /* validating bk_f_forced_id */
2746 p = NULL;
2747 errno = 0;
2748 bk_f_forced_id = strtol(params[11], &p, 10);
2749 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2750 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2751
2752 /* validating srv_f_forced_id */
2753 p = NULL;
2754 errno = 0;
2755 srv_f_forced_id = strtol(params[12], &p, 10);
2756 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2757 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2758
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002759 /* validating srv_fqdn */
2760 fqdn = params[13];
2761 if (fqdn && *fqdn == '-')
2762 fqdn = NULL;
2763 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2764 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2765 fqdn = NULL;
2766 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002767
Frédéric Lécaille31694712017-08-01 08:47:19 +02002768 port_str = params[14];
2769 if (port_str) {
2770 port = strl2uic(port_str, strlen(port_str));
2771 if (port > USHRT_MAX) {
2772 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2773 port_str = NULL;
2774 }
2775 }
2776
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002777 /* SRV record
2778 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2779 */
2780 srvrecord = params[15];
2781 if (srvrecord && *srvrecord != '_')
2782 srvrecord = NULL;
2783
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002784 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002785 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002786 goto out;
2787
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002788 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002789 /* recover operational state and apply it to this server
2790 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002791 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002792 switch (srv_op_state) {
2793 case SRV_ST_STOPPED:
2794 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002795 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002796 break;
2797 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002798 /* If rise == 1 there is no STARTING state, let's switch to
2799 * RUNNING
2800 */
2801 if (srv->check.rise == 1) {
2802 srv->check.health = srv->check.rise + srv->check.fall - 1;
2803 srv_set_running(srv, "", NULL);
2804 break;
2805 }
2806 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2807 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002808 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002809 break;
2810 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002811 /* If fall == 1 there is no STOPPING state, let's switch to
2812 * STOPPED
2813 */
2814 if (srv->check.fall == 1) {
2815 srv->check.health = 0;
2816 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2817 break;
2818 }
2819 if (srv->check.health < srv->check.rise ||
2820 srv->check.health > srv->check.rise + srv->check.fall - 2)
2821 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002822 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002823 break;
2824 case SRV_ST_RUNNING:
2825 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002826 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002827 break;
2828 }
2829
2830 /* When applying server state, the following rules apply:
2831 * - in case of a configuration change, we apply the setting from the new
2832 * configuration, regardless of old running state
2833 * - if no configuration change, we apply old running state only if old running
2834 * state is different from new configuration state
2835 */
2836 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002837 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2838 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002839 srv_adm_set_maint(srv);
2840 else
2841 srv_adm_set_ready(srv);
2842 }
2843 /* configuration is the same, let's compate old running state and new conf state */
2844 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002845 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002846 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002847 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002848 srv_adm_set_ready(srv);
2849 }
2850 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002851 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002852 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002853 * (srv->iweight is the weight set up in configuration).
2854 * There are two possible reasons for FDRAIN to have been present :
2855 * - previous config weight was zero
2856 * - "set server b/s drain" was sent to the CLI
2857 *
2858 * In the first case, we simply want to drop this drain state
2859 * if the new weight is not zero anymore, meaning the administrator
2860 * has intentionally turned the weight back to a positive value to
2861 * enable the server again after an operation. In the second case,
2862 * the drain state was forced on the CLI regardless of the config's
2863 * weight so we don't want a change to the config weight to lose this
2864 * status. What this means is :
2865 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2866 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002867 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002868 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002869 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002870 }
2871
2872 srv->last_change = date.tv_sec - srv_last_time_change;
2873 srv->check.status = srv_check_status;
2874 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002875
2876 /* Only case we want to apply is removing ENABLED flag which could have been
2877 * done by the "disable health" command over the stats socket
2878 */
2879 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2880 (srv_check_state & CHK_ST_CONFIGURED) &&
2881 !(srv_check_state & CHK_ST_ENABLED))
2882 srv->check.state &= ~CHK_ST_ENABLED;
2883
2884 /* Only case we want to apply is removing ENABLED flag which could have been
2885 * done by the "disable agent" command over the stats socket
2886 */
2887 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2888 (srv_agent_state & CHK_ST_CONFIGURED) &&
2889 !(srv_agent_state & CHK_ST_ENABLED))
2890 srv->agent.state &= ~CHK_ST_ENABLED;
2891
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002892 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2893 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002894 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002895 * It means that a configuration file change has precedence over a unix socket change
2896 * for server's weight
2897 *
2898 * by default, HAProxy applies the following weight when parsing the configuration
2899 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002900 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002901 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002902 srv->uweight = srv_uweight;
2903 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002904 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002905
Willy Tarreaue5a60682016-11-09 14:54:53 +01002906 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002907 if (strcmp(params[0], "-"))
2908 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002909
2910 if (fqdn && srv->hostname) {
2911 if (!strcmp(srv->hostname, fqdn)) {
2912 /* Here we reset the 'set from stats socket FQDN' flag
2913 * to support such transitions:
2914 * Let's say initial FQDN value is foo1 (in configuration file).
2915 * - FQDN changed from stats socket, from foo1 to foo2 value,
2916 * - FQDN changed again from file configuration (with the same previous value
2917 set from stats socket, from foo1 to foo2 value),
2918 * - reload for any other reason than a FQDN modification,
2919 * the configuration file FQDN matches the fqdn server state file value.
2920 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002921 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002922 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002923 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002924 }
2925 else {
2926 /* If the FDQN has been changed from stats socket,
2927 * apply fqdn state file value (which is the value set
2928 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002929 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002930 */
2931 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002932 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002933 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002934 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002935 }
2936 }
2937 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002938 /* If all the conditions below are validated, this means
2939 * we're evaluating a server managed by SRV resolution
2940 */
2941 else if (fqdn && !srv->hostname && srvrecord) {
2942 int res;
2943
2944 /* we can't apply previous state if SRV record has changed */
2945 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2946 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);
2947 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2948 goto out;
2949 }
2950
2951 /* create or find a SRV resolution for this srv record */
2952 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2953 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2954 if (srv->srvrq == NULL) {
2955 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2956 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2957 goto out;
2958 }
2959
2960 /* prepare DNS resolution for this server */
2961 res = srv_prepare_for_resolution(srv, fqdn);
2962 if (res == -1) {
2963 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2964 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2965 goto out;
2966 }
2967
2968 /* configure check.port accordingly */
2969 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2970 !(srv->flags & SRV_F_CHECKPORT))
2971 srv->check.port = port;
2972
2973 /* Unset SRV_F_MAPPORTS for SRV records.
2974 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2975 * because no ports are provided in the configuration file.
2976 * This is because HAProxy will use the port found into the SRV record.
2977 */
2978 srv->flags &= ~SRV_F_MAPPORTS;
2979 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002980
Frédéric Lécaille31694712017-08-01 08:47:19 +02002981 if (port_str)
2982 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002983 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002984
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002985 break;
2986 default:
2987 chunk_appendf(msg, ", version '%d' not supported", version);
2988 }
2989
2990 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002991 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002992 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002993 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002995 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002996}
2997
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002998
2999/*
3000 * read next line from file <f> and return the server state version if one found.
3001 * If no version is found, then 0 is returned
3002 * Note that this should be the first read on <f>
3003 */
3004static int srv_state_get_version(FILE *f) {
3005 char buf[2];
3006 int ret;
3007
3008 /* first character of first line of the file must contain the version of the export */
3009 if (fgets(buf, 2, f) == NULL) {
3010 return 0;
3011 }
3012
3013 ret = atoi(buf);
3014 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3015 (ret > SRV_STATE_FILE_VERSION_MAX))
3016 return 0;
3017
3018 return ret;
3019}
3020
3021
3022/*
3023 * parses server state line stored in <buf> and supposedly in version <version>.
3024 * Set <params> and <srv_params> accordingly.
3025 * In case of error, params[0] is set to NULL.
3026 */
3027static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3028{
3029 int buflen, arg, srv_arg;
3030 char *cur, *end;
3031
3032 buflen = strlen(buf);
3033 cur = buf;
3034 end = cur + buflen;
3035
3036 /* we need at least one character */
3037 if (buflen == 0) {
3038 params[0] = NULL;
3039 return;
3040 }
3041
3042 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003043 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003044 ++cur;
3045
3046 /* Ignore empty or commented lines */
3047 if (cur == end || *cur == '#') {
3048 params[0] = NULL;
3049 return;
3050 }
3051
3052 /* truncated lines */
3053 if (buf[buflen - 1] != '\n') {
3054 //ha_warning("server-state file '%s': truncated line\n", filepath);
3055 params[0] = NULL;
3056 return;
3057 }
3058
3059 /* Removes trailing '\n' */
3060 buf[buflen - 1] = '\0';
3061
3062 /* we're now ready to move the line into *srv_params[] */
3063 params[0] = cur;
3064 arg = 1;
3065 srv_arg = 0;
3066 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003067 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003068 *cur = '\0';
3069 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003070 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003071 ++cur;
3072 switch (version) {
3073 case 1:
3074 /*
3075 * srv_addr: params[4] => srv_params[0]
3076 * srv_op_state: params[5] => srv_params[1]
3077 * srv_admin_state: params[6] => srv_params[2]
3078 * srv_uweight: params[7] => srv_params[3]
3079 * srv_iweight: params[8] => srv_params[4]
3080 * srv_last_time_change: params[9] => srv_params[5]
3081 * srv_check_status: params[10] => srv_params[6]
3082 * srv_check_result: params[11] => srv_params[7]
3083 * srv_check_health: params[12] => srv_params[8]
3084 * srv_check_state: params[13] => srv_params[9]
3085 * srv_agent_state: params[14] => srv_params[10]
3086 * bk_f_forced_id: params[15] => srv_params[11]
3087 * srv_f_forced_id: params[16] => srv_params[12]
3088 * srv_fqdn: params[17] => srv_params[13]
3089 * srv_port: params[18] => srv_params[14]
3090 * srvrecord: params[19] => srv_params[15]
3091 */
3092 if (arg >= 4) {
3093 srv_params[srv_arg] = cur;
3094 ++srv_arg;
3095 }
3096 break;
3097 }
3098
3099 params[arg] = cur;
3100 ++arg;
3101 }
3102 else {
3103 ++cur;
3104 }
3105 }
3106
3107 /* if line is incomplete line, then ignore it.
3108 * otherwise, update useful flags */
3109 switch (version) {
3110 case 1:
3111 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3112 params[0] = NULL;
3113 return;
3114 }
3115 break;
3116 }
3117
3118 return;
3119}
3120
3121
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003122/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3123 * For each proxy, it does the following:
3124 * - opens its server state file (either one or local one)
3125 * - read whole file, line by line
3126 * - analyse each line to check if it matches our current backend:
3127 * - backend name matches
3128 * - backend id matches if id is forced and name doesn't match
3129 * - if the server pointed by the line is found, then state is applied
3130 *
3131 * If the running backend uuid or id differs from the state file, then HAProxy reports
3132 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003133 *
3134 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003135 */
3136void apply_server_state(void)
3137{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003138 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003139 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3140 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003141 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142 FILE *f;
3143 char *filepath;
3144 char globalfilepath[MAXPATHLEN + 1];
3145 char localfilepath[MAXPATHLEN + 1];
3146 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003147 struct proxy *curproxy, *bk;
3148 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003149 char *line;
3150 char *bkname, *srvname;
3151 struct state_line *st;
3152 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003153
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003154
3155 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003156 globalfilepathlen = 0;
3157 /* create the globalfilepath variable */
3158 if (global.server_state_file) {
3159 /* absolute path or no base directory provided */
3160 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3161 len = strlen(global.server_state_file);
3162 if (len > MAXPATHLEN) {
3163 globalfilepathlen = 0;
3164 goto globalfileerror;
3165 }
3166 memcpy(globalfilepath, global.server_state_file, len);
3167 globalfilepath[len] = '\0';
3168 globalfilepathlen = len;
3169 }
3170 else if (global.server_state_base) {
3171 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003172 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003173 globalfilepathlen = 0;
3174 goto globalfileerror;
3175 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003176 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003177 globalfilepath[len] = 0;
3178 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003179
3180 /* append a slash if needed */
3181 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3182 if (globalfilepathlen + 1 > MAXPATHLEN) {
3183 globalfilepathlen = 0;
3184 goto globalfileerror;
3185 }
3186 globalfilepath[globalfilepathlen++] = '/';
3187 }
3188
3189 len = strlen(global.server_state_file);
3190 if (globalfilepathlen + len > MAXPATHLEN) {
3191 globalfilepathlen = 0;
3192 goto globalfileerror;
3193 }
3194 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3195 globalfilepathlen += len;
3196 globalfilepath[globalfilepathlen++] = 0;
3197 }
3198 }
3199 globalfileerror:
3200 if (globalfilepathlen == 0)
3201 globalfilepath[0] = '\0';
3202
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003203 /* Load global server state in a tree */
3204 if (globalfilepathlen > 0) {
3205 errno = 0;
3206 f = fopen(globalfilepath, "r");
3207 if (errno)
3208 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003209 if (!f)
3210 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003211
3212 global_file_version = srv_state_get_version(f);
3213 if (global_file_version == 0)
3214 goto out_load_server_state_in_tree;
3215
3216 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3217 line = NULL;
3218
3219 line = strdup(mybuf);
3220 if (line == NULL)
3221 continue;
3222
3223 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3224 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003225 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003226
3227 /* bkname */
3228 bkname = params[1];
3229 /* srvname */
3230 srvname = params[3];
3231
3232 /* key */
3233 chunk_printf(&trash, "%s %s", bkname, srvname);
3234
3235 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003236 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003237 if (st == NULL) {
3238 goto nextline;
3239 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003240 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003241 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3242 /* this is a duplicate key, probably a hand-crafted file,
3243 * drop it!
3244 */
3245 goto nextline;
3246 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003247
3248 /* save line */
3249 st->line = line;
3250
3251 continue;
3252
3253 nextline:
3254 /* free up memory in case of error during the processing of the line */
3255 free(line);
3256 }
3257 }
3258 out_load_server_state_in_tree:
3259
3260 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003261 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003262 /* servers are only in backends */
3263 if (!(curproxy->cap & PR_CAP_BE))
3264 continue;
3265 fileopenerr = 0;
3266 filepath = NULL;
3267
3268 /* search server state file path and name */
3269 switch (curproxy->load_server_state_from_file) {
3270 /* read servers state from global file */
3271 case PR_SRV_STATE_FILE_GLOBAL:
3272 /* there was an error while generating global server state file path */
3273 if (globalfilepathlen == 0)
3274 continue;
3275 filepath = globalfilepath;
3276 fileopenerr = 1;
3277 break;
3278 /* this backend has its own file */
3279 case PR_SRV_STATE_FILE_LOCAL:
3280 localfilepathlen = 0;
3281 localfilepath[0] = '\0';
3282 len = 0;
3283 /* create the localfilepath variable */
3284 /* absolute path or no base directory provided */
3285 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3286 len = strlen(curproxy->server_state_file_name);
3287 if (len > MAXPATHLEN) {
3288 localfilepathlen = 0;
3289 goto localfileerror;
3290 }
3291 memcpy(localfilepath, curproxy->server_state_file_name, len);
3292 localfilepath[len] = '\0';
3293 localfilepathlen = len;
3294 }
3295 else if (global.server_state_base) {
3296 len = strlen(global.server_state_base);
3297 localfilepathlen += len;
3298
3299 if (localfilepathlen > MAXPATHLEN) {
3300 localfilepathlen = 0;
3301 goto localfileerror;
3302 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003303 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003304 localfilepath[localfilepathlen] = 0;
3305
3306 /* append a slash if needed */
3307 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3308 if (localfilepathlen + 1 > MAXPATHLEN) {
3309 localfilepathlen = 0;
3310 goto localfileerror;
3311 }
3312 localfilepath[localfilepathlen++] = '/';
3313 }
3314
3315 len = strlen(curproxy->server_state_file_name);
3316 if (localfilepathlen + len > MAXPATHLEN) {
3317 localfilepathlen = 0;
3318 goto localfileerror;
3319 }
3320 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3321 localfilepathlen += len;
3322 localfilepath[localfilepathlen++] = 0;
3323 }
3324 filepath = localfilepath;
3325 localfileerror:
3326 if (localfilepathlen == 0)
3327 localfilepath[0] = '\0';
3328
3329 break;
3330 case PR_SRV_STATE_FILE_NONE:
3331 default:
3332 continue;
3333 }
3334
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003335 /* when global file is used, we get data from the tree
3336 * Note that in such case we don't check backend name neither uuid.
3337 * Backend name can't be wrong since it's used as a key to retrieve the server state
3338 * line from the tree.
3339 */
3340 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3341 struct server *srv = curproxy->srv;
3342 while (srv) {
3343 struct ebmb_node *node;
3344 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003345
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003346 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3347 node = ebst_lookup(&state_file, trash.area);
3348 if (!node)
3349 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003350
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003351 st = container_of(node, struct state_line, name_name);
3352 memcpy(mybuf, st->line, strlen(st->line));
3353 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003354
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003355 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3356 if (params[0] == NULL)
3357 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003358
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003359 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003360
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003361 next:
3362 srv = srv->next;
3363 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003364
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003365 continue; /* next proxy in list */
3366 }
3367 else {
3368 /* load 'local' state file */
3369 errno = 0;
3370 f = fopen(filepath, "r");
3371 if (errno && fileopenerr)
3372 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3373 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003374 continue;
3375
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003376 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003377
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003378 /* first character of first line of the file must contain the version of the export */
3379 version = srv_state_get_version(f);
3380 if (version == 0) {
3381 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3382 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003383 }
3384
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003385 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3386 int bk_f_forced_id = 0;
3387 int check_id = 0;
3388 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003389
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003390 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003391
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003392 if (params[0] == NULL) {
3393 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003394 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003395
3396 /* if line is incomplete line, then ignore it.
3397 * otherwise, update useful flags */
3398 switch (version) {
3399 case 1:
3400 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3401 check_id = (atoi(params[0]) == curproxy->uuid);
3402 check_name = (strcmp(curproxy->id, params[1]) == 0);
3403 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003404 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003405
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003406 bk = curproxy;
3407
3408 /* if backend can't be found, let's continue */
3409 if (!check_id && !check_name)
3410 continue;
3411 else if (!check_id && check_name) {
3412 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3413 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3414 }
3415 else if (check_id && !check_name) {
3416 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3417 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3418 /* if name doesn't match, we still want to update curproxy if the backend id
3419 * was forced in previous the previous configuration */
3420 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003421 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003422 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003424 /* look for the server by its name: param[3] */
3425 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003426
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003427 if (!srv) {
3428 /* if no server found, then warning and continue with next line */
3429 ha_warning("can't find server '%s' in backend '%s'\n",
3430 params[3], params[1]);
3431 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3432 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003433 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003434 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003435
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003436 /* now we can proceed with server's state update */
3437 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003438 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003439 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003440fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003441 fclose(f);
3442 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003443
3444 /* now free memory allocated for the tree */
3445 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3446 node;
3447 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3448 st = container_of(node, struct state_line, name_name);
3449 ebmb_delete(&st->name_name);
3450 free(st->line);
3451 free(st);
3452 }
3453
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003454}
3455
Simon Horman7d09b9a2013-02-12 10:45:51 +09003456/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003457 * update a server's current IP address.
3458 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3459 * ip is in network format.
3460 * updater is a string which contains an information about the requester of the update.
3461 * updater is used if not NULL.
3462 *
3463 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003464 *
3465 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003466 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003467int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003468{
3469 /* generates a log line and a warning on stderr */
3470 if (1) {
3471 /* book enough space for both IPv4 and IPv6 */
3472 char oldip[INET6_ADDRSTRLEN];
3473 char newip[INET6_ADDRSTRLEN];
3474
3475 memset(oldip, '\0', INET6_ADDRSTRLEN);
3476 memset(newip, '\0', INET6_ADDRSTRLEN);
3477
3478 /* copy old IP address in a string */
3479 switch (s->addr.ss_family) {
3480 case AF_INET:
3481 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3482 break;
3483 case AF_INET6:
3484 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3485 break;
3486 };
3487
3488 /* copy new IP address in a string */
3489 switch (ip_sin_family) {
3490 case AF_INET:
3491 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3492 break;
3493 case AF_INET6:
3494 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3495 break;
3496 };
3497
3498 /* save log line into a buffer */
3499 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3500 s->proxy->id, s->id, oldip, newip, updater);
3501
3502 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003503 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003504
3505 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003506 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003507 }
3508
3509 /* save the new IP family */
3510 s->addr.ss_family = ip_sin_family;
3511 /* save the new IP address */
3512 switch (ip_sin_family) {
3513 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003514 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003515 break;
3516 case AF_INET6:
3517 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3518 break;
3519 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003520 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003521
3522 return 0;
3523}
3524
3525/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003526 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3527 *
3528 * Caller can pass its name through <updater> to get it integrated in the response message
3529 * returned by the function.
3530 *
3531 * The function first does the following, in that order:
3532 * - validates the new addr and/or port
3533 * - checks if an update is required (new IP or port is different than current ones)
3534 * - checks the update is allowed:
3535 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3536 * - allow all changes if no CHECKS are configured
3537 * - if CHECK is configured:
3538 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3539 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3540 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003541 *
3542 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003543 */
3544const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3545{
3546 struct sockaddr_storage sa;
3547 int ret, port_change_required;
3548 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003549 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003550 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003551 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003552
3553 msg = get_trash_chunk();
3554 chunk_reset(msg);
3555
3556 if (addr) {
3557 memset(&sa, 0, sizeof(struct sockaddr_storage));
3558 if (str2ip2(addr, &sa, 0) == NULL) {
3559 chunk_printf(msg, "Invalid addr '%s'", addr);
3560 goto out;
3561 }
3562
3563 /* changes are allowed on AF_INET* families only */
3564 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3565 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3566 goto out;
3567 }
3568
3569 /* collecting data currently setup */
3570 memset(current_addr, '\0', sizeof(current_addr));
3571 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3572 /* changes are allowed on AF_INET* families only */
3573 if ((ret != AF_INET) && (ret != AF_INET6)) {
3574 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3575 goto out;
3576 }
3577
3578 /* applying ADDR changes if required and allowed
3579 * ipcmp returns 0 when both ADDR are the same
3580 */
3581 if (ipcmp(&s->addr, &sa) == 0) {
3582 chunk_appendf(msg, "no need to change the addr");
3583 goto port;
3584 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003585 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003586 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003587
3588 /* we also need to update check's ADDR only if it uses the server's one */
3589 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003590 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003591 }
3592
3593 /* we also need to update agent ADDR only if it use the server's one */
3594 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003595 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003596 }
3597
3598 /* update report for caller */
3599 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3600 }
3601
3602 port:
3603 if (port) {
3604 char sign = '\0';
3605 char *endptr;
3606
3607 if (addr)
3608 chunk_appendf(msg, ", ");
3609
3610 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003611 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003612
3613 /* check if PORT change is required */
3614 port_change_required = 0;
3615
3616 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003617 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003618 new_port = strtol(port, &endptr, 10);
3619 if ((errno != 0) || (port == endptr)) {
3620 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3621 goto out;
3622 }
3623
3624 /* check if caller triggers a port mapped or offset */
3625 if (sign == '-' || (sign == '+')) {
3626 /* check if server currently uses port map */
3627 if (!(s->flags & SRV_F_MAPPORTS)) {
3628 /* switch from fixed port to port map mandatorily triggers
3629 * a port change */
3630 port_change_required = 1;
3631 /* check is configured
3632 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3633 * prevent PORT change if check doesn't have it's dedicated port while switching
3634 * to port mapping */
3635 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3636 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.");
3637 goto out;
3638 }
3639 }
3640 /* we're already using port maps */
3641 else {
3642 port_change_required = current_port != new_port;
3643 }
3644 }
3645 /* fixed port */
3646 else {
3647 port_change_required = current_port != new_port;
3648 }
3649
3650 /* applying PORT changes if required and update response message */
3651 if (port_change_required) {
3652 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003653 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003654 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003655
3656 /* prepare message */
3657 chunk_appendf(msg, "port changed from '");
3658 if (s->flags & SRV_F_MAPPORTS)
3659 chunk_appendf(msg, "+");
3660 chunk_appendf(msg, "%d' to '", current_port);
3661
3662 if (sign == '-') {
3663 s->flags |= SRV_F_MAPPORTS;
3664 chunk_appendf(msg, "%c", sign);
3665 /* just use for result output */
3666 new_port = -new_port;
3667 }
3668 else if (sign == '+') {
3669 s->flags |= SRV_F_MAPPORTS;
3670 chunk_appendf(msg, "%c", sign);
3671 }
3672 else {
3673 s->flags &= ~SRV_F_MAPPORTS;
3674 }
3675
3676 chunk_appendf(msg, "%d'", new_port);
3677
3678 /* we also need to update health checks port only if it uses server's realport */
3679 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3680 s->check.port = new_port;
3681 }
3682 }
3683 else {
3684 chunk_appendf(msg, "no need to change the port");
3685 }
3686 }
3687
3688out:
William Dauchy6318d332020-05-02 21:52:36 +02003689 if (changed) {
3690 /* force connection cleanup on the given server */
3691 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003692 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003693 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003694 if (updater)
3695 chunk_appendf(msg, " by '%s'", updater);
3696 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003697 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003698}
3699
3700
3701/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003702 * update server status based on result of name resolution
3703 * returns:
3704 * 0 if server status is updated
3705 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003706 *
3707 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003708 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003709int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003710{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003711 struct dns_resolvers *resolvers = s->resolvers;
3712 struct dns_resolution *resolution = s->dns_requester->resolution;
3713 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003714
3715 switch (resolution->status) {
3716 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003717 /* status when HAProxy has just (re)started.
3718 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003719 break;
3720
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003721 case RSLV_STATUS_VALID:
3722 /*
3723 * resume health checks
3724 * server will be turned back on if health check is safe
3725 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003726 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003727 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003728 return 1;
3729 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3730 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003731 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003732 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003733
Emeric Brun52a91d32017-08-31 14:41:55 +02003734 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003735 return 1;
3736 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3737 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3738 s->proxy->id, s->id);
3739
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003740 ha_warning("%s.\n", trash.area);
3741 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003742 return 0;
3743
3744 case RSLV_STATUS_NX:
3745 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003746 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3747 if (!tick_is_expired(exp, now_ms))
3748 break;
3749
3750 if (s->next_admin & SRV_ADMF_RMAINT)
3751 return 1;
3752 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3753 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003754
3755 case RSLV_STATUS_TIMEOUT:
3756 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003757 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3758 if (!tick_is_expired(exp, now_ms))
3759 break;
3760
3761 if (s->next_admin & SRV_ADMF_RMAINT)
3762 return 1;
3763 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3764 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003765
3766 case RSLV_STATUS_REFUSED:
3767 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003768 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3769 if (!tick_is_expired(exp, now_ms))
3770 break;
3771
3772 if (s->next_admin & SRV_ADMF_RMAINT)
3773 return 1;
3774 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3775 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003776
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003777 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003778 /* stop server if resolution failed for a long enough period */
3779 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3780 if (!tick_is_expired(exp, now_ms))
3781 break;
3782
3783 if (s->next_admin & SRV_ADMF_RMAINT)
3784 return 1;
3785 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3786 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003787 }
3788
3789 return 1;
3790}
3791
3792/*
3793 * Server Name Resolution valid response callback
3794 * It expects:
3795 * - <nameserver>: the name server which answered the valid response
3796 * - <response>: buffer containing a valid DNS response
3797 * - <response_len>: size of <response>
3798 * It performs the following actions:
3799 * - ignore response if current ip found and server family not met
3800 * - update with first new ip found if family is met and current IP is not found
3801 * returns:
3802 * 0 on error
3803 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003804 *
3805 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003806 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003807int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003809 struct server *s = NULL;
3810 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003811 void *serverip, *firstip;
3812 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003813 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003814 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003815 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003816
Christopher Faulet67957bd2017-09-27 11:00:59 +02003817 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003818 if (!s)
3819 return 1;
3820
Christopher Faulet67957bd2017-09-27 11:00:59 +02003821 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003822
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003823 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003824 firstip = NULL; /* pointer to the first valid response found */
3825 /* it will be used as the new IP if a change is required */
3826 firstip_sin_family = AF_UNSPEC;
3827 serverip = NULL; /* current server IP address */
3828
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829 /* initializing server IP pointer */
3830 server_sin_family = s->addr.ss_family;
3831 switch (server_sin_family) {
3832 case AF_INET:
3833 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3834 break;
3835
3836 case AF_INET6:
3837 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3838 break;
3839
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003840 case AF_UNSPEC:
3841 break;
3842
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003843 default:
3844 goto invalid;
3845 }
3846
Baptiste Assmann729c9012017-05-22 15:13:10 +02003847 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003848 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003849 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003850
3851 switch (ret) {
3852 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003853 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003854
3855 case DNS_UPD_SRVIP_NOT_FOUND:
3856 goto save_ip;
3857
3858 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003859 goto invalid;
3860
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003861 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003862 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003864
Baptiste Assmannfad03182015-10-28 02:03:32 +01003865 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003866 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003867 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003868 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003869
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003870 default:
3871 goto invalid;
3872
3873 }
3874
3875 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003876 if (nameserver) {
3877 nameserver->counters.update++;
3878 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003879 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003880 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003881 else
3882 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003883 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003884
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003886 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003887 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003888
3889 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003890 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003891 nameserver->counters.invalid++;
3892 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003893 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003894 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003895 return 0;
3896}
3897
3898/*
3899 * Server Name Resolution error management callback
3900 * returns:
3901 * 0 on error
3902 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003903 *
3904 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003905 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003906int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003908 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003909
Christopher Faulet67957bd2017-09-27 11:00:59 +02003910 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003911 if (!s)
3912 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003913 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003914 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003915 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003916 return 1;
3917}
3918
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003919/*
3920 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003921 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003922 * It returns a pointer to the first server found or NULL if <ip> is not yet
3923 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003924 *
3925 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003926 */
3927struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3928{
3929 struct server *tmpsrv;
3930 struct proxy *be;
3931
3932 if (!srv)
3933 return NULL;
3934
3935 be = srv->proxy;
3936 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003937 /* we found the current server is the same, ignore it */
3938 if (srv == tmpsrv)
3939 continue;
3940
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003941 /* We want to compare the IP in the record with the IP of the servers in the
3942 * same backend, only if:
3943 * * DNS resolution is enabled on the server
3944 * * the hostname used for the resolution by our server is the same than the
3945 * one used for the server found in the backend
3946 * * the server found in the backend is not our current server
3947 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003948 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003949 if ((tmpsrv->hostname_dn == NULL) ||
3950 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3951 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003952 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003953 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003954 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003955 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003956
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003957 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003958 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003959 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003960 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003961 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003962
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003963 /* At this point, we have 2 different servers using the same DNS hostname
3964 * for their respective resolution.
3965 */
3966 if (*ip_family == tmpsrv->addr.ss_family &&
3967 ((tmpsrv->addr.ss_family == AF_INET &&
3968 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3969 (tmpsrv->addr.ss_family == AF_INET6 &&
3970 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003971 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003972 return tmpsrv;
3973 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003974 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003975 }
3976
Emeric Brune9fd6b52017-11-02 17:20:39 +01003977
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003978 return NULL;
3979}
3980
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003981/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3982 * resolver. This is suited for initial address configuration. Returns 0 on
3983 * success otherwise a non-zero error code. In case of error, *err_code, if
3984 * not NULL, is filled up.
3985 */
3986int srv_set_addr_via_libc(struct server *srv, int *err_code)
3987{
3988 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003989 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003990 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003991 return 1;
3992 }
3993 return 0;
3994}
3995
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003996/* Set the server's FDQN (->hostname) from <hostname>.
3997 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003998 *
3999 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004000 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004001int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004002{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004003 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004004 char *hostname_dn;
4005 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004006
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004007 /* Note that the server lock is already held. */
4008 if (!srv->resolvers)
4009 return -1;
4010
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004011 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004012 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004013 /* run time DNS resolution was not active for this server
4014 * and we can't enable it at run time for now.
4015 */
4016 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004017 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018
4019 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004020 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004021 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004022 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4023 hostname_dn, trash.size);
4024 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004025 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004026
Christopher Faulet67957bd2017-09-27 11:00:59 +02004027 resolution = srv->dns_requester->resolution;
4028 if (resolution &&
4029 resolution->hostname_dn &&
4030 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004031 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004032
Christopher Faulet67957bd2017-09-27 11:00:59 +02004033 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004034
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004035 free(srv->hostname);
4036 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004037 srv->hostname = strdup(hostname);
4038 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004039 srv->hostname_dn_len = hostname_dn_len;
4040 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004041 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004042
Baptiste Assmann13a92322019-06-07 09:40:55 +02004043 if (srv->flags & SRV_F_NO_RESOLUTION)
4044 goto end;
4045
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004046 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004047 goto err;
4048
4049 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004050 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004051 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004052 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004053
4054 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004055 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004056 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004057 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004058}
4059
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004060/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4061 * from the state file. This is suited for initial address configuration.
4062 * Returns 0 on success otherwise a non-zero error code. In case of error,
4063 * *err_code, if not NULL, is filled up.
4064 */
4065static int srv_apply_lastaddr(struct server *srv, int *err_code)
4066{
4067 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4068 if (err_code)
4069 *err_code |= ERR_WARN;
4070 return 1;
4071 }
4072 return 0;
4073}
4074
Willy Tarreau25e51522016-11-04 15:10:17 +01004075/* returns 0 if no error, otherwise a combination of ERR_* flags */
4076static int srv_iterate_initaddr(struct server *srv)
4077{
4078 int return_code = 0;
4079 int err_code;
4080 unsigned int methods;
4081
4082 methods = srv->init_addr_methods;
4083 if (!methods) { // default to "last,libc"
4084 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4085 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4086 }
4087
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004088 /* "-dr" : always append "none" so that server addresses resolution
4089 * failures are silently ignored, this is convenient to validate some
4090 * configs out of their environment.
4091 */
4092 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4093 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4094
Willy Tarreau25e51522016-11-04 15:10:17 +01004095 while (methods) {
4096 err_code = 0;
4097 switch (srv_get_next_initaddr(&methods)) {
4098 case SRV_IADDR_LAST:
4099 if (!srv->lastaddr)
4100 continue;
4101 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004102 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004103 return_code |= err_code;
4104 break;
4105
4106 case SRV_IADDR_LIBC:
4107 if (!srv->hostname)
4108 continue;
4109 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004110 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004111 return_code |= err_code;
4112 break;
4113
Willy Tarreau37ebe122016-11-04 15:17:58 +01004114 case SRV_IADDR_NONE:
4115 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004116 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004117 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4118 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004119 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004120 return return_code;
4121
Willy Tarreau4310d362016-11-02 15:05:56 +01004122 case SRV_IADDR_IP:
4123 ipcpy(&srv->init_addr, &srv->addr);
4124 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004125 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4126 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004127 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004128 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004129
Willy Tarreau25e51522016-11-04 15:10:17 +01004130 default: /* unhandled method */
4131 break;
4132 }
4133 }
4134
4135 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004136 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004137 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4138 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004139 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004140 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004141 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4142 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004143
4144 return_code |= ERR_ALERT | ERR_FATAL;
4145 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004146out:
4147 srv_set_dyncookie(srv);
4148 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004149}
4150
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004151/*
4152 * This function parses all backends and all servers within each backend
4153 * and performs servers' addr resolution based on information provided by:
4154 * - configuration file
4155 * - server-state file (states provided by an 'old' haproxy process)
4156 *
4157 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4158 */
4159int srv_init_addr(void)
4160{
4161 struct proxy *curproxy;
4162 int return_code = 0;
4163
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004164 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004165 while (curproxy) {
4166 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004167
4168 /* servers are in backend only */
4169 if (!(curproxy->cap & PR_CAP_BE))
4170 goto srv_init_addr_next;
4171
Willy Tarreau25e51522016-11-04 15:10:17 +01004172 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004173 if (srv->hostname)
4174 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004175
4176 srv_init_addr_next:
4177 curproxy = curproxy->next;
4178 }
4179
4180 return return_code;
4181}
4182
Willy Tarreau46b7f532018-08-21 11:54:26 +02004183/*
4184 * Must be called with the server lock held.
4185 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004186const 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 +02004187{
4188
Willy Tarreau83061a82018-07-13 11:56:34 +02004189 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004190
4191 msg = get_trash_chunk();
4192 chunk_reset(msg);
4193
Olivier Houchard8da5f982017-08-04 18:35:36 +02004194 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004195 chunk_appendf(msg, "no need to change the FDQN");
4196 goto out;
4197 }
4198
4199 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4200 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4201 goto out;
4202 }
4203
4204 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4205 server->proxy->id, server->id, server->hostname, fqdn);
4206
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004207 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004208 chunk_reset(msg);
4209 chunk_appendf(msg, "could not update %s/%s FQDN",
4210 server->proxy->id, server->id);
4211 goto out;
4212 }
4213
4214 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004215 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004216
4217 out:
4218 if (updater)
4219 chunk_appendf(msg, " by '%s'", updater);
4220 chunk_appendf(msg, "\n");
4221
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004222 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004223}
4224
4225
Willy Tarreau21b069d2016-11-23 17:15:08 +01004226/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4227 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004228 * 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 +01004229 * used for CLI commands requiring a server name.
4230 * Important: the <arg> is modified to remove the '/'.
4231 */
4232struct server *cli_find_server(struct appctx *appctx, char *arg)
4233{
4234 struct proxy *px;
4235 struct server *sv;
4236 char *line;
4237
4238 /* split "backend/server" and make <line> point to server */
4239 for (line = arg; *line; line++)
4240 if (*line == '/') {
4241 *line++ = '\0';
4242 break;
4243 }
4244
4245 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004246 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004247 return NULL;
4248 }
4249
4250 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004251 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004252 return NULL;
4253 }
4254
4255 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004256 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004257 return NULL;
4258 }
4259
4260 return sv;
4261}
4262
William Lallemand222baf22016-11-19 02:00:33 +01004263
Willy Tarreau46b7f532018-08-21 11:54:26 +02004264/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004265static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004266{
4267 struct server *sv;
4268 const char *warning;
4269
4270 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4271 return 1;
4272
4273 sv = cli_find_server(appctx, args[2]);
4274 if (!sv)
4275 return 1;
4276
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004277 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004278
William Lallemand222baf22016-11-19 02:00:33 +01004279 if (strcmp(args[3], "weight") == 0) {
4280 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004281 if (warning)
4282 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004283 }
4284 else if (strcmp(args[3], "state") == 0) {
4285 if (strcmp(args[4], "ready") == 0)
4286 srv_adm_set_ready(sv);
4287 else if (strcmp(args[4], "drain") == 0)
4288 srv_adm_set_drain(sv);
4289 else if (strcmp(args[4], "maint") == 0)
4290 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004291 else
4292 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004293 }
4294 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004295 if (sv->track)
4296 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004297 else if (strcmp(args[4], "up") == 0) {
4298 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004299 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004300 }
4301 else if (strcmp(args[4], "stopping") == 0) {
4302 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004303 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004304 }
4305 else if (strcmp(args[4], "down") == 0) {
4306 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004307 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004308 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004309 else
4310 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004311 }
4312 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004313 if (!(sv->agent.state & CHK_ST_ENABLED))
4314 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004315 else if (strcmp(args[4], "up") == 0) {
4316 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004317 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004318 }
4319 else if (strcmp(args[4], "down") == 0) {
4320 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004321 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004322 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004323 else
4324 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004325 }
Misiek2da082d2017-01-09 09:40:42 +01004326 else if (strcmp(args[3], "agent-addr") == 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 if (str2ip(args[4], &sv->agent.addr) == NULL)
4330 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004331 }
4332 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004333 if (!(sv->agent.state & CHK_ST_ENABLED))
4334 cli_err(appctx, "agent checks are not enabled on this server.\n");
4335 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004336 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004337 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004338 }
4339 }
William Lallemand222baf22016-11-19 02:00:33 +01004340 else if (strcmp(args[3], "check-port") == 0) {
4341 int i = 0;
4342 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004343 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004344 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004345 }
4346 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004347 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004348 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004349 }
4350 /* prevent the update of port to 0 if MAPPORTS are in use */
4351 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004352 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004353 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004354 }
4355 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004356 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004357 }
4358 else if (strcmp(args[3], "addr") == 0) {
4359 char *addr = NULL;
4360 char *port = NULL;
4361 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004362 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004363 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004364 }
4365 else {
4366 addr = args[4];
4367 }
4368 if (strcmp(args[5], "port") == 0) {
4369 port = args[6];
4370 }
4371 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004372 if (warning)
4373 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004374 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4375 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004376 else if (strcmp(args[3], "fqdn") == 0) {
4377 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004378 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004379 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004380 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004381 /* ensure runtime resolver will process this new fqdn */
4382 if (sv->flags & SRV_F_NO_RESOLUTION) {
4383 sv->flags &= ~SRV_F_NO_RESOLUTION;
4384 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004385 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004386 if (warning)
4387 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004388 }
William Lallemand222baf22016-11-19 02:00:33 +01004389 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004390 cli_err(appctx,
4391 "'set server <srv>' only supports 'agent', 'health', 'state',"
4392 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004393 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004394 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004395 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004396 return 1;
4397}
4398
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004399static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004400{
4401 struct stream_interface *si = appctx->owner;
4402 struct proxy *px;
4403 struct server *sv;
4404 char *line;
4405
4406
4407 /* split "backend/server" and make <line> point to server */
4408 for (line = args[2]; *line; line++)
4409 if (*line == '/') {
4410 *line++ = '\0';
4411 break;
4412 }
4413
Willy Tarreau9d008692019-08-09 11:21:01 +02004414 if (!*line)
4415 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004416
Willy Tarreau9d008692019-08-09 11:21:01 +02004417 if (!get_backend_server(args[2], line, &px, &sv))
4418 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004419
4420 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004421 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4422 sv->iweight);
4423 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004424 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004425 return 0;
4426 }
William Lallemand6b160942016-11-22 12:34:35 +01004427 return 1;
4428}
4429
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004430/* Parse a "set weight" command.
4431 *
4432 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004433 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004434static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004435{
4436 struct server *sv;
4437 const char *warning;
4438
4439 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4440 return 1;
4441
4442 sv = cli_find_server(appctx, args[2]);
4443 if (!sv)
4444 return 1;
4445
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004446 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4447
William Lallemand6b160942016-11-22 12:34:35 +01004448 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004449 if (warning)
4450 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004451
4452 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4453
William Lallemand6b160942016-11-22 12:34:35 +01004454 return 1;
4455}
4456
Willy Tarreau46b7f532018-08-21 11:54:26 +02004457/* parse a "set maxconn server" command. It always returns 1.
4458 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004459 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004460 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004461static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004462{
4463 struct server *sv;
4464 const char *warning;
4465
4466 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4467 return 1;
4468
4469 sv = cli_find_server(appctx, args[3]);
4470 if (!sv)
4471 return 1;
4472
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004473 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4474
Willy Tarreaub8026272016-11-23 11:26:56 +01004475 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004476 if (warning)
4477 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004478
4479 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4480
Willy Tarreaub8026272016-11-23 11:26:56 +01004481 return 1;
4482}
William Lallemand6b160942016-11-22 12:34:35 +01004483
Willy Tarreau46b7f532018-08-21 11:54:26 +02004484/* parse a "disable agent" command. It always returns 1.
4485 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004486 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004487 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004488static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004489{
4490 struct server *sv;
4491
4492 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4493 return 1;
4494
4495 sv = cli_find_server(appctx, args[2]);
4496 if (!sv)
4497 return 1;
4498
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004499 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004500 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004501 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004502 return 1;
4503}
4504
Willy Tarreau46b7f532018-08-21 11:54:26 +02004505/* parse a "disable health" command. It always returns 1.
4506 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004507 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004508 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004509static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004510{
4511 struct server *sv;
4512
4513 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4514 return 1;
4515
4516 sv = cli_find_server(appctx, args[2]);
4517 if (!sv)
4518 return 1;
4519
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004520 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004521 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004522 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004523 return 1;
4524}
4525
Willy Tarreau46b7f532018-08-21 11:54:26 +02004526/* parse a "disable server" command. It always returns 1.
4527 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004528 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004529 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004530static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004531{
4532 struct server *sv;
4533
4534 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4535 return 1;
4536
4537 sv = cli_find_server(appctx, args[2]);
4538 if (!sv)
4539 return 1;
4540
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004541 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004542 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004543 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004544 return 1;
4545}
4546
Willy Tarreau46b7f532018-08-21 11:54:26 +02004547/* parse a "enable agent" command. It always returns 1.
4548 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004549 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004550 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004551static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004552{
4553 struct server *sv;
4554
4555 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4556 return 1;
4557
4558 sv = cli_find_server(appctx, args[2]);
4559 if (!sv)
4560 return 1;
4561
Willy Tarreau9d008692019-08-09 11:21:01 +02004562 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4563 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004564
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004565 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004566 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004567 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004568 return 1;
4569}
4570
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571/* parse a "enable health" command. It always returns 1.
4572 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004573 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004574 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004575static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004576{
4577 struct server *sv;
4578
4579 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4580 return 1;
4581
4582 sv = cli_find_server(appctx, args[2]);
4583 if (!sv)
4584 return 1;
4585
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004586 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004587 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004588 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004589 return 1;
4590}
4591
Willy Tarreau46b7f532018-08-21 11:54:26 +02004592/* parse a "enable server" command. It always returns 1.
4593 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004594 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004595 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004596static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004597{
4598 struct server *sv;
4599
4600 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4601 return 1;
4602
4603 sv = cli_find_server(appctx, args[2]);
4604 if (!sv)
4605 return 1;
4606
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004607 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004608 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004609 if (!(sv->flags & SRV_F_COOKIESET)
4610 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4611 sv->cookie)
4612 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004613 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004614 return 1;
4615}
4616
William Lallemand222baf22016-11-19 02:00:33 +01004617/* register cli keywords */
4618static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004619 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004620 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004621 { { "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 +01004622 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004623 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004624 { { "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 +01004625 { { "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 +01004626 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004627 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4628 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4629
William Lallemand222baf22016-11-19 02:00:33 +01004630 {{},}
4631}};
4632
Willy Tarreau0108d902018-11-25 19:14:37 +01004633INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004634
Emeric Brun64cc49c2017-10-03 14:46:45 +02004635/*
4636 * This function applies server's status changes, it is
4637 * is designed to be called asynchronously.
4638 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004639 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004640 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004641static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004642{
4643 struct check *check = &s->check;
4644 int xferred;
4645 struct proxy *px = s->proxy;
4646 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4647 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4648 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004649 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004650
Emeric Brun64cc49c2017-10-03 14:46:45 +02004651 /* If currently main is not set we try to apply pending state changes */
4652 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4653 int next_admin;
4654
4655 /* Backup next admin */
4656 next_admin = s->next_admin;
4657
4658 /* restore current admin state */
4659 s->next_admin = s->cur_admin;
4660
4661 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4662 s->last_change = now.tv_sec;
4663 if (s->proxy->lbprm.set_server_status_down)
4664 s->proxy->lbprm.set_server_status_down(s);
4665
4666 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4667 srv_shutdown_streams(s, SF_ERR_DOWN);
4668
4669 /* we might have streams queued on this server and waiting for
4670 * a connection. Those which are redispatchable will be queued
4671 * to another server or to the proxy itself.
4672 */
4673 xferred = pendconn_redistribute(s);
4674
4675 tmptrash = alloc_trash_chunk();
4676 if (tmptrash) {
4677 chunk_printf(tmptrash,
4678 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4679 s->proxy->id, s->id);
4680
Emeric Brun5a133512017-10-19 14:42:30 +02004681 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004682 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004683
4684 /* we don't send an alert if the server was previously paused */
4685 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004686 send_log(s->proxy, log_level, "%s.\n",
4687 tmptrash->area);
4688 send_email_alert(s, log_level, "%s",
4689 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004690 free_trash_chunk(tmptrash);
4691 tmptrash = NULL;
4692 }
4693 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4694 set_backend_down(s->proxy);
4695
4696 s->counters.down_trans++;
4697 }
4698 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4699 s->last_change = now.tv_sec;
4700 if (s->proxy->lbprm.set_server_status_down)
4701 s->proxy->lbprm.set_server_status_down(s);
4702
4703 /* we might have streams queued on this server and waiting for
4704 * a connection. Those which are redispatchable will be queued
4705 * to another server or to the proxy itself.
4706 */
4707 xferred = pendconn_redistribute(s);
4708
4709 tmptrash = alloc_trash_chunk();
4710 if (tmptrash) {
4711 chunk_printf(tmptrash,
4712 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4713 s->proxy->id, s->id);
4714
Emeric Brun5a133512017-10-19 14:42:30 +02004715 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004716
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004717 ha_warning("%s.\n", tmptrash->area);
4718 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4719 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004720 free_trash_chunk(tmptrash);
4721 tmptrash = NULL;
4722 }
4723
4724 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4725 set_backend_down(s->proxy);
4726 }
4727 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4728 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4729 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4730 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4731 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4732 s->proxy->last_change = now.tv_sec;
4733 }
4734
4735 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4736 s->down_time += now.tv_sec - s->last_change;
4737
4738 s->last_change = now.tv_sec;
4739 if (s->next_state == SRV_ST_STARTING)
4740 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4741
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004742 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004743 /* now propagate the status change to any LB algorithms */
4744 if (px->lbprm.update_server_eweight)
4745 px->lbprm.update_server_eweight(s);
4746 else if (srv_willbe_usable(s)) {
4747 if (px->lbprm.set_server_status_up)
4748 px->lbprm.set_server_status_up(s);
4749 }
4750 else {
4751 if (px->lbprm.set_server_status_down)
4752 px->lbprm.set_server_status_down(s);
4753 }
4754
4755 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4756 * and it's not a backup server and its effective weight is > 0,
4757 * then it can accept new connections, so we shut down all streams
4758 * on all backup servers.
4759 */
4760 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4761 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4762 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4763
4764 /* check if we can handle some connections queued at the proxy. We
4765 * will take as many as we can handle.
4766 */
4767 xferred = pendconn_grab_from_px(s);
4768
4769 tmptrash = alloc_trash_chunk();
4770 if (tmptrash) {
4771 chunk_printf(tmptrash,
4772 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4773 s->proxy->id, s->id);
4774
Emeric Brun5a133512017-10-19 14:42:30 +02004775 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004776 ha_warning("%s.\n", tmptrash->area);
4777 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4778 tmptrash->area);
4779 send_email_alert(s, LOG_NOTICE, "%s",
4780 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004781 free_trash_chunk(tmptrash);
4782 tmptrash = NULL;
4783 }
4784
4785 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4786 set_backend_down(s->proxy);
4787 }
4788 else if (s->cur_eweight != s->next_eweight) {
4789 /* now propagate the status change to any LB algorithms */
4790 if (px->lbprm.update_server_eweight)
4791 px->lbprm.update_server_eweight(s);
4792 else if (srv_willbe_usable(s)) {
4793 if (px->lbprm.set_server_status_up)
4794 px->lbprm.set_server_status_up(s);
4795 }
4796 else {
4797 if (px->lbprm.set_server_status_down)
4798 px->lbprm.set_server_status_down(s);
4799 }
4800
4801 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4802 set_backend_down(s->proxy);
4803 }
4804
4805 s->next_admin = next_admin;
4806 }
4807
Emeric Brun5a133512017-10-19 14:42:30 +02004808 /* reset operational state change */
4809 *s->op_st_chg.reason = 0;
4810 s->op_st_chg.status = s->op_st_chg.code = -1;
4811 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004812
4813 /* Now we try to apply pending admin changes */
4814
4815 /* Maintenance must also disable health checks */
4816 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4817 if (s->check.state & CHK_ST_ENABLED) {
4818 s->check.state |= CHK_ST_PAUSED;
4819 check->health = 0;
4820 }
4821
4822 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004823 tmptrash = alloc_trash_chunk();
4824 if (tmptrash) {
4825 chunk_printf(tmptrash,
4826 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4827 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4828 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004829
Olivier Houchard796a2b32017-10-24 17:42:47 +02004830 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004831
Olivier Houchard796a2b32017-10-24 17:42:47 +02004832 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004833 ha_warning("%s.\n", tmptrash->area);
4834 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4835 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004836 }
4837 free_trash_chunk(tmptrash);
4838 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004839 }
Emeric Brun8f298292017-12-06 16:47:17 +01004840 /* commit new admin status */
4841
4842 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004843 }
4844 else { /* server was still running */
4845 check->health = 0; /* failure */
4846 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004847
4848 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004849 if (s->proxy->lbprm.set_server_status_down)
4850 s->proxy->lbprm.set_server_status_down(s);
4851
Emeric Brun64cc49c2017-10-03 14:46:45 +02004852 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4853 srv_shutdown_streams(s, SF_ERR_DOWN);
4854
William Dauchy6318d332020-05-02 21:52:36 +02004855 /* force connection cleanup on the given server */
4856 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004857 /* we might have streams queued on this server and waiting for
4858 * a connection. Those which are redispatchable will be queued
4859 * to another server or to the proxy itself.
4860 */
4861 xferred = pendconn_redistribute(s);
4862
4863 tmptrash = alloc_trash_chunk();
4864 if (tmptrash) {
4865 chunk_printf(tmptrash,
4866 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4867 s->flags & SRV_F_BACKUP ? "Backup " : "",
4868 s->proxy->id, s->id,
4869 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4870
4871 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4872
4873 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004874 ha_warning("%s.\n", tmptrash->area);
4875 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4876 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004877 }
4878 free_trash_chunk(tmptrash);
4879 tmptrash = NULL;
4880 }
4881 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4882 set_backend_down(s->proxy);
4883
4884 s->counters.down_trans++;
4885 }
4886 }
4887 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4888 /* OK here we're leaving maintenance, we have many things to check,
4889 * because the server might possibly be coming back up depending on
4890 * its state. In practice, leaving maintenance means that we should
4891 * immediately turn to UP (more or less the slowstart) under the
4892 * following conditions :
4893 * - server is neither checked nor tracked
4894 * - server tracks another server which is not checked
4895 * - server tracks another server which is already up
4896 * Which sums up as something simpler :
4897 * "either the tracking server is up or the server's checks are disabled
4898 * or up". Otherwise we only re-enable health checks. There's a special
4899 * case associated to the stopping state which can be inherited. Note
4900 * that the server might still be in drain mode, which is naturally dealt
4901 * with by the lower level functions.
4902 */
4903
4904 if (s->check.state & CHK_ST_ENABLED) {
4905 s->check.state &= ~CHK_ST_PAUSED;
4906 check->health = check->rise; /* start OK but check immediately */
4907 }
4908
4909 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4910 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4911 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4912 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4913 s->next_state = SRV_ST_STOPPING;
4914 }
4915 else {
4916 s->next_state = SRV_ST_STARTING;
4917 if (s->slowstart > 0)
4918 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4919 else
4920 s->next_state = SRV_ST_RUNNING;
4921 }
4922
4923 }
4924
4925 tmptrash = alloc_trash_chunk();
4926 if (tmptrash) {
4927 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4928 chunk_printf(tmptrash,
4929 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4930 s->flags & SRV_F_BACKUP ? "Backup " : "",
4931 s->proxy->id, s->id,
4932 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4933 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4934 }
4935 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4936 chunk_printf(tmptrash,
4937 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4938 s->flags & SRV_F_BACKUP ? "Backup " : "",
4939 s->proxy->id, s->id, s->hostname,
4940 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4941 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4942 }
4943 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4944 chunk_printf(tmptrash,
4945 "%sServer %s/%s is %s/%s (leaving maintenance)",
4946 s->flags & SRV_F_BACKUP ? "Backup " : "",
4947 s->proxy->id, s->id,
4948 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4949 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4950 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004951 ha_warning("%s.\n", tmptrash->area);
4952 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4953 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004954 free_trash_chunk(tmptrash);
4955 tmptrash = NULL;
4956 }
4957
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004958 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004959 /* now propagate the status change to any LB algorithms */
4960 if (px->lbprm.update_server_eweight)
4961 px->lbprm.update_server_eweight(s);
4962 else if (srv_willbe_usable(s)) {
4963 if (px->lbprm.set_server_status_up)
4964 px->lbprm.set_server_status_up(s);
4965 }
4966 else {
4967 if (px->lbprm.set_server_status_down)
4968 px->lbprm.set_server_status_down(s);
4969 }
4970
4971 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4972 set_backend_down(s->proxy);
4973
Willy Tarreau6a78e612018-08-07 10:14:53 +02004974 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4975 * and it's not a backup server and its effective weight is > 0,
4976 * then it can accept new connections, so we shut down all streams
4977 * on all backup servers.
4978 */
4979 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4980 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4981 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4982
4983 /* check if we can handle some connections queued at the proxy. We
4984 * will take as many as we can handle.
4985 */
4986 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004987 }
4988 else if (s->next_admin & SRV_ADMF_MAINT) {
4989 /* remaining in maintenance mode, let's inform precisely about the
4990 * situation.
4991 */
4992 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4993 tmptrash = alloc_trash_chunk();
4994 if (tmptrash) {
4995 chunk_printf(tmptrash,
4996 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4997 s->flags & SRV_F_BACKUP ? "Backup " : "",
4998 s->proxy->id, s->id);
4999
5000 if (s->track) /* normally it's mandatory here */
5001 chunk_appendf(tmptrash, " via %s/%s",
5002 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005003 ha_warning("%s.\n", tmptrash->area);
5004 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5005 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005006 free_trash_chunk(tmptrash);
5007 tmptrash = NULL;
5008 }
5009 }
5010 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5011 tmptrash = alloc_trash_chunk();
5012 if (tmptrash) {
5013 chunk_printf(tmptrash,
5014 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5015 s->flags & SRV_F_BACKUP ? "Backup " : "",
5016 s->proxy->id, s->id, s->hostname);
5017
5018 if (s->track) /* normally it's mandatory here */
5019 chunk_appendf(tmptrash, " via %s/%s",
5020 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005021 ha_warning("%s.\n", tmptrash->area);
5022 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5023 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005024 free_trash_chunk(tmptrash);
5025 tmptrash = NULL;
5026 }
5027 }
5028 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5029 tmptrash = alloc_trash_chunk();
5030 if (tmptrash) {
5031 chunk_printf(tmptrash,
5032 "%sServer %s/%s remains in forced maintenance",
5033 s->flags & SRV_F_BACKUP ? "Backup " : "",
5034 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005035 ha_warning("%s.\n", tmptrash->area);
5036 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5037 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005038 free_trash_chunk(tmptrash);
5039 tmptrash = NULL;
5040 }
5041 }
5042 /* don't report anything when leaving drain mode and remaining in maintenance */
5043
5044 s->cur_admin = s->next_admin;
5045 }
5046
5047 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5048 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5049 /* drain state is applied only if not yet in maint */
5050
5051 s->last_change = now.tv_sec;
5052 if (px->lbprm.set_server_status_down)
5053 px->lbprm.set_server_status_down(s);
5054
5055 /* we might have streams queued on this server and waiting for
5056 * a connection. Those which are redispatchable will be queued
5057 * to another server or to the proxy itself.
5058 */
5059 xferred = pendconn_redistribute(s);
5060
5061 tmptrash = alloc_trash_chunk();
5062 if (tmptrash) {
5063 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5064 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5065 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5066
5067 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5068
5069 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005070 ha_warning("%s.\n", tmptrash->area);
5071 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5072 tmptrash->area);
5073 send_email_alert(s, LOG_NOTICE, "%s",
5074 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005075 }
5076 free_trash_chunk(tmptrash);
5077 tmptrash = NULL;
5078 }
5079
5080 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5081 set_backend_down(s->proxy);
5082 }
5083 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5084 /* OK completely leaving drain mode */
5085 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5086 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5087 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5088 s->proxy->last_change = now.tv_sec;
5089 }
5090
5091 if (s->last_change < now.tv_sec) // ignore negative times
5092 s->down_time += now.tv_sec - s->last_change;
5093 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005094 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005095
5096 tmptrash = alloc_trash_chunk();
5097 if (tmptrash) {
5098 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5099 chunk_printf(tmptrash,
5100 "%sServer %s/%s is %s (leaving forced drain)",
5101 s->flags & SRV_F_BACKUP ? "Backup " : "",
5102 s->proxy->id, s->id,
5103 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5104 }
5105 else {
5106 chunk_printf(tmptrash,
5107 "%sServer %s/%s is %s (leaving drain)",
5108 s->flags & SRV_F_BACKUP ? "Backup " : "",
5109 s->proxy->id, s->id,
5110 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5111 if (s->track) /* normally it's mandatory here */
5112 chunk_appendf(tmptrash, " via %s/%s",
5113 s->track->proxy->id, s->track->id);
5114 }
5115
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005116 ha_warning("%s.\n", tmptrash->area);
5117 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5118 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005119 free_trash_chunk(tmptrash);
5120 tmptrash = NULL;
5121 }
5122
5123 /* now propagate the status change to any LB algorithms */
5124 if (px->lbprm.update_server_eweight)
5125 px->lbprm.update_server_eweight(s);
5126 else if (srv_willbe_usable(s)) {
5127 if (px->lbprm.set_server_status_up)
5128 px->lbprm.set_server_status_up(s);
5129 }
5130 else {
5131 if (px->lbprm.set_server_status_down)
5132 px->lbprm.set_server_status_down(s);
5133 }
5134 }
5135 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5136 /* remaining in drain mode after removing one of its flags */
5137
5138 tmptrash = alloc_trash_chunk();
5139 if (tmptrash) {
5140 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5141 chunk_printf(tmptrash,
5142 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5143 s->flags & SRV_F_BACKUP ? "Backup " : "",
5144 s->proxy->id, s->id);
5145
5146 if (s->track) /* normally it's mandatory here */
5147 chunk_appendf(tmptrash, " via %s/%s",
5148 s->track->proxy->id, s->track->id);
5149 }
5150 else {
5151 chunk_printf(tmptrash,
5152 "%sServer %s/%s remains in forced drain mode",
5153 s->flags & SRV_F_BACKUP ? "Backup " : "",
5154 s->proxy->id, s->id);
5155 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005156 ha_warning("%s.\n", tmptrash->area);
5157 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5158 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005159 free_trash_chunk(tmptrash);
5160 tmptrash = NULL;
5161 }
5162
5163 /* commit new admin status */
5164
5165 s->cur_admin = s->next_admin;
5166 }
5167 }
5168
5169 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005170 *s->adm_st_chg_cause = 0;
5171}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005172
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005173struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5174{
5175 struct connection *conn;
5176
Olivier Houchard859dc802019-08-08 15:47:21 +02005177 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005178 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005179 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005180 }
5181
5182 return task;
5183}
5184
William Dauchy6318d332020-05-02 21:52:36 +02005185/* cleanup connections for a given server
5186 * might be useful when going on forced maintenance or live changing ip/port
5187 */
William Dauchy707ad322020-05-04 13:52:40 +02005188static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005189{
5190 struct connection *conn;
5191 int did_remove;
5192 int i;
5193 int j;
5194
5195 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5196 for (i = 0; i < global.nbthread; i++) {
5197 did_remove = 0;
5198 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
5199 for (j = 0; j < srv->curr_idle_conns; j++) {
5200 conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5201 if (!conn)
5202 conn = MT_LIST_POP(&srv->safe_conns[i],
5203 struct connection *, list);
5204 if (!conn)
5205 break;
5206 did_remove = 1;
5207 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
5208 }
5209 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
5210 if (did_remove)
5211 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5212 }
5213 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5214}
5215
Willy Tarreau980855b2019-02-07 14:59:29 +01005216struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005217{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005218 struct server *srv;
5219 struct eb32_node *eb;
5220 int i;
5221 unsigned int next_wakeup;
5222 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005223
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005224 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5225 while (1) {
5226 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005227 int exceed_conns;
5228 int to_kill;
5229 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005230
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005231 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5232 if (!eb) {
5233 /* we might have reached the end of the tree, typically because
5234 * <now_ms> is in the first half and we're first scanning the last
5235 * half. Let's loop back to the beginning of the tree now.
5236 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005237
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005238 eb = eb32_first(&idle_conn_srv);
5239 if (likely(!eb))
5240 break;
5241 }
5242 if (tick_is_lt(now_ms, eb->key)) {
5243 /* timer not expired yet, revisit it later */
5244 next_wakeup = eb->key;
5245 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005246 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005247 }
5248 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005249
5250 /* Calculate how many idle connections we want to kill :
5251 * we want to remove half the difference between the total
5252 * of established connections (used or idle) and the max
5253 * number of used connections.
5254 */
5255 curr_idle = srv->curr_idle_conns;
5256 if (curr_idle == 0)
5257 goto remove;
5258 exceed_conns = srv->curr_used_conns + curr_idle -
5259 srv->max_used_conns;
5260 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5261 srv->max_used_conns = srv->curr_used_conns;
5262
5263 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5264 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005265 int j;
5266 int did_remove = 0;
5267
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005268 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5269 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005270 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005271 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005272 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5273 if (!conn)
5274 conn = MT_LIST_POP(&srv->safe_conns[i],
5275 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005276 if (!conn)
5277 break;
5278 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005279 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005280 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005281 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005282 if (did_remove && max_conn < srv->curr_idle_thr[i])
5283 srv_is_empty = 0;
5284 if (did_remove)
5285 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5286 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005287remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005288 eb32_delete(&srv->idle_node);
5289 if (!srv_is_empty) {
5290 /* There are still more idle connections, add the
5291 * server back in the tree.
5292 */
5293 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5294 now_ms);
5295 eb32_insert(&idle_conn_srv, &srv->idle_node);
5296 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005297 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005298 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5299
5300 if (need_wakeup)
5301 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005302 else
5303 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005304
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005305 return task;
5306}
Olivier Houchard88698d92019-04-16 19:07:22 +02005307
5308/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5309static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5310 struct proxy *defpx, const char *file, int line,
5311 char **err)
5312{
5313 int arg = -1;
5314
5315 if (too_many_args(1, args, err, NULL))
5316 return -1;
5317
5318 if (*(args[1]) != 0)
5319 arg = atoi(args[1]);
5320
5321 if (arg < 0 || arg > 100) {
5322 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5323 return -1;
5324 }
5325
5326 if (args[0][10] == 'h')
5327 global.tune.pool_high_ratio = arg;
5328 else
5329 global.tune.pool_low_ratio = arg;
5330 return 0;
5331}
5332
5333/* config keyword parsers */
5334static struct cfg_kw_list cfg_kws = {ILH, {
5335 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5336 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5337 { 0, NULL, NULL }
5338}};
5339
5340INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5341
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005342/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005343 * Local variables:
5344 * c-indent-level: 8
5345 * c-basic-offset: 8
5346 * End:
5347 */