blob: dd6340b530bf52043d059477bdad54ca8b073ec2 [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 Tarreau4aac7db2014-05-16 11:48:10 +0200894
Willy Tarreau751153e2021-02-17 13:33:24 +0100895 mt_list_for_each_entry_safe(stream, &srv->actconns, by_srv, elt1, elt2)
Willy Tarreau87b09662015-04-03 00:22:06 +0200896 if (stream->srv_conn == srv)
897 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200898}
899
900/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200901 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200902 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200903 *
904 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200905 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200906void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200907{
908 struct server *srv;
909
910 for (srv = px->srv; srv != NULL; srv = srv->next)
911 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200912 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200913}
914
Willy Tarreaubda92272014-05-20 21:55:30 +0200915/* Appends some information to a message string related to a server going UP or
916 * DOWN. If both <forced> and <reason> are null and the server tracks another
917 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200918 * If <check> is non-null, an entire string describing the check result will be
919 * appended after a comma and a space (eg: to report some information from the
920 * check that changed the state). In the other case, the string will be built
921 * using the check results stored into the struct server if present.
922 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200923 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200924 *
925 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200926 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200927void srv_append_status(struct buffer *msg, struct server *s,
928 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200929{
Emeric Brun5a133512017-10-19 14:42:30 +0200930 short status = s->op_st_chg.status;
931 short code = s->op_st_chg.code;
932 long duration = s->op_st_chg.duration;
933 char *desc = s->op_st_chg.reason;
934
935 if (check) {
936 status = check->status;
937 code = check->code;
938 duration = check->duration;
939 desc = check->desc;
940 }
941
942 if (status != -1) {
943 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
944
945 if (status >= HCHK_STATUS_L57DATA)
946 chunk_appendf(msg, ", code: %d", code);
947
948 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200949 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200950
951 chunk_appendf(msg, ", info: \"");
952
953 chunk_initlen(&src, desc, 0, strlen(desc));
954 chunk_asciiencode(msg, &src, '"');
955
956 chunk_appendf(msg, "\"");
957 }
958
959 if (duration >= 0)
960 chunk_appendf(msg, ", check duration: %ldms", duration);
961 }
962 else if (desc && *desc) {
963 chunk_appendf(msg, ", %s", desc);
964 }
965 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200966 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200967 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200968
969 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200970 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200971 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
972 " %d sessions active, %d requeued, %d remaining in queue",
973 s->proxy->srv_act, s->proxy->srv_bck,
974 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
975 s->cur_sess, xferred, s->nbpend);
976 else
977 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
978 " %d sessions requeued, %d total in queue",
979 s->proxy->srv_act, s->proxy->srv_bck,
980 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
981 xferred, s->nbpend);
982 }
983}
984
Emeric Brun5a133512017-10-19 14:42:30 +0200985/* Marks server <s> down, regardless of its checks' statuses. The server is
986 * registered in a list to postpone the counting of the remaining servers on
987 * the proxy and transfers queued streams whenever possible to other servers at
988 * a sync point. Maintenance servers are ignored. It stores the <reason> if
989 * non-null as the reason for going down or the available data from the check
990 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200991 *
992 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200993 */
Emeric Brun5a133512017-10-19 14:42:30 +0200994void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200995{
996 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200997
Emeric Brun64cc49c2017-10-03 14:46:45 +0200998 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200999 return;
1000
Emeric Brun52a91d32017-08-31 14:41:55 +02001001 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001002 *s->op_st_chg.reason = 0;
1003 s->op_st_chg.status = -1;
1004 if (reason) {
1005 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1006 }
1007 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001008 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001009 s->op_st_chg.code = check->code;
1010 s->op_st_chg.status = check->status;
1011 s->op_st_chg.duration = check->duration;
1012 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001013
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001014 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001015 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001016
Emeric Brun9f0b4582017-10-23 14:39:51 +02001017 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001018 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001019 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001020 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001021 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001022}
1023
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001024/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001025 * in maintenance. The server is registered in a list to postpone the counting
1026 * of the remaining servers on the proxy and tries to grab requests from the
1027 * proxy at a sync point. Maintenance servers are ignored. It stores the
1028 * <reason> if non-null as the reason for going down or the available data
1029 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001030 *
1031 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001032 */
Emeric Brun5a133512017-10-19 14:42:30 +02001033void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001034{
1035 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001036
Emeric Brun64cc49c2017-10-03 14:46:45 +02001037 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038 return;
1039
Emeric Brun52a91d32017-08-31 14:41:55 +02001040 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001041 return;
1042
Emeric Brun52a91d32017-08-31 14:41:55 +02001043 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001044 *s->op_st_chg.reason = 0;
1045 s->op_st_chg.status = -1;
1046 if (reason) {
1047 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1048 }
1049 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001050 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001051 s->op_st_chg.code = check->code;
1052 s->op_st_chg.status = check->status;
1053 s->op_st_chg.duration = check->duration;
1054 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001055
Emeric Brun64cc49c2017-10-03 14:46:45 +02001056 if (s->slowstart <= 0)
1057 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001058
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001059 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001060 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001061
Emeric Brun9f0b4582017-10-23 14:39:51 +02001062 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001063 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001064 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001066 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001067}
1068
Willy Tarreau8eb77842014-05-21 13:54:57 +02001069/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001070 * isn't in maintenance. The server is registered in a list to postpone the
1071 * counting of the remaining servers on the proxy and tries to grab requests
1072 * from the proxy. Maintenance servers are ignored. It stores the
1073 * <reason> if non-null as the reason for going down or the available data
1074 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001075 * up. Note that it makes use of the trash to build the log strings, so <reason>
1076 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001077 *
1078 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001079 */
Emeric Brun5a133512017-10-19 14:42:30 +02001080void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001081{
1082 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001083
Emeric Brun64cc49c2017-10-03 14:46:45 +02001084 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085 return;
1086
Emeric Brun52a91d32017-08-31 14:41:55 +02001087 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001088 return;
1089
Emeric Brun52a91d32017-08-31 14:41:55 +02001090 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001091 *s->op_st_chg.reason = 0;
1092 s->op_st_chg.status = -1;
1093 if (reason) {
1094 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1095 }
1096 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001097 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001098 s->op_st_chg.code = check->code;
1099 s->op_st_chg.status = check->status;
1100 s->op_st_chg.duration = check->duration;
1101 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001102
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001103 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001104 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001105
Emeric Brun9f0b4582017-10-23 14:39:51 +02001106 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001107 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001108 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001109 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001110 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001111}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001112
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001113/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1114 * enforce either maint mode or drain mode. It is not allowed to set more than
1115 * one flag at once. The equivalent "inherited" flag is propagated to all
1116 * tracking servers. Maintenance mode disables health checks (but not agent
1117 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001118 * is done. If <cause> is non-null, it will be displayed at the end of the log
1119 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001120 *
1121 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001122 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001123void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001124{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001125 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126
1127 if (!mode)
1128 return;
1129
1130 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001131 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132 return;
1133
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001135 if (cause)
1136 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1137
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001138 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001139 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001140
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001141 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001142 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1143 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001144 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001145
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001146 /* compute the inherited flag to propagate */
1147 if (mode & SRV_ADMF_MAINT)
1148 mode = SRV_ADMF_IMAINT;
1149 else if (mode & SRV_ADMF_DRAIN)
1150 mode = SRV_ADMF_IDRAIN;
1151
Emeric Brun9f0b4582017-10-23 14:39:51 +02001152 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001153 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001154 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001155 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001156 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001157}
1158
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001159/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1160 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1161 * than one flag at once. The equivalent "inherited" flag is propagated to all
1162 * tracking servers. Leaving maintenance mode re-enables health checks. When
1163 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001164 *
1165 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001166 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001167void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001168{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001169 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170
1171 if (!mode)
1172 return;
1173
1174 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001175 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001176 return;
1177
Emeric Brun52a91d32017-08-31 14:41:55 +02001178 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001179
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001180 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001181 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001182
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001183 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001184 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1185 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001186 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001187
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001188 if (mode & SRV_ADMF_MAINT)
1189 mode = SRV_ADMF_IMAINT;
1190 else if (mode & SRV_ADMF_DRAIN)
1191 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001192
Emeric Brun9f0b4582017-10-23 14:39:51 +02001193 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001194 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001195 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001196 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001197 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001198}
1199
Willy Tarreau757478e2016-11-03 19:22:19 +01001200/* principle: propagate maint and drain to tracking servers. This is useful
1201 * upon startup so that inherited states are correct.
1202 */
1203static void srv_propagate_admin_state(struct server *srv)
1204{
1205 struct server *srv2;
1206
1207 if (!srv->trackers)
1208 return;
1209
1210 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001211 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001212 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001213 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001214
Emeric Brun52a91d32017-08-31 14:41:55 +02001215 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001216 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001217 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001218 }
1219}
1220
1221/* Compute and propagate the admin states for all servers in proxy <px>.
1222 * Only servers *not* tracking another one are considered, because other
1223 * ones will be handled when the server they track is visited.
1224 */
1225void srv_compute_all_admin_states(struct proxy *px)
1226{
1227 struct server *srv;
1228
1229 for (srv = px->srv; srv; srv = srv->next) {
1230 if (srv->track)
1231 continue;
1232 srv_propagate_admin_state(srv);
1233 }
1234}
1235
Willy Tarreaudff55432012-10-10 17:51:05 +02001236/* Note: must not be declared <const> as its list will be overwritten.
1237 * Please take care of keeping this list alphabetically sorted, doing so helps
1238 * all code contributors.
1239 * Optional keywords are also declared with a NULL ->parse() function so that
1240 * the config parser can report an appropriate error when a known keyword was
1241 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001242 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001243 */
1244static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001245 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001246 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001247 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1248 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001249 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001250 { "max-reuse", srv_parse_max_reuse, 1, 1 }, /* Set the max number of requests on a connection, -1 means unlimited */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001251 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001252 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001253 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1254 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001255 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001256 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001257 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001258 { "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 +01001259 { "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 +01001260 { "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 +02001261 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001262 { "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 +01001263 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001264 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1265 { "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 +02001266 { "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 +01001267 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001268 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001269 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001270 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001271 { NULL, NULL, 0 },
1272}};
1273
Willy Tarreau0108d902018-11-25 19:14:37 +01001274INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001275
Willy Tarreau004e0452013-11-21 11:22:01 +01001276/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001277 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001278 * state is automatically disabled if the time is elapsed. If <must_update> is
1279 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001280 *
1281 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001282 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001283void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001284{
1285 struct proxy *px = sv->proxy;
1286 unsigned w;
1287
1288 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1289 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001290 if (sv->next_state == SRV_ST_STARTING)
1291 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001292 }
1293
1294 /* We must take care of not pushing the server to full throttle during slow starts.
1295 * It must also start immediately, at least at the minimal step when leaving maintenance.
1296 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001297 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001298 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1299 else
1300 w = px->lbprm.wdiv;
1301
Emeric Brun52a91d32017-08-31 14:41:55 +02001302 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001303
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001304 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001305 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001306 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001307}
1308
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001310 * Parses weight_str and configures sv accordingly.
1311 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001312 *
1313 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001314 */
1315const char *server_parse_weight_change_request(struct server *sv,
1316 const char *weight_str)
1317{
1318 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001319 long int w;
1320 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001321
1322 px = sv->proxy;
1323
1324 /* if the weight is terminated with '%', it is set relative to
1325 * the initial weight, otherwise it is absolute.
1326 */
1327 if (!*weight_str)
1328 return "Require <weight> or <weight%>.\n";
1329
Simon Hormanb796afa2013-02-12 10:45:53 +09001330 w = strtol(weight_str, &end, 10);
1331 if (end == weight_str)
1332 return "Empty weight string empty or preceded by garbage";
1333 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001334 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001335 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001336 /* Avoid integer overflow */
1337 if (w > 25600)
1338 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001339 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001340 if (w > 256)
1341 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001342 }
1343 else if (w < 0 || w > 256)
1344 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001345 else if (end[0] != '\0')
1346 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001347
1348 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1349 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1350
1351 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001352 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001353
1354 return NULL;
1355}
1356
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001357/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001358 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1359 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001360 * Returns:
1361 * - error string on error
1362 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001363 *
1364 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001365 */
1366const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001367 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001368{
1369 unsigned char ip[INET6_ADDRSTRLEN];
1370
1371 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001372 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001373 return NULL;
1374 }
1375 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001376 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001377 return NULL;
1378 }
1379
1380 return "Could not understand IP address format.\n";
1381}
1382
Willy Tarreau46b7f532018-08-21 11:54:26 +02001383/*
1384 * Must be called with the server lock held.
1385 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001386const char *server_parse_maxconn_change_request(struct server *sv,
1387 const char *maxconn_str)
1388{
1389 long int v;
1390 char *end;
1391
1392 if (!*maxconn_str)
1393 return "Require <maxconn>.\n";
1394
1395 v = strtol(maxconn_str, &end, 10);
1396 if (end == maxconn_str)
1397 return "maxconn string empty or preceded by garbage";
1398 else if (end[0] != '\0')
1399 return "Trailing garbage in maxconn string";
1400
1401 if (sv->maxconn == sv->minconn) { // static maxconn
1402 sv->maxconn = sv->minconn = v;
1403 } else { // dynamic maxconn
1404 sv->maxconn = v;
1405 }
1406
1407 if (may_dequeue_tasks(sv, sv->proxy))
1408 process_srv_queue(sv);
1409
1410 return NULL;
1411}
1412
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001413#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001414static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1415 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001416{
1417 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001418 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001419 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001420 NULL,
1421 };
1422
1423 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001424 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001425
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001426 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001427}
1428
1429static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1430{
1431 struct sample_expr *expr;
1432
1433 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 +01001434 if (!expr) {
1435 memprintf(err, "error detected while parsing sni expression : %s", *err);
1436 return ERR_ALERT | ERR_FATAL;
1437 }
1438
1439 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1440 memprintf(err, "error detected while parsing sni expression : "
1441 " fetch method '%s' extracts information from '%s', "
1442 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001443 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001444 return ERR_ALERT | ERR_FATAL;
1445 }
1446
1447 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1448 release_sample_expr(newsrv->ssl_ctx.sni);
1449 newsrv->ssl_ctx.sni = expr;
1450
1451 return 0;
1452}
1453#endif
1454
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001455static 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 +01001456{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001457 char *msg = "error encountered while processing ";
1458 char *quote = "'";
1459 char *token = args[cur_arg];
1460
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001461 if (err && *err) {
1462 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001463 msg = *err;
1464 quote = "";
1465 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001466 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001467
1468 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1469 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1470 file, linenum, args[0], args[1],
1471 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001472 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001473 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1474 file, linenum, args[0], args[1],
1475 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001476}
1477
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001478static void srv_conn_src_sport_range_cpy(struct server *srv,
1479 struct server *src)
1480{
1481 int range_sz;
1482
1483 range_sz = src->conn_src.sport_range->size;
1484 if (range_sz > 0) {
1485 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1486 if (srv->conn_src.sport_range != NULL) {
1487 int i;
1488
1489 for (i = 0; i < range_sz; i++) {
1490 srv->conn_src.sport_range->ports[i] =
1491 src->conn_src.sport_range->ports[i];
1492 }
1493 }
1494 }
1495}
1496
1497/*
1498 * Copy <src> server connection source settings to <srv> server everything needed.
1499 */
1500static void srv_conn_src_cpy(struct server *srv, struct server *src)
1501{
1502 srv->conn_src.opts = src->conn_src.opts;
1503 srv->conn_src.source_addr = src->conn_src.source_addr;
1504
1505 /* Source port range copy. */
1506 if (src->conn_src.sport_range != NULL)
1507 srv_conn_src_sport_range_cpy(srv, src);
1508
1509#ifdef CONFIG_HAP_TRANSPARENT
1510 if (src->conn_src.bind_hdr_name != NULL) {
1511 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1512 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1513 }
1514 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1515 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1516#endif
1517 if (src->conn_src.iface_name != NULL)
1518 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1519}
1520
1521/*
1522 * Copy <src> server SSL settings to <srv> server allocating
1523 * everything needed.
1524 */
1525#if defined(USE_OPENSSL)
1526static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1527{
1528 if (src->ssl_ctx.ca_file != NULL)
1529 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1530 if (src->ssl_ctx.crl_file != NULL)
1531 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001532
1533 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1534
1535 if (src->ssl_ctx.verify_host != NULL)
1536 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1537 if (src->ssl_ctx.ciphers != NULL)
1538 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001539 if (src->ssl_ctx.options)
1540 srv->ssl_ctx.options = src->ssl_ctx.options;
1541 if (src->ssl_ctx.methods.flags)
1542 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1543 if (src->ssl_ctx.methods.min)
1544 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1545 if (src->ssl_ctx.methods.max)
1546 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1547
Ilya Shipitsin2aa4b3a2021-01-07 11:55:45 +05001548#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001549 if (src->ssl_ctx.ciphersuites != NULL)
1550 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1551#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001552 if (src->sni_expr != NULL)
1553 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001554
1555#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1556 if (src->ssl_ctx.alpn_str) {
1557 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1558 if (srv->ssl_ctx.alpn_str) {
1559 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1560 src->ssl_ctx.alpn_len);
1561 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1562 }
1563 }
1564#endif
1565#ifdef OPENSSL_NPN_NEGOTIATED
1566 if (src->ssl_ctx.npn_str) {
1567 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1568 if (srv->ssl_ctx.npn_str) {
1569 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1570 src->ssl_ctx.npn_len);
1571 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1572 }
1573 }
1574#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001575}
1576#endif
1577
1578/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001579 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001580 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001581 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001582 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01001583int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001584{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001585 char *hostname_dn;
1586 int hostname_len, hostname_dn_len;
1587
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001588 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001589 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001590
Christopher Faulet67957bd2017-09-27 11:00:59 +02001591 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001592 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01001593 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
1594 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595 if (hostname_dn_len == -1)
1596 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001597
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001598
Christopher Faulet67957bd2017-09-27 11:00:59 +02001599 free(srv->hostname);
1600 free(srv->hostname_dn);
1601 srv->hostname = strdup(hostname);
1602 srv->hostname_dn = strdup(hostname_dn);
1603 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001604 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001605 goto err;
1606
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001607 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001608
1609 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001610 ha_free(&srv->hostname);
1611 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001612 return -1;
1613}
1614
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001615/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001616 * Copy <src> server settings to <srv> server allocating
1617 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001618 * This function is not supposed to be called at any time, but only
1619 * during server settings parsing or during server allocations from
1620 * a server template, and just after having calloc()'ed a new server.
1621 * So, <src> may only be a default server (when parsing server settings)
1622 * or a server template (during server allocations from a server template).
1623 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1624 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001625 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001626static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001627{
1628 /* Connection source settings copy */
1629 srv_conn_src_cpy(srv, src);
1630
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001631 if (srv_tmpl) {
1632 srv->addr = src->addr;
1633 srv->svc_port = src->svc_port;
1634 }
1635
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001636 srv->pp_opts = src->pp_opts;
1637 if (src->rdr_pfx != NULL) {
1638 srv->rdr_pfx = strdup(src->rdr_pfx);
1639 srv->rdr_len = src->rdr_len;
1640 }
1641 if (src->cookie != NULL) {
1642 srv->cookie = strdup(src->cookie);
1643 srv->cklen = src->cklen;
1644 }
1645 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001646 srv->check.addr = src->check.addr;
1647 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001648 srv->check.use_ssl = src->check.use_ssl;
1649 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001650 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001651 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001652 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001653 /* Note: 'flags' field has potentially been already initialized. */
1654 srv->flags |= src->flags;
1655 srv->do_check = src->do_check;
1656 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001657 srv->check.inter = src->check.inter;
1658 srv->check.fastinter = src->check.fastinter;
1659 srv->check.downinter = src->check.downinter;
1660 srv->agent.use_ssl = src->agent.use_ssl;
1661 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001662
1663 if (src->agent.tcpcheck_rules) {
1664 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1665 if (srv->agent.tcpcheck_rules) {
1666 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1667 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1668 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1669 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1670 &src->agent.tcpcheck_rules->preset_vars);
1671 }
1672 }
1673
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001674 srv->agent.inter = src->agent.inter;
1675 srv->agent.fastinter = src->agent.fastinter;
1676 srv->agent.downinter = src->agent.downinter;
1677 srv->maxqueue = src->maxqueue;
1678 srv->minconn = src->minconn;
1679 srv->maxconn = src->maxconn;
1680 srv->slowstart = src->slowstart;
1681 srv->observe = src->observe;
1682 srv->onerror = src->onerror;
1683 srv->onmarkeddown = src->onmarkeddown;
1684 srv->onmarkedup = src->onmarkedup;
1685 if (src->trackit != NULL)
1686 srv->trackit = strdup(src->trackit);
1687 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1688 srv->uweight = srv->iweight = src->iweight;
1689
1690 srv->check.send_proxy = src->check.send_proxy;
1691 /* health: up, but will fall down at first failure */
1692 srv->check.rise = srv->check.health = src->check.rise;
1693 srv->check.fall = src->check.fall;
1694
1695 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001696 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1697 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1698 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001699 srv->check.state |= CHK_ST_PAUSED;
1700 srv->check.health = 0;
1701 }
1702
1703 /* health: up but will fall down at first failure */
1704 srv->agent.rise = srv->agent.health = src->agent.rise;
1705 srv->agent.fall = src->agent.fall;
1706
1707 if (src->resolvers_id != NULL)
1708 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01001709 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
1710 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
1711 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
1712 if (srv->resolv_opts.family_prio == AF_UNSPEC)
1713 srv->resolv_opts.family_prio = AF_INET6;
1714 memcpy(srv->resolv_opts.pref_net,
1715 src->resolv_opts.pref_net,
1716 sizeof srv->resolv_opts.pref_net);
1717 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001718
1719 srv->init_addr_methods = src->init_addr_methods;
1720 srv->init_addr = src->init_addr;
1721#if defined(USE_OPENSSL)
1722 srv_ssl_settings_cpy(srv, src);
1723#endif
1724#ifdef TCP_USER_TIMEOUT
1725 srv->tcp_ut = src->tcp_ut;
1726#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001727 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001728 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001729 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01001730 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001731 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001732
Olivier Houchard8da5f982017-08-04 18:35:36 +02001733 if (srv_tmpl)
1734 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001735
1736 srv->check.via_socks4 = src->check.via_socks4;
1737 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001738}
1739
Willy Tarreau198e92a2021-03-05 10:23:32 +01001740/* allocate a server and attach it to the global servers_list. Returns
1741 * the server on success, otherwise NULL.
1742 */
William Lallemand313bfd12018-10-26 14:47:32 +02001743struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001744{
1745 struct server *srv;
1746
1747 srv = calloc(1, sizeof *srv);
1748 if (!srv)
1749 return NULL;
1750
1751 srv->obj_type = OBJ_TYPE_SERVER;
1752 srv->proxy = proxy;
Willy Tarreau751153e2021-02-17 13:33:24 +01001753 MT_LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001754 srv->pendconns = EB_ROOT;
Willy Tarreau198e92a2021-03-05 10:23:32 +01001755 LIST_ADDQ(&servers_list, &srv->global_list);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001756
Emeric Brun52a91d32017-08-31 14:41:55 +02001757 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001758 srv->last_change = now.tv_sec;
1759
Christopher Faulet38290462020-04-21 11:46:40 +02001760 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001761 srv->check.status = HCHK_STATUS_INI;
1762 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001763 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001764 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001765
Christopher Faulet38290462020-04-21 11:46:40 +02001766 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001767 srv->agent.status = HCHK_STATUS_INI;
1768 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001769 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001770 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01001771#if defined(USE_QUIC)
1772 srv->cids = EB_ROOT_UNIQUE;
1773#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001774
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01001775 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01001776#ifdef USE_OPENSSL
1777 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
1778#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01001779
Willy Tarreau975b1552019-06-06 16:25:55 +02001780 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01001781 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02001782 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001783 return srv;
1784}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001785
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001786#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1787static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1788 struct server *srv, struct proxy *proxy)
1789{
1790 int ret;
1791 char *err = NULL;
1792
1793 if (!srv->sni_expr)
1794 return 0;
1795
1796 ret = server_parse_sni_expr(srv, proxy, &err);
1797 if (!ret)
1798 return 0;
1799
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001800 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001801 free(err);
1802
1803 return ret;
1804}
1805#endif
1806
1807/*
1808 * Server initializations finalization.
1809 * Initialize health check, agent check and SNI expression if enabled.
1810 * Must not be called for a default server instance.
1811 */
1812static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1813 struct server *srv, struct proxy *px)
1814{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001815#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001816 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001817#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001818
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001819 if (srv->do_check && srv->trackit) {
1820 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1821 file, linenum);
1822 return ERR_ALERT | ERR_FATAL;
1823 }
1824
1825 if (srv->do_agent && !srv->agent.port) {
1826 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1827 file, linenum, srv->id);
1828 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001829 }
1830
1831#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1832 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1833 return ret;
1834#endif
1835
1836 if (srv->flags & SRV_F_BACKUP)
1837 px->srv_bck++;
1838 else
1839 px->srv_act++;
1840 srv_lb_commit_status(srv);
1841
1842 return 0;
1843}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001844
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001845/*
1846 * Parse as much as possible such a range string argument: low[-high]
1847 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1848 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1849 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1850 * Returns 0 if succeeded, -1 if not.
1851 */
1852static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1853{
1854 char *nb_high_arg;
1855
1856 *nb_high = 0;
1857 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001858 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001859
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001860 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001861 *nb_high_arg++ = '\0';
1862 *nb_high = atoi(nb_high_arg);
1863 }
1864 else {
1865 *nb_high += *nb_low;
1866 *nb_low = 1;
1867 }
1868
1869 if (*nb_low < 0 || *nb_high < *nb_low)
1870 return -1;
1871
1872 return 0;
1873}
1874
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001875static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1876{
1877 chunk_printf(&trash, "%s%d", prefix, nb);
1878 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001879 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001880}
1881
1882/*
1883 * Initialize as much as possible servers from <srv> server template.
1884 * Note that a server template is a special server with
1885 * a few different parameters than a server which has
1886 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001887 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001888 * 'srv' template included.
1889 */
1890static int server_template_init(struct server *srv, struct proxy *px)
1891{
1892 int i;
1893 struct server *newsrv;
1894
1895 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 +02001896 newsrv = new_server(px);
1897 if (!newsrv)
1898 goto err;
1899
Christopher Faulet75bef002020-11-02 22:04:55 +01001900 newsrv->conf.file = strdup(srv->conf.file);
1901 newsrv->conf.line = srv->conf.line;
1902
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001903 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001904 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001905#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1906 if (newsrv->sni_expr) {
1907 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1908 if (!newsrv->ssl_ctx.sni)
1909 goto err;
1910 }
1911#endif
1912 /* Set this new server ID. */
1913 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1914
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001915 /* Linked backwards first. This will be restablished after parsing. */
1916 newsrv->next = px->srv;
1917 px->srv = newsrv;
1918 }
1919 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1920
1921 return i - srv->tmpl_info.nb_low;
1922
1923 err:
1924 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1925 if (newsrv) {
1926#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1927 release_sample_expr(newsrv->ssl_ctx.sni);
1928#endif
1929 free_check(&newsrv->agent);
1930 free_check(&newsrv->check);
Willy Tarreau198e92a2021-03-05 10:23:32 +01001931 LIST_DEL(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001932 }
1933 free(newsrv);
1934 return i - srv->tmpl_info.nb_low;
1935}
1936
Emeric Brund3db3842020-07-21 16:54:36 +02001937int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy,
Willy Tarreaubb8669a2021-02-12 12:15:05 +01001938 const struct proxy *defproxy, int parse_addr, int in_peers_section, int initial_resolve)
Willy Tarreau272adea2014-03-31 10:39:59 +02001939{
1940 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001941 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001942 char *errmsg = NULL;
1943 int err_code = 0;
1944 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001945 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001946
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001947 if (strcmp(args[0], "server") == 0 ||
1948 strcmp(args[0], "peer") == 0 ||
1949 strcmp(args[0], "default-server") == 0 ||
1950 strcmp(args[0], "server-template") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001951 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001952 int defsrv = (*args[0] == 'd');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001953 int srv = !defsrv && (*args[0] == 'p' || strcmp(args[0], "server") == 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954 int srv_tmpl = !defsrv && !srv;
1955 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001956
1957 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001958 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001959 err_code |= ERR_ALERT | ERR_FATAL;
1960 goto out;
1961 }
Christopher Faulete3bdc812021-01-13 13:14:13 +01001962 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
1963 err_code |= ERR_ALERT | ERR_FATAL;
1964 goto out;
1965 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001966
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001968 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001969 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001970 if (in_peers_section) {
1971 return 0;
1972 }
1973 else {
1974 /* 'server' line number of argument check. */
1975 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1976 file, linenum, args[0]);
1977 err_code |= ERR_ALERT | ERR_FATAL;
1978 goto out;
1979 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001980 }
1981
1982 err = invalid_char(args[1]);
1983 }
1984 else if (srv_tmpl) {
1985 if (!*args[3]) {
1986 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001987 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 +02001988 file, linenum, args[0]);
1989 err_code |= ERR_ALERT | ERR_FATAL;
1990 goto out;
1991 }
1992
1993 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001994 }
1995
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001996 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001997 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 +02001998 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001999 err_code |= ERR_ALERT | ERR_FATAL;
2000 goto out;
2001 }
2002
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002003 cur_arg = 2;
2004 if (srv_tmpl) {
2005 /* Parse server-template <nb | range> arg. */
2006 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002007 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002008 file, linenum, args[0], args[cur_arg]);
2009 err_code |= ERR_ALERT | ERR_FATAL;
2010 goto out;
2011 }
2012 cur_arg++;
2013 }
2014
Willy Tarreau272adea2014-03-31 10:39:59 +02002015 if (!defsrv) {
2016 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002017 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002018
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002019 newsrv = new_server(curproxy);
2020 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002021 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002022 err_code |= ERR_ALERT | ERR_ABORT;
2023 goto out;
2024 }
2025
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002026 if (srv_tmpl) {
2027 newsrv->tmpl_info.nb_low = tmpl_range_low;
2028 newsrv->tmpl_info.nb_high = tmpl_range_high;
2029 }
2030
Willy Tarreau272adea2014-03-31 10:39:59 +02002031 /* the servers are linked backwards first */
2032 newsrv->next = curproxy->srv;
2033 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002034 newsrv->conf.file = strdup(file);
2035 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002036 /* Note: for a server template, its id is its prefix.
2037 * This is a temporary id which will be used for server allocations to come
2038 * after parsing.
2039 */
2040 if (srv)
2041 newsrv->id = strdup(args[1]);
2042 else
2043 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002044
2045 /* several ways to check the port component :
2046 * - IP => port=+0, relative (IPv4 only)
2047 * - IP: => port=+0, relative
2048 * - IP:N => port=N, absolute
2049 * - IP:+N => port=+N, relative
2050 * - IP:-N => port=-N, relative
2051 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002052 if (!parse_addr)
2053 goto skip_addr;
2054
Willy Tarreau65ec4e32020-09-16 19:17:08 +02002055 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, NULL, NULL,
2056 &errmsg, NULL, &fqdn,
2057 (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 +02002058 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002059 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002060 err_code |= ERR_ALERT | ERR_FATAL;
2061 goto out;
2062 }
2063
Willy Tarreau272adea2014-03-31 10:39:59 +02002064 if (!port1 || !port2) {
2065 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002066 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002067 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002068
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002069 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002070 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002071 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002072 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002073 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
Emeric Brun08622d32020-12-23 17:41:43 +01002074 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002075 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002076 err_code |= ERR_ALERT | ERR_FATAL;
2077 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002078 }
2079 }
2080 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002081 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002082 file, linenum, newsrv->id);
2083 err_code |= ERR_ALERT | ERR_FATAL;
2084 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002085 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002086 }
2087
Willy Tarreau272adea2014-03-31 10:39:59 +02002088 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002089 newsrv->svc_port = port;
Thayne McCombs92149f92020-11-20 01:28:26 -07002090 // we don't need to lock the server here, because
2091 // we are in the process of initializing
2092 srv_set_addr_desc(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002093
Olivier Houchard8da5f982017-08-04 18:35:36 +02002094 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002095 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002096 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002097 err_code |= ERR_ALERT | ERR_FATAL;
2098 goto out;
2099 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002100
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002101 cur_arg++;
2102 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002103 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002104 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002105 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 } else {
2107 newsrv = &curproxy->defsrv;
2108 cur_arg = 1;
Emeric Brun21fbeed2020-12-23 18:01:04 +01002109 newsrv->resolv_opts.family_prio = AF_INET6;
2110 newsrv->resolv_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002111 }
2112
2113 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002114 if (strcmp(args[cur_arg], "init-addr") == 0) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002115 char *p, *end;
2116 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002117 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002118
2119 newsrv->init_addr_methods = 0;
2120 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2121
2122 for (p = args[cur_arg + 1]; *p; p = end) {
2123 /* cut on next comma */
2124 for (end = p; *end && *end != ','; end++);
2125 if (*end)
2126 *(end++) = 0;
2127
Willy Tarreau4310d362016-11-02 15:05:56 +01002128 memset(&sa, 0, sizeof(sa));
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002129 if (strcmp(p, "libc") == 0) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002130 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2131 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002132 else if (strcmp(p, "last") == 0) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002133 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2134 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002135 else if (strcmp(p, "none") == 0) {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002136 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2137 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002138 else if (str2ip2(p, &sa, 0)) {
2139 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002140 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002141 file, linenum, args[cur_arg], p);
2142 err_code |= ERR_ALERT | ERR_FATAL;
2143 goto out;
2144 }
2145 newsrv->init_addr = sa;
2146 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2147 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002148 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002149 ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002150 file, linenum, args[cur_arg], p);
2151 err_code |= ERR_ALERT | ERR_FATAL;
2152 goto out;
2153 }
2154 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002155 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002156 file, linenum, args[cur_arg], p);
2157 err_code |= ERR_ALERT | ERR_FATAL;
2158 goto out;
2159 }
2160 }
2161 cur_arg += 2;
2162 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002163 else if (strcmp(args[cur_arg], "resolvers") == 0) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002164 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002165 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2166 cur_arg += 2;
2167 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002168 else if (strcmp(args[cur_arg], "resolve-opts") == 0) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002169 char *p, *end;
2170
2171 for (p = args[cur_arg + 1]; *p; p = end) {
2172 /* cut on next comma */
2173 for (end = p; *end && *end != ','; end++);
2174 if (*end)
2175 *(end++) = 0;
2176
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002177 if (strcmp(p, "allow-dup-ip") == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002178 newsrv->resolv_opts.accept_duplicate_ip = 1;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002179 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002180 else if (strcmp(p, "ignore-weight") == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002181 newsrv->resolv_opts.ignore_weight = 1;
Daniel Corbettf8716912019-11-17 09:48:56 -05002182 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002183 else if (strcmp(p, "prevent-dup-ip") == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002184 newsrv->resolv_opts.accept_duplicate_ip = 0;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002185 }
2186 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002187 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.\n",
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002188 file, linenum, args[cur_arg], p);
2189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
2191 }
2192 }
2193
2194 cur_arg += 2;
2195 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002196 else if (strcmp(args[cur_arg], "resolve-prefer") == 0) {
2197 if (strcmp(args[cur_arg + 1], "ipv4") == 0)
Emeric Brun21fbeed2020-12-23 18:01:04 +01002198 newsrv->resolv_opts.family_prio = AF_INET;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002199 else if (strcmp(args[cur_arg + 1], "ipv6") == 0)
Emeric Brun21fbeed2020-12-23 18:01:04 +01002200 newsrv->resolv_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002201 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002202 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002203 file, linenum, args[cur_arg]);
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207 cur_arg += 2;
2208 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002209 else if (strcmp(args[cur_arg], "resolve-net") == 0) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002210 char *p, *e;
2211 unsigned char mask;
Emeric Brun21fbeed2020-12-23 18:01:04 +01002212 struct resolv_options *opt;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002213
2214 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002215 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002216 file, linenum, args[cur_arg]);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220
Emeric Brun21fbeed2020-12-23 18:01:04 +01002221 opt = &newsrv->resolv_opts;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002222
2223 /* Split arguments by comma, and convert it from ipv4 or ipv6
2224 * string network in in_addr or in6_addr.
2225 */
2226 p = args[cur_arg + 1];
2227 e = p;
2228 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002229 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002230 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002231 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002232 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2233 err_code |= ERR_ALERT | ERR_FATAL;
2234 goto out;
2235 }
2236 /* look for end or comma. */
2237 while (*e != ',' && *e != '\0')
2238 e++;
2239 if (*e == ',') {
2240 *e = '\0';
2241 e++;
2242 }
2243 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2244 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2245 /* Try to convert input string from ipv4 or ipv6 network. */
2246 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2247 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2248 &mask)) {
2249 /* Try to convert input string from ipv6 network. */
2250 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2251 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2252 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002253 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002254 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002255 file, linenum, args[cur_arg], p);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto out;
2258 }
2259 opt->pref_net_nb++;
2260 p = e;
2261 }
2262
2263 cur_arg += 2;
2264 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002265 else if (strcmp(args[cur_arg], "weight") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002266 int w;
2267 w = atol(args[cur_arg + 1]);
2268 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002269 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002270 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2271 err_code |= ERR_ALERT | ERR_FATAL;
2272 goto out;
2273 }
2274 newsrv->uweight = newsrv->iweight = w;
2275 cur_arg += 2;
2276 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002277 else if (strcmp(args[cur_arg], "log-proto") == 0) {
2278 if (strcmp(args[cur_arg + 1], "legacy") == 0)
Emeric Brun97556472020-05-30 01:42:45 +02002279 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002280 else if (strcmp(args[cur_arg + 1], "octet-count") == 0)
Emeric Brun97556472020-05-30 01:42:45 +02002281 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2282 else {
2283 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2284 "'octet-count' but got '%s'\n",
2285 file, linenum, args[cur_arg], args[cur_arg + 1]);
2286 err_code |= ERR_ALERT | ERR_FATAL;
2287 goto out;
2288 }
2289 cur_arg += 2;
2290 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002291 else if (strcmp(args[cur_arg], "minconn") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002292 newsrv->minconn = atol(args[cur_arg + 1]);
2293 cur_arg += 2;
2294 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002295 else if (strcmp(args[cur_arg], "maxconn") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002296 newsrv->maxconn = atol(args[cur_arg + 1]);
2297 cur_arg += 2;
2298 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002299 else if (strcmp(args[cur_arg], "maxqueue") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002300 newsrv->maxqueue = atol(args[cur_arg + 1]);
2301 cur_arg += 2;
2302 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002303 else if (strcmp(args[cur_arg], "slowstart") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002304 /* slowstart is stored in seconds */
2305 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002306
2307 if (err == PARSE_TIME_OVER) {
2308 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2309 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2310 err_code |= ERR_ALERT | ERR_FATAL;
2311 goto out;
2312 }
2313 else if (err == PARSE_TIME_UNDER) {
2314 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2315 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2316 err_code |= ERR_ALERT | ERR_FATAL;
2317 goto out;
2318 }
2319 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002320 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002321 file, linenum, *err, newsrv->id);
2322 err_code |= ERR_ALERT | ERR_FATAL;
2323 goto out;
2324 }
2325 newsrv->slowstart = (val + 999) / 1000;
2326 cur_arg += 2;
2327 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002328 else if (strcmp(args[cur_arg], "on-error") == 0) {
2329 if (strcmp(args[cur_arg + 1], "fastinter") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 newsrv->onerror = HANA_ONERR_FASTINTER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002331 else if (strcmp(args[cur_arg + 1], "fail-check") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002332 newsrv->onerror = HANA_ONERR_FAILCHK;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002333 else if (strcmp(args[cur_arg + 1], "sudden-death") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002334 newsrv->onerror = HANA_ONERR_SUDDTH;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002335 else if (strcmp(args[cur_arg + 1], "mark-down") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002336 newsrv->onerror = HANA_ONERR_MARKDWN;
2337 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002338 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002339 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2340 file, linenum, args[cur_arg], args[cur_arg + 1]);
2341 err_code |= ERR_ALERT | ERR_FATAL;
2342 goto out;
2343 }
2344
2345 cur_arg += 2;
2346 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002347 else if (strcmp(args[cur_arg], "on-marked-down") == 0) {
2348 if (strcmp(args[cur_arg + 1], "shutdown-sessions") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002349 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2350 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002351 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002352 file, linenum, args[cur_arg], args[cur_arg + 1]);
2353 err_code |= ERR_ALERT | ERR_FATAL;
2354 goto out;
2355 }
2356
2357 cur_arg += 2;
2358 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002359 else if (strcmp(args[cur_arg], "on-marked-up") == 0) {
2360 if (strcmp(args[cur_arg + 1], "shutdown-backup-sessions") == 0)
Willy Tarreau272adea2014-03-31 10:39:59 +02002361 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2362 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002363 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002364 file, linenum, args[cur_arg], args[cur_arg + 1]);
2365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368
2369 cur_arg += 2;
2370 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002371 else if (strcmp(args[cur_arg], "error-limit") == 0) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002372 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002373 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002374 file, linenum, args[cur_arg]);
2375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
2377 }
2378
2379 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2380
2381 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002382 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002383 file, linenum, args[cur_arg]);
2384 err_code |= ERR_ALERT | ERR_FATAL;
2385 goto out;
2386 }
2387 cur_arg += 2;
2388 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002389 else if (strcmp(args[cur_arg], "usesrc") == 0) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 file, linenum, "usesrc", "source");
2392 err_code |= ERR_ALERT | ERR_FATAL;
2393 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002394 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002395 else {
2396 static int srv_dumped;
2397 struct srv_kw *kw;
2398 char *err;
2399
2400 kw = srv_find_kw(args[cur_arg]);
2401 if (kw) {
2402 char *err = NULL;
2403 int code;
2404
2405 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002406 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002407 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002408 if (kw->skip != -1)
2409 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002410 err_code |= ERR_ALERT | ERR_FATAL;
2411 goto out;
2412 }
2413
2414 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002415 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002416 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002417 if (kw->skip != -1)
2418 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 err_code |= ERR_ALERT;
2420 continue;
2421 }
2422
2423 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2424 err_code |= code;
2425
2426 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002427 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002428 if (code & ERR_FATAL) {
2429 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002430 if (kw->skip != -1)
2431 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002432 goto out;
2433 }
2434 }
2435 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002436 if (kw->skip != -1)
2437 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002438 continue;
2439 }
2440
2441 err = NULL;
2442 if (!srv_dumped) {
2443 srv_dump_kws(&err);
2444 indent_msg(&err, 4);
2445 srv_dumped = 1;
2446 }
2447
Christopher Faulet767a84b2017-11-24 16:50:31 +01002448 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002449 file, linenum, args[0], args[1], args[cur_arg],
2450 err ? " Registered keywords :" : "", err ? err : "");
2451 free(err);
2452
2453 err_code |= ERR_ALERT | ERR_FATAL;
2454 goto out;
2455 }
2456 }
2457
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002458 if (!defsrv)
2459 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2460 if (err_code & ERR_FATAL)
2461 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002462 if (srv_tmpl)
2463 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002465 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002466 return 0;
2467
2468 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002469 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002470 free(errmsg);
2471 return err_code;
2472}
2473
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002474/* Returns a pointer to the first server matching either id <id>.
2475 * NULL is returned if no match is found.
2476 * the lookup is performed in the backend <bk>
2477 */
2478struct server *server_find_by_id(struct proxy *bk, int id)
2479{
2480 struct eb32_node *eb32;
2481 struct server *curserver;
2482
2483 if (!bk || (id ==0))
2484 return NULL;
2485
2486 /* <bk> has no backend capabilities, so it can't have a server */
2487 if (!(bk->cap & PR_CAP_BE))
2488 return NULL;
2489
2490 curserver = NULL;
2491
2492 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2493 if (eb32)
2494 curserver = container_of(eb32, struct server, conf.id);
2495
2496 return curserver;
2497}
2498
2499/* Returns a pointer to the first server matching either name <name>, or id
2500 * if <name> starts with a '#'. NULL is returned if no match is found.
2501 * the lookup is performed in the backend <bk>
2502 */
2503struct server *server_find_by_name(struct proxy *bk, const char *name)
2504{
2505 struct server *curserver;
2506
2507 if (!bk || !name)
2508 return NULL;
2509
2510 /* <bk> has no backend capabilities, so it can't have a server */
2511 if (!(bk->cap & PR_CAP_BE))
2512 return NULL;
2513
2514 curserver = NULL;
2515 if (*name == '#') {
2516 curserver = server_find_by_id(bk, atoi(name + 1));
2517 if (curserver)
2518 return curserver;
2519 }
2520 else {
2521 curserver = bk->srv;
2522
2523 while (curserver && (strcmp(curserver->id, name) != 0))
2524 curserver = curserver->next;
2525
2526 if (curserver)
2527 return curserver;
2528 }
2529
2530 return NULL;
2531}
2532
2533struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2534{
2535 struct server *byname;
2536 struct server *byid;
2537
2538 if (!name && !id)
2539 return NULL;
2540
2541 if (diff)
2542 *diff = 0;
2543
2544 byname = byid = NULL;
2545
2546 if (name) {
2547 byname = server_find_by_name(bk, name);
2548 if (byname && (!id || byname->puid == id))
2549 return byname;
2550 }
2551
2552 /* remaining possibilities :
2553 * - name not set
2554 * - name set but not found
2555 * - name found but ID doesn't match
2556 */
2557 if (id) {
2558 byid = server_find_by_id(bk, id);
2559 if (byid) {
2560 if (byname) {
2561 /* use id only if forced by configuration */
2562 if (byid->flags & SRV_F_FORCED_ID) {
2563 if (diff)
2564 *diff |= 2;
2565 return byid;
2566 }
2567 else {
2568 if (diff)
2569 *diff |= 1;
2570 return byname;
2571 }
2572 }
2573
2574 /* remaining possibilities:
2575 * - name not set
2576 * - name set but not found
2577 */
2578 if (name && diff)
2579 *diff |= 2;
2580 return byid;
2581 }
2582
2583 /* id bot found */
2584 if (byname) {
2585 if (diff)
2586 *diff |= 1;
2587 return byname;
2588 }
2589 }
2590
2591 return NULL;
2592}
2593
Simon Horman7d09b9a2013-02-12 10:45:51 +09002594/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002595 * update a server's current IP address.
2596 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2597 * ip is in network format.
2598 * updater is a string which contains an information about the requester of the update.
2599 * updater is used if not NULL.
2600 *
2601 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02002602 *
2603 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02002604 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002605int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002606{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02002607 /* save the new IP family & address if necessary */
2608 switch (ip_sin_family) {
2609 case AF_INET:
2610 if (s->addr.ss_family == ip_sin_family &&
2611 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
2612 return 0;
2613 break;
2614 case AF_INET6:
2615 if (s->addr.ss_family == ip_sin_family &&
2616 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
2617 return 0;
2618 break;
2619 };
2620
Baptiste Assmann14e40142015-04-14 01:13:07 +02002621 /* generates a log line and a warning on stderr */
2622 if (1) {
2623 /* book enough space for both IPv4 and IPv6 */
2624 char oldip[INET6_ADDRSTRLEN];
2625 char newip[INET6_ADDRSTRLEN];
2626
2627 memset(oldip, '\0', INET6_ADDRSTRLEN);
2628 memset(newip, '\0', INET6_ADDRSTRLEN);
2629
2630 /* copy old IP address in a string */
2631 switch (s->addr.ss_family) {
2632 case AF_INET:
2633 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2634 break;
2635 case AF_INET6:
2636 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2637 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02002638 default:
2639 strcpy(oldip, "(none)");
2640 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02002641 };
2642
2643 /* copy new IP address in a string */
2644 switch (ip_sin_family) {
2645 case AF_INET:
2646 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2647 break;
2648 case AF_INET6:
2649 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2650 break;
2651 };
2652
2653 /* save log line into a buffer */
2654 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2655 s->proxy->id, s->id, oldip, newip, updater);
2656
2657 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002659
2660 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002661 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002662 }
2663
2664 /* save the new IP family */
2665 s->addr.ss_family = ip_sin_family;
2666 /* save the new IP address */
2667 switch (ip_sin_family) {
2668 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002669 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002670 break;
2671 case AF_INET6:
2672 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2673 break;
2674 };
Olivier Houchard4e694042017-03-14 20:01:29 +01002675 srv_set_dyncookie(s);
Thayne McCombs92149f92020-11-20 01:28:26 -07002676 srv_set_addr_desc(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002677
2678 return 0;
2679}
2680
William Dauchy7cabc062021-02-11 22:51:24 +01002681/* update agent health check address and port
2682 * addr can be ip4/ip6 or a hostname
2683 * if one error occurs, don't apply anything
2684 * must be called with the server lock held.
2685 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002686const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01002687{
2688 struct sockaddr_storage sk;
2689 struct buffer *msg;
2690 int new_port;
2691
2692 msg = get_trash_chunk();
2693 chunk_reset(msg);
2694
2695 if (!(s->agent.state & CHK_ST_ENABLED)) {
2696 chunk_strcat(msg, "agent checks are not enabled on this server");
2697 goto out;
2698 }
2699 if (addr) {
2700 memset(&sk, 0, sizeof(struct sockaddr_storage));
2701 if (str2ip(addr, &sk) == NULL) {
2702 chunk_appendf(msg, "invalid addr '%s'", addr);
2703 goto out;
2704 }
2705 }
2706 if (port) {
2707 if (strl2irc(port, strlen(port), &new_port) != 0) {
2708 chunk_appendf(msg, "provided port is not an integer");
2709 goto out;
2710 }
2711 if (new_port < 0 || new_port > 65535) {
2712 chunk_appendf(msg, "provided port is invalid");
2713 goto out;
2714 }
2715 }
2716out:
2717 if (msg->data)
2718 return msg->area;
2719 else {
2720 if (addr)
2721 set_srv_agent_addr(s, &sk);
2722 if (port)
2723 set_srv_agent_port(s, new_port);
2724 }
2725 return NULL;
2726}
2727
William Dauchyb456e1f2021-02-11 22:51:23 +01002728/* update server health check address and port
2729 * addr must be ip4 or ip6, it won't be resolved
2730 * if one error occurs, don't apply anything
2731 * must be called with the server lock held.
2732 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002733const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01002734{
2735 struct sockaddr_storage sk;
2736 struct buffer *msg;
2737 int new_port;
2738
2739 msg = get_trash_chunk();
2740 chunk_reset(msg);
2741
2742 if (!(s->check.state & CHK_ST_ENABLED)) {
2743 chunk_strcat(msg, "health checks are not enabled on this server");
2744 goto out;
2745 }
2746 if (addr) {
2747 memset(&sk, 0, sizeof(struct sockaddr_storage));
2748 if (str2ip2(addr, &sk, 0) == NULL) {
2749 chunk_appendf(msg, "invalid addr '%s'", addr);
2750 goto out;
2751 }
2752 }
2753 if (port) {
2754 if (strl2irc(port, strlen(port), &new_port) != 0) {
2755 chunk_appendf(msg, "provided port is not an integer");
2756 goto out;
2757 }
2758 if (new_port < 0 || new_port > 65535) {
2759 chunk_appendf(msg, "provided port is invalid");
2760 goto out;
2761 }
2762 /* prevent the update of port to 0 if MAPPORTS are in use */
2763 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
2764 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
2765 goto out;
2766 }
2767 }
2768out:
2769 if (msg->data)
2770 return msg->area;
2771 else {
2772 if (addr)
2773 s->check.addr = sk;
2774 if (port)
2775 s->check.port = new_port;
2776 }
2777 return NULL;
2778}
2779
Baptiste Assmann14e40142015-04-14 01:13:07 +02002780/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002781 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
2782 *
2783 * Caller can pass its name through <updater> to get it integrated in the response message
2784 * returned by the function.
2785 *
2786 * The function first does the following, in that order:
2787 * - validates the new addr and/or port
2788 * - checks if an update is required (new IP or port is different than current ones)
2789 * - checks the update is allowed:
2790 * - don't switch from/to a family other than AF_INET4 and AF_INET6
2791 * - allow all changes if no CHECKS are configured
2792 * - if CHECK is configured:
2793 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
2794 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
2795 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02002796 *
2797 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002798 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002799const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002800{
2801 struct sockaddr_storage sa;
2802 int ret, port_change_required;
2803 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00002804 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02002805 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01002806 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002807
2808 msg = get_trash_chunk();
2809 chunk_reset(msg);
2810
2811 if (addr) {
2812 memset(&sa, 0, sizeof(struct sockaddr_storage));
2813 if (str2ip2(addr, &sa, 0) == NULL) {
2814 chunk_printf(msg, "Invalid addr '%s'", addr);
2815 goto out;
2816 }
2817
2818 /* changes are allowed on AF_INET* families only */
2819 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
2820 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
2821 goto out;
2822 }
2823
2824 /* collecting data currently setup */
2825 memset(current_addr, '\0', sizeof(current_addr));
2826 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
2827 /* changes are allowed on AF_INET* families only */
2828 if ((ret != AF_INET) && (ret != AF_INET6)) {
2829 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
2830 goto out;
2831 }
2832
2833 /* applying ADDR changes if required and allowed
2834 * ipcmp returns 0 when both ADDR are the same
2835 */
2836 if (ipcmp(&s->addr, &sa) == 0) {
2837 chunk_appendf(msg, "no need to change the addr");
2838 goto port;
2839 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002840 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01002841 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002842
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002843 /* update report for caller */
2844 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
2845 }
2846
2847 port:
2848 if (port) {
2849 char sign = '\0';
2850 char *endptr;
2851
2852 if (addr)
2853 chunk_appendf(msg, ", ");
2854
2855 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01002856 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002857
2858 /* check if PORT change is required */
2859 port_change_required = 0;
2860
2861 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04002862 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002863 new_port = strtol(port, &endptr, 10);
2864 if ((errno != 0) || (port == endptr)) {
2865 chunk_appendf(msg, "problem converting port '%s' to an int", port);
2866 goto out;
2867 }
2868
2869 /* check if caller triggers a port mapped or offset */
2870 if (sign == '-' || (sign == '+')) {
2871 /* check if server currently uses port map */
2872 if (!(s->flags & SRV_F_MAPPORTS)) {
2873 /* switch from fixed port to port map mandatorily triggers
2874 * a port change */
2875 port_change_required = 1;
2876 /* check is configured
2877 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
2878 * prevent PORT change if check doesn't have it's dedicated port while switching
2879 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01002880 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002881 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.");
2882 goto out;
2883 }
2884 }
2885 /* we're already using port maps */
2886 else {
2887 port_change_required = current_port != new_port;
2888 }
2889 }
2890 /* fixed port */
2891 else {
2892 port_change_required = current_port != new_port;
2893 }
2894
2895 /* applying PORT changes if required and update response message */
2896 if (port_change_required) {
2897 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01002898 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01002899 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002900
2901 /* prepare message */
2902 chunk_appendf(msg, "port changed from '");
2903 if (s->flags & SRV_F_MAPPORTS)
2904 chunk_appendf(msg, "+");
2905 chunk_appendf(msg, "%d' to '", current_port);
2906
2907 if (sign == '-') {
2908 s->flags |= SRV_F_MAPPORTS;
2909 chunk_appendf(msg, "%c", sign);
2910 /* just use for result output */
2911 new_port = -new_port;
2912 }
2913 else if (sign == '+') {
2914 s->flags |= SRV_F_MAPPORTS;
2915 chunk_appendf(msg, "%c", sign);
2916 }
2917 else {
2918 s->flags &= ~SRV_F_MAPPORTS;
2919 }
2920
2921 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002922 }
2923 else {
2924 chunk_appendf(msg, "no need to change the port");
2925 }
2926 }
2927
2928out:
William Dauchy6318d332020-05-02 21:52:36 +02002929 if (changed) {
2930 /* force connection cleanup on the given server */
2931 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01002932 srv_set_dyncookie(s);
Thayne McCombs92149f92020-11-20 01:28:26 -07002933 srv_set_addr_desc(s);
William Dauchy6318d332020-05-02 21:52:36 +02002934 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002935 if (updater)
2936 chunk_appendf(msg, " by '%s'", updater);
2937 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002938 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02002939}
2940
2941
2942/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002943 * update server status based on result of name resolution
2944 * returns:
2945 * 0 if server status is updated
2946 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02002947 *
2948 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002949 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002950int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002951{
Emeric Brun750fe792020-12-23 16:51:12 +01002952 struct resolvers *resolvers = s->resolvers;
Emeric Brun08622d32020-12-23 17:41:43 +01002953 struct resolv_resolution *resolution = s->resolv_requester->resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002954 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002955
Jerome Magnin012261a2020-07-28 13:38:22 +02002956 /* If resolution is NULL we're dealing with SRV records Additional records */
2957 if (resolution == NULL) {
Baptiste Assmann87138c32020-08-04 10:57:21 +02002958 /* since this server has an IP, it can go back in production */
2959 if (has_no_ip == 0) {
2960 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2961 return 1;
2962 }
2963
Jerome Magnin012261a2020-07-28 13:38:22 +02002964 if (s->next_admin & SRV_ADMF_RMAINT)
2965 return 1;
2966
2967 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
2968 return 0;
2969 }
2970
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002971 switch (resolution->status) {
2972 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002973 /* status when HAProxy has just (re)started.
2974 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002975 break;
2976
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002977 case RSLV_STATUS_VALID:
2978 /*
2979 * resume health checks
2980 * server will be turned back on if health check is safe
2981 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002982 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02002983 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002984 return 1;
2985 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
2986 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02002987 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002988 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002989
Emeric Brun52a91d32017-08-31 14:41:55 +02002990 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002991 return 1;
2992 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
2993 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
2994 s->proxy->id, s->id);
2995
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002996 ha_warning("%s.\n", trash.area);
2997 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01002998 return 0;
2999
3000 case RSLV_STATUS_NX:
3001 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003002 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3003 if (!tick_is_expired(exp, now_ms))
3004 break;
3005
3006 if (s->next_admin & SRV_ADMF_RMAINT)
3007 return 1;
3008 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3009 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003010
3011 case RSLV_STATUS_TIMEOUT:
3012 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003013 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3014 if (!tick_is_expired(exp, now_ms))
3015 break;
3016
3017 if (s->next_admin & SRV_ADMF_RMAINT)
3018 return 1;
3019 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3020 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003021
3022 case RSLV_STATUS_REFUSED:
3023 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003024 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3025 if (!tick_is_expired(exp, now_ms))
3026 break;
3027
3028 if (s->next_admin & SRV_ADMF_RMAINT)
3029 return 1;
3030 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3031 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003032
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003033 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003034 /* stop server if resolution failed for a long enough period */
3035 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3036 if (!tick_is_expired(exp, now_ms))
3037 break;
3038
3039 if (s->next_admin & SRV_ADMF_RMAINT)
3040 return 1;
3041 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3042 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003043 }
3044
3045 return 1;
3046}
3047
3048/*
3049 * Server Name Resolution valid response callback
3050 * It expects:
3051 * - <nameserver>: the name server which answered the valid response
3052 * - <response>: buffer containing a valid DNS response
3053 * - <response_len>: size of <response>
3054 * It performs the following actions:
3055 * - ignore response if current ip found and server family not met
3056 * - update with first new ip found if family is met and current IP is not found
3057 * returns:
3058 * 0 on error
3059 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003060 *
3061 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003062 */
Emeric Brun08622d32020-12-23 17:41:43 +01003063int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003064{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003065 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003066 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003067 void *serverip, *firstip;
3068 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003069 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003070 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003071 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003072
Christopher Faulet67957bd2017-09-27 11:00:59 +02003073 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003074 if (!s)
3075 return 1;
3076
Emeric Brun08622d32020-12-23 17:41:43 +01003077 resolution = s->resolv_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003078
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003079 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003080 firstip = NULL; /* pointer to the first valid response found */
3081 /* it will be used as the new IP if a change is required */
3082 firstip_sin_family = AF_UNSPEC;
3083 serverip = NULL; /* current server IP address */
3084
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003085 /* initializing server IP pointer */
3086 server_sin_family = s->addr.ss_family;
3087 switch (server_sin_family) {
3088 case AF_INET:
3089 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3090 break;
3091
3092 case AF_INET6:
3093 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3094 break;
3095
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003096 case AF_UNSPEC:
3097 break;
3098
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003099 default:
3100 goto invalid;
3101 }
3102
Emeric Brund30e9a12020-12-23 18:49:16 +01003103 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3104 serverip, server_sin_family, &firstip,
3105 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003106
3107 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003108 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003109 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003110
Emeric Brun456de772020-12-23 18:17:31 +01003111 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003112 goto save_ip;
3113
Emeric Brun456de772020-12-23 18:17:31 +01003114 case RSLV_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003115 goto invalid;
3116
Emeric Brun456de772020-12-23 18:17:31 +01003117 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003118 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003119 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003120
Emeric Brun456de772020-12-23 18:17:31 +01003121 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003122 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003123 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003124 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003125
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003126 default:
3127 goto invalid;
3128
3129 }
3130
3131 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003132 if (counters) {
3133 counters->update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003134 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003135 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003136 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003137 else
3138 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003139 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003140
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003141 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003142 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003143 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003144
3145 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003146 if (counters) {
3147 counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003148 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003149 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003150 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003151 return 0;
3152}
3153
3154/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003155 * SRV record error management callback
3156 * returns:
3157 * 0 on error
3158 * 1 when no error or safe ignore
3159 *
3160 * Grabs the server's lock.
3161 */
3162int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3163{
3164 struct server *s;
3165 struct resolv_srvrq *srvrq;
3166 struct resolv_resolution *res;
3167 struct resolvers *resolvers;
3168 int exp;
3169
3170 /* SRV records */
3171 srvrq = objt_resolv_srvrq(requester->owner);
3172 if (!srvrq)
3173 return 1;
3174
3175 resolvers = srvrq->resolvers;
3176 res = requester->resolution;
3177
3178 switch (res->status) {
3179
3180 case RSLV_STATUS_NX:
3181 /* stop server if resolution is NX for a long enough period */
3182 exp = tick_add(res->last_valid, resolvers->hold.nx);
3183 if (!tick_is_expired(exp, now_ms))
3184 return 1;
3185 break;
3186
3187 case RSLV_STATUS_TIMEOUT:
3188 /* stop server if resolution is TIMEOUT for a long enough period */
3189 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3190 if (!tick_is_expired(exp, now_ms))
3191 return 1;
3192 break;
3193
3194 case RSLV_STATUS_REFUSED:
3195 /* stop server if resolution is REFUSED for a long enough period */
3196 exp = tick_add(res->last_valid, resolvers->hold.refused);
3197 if (!tick_is_expired(exp, now_ms))
3198 return 1;
3199 break;
3200
3201 default:
3202 /* stop server if resolution failed for a long enough period */
3203 exp = tick_add(res->last_valid, resolvers->hold.other);
3204 if (!tick_is_expired(exp, now_ms))
3205 return 1;
3206 }
3207
3208 /* Remove any associated server */
3209 for (s = srvrq->proxy->srv; s != NULL; s = s->next) {
3210 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
3211 if (s->srvrq == srvrq) {
3212 snr_update_srv_status(s, 1);
3213 free(s->hostname);
3214 free(s->hostname_dn);
3215 s->hostname = NULL;
3216 s->hostname_dn = NULL;
3217 s->hostname_dn_len = 0;
Christopher Faulet52d4d302021-02-23 12:24:09 +01003218 memset(&s->addr, 0, sizeof(s->addr));
3219 s->svc_port = 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003220 resolv_unlink_resolution(s->resolv_requester);
3221 }
3222 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
3223 }
3224
3225 return 1;
3226}
3227
3228/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003229 * Server Name Resolution error management callback
3230 * returns:
3231 * 0 on error
3232 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003233 *
3234 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003235 */
Emeric Brun08622d32020-12-23 17:41:43 +01003236int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003237{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003238 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003239
Christopher Faulet67957bd2017-09-27 11:00:59 +02003240 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003241 if (!s)
3242 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003243 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003244 snr_update_srv_status(s, 0);
Christopher Fauletd127ffa2021-02-23 12:33:17 +01003245 memset(&s->addr, 0, sizeof(s->addr));
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003246 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003247 return 1;
3248}
3249
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003250/*
3251 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003252 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003253 * It returns a pointer to the first server found or NULL if <ip> is not yet
3254 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003255 *
3256 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003257 */
3258struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3259{
3260 struct server *tmpsrv;
3261 struct proxy *be;
3262
3263 if (!srv)
3264 return NULL;
3265
3266 be = srv->proxy;
3267 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003268 /* we found the current server is the same, ignore it */
3269 if (srv == tmpsrv)
3270 continue;
3271
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003272 /* We want to compare the IP in the record with the IP of the servers in the
3273 * same backend, only if:
3274 * * DNS resolution is enabled on the server
3275 * * the hostname used for the resolution by our server is the same than the
3276 * one used for the server found in the backend
3277 * * the server found in the backend is not our current server
3278 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003279 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003280 if ((tmpsrv->hostname_dn == NULL) ||
3281 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3282 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003283 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003284 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003285 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003286 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003287
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003288 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003289 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003290 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003291 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003292 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003293
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003294 /* At this point, we have 2 different servers using the same DNS hostname
3295 * for their respective resolution.
3296 */
3297 if (*ip_family == tmpsrv->addr.ss_family &&
3298 ((tmpsrv->addr.ss_family == AF_INET &&
3299 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3300 (tmpsrv->addr.ss_family == AF_INET6 &&
3301 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003302 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003303 return tmpsrv;
3304 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003305 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003306 }
3307
Emeric Brune9fd6b52017-11-02 17:20:39 +01003308
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003309 return NULL;
3310}
3311
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003312/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3313 * resolver. This is suited for initial address configuration. Returns 0 on
3314 * success otherwise a non-zero error code. In case of error, *err_code, if
3315 * not NULL, is filled up.
3316 */
3317int srv_set_addr_via_libc(struct server *srv, int *err_code)
3318{
3319 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003320 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003321 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003322 return 1;
3323 }
3324 return 0;
3325}
3326
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003327/* Set the server's FDQN (->hostname) from <hostname>.
3328 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003329 *
3330 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003331 */
Emeric Brun08622d32020-12-23 17:41:43 +01003332int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003333{
Emeric Brun08622d32020-12-23 17:41:43 +01003334 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003335 char *hostname_dn;
3336 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003337
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003338 /* Note that the server lock is already held. */
3339 if (!srv->resolvers)
3340 return -1;
3341
Emeric Brun08622d32020-12-23 17:41:43 +01003342 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003343 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003344 /* run time DNS resolution was not active for this server
3345 * and we can't enable it at run time for now.
3346 */
Emeric Brun08622d32020-12-23 17:41:43 +01003347 if (!srv->resolv_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003348 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003349
3350 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003351 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003352 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01003353 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
3354 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003355 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003356 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003357
Emeric Brun08622d32020-12-23 17:41:43 +01003358 resolution = srv->resolv_requester->resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003359 if (resolution &&
3360 resolution->hostname_dn &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003361 strcmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003362 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003363
Emeric Brund30e9a12020-12-23 18:49:16 +01003364 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003365
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003366 free(srv->hostname);
3367 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003368 srv->hostname = strdup(hostname);
3369 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003370 srv->hostname_dn_len = hostname_dn_len;
3371 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003372 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003373
Baptiste Assmann13a92322019-06-07 09:40:55 +02003374 if (srv->flags & SRV_F_NO_RESOLUTION)
3375 goto end;
3376
Emeric Brund30e9a12020-12-23 18:49:16 +01003377 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003378 goto err;
3379
3380 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003381 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003382 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003383 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003384
3385 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003386 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003387 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003388 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003389}
3390
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003391/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3392 * from the state file. This is suited for initial address configuration.
3393 * Returns 0 on success otherwise a non-zero error code. In case of error,
3394 * *err_code, if not NULL, is filled up.
3395 */
3396static int srv_apply_lastaddr(struct server *srv, int *err_code)
3397{
3398 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3399 if (err_code)
3400 *err_code |= ERR_WARN;
3401 return 1;
3402 }
3403 return 0;
3404}
3405
Willy Tarreau25e51522016-11-04 15:10:17 +01003406/* returns 0 if no error, otherwise a combination of ERR_* flags */
3407static int srv_iterate_initaddr(struct server *srv)
3408{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003409 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003410 int return_code = 0;
3411 int err_code;
3412 unsigned int methods;
3413
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003414 /* If no addr and no hostname set, get the name from the DNS SRV request */
3415 if (!name && srv->srvrq)
3416 name = srv->srvrq->name;
3417
Willy Tarreau25e51522016-11-04 15:10:17 +01003418 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003419 if (!methods) {
3420 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003421 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3422 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003423 if (srv->resolvers_id) {
3424 /* dns resolution is configured, add "none" to not fail on startup */
3425 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3426 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003427 }
3428
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003429 /* "-dr" : always append "none" so that server addresses resolution
3430 * failures are silently ignored, this is convenient to validate some
3431 * configs out of their environment.
3432 */
3433 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3434 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3435
Willy Tarreau25e51522016-11-04 15:10:17 +01003436 while (methods) {
3437 err_code = 0;
3438 switch (srv_get_next_initaddr(&methods)) {
3439 case SRV_IADDR_LAST:
3440 if (!srv->lastaddr)
3441 continue;
3442 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003443 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003444 return_code |= err_code;
3445 break;
3446
3447 case SRV_IADDR_LIBC:
3448 if (!srv->hostname)
3449 continue;
3450 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003451 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003452 return_code |= err_code;
3453 break;
3454
Willy Tarreau37ebe122016-11-04 15:17:58 +01003455 case SRV_IADDR_NONE:
3456 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003457 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003458 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003459 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003460 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003461 return return_code;
3462
Willy Tarreau4310d362016-11-02 15:05:56 +01003463 case SRV_IADDR_IP:
3464 ipcpy(&srv->init_addr, &srv->addr);
3465 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003466 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 +01003467 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau4310d362016-11-02 15:05:56 +01003468 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003469 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003470
Willy Tarreau25e51522016-11-04 15:10:17 +01003471 default: /* unhandled method */
3472 break;
3473 }
3474 }
3475
3476 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003477 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003478 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau25e51522016-11-04 15:10:17 +01003479 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003480 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003481 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003482 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003483 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003484
3485 return_code |= ERR_ALERT | ERR_FATAL;
3486 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003487out:
3488 srv_set_dyncookie(srv);
Thayne McCombs92149f92020-11-20 01:28:26 -07003489 srv_set_addr_desc(srv);
Olivier Houchard4e694042017-03-14 20:01:29 +01003490 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003491}
3492
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003493/*
3494 * This function parses all backends and all servers within each backend
3495 * and performs servers' addr resolution based on information provided by:
3496 * - configuration file
3497 * - server-state file (states provided by an 'old' haproxy process)
3498 *
3499 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3500 */
3501int srv_init_addr(void)
3502{
3503 struct proxy *curproxy;
3504 int return_code = 0;
3505
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003506 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003507 while (curproxy) {
3508 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003509
3510 /* servers are in backend only */
Amaury Denoyellee3c41922021-01-06 14:28:50 +01003511 if (!(curproxy->cap & PR_CAP_BE) || curproxy->disabled)
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003512 goto srv_init_addr_next;
3513
Willy Tarreau25e51522016-11-04 15:10:17 +01003514 for (srv = curproxy->srv; srv; srv = srv->next)
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003515 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003516 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003517
3518 srv_init_addr_next:
3519 curproxy = curproxy->next;
3520 }
3521
3522 return return_code;
3523}
3524
Willy Tarreau46b7f532018-08-21 11:54:26 +02003525/*
3526 * Must be called with the server lock held.
3527 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003528const 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 +02003529{
3530
Willy Tarreau83061a82018-07-13 11:56:34 +02003531 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003532
3533 msg = get_trash_chunk();
3534 chunk_reset(msg);
3535
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003536 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003537 chunk_appendf(msg, "no need to change the FDQN");
3538 goto out;
3539 }
3540
3541 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3542 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3543 goto out;
3544 }
3545
3546 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3547 server->proxy->id, server->id, server->hostname, fqdn);
3548
Emeric Brun08622d32020-12-23 17:41:43 +01003549 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003550 chunk_reset(msg);
3551 chunk_appendf(msg, "could not update %s/%s FQDN",
3552 server->proxy->id, server->id);
3553 goto out;
3554 }
3555
3556 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003557 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003558
3559 out:
3560 if (updater)
3561 chunk_appendf(msg, " by '%s'", updater);
3562 chunk_appendf(msg, "\n");
3563
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003564 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003565}
3566
3567
Willy Tarreau21b069d2016-11-23 17:15:08 +01003568/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3569 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003570 * 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 +01003571 * used for CLI commands requiring a server name.
3572 * Important: the <arg> is modified to remove the '/'.
3573 */
3574struct server *cli_find_server(struct appctx *appctx, char *arg)
3575{
3576 struct proxy *px;
3577 struct server *sv;
3578 char *line;
3579
3580 /* split "backend/server" and make <line> point to server */
3581 for (line = arg; *line; line++)
3582 if (*line == '/') {
3583 *line++ = '\0';
3584 break;
3585 }
3586
3587 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003588 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003589 return NULL;
3590 }
3591
3592 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003593 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003594 return NULL;
3595 }
3596
Willy Tarreauc3914d42020-09-24 08:39:22 +02003597 if (px->disabled) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003598 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003599 return NULL;
3600 }
3601
3602 return sv;
3603}
3604
William Lallemand222baf22016-11-19 02:00:33 +01003605
Willy Tarreau46b7f532018-08-21 11:54:26 +02003606/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003607static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01003608{
3609 struct server *sv;
3610 const char *warning;
3611
3612 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3613 return 1;
3614
3615 sv = cli_find_server(appctx, args[2]);
3616 if (!sv)
3617 return 1;
3618
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003619 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02003620
William Lallemand222baf22016-11-19 02:00:33 +01003621 if (strcmp(args[3], "weight") == 0) {
3622 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003623 if (warning)
3624 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003625 }
3626 else if (strcmp(args[3], "state") == 0) {
3627 if (strcmp(args[4], "ready") == 0)
3628 srv_adm_set_ready(sv);
3629 else if (strcmp(args[4], "drain") == 0)
3630 srv_adm_set_drain(sv);
3631 else if (strcmp(args[4], "maint") == 0)
3632 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02003633 else
3634 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003635 }
3636 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003637 if (sv->track)
3638 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003639 else if (strcmp(args[4], "up") == 0) {
3640 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003641 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003642 }
3643 else if (strcmp(args[4], "stopping") == 0) {
3644 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003645 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003646 }
3647 else if (strcmp(args[4], "down") == 0) {
3648 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003649 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003650 }
Willy Tarreau9d008692019-08-09 11:21:01 +02003651 else
3652 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003653 }
3654 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003655 if (!(sv->agent.state & CHK_ST_ENABLED))
3656 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003657 else if (strcmp(args[4], "up") == 0) {
3658 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003659 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003660 }
3661 else if (strcmp(args[4], "down") == 0) {
3662 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003663 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003664 }
Willy Tarreau9d008692019-08-09 11:21:01 +02003665 else
3666 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003667 }
Misiek2da082d2017-01-09 09:40:42 +01003668 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01003669 char *addr = NULL;
3670 char *port = NULL;
3671 if (strlen(args[4]) == 0) {
3672 cli_err(appctx, "set server <b>/<s> agent-addr requires"
3673 " an address and optionally a port.\n");
3674 goto out_unlock;
3675 }
3676 addr = args[4];
3677 if (strcmp(args[5], "port") == 0)
3678 port = args[6];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003679 warning = srv_update_agent_addr_port(sv, addr, port);
William Dauchy7cabc062021-02-11 22:51:24 +01003680 if (warning)
3681 cli_msg(appctx, LOG_WARNING, warning);
3682 }
3683 else if (strcmp(args[3], "agent-port") == 0) {
3684 char *port = NULL;
3685 if (strlen(args[4]) == 0) {
3686 cli_err(appctx, "set server <b>/<s> agent-port requires"
3687 " a port.\n");
3688 goto out_unlock;
3689 }
3690 port = args[4];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003691 warning = srv_update_agent_addr_port(sv, NULL, port);
William Dauchy7cabc062021-02-11 22:51:24 +01003692 if (warning)
3693 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01003694 }
3695 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003696 if (!(sv->agent.state & CHK_ST_ENABLED))
3697 cli_err(appctx, "agent checks are not enabled on this server.\n");
3698 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02003699 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02003700 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01003701 }
3702 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003703 else if (strcmp(args[3], "check-addr") == 0) {
3704 char *addr = NULL;
3705 char *port = NULL;
3706 if (strlen(args[4]) == 0) {
3707 cli_err(appctx, "set server <b>/<s> check-addr requires"
3708 " an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003709 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01003710 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003711 addr = args[4];
3712 if (strcmp(args[5], "port") == 0)
3713 port = args[6];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003714 warning = srv_update_check_addr_port(sv, addr, port);
William Dauchyb456e1f2021-02-11 22:51:23 +01003715 if (warning)
3716 cli_msg(appctx, LOG_WARNING, warning);
3717 }
3718 else if (strcmp(args[3], "check-port") == 0) {
3719 char *port = NULL;
3720 if (strlen(args[4]) == 0) {
3721 cli_err(appctx, "set server <b>/<s> check-port requires"
3722 " a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003723 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01003724 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003725 port = args[4];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003726 warning = srv_update_check_addr_port(sv, NULL, port);
William Dauchyb456e1f2021-02-11 22:51:23 +01003727 if (warning)
3728 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003729 }
3730 else if (strcmp(args[3], "addr") == 0) {
3731 char *addr = NULL;
3732 char *port = NULL;
3733 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003734 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003735 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01003736 }
3737 else {
3738 addr = args[4];
3739 }
3740 if (strcmp(args[5], "port") == 0) {
3741 port = args[6];
3742 }
Christopher Faulet69beaa92021-02-16 12:07:47 +01003743 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02003744 if (warning)
3745 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003746 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3747 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003748 else if (strcmp(args[3], "fqdn") == 0) {
3749 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003750 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003751 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003752 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02003753 /* ensure runtime resolver will process this new fqdn */
3754 if (sv->flags & SRV_F_NO_RESOLUTION) {
3755 sv->flags &= ~SRV_F_NO_RESOLUTION;
3756 }
Christopher Faulet69beaa92021-02-16 12:07:47 +01003757 warning = srv_update_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02003758 if (warning)
3759 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003760 }
William Dauchyf6370442020-11-14 19:25:33 +01003761 else if (strcmp(args[3], "ssl") == 0) {
3762#ifdef USE_OPENSSL
3763 if (sv->ssl_ctx.ctx == NULL) {
3764 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
3765 " default-server should define ssl settings\n");
3766 goto out_unlock;
3767 } else if (strcmp(args[4], "on") == 0) {
3768 ssl_sock_set_srv(sv, 1);
3769 } else if (strcmp(args[4], "off") == 0) {
3770 ssl_sock_set_srv(sv, 0);
3771 } else {
3772 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
3773 goto out_unlock;
3774 }
3775 srv_cleanup_connections(sv);
3776 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
3777#else
3778 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
3779#endif
3780 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02003781 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01003782 "usage: set server <backend>/<server> "
3783 "addr | agent | agent-addr | agent-port | agent-send | "
3784 "check-addr | check-port | fqdn | health | ssl | "
3785 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01003786 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003787 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003788 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003789 return 1;
3790}
3791
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003792static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01003793{
3794 struct stream_interface *si = appctx->owner;
3795 struct proxy *px;
3796 struct server *sv;
3797 char *line;
3798
3799
3800 /* split "backend/server" and make <line> point to server */
3801 for (line = args[2]; *line; line++)
3802 if (*line == '/') {
3803 *line++ = '\0';
3804 break;
3805 }
3806
Willy Tarreau9d008692019-08-09 11:21:01 +02003807 if (!*line)
3808 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01003809
Willy Tarreau9d008692019-08-09 11:21:01 +02003810 if (!get_backend_server(args[2], line, &px, &sv))
3811 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01003812
3813 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003814 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
3815 sv->iweight);
3816 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003817 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003818 return 0;
3819 }
William Lallemand6b160942016-11-22 12:34:35 +01003820 return 1;
3821}
3822
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003823/* Parse a "set weight" command.
3824 *
3825 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003826 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003827static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01003828{
3829 struct server *sv;
3830 const char *warning;
3831
3832 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3833 return 1;
3834
3835 sv = cli_find_server(appctx, args[2]);
3836 if (!sv)
3837 return 1;
3838
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003839 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
3840
William Lallemand6b160942016-11-22 12:34:35 +01003841 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003842 if (warning)
3843 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003844
3845 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
3846
William Lallemand6b160942016-11-22 12:34:35 +01003847 return 1;
3848}
3849
Willy Tarreau46b7f532018-08-21 11:54:26 +02003850/* parse a "set maxconn server" command. It always returns 1.
3851 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003852 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003853 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003854static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01003855{
3856 struct server *sv;
3857 const char *warning;
3858
3859 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3860 return 1;
3861
3862 sv = cli_find_server(appctx, args[3]);
3863 if (!sv)
3864 return 1;
3865
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003866 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
3867
Willy Tarreaub8026272016-11-23 11:26:56 +01003868 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003869 if (warning)
3870 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003871
3872 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
3873
Willy Tarreaub8026272016-11-23 11:26:56 +01003874 return 1;
3875}
William Lallemand6b160942016-11-22 12:34:35 +01003876
Willy Tarreau46b7f532018-08-21 11:54:26 +02003877/* parse a "disable agent" command. It always returns 1.
3878 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003879 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003880 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003881static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003882{
3883 struct server *sv;
3884
3885 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3886 return 1;
3887
3888 sv = cli_find_server(appctx, args[2]);
3889 if (!sv)
3890 return 1;
3891
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003892 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003893 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003894 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003895 return 1;
3896}
3897
Willy Tarreau46b7f532018-08-21 11:54:26 +02003898/* parse a "disable health" command. It always returns 1.
3899 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003900 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003901 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003902static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003903{
3904 struct server *sv;
3905
3906 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3907 return 1;
3908
3909 sv = cli_find_server(appctx, args[2]);
3910 if (!sv)
3911 return 1;
3912
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003913 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003914 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003915 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003916 return 1;
3917}
3918
Willy Tarreau46b7f532018-08-21 11:54:26 +02003919/* parse a "disable server" command. It always returns 1.
3920 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003921 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003922 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003923static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01003924{
3925 struct server *sv;
3926
3927 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3928 return 1;
3929
3930 sv = cli_find_server(appctx, args[2]);
3931 if (!sv)
3932 return 1;
3933
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003934 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01003935 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003936 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01003937 return 1;
3938}
3939
Willy Tarreau46b7f532018-08-21 11:54:26 +02003940/* parse a "enable agent" command. It always returns 1.
3941 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003942 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003943 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003944static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003945{
3946 struct server *sv;
3947
3948 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3949 return 1;
3950
3951 sv = cli_find_server(appctx, args[2]);
3952 if (!sv)
3953 return 1;
3954
Willy Tarreau9d008692019-08-09 11:21:01 +02003955 if (!(sv->agent.state & CHK_ST_CONFIGURED))
3956 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003957
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003958 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003959 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003960 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01003961 return 1;
3962}
3963
Willy Tarreau46b7f532018-08-21 11:54:26 +02003964/* parse a "enable health" command. It always returns 1.
3965 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003966 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003967 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003968static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003969{
3970 struct server *sv;
3971
3972 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3973 return 1;
3974
3975 sv = cli_find_server(appctx, args[2]);
3976 if (!sv)
3977 return 1;
3978
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003979 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003980 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003981 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01003982 return 1;
3983}
3984
Willy Tarreau46b7f532018-08-21 11:54:26 +02003985/* parse a "enable server" command. It always returns 1.
3986 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02003987 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003988 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003989static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01003990{
3991 struct server *sv;
3992
3993 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3994 return 1;
3995
3996 sv = cli_find_server(appctx, args[2]);
3997 if (!sv)
3998 return 1;
3999
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004000 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004001 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004002 if (!(sv->flags & SRV_F_COOKIESET)
4003 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4004 sv->cookie)
4005 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004006 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004007 return 1;
4008}
4009
William Lallemand222baf22016-11-19 02:00:33 +01004010/* register cli keywords */
4011static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004012 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004013 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004014 { { "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 +01004015 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004016 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004017 { { "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 +01004018 { { "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 +01004019 { { "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 +01004020 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4021 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4022
William Lallemand222baf22016-11-19 02:00:33 +01004023 {{},}
4024}};
4025
Willy Tarreau0108d902018-11-25 19:14:37 +01004026INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004027
Emeric Brun64cc49c2017-10-03 14:46:45 +02004028/*
4029 * This function applies server's status changes, it is
4030 * is designed to be called asynchronously.
4031 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004032 * Must be called with the server lock held. This may also be called at init
4033 * time as the result of parsing the state file, in which case no lock will be
4034 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004035 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004036static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004037{
4038 struct check *check = &s->check;
4039 int xferred;
4040 struct proxy *px = s->proxy;
4041 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4042 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4043 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004044 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004045
Emeric Brun64cc49c2017-10-03 14:46:45 +02004046 /* If currently main is not set we try to apply pending state changes */
4047 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4048 int next_admin;
4049
4050 /* Backup next admin */
4051 next_admin = s->next_admin;
4052
4053 /* restore current admin state */
4054 s->next_admin = s->cur_admin;
4055
4056 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4057 s->last_change = now.tv_sec;
4058 if (s->proxy->lbprm.set_server_status_down)
4059 s->proxy->lbprm.set_server_status_down(s);
4060
4061 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4062 srv_shutdown_streams(s, SF_ERR_DOWN);
4063
4064 /* we might have streams queued on this server and waiting for
4065 * a connection. Those which are redispatchable will be queued
4066 * to another server or to the proxy itself.
4067 */
4068 xferred = pendconn_redistribute(s);
4069
4070 tmptrash = alloc_trash_chunk();
4071 if (tmptrash) {
4072 chunk_printf(tmptrash,
4073 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4074 s->proxy->id, s->id);
4075
Emeric Brun5a133512017-10-19 14:42:30 +02004076 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004077 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004078
4079 /* we don't send an alert if the server was previously paused */
4080 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004081 send_log(s->proxy, log_level, "%s.\n",
4082 tmptrash->area);
4083 send_email_alert(s, log_level, "%s",
4084 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004085 free_trash_chunk(tmptrash);
4086 tmptrash = NULL;
4087 }
4088 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4089 set_backend_down(s->proxy);
4090
4091 s->counters.down_trans++;
4092 }
4093 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4094 s->last_change = now.tv_sec;
4095 if (s->proxy->lbprm.set_server_status_down)
4096 s->proxy->lbprm.set_server_status_down(s);
4097
4098 /* we might have streams queued on this server and waiting for
4099 * a connection. Those which are redispatchable will be queued
4100 * to another server or to the proxy itself.
4101 */
4102 xferred = pendconn_redistribute(s);
4103
4104 tmptrash = alloc_trash_chunk();
4105 if (tmptrash) {
4106 chunk_printf(tmptrash,
4107 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4108 s->proxy->id, s->id);
4109
Emeric Brun5a133512017-10-19 14:42:30 +02004110 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004111
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004112 ha_warning("%s.\n", tmptrash->area);
4113 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4114 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004115 free_trash_chunk(tmptrash);
4116 tmptrash = NULL;
4117 }
4118
4119 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4120 set_backend_down(s->proxy);
4121 }
4122 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4123 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4124 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4125 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4126 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4127 s->proxy->last_change = now.tv_sec;
4128 }
4129
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01004130 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02004131 s->down_time += now.tv_sec - s->last_change;
4132
4133 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004134 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004135 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4136
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004137 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004138 /* now propagate the status change to any LB algorithms */
4139 if (px->lbprm.update_server_eweight)
4140 px->lbprm.update_server_eweight(s);
4141 else if (srv_willbe_usable(s)) {
4142 if (px->lbprm.set_server_status_up)
4143 px->lbprm.set_server_status_up(s);
4144 }
4145 else {
4146 if (px->lbprm.set_server_status_down)
4147 px->lbprm.set_server_status_down(s);
4148 }
4149
4150 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4151 * and it's not a backup server and its effective weight is > 0,
4152 * then it can accept new connections, so we shut down all streams
4153 * on all backup servers.
4154 */
4155 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4156 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4157 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4158
4159 /* check if we can handle some connections queued at the proxy. We
4160 * will take as many as we can handle.
4161 */
4162 xferred = pendconn_grab_from_px(s);
4163
4164 tmptrash = alloc_trash_chunk();
4165 if (tmptrash) {
4166 chunk_printf(tmptrash,
4167 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4168 s->proxy->id, s->id);
4169
Emeric Brun5a133512017-10-19 14:42:30 +02004170 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004171 ha_warning("%s.\n", tmptrash->area);
4172 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4173 tmptrash->area);
4174 send_email_alert(s, LOG_NOTICE, "%s",
4175 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004176 free_trash_chunk(tmptrash);
4177 tmptrash = NULL;
4178 }
4179
4180 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4181 set_backend_down(s->proxy);
4182 }
4183 else if (s->cur_eweight != s->next_eweight) {
4184 /* now propagate the status change to any LB algorithms */
4185 if (px->lbprm.update_server_eweight)
4186 px->lbprm.update_server_eweight(s);
4187 else if (srv_willbe_usable(s)) {
4188 if (px->lbprm.set_server_status_up)
4189 px->lbprm.set_server_status_up(s);
4190 }
4191 else {
4192 if (px->lbprm.set_server_status_down)
4193 px->lbprm.set_server_status_down(s);
4194 }
4195
4196 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4197 set_backend_down(s->proxy);
4198 }
4199
4200 s->next_admin = next_admin;
4201 }
4202
Emeric Brun5a133512017-10-19 14:42:30 +02004203 /* reset operational state change */
4204 *s->op_st_chg.reason = 0;
4205 s->op_st_chg.status = s->op_st_chg.code = -1;
4206 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004207
4208 /* Now we try to apply pending admin changes */
4209
4210 /* Maintenance must also disable health checks */
4211 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4212 if (s->check.state & CHK_ST_ENABLED) {
4213 s->check.state |= CHK_ST_PAUSED;
4214 check->health = 0;
4215 }
4216
4217 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004218 tmptrash = alloc_trash_chunk();
4219 if (tmptrash) {
4220 chunk_printf(tmptrash,
4221 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4222 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4223 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004224
Olivier Houchard796a2b32017-10-24 17:42:47 +02004225 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004226
Olivier Houchard796a2b32017-10-24 17:42:47 +02004227 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004228 ha_warning("%s.\n", tmptrash->area);
4229 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4230 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004231 }
4232 free_trash_chunk(tmptrash);
4233 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004234 }
Emeric Brun8f298292017-12-06 16:47:17 +01004235 /* commit new admin status */
4236
4237 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004238 }
4239 else { /* server was still running */
4240 check->health = 0; /* failure */
4241 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004242
4243 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004244 if (s->proxy->lbprm.set_server_status_down)
4245 s->proxy->lbprm.set_server_status_down(s);
4246
Emeric Brun64cc49c2017-10-03 14:46:45 +02004247 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4248 srv_shutdown_streams(s, SF_ERR_DOWN);
4249
William Dauchy6318d332020-05-02 21:52:36 +02004250 /* force connection cleanup on the given server */
4251 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004252 /* we might have streams queued on this server and waiting for
4253 * a connection. Those which are redispatchable will be queued
4254 * to another server or to the proxy itself.
4255 */
4256 xferred = pendconn_redistribute(s);
4257
4258 tmptrash = alloc_trash_chunk();
4259 if (tmptrash) {
4260 chunk_printf(tmptrash,
4261 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4262 s->flags & SRV_F_BACKUP ? "Backup " : "",
4263 s->proxy->id, s->id,
4264 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4265
4266 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4267
4268 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004269 ha_warning("%s.\n", tmptrash->area);
4270 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4271 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004272 }
4273 free_trash_chunk(tmptrash);
4274 tmptrash = NULL;
4275 }
4276 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4277 set_backend_down(s->proxy);
4278
4279 s->counters.down_trans++;
4280 }
4281 }
4282 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4283 /* OK here we're leaving maintenance, we have many things to check,
4284 * because the server might possibly be coming back up depending on
4285 * its state. In practice, leaving maintenance means that we should
4286 * immediately turn to UP (more or less the slowstart) under the
4287 * following conditions :
4288 * - server is neither checked nor tracked
4289 * - server tracks another server which is not checked
4290 * - server tracks another server which is already up
4291 * Which sums up as something simpler :
4292 * "either the tracking server is up or the server's checks are disabled
4293 * or up". Otherwise we only re-enable health checks. There's a special
4294 * case associated to the stopping state which can be inherited. Note
4295 * that the server might still be in drain mode, which is naturally dealt
4296 * with by the lower level functions.
4297 */
4298
4299 if (s->check.state & CHK_ST_ENABLED) {
4300 s->check.state &= ~CHK_ST_PAUSED;
4301 check->health = check->rise; /* start OK but check immediately */
4302 }
4303
4304 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4305 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4306 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4307 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4308 s->next_state = SRV_ST_STOPPING;
4309 }
4310 else {
4311 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004312 if (s->slowstart > 0) {
4313 if (s->warmup)
4314 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4315 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02004316 else
4317 s->next_state = SRV_ST_RUNNING;
4318 }
4319
4320 }
4321
4322 tmptrash = alloc_trash_chunk();
4323 if (tmptrash) {
4324 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4325 chunk_printf(tmptrash,
4326 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4327 s->flags & SRV_F_BACKUP ? "Backup " : "",
4328 s->proxy->id, s->id,
4329 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4330 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4331 }
4332 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4333 chunk_printf(tmptrash,
4334 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4335 s->flags & SRV_F_BACKUP ? "Backup " : "",
4336 s->proxy->id, s->id, s->hostname,
4337 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4338 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4339 }
4340 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4341 chunk_printf(tmptrash,
4342 "%sServer %s/%s is %s/%s (leaving maintenance)",
4343 s->flags & SRV_F_BACKUP ? "Backup " : "",
4344 s->proxy->id, s->id,
4345 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4346 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4347 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004348 ha_warning("%s.\n", tmptrash->area);
4349 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4350 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004351 free_trash_chunk(tmptrash);
4352 tmptrash = NULL;
4353 }
4354
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004355 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004356 /* now propagate the status change to any LB algorithms */
4357 if (px->lbprm.update_server_eweight)
4358 px->lbprm.update_server_eweight(s);
4359 else if (srv_willbe_usable(s)) {
4360 if (px->lbprm.set_server_status_up)
4361 px->lbprm.set_server_status_up(s);
4362 }
4363 else {
4364 if (px->lbprm.set_server_status_down)
4365 px->lbprm.set_server_status_down(s);
4366 }
4367
4368 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4369 set_backend_down(s->proxy);
4370
Willy Tarreau6a78e612018-08-07 10:14:53 +02004371 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4372 * and it's not a backup server and its effective weight is > 0,
4373 * then it can accept new connections, so we shut down all streams
4374 * on all backup servers.
4375 */
4376 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4377 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4378 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4379
4380 /* check if we can handle some connections queued at the proxy. We
4381 * will take as many as we can handle.
4382 */
4383 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004384 }
4385 else if (s->next_admin & SRV_ADMF_MAINT) {
4386 /* remaining in maintenance mode, let's inform precisely about the
4387 * situation.
4388 */
4389 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4390 tmptrash = alloc_trash_chunk();
4391 if (tmptrash) {
4392 chunk_printf(tmptrash,
4393 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4394 s->flags & SRV_F_BACKUP ? "Backup " : "",
4395 s->proxy->id, s->id);
4396
4397 if (s->track) /* normally it's mandatory here */
4398 chunk_appendf(tmptrash, " via %s/%s",
4399 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004400 ha_warning("%s.\n", tmptrash->area);
4401 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4402 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004403 free_trash_chunk(tmptrash);
4404 tmptrash = NULL;
4405 }
4406 }
4407 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4408 tmptrash = alloc_trash_chunk();
4409 if (tmptrash) {
4410 chunk_printf(tmptrash,
4411 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4412 s->flags & SRV_F_BACKUP ? "Backup " : "",
4413 s->proxy->id, s->id, s->hostname);
4414
4415 if (s->track) /* normally it's mandatory here */
4416 chunk_appendf(tmptrash, " via %s/%s",
4417 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004418 ha_warning("%s.\n", tmptrash->area);
4419 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4420 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004421 free_trash_chunk(tmptrash);
4422 tmptrash = NULL;
4423 }
4424 }
4425 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4426 tmptrash = alloc_trash_chunk();
4427 if (tmptrash) {
4428 chunk_printf(tmptrash,
4429 "%sServer %s/%s remains in forced maintenance",
4430 s->flags & SRV_F_BACKUP ? "Backup " : "",
4431 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004432 ha_warning("%s.\n", tmptrash->area);
4433 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4434 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004435 free_trash_chunk(tmptrash);
4436 tmptrash = NULL;
4437 }
4438 }
4439 /* don't report anything when leaving drain mode and remaining in maintenance */
4440
4441 s->cur_admin = s->next_admin;
4442 }
4443
4444 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4445 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4446 /* drain state is applied only if not yet in maint */
4447
4448 s->last_change = now.tv_sec;
4449 if (px->lbprm.set_server_status_down)
4450 px->lbprm.set_server_status_down(s);
4451
4452 /* we might have streams queued on this server and waiting for
4453 * a connection. Those which are redispatchable will be queued
4454 * to another server or to the proxy itself.
4455 */
4456 xferred = pendconn_redistribute(s);
4457
4458 tmptrash = alloc_trash_chunk();
4459 if (tmptrash) {
4460 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4461 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4462 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4463
4464 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4465
4466 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004467 ha_warning("%s.\n", tmptrash->area);
4468 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4469 tmptrash->area);
4470 send_email_alert(s, LOG_NOTICE, "%s",
4471 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004472 }
4473 free_trash_chunk(tmptrash);
4474 tmptrash = NULL;
4475 }
4476
4477 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4478 set_backend_down(s->proxy);
4479 }
4480 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4481 /* OK completely leaving drain mode */
4482 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4483 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4484 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4485 s->proxy->last_change = now.tv_sec;
4486 }
4487
4488 if (s->last_change < now.tv_sec) // ignore negative times
4489 s->down_time += now.tv_sec - s->last_change;
4490 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004491 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004492
4493 tmptrash = alloc_trash_chunk();
4494 if (tmptrash) {
4495 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4496 chunk_printf(tmptrash,
4497 "%sServer %s/%s is %s (leaving forced drain)",
4498 s->flags & SRV_F_BACKUP ? "Backup " : "",
4499 s->proxy->id, s->id,
4500 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4501 }
4502 else {
4503 chunk_printf(tmptrash,
4504 "%sServer %s/%s is %s (leaving drain)",
4505 s->flags & SRV_F_BACKUP ? "Backup " : "",
4506 s->proxy->id, s->id,
4507 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4508 if (s->track) /* normally it's mandatory here */
4509 chunk_appendf(tmptrash, " via %s/%s",
4510 s->track->proxy->id, s->track->id);
4511 }
4512
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004513 ha_warning("%s.\n", tmptrash->area);
4514 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4515 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004516 free_trash_chunk(tmptrash);
4517 tmptrash = NULL;
4518 }
4519
4520 /* now propagate the status change to any LB algorithms */
4521 if (px->lbprm.update_server_eweight)
4522 px->lbprm.update_server_eweight(s);
4523 else if (srv_willbe_usable(s)) {
4524 if (px->lbprm.set_server_status_up)
4525 px->lbprm.set_server_status_up(s);
4526 }
4527 else {
4528 if (px->lbprm.set_server_status_down)
4529 px->lbprm.set_server_status_down(s);
4530 }
4531 }
4532 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4533 /* remaining in drain mode after removing one of its flags */
4534
4535 tmptrash = alloc_trash_chunk();
4536 if (tmptrash) {
4537 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4538 chunk_printf(tmptrash,
4539 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4540 s->flags & SRV_F_BACKUP ? "Backup " : "",
4541 s->proxy->id, s->id);
4542
4543 if (s->track) /* normally it's mandatory here */
4544 chunk_appendf(tmptrash, " via %s/%s",
4545 s->track->proxy->id, s->track->id);
4546 }
4547 else {
4548 chunk_printf(tmptrash,
4549 "%sServer %s/%s remains in forced drain mode",
4550 s->flags & SRV_F_BACKUP ? "Backup " : "",
4551 s->proxy->id, s->id);
4552 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004553 ha_warning("%s.\n", tmptrash->area);
4554 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4555 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004556 free_trash_chunk(tmptrash);
4557 tmptrash = NULL;
4558 }
4559
4560 /* commit new admin status */
4561
4562 s->cur_admin = s->next_admin;
4563 }
4564 }
4565
4566 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004567 *s->adm_st_chg_cause = 0;
4568}
Emeric Brun64cc49c2017-10-03 14:46:45 +02004569
Willy Tarreau144f84a2021-03-02 16:09:26 +01004570struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004571{
4572 struct connection *conn;
4573
Willy Tarreau4d82bf52020-06-28 00:19:17 +02004574 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004575 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02004576 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004577 }
4578
4579 return task;
4580}
4581
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004582/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02004583 * moving them all.
4584 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004585 *
4586 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02004587 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004588static 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 +02004589{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004590 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004591 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02004592 int i = 0;
4593
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004594 node = eb_first(idle_tree);
4595 while (node) {
4596 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004597 if (toremove_nb != -1 && i >= toremove_nb)
4598 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004599
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004600 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004601 eb_delete(node);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004602 MT_LIST_ADDQ(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004603 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004604
4605 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02004606 }
4607 return i;
4608}
William Dauchy6318d332020-05-02 21:52:36 +02004609/* cleanup connections for a given server
4610 * might be useful when going on forced maintenance or live changing ip/port
4611 */
William Dauchy707ad322020-05-04 13:52:40 +02004612static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02004613{
William Dauchy6318d332020-05-02 21:52:36 +02004614 int did_remove;
4615 int i;
William Dauchy6318d332020-05-02 21:52:36 +02004616
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01004617 /* nothing to do if pool-max-conn is null */
4618 if (!srv->max_idle_conns)
4619 return;
4620
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004621 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004622 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02004623 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004624 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004625 if (srv_migrate_conns_to_remove(&srv->idle_conns_tree[i], &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02004626 did_remove = 1;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004627 if (srv_migrate_conns_to_remove(&srv->safe_conns_tree[i], &idle_conns[i].toremove_conns, -1) > 0)
Olivier Houchardff1d0922020-06-29 20:15:59 +02004628 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004629 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02004630 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02004631 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004632
4633 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
4634 break;
William Dauchy6318d332020-05-02 21:52:36 +02004635 }
William Dauchy6318d332020-05-02 21:52:36 +02004636}
4637
Willy Tarreau144f84a2021-03-02 16:09:26 +01004638struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004639{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004640 struct server *srv;
4641 struct eb32_node *eb;
4642 int i;
4643 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01004644
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004645 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004646 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
4647 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004648 int exceed_conns;
4649 int to_kill;
4650 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01004651
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004652 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
4653 if (!eb) {
4654 /* we might have reached the end of the tree, typically because
4655 * <now_ms> is in the first half and we're first scanning the last
4656 * half. Let's loop back to the beginning of the tree now.
4657 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004658
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004659 eb = eb32_first(&idle_conn_srv);
4660 if (likely(!eb))
4661 break;
4662 }
4663 if (tick_is_lt(now_ms, eb->key)) {
4664 /* timer not expired yet, revisit it later */
4665 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004666 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004667 }
4668 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004669
4670 /* Calculate how many idle connections we want to kill :
4671 * we want to remove half the difference between the total
4672 * of established connections (used or idle) and the max
4673 * number of used connections.
4674 */
4675 curr_idle = srv->curr_idle_conns;
4676 if (curr_idle == 0)
4677 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02004678 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004679 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02004680
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004681 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02004682 if (srv->est_need_conns < srv->max_used_conns)
4683 srv->est_need_conns = srv->max_used_conns;
4684
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004685 srv->max_used_conns = srv->curr_used_conns;
4686
Willy Tarreau18ed7892020-07-02 19:05:30 +02004687 if (exceed_conns <= 0)
4688 goto remove;
4689
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004690 /* check all threads starting with ours */
4691 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004692 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004693 int j;
4694 int did_remove = 0;
4695
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004696 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
4697 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02004698
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004699 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004700 j = srv_migrate_conns_to_remove(&srv->idle_conns_tree[i], &idle_conns[i].toremove_conns, max_conn);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004701 if (j > 0)
4702 did_remove = 1;
4703 if (max_conn - j > 0 &&
Amaury Denoyellef232cb32021-01-06 16:14:12 +01004704 srv_migrate_conns_to_remove(&srv->safe_conns_tree[i], &idle_conns[i].toremove_conns, max_conn - j) > 0)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004705 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004706 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02004707
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004708 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02004709 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02004710
4711 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
4712 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004713 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02004714remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004715 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004716
4717 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004718 /* There are still more idle connections, add the
4719 * server back in the tree.
4720 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004721 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004722 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004723 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004724 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004725 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01004726 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
4727
Willy Tarreau21b9ff52020-11-05 09:12:20 +01004728 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01004729 return task;
4730}
Olivier Houchard88698d92019-04-16 19:07:22 +02004731
Willy Tarreau76cc6992020-07-01 18:49:24 +02004732/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
4733static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
4734 struct proxy *defpx, const char *file, int line,
4735 char **err)
4736{
4737 if (too_many_args(1, args, err, NULL))
4738 return -1;
4739
4740 if (strcmp(args[1], "on") == 0)
4741 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
4742 else if (strcmp(args[1], "off") == 0)
4743 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
4744 else {
4745 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
4746 return -1;
4747 }
4748 return 0;
4749}
4750
Olivier Houchard88698d92019-04-16 19:07:22 +02004751/* config parser for global "tune.pool-{low,high}-fd-ratio" */
4752static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
4753 struct proxy *defpx, const char *file, int line,
4754 char **err)
4755{
4756 int arg = -1;
4757
4758 if (too_many_args(1, args, err, NULL))
4759 return -1;
4760
4761 if (*(args[1]) != 0)
4762 arg = atoi(args[1]);
4763
4764 if (arg < 0 || arg > 100) {
4765 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
4766 return -1;
4767 }
4768
4769 if (args[0][10] == 'h')
4770 global.tune.pool_high_ratio = arg;
4771 else
4772 global.tune.pool_low_ratio = arg;
4773 return 0;
4774}
4775
4776/* config keyword parsers */
4777static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02004778 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02004779 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
4780 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
4781 { 0, NULL, NULL }
4782}};
4783
4784INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4785
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004786/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004787 * Local variables:
4788 * c-indent-level: 8
4789 * c-basic-offset: 8
4790 * End:
4791 */