blob: 04d2347798f4990ffda279cdb2803ec4c902965b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau0a4b0ab2020-06-11 11:22:44 +020014#include <sys/types.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <netinet/tcp.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020017#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020018
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <import/xxhash.h>
20
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020022#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020023#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020025#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020027#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020028#include <haproxy/dict-t.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020029#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020032#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020033#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/port_range.h>
35#include <haproxy/protocol.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020036#include <haproxy/queue.h>
Emeric Brunc9437992021-02-12 19:42:55 +010037#include <haproxy/resolvers.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020038#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020039#include <haproxy/server.h>
William Dauchyf6370442020-11-14 19:25:33 +010040#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020041#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020044#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020045#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020046#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020047
Baptiste Assmannda29fe22019-06-13 13:24:29 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010050static int srv_apply_lastaddr(struct server *srv, int *err_code);
William Dauchy6318d332020-05-02 21:52:36 +020051static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau21faa912012-10-10 08:27:36 +020053/* List head of all known server keywords */
54static struct srv_kw_list srv_keywords = {
55 .list = LIST_HEAD_INIT(srv_keywords.list)
56};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057
Willy Tarreauaf613e82020-06-05 08:40:51 +020058__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010059struct eb_root idle_conn_srv = EB_ROOT;
60struct task *idle_conn_task = NULL;
Willy Tarreau198e92a2021-03-05 10:23:32 +010061struct list servers_list = LIST_HEAD_INIT(servers_list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010062
Frédéric Lécaille7da71292019-05-20 09:47:07 +020063/* The server names dictionary */
Thayne McCombs92149f92020-11-20 01:28:26 -070064struct dict server_key_dict = {
65 .name = "server keys",
Frédéric Lécaille7da71292019-05-20 09:47:07 +020066 .values = EB_ROOT_UNIQUE,
67};
68
Simon Hormana3608442013-11-01 16:46:15 +090069int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020070{
Amaury Denoyellee6ba7912020-10-29 15:59:05 +010071 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020072 return s->down_time;
73
74 return now.tv_sec - s->last_change + s->down_time;
75}
Willy Tarreaubaaee002006-06-26 02:48:02 +020076
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050077int srv_lastsession(const struct server *s)
78{
79 if (s->counters.last_sess)
80 return now.tv_sec - s->counters.last_sess;
81
82 return -1;
83}
84
Simon Horman4a741432013-02-23 15:35:38 +090085int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020086{
Simon Horman4a741432013-02-23 15:35:38 +090087 const struct server *s = check->server;
88
Willy Tarreauff5ae352013-12-11 20:36:34 +010089 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090090 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010091
Emeric Brun52a91d32017-08-31 14:41:55 +020092 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090093 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010094
Simon Horman4a741432013-02-23 15:35:38 +090095 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010096}
97
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010098/*
99 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200100 * Unlikely, but it can happen. The server's proxy must be at least
101 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100102 */
103static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100104{
105 struct proxy *p = s->proxy;
106 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100107
108 for (tmpserv = p->srv; tmpserv != NULL;
109 tmpserv = tmpserv->next) {
110 if (tmpserv == s)
111 continue;
112 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
113 continue;
114 if (tmpserv->cookie &&
115 strcmp(tmpserv->cookie, s->cookie) == 0) {
116 ha_warning("We generated two equal cookies for two different servers.\n"
117 "Please change the secret key for '%s'.\n",
118 s->proxy->id);
119 }
120 }
121
122}
123
Willy Tarreau46b7f532018-08-21 11:54:26 +0200124/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200125 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200126 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100127void srv_set_dyncookie(struct server *s)
128{
129 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100130 char *tmpbuf;
131 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100132 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100133 size_t buffer_len;
134 int addr_len;
135 int port;
136
Willy Tarreau595e7672020-10-20 17:30:08 +0200137 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200138
Olivier Houchard4e694042017-03-14 20:01:29 +0100139 if ((s->flags & SRV_F_COOKIESET) ||
140 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
141 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200142 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100143 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100144
145 if (s->addr.ss_family != AF_INET &&
146 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200147 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100148 /*
149 * Buffer to calculate the cookie value.
150 * The buffer contains the secret key + the server IP address
151 * + the TCP port.
152 */
153 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
154 /*
155 * The TCP port should use only 2 bytes, but is stored in
156 * an unsigned int in struct server, so let's use 4, to be
157 * on the safe side.
158 */
159 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200160 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100161 memcpy(tmpbuf, p->dyncookie_key, key_len);
162 memcpy(&(tmpbuf[key_len]),
163 s->addr.ss_family == AF_INET ?
164 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
165 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
166 addr_len);
167 /*
168 * Make sure it's the same across all the load balancers,
169 * no matter their endianness.
170 */
171 port = htonl(s->svc_port);
172 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
173 hash_value = XXH64(tmpbuf, buffer_len, 0);
174 memprintf(&s->cookie, "%016llx", hash_value);
175 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200176 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100177 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100178
179 /* Don't bother checking if the dyncookie is duplicated if
180 * the server is marked as "disabled", maybe it doesn't have
181 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100182 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100183 if (!(s->next_admin & SRV_ADMF_FMAINT))
184 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200185 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200186 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100187}
188
Willy Tarreau21faa912012-10-10 08:27:36 +0200189/*
Thayne McCombs92149f92020-11-20 01:28:26 -0700190 * Must be called with the server lock held, and will write-lock the proxy.
191 */
192static void srv_set_addr_desc(struct server *s)
193{
194 struct proxy *p = s->proxy;
195 char *key;
196
197 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
198
199 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700200 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700201 free(key);
202 return;
203 }
204
205 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
206 ebpt_delete(&s->addr_node);
207 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
208
209 free(s->addr_node.key);
210 }
211
212 s->addr_node.key = key;
213
Thayne McCombs24da7e12021-01-05 23:10:09 -0700214 if (s->addr_node.key) {
215 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
216 ebis_insert(&p->used_server_addr, &s->addr_node);
217 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
218 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700219}
220
221/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200222 * Registers the server keyword list <kwl> as a list of valid keywords for next
223 * parsing sessions.
224 */
225void srv_register_keywords(struct srv_kw_list *kwl)
226{
227 LIST_ADDQ(&srv_keywords.list, &kwl->list);
228}
229
230/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
231 * keyword is found with a NULL ->parse() function, then an attempt is made to
232 * find one with a valid ->parse() function. This way it is possible to declare
233 * platform-dependant, known keywords as NULL, then only declare them as valid
234 * if some options are met. Note that if the requested keyword contains an
235 * opening parenthesis, everything from this point is ignored.
236 */
237struct srv_kw *srv_find_kw(const char *kw)
238{
239 int index;
240 const char *kwend;
241 struct srv_kw_list *kwl;
242 struct srv_kw *ret = NULL;
243
244 kwend = strchr(kw, '(');
245 if (!kwend)
246 kwend = kw + strlen(kw);
247
248 list_for_each_entry(kwl, &srv_keywords.list, list) {
249 for (index = 0; kwl->kw[index].kw != NULL; index++) {
250 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
251 kwl->kw[index].kw[kwend-kw] == 0) {
252 if (kwl->kw[index].parse)
253 return &kwl->kw[index]; /* found it !*/
254 else
255 ret = &kwl->kw[index]; /* may be OK */
256 }
257 }
258 }
259 return ret;
260}
261
262/* Dumps all registered "server" keywords to the <out> string pointer. The
263 * unsupported keywords are only dumped if their supported form was not
264 * found.
265 */
266void srv_dump_kws(char **out)
267{
268 struct srv_kw_list *kwl;
269 int index;
270
Christopher Faulet784063e2020-05-18 12:14:18 +0200271 if (!out)
272 return;
273
Willy Tarreau21faa912012-10-10 08:27:36 +0200274 *out = NULL;
275 list_for_each_entry(kwl, &srv_keywords.list, list) {
276 for (index = 0; kwl->kw[index].kw != NULL; index++) {
277 if (kwl->kw[index].parse ||
278 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
279 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
280 kwl->scope,
281 kwl->kw[index].kw,
282 kwl->kw[index].skip ? " <arg>" : "",
283 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
284 kwl->kw[index].parse ? "" : " (not supported)");
285 }
286 }
287 }
288}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100289
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100290/* Parse the "backup" server keyword */
291static int srv_parse_backup(char **args, int *cur_arg,
292 struct proxy *curproxy, struct server *newsrv, char **err)
293{
294 newsrv->flags |= SRV_F_BACKUP;
295 return 0;
296}
297
Alexander Liu2a54bb72019-05-22 19:44:48 +0800298
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100299/* Parse the "cookie" server keyword */
300static int srv_parse_cookie(char **args, int *cur_arg,
301 struct proxy *curproxy, struct server *newsrv, char **err)
302{
303 char *arg;
304
305 arg = args[*cur_arg + 1];
306 if (!*arg) {
307 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
308 return ERR_ALERT | ERR_FATAL;
309 }
310
311 free(newsrv->cookie);
312 newsrv->cookie = strdup(arg);
313 newsrv->cklen = strlen(arg);
314 newsrv->flags |= SRV_F_COOKIESET;
315 return 0;
316}
317
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100318/* Parse the "disabled" server keyword */
319static int srv_parse_disabled(char **args, int *cur_arg,
320 struct proxy *curproxy, struct server *newsrv, char **err)
321{
Emeric Brun52a91d32017-08-31 14:41:55 +0200322 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
323 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100324 newsrv->check.state |= CHK_ST_PAUSED;
325 newsrv->check.health = 0;
326 return 0;
327}
328
329/* Parse the "enabled" server keyword */
330static int srv_parse_enabled(char **args, int *cur_arg,
331 struct proxy *curproxy, struct server *newsrv, char **err)
332{
Emeric Brun52a91d32017-08-31 14:41:55 +0200333 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
334 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100335 newsrv->check.state &= ~CHK_ST_PAUSED;
336 newsrv->check.health = newsrv->check.rise;
337 return 0;
338}
339
Willy Tarreau9c538e02019-01-23 10:21:49 +0100340static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
341{
342 char *arg;
343
344 arg = args[*cur_arg + 1];
345 if (!*arg) {
346 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
347 return ERR_ALERT | ERR_FATAL;
348 }
349 newsrv->max_reuse = atoi(arg);
350
351 return 0;
352}
353
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100354static 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 +0100355{
356 const char *res;
357 char *arg;
358 unsigned int time;
359
360 arg = args[*cur_arg + 1];
361 if (!*arg) {
362 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
363 return ERR_ALERT | ERR_FATAL;
364 }
365 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200366 if (res == PARSE_TIME_OVER) {
367 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
368 args[*cur_arg+1], args[*cur_arg]);
369 return ERR_ALERT | ERR_FATAL;
370 }
371 else if (res == PARSE_TIME_UNDER) {
372 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
373 args[*cur_arg+1], args[*cur_arg]);
374 return ERR_ALERT | ERR_FATAL;
375 }
376 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100377 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
378 *res, args[*cur_arg]);
379 return ERR_ALERT | ERR_FATAL;
380 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100381 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100382
383 return 0;
384}
385
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200386static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
387{
388 char *arg;
389
390 arg = args[*cur_arg + 1];
391 if (!*arg) {
392 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
393 return ERR_ALERT | ERR_FATAL;
394 }
395
396 newsrv->low_idle_conns = atoi(arg);
397 return 0;
398}
399
Olivier Houchard006e3102018-12-10 18:30:32 +0100400static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
401{
402 char *arg;
403
404 arg = args[*cur_arg + 1];
405 if (!*arg) {
406 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
407 return ERR_ALERT | ERR_FATAL;
408 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100409
Olivier Houchard006e3102018-12-10 18:30:32 +0100410 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100411 if ((int)newsrv->max_idle_conns < -1) {
412 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
413 return ERR_ALERT | ERR_FATAL;
414 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100415
416 return 0;
417}
418
Willy Tarreaudff55432012-10-10 17:51:05 +0200419/* parse the "id" server keyword */
420static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
421{
422 struct eb32_node *node;
423
424 if (!*args[*cur_arg + 1]) {
425 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
426 return ERR_ALERT | ERR_FATAL;
427 }
428
429 newsrv->puid = atol(args[*cur_arg + 1]);
430 newsrv->conf.id.key = newsrv->puid;
431
432 if (newsrv->puid <= 0) {
433 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
434 return ERR_ALERT | ERR_FATAL;
435 }
436
437 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
438 if (node) {
439 struct server *target = container_of(node, struct server, conf.id);
440 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
441 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
442 target->id);
443 return ERR_ALERT | ERR_FATAL;
444 }
445
446 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200447 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200448 return 0;
449}
450
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100451/* Parse the "namespace" server keyword */
452static int srv_parse_namespace(char **args, int *cur_arg,
453 struct proxy *curproxy, struct server *newsrv, char **err)
454{
Willy Tarreaue5733232019-05-22 19:24:06 +0200455#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100456 char *arg;
457
458 arg = args[*cur_arg + 1];
459 if (!*arg) {
460 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
461 return ERR_ALERT | ERR_FATAL;
462 }
463
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100464 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100465 /* Use the namespace associated with the connection (if present). */
466 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
467 return 0;
468 }
469
470 /*
471 * As this parser may be called several times for the same 'default-server'
472 * object, or for a new 'server' instance deriving from a 'default-server'
473 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
474 */
475 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
476
477 newsrv->netns = netns_store_lookup(arg, strlen(arg));
478 if (!newsrv->netns)
479 newsrv->netns = netns_store_insert(arg);
480
481 if (!newsrv->netns) {
482 memprintf(err, "Cannot open namespace '%s'", arg);
483 return ERR_ALERT | ERR_FATAL;
484 }
485
486 return 0;
487#else
488 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
489 return ERR_ALERT | ERR_FATAL;
490#endif
491}
492
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100493/* Parse the "no-backup" server keyword */
494static int srv_parse_no_backup(char **args, int *cur_arg,
495 struct proxy *curproxy, struct server *newsrv, char **err)
496{
497 newsrv->flags &= ~SRV_F_BACKUP;
498 return 0;
499}
500
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100501
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100502/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200503static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100504{
505 srv->pp_opts &= ~flags;
506 return 0;
507}
508
509/* Parse the "no-send-proxy" server keyword */
510static int srv_parse_no_send_proxy(char **args, int *cur_arg,
511 struct proxy *curproxy, struct server *newsrv, char **err)
512{
513 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
514}
515
516/* Parse the "no-send-proxy-v2" server keyword */
517static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
518 struct proxy *curproxy, struct server *newsrv, char **err)
519{
520 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
521}
522
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200523/* Parse the "no-tfo" server keyword */
524static int srv_parse_no_tfo(char **args, int *cur_arg,
525 struct proxy *curproxy, struct server *newsrv, char **err)
526{
527 newsrv->flags &= ~SRV_F_FASTOPEN;
528 return 0;
529}
530
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100531/* Parse the "non-stick" server keyword */
532static int srv_parse_non_stick(char **args, int *cur_arg,
533 struct proxy *curproxy, struct server *newsrv, char **err)
534{
535 newsrv->flags |= SRV_F_NON_STICK;
536 return 0;
537}
538
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100539/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200540static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100541{
542 srv->pp_opts |= flags;
543 return 0;
544}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200545/* parse the "proto" server keyword */
546static int srv_parse_proto(char **args, int *cur_arg,
547 struct proxy *px, struct server *newsrv, char **err)
548{
549 struct ist proto;
550
551 if (!*args[*cur_arg + 1]) {
552 memprintf(err, "'%s' : missing value", args[*cur_arg]);
553 return ERR_ALERT | ERR_FATAL;
554 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100555 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200556 newsrv->mux_proto = get_mux_proto(proto);
557 if (!newsrv->mux_proto) {
558 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
559 return ERR_ALERT | ERR_FATAL;
560 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200561 return 0;
562}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100563
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100564/* parse the "proxy-v2-options" */
565static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
566 struct proxy *px, struct server *newsrv, char **err)
567{
568 char *p, *n;
569 for (p = args[*cur_arg+1]; p; p = n) {
570 n = strchr(p, ',');
571 if (n)
572 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100573 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100574 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100575 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100576 newsrv->pp_opts |= SRV_PP_V2_SSL;
577 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100578 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100579 newsrv->pp_opts |= SRV_PP_V2_SSL;
580 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100581 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100582 newsrv->pp_opts |= SRV_PP_V2_SSL;
583 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100584 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100585 newsrv->pp_opts |= SRV_PP_V2_SSL;
586 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100587 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100588 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100589 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100590 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100591 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100592 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100593 } else
594 goto fail;
595 }
596 return 0;
597 fail:
598 if (err)
599 memprintf(err, "'%s' : proxy v2 option not implemented", p);
600 return ERR_ALERT | ERR_FATAL;
601}
602
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100603/* Parse the "observe" server keyword */
604static int srv_parse_observe(char **args, int *cur_arg,
605 struct proxy *curproxy, struct server *newsrv, char **err)
606{
607 char *arg;
608
609 arg = args[*cur_arg + 1];
610 if (!*arg) {
611 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
612 return ERR_ALERT | ERR_FATAL;
613 }
614
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100615 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100616 newsrv->observe = HANA_OBS_NONE;
617 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100618 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100619 newsrv->observe = HANA_OBS_LAYER4;
620 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100621 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100622 if (curproxy->mode != PR_MODE_HTTP) {
623 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
624 return ERR_ALERT;
625 }
626 newsrv->observe = HANA_OBS_LAYER7;
627 }
628 else {
629 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
630 "but got '%s'\n", args[*cur_arg], arg);
631 return ERR_ALERT | ERR_FATAL;
632 }
633
634 return 0;
635}
636
Frédéric Lécaille16186232017-03-14 16:42:49 +0100637/* Parse the "redir" server keyword */
638static int srv_parse_redir(char **args, int *cur_arg,
639 struct proxy *curproxy, struct server *newsrv, char **err)
640{
641 char *arg;
642
643 arg = args[*cur_arg + 1];
644 if (!*arg) {
645 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
646 return ERR_ALERT | ERR_FATAL;
647 }
648
649 free(newsrv->rdr_pfx);
650 newsrv->rdr_pfx = strdup(arg);
651 newsrv->rdr_len = strlen(arg);
652
653 return 0;
654}
655
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100656/* Parse the "send-proxy" server keyword */
657static int srv_parse_send_proxy(char **args, int *cur_arg,
658 struct proxy *curproxy, struct server *newsrv, char **err)
659{
660 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
661}
662
663/* Parse the "send-proxy-v2" server keyword */
664static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
665 struct proxy *curproxy, struct server *newsrv, char **err)
666{
667 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
668}
669
Frédéric Lécailledba97072017-03-17 15:33:50 +0100670
671/* Parse the "source" server keyword */
672static int srv_parse_source(char **args, int *cur_arg,
673 struct proxy *curproxy, struct server *newsrv, char **err)
674{
675 char *errmsg;
676 int port_low, port_high;
677 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +0100678
679 errmsg = NULL;
680
681 if (!*args[*cur_arg + 1]) {
682 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
683 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
684 goto err;
685 }
686
687 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200688 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
689 &errmsg, NULL, NULL,
690 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100691 if (!sk) {
692 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
693 goto err;
694 }
695
Frédéric Lécailledba97072017-03-17 15:33:50 +0100696 newsrv->conn_src.opts |= CO_SRC_BIND;
697 newsrv->conn_src.source_addr = *sk;
698
699 if (port_low != port_high) {
700 int i;
701
Frédéric Lécailledba97072017-03-17 15:33:50 +0100702 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
703 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
704 newsrv->conn_src.sport_range->ports[i] = port_low + i;
705 }
706
707 *cur_arg += 2;
708 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100709 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +0100710#if defined(CONFIG_HAP_TRANSPARENT)
711 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100712 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
713 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100714 goto err;
715 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100716 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +0100717 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
718 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
719 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100720 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +0100721 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
722 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
723 }
724 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
725 char *name, *end;
726
727 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100728 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100729 name++;
730
731 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100732 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100733 end++;
734
735 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
736 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
737 free(newsrv->conn_src.bind_hdr_name);
738 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
739 newsrv->conn_src.bind_hdr_len = end - name;
740 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
741 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
742 newsrv->conn_src.bind_hdr_occ = -1;
743
744 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100745 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100746 end++;
747 if (*end == ',') {
748 end++;
749 name = end;
750 if (*end == '-')
751 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100752 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753 end++;
754 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
755 }
756
757 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_alert("usesrc hdr_ip(name,num) does not support negative"
759 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 goto err;
761 }
762 }
763 else {
764 struct sockaddr_storage *sk;
765 int port1, port2;
766
767 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200768 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
769 &errmsg, NULL, NULL,
770 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100771 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100772 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100773 goto err;
774 }
775
Frédéric Lécailledba97072017-03-17 15:33:50 +0100776 newsrv->conn_src.tproxy_addr = *sk;
777 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
778 }
779 global.last_checks |= LSTCHK_NETADM;
780 *cur_arg += 2;
781 continue;
782#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 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 +0100784 goto err;
785#endif /* defined(CONFIG_HAP_TRANSPARENT) */
786 } /* "usesrc" */
787
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100788 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +0100789#ifdef SO_BINDTODEVICE
790 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100792 goto err;
793 }
794 free(newsrv->conn_src.iface_name);
795 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
796 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
797 global.last_checks |= LSTCHK_NETADM;
798#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100799 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100800 goto err;
801#endif
802 *cur_arg += 2;
803 continue;
804 }
805 /* this keyword in not an option of "source" */
806 break;
807 } /* while */
808
809 return 0;
810
811 err:
812 free(errmsg);
813 return ERR_ALERT | ERR_FATAL;
814}
815
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100816/* Parse the "stick" server keyword */
817static int srv_parse_stick(char **args, int *cur_arg,
818 struct proxy *curproxy, struct server *newsrv, char **err)
819{
820 newsrv->flags &= ~SRV_F_NON_STICK;
821 return 0;
822}
823
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100824/* Parse the "track" server keyword */
825static int srv_parse_track(char **args, int *cur_arg,
826 struct proxy *curproxy, struct server *newsrv, char **err)
827{
828 char *arg;
829
830 arg = args[*cur_arg + 1];
831 if (!*arg) {
832 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
833 return ERR_ALERT | ERR_FATAL;
834 }
835
836 free(newsrv->trackit);
837 newsrv->trackit = strdup(arg);
838
839 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800840}
841
842/* Parse the "socks4" server keyword */
843static int srv_parse_socks4(char **args, int *cur_arg,
844 struct proxy *curproxy, struct server *newsrv, char **err)
845{
846 char *errmsg;
847 int port_low, port_high;
848 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800849
850 errmsg = NULL;
851
852 if (!*args[*cur_arg + 1]) {
853 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
854 goto err;
855 }
856
857 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200858 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
859 &errmsg, NULL, NULL,
860 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +0800861 if (!sk) {
862 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
863 goto err;
864 }
865
Alexander Liu2a54bb72019-05-22 19:44:48 +0800866 newsrv->flags |= SRV_F_SOCKS4_PROXY;
867 newsrv->socks4_addr = *sk;
868
Alexander Liu2a54bb72019-05-22 19:44:48 +0800869 return 0;
870
871 err:
872 free(errmsg);
873 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100874}
875
Frédéric Lécailledba97072017-03-17 15:33:50 +0100876
Willy Tarreau034c88c2017-01-23 23:36:45 +0100877/* parse the "tfo" server keyword */
878static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
879{
880 newsrv->flags |= SRV_F_FASTOPEN;
881 return 0;
882}
883
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200884/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200885 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200886 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200887 *
888 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200889 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200890void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200891{
Willy Tarreau751153e2021-02-17 13:33:24 +0100892 struct stream *stream;
893 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +0100894 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200895
Willy Tarreaud4e78d82021-03-04 10:47:54 +0100896 for (thr = 0; thr < global.nbthread; thr++)
897 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
898 if (stream->srv_conn == srv)
899 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200900}
901
902/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200903 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200904 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200905 *
906 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200907 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200908void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200909{
910 struct server *srv;
911
912 for (srv = px->srv; srv != NULL; srv = srv->next)
913 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200914 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200915}
916
Willy Tarreaubda92272014-05-20 21:55:30 +0200917/* Appends some information to a message string related to a server going UP or
918 * DOWN. If both <forced> and <reason> are null and the server tracks another
919 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200920 * If <check> is non-null, an entire string describing the check result will be
921 * appended after a comma and a space (eg: to report some information from the
922 * check that changed the state). In the other case, the string will be built
923 * using the check results stored into the struct server if present.
924 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200925 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200926 *
927 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200928 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200929void srv_append_status(struct buffer *msg, struct server *s,
930 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200931{
Emeric Brun5a133512017-10-19 14:42:30 +0200932 short status = s->op_st_chg.status;
933 short code = s->op_st_chg.code;
934 long duration = s->op_st_chg.duration;
935 char *desc = s->op_st_chg.reason;
936
937 if (check) {
938 status = check->status;
939 code = check->code;
940 duration = check->duration;
941 desc = check->desc;
942 }
943
944 if (status != -1) {
945 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
946
947 if (status >= HCHK_STATUS_L57DATA)
948 chunk_appendf(msg, ", code: %d", code);
949
950 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200951 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200952
953 chunk_appendf(msg, ", info: \"");
954
955 chunk_initlen(&src, desc, 0, strlen(desc));
956 chunk_asciiencode(msg, &src, '"');
957
958 chunk_appendf(msg, "\"");
959 }
960
961 if (duration >= 0)
962 chunk_appendf(msg, ", check duration: %ldms", duration);
963 }
964 else if (desc && *desc) {
965 chunk_appendf(msg, ", %s", desc);
966 }
967 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200968 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200969 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200970
971 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200972 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200973 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
974 " %d sessions active, %d requeued, %d remaining in queue",
975 s->proxy->srv_act, s->proxy->srv_bck,
976 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
977 s->cur_sess, xferred, s->nbpend);
978 else
979 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
980 " %d sessions requeued, %d total in queue",
981 s->proxy->srv_act, s->proxy->srv_bck,
982 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
983 xferred, s->nbpend);
984 }
985}
986
Emeric Brun5a133512017-10-19 14:42:30 +0200987/* Marks server <s> down, regardless of its checks' statuses. The server is
988 * registered in a list to postpone the counting of the remaining servers on
989 * the proxy and transfers queued streams whenever possible to other servers at
990 * a sync point. Maintenance servers are ignored. It stores the <reason> if
991 * non-null as the reason for going down or the available data from the check
992 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200993 *
994 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200995 */
Emeric Brun5a133512017-10-19 14:42:30 +0200996void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200997{
998 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200999
Emeric Brun64cc49c2017-10-03 14:46:45 +02001000 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001001 return;
1002
Emeric Brun52a91d32017-08-31 14:41:55 +02001003 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001004 *s->op_st_chg.reason = 0;
1005 s->op_st_chg.status = -1;
1006 if (reason) {
1007 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1008 }
1009 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001010 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001011 s->op_st_chg.code = check->code;
1012 s->op_st_chg.status = check->status;
1013 s->op_st_chg.duration = check->duration;
1014 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001015
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001016 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001017 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001018
Emeric Brun9f0b4582017-10-23 14:39:51 +02001019 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001020 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001021 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001022 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001023 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001024}
1025
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001026/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001027 * in maintenance. The server is registered in a list to postpone the counting
1028 * of the remaining servers on the proxy and tries to grab requests from the
1029 * proxy at a sync point. Maintenance servers are ignored. It stores the
1030 * <reason> if non-null as the reason for going down or the available data
1031 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001032 *
1033 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001034 */
Emeric Brun5a133512017-10-19 14:42:30 +02001035void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001036{
1037 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038
Emeric Brun64cc49c2017-10-03 14:46:45 +02001039 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001040 return;
1041
Emeric Brun52a91d32017-08-31 14:41:55 +02001042 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001043 return;
1044
Emeric Brun52a91d32017-08-31 14:41:55 +02001045 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001046 *s->op_st_chg.reason = 0;
1047 s->op_st_chg.status = -1;
1048 if (reason) {
1049 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1050 }
1051 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001052 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001053 s->op_st_chg.code = check->code;
1054 s->op_st_chg.status = check->status;
1055 s->op_st_chg.duration = check->duration;
1056 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001057
Emeric Brun64cc49c2017-10-03 14:46:45 +02001058 if (s->slowstart <= 0)
1059 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001060
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001061 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001062 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001063
Emeric Brun9f0b4582017-10-23 14:39:51 +02001064 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001066 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001067 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001068 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001069}
1070
Willy Tarreau8eb77842014-05-21 13:54:57 +02001071/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001072 * isn't in maintenance. The server is registered in a list to postpone the
1073 * counting of the remaining servers on the proxy and tries to grab requests
1074 * from the proxy. Maintenance servers are ignored. It stores the
1075 * <reason> if non-null as the reason for going down or the available data
1076 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001077 * up. Note that it makes use of the trash to build the log strings, so <reason>
1078 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001079 *
1080 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001081 */
Emeric Brun5a133512017-10-19 14:42:30 +02001082void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001083{
1084 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085
Emeric Brun64cc49c2017-10-03 14:46:45 +02001086 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001087 return;
1088
Emeric Brun52a91d32017-08-31 14:41:55 +02001089 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001090 return;
1091
Emeric Brun52a91d32017-08-31 14:41:55 +02001092 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001093 *s->op_st_chg.reason = 0;
1094 s->op_st_chg.status = -1;
1095 if (reason) {
1096 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1097 }
1098 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001099 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001100 s->op_st_chg.code = check->code;
1101 s->op_st_chg.status = check->status;
1102 s->op_st_chg.duration = check->duration;
1103 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001104
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001105 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001106 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001107
Emeric Brun9f0b4582017-10-23 14:39:51 +02001108 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001109 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001110 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001111 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001112 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001113}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001114
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001115/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1116 * enforce either maint mode or drain mode. It is not allowed to set more than
1117 * one flag at once. The equivalent "inherited" flag is propagated to all
1118 * tracking servers. Maintenance mode disables health checks (but not agent
1119 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001120 * is done. If <cause> is non-null, it will be displayed at the end of the log
1121 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001122 *
1123 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001124 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001125void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001127 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001128
1129 if (!mode)
1130 return;
1131
1132 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001133 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001134 return;
1135
Emeric Brun52a91d32017-08-31 14:41:55 +02001136 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001137 if (cause)
1138 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1139
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001140 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001141 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001142
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001143 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001144 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1145 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001146 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001147
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001148 /* compute the inherited flag to propagate */
1149 if (mode & SRV_ADMF_MAINT)
1150 mode = SRV_ADMF_IMAINT;
1151 else if (mode & SRV_ADMF_DRAIN)
1152 mode = SRV_ADMF_IDRAIN;
1153
Emeric Brun9f0b4582017-10-23 14:39:51 +02001154 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001155 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001156 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001157 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001158 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001159}
1160
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001161/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1162 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1163 * than one flag at once. The equivalent "inherited" flag is propagated to all
1164 * tracking servers. Leaving maintenance mode re-enables health checks. When
1165 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001166 *
1167 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001168 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001169void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001171 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001172
1173 if (!mode)
1174 return;
1175
1176 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001177 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001178 return;
1179
Emeric Brun52a91d32017-08-31 14:41:55 +02001180 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001181
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001182 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001183 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001184
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001185 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001186 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1187 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001188 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001189
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001190 if (mode & SRV_ADMF_MAINT)
1191 mode = SRV_ADMF_IMAINT;
1192 else if (mode & SRV_ADMF_DRAIN)
1193 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001194
Emeric Brun9f0b4582017-10-23 14:39:51 +02001195 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001196 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001197 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001198 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001199 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001200}
1201
Willy Tarreau757478e2016-11-03 19:22:19 +01001202/* principle: propagate maint and drain to tracking servers. This is useful
1203 * upon startup so that inherited states are correct.
1204 */
1205static void srv_propagate_admin_state(struct server *srv)
1206{
1207 struct server *srv2;
1208
1209 if (!srv->trackers)
1210 return;
1211
1212 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001213 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001214 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001215 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001216
Emeric Brun52a91d32017-08-31 14:41:55 +02001217 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001218 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001219 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001220 }
1221}
1222
1223/* Compute and propagate the admin states for all servers in proxy <px>.
1224 * Only servers *not* tracking another one are considered, because other
1225 * ones will be handled when the server they track is visited.
1226 */
1227void srv_compute_all_admin_states(struct proxy *px)
1228{
1229 struct server *srv;
1230
1231 for (srv = px->srv; srv; srv = srv->next) {
1232 if (srv->track)
1233 continue;
1234 srv_propagate_admin_state(srv);
1235 }
1236}
1237
Willy Tarreaudff55432012-10-10 17:51:05 +02001238/* Note: must not be declared <const> as its list will be overwritten.
1239 * Please take care of keeping this list alphabetically sorted, doing so helps
1240 * all code contributors.
1241 * Optional keywords are also declared with a NULL ->parse() function so that
1242 * the config parser can report an appropriate error when a known keyword was
1243 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001244 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001245 */
1246static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001247 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001248 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001249 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1250 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001251 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001252 { "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 +01001253 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001254 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001255 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1256 { "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 +02001257 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001258 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001259 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001260 { "pool-low-conn", srv_parse_pool_low_conn, 1, 1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */
Amaury Denoyelle18c68df2021-01-26 14:35:24 +01001261 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001262 { "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 +02001263 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001264 { "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 +01001265 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001266 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1267 { "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 +02001268 { "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 +01001269 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001270 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001271 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001272 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001273 { NULL, NULL, 0 },
1274}};
1275
Willy Tarreau0108d902018-11-25 19:14:37 +01001276INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001277
Willy Tarreau004e0452013-11-21 11:22:01 +01001278/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001279 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001280 * state is automatically disabled if the time is elapsed. If <must_update> is
1281 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001282 *
1283 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001284 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001285void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001286{
1287 struct proxy *px = sv->proxy;
1288 unsigned w;
1289
1290 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1291 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001292 if (sv->next_state == SRV_ST_STARTING)
1293 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001294 }
1295
1296 /* We must take care of not pushing the server to full throttle during slow starts.
1297 * It must also start immediately, at least at the minimal step when leaving maintenance.
1298 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001299 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001300 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1301 else
1302 w = px->lbprm.wdiv;
1303
Emeric Brun52a91d32017-08-31 14:41:55 +02001304 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001305
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001306 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001307 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001308 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001309}
1310
Willy Tarreaubaaee002006-06-26 02:48:02 +02001311/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001312 * Parses weight_str and configures sv accordingly.
1313 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001314 *
1315 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001316 */
1317const char *server_parse_weight_change_request(struct server *sv,
1318 const char *weight_str)
1319{
1320 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001321 long int w;
1322 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001323
1324 px = sv->proxy;
1325
1326 /* if the weight is terminated with '%', it is set relative to
1327 * the initial weight, otherwise it is absolute.
1328 */
1329 if (!*weight_str)
1330 return "Require <weight> or <weight%>.\n";
1331
Simon Hormanb796afa2013-02-12 10:45:53 +09001332 w = strtol(weight_str, &end, 10);
1333 if (end == weight_str)
1334 return "Empty weight string empty or preceded by garbage";
1335 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001336 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001337 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001338 /* Avoid integer overflow */
1339 if (w > 25600)
1340 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001341 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001342 if (w > 256)
1343 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001344 }
1345 else if (w < 0 || w > 256)
1346 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001347 else if (end[0] != '\0')
1348 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001349
1350 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1351 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1352
1353 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001354 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001355
1356 return NULL;
1357}
1358
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001359/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001360 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1361 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001362 * Returns:
1363 * - error string on error
1364 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001365 *
1366 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001367 */
1368const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001369 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001370{
1371 unsigned char ip[INET6_ADDRSTRLEN];
1372
1373 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001374 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001375 return NULL;
1376 }
1377 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001378 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001379 return NULL;
1380 }
1381
1382 return "Could not understand IP address format.\n";
1383}
1384
Willy Tarreau46b7f532018-08-21 11:54:26 +02001385/*
1386 * Must be called with the server lock held.
1387 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001388const char *server_parse_maxconn_change_request(struct server *sv,
1389 const char *maxconn_str)
1390{
1391 long int v;
1392 char *end;
1393
1394 if (!*maxconn_str)
1395 return "Require <maxconn>.\n";
1396
1397 v = strtol(maxconn_str, &end, 10);
1398 if (end == maxconn_str)
1399 return "maxconn string empty or preceded by garbage";
1400 else if (end[0] != '\0')
1401 return "Trailing garbage in maxconn string";
1402
1403 if (sv->maxconn == sv->minconn) { // static maxconn
1404 sv->maxconn = sv->minconn = v;
1405 } else { // dynamic maxconn
1406 sv->maxconn = v;
1407 }
1408
1409 if (may_dequeue_tasks(sv, sv->proxy))
1410 process_srv_queue(sv);
1411
1412 return NULL;
1413}
1414
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001415#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001416static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1417 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001418{
1419 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001420 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001421 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001422 NULL,
1423 };
1424
1425 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001426 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001427
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001428 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001429}
1430
1431static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1432{
1433 struct sample_expr *expr;
1434
1435 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 +01001436 if (!expr) {
1437 memprintf(err, "error detected while parsing sni expression : %s", *err);
1438 return ERR_ALERT | ERR_FATAL;
1439 }
1440
1441 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1442 memprintf(err, "error detected while parsing sni expression : "
1443 " fetch method '%s' extracts information from '%s', "
1444 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001445 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001446 return ERR_ALERT | ERR_FATAL;
1447 }
1448
1449 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1450 release_sample_expr(newsrv->ssl_ctx.sni);
1451 newsrv->ssl_ctx.sni = expr;
1452
1453 return 0;
1454}
1455#endif
1456
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001457static 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 +01001458{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001459 char *msg = "error encountered while processing ";
1460 char *quote = "'";
1461 char *token = args[cur_arg];
1462
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001463 if (err && *err) {
1464 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001465 msg = *err;
1466 quote = "";
1467 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001468 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001469
1470 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1471 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1472 file, linenum, args[0], args[1],
1473 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001474 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001475 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1476 file, linenum, args[0], args[1],
1477 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001478}
1479
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001480static void srv_conn_src_sport_range_cpy(struct server *srv,
1481 struct server *src)
1482{
1483 int range_sz;
1484
1485 range_sz = src->conn_src.sport_range->size;
1486 if (range_sz > 0) {
1487 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1488 if (srv->conn_src.sport_range != NULL) {
1489 int i;
1490
1491 for (i = 0; i < range_sz; i++) {
1492 srv->conn_src.sport_range->ports[i] =
1493 src->conn_src.sport_range->ports[i];
1494 }
1495 }
1496 }
1497}
1498
1499/*
1500 * Copy <src> server connection source settings to <srv> server everything needed.
1501 */
1502static void srv_conn_src_cpy(struct server *srv, struct server *src)
1503{
1504 srv->conn_src.opts = src->conn_src.opts;
1505 srv->conn_src.source_addr = src->conn_src.source_addr;
1506
1507 /* Source port range copy. */
1508 if (src->conn_src.sport_range != NULL)
1509 srv_conn_src_sport_range_cpy(srv, src);
1510
1511#ifdef CONFIG_HAP_TRANSPARENT
1512 if (src->conn_src.bind_hdr_name != NULL) {
1513 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1514 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1515 }
1516 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1517 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1518#endif
1519 if (src->conn_src.iface_name != NULL)
1520 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1521}
1522
1523/*
1524 * Copy <src> server SSL settings to <srv> server allocating
1525 * everything needed.
1526 */
1527#if defined(USE_OPENSSL)
1528static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1529{
1530 if (src->ssl_ctx.ca_file != NULL)
1531 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1532 if (src->ssl_ctx.crl_file != NULL)
1533 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001534
1535 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1536
1537 if (src->ssl_ctx.verify_host != NULL)
1538 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1539 if (src->ssl_ctx.ciphers != NULL)
1540 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001541 if (src->ssl_ctx.options)
1542 srv->ssl_ctx.options = src->ssl_ctx.options;
1543 if (src->ssl_ctx.methods.flags)
1544 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1545 if (src->ssl_ctx.methods.min)
1546 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1547 if (src->ssl_ctx.methods.max)
1548 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1549
Ilya Shipitsin2aa4b3a2021-01-07 11:55:45 +05001550#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001551 if (src->ssl_ctx.ciphersuites != NULL)
1552 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1553#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001554 if (src->sni_expr != NULL)
1555 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001556
1557#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1558 if (src->ssl_ctx.alpn_str) {
1559 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1560 if (srv->ssl_ctx.alpn_str) {
1561 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1562 src->ssl_ctx.alpn_len);
1563 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1564 }
1565 }
1566#endif
1567#ifdef OPENSSL_NPN_NEGOTIATED
1568 if (src->ssl_ctx.npn_str) {
1569 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1570 if (srv->ssl_ctx.npn_str) {
1571 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1572 src->ssl_ctx.npn_len);
1573 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1574 }
1575 }
1576#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001577}
1578#endif
1579
1580/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001581 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001582 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001583 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001584 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01001585int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001586{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001587 char *hostname_dn;
1588 int hostname_len, hostname_dn_len;
1589
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001590 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001591 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001592
Christopher Faulet67957bd2017-09-27 11:00:59 +02001593 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001594 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01001595 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
1596 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001597 if (hostname_dn_len == -1)
1598 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001599
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001600
Christopher Faulet67957bd2017-09-27 11:00:59 +02001601 free(srv->hostname);
1602 free(srv->hostname_dn);
1603 srv->hostname = strdup(hostname);
1604 srv->hostname_dn = strdup(hostname_dn);
1605 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001606 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001607 goto err;
1608
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001609 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001610
1611 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001612 ha_free(&srv->hostname);
1613 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001614 return -1;
1615}
1616
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001617/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001618 * Copy <src> server settings to <srv> server allocating
1619 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001620 * This function is not supposed to be called at any time, but only
1621 * during server settings parsing or during server allocations from
1622 * a server template, and just after having calloc()'ed a new server.
1623 * So, <src> may only be a default server (when parsing server settings)
1624 * or a server template (during server allocations from a server template).
1625 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1626 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001627 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001628static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001629{
1630 /* Connection source settings copy */
1631 srv_conn_src_cpy(srv, src);
1632
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001633 if (srv_tmpl) {
1634 srv->addr = src->addr;
1635 srv->svc_port = src->svc_port;
1636 }
1637
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001638 srv->pp_opts = src->pp_opts;
1639 if (src->rdr_pfx != NULL) {
1640 srv->rdr_pfx = strdup(src->rdr_pfx);
1641 srv->rdr_len = src->rdr_len;
1642 }
1643 if (src->cookie != NULL) {
1644 srv->cookie = strdup(src->cookie);
1645 srv->cklen = src->cklen;
1646 }
1647 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001648 srv->check.addr = src->check.addr;
1649 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001650 srv->check.use_ssl = src->check.use_ssl;
1651 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001652 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001653 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001654 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001655 /* Note: 'flags' field has potentially been already initialized. */
1656 srv->flags |= src->flags;
1657 srv->do_check = src->do_check;
1658 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001659 srv->check.inter = src->check.inter;
1660 srv->check.fastinter = src->check.fastinter;
1661 srv->check.downinter = src->check.downinter;
1662 srv->agent.use_ssl = src->agent.use_ssl;
1663 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001664
1665 if (src->agent.tcpcheck_rules) {
1666 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1667 if (srv->agent.tcpcheck_rules) {
1668 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1669 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1670 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1671 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1672 &src->agent.tcpcheck_rules->preset_vars);
1673 }
1674 }
1675
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001676 srv->agent.inter = src->agent.inter;
1677 srv->agent.fastinter = src->agent.fastinter;
1678 srv->agent.downinter = src->agent.downinter;
1679 srv->maxqueue = src->maxqueue;
1680 srv->minconn = src->minconn;
1681 srv->maxconn = src->maxconn;
1682 srv->slowstart = src->slowstart;
1683 srv->observe = src->observe;
1684 srv->onerror = src->onerror;
1685 srv->onmarkeddown = src->onmarkeddown;
1686 srv->onmarkedup = src->onmarkedup;
1687 if (src->trackit != NULL)
1688 srv->trackit = strdup(src->trackit);
1689 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1690 srv->uweight = srv->iweight = src->iweight;
1691
1692 srv->check.send_proxy = src->check.send_proxy;
1693 /* health: up, but will fall down at first failure */
1694 srv->check.rise = srv->check.health = src->check.rise;
1695 srv->check.fall = src->check.fall;
1696
1697 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001698 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1699 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1700 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001701 srv->check.state |= CHK_ST_PAUSED;
1702 srv->check.health = 0;
1703 }
1704
1705 /* health: up but will fall down at first failure */
1706 srv->agent.rise = srv->agent.health = src->agent.rise;
1707 srv->agent.fall = src->agent.fall;
1708
1709 if (src->resolvers_id != NULL)
1710 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01001711 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
1712 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
1713 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
1714 if (srv->resolv_opts.family_prio == AF_UNSPEC)
1715 srv->resolv_opts.family_prio = AF_INET6;
1716 memcpy(srv->resolv_opts.pref_net,
1717 src->resolv_opts.pref_net,
1718 sizeof srv->resolv_opts.pref_net);
1719 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001720
1721 srv->init_addr_methods = src->init_addr_methods;
1722 srv->init_addr = src->init_addr;
1723#if defined(USE_OPENSSL)
1724 srv_ssl_settings_cpy(srv, src);
1725#endif
1726#ifdef TCP_USER_TIMEOUT
1727 srv->tcp_ut = src->tcp_ut;
1728#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001729 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001730 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001731 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01001732 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001733 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001734
Olivier Houchard8da5f982017-08-04 18:35:36 +02001735 if (srv_tmpl)
1736 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001737
1738 srv->check.via_socks4 = src->check.via_socks4;
1739 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001740}
1741
Willy Tarreau198e92a2021-03-05 10:23:32 +01001742/* allocate a server and attach it to the global servers_list. Returns
1743 * the server on success, otherwise NULL.
1744 */
William Lallemand313bfd12018-10-26 14:47:32 +02001745struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001746{
1747 struct server *srv;
1748
1749 srv = calloc(1, sizeof *srv);
1750 if (!srv)
1751 return NULL;
1752
1753 srv->obj_type = OBJ_TYPE_SERVER;
1754 srv->proxy = proxy;
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001755 srv->pendconns = EB_ROOT;
Willy Tarreau198e92a2021-03-05 10:23:32 +01001756 LIST_ADDQ(&servers_list, &srv->global_list);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001757
Emeric Brun52a91d32017-08-31 14:41:55 +02001758 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001759 srv->last_change = now.tv_sec;
1760
Christopher Faulet38290462020-04-21 11:46:40 +02001761 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001762 srv->check.status = HCHK_STATUS_INI;
1763 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001764 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001765 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001766
Christopher Faulet38290462020-04-21 11:46:40 +02001767 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001768 srv->agent.status = HCHK_STATUS_INI;
1769 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001770 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001771 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01001772#if defined(USE_QUIC)
1773 srv->cids = EB_ROOT_UNIQUE;
1774#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001775
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01001776 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01001777#ifdef USE_OPENSSL
1778 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
1779#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01001780
Willy Tarreau975b1552019-06-06 16:25:55 +02001781 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01001782 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02001783 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001784 return srv;
1785}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001786
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001787#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1788static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1789 struct server *srv, struct proxy *proxy)
1790{
1791 int ret;
1792 char *err = NULL;
1793
1794 if (!srv->sni_expr)
1795 return 0;
1796
1797 ret = server_parse_sni_expr(srv, proxy, &err);
1798 if (!ret)
1799 return 0;
1800
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001801 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001802 free(err);
1803
1804 return ret;
1805}
1806#endif
1807
1808/*
1809 * Server initializations finalization.
1810 * Initialize health check, agent check and SNI expression if enabled.
1811 * Must not be called for a default server instance.
1812 */
1813static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1814 struct server *srv, struct proxy *px)
1815{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001816#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001817 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001818#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001819
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001820 if (srv->do_check && srv->trackit) {
1821 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1822 file, linenum);
1823 return ERR_ALERT | ERR_FATAL;
1824 }
1825
1826 if (srv->do_agent && !srv->agent.port) {
1827 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1828 file, linenum, srv->id);
1829 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001830 }
1831
1832#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1833 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1834 return ret;
1835#endif
1836
1837 if (srv->flags & SRV_F_BACKUP)
1838 px->srv_bck++;
1839 else
1840 px->srv_act++;
1841 srv_lb_commit_status(srv);
1842
1843 return 0;
1844}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001845
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001846/*
1847 * Parse as much as possible such a range string argument: low[-high]
1848 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1849 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1850 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1851 * Returns 0 if succeeded, -1 if not.
1852 */
1853static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1854{
1855 char *nb_high_arg;
1856
1857 *nb_high = 0;
1858 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001859 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001860
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001861 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001862 *nb_high_arg++ = '\0';
1863 *nb_high = atoi(nb_high_arg);
1864 }
1865 else {
1866 *nb_high += *nb_low;
1867 *nb_low = 1;
1868 }
1869
1870 if (*nb_low < 0 || *nb_high < *nb_low)
1871 return -1;
1872
1873 return 0;
1874}
1875
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001876static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1877{
1878 chunk_printf(&trash, "%s%d", prefix, nb);
1879 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001880 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001881}
1882
1883/*
1884 * Initialize as much as possible servers from <srv> server template.
1885 * Note that a server template is a special server with
1886 * a few different parameters than a server which has
1887 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001888 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001889 * 'srv' template included.
1890 */
1891static int server_template_init(struct server *srv, struct proxy *px)
1892{
1893 int i;
1894 struct server *newsrv;
1895
1896 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 +02001897 newsrv = new_server(px);
1898 if (!newsrv)
1899 goto err;
1900
Christopher Faulet75bef002020-11-02 22:04:55 +01001901 newsrv->conf.file = strdup(srv->conf.file);
1902 newsrv->conf.line = srv->conf.line;
1903
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001904 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001905 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001906#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1907 if (newsrv->sni_expr) {
1908 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1909 if (!newsrv->ssl_ctx.sni)
1910 goto err;
1911 }
1912#endif
1913 /* Set this new server ID. */
1914 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1915
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001916 /* Linked backwards first. This will be restablished after parsing. */
1917 newsrv->next = px->srv;
1918 px->srv = newsrv;
1919 }
1920 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1921
1922 return i - srv->tmpl_info.nb_low;
1923
1924 err:
1925 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1926 if (newsrv) {
1927#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1928 release_sample_expr(newsrv->ssl_ctx.sni);
1929#endif
1930 free_check(&newsrv->agent);
1931 free_check(&newsrv->check);
Willy Tarreau198e92a2021-03-05 10:23:32 +01001932 LIST_DEL(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001933 }
1934 free(newsrv);
1935 return i - srv->tmpl_info.nb_low;
1936}
1937
Emeric Brund3db3842020-07-21 16:54:36 +02001938int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy,
Willy Tarreaubb8669a2021-02-12 12:15:05 +01001939 const struct proxy *defproxy, int parse_addr, int in_peers_section, int initial_resolve)
Willy Tarreau272adea2014-03-31 10:39:59 +02001940{
1941 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001942 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001943 char *errmsg = NULL;
1944 int err_code = 0;
1945 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001946 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001947
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001948 if (strcmp(args[0], "server") == 0 ||
1949 strcmp(args[0], "peer") == 0 ||
1950 strcmp(args[0], "default-server") == 0 ||
1951 strcmp(args[0], "server-template") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001952 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001953 int defsrv = (*args[0] == 'd');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001954 int srv = !defsrv && (*args[0] == 'p' || strcmp(args[0], "server") == 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001955 int srv_tmpl = !defsrv && !srv;
1956 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001957
1958 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001959 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001960 err_code |= ERR_ALERT | ERR_FATAL;
1961 goto out;
1962 }
Christopher Faulete3bdc812021-01-13 13:14:13 +01001963 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
1964 err_code |= ERR_ALERT | ERR_FATAL;
1965 goto out;
1966 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001967
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001968 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001969 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001970 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001971 if (in_peers_section) {
1972 return 0;
1973 }
1974 else {
1975 /* 'server' line number of argument check. */
1976 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1977 file, linenum, args[0]);
1978 err_code |= ERR_ALERT | ERR_FATAL;
1979 goto out;
1980 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001981 }
1982
1983 err = invalid_char(args[1]);
1984 }
1985 else if (srv_tmpl) {
1986 if (!*args[3]) {
1987 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001988 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 +02001989 file, linenum, args[0]);
1990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992 }
1993
1994 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001995 }
1996
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001997 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001998 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 +02001999 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002000 err_code |= ERR_ALERT | ERR_FATAL;
2001 goto out;
2002 }
2003
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002004 cur_arg = 2;
2005 if (srv_tmpl) {
2006 /* Parse server-template <nb | range> arg. */
2007 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002008 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002009 file, linenum, args[0], args[cur_arg]);
2010 err_code |= ERR_ALERT | ERR_FATAL;
2011 goto out;
2012 }
2013 cur_arg++;
2014 }
2015
Willy Tarreau272adea2014-03-31 10:39:59 +02002016 if (!defsrv) {
2017 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002018 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002019
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002020 newsrv = new_server(curproxy);
2021 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002022 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002023 err_code |= ERR_ALERT | ERR_ABORT;
2024 goto out;
2025 }
2026
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002027 if (srv_tmpl) {
2028 newsrv->tmpl_info.nb_low = tmpl_range_low;
2029 newsrv->tmpl_info.nb_high = tmpl_range_high;
2030 }
2031
Willy Tarreau272adea2014-03-31 10:39:59 +02002032 /* the servers are linked backwards first */
2033 newsrv->next = curproxy->srv;
2034 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002035 newsrv->conf.file = strdup(file);
2036 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002037 /* Note: for a server template, its id is its prefix.
2038 * This is a temporary id which will be used for server allocations to come
2039 * after parsing.
2040 */
2041 if (srv)
2042 newsrv->id = strdup(args[1]);
2043 else
2044 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002045
2046 /* several ways to check the port component :
2047 * - IP => port=+0, relative (IPv4 only)
2048 * - IP: => port=+0, relative
2049 * - IP:N => port=N, absolute
2050 * - IP:+N => port=+N, relative
2051 * - IP:-N => port=-N, relative
2052 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002053 if (!parse_addr)
2054 goto skip_addr;
2055
Willy Tarreau65ec4e32020-09-16 19:17:08 +02002056 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, NULL, NULL,
2057 &errmsg, NULL, &fqdn,
2058 (initial_resolve ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | PA_O_PORT_OFS | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
Willy Tarreau272adea2014-03-31 10:39:59 +02002059 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002060 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002061 err_code |= ERR_ALERT | ERR_FATAL;
2062 goto out;
2063 }
2064
Willy Tarreau272adea2014-03-31 10:39:59 +02002065 if (!port1 || !port2) {
2066 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002067 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002068 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002069
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002070 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002071 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002072 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002073 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002074 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
Emeric Brun08622d32020-12-23 17:41:43 +01002075 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002076 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002077 err_code |= ERR_ALERT | ERR_FATAL;
2078 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002079 }
2080 }
2081 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002082 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002083 file, linenum, newsrv->id);
2084 err_code |= ERR_ALERT | ERR_FATAL;
2085 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002086 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002087 }
2088
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002090 newsrv->svc_port = port;
Thayne McCombs92149f92020-11-20 01:28:26 -07002091 // we don't need to lock the server here, because
2092 // we are in the process of initializing
2093 srv_set_addr_desc(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002094
Olivier Houchard8da5f982017-08-04 18:35:36 +02002095 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002096 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002097 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002098 err_code |= ERR_ALERT | ERR_FATAL;
2099 goto out;
2100 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002101
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002102 cur_arg++;
2103 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002104 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002105 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002106 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002107 } else {
2108 newsrv = &curproxy->defsrv;
2109 cur_arg = 1;
Emeric Brun21fbeed2020-12-23 18:01:04 +01002110 newsrv->resolv_opts.family_prio = AF_INET6;
2111 newsrv->resolv_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002112 }
2113
2114 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002115 if (strcmp(args[cur_arg], "init-addr") == 0) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002116 char *p, *end;
2117 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002118 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002119
2120 newsrv->init_addr_methods = 0;
2121 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2122
2123 for (p = args[cur_arg + 1]; *p; p = end) {
2124 /* cut on next comma */
2125 for (end = p; *end && *end != ','; end++);
2126 if (*end)
2127 *(end++) = 0;
2128
Willy Tarreau4310d362016-11-02 15:05:56 +01002129 memset(&sa, 0, sizeof(sa));
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002130 if (strcmp(p, "libc") == 0) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002131 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2132 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002133 else if (strcmp(p, "last") == 0) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002134 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2135 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002136 else if (strcmp(p, "none") == 0) {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002137 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2138 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002139 else if (str2ip2(p, &sa, 0)) {
2140 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002141 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002142 file, linenum, args[cur_arg], p);
2143 err_code |= ERR_ALERT | ERR_FATAL;
2144 goto out;
2145 }
2146 newsrv->init_addr = sa;
2147 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2148 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002149 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002150 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 +02002151 file, linenum, args[cur_arg], p);
2152 err_code |= ERR_ALERT | ERR_FATAL;
2153 goto out;
2154 }
2155 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002156 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002157 file, linenum, args[cur_arg], p);
2158 err_code |= ERR_ALERT | ERR_FATAL;
2159 goto out;
2160 }
2161 }
2162 cur_arg += 2;
2163 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002164 else if (strcmp(args[cur_arg], "resolvers") == 0) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002165 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002166 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2167 cur_arg += 2;
2168 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002169 else if (strcmp(args[cur_arg], "resolve-opts") == 0) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002170 char *p, *end;
2171
2172 for (p = args[cur_arg + 1]; *p; p = end) {
2173 /* cut on next comma */
2174 for (end = p; *end && *end != ','; end++);
2175 if (*end)
2176 *(end++) = 0;
2177
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002178 if (strcmp(p, "allow-dup-ip") == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002179 newsrv->resolv_opts.accept_duplicate_ip = 1;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002180 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002181 else if (strcmp(p, "ignore-weight") == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002182 newsrv->resolv_opts.ignore_weight = 1;
Daniel Corbettf8716912019-11-17 09:48:56 -05002183 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002184 else if (strcmp(p, "prevent-dup-ip") == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002185 newsrv->resolv_opts.accept_duplicate_ip = 0;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002186 }
2187 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002188 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 +02002189 file, linenum, args[cur_arg], p);
2190 err_code |= ERR_ALERT | ERR_FATAL;
2191 goto out;
2192 }
2193 }
2194
2195 cur_arg += 2;
2196 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002197 else if (strcmp(args[cur_arg], "resolve-prefer") == 0) {
2198 if (strcmp(args[cur_arg + 1], "ipv4") == 0)
Emeric Brun21fbeed2020-12-23 18:01:04 +01002199 newsrv->resolv_opts.family_prio = AF_INET;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002200 else if (strcmp(args[cur_arg + 1], "ipv6") == 0)
Emeric Brun21fbeed2020-12-23 18:01:04 +01002201 newsrv->resolv_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002202 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002203 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002204 file, linenum, args[cur_arg]);
2205 err_code |= ERR_ALERT | ERR_FATAL;
2206 goto out;
2207 }
2208 cur_arg += 2;
2209 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002210 else if (strcmp(args[cur_arg], "resolve-net") == 0) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002211 char *p, *e;
2212 unsigned char mask;
Emeric Brun21fbeed2020-12-23 18:01:04 +01002213 struct resolv_options *opt;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002214
2215 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002216 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002217 file, linenum, args[cur_arg]);
2218 err_code |= ERR_ALERT | ERR_FATAL;
2219 goto out;
2220 }
2221
Emeric Brun21fbeed2020-12-23 18:01:04 +01002222 opt = &newsrv->resolv_opts;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002223
2224 /* Split arguments by comma, and convert it from ipv4 or ipv6
2225 * string network in in_addr or in6_addr.
2226 */
2227 p = args[cur_arg + 1];
2228 e = p;
2229 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002230 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002231 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002232 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002233 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2234 err_code |= ERR_ALERT | ERR_FATAL;
2235 goto out;
2236 }
2237 /* look for end or comma. */
2238 while (*e != ',' && *e != '\0')
2239 e++;
2240 if (*e == ',') {
2241 *e = '\0';
2242 e++;
2243 }
2244 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2245 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2246 /* Try to convert input string from ipv4 or ipv6 network. */
2247 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2248 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2249 &mask)) {
2250 /* Try to convert input string from ipv6 network. */
2251 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2252 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2253 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002254 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002255 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002256 file, linenum, args[cur_arg], p);
2257 err_code |= ERR_ALERT | ERR_FATAL;
2258 goto out;
2259 }
2260 opt->pref_net_nb++;
2261 p = e;
2262 }
2263
2264 cur_arg += 2;
2265 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002266 else if (strcmp(args[cur_arg], "weight") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002267 int w;
2268 w = atol(args[cur_arg + 1]);
2269 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002270 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002271 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2272 err_code |= ERR_ALERT | ERR_FATAL;
2273 goto out;
2274 }
2275 newsrv->uweight = newsrv->iweight = w;
2276 cur_arg += 2;
2277 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002278 else if (strcmp(args[cur_arg], "log-proto") == 0) {
2279 if (strcmp(args[cur_arg + 1], "legacy") == 0)
Emeric Brun97556472020-05-30 01:42:45 +02002280 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002281 else if (strcmp(args[cur_arg + 1], "octet-count") == 0)
Emeric Brun97556472020-05-30 01:42:45 +02002282 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2283 else {
2284 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2285 "'octet-count' but got '%s'\n",
2286 file, linenum, args[cur_arg], args[cur_arg + 1]);
2287 err_code |= ERR_ALERT | ERR_FATAL;
2288 goto out;
2289 }
2290 cur_arg += 2;
2291 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002292 else if (strcmp(args[cur_arg], "minconn") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002293 newsrv->minconn = atol(args[cur_arg + 1]);
2294 cur_arg += 2;
2295 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002296 else if (strcmp(args[cur_arg], "maxconn") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002297 newsrv->maxconn = atol(args[cur_arg + 1]);
2298 cur_arg += 2;
2299 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002300 else if (strcmp(args[cur_arg], "maxqueue") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002301 newsrv->maxqueue = atol(args[cur_arg + 1]);
2302 cur_arg += 2;
2303 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002304 else if (strcmp(args[cur_arg], "slowstart") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002305 /* slowstart is stored in seconds */
2306 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002307
2308 if (err == PARSE_TIME_OVER) {
2309 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2310 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
2313 }
2314 else if (err == PARSE_TIME_UNDER) {
2315 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2316 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2317 err_code |= ERR_ALERT | ERR_FATAL;
2318 goto out;
2319 }
2320 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002321 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002322 file, linenum, *err, newsrv->id);
2323 err_code |= ERR_ALERT | ERR_FATAL;
2324 goto out;
2325 }
2326 newsrv->slowstart = (val + 999) / 1000;
2327 cur_arg += 2;
2328 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002329 else if (strcmp(args[cur_arg], "on-error") == 0) {
2330 if (strcmp(args[cur_arg + 1], "fastinter") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002331 newsrv->onerror = HANA_ONERR_FASTINTER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002332 else if (strcmp(args[cur_arg + 1], "fail-check") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002333 newsrv->onerror = HANA_ONERR_FAILCHK;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002334 else if (strcmp(args[cur_arg + 1], "sudden-death") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002335 newsrv->onerror = HANA_ONERR_SUDDTH;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002336 else if (strcmp(args[cur_arg + 1], "mark-down") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002337 newsrv->onerror = HANA_ONERR_MARKDWN;
2338 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002339 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002340 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2341 file, linenum, args[cur_arg], args[cur_arg + 1]);
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344 }
2345
2346 cur_arg += 2;
2347 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002348 else if (strcmp(args[cur_arg], "on-marked-down") == 0) {
2349 if (strcmp(args[cur_arg + 1], "shutdown-sessions") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2351 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002352 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002353 file, linenum, args[cur_arg], args[cur_arg + 1]);
2354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
2357
2358 cur_arg += 2;
2359 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002360 else if (strcmp(args[cur_arg], "on-marked-up") == 0) {
2361 if (strcmp(args[cur_arg + 1], "shutdown-backup-sessions") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002362 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2363 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002364 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002365 file, linenum, args[cur_arg], args[cur_arg + 1]);
2366 err_code |= ERR_ALERT | ERR_FATAL;
2367 goto out;
2368 }
2369
2370 cur_arg += 2;
2371 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002372 else if (strcmp(args[cur_arg], "error-limit") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002373 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002374 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002375 file, linenum, args[cur_arg]);
2376 err_code |= ERR_ALERT | ERR_FATAL;
2377 goto out;
2378 }
2379
2380 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2381
2382 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002383 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002384 file, linenum, args[cur_arg]);
2385 err_code |= ERR_ALERT | ERR_FATAL;
2386 goto out;
2387 }
2388 cur_arg += 2;
2389 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002390 else if (strcmp(args[cur_arg], "usesrc") == 0) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002391 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002392 file, linenum, "usesrc", "source");
2393 err_code |= ERR_ALERT | ERR_FATAL;
2394 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002395 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002396 else {
2397 static int srv_dumped;
2398 struct srv_kw *kw;
2399 char *err;
2400
2401 kw = srv_find_kw(args[cur_arg]);
2402 if (kw) {
2403 char *err = NULL;
2404 int code;
2405
2406 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002407 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 +02002408 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002409 if (kw->skip != -1)
2410 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002411 err_code |= ERR_ALERT | ERR_FATAL;
2412 goto out;
2413 }
2414
2415 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002416 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002417 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002418 if (kw->skip != -1)
2419 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002420 err_code |= ERR_ALERT;
2421 continue;
2422 }
2423
2424 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2425 err_code |= code;
2426
2427 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002428 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002429 if (code & ERR_FATAL) {
2430 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002431 if (kw->skip != -1)
2432 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002433 goto out;
2434 }
2435 }
2436 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002437 if (kw->skip != -1)
2438 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002439 continue;
2440 }
2441
2442 err = NULL;
2443 if (!srv_dumped) {
2444 srv_dump_kws(&err);
2445 indent_msg(&err, 4);
2446 srv_dumped = 1;
2447 }
2448
Christopher Faulet767a84b2017-11-24 16:50:31 +01002449 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002450 file, linenum, args[0], args[1], args[cur_arg],
2451 err ? " Registered keywords :" : "", err ? err : "");
2452 free(err);
2453
2454 err_code |= ERR_ALERT | ERR_FATAL;
2455 goto out;
2456 }
2457 }
2458
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002459 if (!defsrv)
2460 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2461 if (err_code & ERR_FATAL)
2462 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002463 if (srv_tmpl)
2464 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002465 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002466 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002467 return 0;
2468
2469 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002470 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002471 free(errmsg);
2472 return err_code;
2473}
2474
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002475/* Returns a pointer to the first server matching either id <id>.
2476 * NULL is returned if no match is found.
2477 * the lookup is performed in the backend <bk>
2478 */
2479struct server *server_find_by_id(struct proxy *bk, int id)
2480{
2481 struct eb32_node *eb32;
2482 struct server *curserver;
2483
2484 if (!bk || (id ==0))
2485 return NULL;
2486
2487 /* <bk> has no backend capabilities, so it can't have a server */
2488 if (!(bk->cap & PR_CAP_BE))
2489 return NULL;
2490
2491 curserver = NULL;
2492
2493 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2494 if (eb32)
2495 curserver = container_of(eb32, struct server, conf.id);
2496
2497 return curserver;
2498}
2499
2500/* Returns a pointer to the first server matching either name <name>, or id
2501 * if <name> starts with a '#'. NULL is returned if no match is found.
2502 * the lookup is performed in the backend <bk>
2503 */
2504struct server *server_find_by_name(struct proxy *bk, const char *name)
2505{
2506 struct server *curserver;
2507
2508 if (!bk || !name)
2509 return NULL;
2510
2511 /* <bk> has no backend capabilities, so it can't have a server */
2512 if (!(bk->cap & PR_CAP_BE))
2513 return NULL;
2514
2515 curserver = NULL;
2516 if (*name == '#') {
2517 curserver = server_find_by_id(bk, atoi(name + 1));
2518 if (curserver)
2519 return curserver;
2520 }
2521 else {
2522 curserver = bk->srv;
2523
2524 while (curserver && (strcmp(curserver->id, name) != 0))
2525 curserver = curserver->next;
2526
2527 if (curserver)
2528 return curserver;
2529 }
2530
2531 return NULL;
2532}
2533
2534struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2535{
2536 struct server *byname;
2537 struct server *byid;
2538
2539 if (!name && !id)
2540 return NULL;
2541
2542 if (diff)
2543 *diff = 0;
2544
2545 byname = byid = NULL;
2546
2547 if (name) {
2548 byname = server_find_by_name(bk, name);
2549 if (byname && (!id || byname->puid == id))
2550 return byname;
2551 }
2552
2553 /* remaining possibilities :
2554 * - name not set
2555 * - name set but not found
2556 * - name found but ID doesn't match
2557 */
2558 if (id) {
2559 byid = server_find_by_id(bk, id);
2560 if (byid) {
2561 if (byname) {
2562 /* use id only if forced by configuration */
2563 if (byid->flags & SRV_F_FORCED_ID) {
2564 if (diff)
2565 *diff |= 2;
2566 return byid;
2567 }
2568 else {
2569 if (diff)
2570 *diff |= 1;
2571 return byname;
2572 }
2573 }
2574
2575 /* remaining possibilities:
2576 * - name not set
2577 * - name set but not found
2578 */
2579 if (name && diff)
2580 *diff |= 2;
2581 return byid;
2582 }
2583
2584 /* id bot found */
2585 if (byname) {
2586 if (diff)
2587 *diff |= 1;
2588 return byname;
2589 }
2590 }
2591
2592 return NULL;
2593}
2594
Simon Horman7d09b9a2013-02-12 10:45:51 +09002595/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002596 * update a server's current IP address.
2597 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2598 * ip is in network format.
2599 * updater is a string which contains an information about the requester of the update.
2600 * updater is used if not NULL.
2601 *
2602 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02002603 *
2604 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02002605 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002606int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002607{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02002608 /* save the new IP family & address if necessary */
2609 switch (ip_sin_family) {
2610 case AF_INET:
2611 if (s->addr.ss_family == ip_sin_family &&
2612 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
2613 return 0;
2614 break;
2615 case AF_INET6:
2616 if (s->addr.ss_family == ip_sin_family &&
2617 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
2618 return 0;
2619 break;
2620 };
2621
Baptiste Assmann14e40142015-04-14 01:13:07 +02002622 /* generates a log line and a warning on stderr */
2623 if (1) {
2624 /* book enough space for both IPv4 and IPv6 */
2625 char oldip[INET6_ADDRSTRLEN];
2626 char newip[INET6_ADDRSTRLEN];
2627
2628 memset(oldip, '\0', INET6_ADDRSTRLEN);
2629 memset(newip, '\0', INET6_ADDRSTRLEN);
2630
2631 /* copy old IP address in a string */
2632 switch (s->addr.ss_family) {
2633 case AF_INET:
2634 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2635 break;
2636 case AF_INET6:
2637 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2638 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02002639 default:
2640 strcpy(oldip, "(none)");
2641 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02002642 };
2643
2644 /* copy new IP address in a string */
2645 switch (ip_sin_family) {
2646 case AF_INET:
2647 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2648 break;
2649 case AF_INET6:
2650 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2651 break;
2652 };
2653
2654 /* save log line into a buffer */
2655 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2656 s->proxy->id, s->id, oldip, newip, updater);
2657
2658 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002659 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002660
2661 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002662 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002663 }
2664
2665 /* save the new IP family */
2666 s->addr.ss_family = ip_sin_family;
2667 /* save the new IP address */
2668 switch (ip_sin_family) {
2669 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002670 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002671 break;
2672 case AF_INET6:
2673 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2674 break;
2675 };
Olivier Houchard4e694042017-03-14 20:01:29 +01002676 srv_set_dyncookie(s);
Thayne McCombs92149f92020-11-20 01:28:26 -07002677 srv_set_addr_desc(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002678
2679 return 0;
2680}
2681
William Dauchy7cabc062021-02-11 22:51:24 +01002682/* update agent health check address and port
2683 * addr can be ip4/ip6 or a hostname
2684 * if one error occurs, don't apply anything
2685 * must be called with the server lock held.
2686 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002687const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01002688{
2689 struct sockaddr_storage sk;
2690 struct buffer *msg;
2691 int new_port;
2692
2693 msg = get_trash_chunk();
2694 chunk_reset(msg);
2695
2696 if (!(s->agent.state & CHK_ST_ENABLED)) {
2697 chunk_strcat(msg, "agent checks are not enabled on this server");
2698 goto out;
2699 }
2700 if (addr) {
2701 memset(&sk, 0, sizeof(struct sockaddr_storage));
2702 if (str2ip(addr, &sk) == NULL) {
2703 chunk_appendf(msg, "invalid addr '%s'", addr);
2704 goto out;
2705 }
2706 }
2707 if (port) {
2708 if (strl2irc(port, strlen(port), &new_port) != 0) {
2709 chunk_appendf(msg, "provided port is not an integer");
2710 goto out;
2711 }
2712 if (new_port < 0 || new_port > 65535) {
2713 chunk_appendf(msg, "provided port is invalid");
2714 goto out;
2715 }
2716 }
2717out:
2718 if (msg->data)
2719 return msg->area;
2720 else {
2721 if (addr)
2722 set_srv_agent_addr(s, &sk);
2723 if (port)
2724 set_srv_agent_port(s, new_port);
2725 }
2726 return NULL;
2727}
2728
William Dauchyb456e1f2021-02-11 22:51:23 +01002729/* update server health check address and port
2730 * addr must be ip4 or ip6, it won't be resolved
2731 * if one error occurs, don't apply anything
2732 * must be called with the server lock held.
2733 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002734const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01002735{
2736 struct sockaddr_storage sk;
2737 struct buffer *msg;
2738 int new_port;
2739
2740 msg = get_trash_chunk();
2741 chunk_reset(msg);
2742
2743 if (!(s->check.state & CHK_ST_ENABLED)) {
2744 chunk_strcat(msg, "health checks are not enabled on this server");
2745 goto out;
2746 }
2747 if (addr) {
2748 memset(&sk, 0, sizeof(struct sockaddr_storage));
2749 if (str2ip2(addr, &sk, 0) == NULL) {
2750 chunk_appendf(msg, "invalid addr '%s'", addr);
2751 goto out;
2752 }
2753 }
2754 if (port) {
2755 if (strl2irc(port, strlen(port), &new_port) != 0) {
2756 chunk_appendf(msg, "provided port is not an integer");
2757 goto out;
2758 }
2759 if (new_port < 0 || new_port > 65535) {
2760 chunk_appendf(msg, "provided port is invalid");
2761 goto out;
2762 }
2763 /* prevent the update of port to 0 if MAPPORTS are in use */
2764 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
2765 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
2766 goto out;
2767 }
2768 }
2769out:
2770 if (msg->data)
2771 return msg->area;
2772 else {
2773 if (addr)
2774 s->check.addr = sk;
2775 if (port)
2776 s->check.port = new_port;
2777 }
2778 return NULL;
2779}
2780
Baptiste Assmann14e40142015-04-14 01:13:07 +02002781/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002782 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2783 *
2784 * Caller can pass its name through <updater> to get it integrated in the response message
2785 * returned by the function.
2786 *
2787 * The function first does the following, in that order:
2788 * - validates the new addr and/or port
2789 * - checks if an update is required (new IP or port is different than current ones)
2790 * - checks the update is allowed:
2791 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2792 * - allow all changes if no CHECKS are configured
2793 * - if CHECK is configured:
2794 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2795 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2796 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02002797 *
2798 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002799 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002800const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002801{
2802 struct sockaddr_storage sa;
2803 int ret, port_change_required;
2804 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00002805 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02002806 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01002807 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002808
2809 msg = get_trash_chunk();
2810 chunk_reset(msg);
2811
2812 if (addr) {
2813 memset(&sa, 0, sizeof(struct sockaddr_storage));
2814 if (str2ip2(addr, &sa, 0) == NULL) {
2815 chunk_printf(msg, "Invalid addr '%s'", addr);
2816 goto out;
2817 }
2818
2819 /* changes are allowed on AF_INET* families only */
2820 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2821 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2822 goto out;
2823 }
2824
2825 /* collecting data currently setup */
2826 memset(current_addr, '\0', sizeof(current_addr));
2827 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2828 /* changes are allowed on AF_INET* families only */
2829 if ((ret != AF_INET) && (ret != AF_INET6)) {
2830 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2831 goto out;
2832 }
2833
2834 /* applying ADDR changes if required and allowed
2835 * ipcmp returns 0 when both ADDR are the same
2836 */
2837 if (ipcmp(&s->addr, &sa) == 0) {
2838 chunk_appendf(msg, "no need to change the addr");
2839 goto port;
2840 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002841 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01002842 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002843
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002844 /* update report for caller */
2845 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2846 }
2847
2848 port:
2849 if (port) {
2850 char sign = '\0';
2851 char *endptr;
2852
2853 if (addr)
2854 chunk_appendf(msg, ", ");
2855
2856 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01002857 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002858
2859 /* check if PORT change is required */
2860 port_change_required = 0;
2861
2862 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04002863 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002864 new_port = strtol(port, &endptr, 10);
2865 if ((errno != 0) || (port == endptr)) {
2866 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2867 goto out;
2868 }
2869
2870 /* check if caller triggers a port mapped or offset */
2871 if (sign == '-' || (sign == '+')) {
2872 /* check if server currently uses port map */
2873 if (!(s->flags & SRV_F_MAPPORTS)) {
2874 /* switch from fixed port to port map mandatorily triggers
2875 * a port change */
2876 port_change_required = 1;
2877 /* check is configured
2878 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2879 * prevent PORT change if check doesn't have it's dedicated port while switching
2880 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01002881 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002882 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.");
2883 goto out;
2884 }
2885 }
2886 /* we're already using port maps */
2887 else {
2888 port_change_required = current_port != new_port;
2889 }
2890 }
2891 /* fixed port */
2892 else {
2893 port_change_required = current_port != new_port;
2894 }
2895
2896 /* applying PORT changes if required and update response message */
2897 if (port_change_required) {
2898 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01002899 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01002900 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002901
2902 /* prepare message */
2903 chunk_appendf(msg, "port changed from '");
2904 if (s->flags & SRV_F_MAPPORTS)
2905 chunk_appendf(msg, "+");
2906 chunk_appendf(msg, "%d' to '", current_port);
2907
2908 if (sign == '-') {
2909 s->flags |= SRV_F_MAPPORTS;
2910 chunk_appendf(msg, "%c", sign);
2911 /* just use for result output */
2912 new_port = -new_port;
2913 }
2914 else if (sign == '+') {
2915 s->flags |= SRV_F_MAPPORTS;
2916 chunk_appendf(msg, "%c", sign);
2917 }
2918 else {
2919 s->flags &= ~SRV_F_MAPPORTS;
2920 }
2921
2922 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002923 }
2924 else {
2925 chunk_appendf(msg, "no need to change the port");
2926 }
2927 }
2928
2929out:
William Dauchy6318d332020-05-02 21:52:36 +02002930 if (changed) {
2931 /* force connection cleanup on the given server */
2932 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01002933 srv_set_dyncookie(s);
Thayne McCombs92149f92020-11-20 01:28:26 -07002934 srv_set_addr_desc(s);
William Dauchy6318d332020-05-02 21:52:36 +02002935 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002936 if (updater)
2937 chunk_appendf(msg, " by '%s'", updater);
2938 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002939 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002940}
2941
2942
2943/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002944 * update server status based on result of name resolution
2945 * returns:
2946 * 0 if server status is updated
2947 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02002948 *
2949 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002950 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002951int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002952{
Emeric Brun750fe792020-12-23 16:51:12 +01002953 struct resolvers *resolvers = s->resolvers;
Emeric Brun08622d32020-12-23 17:41:43 +01002954 struct resolv_resolution *resolution = s->resolv_requester->resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002955 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002956
Jerome Magnin012261a2020-07-28 13:38:22 +02002957 /* If resolution is NULL we're dealing with SRV records Additional records */
2958 if (resolution == NULL) {
Baptiste Assmann87138c32020-08-04 10:57:21 +02002959 /* since this server has an IP, it can go back in production */
2960 if (has_no_ip == 0) {
2961 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2962 return 1;
2963 }
2964
Jerome Magnin012261a2020-07-28 13:38:22 +02002965 if (s->next_admin & SRV_ADMF_RMAINT)
2966 return 1;
2967
2968 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
2969 return 0;
2970 }
2971
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002972 switch (resolution->status) {
2973 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002974 /* status when HAProxy has just (re)started.
2975 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002976 break;
2977
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002978 case RSLV_STATUS_VALID:
2979 /*
2980 * resume health checks
2981 * server will be turned back on if health check is safe
2982 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002983 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02002984 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002985 return 1;
2986 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
2987 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02002988 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002989 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002990
Emeric Brun52a91d32017-08-31 14:41:55 +02002991 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002992 return 1;
2993 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2994 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
2995 s->proxy->id, s->id);
2996
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002997 ha_warning("%s.\n", trash.area);
2998 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002999 return 0;
3000
3001 case RSLV_STATUS_NX:
3002 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003003 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3004 if (!tick_is_expired(exp, now_ms))
3005 break;
3006
3007 if (s->next_admin & SRV_ADMF_RMAINT)
3008 return 1;
3009 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3010 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003011
3012 case RSLV_STATUS_TIMEOUT:
3013 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003014 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3015 if (!tick_is_expired(exp, now_ms))
3016 break;
3017
3018 if (s->next_admin & SRV_ADMF_RMAINT)
3019 return 1;
3020 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3021 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003022
3023 case RSLV_STATUS_REFUSED:
3024 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003025 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3026 if (!tick_is_expired(exp, now_ms))
3027 break;
3028
3029 if (s->next_admin & SRV_ADMF_RMAINT)
3030 return 1;
3031 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3032 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003033
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003034 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003035 /* stop server if resolution failed for a long enough period */
3036 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3037 if (!tick_is_expired(exp, now_ms))
3038 break;
3039
3040 if (s->next_admin & SRV_ADMF_RMAINT)
3041 return 1;
3042 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3043 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003044 }
3045
3046 return 1;
3047}
3048
3049/*
3050 * Server Name Resolution valid response callback
3051 * It expects:
3052 * - <nameserver>: the name server which answered the valid response
3053 * - <response>: buffer containing a valid DNS response
3054 * - <response_len>: size of <response>
3055 * It performs the following actions:
3056 * - ignore response if current ip found and server family not met
3057 * - update with first new ip found if family is met and current IP is not found
3058 * returns:
3059 * 0 on error
3060 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003061 *
3062 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003063 */
Emeric Brun08622d32020-12-23 17:41:43 +01003064int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003065{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003066 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003067 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003068 void *serverip, *firstip;
3069 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003070 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003071 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003072 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003073
Christopher Faulet67957bd2017-09-27 11:00:59 +02003074 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003075 if (!s)
3076 return 1;
3077
Emeric Brun08622d32020-12-23 17:41:43 +01003078 resolution = s->resolv_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003079
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003080 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003081 firstip = NULL; /* pointer to the first valid response found */
3082 /* it will be used as the new IP if a change is required */
3083 firstip_sin_family = AF_UNSPEC;
3084 serverip = NULL; /* current server IP address */
3085
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003086 /* initializing server IP pointer */
3087 server_sin_family = s->addr.ss_family;
3088 switch (server_sin_family) {
3089 case AF_INET:
3090 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3091 break;
3092
3093 case AF_INET6:
3094 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3095 break;
3096
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003097 case AF_UNSPEC:
3098 break;
3099
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003100 default:
3101 goto invalid;
3102 }
3103
Emeric Brund30e9a12020-12-23 18:49:16 +01003104 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3105 serverip, server_sin_family, &firstip,
3106 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003107
3108 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003109 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003110 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003111
Emeric Brun456de772020-12-23 18:17:31 +01003112 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003113 goto save_ip;
3114
Emeric Brun456de772020-12-23 18:17:31 +01003115 case RSLV_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003116 goto invalid;
3117
Emeric Brun456de772020-12-23 18:17:31 +01003118 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003119 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003120 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003121
Emeric Brun456de772020-12-23 18:17:31 +01003122 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003123 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003124 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003125 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003126
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003127 default:
3128 goto invalid;
3129
3130 }
3131
3132 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003133 if (counters) {
3134 counters->update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003135 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003136 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003137 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003138 else
3139 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003140 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003141
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003142 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003143 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003144 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003145
3146 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003147 if (counters) {
3148 counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003149 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003150 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003151 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003152 return 0;
3153}
3154
3155/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003156 * SRV record error management callback
3157 * returns:
3158 * 0 on error
3159 * 1 when no error or safe ignore
3160 *
3161 * Grabs the server's lock.
3162 */
3163int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3164{
3165 struct server *s;
3166 struct resolv_srvrq *srvrq;
3167 struct resolv_resolution *res;
3168 struct resolvers *resolvers;
3169 int exp;
3170
3171 /* SRV records */
3172 srvrq = objt_resolv_srvrq(requester->owner);
3173 if (!srvrq)
3174 return 1;
3175
3176 resolvers = srvrq->resolvers;
3177 res = requester->resolution;
3178
3179 switch (res->status) {
3180
3181 case RSLV_STATUS_NX:
3182 /* stop server if resolution is NX for a long enough period */
3183 exp = tick_add(res->last_valid, resolvers->hold.nx);
3184 if (!tick_is_expired(exp, now_ms))
3185 return 1;
3186 break;
3187
3188 case RSLV_STATUS_TIMEOUT:
3189 /* stop server if resolution is TIMEOUT for a long enough period */
3190 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3191 if (!tick_is_expired(exp, now_ms))
3192 return 1;
3193 break;
3194
3195 case RSLV_STATUS_REFUSED:
3196 /* stop server if resolution is REFUSED for a long enough period */
3197 exp = tick_add(res->last_valid, resolvers->hold.refused);
3198 if (!tick_is_expired(exp, now_ms))
3199 return 1;
3200 break;
3201
3202 default:
3203 /* stop server if resolution failed for a long enough period */
3204 exp = tick_add(res->last_valid, resolvers->hold.other);
3205 if (!tick_is_expired(exp, now_ms))
3206 return 1;
3207 }
3208
3209 /* Remove any associated server */
3210 for (s = srvrq->proxy->srv; s != NULL; s = s->next) {
3211 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
3212 if (s->srvrq == srvrq) {
3213 snr_update_srv_status(s, 1);
3214 free(s->hostname);
3215 free(s->hostname_dn);
3216 s->hostname = NULL;
3217 s->hostname_dn = NULL;
3218 s->hostname_dn_len = 0;
Christopher Faulet52d4d302021-02-23 12:24:09 +01003219 memset(&s->addr, 0, sizeof(s->addr));
3220 s->svc_port = 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003221 resolv_unlink_resolution(s->resolv_requester);
3222 }
3223 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
3224 }
3225
3226 return 1;
3227}
3228
3229/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003230 * Server Name Resolution error management callback
3231 * returns:
3232 * 0 on error
3233 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003234 *
3235 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003236 */
Emeric Brun08622d32020-12-23 17:41:43 +01003237int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003238{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003239 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003240
Christopher Faulet67957bd2017-09-27 11:00:59 +02003241 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003242 if (!s)
3243 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003244 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003245 snr_update_srv_status(s, 0);
Christopher Fauletd127ffa2021-02-23 12:33:17 +01003246 memset(&s->addr, 0, sizeof(s->addr));
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003247 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003248 return 1;
3249}
3250
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003251/*
3252 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003253 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003254 * It returns a pointer to the first server found or NULL if <ip> is not yet
3255 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003256 *
3257 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003258 */
3259struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3260{
3261 struct server *tmpsrv;
3262 struct proxy *be;
3263
3264 if (!srv)
3265 return NULL;
3266
3267 be = srv->proxy;
3268 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003269 /* we found the current server is the same, ignore it */
3270 if (srv == tmpsrv)
3271 continue;
3272
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003273 /* We want to compare the IP in the record with the IP of the servers in the
3274 * same backend, only if:
3275 * * DNS resolution is enabled on the server
3276 * * the hostname used for the resolution by our server is the same than the
3277 * one used for the server found in the backend
3278 * * the server found in the backend is not our current server
3279 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003280 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003281 if ((tmpsrv->hostname_dn == NULL) ||
3282 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3283 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003284 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003285 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003286 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003287 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003288
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003289 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003290 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003291 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003292 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003293 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003294
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003295 /* At this point, we have 2 different servers using the same DNS hostname
3296 * for their respective resolution.
3297 */
3298 if (*ip_family == tmpsrv->addr.ss_family &&
3299 ((tmpsrv->addr.ss_family == AF_INET &&
3300 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3301 (tmpsrv->addr.ss_family == AF_INET6 &&
3302 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003303 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003304 return tmpsrv;
3305 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003306 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003307 }
3308
Emeric Brune9fd6b52017-11-02 17:20:39 +01003309
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003310 return NULL;
3311}
3312
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003313/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3314 * resolver. This is suited for initial address configuration. Returns 0 on
3315 * success otherwise a non-zero error code. In case of error, *err_code, if
3316 * not NULL, is filled up.
3317 */
3318int srv_set_addr_via_libc(struct server *srv, int *err_code)
3319{
3320 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003321 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003322 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003323 return 1;
3324 }
3325 return 0;
3326}
3327
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003328/* Set the server's FDQN (->hostname) from <hostname>.
3329 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003330 *
3331 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003332 */
Emeric Brun08622d32020-12-23 17:41:43 +01003333int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003334{
Emeric Brun08622d32020-12-23 17:41:43 +01003335 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003336 char *hostname_dn;
3337 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003338
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003339 /* Note that the server lock is already held. */
3340 if (!srv->resolvers)
3341 return -1;
3342
Emeric Brun08622d32020-12-23 17:41:43 +01003343 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003344 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003345 /* run time DNS resolution was not active for this server
3346 * and we can't enable it at run time for now.
3347 */
Emeric Brun08622d32020-12-23 17:41:43 +01003348 if (!srv->resolv_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003349 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003350
3351 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003352 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003353 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01003354 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
3355 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003356 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003357 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003358
Emeric Brun08622d32020-12-23 17:41:43 +01003359 resolution = srv->resolv_requester->resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003360 if (resolution &&
3361 resolution->hostname_dn &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003362 strcmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003363 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003364
Emeric Brund30e9a12020-12-23 18:49:16 +01003365 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003366
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003367 free(srv->hostname);
3368 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003369 srv->hostname = strdup(hostname);
3370 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003371 srv->hostname_dn_len = hostname_dn_len;
3372 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003373 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003374
Baptiste Assmann13a92322019-06-07 09:40:55 +02003375 if (srv->flags & SRV_F_NO_RESOLUTION)
3376 goto end;
3377
Emeric Brund30e9a12020-12-23 18:49:16 +01003378 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003379 goto err;
3380
3381 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003382 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003383 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003384 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003385
3386 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003387 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003388 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003389 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003390}
3391
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003392/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3393 * from the state file. This is suited for initial address configuration.
3394 * Returns 0 on success otherwise a non-zero error code. In case of error,
3395 * *err_code, if not NULL, is filled up.
3396 */
3397static int srv_apply_lastaddr(struct server *srv, int *err_code)
3398{
3399 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3400 if (err_code)
3401 *err_code |= ERR_WARN;
3402 return 1;
3403 }
3404 return 0;
3405}
3406
Willy Tarreau25e51522016-11-04 15:10:17 +01003407/* returns 0 if no error, otherwise a combination of ERR_* flags */
3408static int srv_iterate_initaddr(struct server *srv)
3409{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003410 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003411 int return_code = 0;
3412 int err_code;
3413 unsigned int methods;
3414
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003415 /* If no addr and no hostname set, get the name from the DNS SRV request */
3416 if (!name && srv->srvrq)
3417 name = srv->srvrq->name;
3418
Willy Tarreau25e51522016-11-04 15:10:17 +01003419 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003420 if (!methods) {
3421 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003422 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3423 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003424 if (srv->resolvers_id) {
3425 /* dns resolution is configured, add "none" to not fail on startup */
3426 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3427 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003428 }
3429
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003430 /* "-dr" : always append "none" so that server addresses resolution
3431 * failures are silently ignored, this is convenient to validate some
3432 * configs out of their environment.
3433 */
3434 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3435 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3436
Willy Tarreau25e51522016-11-04 15:10:17 +01003437 while (methods) {
3438 err_code = 0;
3439 switch (srv_get_next_initaddr(&methods)) {
3440 case SRV_IADDR_LAST:
3441 if (!srv->lastaddr)
3442 continue;
3443 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003444 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003445 return_code |= err_code;
3446 break;
3447
3448 case SRV_IADDR_LIBC:
3449 if (!srv->hostname)
3450 continue;
3451 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003452 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003453 return_code |= err_code;
3454 break;
3455
Willy Tarreau37ebe122016-11-04 15:17:58 +01003456 case SRV_IADDR_NONE:
3457 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003458 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003459 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003460 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003461 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003462 return return_code;
3463
Willy Tarreau4310d362016-11-02 15:05:56 +01003464 case SRV_IADDR_IP:
3465 ipcpy(&srv->init_addr, &srv->addr);
3466 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003467 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003468 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau4310d362016-11-02 15:05:56 +01003469 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003470 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003471
Willy Tarreau25e51522016-11-04 15:10:17 +01003472 default: /* unhandled method */
3473 break;
3474 }
3475 }
3476
3477 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003478 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003479 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau25e51522016-11-04 15:10:17 +01003480 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003481 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003482 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003483 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003484 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003485
3486 return_code |= ERR_ALERT | ERR_FATAL;
3487 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003488out:
3489 srv_set_dyncookie(srv);
Thayne McCombs92149f92020-11-20 01:28:26 -07003490 srv_set_addr_desc(srv);
Olivier Houchard4e694042017-03-14 20:01:29 +01003491 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003492}
3493
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003494/*
3495 * This function parses all backends and all servers within each backend
3496 * and performs servers' addr resolution based on information provided by:
3497 * - configuration file
3498 * - server-state file (states provided by an 'old' haproxy process)
3499 *
3500 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3501 */
3502int srv_init_addr(void)
3503{
3504 struct proxy *curproxy;
3505 int return_code = 0;
3506
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003507 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003508 while (curproxy) {
3509 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003510
3511 /* servers are in backend only */
Amaury Denoyellee3c41922021-01-06 14:28:50 +01003512 if (!(curproxy->cap & PR_CAP_BE) || curproxy->disabled)
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003513 goto srv_init_addr_next;
3514
Willy Tarreau25e51522016-11-04 15:10:17 +01003515 for (srv = curproxy->srv; srv; srv = srv->next)
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003516 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003517 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003518
3519 srv_init_addr_next:
3520 curproxy = curproxy->next;
3521 }
3522
3523 return return_code;
3524}
3525
Willy Tarreau46b7f532018-08-21 11:54:26 +02003526/*
3527 * Must be called with the server lock held.
3528 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003529const char *srv_update_fqdn(struct server *server, const char *fqdn, const char *updater, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003530{
3531
Willy Tarreau83061a82018-07-13 11:56:34 +02003532 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003533
3534 msg = get_trash_chunk();
3535 chunk_reset(msg);
3536
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003537 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003538 chunk_appendf(msg, "no need to change the FDQN");
3539 goto out;
3540 }
3541
3542 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3543 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3544 goto out;
3545 }
3546
3547 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3548 server->proxy->id, server->id, server->hostname, fqdn);
3549
Emeric Brun08622d32020-12-23 17:41:43 +01003550 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003551 chunk_reset(msg);
3552 chunk_appendf(msg, "could not update %s/%s FQDN",
3553 server->proxy->id, server->id);
3554 goto out;
3555 }
3556
3557 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003558 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003559
3560 out:
3561 if (updater)
3562 chunk_appendf(msg, " by '%s'", updater);
3563 chunk_appendf(msg, "\n");
3564
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003565 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003566}
3567
3568
Willy Tarreau21b069d2016-11-23 17:15:08 +01003569/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3570 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003571 * 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 +01003572 * used for CLI commands requiring a server name.
3573 * Important: the <arg> is modified to remove the '/'.
3574 */
3575struct server *cli_find_server(struct appctx *appctx, char *arg)
3576{
3577 struct proxy *px;
3578 struct server *sv;
3579 char *line;
3580
3581 /* split "backend/server" and make <line> point to server */
3582 for (line = arg; *line; line++)
3583 if (*line == '/') {
3584 *line++ = '\0';
3585 break;
3586 }
3587
3588 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003589 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003590 return NULL;
3591 }
3592
3593 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003594 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003595 return NULL;
3596 }
3597
Willy Tarreauc3914d42020-09-24 08:39:22 +02003598 if (px->disabled) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003599 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003600 return NULL;
3601 }
3602
3603 return sv;
3604}
3605
William Lallemand222baf22016-11-19 02:00:33 +01003606
Willy Tarreau46b7f532018-08-21 11:54:26 +02003607/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003608static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01003609{
3610 struct server *sv;
3611 const char *warning;
3612
3613 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3614 return 1;
3615
3616 sv = cli_find_server(appctx, args[2]);
3617 if (!sv)
3618 return 1;
3619
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003620 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02003621
William Lallemand222baf22016-11-19 02:00:33 +01003622 if (strcmp(args[3], "weight") == 0) {
3623 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003624 if (warning)
3625 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003626 }
3627 else if (strcmp(args[3], "state") == 0) {
3628 if (strcmp(args[4], "ready") == 0)
3629 srv_adm_set_ready(sv);
3630 else if (strcmp(args[4], "drain") == 0)
3631 srv_adm_set_drain(sv);
3632 else if (strcmp(args[4], "maint") == 0)
3633 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02003634 else
3635 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003636 }
3637 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003638 if (sv->track)
3639 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003640 else if (strcmp(args[4], "up") == 0) {
3641 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003642 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003643 }
3644 else if (strcmp(args[4], "stopping") == 0) {
3645 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003646 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003647 }
3648 else if (strcmp(args[4], "down") == 0) {
3649 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003650 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003651 }
Willy Tarreau9d008692019-08-09 11:21:01 +02003652 else
3653 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003654 }
3655 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003656 if (!(sv->agent.state & CHK_ST_ENABLED))
3657 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003658 else if (strcmp(args[4], "up") == 0) {
3659 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003660 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003661 }
3662 else if (strcmp(args[4], "down") == 0) {
3663 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003664 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003665 }
Willy Tarreau9d008692019-08-09 11:21:01 +02003666 else
3667 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003668 }
Misiek2da082d2017-01-09 09:40:42 +01003669 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01003670 char *addr = NULL;
3671 char *port = NULL;
3672 if (strlen(args[4]) == 0) {
3673 cli_err(appctx, "set server <b>/<s> agent-addr requires"
3674 " an address and optionally a port.\n");
3675 goto out_unlock;
3676 }
3677 addr = args[4];
3678 if (strcmp(args[5], "port") == 0)
3679 port = args[6];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003680 warning = srv_update_agent_addr_port(sv, addr, port);
William Dauchy7cabc062021-02-11 22:51:24 +01003681 if (warning)
3682 cli_msg(appctx, LOG_WARNING, warning);
3683 }
3684 else if (strcmp(args[3], "agent-port") == 0) {
3685 char *port = NULL;
3686 if (strlen(args[4]) == 0) {
3687 cli_err(appctx, "set server <b>/<s> agent-port requires"
3688 " a port.\n");
3689 goto out_unlock;
3690 }
3691 port = args[4];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003692 warning = srv_update_agent_addr_port(sv, NULL, port);
William Dauchy7cabc062021-02-11 22:51:24 +01003693 if (warning)
3694 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01003695 }
3696 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003697 if (!(sv->agent.state & CHK_ST_ENABLED))
3698 cli_err(appctx, "agent checks are not enabled on this server.\n");
3699 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02003700 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02003701 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01003702 }
3703 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003704 else if (strcmp(args[3], "check-addr") == 0) {
3705 char *addr = NULL;
3706 char *port = NULL;
3707 if (strlen(args[4]) == 0) {
3708 cli_err(appctx, "set server <b>/<s> check-addr requires"
3709 " an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003710 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01003711 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003712 addr = args[4];
3713 if (strcmp(args[5], "port") == 0)
3714 port = args[6];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003715 warning = srv_update_check_addr_port(sv, addr, port);
William Dauchyb456e1f2021-02-11 22:51:23 +01003716 if (warning)
3717 cli_msg(appctx, LOG_WARNING, warning);
3718 }
3719 else if (strcmp(args[3], "check-port") == 0) {
3720 char *port = NULL;
3721 if (strlen(args[4]) == 0) {
3722 cli_err(appctx, "set server <b>/<s> check-port requires"
3723 " a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003724 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01003725 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003726 port = args[4];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003727 warning = srv_update_check_addr_port(sv, NULL, port);
William Dauchyb456e1f2021-02-11 22:51:23 +01003728 if (warning)
3729 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003730 }
3731 else if (strcmp(args[3], "addr") == 0) {
3732 char *addr = NULL;
3733 char *port = NULL;
3734 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003735 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003736 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01003737 }
3738 else {
3739 addr = args[4];
3740 }
3741 if (strcmp(args[5], "port") == 0) {
3742 port = args[6];
3743 }
Christopher Faulet69beaa92021-02-16 12:07:47 +01003744 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02003745 if (warning)
3746 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003747 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3748 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003749 else if (strcmp(args[3], "fqdn") == 0) {
3750 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003751 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003752 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003753 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02003754 /* ensure runtime resolver will process this new fqdn */
3755 if (sv->flags & SRV_F_NO_RESOLUTION) {
3756 sv->flags &= ~SRV_F_NO_RESOLUTION;
3757 }
Christopher Faulet69beaa92021-02-16 12:07:47 +01003758 warning = srv_update_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02003759 if (warning)
3760 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003761 }
William Dauchyf6370442020-11-14 19:25:33 +01003762 else if (strcmp(args[3], "ssl") == 0) {
3763#ifdef USE_OPENSSL
3764 if (sv->ssl_ctx.ctx == NULL) {
3765 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
3766 " default-server should define ssl settings\n");
3767 goto out_unlock;
3768 } else if (strcmp(args[4], "on") == 0) {
3769 ssl_sock_set_srv(sv, 1);
3770 } else if (strcmp(args[4], "off") == 0) {
3771 ssl_sock_set_srv(sv, 0);
3772 } else {
3773 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
3774 goto out_unlock;
3775 }
3776 srv_cleanup_connections(sv);
3777 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
3778#else
3779 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
3780#endif
3781 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02003782 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01003783 "usage: set server <backend>/<server> "
3784 "addr | agent | agent-addr | agent-port | agent-send | "
3785 "check-addr | check-port | fqdn | health | ssl | "
3786 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01003787 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003788 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003789 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003790 return 1;
3791}
3792
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003793static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01003794{
3795 struct stream_interface *si = appctx->owner;
3796 struct proxy *px;
3797 struct server *sv;
3798 char *line;
3799
3800
3801 /* split "backend/server" and make <line> point to server */
3802 for (line = args[2]; *line; line++)
3803 if (*line == '/') {
3804 *line++ = '\0';
3805 break;
3806 }
3807
Willy Tarreau9d008692019-08-09 11:21:01 +02003808 if (!*line)
3809 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01003810
Willy Tarreau9d008692019-08-09 11:21:01 +02003811 if (!get_backend_server(args[2], line, &px, &sv))
3812 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01003813
3814 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003815 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
3816 sv->iweight);
3817 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003818 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003819 return 0;
3820 }
William Lallemand6b160942016-11-22 12:34:35 +01003821 return 1;
3822}
3823
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003824/* Parse a "set weight" command.
3825 *
3826 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003827 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003828static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01003829{
3830 struct server *sv;
3831 const char *warning;
3832
3833 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3834 return 1;
3835
3836 sv = cli_find_server(appctx, args[2]);
3837 if (!sv)
3838 return 1;
3839
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003840 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
3841
William Lallemand6b160942016-11-22 12:34:35 +01003842 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003843 if (warning)
3844 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003845
3846 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
3847
William Lallemand6b160942016-11-22 12:34:35 +01003848 return 1;
3849}
3850
Willy Tarreau46b7f532018-08-21 11:54:26 +02003851/* parse a "set maxconn server" command. It always returns 1.
3852 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003853 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003854 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003855static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01003856{
3857 struct server *sv;
3858 const char *warning;
3859
3860 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3861 return 1;
3862
3863 sv = cli_find_server(appctx, args[3]);
3864 if (!sv)
3865 return 1;
3866
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003867 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
3868
Willy Tarreaub8026272016-11-23 11:26:56 +01003869 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003870 if (warning)
3871 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003872
3873 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
3874
Willy Tarreaub8026272016-11-23 11:26:56 +01003875 return 1;
3876}
William Lallemand6b160942016-11-22 12:34:35 +01003877
Willy Tarreau46b7f532018-08-21 11:54:26 +02003878/* parse a "disable agent" command. It always returns 1.
3879 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003880 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003881 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003882static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003883{
3884 struct server *sv;
3885
3886 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3887 return 1;
3888
3889 sv = cli_find_server(appctx, args[2]);
3890 if (!sv)
3891 return 1;
3892
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003893 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003894 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003895 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003896 return 1;
3897}
3898
Willy Tarreau46b7f532018-08-21 11:54:26 +02003899/* parse a "disable health" command. It always returns 1.
3900 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003901 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003902 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003903static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003904{
3905 struct server *sv;
3906
3907 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3908 return 1;
3909
3910 sv = cli_find_server(appctx, args[2]);
3911 if (!sv)
3912 return 1;
3913
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003914 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003915 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003916 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003917 return 1;
3918}
3919
Willy Tarreau46b7f532018-08-21 11:54:26 +02003920/* parse a "disable server" command. It always returns 1.
3921 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003922 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003923 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003924static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01003925{
3926 struct server *sv;
3927
3928 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3929 return 1;
3930
3931 sv = cli_find_server(appctx, args[2]);
3932 if (!sv)
3933 return 1;
3934
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003935 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01003936 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003937 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01003938 return 1;
3939}
3940
Willy Tarreau46b7f532018-08-21 11:54:26 +02003941/* parse a "enable agent" command. It always returns 1.
3942 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003943 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003944 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003945static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003946{
3947 struct server *sv;
3948
3949 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3950 return 1;
3951
3952 sv = cli_find_server(appctx, args[2]);
3953 if (!sv)
3954 return 1;
3955
Willy Tarreau9d008692019-08-09 11:21:01 +02003956 if (!(sv->agent.state & CHK_ST_CONFIGURED))
3957 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003958
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003959 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003960 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003961 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003962 return 1;
3963}
3964
Willy Tarreau46b7f532018-08-21 11:54:26 +02003965/* parse a "enable health" command. It always returns 1.
3966 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003967 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003968 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003969static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003970{
3971 struct server *sv;
3972
3973 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3974 return 1;
3975
3976 sv = cli_find_server(appctx, args[2]);
3977 if (!sv)
3978 return 1;
3979
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003980 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003981 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003982 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003983 return 1;
3984}
3985
Willy Tarreau46b7f532018-08-21 11:54:26 +02003986/* parse a "enable server" command. It always returns 1.
3987 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003988 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003989 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003990static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01003991{
3992 struct server *sv;
3993
3994 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3995 return 1;
3996
3997 sv = cli_find_server(appctx, args[2]);
3998 if (!sv)
3999 return 1;
4000
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004001 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004002 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004003 if (!(sv->flags & SRV_F_COOKIESET)
4004 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4005 sv->cookie)
4006 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004007 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004008 return 1;
4009}
4010
William Lallemand222baf22016-11-19 02:00:33 +01004011/* register cli keywords */
4012static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004013 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004014 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004015 { { "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 +01004016 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004017 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004018 { { "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 +01004019 { { "set", "maxconn", "server", NULL }, "set maxconn server : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
William Dauchyf6370442020-11-14 19:25:33 +01004020 { { "set", "server", NULL }, "set server : change a server's state, weight, address or ssl", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004021 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4022 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4023
William Lallemand222baf22016-11-19 02:00:33 +01004024 {{},}
4025}};
4026
Willy Tarreau0108d902018-11-25 19:14:37 +01004027INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004028
Emeric Brun64cc49c2017-10-03 14:46:45 +02004029/*
4030 * This function applies server's status changes, it is
4031 * is designed to be called asynchronously.
4032 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004033 * Must be called with the server lock held. This may also be called at init
4034 * time as the result of parsing the state file, in which case no lock will be
4035 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004036 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004037static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004038{
4039 struct check *check = &s->check;
4040 int xferred;
4041 struct proxy *px = s->proxy;
4042 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4043 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4044 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004045 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004046
Emeric Brun64cc49c2017-10-03 14:46:45 +02004047 /* If currently main is not set we try to apply pending state changes */
4048 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4049 int next_admin;
4050
4051 /* Backup next admin */
4052 next_admin = s->next_admin;
4053
4054 /* restore current admin state */
4055 s->next_admin = s->cur_admin;
4056
4057 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4058 s->last_change = now.tv_sec;
4059 if (s->proxy->lbprm.set_server_status_down)
4060 s->proxy->lbprm.set_server_status_down(s);
4061
4062 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4063 srv_shutdown_streams(s, SF_ERR_DOWN);
4064
4065 /* we might have streams queued on this server and waiting for
4066 * a connection. Those which are redispatchable will be queued
4067 * to another server or to the proxy itself.
4068 */
4069 xferred = pendconn_redistribute(s);
4070
4071 tmptrash = alloc_trash_chunk();
4072 if (tmptrash) {
4073 chunk_printf(tmptrash,
4074 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4075 s->proxy->id, s->id);
4076
Emeric Brun5a133512017-10-19 14:42:30 +02004077 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004078 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004079
4080 /* we don't send an alert if the server was previously paused */
4081 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004082 send_log(s->proxy, log_level, "%s.\n",
4083 tmptrash->area);
4084 send_email_alert(s, log_level, "%s",
4085 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004086 free_trash_chunk(tmptrash);
4087 tmptrash = NULL;
4088 }
4089 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4090 set_backend_down(s->proxy);
4091
4092 s->counters.down_trans++;
4093 }
4094 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4095 s->last_change = now.tv_sec;
4096 if (s->proxy->lbprm.set_server_status_down)
4097 s->proxy->lbprm.set_server_status_down(s);
4098
4099 /* we might have streams queued on this server and waiting for
4100 * a connection. Those which are redispatchable will be queued
4101 * to another server or to the proxy itself.
4102 */
4103 xferred = pendconn_redistribute(s);
4104
4105 tmptrash = alloc_trash_chunk();
4106 if (tmptrash) {
4107 chunk_printf(tmptrash,
4108 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4109 s->proxy->id, s->id);
4110
Emeric Brun5a133512017-10-19 14:42:30 +02004111 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004112
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004113 ha_warning("%s.\n", tmptrash->area);
4114 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4115 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004116 free_trash_chunk(tmptrash);
4117 tmptrash = NULL;
4118 }
4119
4120 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4121 set_backend_down(s->proxy);
4122 }
4123 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4124 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4125 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4126 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4127 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4128 s->proxy->last_change = now.tv_sec;
4129 }
4130
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01004131 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02004132 s->down_time += now.tv_sec - s->last_change;
4133
4134 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004135 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004136 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4137
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004138 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004139 /* now propagate the status change to any LB algorithms */
4140 if (px->lbprm.update_server_eweight)
4141 px->lbprm.update_server_eweight(s);
4142 else if (srv_willbe_usable(s)) {
4143 if (px->lbprm.set_server_status_up)
4144 px->lbprm.set_server_status_up(s);
4145 }
4146 else {
4147 if (px->lbprm.set_server_status_down)
4148 px->lbprm.set_server_status_down(s);
4149 }
4150
4151 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4152 * and it's not a backup server and its effective weight is > 0,
4153 * then it can accept new connections, so we shut down all streams
4154 * on all backup servers.
4155 */
4156 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4157 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4158 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4159
4160 /* check if we can handle some connections queued at the proxy. We
4161 * will take as many as we can handle.
4162 */
4163 xferred = pendconn_grab_from_px(s);
4164
4165 tmptrash = alloc_trash_chunk();
4166 if (tmptrash) {
4167 chunk_printf(tmptrash,
4168 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4169 s->proxy->id, s->id);
4170
Emeric Brun5a133512017-10-19 14:42:30 +02004171 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004172 ha_warning("%s.\n", tmptrash->area);
4173 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4174 tmptrash->area);
4175 send_email_alert(s, LOG_NOTICE, "%s",
4176 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004177 free_trash_chunk(tmptrash);
4178 tmptrash = NULL;
4179 }
4180
4181 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4182 set_backend_down(s->proxy);
4183 }
4184 else if (s->cur_eweight != s->next_eweight) {
4185 /* now propagate the status change to any LB algorithms */
4186 if (px->lbprm.update_server_eweight)
4187 px->lbprm.update_server_eweight(s);
4188 else if (srv_willbe_usable(s)) {
4189 if (px->lbprm.set_server_status_up)
4190 px->lbprm.set_server_status_up(s);
4191 }
4192 else {
4193 if (px->lbprm.set_server_status_down)
4194 px->lbprm.set_server_status_down(s);
4195 }
4196
4197 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4198 set_backend_down(s->proxy);
4199 }
4200
4201 s->next_admin = next_admin;
4202 }
4203
Emeric Brun5a133512017-10-19 14:42:30 +02004204 /* reset operational state change */
4205 *s->op_st_chg.reason = 0;
4206 s->op_st_chg.status = s->op_st_chg.code = -1;
4207 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004208
4209 /* Now we try to apply pending admin changes */
4210
4211 /* Maintenance must also disable health checks */
4212 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4213 if (s->check.state & CHK_ST_ENABLED) {
4214 s->check.state |= CHK_ST_PAUSED;
4215 check->health = 0;
4216 }
4217
4218 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004219 tmptrash = alloc_trash_chunk();
4220 if (tmptrash) {
4221 chunk_printf(tmptrash,
4222 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4223 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4224 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004225
Olivier Houchard796a2b32017-10-24 17:42:47 +02004226 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004227
Olivier Houchard796a2b32017-10-24 17:42:47 +02004228 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004229 ha_warning("%s.\n", tmptrash->area);
4230 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4231 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004232 }
4233 free_trash_chunk(tmptrash);
4234 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004235 }
Emeric Brun8f298292017-12-06 16:47:17 +01004236 /* commit new admin status */
4237
4238 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004239 }
4240 else { /* server was still running */
4241 check->health = 0; /* failure */
4242 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004243
4244 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004245 if (s->proxy->lbprm.set_server_status_down)
4246 s->proxy->lbprm.set_server_status_down(s);
4247
Emeric Brun64cc49c2017-10-03 14:46:45 +02004248 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4249 srv_shutdown_streams(s, SF_ERR_DOWN);
4250
William Dauchy6318d332020-05-02 21:52:36 +02004251 /* force connection cleanup on the given server */
4252 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004253 /* we might have streams queued on this server and waiting for
4254 * a connection. Those which are redispatchable will be queued
4255 * to another server or to the proxy itself.
4256 */
4257 xferred = pendconn_redistribute(s);
4258
4259 tmptrash = alloc_trash_chunk();
4260 if (tmptrash) {
4261 chunk_printf(tmptrash,
4262 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4263 s->flags & SRV_F_BACKUP ? "Backup " : "",
4264 s->proxy->id, s->id,
4265 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4266
4267 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4268
4269 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004270 ha_warning("%s.\n", tmptrash->area);
4271 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4272 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004273 }
4274 free_trash_chunk(tmptrash);
4275 tmptrash = NULL;
4276 }
4277 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4278 set_backend_down(s->proxy);
4279
4280 s->counters.down_trans++;
4281 }
4282 }
4283 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4284 /* OK here we're leaving maintenance, we have many things to check,
4285 * because the server might possibly be coming back up depending on
4286 * its state. In practice, leaving maintenance means that we should
4287 * immediately turn to UP (more or less the slowstart) under the
4288 * following conditions :
4289 * - server is neither checked nor tracked
4290 * - server tracks another server which is not checked
4291 * - server tracks another server which is already up
4292 * Which sums up as something simpler :
4293 * "either the tracking server is up or the server's checks are disabled
4294 * or up". Otherwise we only re-enable health checks. There's a special
4295 * case associated to the stopping state which can be inherited. Note
4296 * that the server might still be in drain mode, which is naturally dealt
4297 * with by the lower level functions.
4298 */
4299
4300 if (s->check.state & CHK_ST_ENABLED) {
4301 s->check.state &= ~CHK_ST_PAUSED;
4302 check->health = check->rise; /* start OK but check immediately */
4303 }
4304
4305 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4306 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4307 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4308 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4309 s->next_state = SRV_ST_STOPPING;
4310 }
4311 else {
4312 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004313 if (s->slowstart > 0) {
4314 if (s->warmup)
4315 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4316 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02004317 else
4318 s->next_state = SRV_ST_RUNNING;
4319 }
4320
4321 }
4322
4323 tmptrash = alloc_trash_chunk();
4324 if (tmptrash) {
4325 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4326 chunk_printf(tmptrash,
4327 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4328 s->flags & SRV_F_BACKUP ? "Backup " : "",
4329 s->proxy->id, s->id,
4330 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4331 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4332 }
4333 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4334 chunk_printf(tmptrash,
4335 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4336 s->flags & SRV_F_BACKUP ? "Backup " : "",
4337 s->proxy->id, s->id, s->hostname,
4338 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4339 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4340 }
4341 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4342 chunk_printf(tmptrash,
4343 "%sServer %s/%s is %s/%s (leaving maintenance)",
4344 s->flags & SRV_F_BACKUP ? "Backup " : "",
4345 s->proxy->id, s->id,
4346 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4347 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4348 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004349 ha_warning("%s.\n", tmptrash->area);
4350 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4351 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004352 free_trash_chunk(tmptrash);
4353 tmptrash = NULL;
4354 }
4355
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004356 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004357 /* now propagate the status change to any LB algorithms */
4358 if (px->lbprm.update_server_eweight)
4359 px->lbprm.update_server_eweight(s);
4360 else if (srv_willbe_usable(s)) {
4361 if (px->lbprm.set_server_status_up)
4362 px->lbprm.set_server_status_up(s);
4363 }
4364 else {
4365 if (px->lbprm.set_server_status_down)
4366 px->lbprm.set_server_status_down(s);
4367 }
4368
4369 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4370 set_backend_down(s->proxy);
4371
Willy Tarreau6a78e612018-08-07 10:14:53 +02004372 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4373 * and it's not a backup server and its effective weight is > 0,
4374 * then it can accept new connections, so we shut down all streams
4375 * on all backup servers.
4376 */
4377 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4378 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4379 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4380
4381 /* check if we can handle some connections queued at the proxy. We
4382 * will take as many as we can handle.
4383 */
4384 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004385 }
4386 else if (s->next_admin & SRV_ADMF_MAINT) {
4387 /* remaining in maintenance mode, let's inform precisely about the
4388 * situation.
4389 */
4390 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4391 tmptrash = alloc_trash_chunk();
4392 if (tmptrash) {
4393 chunk_printf(tmptrash,
4394 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4395 s->flags & SRV_F_BACKUP ? "Backup " : "",
4396 s->proxy->id, s->id);
4397
4398 if (s->track) /* normally it's mandatory here */
4399 chunk_appendf(tmptrash, " via %s/%s",
4400 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004401 ha_warning("%s.\n", tmptrash->area);
4402 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4403 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004404 free_trash_chunk(tmptrash);
4405 tmptrash = NULL;
4406 }
4407 }
4408 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4409 tmptrash = alloc_trash_chunk();
4410 if (tmptrash) {
4411 chunk_printf(tmptrash,
4412 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4413 s->flags & SRV_F_BACKUP ? "Backup " : "",
4414 s->proxy->id, s->id, s->hostname);
4415
4416 if (s->track) /* normally it's mandatory here */
4417 chunk_appendf(tmptrash, " via %s/%s",
4418 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004419 ha_warning("%s.\n", tmptrash->area);
4420 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4421 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004422 free_trash_chunk(tmptrash);
4423 tmptrash = NULL;
4424 }
4425 }
4426 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4427 tmptrash = alloc_trash_chunk();
4428 if (tmptrash) {
4429 chunk_printf(tmptrash,
4430 "%sServer %s/%s remains in forced maintenance",
4431 s->flags & SRV_F_BACKUP ? "Backup " : "",
4432 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004433 ha_warning("%s.\n", tmptrash->area);
4434 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4435 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004436 free_trash_chunk(tmptrash);
4437 tmptrash = NULL;
4438 }
4439 }
4440 /* don't report anything when leaving drain mode and remaining in maintenance */
4441
4442 s->cur_admin = s->next_admin;
4443 }
4444
4445 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4446 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4447 /* drain state is applied only if not yet in maint */
4448
4449 s->last_change = now.tv_sec;
4450 if (px->lbprm.set_server_status_down)
4451 px->lbprm.set_server_status_down(s);
4452
4453 /* we might have streams queued on this server and waiting for
4454 * a connection. Those which are redispatchable will be queued
4455 * to another server or to the proxy itself.
4456 */
4457 xferred = pendconn_redistribute(s);
4458
4459 tmptrash = alloc_trash_chunk();
4460 if (tmptrash) {
4461 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4462 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4463 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4464
4465 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4466
4467 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004468 ha_warning("%s.\n", tmptrash->area);
4469 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4470 tmptrash->area);
4471 send_email_alert(s, LOG_NOTICE, "%s",
4472 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004473 }
4474 free_trash_chunk(tmptrash);
4475 tmptrash = NULL;
4476 }
4477
4478 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4479 set_backend_down(s->proxy);
4480 }
4481 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4482 /* OK completely leaving drain mode */
4483 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4484 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4485 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4486 s->proxy->last_change = now.tv_sec;
4487 }
4488
4489 if (s->last_change < now.tv_sec) // ignore negative times
4490 s->down_time += now.tv_sec - s->last_change;
4491 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004492 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004493
4494 tmptrash = alloc_trash_chunk();
4495 if (tmptrash) {
4496 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4497 chunk_printf(tmptrash,
4498 "%sServer %s/%s is %s (leaving forced drain)",
4499 s->flags & SRV_F_BACKUP ? "Backup " : "",
4500 s->proxy->id, s->id,
4501 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4502 }
4503 else {
4504 chunk_printf(tmptrash,
4505 "%sServer %s/%s is %s (leaving drain)",
4506 s->flags & SRV_F_BACKUP ? "Backup " : "",
4507 s->proxy->id, s->id,
4508 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4509 if (s->track) /* normally it's mandatory here */
4510 chunk_appendf(tmptrash, " via %s/%s",
4511 s->track->proxy->id, s->track->id);
4512 }
4513
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004514 ha_warning("%s.\n", tmptrash->area);
4515 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4516 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004517 free_trash_chunk(tmptrash);
4518 tmptrash = NULL;
4519 }
4520
4521 /* now propagate the status change to any LB algorithms */
4522 if (px->lbprm.update_server_eweight)
4523 px->lbprm.update_server_eweight(s);
4524 else if (srv_willbe_usable(s)) {
4525 if (px->lbprm.set_server_status_up)
4526 px->lbprm.set_server_status_up(s);
4527 }
4528 else {
4529 if (px->lbprm.set_server_status_down)
4530 px->lbprm.set_server_status_down(s);
4531 }
4532 }
4533 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4534 /* remaining in drain mode after removing one of its flags */
4535
4536 tmptrash = alloc_trash_chunk();
4537 if (tmptrash) {
4538 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4539 chunk_printf(tmptrash,
4540 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4541 s->flags & SRV_F_BACKUP ? "Backup " : "",
4542 s->proxy->id, s->id);
4543
4544 if (s->track) /* normally it's mandatory here */
4545 chunk_appendf(tmptrash, " via %s/%s",
4546 s->track->proxy->id, s->track->id);
4547 }
4548 else {
4549 chunk_printf(tmptrash,
4550 "%sServer %s/%s remains in forced drain mode",
4551 s->flags & SRV_F_BACKUP ? "Backup " : "",
4552 s->proxy->id, s->id);
4553 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004554 ha_warning("%s.\n", tmptrash->area);
4555 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4556 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004557 free_trash_chunk(tmptrash);
4558 tmptrash = NULL;
4559 }
4560
4561 /* commit new admin status */
4562
4563 s->cur_admin = s->next_admin;
4564 }
4565 }
4566
4567 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004568 *s->adm_st_chg_cause = 0;
4569}
Emeric Brun64cc49c2017-10-03 14:46:45 +02004570
Willy Tarreau144f84a2021-03-02 16:09:26 +01004571struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004572{
4573 struct connection *conn;
4574
Willy Tarreau4d82bf52020-06-28 00:19:17 +02004575 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004576 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02004577 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004578 }
4579
4580 return task;
4581}
4582
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004583/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02004584 * moving them all.
4585 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004586 *
4587 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02004588 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004589static int srv_migrate_conns_to_remove(struct eb_root *idle_tree, struct mt_list *toremove_list, int toremove_nb)
Olivier Houchardff1d0922020-06-29 20:15:59 +02004590{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004591 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004592 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02004593 int i = 0;
4594
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004595 node = eb_first(idle_tree);
4596 while (node) {
4597 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004598 if (toremove_nb != -1 && i >= toremove_nb)
4599 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004600
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004601 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004602 eb_delete(node);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004603 MT_LIST_ADDQ(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004604 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004605
4606 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02004607 }
4608 return i;
4609}
William Dauchy6318d332020-05-02 21:52:36 +02004610/* cleanup connections for a given server
4611 * might be useful when going on forced maintenance or live changing ip/port
4612 */
William Dauchy707ad322020-05-04 13:52:40 +02004613static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02004614{
William Dauchy6318d332020-05-02 21:52:36 +02004615 int did_remove;
4616 int i;
William Dauchy6318d332020-05-02 21:52:36 +02004617
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01004618 /* nothing to do if pool-max-conn is null */
4619 if (!srv->max_idle_conns)
4620 return;
4621
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004622 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004623 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02004624 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004625 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01004626 if (srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02004627 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01004628 if (srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, -1) > 0)
Olivier Houchardff1d0922020-06-29 20:15:59 +02004629 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004630 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02004631 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02004632 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004633
4634 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
4635 break;
William Dauchy6318d332020-05-02 21:52:36 +02004636 }
William Dauchy6318d332020-05-02 21:52:36 +02004637}
4638
Willy Tarreau144f84a2021-03-02 16:09:26 +01004639struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004640{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004641 struct server *srv;
4642 struct eb32_node *eb;
4643 int i;
4644 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01004645
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004646 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004647 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
4648 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004649 int exceed_conns;
4650 int to_kill;
4651 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01004652
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004653 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
4654 if (!eb) {
4655 /* we might have reached the end of the tree, typically because
4656 * <now_ms> is in the first half and we're first scanning the last
4657 * half. Let's loop back to the beginning of the tree now.
4658 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004659
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004660 eb = eb32_first(&idle_conn_srv);
4661 if (likely(!eb))
4662 break;
4663 }
4664 if (tick_is_lt(now_ms, eb->key)) {
4665 /* timer not expired yet, revisit it later */
4666 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004667 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004668 }
4669 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004670
4671 /* Calculate how many idle connections we want to kill :
4672 * we want to remove half the difference between the total
4673 * of established connections (used or idle) and the max
4674 * number of used connections.
4675 */
4676 curr_idle = srv->curr_idle_conns;
4677 if (curr_idle == 0)
4678 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02004679 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004680 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02004681
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004682 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02004683 if (srv->est_need_conns < srv->max_used_conns)
4684 srv->est_need_conns = srv->max_used_conns;
4685
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004686 srv->max_used_conns = srv->curr_used_conns;
4687
Willy Tarreau18ed7892020-07-02 19:05:30 +02004688 if (exceed_conns <= 0)
4689 goto remove;
4690
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004691 /* check all threads starting with ours */
4692 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004693 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004694 int j;
4695 int did_remove = 0;
4696
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004697 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
4698 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02004699
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004700 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01004701 j = srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, max_conn);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004702 if (j > 0)
4703 did_remove = 1;
4704 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01004705 srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, max_conn - j) > 0)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004706 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004707 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004708
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004709 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02004710 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004711
4712 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
4713 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004714 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004715remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004716 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004717
4718 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004719 /* There are still more idle connections, add the
4720 * server back in the tree.
4721 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004722 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004723 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004724 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004725 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004726 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004727 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
4728
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004729 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004730 return task;
4731}
Olivier Houchard88698d92019-04-16 19:07:22 +02004732
Willy Tarreau76cc6992020-07-01 18:49:24 +02004733/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
4734static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
4735 struct proxy *defpx, const char *file, int line,
4736 char **err)
4737{
4738 if (too_many_args(1, args, err, NULL))
4739 return -1;
4740
4741 if (strcmp(args[1], "on") == 0)
4742 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
4743 else if (strcmp(args[1], "off") == 0)
4744 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
4745 else {
4746 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
4747 return -1;
4748 }
4749 return 0;
4750}
4751
Olivier Houchard88698d92019-04-16 19:07:22 +02004752/* config parser for global "tune.pool-{low,high}-fd-ratio" */
4753static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
4754 struct proxy *defpx, const char *file, int line,
4755 char **err)
4756{
4757 int arg = -1;
4758
4759 if (too_many_args(1, args, err, NULL))
4760 return -1;
4761
4762 if (*(args[1]) != 0)
4763 arg = atoi(args[1]);
4764
4765 if (arg < 0 || arg > 100) {
4766 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
4767 return -1;
4768 }
4769
4770 if (args[0][10] == 'h')
4771 global.tune.pool_high_ratio = arg;
4772 else
4773 global.tune.pool_low_ratio = arg;
4774 return 0;
4775}
4776
4777/* config keyword parsers */
4778static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02004779 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02004780 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
4781 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
4782 { 0, NULL, NULL }
4783}};
4784
4785INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4786
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004787/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004788 * Local variables:
4789 * c-indent-level: 8
4790 * c-basic-offset: 8
4791 * End:
4792 */