blob: fc78889dc2bfbc0067923ee1c424d0498255d35b [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>
Amaury Denoyellef99f77a2021-03-08 17:13:32 +010041#include <haproxy/stats.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>
Willy Tarreauba6300e2021-05-08 14:09:40 +020047#include <haproxy/tools.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020048
Baptiste Assmannda29fe22019-06-13 13:24:29 +020049
Willy Tarreau3ff577e2018-08-02 11:48:52 +020050static void srv_update_status(struct server *s);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
William Dauchy6318d332020-05-02 21:52:36 +020052static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010054/* extra keywords used as value for other arguments. They are used as
55 * suggestions for mistyped words.
Willy Tarreau49c2b452021-03-12 09:58:04 +010056 */
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010057static const char *extra_kw_list[] = {
58 "ipv4", "ipv6", "legacy", "octet-count",
Amaury Denoyelle3b89c112021-03-12 16:04:00 +010059 "fail-check", "sudden-death", "mark-down",
Willy Tarreau49c2b452021-03-12 09:58:04 +010060 NULL /* must be last */
61};
62
Willy Tarreau21faa912012-10-10 08:27:36 +020063/* List head of all known server keywords */
64static struct srv_kw_list srv_keywords = {
65 .list = LIST_HEAD_INIT(srv_keywords.list)
66};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020067
Willy Tarreauaf613e82020-06-05 08:40:51 +020068__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010069struct eb_root idle_conn_srv = EB_ROOT;
Willy Tarreau14015b82021-04-10 17:33:15 +020070struct task *idle_conn_task __read_mostly = NULL;
Willy Tarreau198e92a2021-03-05 10:23:32 +010071struct list servers_list = LIST_HEAD_INIT(servers_list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010072
Frédéric Lécaille7da71292019-05-20 09:47:07 +020073/* The server names dictionary */
Thayne McCombs92149f92020-11-20 01:28:26 -070074struct dict server_key_dict = {
75 .name = "server keys",
Frédéric Lécaille7da71292019-05-20 09:47:07 +020076 .values = EB_ROOT_UNIQUE,
77};
78
Simon Hormana3608442013-11-01 16:46:15 +090079int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020080{
Amaury Denoyellee6ba7912020-10-29 15:59:05 +010081 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020082 return s->down_time;
83
84 return now.tv_sec - s->last_change + s->down_time;
85}
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050087int srv_lastsession(const struct server *s)
88{
89 if (s->counters.last_sess)
90 return now.tv_sec - s->counters.last_sess;
91
92 return -1;
93}
94
Simon Horman4a741432013-02-23 15:35:38 +090095int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020096{
Simon Horman4a741432013-02-23 15:35:38 +090097 const struct server *s = check->server;
98
Willy Tarreauff5ae352013-12-11 20:36:34 +010099 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900100 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100101
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900103 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100104
Simon Horman4a741432013-02-23 15:35:38 +0900105 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100106}
107
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100108/*
109 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200110 * Unlikely, but it can happen. The server's proxy must be at least
111 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100112 */
113static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100114{
115 struct proxy *p = s->proxy;
116 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117
118 for (tmpserv = p->srv; tmpserv != NULL;
119 tmpserv = tmpserv->next) {
120 if (tmpserv == s)
121 continue;
122 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
123 continue;
124 if (tmpserv->cookie &&
125 strcmp(tmpserv->cookie, s->cookie) == 0) {
126 ha_warning("We generated two equal cookies for two different servers.\n"
127 "Please change the secret key for '%s'.\n",
128 s->proxy->id);
129 }
130 }
131
132}
133
Willy Tarreau46b7f532018-08-21 11:54:26 +0200134/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200135 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200136 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137void srv_set_dyncookie(struct server *s)
138{
139 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100140 char *tmpbuf;
141 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100142 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 size_t buffer_len;
144 int addr_len;
145 int port;
146
Willy Tarreau595e7672020-10-20 17:30:08 +0200147 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200148
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 if ((s->flags & SRV_F_COOKIESET) ||
150 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
151 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200152 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100153 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100154
155 if (s->addr.ss_family != AF_INET &&
156 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200157 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100158 /*
159 * Buffer to calculate the cookie value.
160 * The buffer contains the secret key + the server IP address
161 * + the TCP port.
162 */
163 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
164 /*
165 * The TCP port should use only 2 bytes, but is stored in
166 * an unsigned int in struct server, so let's use 4, to be
167 * on the safe side.
168 */
169 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200170 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100171 memcpy(tmpbuf, p->dyncookie_key, key_len);
172 memcpy(&(tmpbuf[key_len]),
173 s->addr.ss_family == AF_INET ?
174 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
175 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
176 addr_len);
177 /*
178 * Make sure it's the same across all the load balancers,
179 * no matter their endianness.
180 */
181 port = htonl(s->svc_port);
182 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
183 hash_value = XXH64(tmpbuf, buffer_len, 0);
184 memprintf(&s->cookie, "%016llx", hash_value);
185 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200186 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100187 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100188
189 /* Don't bother checking if the dyncookie is duplicated if
190 * the server is marked as "disabled", maybe it doesn't have
191 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100192 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100193 if (!(s->next_admin & SRV_ADMF_FMAINT))
194 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200195 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200196 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100197}
198
Willy Tarreau21faa912012-10-10 08:27:36 +0200199/*
Thayne McCombs92149f92020-11-20 01:28:26 -0700200 * Must be called with the server lock held, and will write-lock the proxy.
201 */
202static void srv_set_addr_desc(struct server *s)
203{
204 struct proxy *p = s->proxy;
205 char *key;
206
207 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
208
209 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700210 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700211 free(key);
212 return;
213 }
214
215 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
216 ebpt_delete(&s->addr_node);
217 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
218
219 free(s->addr_node.key);
220 }
221
222 s->addr_node.key = key;
223
Thayne McCombs24da7e12021-01-05 23:10:09 -0700224 if (s->addr_node.key) {
225 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
226 ebis_insert(&p->used_server_addr, &s->addr_node);
227 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
228 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700229}
230
231/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200232 * Registers the server keyword list <kwl> as a list of valid keywords for next
233 * parsing sessions.
234 */
235void srv_register_keywords(struct srv_kw_list *kwl)
236{
Willy Tarreau2b718102021-04-21 07:32:39 +0200237 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200238}
239
240/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
241 * keyword is found with a NULL ->parse() function, then an attempt is made to
242 * find one with a valid ->parse() function. This way it is possible to declare
243 * platform-dependant, known keywords as NULL, then only declare them as valid
244 * if some options are met. Note that if the requested keyword contains an
245 * opening parenthesis, everything from this point is ignored.
246 */
247struct srv_kw *srv_find_kw(const char *kw)
248{
249 int index;
250 const char *kwend;
251 struct srv_kw_list *kwl;
252 struct srv_kw *ret = NULL;
253
254 kwend = strchr(kw, '(');
255 if (!kwend)
256 kwend = kw + strlen(kw);
257
258 list_for_each_entry(kwl, &srv_keywords.list, list) {
259 for (index = 0; kwl->kw[index].kw != NULL; index++) {
260 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
261 kwl->kw[index].kw[kwend-kw] == 0) {
262 if (kwl->kw[index].parse)
263 return &kwl->kw[index]; /* found it !*/
264 else
265 ret = &kwl->kw[index]; /* may be OK */
266 }
267 }
268 }
269 return ret;
270}
271
272/* Dumps all registered "server" keywords to the <out> string pointer. The
273 * unsupported keywords are only dumped if their supported form was not
274 * found.
275 */
276void srv_dump_kws(char **out)
277{
278 struct srv_kw_list *kwl;
279 int index;
280
Christopher Faulet784063e2020-05-18 12:14:18 +0200281 if (!out)
282 return;
283
Willy Tarreau21faa912012-10-10 08:27:36 +0200284 *out = NULL;
285 list_for_each_entry(kwl, &srv_keywords.list, list) {
286 for (index = 0; kwl->kw[index].kw != NULL; index++) {
287 if (kwl->kw[index].parse ||
288 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
289 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
290 kwl->scope,
291 kwl->kw[index].kw,
292 kwl->kw[index].skip ? " <arg>" : "",
293 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
294 kwl->kw[index].parse ? "" : " (not supported)");
295 }
296 }
297 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100298}
299
300/* Try to find in srv_keyword the word that looks closest to <word> by counting
301 * transitions between letters, digits and other characters. Will return the
302 * best matching word if found, otherwise NULL. An optional array of extra
303 * words to compare may be passed in <extra>, but it must then be terminated
304 * by a NULL entry. If unused it may be NULL.
305 */
306static const char *srv_find_best_kw(const char *word)
307{
308 uint8_t word_sig[1024];
309 uint8_t list_sig[1024];
310 const struct srv_kw_list *kwl;
311 const char *best_ptr = NULL;
312 int dist, best_dist = INT_MAX;
313 const char **extra;
314 int index;
315
316 make_word_fingerprint(word_sig, word);
317 list_for_each_entry(kwl, &srv_keywords.list, list) {
318 for (index = 0; kwl->kw[index].kw != NULL; index++) {
319 make_word_fingerprint(list_sig, kwl->kw[index].kw);
320 dist = word_fingerprint_distance(word_sig, list_sig);
321 if (dist < best_dist) {
322 best_dist = dist;
323 best_ptr = kwl->kw[index].kw;
324 }
325 }
326 }
327
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100328 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100329 make_word_fingerprint(list_sig, *extra);
330 dist = word_fingerprint_distance(word_sig, list_sig);
331 if (dist < best_dist) {
332 best_dist = dist;
333 best_ptr = *extra;
334 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100335 }
336
337 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
338 best_ptr = NULL;
339
340 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200341}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100342
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100343/* Parse the "backup" server keyword */
344static int srv_parse_backup(char **args, int *cur_arg,
345 struct proxy *curproxy, struct server *newsrv, char **err)
346{
347 newsrv->flags |= SRV_F_BACKUP;
348 return 0;
349}
350
Alexander Liu2a54bb72019-05-22 19:44:48 +0800351
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100352/* Parse the "cookie" server keyword */
353static int srv_parse_cookie(char **args, int *cur_arg,
354 struct proxy *curproxy, struct server *newsrv, char **err)
355{
356 char *arg;
357
358 arg = args[*cur_arg + 1];
359 if (!*arg) {
360 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
361 return ERR_ALERT | ERR_FATAL;
362 }
363
364 free(newsrv->cookie);
365 newsrv->cookie = strdup(arg);
366 newsrv->cklen = strlen(arg);
367 newsrv->flags |= SRV_F_COOKIESET;
368 return 0;
369}
370
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100371/* Parse the "disabled" server keyword */
372static int srv_parse_disabled(char **args, int *cur_arg,
373 struct proxy *curproxy, struct server *newsrv, char **err)
374{
Emeric Brun52a91d32017-08-31 14:41:55 +0200375 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
376 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100377 newsrv->check.state |= CHK_ST_PAUSED;
378 newsrv->check.health = 0;
379 return 0;
380}
381
382/* Parse the "enabled" server keyword */
383static int srv_parse_enabled(char **args, int *cur_arg,
384 struct proxy *curproxy, struct server *newsrv, char **err)
385{
Emeric Brun52a91d32017-08-31 14:41:55 +0200386 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
387 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100388 newsrv->check.state &= ~CHK_ST_PAUSED;
389 newsrv->check.health = newsrv->check.rise;
390 return 0;
391}
392
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100393/* Parse the "error-limit" server keyword */
394static int srv_parse_error_limit(char **args, int *cur_arg,
395 struct proxy *curproxy, struct server *newsrv, char **err)
396{
397 if (!*args[*cur_arg + 1]) {
398 memprintf(err, "'%s' expects an integer argument.",
399 args[*cur_arg]);
400 return ERR_ALERT | ERR_FATAL;
401 }
402
403 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
404
405 if (newsrv->consecutive_errors_limit <= 0) {
406 memprintf(err, "%s has to be > 0.",
407 args[*cur_arg]);
408 return ERR_ALERT | ERR_FATAL;
409 }
410
411 return 0;
412}
413
414/* Parse the "init-addr" server keyword */
415static int srv_parse_init_addr(char **args, int *cur_arg,
416 struct proxy *curproxy, struct server *newsrv, char **err)
417{
418 char *p, *end;
419 int done;
420 struct sockaddr_storage sa;
421
422 newsrv->init_addr_methods = 0;
423 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
424
425 for (p = args[*cur_arg + 1]; *p; p = end) {
426 /* cut on next comma */
427 for (end = p; *end && *end != ','; end++);
428 if (*end)
429 *(end++) = 0;
430
431 memset(&sa, 0, sizeof(sa));
432 if (strcmp(p, "libc") == 0) {
433 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
434 }
435 else if (strcmp(p, "last") == 0) {
436 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
437 }
438 else if (strcmp(p, "none") == 0) {
439 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
440 }
441 else if (str2ip2(p, &sa, 0)) {
442 if (is_addr(&newsrv->init_addr)) {
443 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
444 args[*cur_arg], p);
445 return ERR_ALERT | ERR_FATAL;
446 }
447 newsrv->init_addr = sa;
448 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
449 }
450 else {
451 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
452 args[*cur_arg], p);
453 return ERR_ALERT | ERR_FATAL;
454 }
455 if (!done) {
456 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
457 args[*cur_arg], p);
458 return ERR_ALERT | ERR_FATAL;
459 }
460 }
461
462 return 0;
463}
464
465/* Parse the "log-proto" server keyword */
466static int srv_parse_log_proto(char **args, int *cur_arg,
467 struct proxy *curproxy, struct server *newsrv, char **err)
468{
469 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
470 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
471 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
472 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
473 else {
474 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
475 args[*cur_arg], args[*cur_arg + 1]);
476 return ERR_ALERT | ERR_FATAL;
477 }
478
479 return 0;
480}
481
482/* Parse the "maxconn" server keyword */
483static int srv_parse_maxconn(char **args, int *cur_arg,
484 struct proxy *curproxy, struct server *newsrv, char **err)
485{
486 newsrv->maxconn = atol(args[*cur_arg + 1]);
487 return 0;
488}
489
490/* Parse the "maxqueue" server keyword */
491static int srv_parse_maxqueue(char **args, int *cur_arg,
492 struct proxy *curproxy, struct server *newsrv, char **err)
493{
494 newsrv->maxqueue = atol(args[*cur_arg + 1]);
495 return 0;
496}
497
498/* Parse the "minconn" server keyword */
499static int srv_parse_minconn(char **args, int *cur_arg,
500 struct proxy *curproxy, struct server *newsrv, char **err)
501{
502 newsrv->minconn = atol(args[*cur_arg + 1]);
503 return 0;
504}
505
Willy Tarreau9c538e02019-01-23 10:21:49 +0100506static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
507{
508 char *arg;
509
510 arg = args[*cur_arg + 1];
511 if (!*arg) {
512 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
513 return ERR_ALERT | ERR_FATAL;
514 }
515 newsrv->max_reuse = atoi(arg);
516
517 return 0;
518}
519
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100520static 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 +0100521{
522 const char *res;
523 char *arg;
524 unsigned int time;
525
526 arg = args[*cur_arg + 1];
527 if (!*arg) {
528 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
529 return ERR_ALERT | ERR_FATAL;
530 }
531 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200532 if (res == PARSE_TIME_OVER) {
533 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
534 args[*cur_arg+1], args[*cur_arg]);
535 return ERR_ALERT | ERR_FATAL;
536 }
537 else if (res == PARSE_TIME_UNDER) {
538 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
539 args[*cur_arg+1], args[*cur_arg]);
540 return ERR_ALERT | ERR_FATAL;
541 }
542 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100543 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
544 *res, args[*cur_arg]);
545 return ERR_ALERT | ERR_FATAL;
546 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100547 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100548
549 return 0;
550}
551
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200552static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
553{
554 char *arg;
555
556 arg = args[*cur_arg + 1];
557 if (!*arg) {
558 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
559 return ERR_ALERT | ERR_FATAL;
560 }
561
562 newsrv->low_idle_conns = atoi(arg);
563 return 0;
564}
565
Olivier Houchard006e3102018-12-10 18:30:32 +0100566static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
567{
568 char *arg;
569
570 arg = args[*cur_arg + 1];
571 if (!*arg) {
572 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
573 return ERR_ALERT | ERR_FATAL;
574 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100575
Olivier Houchard006e3102018-12-10 18:30:32 +0100576 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100577 if ((int)newsrv->max_idle_conns < -1) {
578 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
579 return ERR_ALERT | ERR_FATAL;
580 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100581
582 return 0;
583}
584
Willy Tarreaudff55432012-10-10 17:51:05 +0200585/* parse the "id" server keyword */
586static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
587{
588 struct eb32_node *node;
589
590 if (!*args[*cur_arg + 1]) {
591 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
592 return ERR_ALERT | ERR_FATAL;
593 }
594
595 newsrv->puid = atol(args[*cur_arg + 1]);
596 newsrv->conf.id.key = newsrv->puid;
597
598 if (newsrv->puid <= 0) {
599 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
600 return ERR_ALERT | ERR_FATAL;
601 }
602
603 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
604 if (node) {
605 struct server *target = container_of(node, struct server, conf.id);
606 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
607 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
608 target->id);
609 return ERR_ALERT | ERR_FATAL;
610 }
611
612 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200613 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200614 return 0;
615}
616
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100617/* Parse the "namespace" server keyword */
618static int srv_parse_namespace(char **args, int *cur_arg,
619 struct proxy *curproxy, struct server *newsrv, char **err)
620{
Willy Tarreaue5733232019-05-22 19:24:06 +0200621#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100622 char *arg;
623
624 arg = args[*cur_arg + 1];
625 if (!*arg) {
626 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
627 return ERR_ALERT | ERR_FATAL;
628 }
629
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100630 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100631 /* Use the namespace associated with the connection (if present). */
632 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
633 return 0;
634 }
635
636 /*
637 * As this parser may be called several times for the same 'default-server'
638 * object, or for a new 'server' instance deriving from a 'default-server'
639 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
640 */
641 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
642
643 newsrv->netns = netns_store_lookup(arg, strlen(arg));
644 if (!newsrv->netns)
645 newsrv->netns = netns_store_insert(arg);
646
647 if (!newsrv->netns) {
648 memprintf(err, "Cannot open namespace '%s'", arg);
649 return ERR_ALERT | ERR_FATAL;
650 }
651
652 return 0;
653#else
654 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
655 return ERR_ALERT | ERR_FATAL;
656#endif
657}
658
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100659/* Parse the "no-backup" server keyword */
660static int srv_parse_no_backup(char **args, int *cur_arg,
661 struct proxy *curproxy, struct server *newsrv, char **err)
662{
663 newsrv->flags &= ~SRV_F_BACKUP;
664 return 0;
665}
666
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100667
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100668/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200669static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100670{
671 srv->pp_opts &= ~flags;
672 return 0;
673}
674
675/* Parse the "no-send-proxy" server keyword */
676static int srv_parse_no_send_proxy(char **args, int *cur_arg,
677 struct proxy *curproxy, struct server *newsrv, char **err)
678{
679 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
680}
681
682/* Parse the "no-send-proxy-v2" server keyword */
683static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
684 struct proxy *curproxy, struct server *newsrv, char **err)
685{
686 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
687}
688
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200689/* Parse the "no-tfo" server keyword */
690static int srv_parse_no_tfo(char **args, int *cur_arg,
691 struct proxy *curproxy, struct server *newsrv, char **err)
692{
693 newsrv->flags &= ~SRV_F_FASTOPEN;
694 return 0;
695}
696
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100697/* Parse the "non-stick" server keyword */
698static int srv_parse_non_stick(char **args, int *cur_arg,
699 struct proxy *curproxy, struct server *newsrv, char **err)
700{
701 newsrv->flags |= SRV_F_NON_STICK;
702 return 0;
703}
704
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100705/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200706static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100707{
708 srv->pp_opts |= flags;
709 return 0;
710}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200711/* parse the "proto" server keyword */
712static int srv_parse_proto(char **args, int *cur_arg,
713 struct proxy *px, struct server *newsrv, char **err)
714{
715 struct ist proto;
716
717 if (!*args[*cur_arg + 1]) {
718 memprintf(err, "'%s' : missing value", args[*cur_arg]);
719 return ERR_ALERT | ERR_FATAL;
720 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100721 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200722 newsrv->mux_proto = get_mux_proto(proto);
723 if (!newsrv->mux_proto) {
724 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
725 return ERR_ALERT | ERR_FATAL;
726 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200727 return 0;
728}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100729
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100730/* parse the "proxy-v2-options" */
731static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
732 struct proxy *px, struct server *newsrv, char **err)
733{
734 char *p, *n;
735 for (p = args[*cur_arg+1]; p; p = n) {
736 n = strchr(p, ',');
737 if (n)
738 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100739 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100740 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100741 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100742 newsrv->pp_opts |= SRV_PP_V2_SSL;
743 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100744 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100745 newsrv->pp_opts |= SRV_PP_V2_SSL;
746 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100747 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100748 newsrv->pp_opts |= SRV_PP_V2_SSL;
749 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100750 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100751 newsrv->pp_opts |= SRV_PP_V2_SSL;
752 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100753 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100754 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100755 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100756 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100757 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100758 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100759 } else
760 goto fail;
761 }
762 return 0;
763 fail:
764 if (err)
765 memprintf(err, "'%s' : proxy v2 option not implemented", p);
766 return ERR_ALERT | ERR_FATAL;
767}
768
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100769/* Parse the "observe" server keyword */
770static int srv_parse_observe(char **args, int *cur_arg,
771 struct proxy *curproxy, struct server *newsrv, char **err)
772{
773 char *arg;
774
775 arg = args[*cur_arg + 1];
776 if (!*arg) {
777 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
778 return ERR_ALERT | ERR_FATAL;
779 }
780
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100781 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100782 newsrv->observe = HANA_OBS_NONE;
783 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100784 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100785 newsrv->observe = HANA_OBS_LAYER4;
786 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100787 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100788 if (curproxy->mode != PR_MODE_HTTP) {
789 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
790 return ERR_ALERT;
791 }
792 newsrv->observe = HANA_OBS_LAYER7;
793 }
794 else {
795 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
796 "but got '%s'\n", args[*cur_arg], arg);
797 return ERR_ALERT | ERR_FATAL;
798 }
799
800 return 0;
801}
802
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100803/* Parse the "on-error" server keyword */
804static int srv_parse_on_error(char **args, int *cur_arg,
805 struct proxy *curproxy, struct server *newsrv, char **err)
806{
807 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
808 newsrv->onerror = HANA_ONERR_FASTINTER;
809 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
810 newsrv->onerror = HANA_ONERR_FAILCHK;
811 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
812 newsrv->onerror = HANA_ONERR_SUDDTH;
813 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
814 newsrv->onerror = HANA_ONERR_MARKDWN;
815 else {
816 memprintf(err, "'%s' expects one of 'fastinter', "
817 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
818 args[*cur_arg], args[*cur_arg + 1]);
819 return ERR_ALERT | ERR_FATAL;
820 }
821
822 return 0;
823}
824
825/* Parse the "on-marked-down" server keyword */
826static int srv_parse_on_marked_down(char **args, int *cur_arg,
827 struct proxy *curproxy, struct server *newsrv, char **err)
828{
829 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
830 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
831 else {
832 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
833 args[*cur_arg], args[*cur_arg + 1]);
834 return ERR_ALERT | ERR_FATAL;
835 }
836
837 return 0;
838}
839
840/* Parse the "on-marked-up" server keyword */
841static int srv_parse_on_marked_up(char **args, int *cur_arg,
842 struct proxy *curproxy, struct server *newsrv, char **err)
843{
844 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
845 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
846 else {
847 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
848 args[*cur_arg], args[*cur_arg + 1]);
849 return ERR_ALERT | ERR_FATAL;
850 }
851
852 return 0;
853}
854
Frédéric Lécaille16186232017-03-14 16:42:49 +0100855/* Parse the "redir" server keyword */
856static int srv_parse_redir(char **args, int *cur_arg,
857 struct proxy *curproxy, struct server *newsrv, char **err)
858{
859 char *arg;
860
861 arg = args[*cur_arg + 1];
862 if (!*arg) {
863 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
864 return ERR_ALERT | ERR_FATAL;
865 }
866
867 free(newsrv->rdr_pfx);
868 newsrv->rdr_pfx = strdup(arg);
869 newsrv->rdr_len = strlen(arg);
870
871 return 0;
872}
873
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100874/* Parse the "resolvers" server keyword */
875static int srv_parse_resolvers(char **args, int *cur_arg,
876 struct proxy *curproxy, struct server *newsrv, char **err)
877{
878 free(newsrv->resolvers_id);
879 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
880 return 0;
881}
882
883/* Parse the "resolve-net" server keyword */
884static int srv_parse_resolve_net(char **args, int *cur_arg,
885 struct proxy *curproxy, struct server *newsrv, char **err)
886{
887 char *p, *e;
888 unsigned char mask;
889 struct resolv_options *opt;
890
891 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
892 memprintf(err, "'%s' expects a list of networks.",
893 args[*cur_arg]);
894 return ERR_ALERT | ERR_FATAL;
895 }
896
897 opt = &newsrv->resolv_opts;
898
899 /* Split arguments by comma, and convert it from ipv4 or ipv6
900 * string network in in_addr or in6_addr.
901 */
902 p = args[*cur_arg + 1];
903 e = p;
904 while (*p != '\0') {
905 /* If no room available, return error. */
906 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
907 memprintf(err, "'%s' exceed %d networks.",
908 args[*cur_arg], SRV_MAX_PREF_NET);
909 return ERR_ALERT | ERR_FATAL;
910 }
911 /* look for end or comma. */
912 while (*e != ',' && *e != '\0')
913 e++;
914 if (*e == ',') {
915 *e = '\0';
916 e++;
917 }
918 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
919 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
920 /* Try to convert input string from ipv4 or ipv6 network. */
921 opt->pref_net[opt->pref_net_nb].family = AF_INET;
922 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
923 &mask)) {
924 /* Try to convert input string from ipv6 network. */
925 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
926 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
927 } else {
928 /* All network conversions fail, return error. */
929 memprintf(err, "'%s' invalid network '%s'.",
930 args[*cur_arg], p);
931 return ERR_ALERT | ERR_FATAL;
932 }
933 opt->pref_net_nb++;
934 p = e;
935 }
936
937 return 0;
938}
939
940/* Parse the "resolve-opts" server keyword */
941static int srv_parse_resolve_opts(char **args, int *cur_arg,
942 struct proxy *curproxy, struct server *newsrv, char **err)
943{
944 char *p, *end;
945
946 for (p = args[*cur_arg + 1]; *p; p = end) {
947 /* cut on next comma */
948 for (end = p; *end && *end != ','; end++);
949 if (*end)
950 *(end++) = 0;
951
952 if (strcmp(p, "allow-dup-ip") == 0) {
953 newsrv->resolv_opts.accept_duplicate_ip = 1;
954 }
955 else if (strcmp(p, "ignore-weight") == 0) {
956 newsrv->resolv_opts.ignore_weight = 1;
957 }
958 else if (strcmp(p, "prevent-dup-ip") == 0) {
959 newsrv->resolv_opts.accept_duplicate_ip = 0;
960 }
961 else {
962 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
963 args[*cur_arg], p);
964 return ERR_ALERT | ERR_FATAL;
965 }
966 }
967
968 return 0;
969}
970
971/* Parse the "resolve-prefer" server keyword */
972static int srv_parse_resolve_prefer(char **args, int *cur_arg,
973 struct proxy *curproxy, struct server *newsrv, char **err)
974{
975 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
976 newsrv->resolv_opts.family_prio = AF_INET;
977 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
978 newsrv->resolv_opts.family_prio = AF_INET6;
979 else {
980 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
981 args[*cur_arg]);
982 return ERR_ALERT | ERR_FATAL;
983 }
984
985 return 0;
986}
987
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100988/* Parse the "send-proxy" server keyword */
989static int srv_parse_send_proxy(char **args, int *cur_arg,
990 struct proxy *curproxy, struct server *newsrv, char **err)
991{
992 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
993}
994
995/* Parse the "send-proxy-v2" server keyword */
996static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
997 struct proxy *curproxy, struct server *newsrv, char **err)
998{
999 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1000}
1001
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001002/* Parse the "slowstart" server keyword */
1003static int srv_parse_slowstart(char **args, int *cur_arg,
1004 struct proxy *curproxy, struct server *newsrv, char **err)
1005{
1006 /* slowstart is stored in seconds */
1007 unsigned int val;
1008 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1009
1010 if (time_err == PARSE_TIME_OVER) {
1011 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1012 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1013 return ERR_ALERT | ERR_FATAL;
1014 }
1015 else if (time_err == PARSE_TIME_UNDER) {
1016 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1017 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1018 return ERR_ALERT | ERR_FATAL;
1019 }
1020 else if (time_err) {
1021 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1022 *time_err, newsrv->id);
1023 return ERR_ALERT | ERR_FATAL;
1024 }
1025 newsrv->slowstart = (val + 999) / 1000;
1026
1027 return 0;
1028}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001029
1030/* Parse the "source" server keyword */
1031static int srv_parse_source(char **args, int *cur_arg,
1032 struct proxy *curproxy, struct server *newsrv, char **err)
1033{
1034 char *errmsg;
1035 int port_low, port_high;
1036 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001037
1038 errmsg = NULL;
1039
1040 if (!*args[*cur_arg + 1]) {
1041 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1042 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1043 goto err;
1044 }
1045
1046 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001047 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1048 &errmsg, NULL, NULL,
1049 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 +01001050 if (!sk) {
1051 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1052 goto err;
1053 }
1054
Frédéric Lécailledba97072017-03-17 15:33:50 +01001055 newsrv->conn_src.opts |= CO_SRC_BIND;
1056 newsrv->conn_src.source_addr = *sk;
1057
1058 if (port_low != port_high) {
1059 int i;
1060
Frédéric Lécailledba97072017-03-17 15:33:50 +01001061 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1062 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1063 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1064 }
1065
1066 *cur_arg += 2;
1067 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001068 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001069#if defined(CONFIG_HAP_TRANSPARENT)
1070 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001071 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1072 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001073 goto err;
1074 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001075 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001076 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1077 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1078 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001079 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001080 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1081 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1082 }
1083 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1084 char *name, *end;
1085
1086 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001087 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001088 name++;
1089
1090 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001091 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001092 end++;
1093
1094 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1095 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1096 free(newsrv->conn_src.bind_hdr_name);
1097 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1098 newsrv->conn_src.bind_hdr_len = end - name;
1099 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1100 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1101 newsrv->conn_src.bind_hdr_occ = -1;
1102
1103 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001104 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001105 end++;
1106 if (*end == ',') {
1107 end++;
1108 name = end;
1109 if (*end == '-')
1110 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001111 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001112 end++;
1113 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1114 }
1115
1116 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001117 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1118 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001119 goto err;
1120 }
1121 }
1122 else {
1123 struct sockaddr_storage *sk;
1124 int port1, port2;
1125
1126 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001127 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1128 &errmsg, NULL, NULL,
1129 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001130 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001131 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001132 goto err;
1133 }
1134
Frédéric Lécailledba97072017-03-17 15:33:50 +01001135 newsrv->conn_src.tproxy_addr = *sk;
1136 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1137 }
1138 global.last_checks |= LSTCHK_NETADM;
1139 *cur_arg += 2;
1140 continue;
1141#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001142 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 +01001143 goto err;
1144#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1145 } /* "usesrc" */
1146
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001147 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001148#ifdef SO_BINDTODEVICE
1149 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001150 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001151 goto err;
1152 }
1153 free(newsrv->conn_src.iface_name);
1154 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1155 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1156 global.last_checks |= LSTCHK_NETADM;
1157#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001158 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001159 goto err;
1160#endif
1161 *cur_arg += 2;
1162 continue;
1163 }
1164 /* this keyword in not an option of "source" */
1165 break;
1166 } /* while */
1167
1168 return 0;
1169
1170 err:
1171 free(errmsg);
1172 return ERR_ALERT | ERR_FATAL;
1173}
1174
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001175/* Parse the "stick" server keyword */
1176static int srv_parse_stick(char **args, int *cur_arg,
1177 struct proxy *curproxy, struct server *newsrv, char **err)
1178{
1179 newsrv->flags &= ~SRV_F_NON_STICK;
1180 return 0;
1181}
1182
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001183/* Parse the "track" server keyword */
1184static int srv_parse_track(char **args, int *cur_arg,
1185 struct proxy *curproxy, struct server *newsrv, char **err)
1186{
1187 char *arg;
1188
1189 arg = args[*cur_arg + 1];
1190 if (!*arg) {
1191 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1192 return ERR_ALERT | ERR_FATAL;
1193 }
1194
1195 free(newsrv->trackit);
1196 newsrv->trackit = strdup(arg);
1197
1198 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001199}
1200
1201/* Parse the "socks4" server keyword */
1202static int srv_parse_socks4(char **args, int *cur_arg,
1203 struct proxy *curproxy, struct server *newsrv, char **err)
1204{
1205 char *errmsg;
1206 int port_low, port_high;
1207 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001208
1209 errmsg = NULL;
1210
1211 if (!*args[*cur_arg + 1]) {
1212 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1213 goto err;
1214 }
1215
1216 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001217 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1218 &errmsg, NULL, NULL,
1219 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001220 if (!sk) {
1221 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1222 goto err;
1223 }
1224
Alexander Liu2a54bb72019-05-22 19:44:48 +08001225 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1226 newsrv->socks4_addr = *sk;
1227
Alexander Liu2a54bb72019-05-22 19:44:48 +08001228 return 0;
1229
1230 err:
1231 free(errmsg);
1232 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001233}
1234
Frédéric Lécailledba97072017-03-17 15:33:50 +01001235
Willy Tarreau034c88c2017-01-23 23:36:45 +01001236/* parse the "tfo" server keyword */
1237static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1238{
1239 newsrv->flags |= SRV_F_FASTOPEN;
1240 return 0;
1241}
1242
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001243/* parse the "usesrc" server keyword */
1244static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1245{
1246 memprintf(err, "'%s' only allowed after a '%s' statement.",
1247 "usesrc", "source");
1248 return ERR_ALERT | ERR_FATAL;
1249}
1250
1251/* parse the "weight" server keyword */
1252static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1253{
1254 int w;
1255
1256 w = atol(args[*cur_arg + 1]);
1257 if (w < 0 || w > SRV_UWGHT_MAX) {
1258 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1259 newsrv->id, SRV_UWGHT_MAX, w);
1260 return ERR_ALERT | ERR_FATAL;
1261 }
1262 newsrv->uweight = newsrv->iweight = w;
1263
1264 return 0;
1265}
1266
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001267/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001268 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001269 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001270 *
1271 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001272 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001273void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001274{
Willy Tarreau751153e2021-02-17 13:33:24 +01001275 struct stream *stream;
1276 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001277 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001278
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001279 for (thr = 0; thr < global.nbthread; thr++)
1280 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1281 if (stream->srv_conn == srv)
1282 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001283}
1284
1285/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001286 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001287 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001288 *
1289 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001290 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001291void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001292{
1293 struct server *srv;
1294
1295 for (srv = px->srv; srv != NULL; srv = srv->next)
1296 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001297 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001298}
1299
Willy Tarreaubda92272014-05-20 21:55:30 +02001300/* Appends some information to a message string related to a server going UP or
1301 * DOWN. If both <forced> and <reason> are null and the server tracks another
1302 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001303 * If <check> is non-null, an entire string describing the check result will be
1304 * appended after a comma and a space (eg: to report some information from the
1305 * check that changed the state). In the other case, the string will be built
1306 * using the check results stored into the struct server if present.
1307 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001308 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001309 *
1310 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001311 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001312void srv_append_status(struct buffer *msg, struct server *s,
1313 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001314{
Emeric Brun5a133512017-10-19 14:42:30 +02001315 short status = s->op_st_chg.status;
1316 short code = s->op_st_chg.code;
1317 long duration = s->op_st_chg.duration;
1318 char *desc = s->op_st_chg.reason;
1319
1320 if (check) {
1321 status = check->status;
1322 code = check->code;
1323 duration = check->duration;
1324 desc = check->desc;
1325 }
1326
1327 if (status != -1) {
1328 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1329
1330 if (status >= HCHK_STATUS_L57DATA)
1331 chunk_appendf(msg, ", code: %d", code);
1332
1333 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001334 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001335
1336 chunk_appendf(msg, ", info: \"");
1337
1338 chunk_initlen(&src, desc, 0, strlen(desc));
1339 chunk_asciiencode(msg, &src, '"');
1340
1341 chunk_appendf(msg, "\"");
1342 }
1343
1344 if (duration >= 0)
1345 chunk_appendf(msg, ", check duration: %ldms", duration);
1346 }
1347 else if (desc && *desc) {
1348 chunk_appendf(msg, ", %s", desc);
1349 }
1350 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001351 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001352 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001353
1354 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001355 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001356 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1357 " %d sessions active, %d requeued, %d remaining in queue",
1358 s->proxy->srv_act, s->proxy->srv_bck,
1359 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1360 s->cur_sess, xferred, s->nbpend);
1361 else
1362 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1363 " %d sessions requeued, %d total in queue",
1364 s->proxy->srv_act, s->proxy->srv_bck,
1365 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1366 xferred, s->nbpend);
1367 }
1368}
1369
Emeric Brun5a133512017-10-19 14:42:30 +02001370/* Marks server <s> down, regardless of its checks' statuses. The server is
1371 * registered in a list to postpone the counting of the remaining servers on
1372 * the proxy and transfers queued streams whenever possible to other servers at
1373 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1374 * non-null as the reason for going down or the available data from the check
1375 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001376 *
1377 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001378 */
Emeric Brun5a133512017-10-19 14:42:30 +02001379void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001380{
1381 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001382
Emeric Brun64cc49c2017-10-03 14:46:45 +02001383 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001384 return;
1385
Emeric Brun52a91d32017-08-31 14:41:55 +02001386 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001387 *s->op_st_chg.reason = 0;
1388 s->op_st_chg.status = -1;
1389 if (reason) {
1390 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1391 }
1392 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001393 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001394 s->op_st_chg.code = check->code;
1395 s->op_st_chg.status = check->status;
1396 s->op_st_chg.duration = check->duration;
1397 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001398
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001399 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001400 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001401
Emeric Brun9f0b4582017-10-23 14:39:51 +02001402 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001403 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001404 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001405 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001406 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001407}
1408
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001409/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001410 * in maintenance. The server is registered in a list to postpone the counting
1411 * of the remaining servers on the proxy and tries to grab requests from the
1412 * proxy at a sync point. Maintenance servers are ignored. It stores the
1413 * <reason> if non-null as the reason for going down or the available data
1414 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001415 *
1416 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001417 */
Emeric Brun5a133512017-10-19 14:42:30 +02001418void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001419{
1420 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001421
Emeric Brun64cc49c2017-10-03 14:46:45 +02001422 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001423 return;
1424
Emeric Brun52a91d32017-08-31 14:41:55 +02001425 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001426 return;
1427
Emeric Brun52a91d32017-08-31 14:41:55 +02001428 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001429 *s->op_st_chg.reason = 0;
1430 s->op_st_chg.status = -1;
1431 if (reason) {
1432 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1433 }
1434 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001435 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001436 s->op_st_chg.code = check->code;
1437 s->op_st_chg.status = check->status;
1438 s->op_st_chg.duration = check->duration;
1439 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001440
Emeric Brun64cc49c2017-10-03 14:46:45 +02001441 if (s->slowstart <= 0)
1442 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001443
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001444 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001445 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001446
Emeric Brun9f0b4582017-10-23 14:39:51 +02001447 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001448 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001449 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001450 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001451 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001452}
1453
Willy Tarreau8eb77842014-05-21 13:54:57 +02001454/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001455 * isn't in maintenance. The server is registered in a list to postpone the
1456 * counting of the remaining servers on the proxy and tries to grab requests
1457 * from the proxy. Maintenance servers are ignored. It stores the
1458 * <reason> if non-null as the reason for going down or the available data
1459 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001460 * up. Note that it makes use of the trash to build the log strings, so <reason>
1461 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001462 *
1463 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001464 */
Emeric Brun5a133512017-10-19 14:42:30 +02001465void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001466{
1467 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001468
Emeric Brun64cc49c2017-10-03 14:46:45 +02001469 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001470 return;
1471
Emeric Brun52a91d32017-08-31 14:41:55 +02001472 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001473 return;
1474
Emeric Brun52a91d32017-08-31 14:41:55 +02001475 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001476 *s->op_st_chg.reason = 0;
1477 s->op_st_chg.status = -1;
1478 if (reason) {
1479 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1480 }
1481 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001482 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001483 s->op_st_chg.code = check->code;
1484 s->op_st_chg.status = check->status;
1485 s->op_st_chg.duration = check->duration;
1486 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001487
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001488 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001489 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001490
Emeric Brun9f0b4582017-10-23 14:39:51 +02001491 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001492 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001493 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001494 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001495 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001496}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001497
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001498/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1499 * enforce either maint mode or drain mode. It is not allowed to set more than
1500 * one flag at once. The equivalent "inherited" flag is propagated to all
1501 * tracking servers. Maintenance mode disables health checks (but not agent
1502 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001503 * is done. If <cause> is non-null, it will be displayed at the end of the log
1504 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001505 *
1506 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001507 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001508void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001509{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001510 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001511
1512 if (!mode)
1513 return;
1514
1515 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001516 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001517 return;
1518
Emeric Brun52a91d32017-08-31 14:41:55 +02001519 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001520 if (cause)
1521 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1522
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001523 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001524 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001525
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001526 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001527 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1528 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001529 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001530
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001531 /* compute the inherited flag to propagate */
1532 if (mode & SRV_ADMF_MAINT)
1533 mode = SRV_ADMF_IMAINT;
1534 else if (mode & SRV_ADMF_DRAIN)
1535 mode = SRV_ADMF_IDRAIN;
1536
Emeric Brun9f0b4582017-10-23 14:39:51 +02001537 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001538 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001539 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001540 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001541 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001542}
1543
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001544/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1545 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1546 * than one flag at once. The equivalent "inherited" flag is propagated to all
1547 * tracking servers. Leaving maintenance mode re-enables health checks. When
1548 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001549 *
1550 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001551 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001552void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001553{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001554 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001555
1556 if (!mode)
1557 return;
1558
1559 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001560 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001561 return;
1562
Emeric Brun52a91d32017-08-31 14:41:55 +02001563 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001564
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001565 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001566 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001567
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001568 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001569 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1570 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001571 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001572
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001573 if (mode & SRV_ADMF_MAINT)
1574 mode = SRV_ADMF_IMAINT;
1575 else if (mode & SRV_ADMF_DRAIN)
1576 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001577
Emeric Brun9f0b4582017-10-23 14:39:51 +02001578 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001579 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001580 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001581 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001582 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001583}
1584
Willy Tarreau757478e2016-11-03 19:22:19 +01001585/* principle: propagate maint and drain to tracking servers. This is useful
1586 * upon startup so that inherited states are correct.
1587 */
1588static void srv_propagate_admin_state(struct server *srv)
1589{
1590 struct server *srv2;
1591
1592 if (!srv->trackers)
1593 return;
1594
1595 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001596 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001597 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001598 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001599
Emeric Brun52a91d32017-08-31 14:41:55 +02001600 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001601 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001602 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001603 }
1604}
1605
1606/* Compute and propagate the admin states for all servers in proxy <px>.
1607 * Only servers *not* tracking another one are considered, because other
1608 * ones will be handled when the server they track is visited.
1609 */
1610void srv_compute_all_admin_states(struct proxy *px)
1611{
1612 struct server *srv;
1613
1614 for (srv = px->srv; srv; srv = srv->next) {
1615 if (srv->track)
1616 continue;
1617 srv_propagate_admin_state(srv);
1618 }
1619}
1620
Willy Tarreaudff55432012-10-10 17:51:05 +02001621/* Note: must not be declared <const> as its list will be overwritten.
1622 * Please take care of keeping this list alphabetically sorted, doing so helps
1623 * all code contributors.
1624 * Optional keywords are also declared with a NULL ->parse() function so that
1625 * the config parser can report an appropriate error when a known keyword was
1626 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001627 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001628 */
1629static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001630 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001631 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001632 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1633 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001634 { "error-limit", srv_parse_error_limit, 1, 1, 0 }, /* Configure the consecutive count of check failures to consider a server on error */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001635 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001636 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1637 { "log-proto", srv_parse_log_proto, 1, 1, 0 }, /* Set the protocol for event messages, only relevant in a ring section */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001638 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1639 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001640 { "max-reuse", srv_parse_max_reuse, 1, 1, 0 }, /* Set the max number of requests on a connection, -1 means unlimited */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001641 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001642 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001643 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1644 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1645 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1646 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001647 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
1648 { "observe", srv_parse_observe, 1, 1, 0 }, /* Enables health adjusting based on observing communication with the server */
1649 { "on-error", srv_parse_on_error, 1, 1, 0 }, /* Configure the action on check failure */
1650 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 0 }, /* Configure the action when a server is marked down */
1651 { "on-marked-up", srv_parse_on_marked_up, 1, 1, 0 }, /* Configure the action when a server is marked up */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001652 { "pool-low-conn", srv_parse_pool_low_conn, 1, 1, 1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */
1653 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1654 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Amaury Denoyelle30467232021-03-12 18:03:27 +01001655 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001656 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001657 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001658 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001659 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001660 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001661 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001662 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1663 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1, 1 }, /* Enforce use of PROXY V2 protocol */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001664 { "slowstart", srv_parse_slowstart, 1, 1, 0 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001665 { "source", srv_parse_source, -1, 1, 1 }, /* Set the source address to be used to connect to the server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001666 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001667 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001668 { "track", srv_parse_track, 1, 1, 0 }, /* Set the current state of the server, tracking another one */
1669 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001670 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1671 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001672 { NULL, NULL, 0 },
1673}};
1674
Willy Tarreau0108d902018-11-25 19:14:37 +01001675INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001676
Willy Tarreau004e0452013-11-21 11:22:01 +01001677/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001678 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001679 * state is automatically disabled if the time is elapsed. If <must_update> is
1680 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001681 *
1682 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001683 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001684void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001685{
1686 struct proxy *px = sv->proxy;
1687 unsigned w;
1688
1689 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1690 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001691 if (sv->next_state == SRV_ST_STARTING)
1692 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001693 }
1694
1695 /* We must take care of not pushing the server to full throttle during slow starts.
1696 * It must also start immediately, at least at the minimal step when leaving maintenance.
1697 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001698 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001699 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1700 else
1701 w = px->lbprm.wdiv;
1702
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001704
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001705 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001706 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001707 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001708}
1709
Willy Tarreaubaaee002006-06-26 02:48:02 +02001710/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001711 * Parses weight_str and configures sv accordingly.
1712 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001713 *
1714 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001715 */
1716const char *server_parse_weight_change_request(struct server *sv,
1717 const char *weight_str)
1718{
1719 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001720 long int w;
1721 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001722
1723 px = sv->proxy;
1724
1725 /* if the weight is terminated with '%', it is set relative to
1726 * the initial weight, otherwise it is absolute.
1727 */
1728 if (!*weight_str)
1729 return "Require <weight> or <weight%>.\n";
1730
Simon Hormanb796afa2013-02-12 10:45:53 +09001731 w = strtol(weight_str, &end, 10);
1732 if (end == weight_str)
1733 return "Empty weight string empty or preceded by garbage";
1734 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001735 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001736 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001737 /* Avoid integer overflow */
1738 if (w > 25600)
1739 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001740 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001741 if (w > 256)
1742 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001743 }
1744 else if (w < 0 || w > 256)
1745 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001746 else if (end[0] != '\0')
1747 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001748
1749 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1750 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1751
1752 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001753 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001754
1755 return NULL;
1756}
1757
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001758/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001759 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1760 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001761 * Returns:
1762 * - error string on error
1763 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001764 *
1765 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001766 */
1767const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001768 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001769{
1770 unsigned char ip[INET6_ADDRSTRLEN];
1771
1772 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001773 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001774 return NULL;
1775 }
1776 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001777 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001778 return NULL;
1779 }
1780
1781 return "Could not understand IP address format.\n";
1782}
1783
Willy Tarreau46b7f532018-08-21 11:54:26 +02001784/*
1785 * Must be called with the server lock held.
1786 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001787const char *server_parse_maxconn_change_request(struct server *sv,
1788 const char *maxconn_str)
1789{
1790 long int v;
1791 char *end;
1792
1793 if (!*maxconn_str)
1794 return "Require <maxconn>.\n";
1795
1796 v = strtol(maxconn_str, &end, 10);
1797 if (end == maxconn_str)
1798 return "maxconn string empty or preceded by garbage";
1799 else if (end[0] != '\0')
1800 return "Trailing garbage in maxconn string";
1801
1802 if (sv->maxconn == sv->minconn) { // static maxconn
1803 sv->maxconn = sv->minconn = v;
1804 } else { // dynamic maxconn
1805 sv->maxconn = v;
1806 }
1807
1808 if (may_dequeue_tasks(sv, sv->proxy))
1809 process_srv_queue(sv);
1810
1811 return NULL;
1812}
1813
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001814#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001815static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1816 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001817{
1818 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001819 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001820 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001821 NULL,
1822 };
1823
1824 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001825 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001826
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001827 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001828}
1829
1830static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1831{
1832 struct sample_expr *expr;
1833
1834 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 +01001835 if (!expr) {
1836 memprintf(err, "error detected while parsing sni expression : %s", *err);
1837 return ERR_ALERT | ERR_FATAL;
1838 }
1839
1840 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1841 memprintf(err, "error detected while parsing sni expression : "
1842 " fetch method '%s' extracts information from '%s', "
1843 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001844 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001845 return ERR_ALERT | ERR_FATAL;
1846 }
1847
1848 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1849 release_sample_expr(newsrv->ssl_ctx.sni);
1850 newsrv->ssl_ctx.sni = expr;
1851
1852 return 0;
1853}
1854#endif
1855
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001856static 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 +01001857{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001858 char *msg = "error encountered while processing ";
1859 char *quote = "'";
1860 char *token = args[cur_arg];
1861
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001862 if (err && *err) {
1863 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001864 msg = *err;
1865 quote = "";
1866 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001867 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001868
1869 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1870 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1871 file, linenum, args[0], args[1],
1872 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001873 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001874 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1875 file, linenum, args[0], args[1],
1876 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001877}
1878
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001879static void srv_conn_src_sport_range_cpy(struct server *srv,
1880 struct server *src)
1881{
1882 int range_sz;
1883
1884 range_sz = src->conn_src.sport_range->size;
1885 if (range_sz > 0) {
1886 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1887 if (srv->conn_src.sport_range != NULL) {
1888 int i;
1889
1890 for (i = 0; i < range_sz; i++) {
1891 srv->conn_src.sport_range->ports[i] =
1892 src->conn_src.sport_range->ports[i];
1893 }
1894 }
1895 }
1896}
1897
1898/*
1899 * Copy <src> server connection source settings to <srv> server everything needed.
1900 */
1901static void srv_conn_src_cpy(struct server *srv, struct server *src)
1902{
1903 srv->conn_src.opts = src->conn_src.opts;
1904 srv->conn_src.source_addr = src->conn_src.source_addr;
1905
1906 /* Source port range copy. */
1907 if (src->conn_src.sport_range != NULL)
1908 srv_conn_src_sport_range_cpy(srv, src);
1909
1910#ifdef CONFIG_HAP_TRANSPARENT
1911 if (src->conn_src.bind_hdr_name != NULL) {
1912 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1913 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1914 }
1915 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1916 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1917#endif
1918 if (src->conn_src.iface_name != NULL)
1919 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1920}
1921
1922/*
1923 * Copy <src> server SSL settings to <srv> server allocating
1924 * everything needed.
1925 */
1926#if defined(USE_OPENSSL)
1927static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1928{
1929 if (src->ssl_ctx.ca_file != NULL)
1930 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1931 if (src->ssl_ctx.crl_file != NULL)
1932 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001933
1934 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1935
1936 if (src->ssl_ctx.verify_host != NULL)
1937 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1938 if (src->ssl_ctx.ciphers != NULL)
1939 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001940 if (src->ssl_ctx.options)
1941 srv->ssl_ctx.options = src->ssl_ctx.options;
1942 if (src->ssl_ctx.methods.flags)
1943 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1944 if (src->ssl_ctx.methods.min)
1945 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1946 if (src->ssl_ctx.methods.max)
1947 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1948
Ilya Shipitsin2aa4b3a2021-01-07 11:55:45 +05001949#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001950 if (src->ssl_ctx.ciphersuites != NULL)
1951 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1952#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001953 if (src->sni_expr != NULL)
1954 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001955
1956#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1957 if (src->ssl_ctx.alpn_str) {
1958 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1959 if (srv->ssl_ctx.alpn_str) {
1960 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1961 src->ssl_ctx.alpn_len);
1962 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1963 }
1964 }
1965#endif
1966#ifdef OPENSSL_NPN_NEGOTIATED
1967 if (src->ssl_ctx.npn_str) {
1968 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1969 if (srv->ssl_ctx.npn_str) {
1970 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1971 src->ssl_ctx.npn_len);
1972 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1973 }
1974 }
1975#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001976}
1977#endif
1978
1979/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001980 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001981 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001982 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001983 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01001984int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001985{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001986 char *hostname_dn;
1987 int hostname_len, hostname_dn_len;
1988
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001989 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001990 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001991
Christopher Faulet67957bd2017-09-27 11:00:59 +02001992 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001993 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01001994 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
1995 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 if (hostname_dn_len == -1)
1997 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001998
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001999
Christopher Faulet67957bd2017-09-27 11:00:59 +02002000 free(srv->hostname);
2001 free(srv->hostname_dn);
2002 srv->hostname = strdup(hostname);
2003 srv->hostname_dn = strdup(hostname_dn);
2004 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002005 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002006 goto err;
2007
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002008 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002009
2010 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002011 ha_free(&srv->hostname);
2012 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002013 return -1;
2014}
2015
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002016/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002017 * Copy <src> server settings to <srv> server allocating
2018 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002019 * This function is not supposed to be called at any time, but only
2020 * during server settings parsing or during server allocations from
2021 * a server template, and just after having calloc()'ed a new server.
2022 * So, <src> may only be a default server (when parsing server settings)
2023 * or a server template (during server allocations from a server template).
2024 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2025 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002026 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002027static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002028{
2029 /* Connection source settings copy */
2030 srv_conn_src_cpy(srv, src);
2031
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002032 if (srv_tmpl) {
2033 srv->addr = src->addr;
2034 srv->svc_port = src->svc_port;
2035 }
2036
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002037 srv->pp_opts = src->pp_opts;
2038 if (src->rdr_pfx != NULL) {
2039 srv->rdr_pfx = strdup(src->rdr_pfx);
2040 srv->rdr_len = src->rdr_len;
2041 }
2042 if (src->cookie != NULL) {
2043 srv->cookie = strdup(src->cookie);
2044 srv->cklen = src->cklen;
2045 }
2046 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002047 srv->check.addr = src->check.addr;
2048 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002049 srv->check.use_ssl = src->check.use_ssl;
2050 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002051 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002052 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002053 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002054 /* Note: 'flags' field has potentially been already initialized. */
2055 srv->flags |= src->flags;
2056 srv->do_check = src->do_check;
2057 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002058 srv->check.inter = src->check.inter;
2059 srv->check.fastinter = src->check.fastinter;
2060 srv->check.downinter = src->check.downinter;
2061 srv->agent.use_ssl = src->agent.use_ssl;
2062 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002063
2064 if (src->agent.tcpcheck_rules) {
2065 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2066 if (srv->agent.tcpcheck_rules) {
2067 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2068 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2069 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2070 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2071 &src->agent.tcpcheck_rules->preset_vars);
2072 }
2073 }
2074
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002075 srv->agent.inter = src->agent.inter;
2076 srv->agent.fastinter = src->agent.fastinter;
2077 srv->agent.downinter = src->agent.downinter;
2078 srv->maxqueue = src->maxqueue;
2079 srv->minconn = src->minconn;
2080 srv->maxconn = src->maxconn;
2081 srv->slowstart = src->slowstart;
2082 srv->observe = src->observe;
2083 srv->onerror = src->onerror;
2084 srv->onmarkeddown = src->onmarkeddown;
2085 srv->onmarkedup = src->onmarkedup;
2086 if (src->trackit != NULL)
2087 srv->trackit = strdup(src->trackit);
2088 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2089 srv->uweight = srv->iweight = src->iweight;
2090
2091 srv->check.send_proxy = src->check.send_proxy;
2092 /* health: up, but will fall down at first failure */
2093 srv->check.rise = srv->check.health = src->check.rise;
2094 srv->check.fall = src->check.fall;
2095
2096 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002097 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2098 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2099 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002100 srv->check.state |= CHK_ST_PAUSED;
2101 srv->check.health = 0;
2102 }
2103
2104 /* health: up but will fall down at first failure */
2105 srv->agent.rise = srv->agent.health = src->agent.rise;
2106 srv->agent.fall = src->agent.fall;
2107
2108 if (src->resolvers_id != NULL)
2109 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002110 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2111 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2112 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2113 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2114 srv->resolv_opts.family_prio = AF_INET6;
2115 memcpy(srv->resolv_opts.pref_net,
2116 src->resolv_opts.pref_net,
2117 sizeof srv->resolv_opts.pref_net);
2118 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002119
2120 srv->init_addr_methods = src->init_addr_methods;
2121 srv->init_addr = src->init_addr;
2122#if defined(USE_OPENSSL)
2123 srv_ssl_settings_cpy(srv, src);
2124#endif
2125#ifdef TCP_USER_TIMEOUT
2126 srv->tcp_ut = src->tcp_ut;
2127#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002128 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002129 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002130 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002131 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002132 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002133
Olivier Houchard8da5f982017-08-04 18:35:36 +02002134 if (srv_tmpl)
2135 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002136
2137 srv->check.via_socks4 = src->check.via_socks4;
2138 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002139}
2140
Willy Tarreau198e92a2021-03-05 10:23:32 +01002141/* allocate a server and attach it to the global servers_list. Returns
2142 * the server on success, otherwise NULL.
2143 */
William Lallemand313bfd12018-10-26 14:47:32 +02002144struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002145{
2146 struct server *srv;
2147
2148 srv = calloc(1, sizeof *srv);
2149 if (!srv)
2150 return NULL;
2151
2152 srv->obj_type = OBJ_TYPE_SERVER;
2153 srv->proxy = proxy;
Patrick Hemmer0355dab2018-05-11 12:52:31 -04002154 srv->pendconns = EB_ROOT;
Willy Tarreau2b718102021-04-21 07:32:39 +02002155 LIST_APPEND(&servers_list, &srv->global_list);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002156
Emeric Brun52a91d32017-08-31 14:41:55 +02002157 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002158 srv->last_change = now.tv_sec;
2159
Christopher Faulet38290462020-04-21 11:46:40 +02002160 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002161 srv->check.status = HCHK_STATUS_INI;
2162 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002163 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002164 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002165
Christopher Faulet38290462020-04-21 11:46:40 +02002166 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002167 srv->agent.status = HCHK_STATUS_INI;
2168 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002169 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002170 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002171#if defined(USE_QUIC)
2172 srv->cids = EB_ROOT_UNIQUE;
2173#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002174
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002175 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002176#ifdef USE_OPENSSL
2177 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2178#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002179
Willy Tarreau975b1552019-06-06 16:25:55 +02002180 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002181 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002182 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002183 return srv;
2184}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002185
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002186/* Deallocate a server <srv> and its member. <srv> must be allocated.
2187 */
2188void free_server(struct server *srv)
2189{
2190 task_destroy(srv->warmup);
2191
2192 free(srv->id);
2193 free(srv->cookie);
2194 free(srv->hostname);
2195 free(srv->hostname_dn);
2196 free((char*)srv->conf.file);
2197 free(srv->per_thr);
2198 free(srv->curr_idle_thr);
2199 free(srv->resolvers_id);
2200 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002201 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002202
2203 if (srv->use_ssl == 1 || srv->check.use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL)) {
2204 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2205 xprt_get(XPRT_SSL)->destroy_srv(srv);
2206 }
2207 HA_SPIN_DESTROY(&srv->lock);
2208
Willy Tarreau2b718102021-04-21 07:32:39 +02002209 LIST_DELETE(&srv->global_list);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002210
2211 EXTRA_COUNTERS_FREE(srv->extra_counters);
2212
2213 free(srv);
2214 srv = NULL;
2215}
2216
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002217/*
2218 * Parse as much as possible such a range string argument: low[-high]
2219 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2220 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2221 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2222 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002223 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002224static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2225 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002226{
2227 char *nb_high_arg;
2228
2229 *nb_high = 0;
2230 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002231 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002232
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002233 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002234 *nb_high_arg++ = '\0';
2235 *nb_high = atoi(nb_high_arg);
2236 }
2237 else {
2238 *nb_high += *nb_low;
2239 *nb_low = 1;
2240 }
2241
2242 if (*nb_low < 0 || *nb_high < *nb_low)
2243 return -1;
2244
2245 return 0;
2246}
2247
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002248/* Parse as much as possible such a range string argument: low[-high]
2249 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2250 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2251 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002252 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002253 * initialize a new server on startup.
2254 *
2255 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2256 * Returns 0 if succeeded, -1 if not.
2257 */
2258static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2259 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002260{
2261 chunk_printf(&trash, "%s%d", prefix, nb);
2262 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002263 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002264}
2265
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002266/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002267 * Note that a server template is a special server with
2268 * a few different parameters than a server which has
2269 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002270 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002271 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002272 * initialize a new server on startup.
2273 *
Joseph Herlant44466822018-11-15 08:57:51 -08002274 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002275 * 'srv' template included.
2276 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002277static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002278{
2279 int i;
2280 struct server *newsrv;
2281
2282 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 +02002283 newsrv = new_server(px);
2284 if (!newsrv)
2285 goto err;
2286
Christopher Faulet75bef002020-11-02 22:04:55 +01002287 newsrv->conf.file = strdup(srv->conf.file);
2288 newsrv->conf.line = srv->conf.line;
2289
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002290 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002291 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002292#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2293 if (newsrv->sni_expr) {
2294 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2295 if (!newsrv->ssl_ctx.sni)
2296 goto err;
2297 }
2298#endif
2299 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002300 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002301
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002302 /* Linked backwards first. This will be restablished after parsing. */
2303 newsrv->next = px->srv;
2304 px->srv = newsrv;
2305 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002306 _srv_parse_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002307
2308 return i - srv->tmpl_info.nb_low;
2309
2310 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002311 _srv_parse_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002312 if (newsrv) {
2313#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2314 release_sample_expr(newsrv->ssl_ctx.sni);
2315#endif
2316 free_check(&newsrv->agent);
2317 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002318 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002319 }
2320 free(newsrv);
2321 return i - srv->tmpl_info.nb_low;
2322}
2323
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002324/* Allocate a new server pointed by <srv> and try to parse the first arguments
2325 * in <args> as an address for a server or an address-range for a template or
2326 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2327 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002328 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002329 * initialize a new server on startup.
2330 *
2331 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2332 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2333 * <srv> will be set to NULL.
2334 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002335static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2336 struct proxy *curproxy,
2337 int parse_flags, char **errmsg)
Willy Tarreau272adea2014-03-31 10:39:59 +02002338{
2339 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002340 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002341 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002342 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002343 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002344
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002345 *srv = NULL;
2346
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002347 /* There is no mandatory first arguments for default server. */
2348 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2349 if (parse_flags & SRV_PARSE_TEMPLATE) {
2350 if (!*args[3]) {
2351 /* 'server-template' line number of argument check. */
2352 memprintf(errmsg, "'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.",
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002353 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002356 }
2357
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002358 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002359 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002360 else {
2361 if (!*args[2]) {
2362 /* 'server' line number of argument check. */
2363 memprintf(errmsg, "'%s' expects <name> and <addr>[:<port>] as arguments.",
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002364 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002369 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002370 }
2371
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002372 if (err) {
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002373 memprintf(errmsg, "character '%c' is not permitted in %s %s '%s'.",
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002374 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
2377 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002378 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002379
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002380 *cur_arg = 2;
2381 if (parse_flags & SRV_PARSE_TEMPLATE) {
2382 /* Parse server-template <nb | range> arg. */
2383 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
2384 memprintf(errmsg, "Wrong %s number or range arg '%s'.",
2385 args[0], args[*cur_arg]);
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002388 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002389 (*cur_arg)++;
2390 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002391
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002392 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2393 struct sockaddr_storage *sk;
2394 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002395
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002396 *srv = newsrv = new_server(curproxy);
2397 if (!newsrv) {
2398 memprintf(errmsg, "out of memory.");
2399 err_code |= ERR_ALERT | ERR_ABORT;
2400 goto out;
2401 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002402
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002403 if (parse_flags & SRV_PARSE_TEMPLATE) {
2404 newsrv->tmpl_info.nb_low = tmpl_range_low;
2405 newsrv->tmpl_info.nb_high = tmpl_range_high;
2406 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002407
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002408 if (parse_flags & SRV_PARSE_DYNAMIC)
2409 newsrv->flags |= SRV_F_DYNAMIC;
2410
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002411 /* Note: for a server template, its id is its prefix.
2412 * This is a temporary id which will be used for server allocations to come
2413 * after parsing.
2414 */
2415 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2416 newsrv->id = strdup(args[1]);
2417 else
2418 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002419
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002420 /* several ways to check the port component :
2421 * - IP => port=+0, relative (IPv4 only)
2422 * - IP: => port=+0, relative
2423 * - IP:N => port=N, absolute
2424 * - IP:+N => port=+N, relative
2425 * - IP:-N => port=-N, relative
2426 */
2427 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2428 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002429
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002430 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
2431 errmsg, NULL, &fqdn,
2432 (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | PA_O_PORT_OFS | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
2433 if (!sk) {
2434 memprintf(errmsg, "'%s %s' : %s", args[0], args[1], *errmsg);
2435 err_code |= ERR_ALERT | ERR_FATAL;
2436 goto out;
2437 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002438
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002439 if (!port1 || !port2) {
2440 /* no port specified, +offset, -offset */
2441 newsrv->flags |= SRV_F_MAPPORTS;
2442 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002443
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002444 /* save hostname and create associated name resolution */
2445 if (fqdn) {
2446 if (fqdn[0] == '_') { /* SRV record */
2447 /* Check if a SRV request already exists, and if not, create it */
2448 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2449 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2450 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002451 err_code |= ERR_ALERT | ERR_FATAL;
2452 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002453 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002454 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002455 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2456 memprintf(errmsg, "Can't create DNS resolution for server '%s'",
2457 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002458 err_code |= ERR_ALERT | ERR_FATAL;
2459 goto out;
2460 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002461 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002462
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002463 newsrv->addr = *sk;
2464 newsrv->svc_port = port;
2465 // we don't need to lock the server here, because
2466 // we are in the process of initializing
2467 srv_set_addr_desc(newsrv);
2468
2469 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
2470 memprintf(errmsg, "Unknown protocol family %d '%s'",
2471 newsrv->addr.ss_family, args[*cur_arg]);
2472 err_code |= ERR_ALERT | ERR_FATAL;
2473 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002474 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002475
2476 (*cur_arg)++;
2477 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002478 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2479 /* Copy default server settings to new server */
2480 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2481 } else {
2482 /* Initialize dynamic server weight to 1 */
2483 newsrv->uweight = newsrv->iweight = 1;
2484
2485 /* A dynamic server is disabled on startup */
2486 newsrv->next_admin = SRV_ADMF_FMAINT;
2487 newsrv->next_state = SRV_ST_STOPPED;
2488 server_recalc_eweight(newsrv, 0);
2489 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002490 HA_SPIN_INIT(&newsrv->lock);
2491 }
2492 else {
2493 *srv = newsrv = &curproxy->defsrv;
2494 *cur_arg = 1;
2495 newsrv->resolv_opts.family_prio = AF_INET6;
2496 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002497 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002498
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002499 free(fqdn);
2500 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002501
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002502out:
2503 free(fqdn);
2504 return err_code;
2505}
Willy Tarreau272adea2014-03-31 10:39:59 +02002506
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002507/* Parse the server keyword in <args>.
2508 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2509 * might not be the case if an error is reported.
2510 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002511 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002512 * initialize a new server on startup.
2513 *
2514 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2515 * interrupted.
2516 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002517static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2518 struct proxy *curproxy,
2519 int parse_flags, char **errmsg)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002520{
2521 int err_code = 0;
2522 struct srv_kw *kw;
2523 const char *best;
Willy Tarreau272adea2014-03-31 10:39:59 +02002524
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002525 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002526 if (!kw) {
2527 best = srv_find_best_kw(args[*cur_arg]);
2528 if (best)
2529 memprintf(errmsg, "unknown keyword '%s'; did you mean '%s' maybe ?",
2530 args[*cur_arg], best);
2531 else
2532 memprintf(errmsg, "unknown keyword '%s'.",
2533 args[*cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002534
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002535 return ERR_ALERT | ERR_FATAL;
2536 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002537
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002538 if (!kw->parse) {
2539 memprintf(errmsg, "'%s' option is not implemented in this version (check build options)",
2540 args[*cur_arg]);
2541 err_code = ERR_ALERT | ERR_FATAL;
2542 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002543 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002544
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002545 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002546 memprintf(errmsg, "'%s' option is not accepted in default-server sections",
2547 args[*cur_arg]);
2548 err_code = ERR_ALERT;
2549 goto out;
2550 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002551 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
2552 memprintf(errmsg, "'%s' option is not accepted for dynamic server",
2553 args[*cur_arg]);
2554 err_code |= ERR_ALERT;
2555 goto out;
2556 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002557
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002558 err_code = kw->parse(args, cur_arg, curproxy, srv, errmsg);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002559
2560out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002561 if (kw->skip != -1)
2562 *cur_arg += 1 + kw->skip;
2563
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002564 return err_code;
2565}
2566
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002567#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002568/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002569 * initialize a new server on startup.
2570 */
2571static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2572 struct server *srv, struct proxy *proxy,
2573 char **errmsg)
2574{
2575 int ret;
2576
2577 if (!srv->sni_expr)
2578 return 0;
2579
2580 ret = server_parse_sni_expr(srv, proxy, errmsg);
2581 if (!ret)
2582 return 0;
2583
2584 return ret;
2585}
2586#endif
2587
2588/* Server initializations finalization.
2589 * Initialize health check, agent check and SNI expression if enabled.
2590 * Must not be called for a default server instance.
2591 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002592 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002593 * initialize a new server on startup.
2594 */
2595static int _srv_parse_finalize(char **args, int cur_arg,
2596 struct server *srv, struct proxy *px,
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002597 int parse_flags, char **errmsg)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002598{
2599#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2600 int ret;
2601#endif
2602
2603 if (srv->do_check && srv->trackit) {
2604 memprintf(errmsg, "unable to enable checks and tracking at the same time!");
2605 return ERR_ALERT | ERR_FATAL;
2606 }
2607
2608 if (srv->do_agent && !srv->agent.port) {
2609 memprintf(errmsg, "server %s does not have agent port. Agent check has been disabled.",
2610 srv->id);
2611 return ERR_ALERT | ERR_FATAL;
2612 }
2613
2614#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2615 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, errmsg)) != 0)
2616 return ret;
2617#endif
2618
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002619 /* A dynamic server is disabled on startup. It must not be counted as
2620 * an active backend entry.
2621 */
2622 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2623 if (srv->flags & SRV_F_BACKUP)
2624 px->srv_bck++;
2625 else
2626 px->srv_act++;
2627 }
2628
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002629 srv_lb_commit_status(srv);
2630
2631 return 0;
2632}
2633
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002634int parse_server(const char *file, int linenum, char **args,
2635 struct proxy *curproxy, const struct proxy *defproxy,
2636 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002637{
2638 struct server *newsrv = NULL;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002639 char *errmsg = NULL;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002640 int err_code = 0;
2641
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002642 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002643
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002644 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
2645 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2646 err_code |= ERR_ALERT | ERR_FATAL;
2647 goto out;
2648 }
2649 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2650 err_code |= ERR_ALERT | ERR_FATAL;
2651 goto out;
2652 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002653
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002654 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2655 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2656 if (!*args[2])
2657 return 0;
2658 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002659
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002660 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
2661 parse_flags, &errmsg);
2662 if (errmsg) {
2663 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
2664 free(errmsg);
2665 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002666
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002667 /* the servers are linked backwards first */
2668 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2669 newsrv->next = curproxy->srv;
2670 curproxy->srv = newsrv;
2671 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002672
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002673 if (err_code & ERR_CODE)
2674 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002675
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002676 newsrv->conf.file = strdup(file);
2677 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002678
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002679 while (*args[cur_arg]) {
2680 errmsg = NULL;
2681 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
2682 parse_flags, &errmsg);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002683
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002684 if (err_code & ERR_ALERT) {
2685 display_parser_err(file, linenum, args, cur_arg, err_code, &errmsg);
2686 free(errmsg);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002687 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002688
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002689 if (err_code & ERR_FATAL)
2690 goto out;
2691 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002692
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002693 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002694 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags, &errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002695 if (err_code) {
2696 display_parser_err(file, linenum, args, cur_arg, err_code, &errmsg);
2697 free(errmsg);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002698 }
2699
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002700 if (err_code & ERR_FATAL)
2701 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002702 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002703
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002704 if (parse_flags & SRV_PARSE_TEMPLATE)
2705 _srv_parse_tmpl_init(newsrv, curproxy);
2706
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02002707 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02002708 "parsing [%s:%d] : 'server %s' : configured with weight of 0 will never be selected by load balancing algorithms\n",
2709 file, linenum, newsrv->id);
2710
Willy Tarreau272adea2014-03-31 10:39:59 +02002711 return 0;
2712
2713 out:
Willy Tarreau272adea2014-03-31 10:39:59 +02002714 return err_code;
2715}
2716
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002717/* Returns a pointer to the first server matching either id <id>.
2718 * NULL is returned if no match is found.
2719 * the lookup is performed in the backend <bk>
2720 */
2721struct server *server_find_by_id(struct proxy *bk, int id)
2722{
2723 struct eb32_node *eb32;
2724 struct server *curserver;
2725
2726 if (!bk || (id ==0))
2727 return NULL;
2728
2729 /* <bk> has no backend capabilities, so it can't have a server */
2730 if (!(bk->cap & PR_CAP_BE))
2731 return NULL;
2732
2733 curserver = NULL;
2734
2735 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2736 if (eb32)
2737 curserver = container_of(eb32, struct server, conf.id);
2738
2739 return curserver;
2740}
2741
2742/* Returns a pointer to the first server matching either name <name>, or id
2743 * if <name> starts with a '#'. NULL is returned if no match is found.
2744 * the lookup is performed in the backend <bk>
2745 */
2746struct server *server_find_by_name(struct proxy *bk, const char *name)
2747{
2748 struct server *curserver;
2749
2750 if (!bk || !name)
2751 return NULL;
2752
2753 /* <bk> has no backend capabilities, so it can't have a server */
2754 if (!(bk->cap & PR_CAP_BE))
2755 return NULL;
2756
2757 curserver = NULL;
2758 if (*name == '#') {
2759 curserver = server_find_by_id(bk, atoi(name + 1));
2760 if (curserver)
2761 return curserver;
2762 }
2763 else {
2764 curserver = bk->srv;
2765
2766 while (curserver && (strcmp(curserver->id, name) != 0))
2767 curserver = curserver->next;
2768
2769 if (curserver)
2770 return curserver;
2771 }
2772
2773 return NULL;
2774}
2775
2776struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2777{
2778 struct server *byname;
2779 struct server *byid;
2780
2781 if (!name && !id)
2782 return NULL;
2783
2784 if (diff)
2785 *diff = 0;
2786
2787 byname = byid = NULL;
2788
2789 if (name) {
2790 byname = server_find_by_name(bk, name);
2791 if (byname && (!id || byname->puid == id))
2792 return byname;
2793 }
2794
2795 /* remaining possibilities :
2796 * - name not set
2797 * - name set but not found
2798 * - name found but ID doesn't match
2799 */
2800 if (id) {
2801 byid = server_find_by_id(bk, id);
2802 if (byid) {
2803 if (byname) {
2804 /* use id only if forced by configuration */
2805 if (byid->flags & SRV_F_FORCED_ID) {
2806 if (diff)
2807 *diff |= 2;
2808 return byid;
2809 }
2810 else {
2811 if (diff)
2812 *diff |= 1;
2813 return byname;
2814 }
2815 }
2816
2817 /* remaining possibilities:
2818 * - name not set
2819 * - name set but not found
2820 */
2821 if (name && diff)
2822 *diff |= 2;
2823 return byid;
2824 }
2825
2826 /* id bot found */
2827 if (byname) {
2828 if (diff)
2829 *diff |= 1;
2830 return byname;
2831 }
2832 }
2833
2834 return NULL;
2835}
2836
Simon Horman7d09b9a2013-02-12 10:45:51 +09002837/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002838 * update a server's current IP address.
2839 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2840 * ip is in network format.
2841 * updater is a string which contains an information about the requester of the update.
2842 * updater is used if not NULL.
2843 *
2844 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02002845 *
2846 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02002847 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002848int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002849{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02002850 /* save the new IP family & address if necessary */
2851 switch (ip_sin_family) {
2852 case AF_INET:
2853 if (s->addr.ss_family == ip_sin_family &&
2854 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
2855 return 0;
2856 break;
2857 case AF_INET6:
2858 if (s->addr.ss_family == ip_sin_family &&
2859 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
2860 return 0;
2861 break;
2862 };
2863
Baptiste Assmann14e40142015-04-14 01:13:07 +02002864 /* generates a log line and a warning on stderr */
2865 if (1) {
2866 /* book enough space for both IPv4 and IPv6 */
2867 char oldip[INET6_ADDRSTRLEN];
2868 char newip[INET6_ADDRSTRLEN];
2869
2870 memset(oldip, '\0', INET6_ADDRSTRLEN);
2871 memset(newip, '\0', INET6_ADDRSTRLEN);
2872
2873 /* copy old IP address in a string */
2874 switch (s->addr.ss_family) {
2875 case AF_INET:
2876 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2877 break;
2878 case AF_INET6:
2879 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2880 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02002881 default:
2882 strcpy(oldip, "(none)");
2883 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02002884 };
2885
2886 /* copy new IP address in a string */
2887 switch (ip_sin_family) {
2888 case AF_INET:
2889 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2890 break;
2891 case AF_INET6:
2892 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2893 break;
2894 };
2895
2896 /* save log line into a buffer */
2897 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2898 s->proxy->id, s->id, oldip, newip, updater);
2899
2900 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002901 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002902
2903 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002904 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002905 }
2906
2907 /* save the new IP family */
2908 s->addr.ss_family = ip_sin_family;
2909 /* save the new IP address */
2910 switch (ip_sin_family) {
2911 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002912 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002913 break;
2914 case AF_INET6:
2915 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2916 break;
2917 };
Olivier Houchard4e694042017-03-14 20:01:29 +01002918 srv_set_dyncookie(s);
Thayne McCombs92149f92020-11-20 01:28:26 -07002919 srv_set_addr_desc(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002920
2921 return 0;
2922}
2923
William Dauchy7cabc062021-02-11 22:51:24 +01002924/* update agent health check address and port
2925 * addr can be ip4/ip6 or a hostname
2926 * if one error occurs, don't apply anything
2927 * must be called with the server lock held.
2928 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002929const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01002930{
2931 struct sockaddr_storage sk;
2932 struct buffer *msg;
2933 int new_port;
2934
2935 msg = get_trash_chunk();
2936 chunk_reset(msg);
2937
2938 if (!(s->agent.state & CHK_ST_ENABLED)) {
2939 chunk_strcat(msg, "agent checks are not enabled on this server");
2940 goto out;
2941 }
2942 if (addr) {
2943 memset(&sk, 0, sizeof(struct sockaddr_storage));
2944 if (str2ip(addr, &sk) == NULL) {
2945 chunk_appendf(msg, "invalid addr '%s'", addr);
2946 goto out;
2947 }
2948 }
2949 if (port) {
2950 if (strl2irc(port, strlen(port), &new_port) != 0) {
2951 chunk_appendf(msg, "provided port is not an integer");
2952 goto out;
2953 }
2954 if (new_port < 0 || new_port > 65535) {
2955 chunk_appendf(msg, "provided port is invalid");
2956 goto out;
2957 }
2958 }
2959out:
2960 if (msg->data)
2961 return msg->area;
2962 else {
2963 if (addr)
2964 set_srv_agent_addr(s, &sk);
2965 if (port)
2966 set_srv_agent_port(s, new_port);
2967 }
2968 return NULL;
2969}
2970
William Dauchyb456e1f2021-02-11 22:51:23 +01002971/* update server health check address and port
2972 * addr must be ip4 or ip6, it won't be resolved
2973 * if one error occurs, don't apply anything
2974 * must be called with the server lock held.
2975 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002976const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01002977{
2978 struct sockaddr_storage sk;
2979 struct buffer *msg;
2980 int new_port;
2981
2982 msg = get_trash_chunk();
2983 chunk_reset(msg);
2984
2985 if (!(s->check.state & CHK_ST_ENABLED)) {
2986 chunk_strcat(msg, "health checks are not enabled on this server");
2987 goto out;
2988 }
2989 if (addr) {
2990 memset(&sk, 0, sizeof(struct sockaddr_storage));
2991 if (str2ip2(addr, &sk, 0) == NULL) {
2992 chunk_appendf(msg, "invalid addr '%s'", addr);
2993 goto out;
2994 }
2995 }
2996 if (port) {
2997 if (strl2irc(port, strlen(port), &new_port) != 0) {
2998 chunk_appendf(msg, "provided port is not an integer");
2999 goto out;
3000 }
3001 if (new_port < 0 || new_port > 65535) {
3002 chunk_appendf(msg, "provided port is invalid");
3003 goto out;
3004 }
3005 /* prevent the update of port to 0 if MAPPORTS are in use */
3006 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3007 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3008 goto out;
3009 }
3010 }
3011out:
3012 if (msg->data)
3013 return msg->area;
3014 else {
3015 if (addr)
3016 s->check.addr = sk;
3017 if (port)
3018 s->check.port = new_port;
3019 }
3020 return NULL;
3021}
3022
Baptiste Assmann14e40142015-04-14 01:13:07 +02003023/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003024 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3025 *
3026 * Caller can pass its name through <updater> to get it integrated in the response message
3027 * returned by the function.
3028 *
3029 * The function first does the following, in that order:
3030 * - validates the new addr and/or port
3031 * - checks if an update is required (new IP or port is different than current ones)
3032 * - checks the update is allowed:
3033 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3034 * - allow all changes if no CHECKS are configured
3035 * - if CHECK is configured:
3036 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3037 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3038 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003039 *
3040 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003041 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003042const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003043{
3044 struct sockaddr_storage sa;
3045 int ret, port_change_required;
3046 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003047 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003048 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003049 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003050
3051 msg = get_trash_chunk();
3052 chunk_reset(msg);
3053
3054 if (addr) {
3055 memset(&sa, 0, sizeof(struct sockaddr_storage));
3056 if (str2ip2(addr, &sa, 0) == NULL) {
3057 chunk_printf(msg, "Invalid addr '%s'", addr);
3058 goto out;
3059 }
3060
3061 /* changes are allowed on AF_INET* families only */
3062 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3063 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3064 goto out;
3065 }
3066
3067 /* collecting data currently setup */
3068 memset(current_addr, '\0', sizeof(current_addr));
3069 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3070 /* changes are allowed on AF_INET* families only */
3071 if ((ret != AF_INET) && (ret != AF_INET6)) {
3072 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3073 goto out;
3074 }
3075
3076 /* applying ADDR changes if required and allowed
3077 * ipcmp returns 0 when both ADDR are the same
3078 */
3079 if (ipcmp(&s->addr, &sa) == 0) {
3080 chunk_appendf(msg, "no need to change the addr");
3081 goto port;
3082 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003083 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003084 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003085
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003086 /* update report for caller */
3087 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3088 }
3089
3090 port:
3091 if (port) {
3092 char sign = '\0';
3093 char *endptr;
3094
3095 if (addr)
3096 chunk_appendf(msg, ", ");
3097
3098 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003099 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003100
3101 /* check if PORT change is required */
3102 port_change_required = 0;
3103
3104 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003105 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003106 new_port = strtol(port, &endptr, 10);
3107 if ((errno != 0) || (port == endptr)) {
3108 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3109 goto out;
3110 }
3111
3112 /* check if caller triggers a port mapped or offset */
3113 if (sign == '-' || (sign == '+')) {
3114 /* check if server currently uses port map */
3115 if (!(s->flags & SRV_F_MAPPORTS)) {
3116 /* switch from fixed port to port map mandatorily triggers
3117 * a port change */
3118 port_change_required = 1;
3119 /* check is configured
3120 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3121 * prevent PORT change if check doesn't have it's dedicated port while switching
3122 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003123 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003124 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.");
3125 goto out;
3126 }
3127 }
3128 /* we're already using port maps */
3129 else {
3130 port_change_required = current_port != new_port;
3131 }
3132 }
3133 /* fixed port */
3134 else {
3135 port_change_required = current_port != new_port;
3136 }
3137
3138 /* applying PORT changes if required and update response message */
3139 if (port_change_required) {
3140 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003141 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003142 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003143
3144 /* prepare message */
3145 chunk_appendf(msg, "port changed from '");
3146 if (s->flags & SRV_F_MAPPORTS)
3147 chunk_appendf(msg, "+");
3148 chunk_appendf(msg, "%d' to '", current_port);
3149
3150 if (sign == '-') {
3151 s->flags |= SRV_F_MAPPORTS;
3152 chunk_appendf(msg, "%c", sign);
3153 /* just use for result output */
3154 new_port = -new_port;
3155 }
3156 else if (sign == '+') {
3157 s->flags |= SRV_F_MAPPORTS;
3158 chunk_appendf(msg, "%c", sign);
3159 }
3160 else {
3161 s->flags &= ~SRV_F_MAPPORTS;
3162 }
3163
3164 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003165 }
3166 else {
3167 chunk_appendf(msg, "no need to change the port");
3168 }
3169 }
3170
3171out:
William Dauchy6318d332020-05-02 21:52:36 +02003172 if (changed) {
3173 /* force connection cleanup on the given server */
3174 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003175 srv_set_dyncookie(s);
Thayne McCombs92149f92020-11-20 01:28:26 -07003176 srv_set_addr_desc(s);
William Dauchy6318d332020-05-02 21:52:36 +02003177 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003178 if (updater)
3179 chunk_appendf(msg, " by '%s'", updater);
3180 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003181 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003182}
3183
Christopher Faulet5efdef22021-03-11 18:03:21 +01003184/*
3185 * update server status based on result of SRV resolution
3186 * returns:
3187 * 0 if server status is updated
3188 * 1 if server status has not changed
3189 *
3190 * Must be called with the server lock held.
3191 */
3192int srvrq_update_srv_status(struct server *s, int has_no_ip)
3193{
3194 if (!s->srvrq)
3195 return 1;
3196
3197 /* since this server has an IP, it can go back in production */
3198 if (has_no_ip == 0) {
3199 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3200 return 1;
3201 }
3202
3203 if (s->next_admin & SRV_ADMF_RMAINT)
3204 return 1;
3205
3206 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3207 return 0;
3208}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003209
3210/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003211 * update server status based on result of name resolution
3212 * returns:
3213 * 0 if server status is updated
3214 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003215 *
3216 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003217 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003218int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003219{
Emeric Brun750fe792020-12-23 16:51:12 +01003220 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003221 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003222 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003223
Jerome Magnin012261a2020-07-28 13:38:22 +02003224 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003225 if (resolution == NULL)
3226 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003227
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003228 switch (resolution->status) {
3229 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003230 /* status when HAProxy has just (re)started.
3231 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003232 break;
3233
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003234 case RSLV_STATUS_VALID:
3235 /*
3236 * resume health checks
3237 * server will be turned back on if health check is safe
3238 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003239 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003240 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003241 return 1;
3242 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3243 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003244 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003245 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003246
Emeric Brun52a91d32017-08-31 14:41:55 +02003247 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003248 return 1;
3249 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3250 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3251 s->proxy->id, s->id);
3252
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003253 ha_warning("%s.\n", trash.area);
3254 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003255 return 0;
3256
3257 case RSLV_STATUS_NX:
3258 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003259 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3260 if (!tick_is_expired(exp, now_ms))
3261 break;
3262
3263 if (s->next_admin & SRV_ADMF_RMAINT)
3264 return 1;
3265 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3266 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003267
3268 case RSLV_STATUS_TIMEOUT:
3269 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003270 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3271 if (!tick_is_expired(exp, now_ms))
3272 break;
3273
3274 if (s->next_admin & SRV_ADMF_RMAINT)
3275 return 1;
3276 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3277 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003278
3279 case RSLV_STATUS_REFUSED:
3280 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003281 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3282 if (!tick_is_expired(exp, now_ms))
3283 break;
3284
3285 if (s->next_admin & SRV_ADMF_RMAINT)
3286 return 1;
3287 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3288 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003289
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003290 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003291 /* stop server if resolution failed for a long enough period */
3292 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3293 if (!tick_is_expired(exp, now_ms))
3294 break;
3295
3296 if (s->next_admin & SRV_ADMF_RMAINT)
3297 return 1;
3298 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3299 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003300 }
3301
3302 return 1;
3303}
3304
3305/*
3306 * Server Name Resolution valid response callback
3307 * It expects:
3308 * - <nameserver>: the name server which answered the valid response
3309 * - <response>: buffer containing a valid DNS response
3310 * - <response_len>: size of <response>
3311 * It performs the following actions:
3312 * - ignore response if current ip found and server family not met
3313 * - update with first new ip found if family is met and current IP is not found
3314 * returns:
3315 * 0 on error
3316 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003317 *
3318 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003319 */
Emeric Brun08622d32020-12-23 17:41:43 +01003320int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003321{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003322 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003323 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003324 void *serverip, *firstip;
3325 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003326 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003327 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003328 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003329
Christopher Faulet67957bd2017-09-27 11:00:59 +02003330 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003331 if (!s)
3332 return 1;
3333
Christopher Faulet49531e82021-03-10 15:07:27 +01003334 if (s->srvrq) {
3335 struct resolv_answer_item *srv_item;
3336
3337 /* If DNS resolution is disabled ignore it. */
3338 if (s->flags & SRV_F_NO_RESOLUTION)
3339 return 1;
3340
3341 /* The server is based on a SRV record, thus, find the
3342 * associated answer record. If not found, it means the SRV item
3343 * has expired and this resolution must be ignored.
3344 */
3345 srv_item = find_srvrq_answer_record(requester);
3346 if (!srv_item)
3347 return 1;
3348 }
3349
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003350 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003351 if (!resolution)
3352 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003353
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003354 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003355 firstip = NULL; /* pointer to the first valid response found */
3356 /* it will be used as the new IP if a change is required */
3357 firstip_sin_family = AF_UNSPEC;
3358 serverip = NULL; /* current server IP address */
3359
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003360 /* initializing server IP pointer */
3361 server_sin_family = s->addr.ss_family;
3362 switch (server_sin_family) {
3363 case AF_INET:
3364 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3365 break;
3366
3367 case AF_INET6:
3368 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3369 break;
3370
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003371 case AF_UNSPEC:
3372 break;
3373
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003374 default:
3375 goto invalid;
3376 }
3377
Emeric Brund30e9a12020-12-23 18:49:16 +01003378 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3379 serverip, server_sin_family, &firstip,
3380 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003381
3382 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003383 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003384 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003385
Emeric Brun456de772020-12-23 18:17:31 +01003386 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003387 goto save_ip;
3388
Emeric Brun456de772020-12-23 18:17:31 +01003389 case RSLV_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003390 goto invalid;
3391
Emeric Brun456de772020-12-23 18:17:31 +01003392 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003393 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003394 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003395
Emeric Brun456de772020-12-23 18:17:31 +01003396 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003397 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003398 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003399 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003400
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003401 default:
3402 goto invalid;
3403
3404 }
3405
3406 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003407 if (counters) {
3408 counters->update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003409 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003410 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003411 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003412 else
3413 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003414 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003415
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003416 update_status:
Christopher Faulet49531e82021-03-10 15:07:27 +01003417
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003418 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003419 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003420
3421 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003422 if (counters) {
3423 counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003424 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003425 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003426 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003427 return 0;
3428}
3429
3430/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003431 * SRV record error management callback
3432 * returns:
3433 * 0 on error
3434 * 1 when no error or safe ignore
3435 *
3436 * Grabs the server's lock.
3437 */
3438int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3439{
3440 struct server *s;
3441 struct resolv_srvrq *srvrq;
3442 struct resolv_resolution *res;
3443 struct resolvers *resolvers;
3444 int exp;
3445
3446 /* SRV records */
3447 srvrq = objt_resolv_srvrq(requester->owner);
3448 if (!srvrq)
3449 return 1;
3450
3451 resolvers = srvrq->resolvers;
3452 res = requester->resolution;
3453
3454 switch (res->status) {
3455
3456 case RSLV_STATUS_NX:
3457 /* stop server if resolution is NX for a long enough period */
3458 exp = tick_add(res->last_valid, resolvers->hold.nx);
3459 if (!tick_is_expired(exp, now_ms))
3460 return 1;
3461 break;
3462
3463 case RSLV_STATUS_TIMEOUT:
3464 /* stop server if resolution is TIMEOUT for a long enough period */
3465 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3466 if (!tick_is_expired(exp, now_ms))
3467 return 1;
3468 break;
3469
3470 case RSLV_STATUS_REFUSED:
3471 /* stop server if resolution is REFUSED for a long enough period */
3472 exp = tick_add(res->last_valid, resolvers->hold.refused);
3473 if (!tick_is_expired(exp, now_ms))
3474 return 1;
3475 break;
3476
3477 default:
3478 /* stop server if resolution failed for a long enough period */
3479 exp = tick_add(res->last_valid, resolvers->hold.other);
3480 if (!tick_is_expired(exp, now_ms))
3481 return 1;
3482 }
3483
3484 /* Remove any associated server */
3485 for (s = srvrq->proxy->srv; s != NULL; s = s->next) {
3486 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
3487 if (s->srvrq == srvrq) {
Christopher Faulet0efc0992021-03-11 18:09:53 +01003488 resolv_unlink_resolution(s->resolv_requester, 1);
Christopher Faulet6b117ae2021-03-11 18:06:23 +01003489 srvrq_update_srv_status(s, 1);
Christopher Faulet52d4d302021-02-23 12:24:09 +01003490 memset(&s->addr, 0, sizeof(s->addr));
Christopher Fauletc392d462021-03-10 15:51:13 +01003491 ha_free(&s->hostname);
3492 ha_free(&s->hostname_dn);
3493 s->hostname_dn_len = 0;
Christopher Faulet52d4d302021-02-23 12:24:09 +01003494 s->svc_port = 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003495 }
3496 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
3497 }
3498
Christopher Faulet51d5e3b2021-03-10 15:46:46 +01003499 resolv_purge_resolution_answer_records(res);
3500
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003501 return 1;
3502}
3503
3504/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003505 * Server Name Resolution error management callback
3506 * returns:
3507 * 0 on error
3508 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003509 *
3510 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003511 */
Emeric Brun08622d32020-12-23 17:41:43 +01003512int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003513{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003514 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003515
Christopher Faulet67957bd2017-09-27 11:00:59 +02003516 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003517 if (!s)
3518 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003519 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5130c212021-03-10 20:31:40 +01003520 if (!snr_update_srv_status(s, 1))
3521 memset(&s->addr, 0, sizeof(s->addr));
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003522 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003523 return 1;
3524}
3525
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003526/*
3527 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003528 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003529 * It returns a pointer to the first server found or NULL if <ip> is not yet
3530 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003531 *
3532 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003533 */
3534struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3535{
3536 struct server *tmpsrv;
3537 struct proxy *be;
3538
3539 if (!srv)
3540 return NULL;
3541
3542 be = srv->proxy;
3543 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003544 /* we found the current server is the same, ignore it */
3545 if (srv == tmpsrv)
3546 continue;
3547
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003548 /* We want to compare the IP in the record with the IP of the servers in the
3549 * same backend, only if:
3550 * * DNS resolution is enabled on the server
3551 * * the hostname used for the resolution by our server is the same than the
3552 * one used for the server found in the backend
3553 * * the server found in the backend is not our current server
3554 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003555 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003556 if ((tmpsrv->hostname_dn == NULL) ||
3557 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003558 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003559 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003560 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003561 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003562 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003563
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003564 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003565 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003566 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003567 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003568 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003569
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003570 /* At this point, we have 2 different servers using the same DNS hostname
3571 * for their respective resolution.
3572 */
3573 if (*ip_family == tmpsrv->addr.ss_family &&
3574 ((tmpsrv->addr.ss_family == AF_INET &&
3575 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3576 (tmpsrv->addr.ss_family == AF_INET6 &&
3577 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003578 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003579 return tmpsrv;
3580 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003581 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003582 }
3583
Emeric Brune9fd6b52017-11-02 17:20:39 +01003584
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003585 return NULL;
3586}
3587
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003588/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3589 * resolver. This is suited for initial address configuration. Returns 0 on
3590 * success otherwise a non-zero error code. In case of error, *err_code, if
3591 * not NULL, is filled up.
3592 */
3593int srv_set_addr_via_libc(struct server *srv, int *err_code)
3594{
3595 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003596 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003597 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003598 return 1;
3599 }
3600 return 0;
3601}
3602
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003603/* Set the server's FDQN (->hostname) from <hostname>.
3604 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003605 *
3606 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003607 */
Emeric Brun08622d32020-12-23 17:41:43 +01003608int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003609{
Emeric Brun08622d32020-12-23 17:41:43 +01003610 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003611 char *hostname_dn;
3612 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003613
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003614 /* Note that the server lock is already held. */
3615 if (!srv->resolvers)
3616 return -1;
3617
Emeric Brun08622d32020-12-23 17:41:43 +01003618 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003619 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003620 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003621 * and we can't enable it at run time for now.
3622 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003623 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003624 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003625
3626 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003627 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003628 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01003629 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
3630 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003631 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003632 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003633
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003634 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003635 if (resolution &&
3636 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003637 resolution->hostname_dn_len == hostname_dn_len &&
3638 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003639 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003640
Christopher Faulet0efc0992021-03-11 18:09:53 +01003641 resolv_unlink_resolution(srv->resolv_requester, 0);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003642
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003643 free(srv->hostname);
3644 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003645 srv->hostname = strdup(hostname);
3646 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003647 srv->hostname_dn_len = hostname_dn_len;
3648 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003649 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003650
Baptiste Assmann13a92322019-06-07 09:40:55 +02003651 if (srv->flags & SRV_F_NO_RESOLUTION)
3652 goto end;
3653
Emeric Brund30e9a12020-12-23 18:49:16 +01003654 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003655 goto err;
3656
3657 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003658 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003659 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003660 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003661
3662 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003663 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003664 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003665 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003666}
3667
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003668/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3669 * from the state file. This is suited for initial address configuration.
3670 * Returns 0 on success otherwise a non-zero error code. In case of error,
3671 * *err_code, if not NULL, is filled up.
3672 */
3673static int srv_apply_lastaddr(struct server *srv, int *err_code)
3674{
3675 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3676 if (err_code)
3677 *err_code |= ERR_WARN;
3678 return 1;
3679 }
3680 return 0;
3681}
3682
Willy Tarreau25e51522016-11-04 15:10:17 +01003683/* returns 0 if no error, otherwise a combination of ERR_* flags */
3684static int srv_iterate_initaddr(struct server *srv)
3685{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003686 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003687 int return_code = 0;
3688 int err_code;
3689 unsigned int methods;
3690
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003691 /* If no addr and no hostname set, get the name from the DNS SRV request */
3692 if (!name && srv->srvrq)
3693 name = srv->srvrq->name;
3694
Willy Tarreau25e51522016-11-04 15:10:17 +01003695 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003696 if (!methods) {
3697 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003698 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3699 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003700 if (srv->resolvers_id) {
3701 /* dns resolution is configured, add "none" to not fail on startup */
3702 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3703 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003704 }
3705
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003706 /* "-dr" : always append "none" so that server addresses resolution
3707 * failures are silently ignored, this is convenient to validate some
3708 * configs out of their environment.
3709 */
3710 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3711 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3712
Willy Tarreau25e51522016-11-04 15:10:17 +01003713 while (methods) {
3714 err_code = 0;
3715 switch (srv_get_next_initaddr(&methods)) {
3716 case SRV_IADDR_LAST:
3717 if (!srv->lastaddr)
3718 continue;
3719 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003720 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003721 return_code |= err_code;
3722 break;
3723
3724 case SRV_IADDR_LIBC:
3725 if (!srv->hostname)
3726 continue;
3727 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003728 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003729 return_code |= err_code;
3730 break;
3731
Willy Tarreau37ebe122016-11-04 15:17:58 +01003732 case SRV_IADDR_NONE:
3733 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003734 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003735 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003736 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003737 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003738 return return_code;
3739
Willy Tarreau4310d362016-11-02 15:05:56 +01003740 case SRV_IADDR_IP:
3741 ipcpy(&srv->init_addr, &srv->addr);
3742 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003743 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 +01003744 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau4310d362016-11-02 15:05:56 +01003745 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003746 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003747
Willy Tarreau25e51522016-11-04 15:10:17 +01003748 default: /* unhandled method */
3749 break;
3750 }
3751 }
3752
3753 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003754 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003755 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau25e51522016-11-04 15:10:17 +01003756 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003757 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003758 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003759 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003760 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003761
3762 return_code |= ERR_ALERT | ERR_FATAL;
3763 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003764out:
3765 srv_set_dyncookie(srv);
Thayne McCombs92149f92020-11-20 01:28:26 -07003766 srv_set_addr_desc(srv);
Olivier Houchard4e694042017-03-14 20:01:29 +01003767 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003768}
3769
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003770/*
3771 * This function parses all backends and all servers within each backend
3772 * and performs servers' addr resolution based on information provided by:
3773 * - configuration file
3774 * - server-state file (states provided by an 'old' haproxy process)
3775 *
3776 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3777 */
3778int srv_init_addr(void)
3779{
3780 struct proxy *curproxy;
3781 int return_code = 0;
3782
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003783 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003784 while (curproxy) {
3785 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003786
3787 /* servers are in backend only */
Amaury Denoyellee3c41922021-01-06 14:28:50 +01003788 if (!(curproxy->cap & PR_CAP_BE) || curproxy->disabled)
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003789 goto srv_init_addr_next;
3790
Willy Tarreau25e51522016-11-04 15:10:17 +01003791 for (srv = curproxy->srv; srv; srv = srv->next)
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003792 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003793 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003794
3795 srv_init_addr_next:
3796 curproxy = curproxy->next;
3797 }
3798
3799 return return_code;
3800}
3801
Willy Tarreau46b7f532018-08-21 11:54:26 +02003802/*
3803 * Must be called with the server lock held.
3804 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003805const 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 +02003806{
3807
Willy Tarreau83061a82018-07-13 11:56:34 +02003808 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003809
3810 msg = get_trash_chunk();
3811 chunk_reset(msg);
3812
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003813 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003814 chunk_appendf(msg, "no need to change the FDQN");
3815 goto out;
3816 }
3817
3818 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3819 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3820 goto out;
3821 }
3822
3823 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3824 server->proxy->id, server->id, server->hostname, fqdn);
3825
Emeric Brun08622d32020-12-23 17:41:43 +01003826 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003827 chunk_reset(msg);
3828 chunk_appendf(msg, "could not update %s/%s FQDN",
3829 server->proxy->id, server->id);
3830 goto out;
3831 }
3832
3833 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003834 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003835
3836 out:
3837 if (updater)
3838 chunk_appendf(msg, " by '%s'", updater);
3839 chunk_appendf(msg, "\n");
3840
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003841 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003842}
3843
3844
Willy Tarreau21b069d2016-11-23 17:15:08 +01003845/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3846 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003847 * 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 +01003848 * used for CLI commands requiring a server name.
3849 * Important: the <arg> is modified to remove the '/'.
3850 */
3851struct server *cli_find_server(struct appctx *appctx, char *arg)
3852{
3853 struct proxy *px;
3854 struct server *sv;
3855 char *line;
3856
3857 /* split "backend/server" and make <line> point to server */
3858 for (line = arg; *line; line++)
3859 if (*line == '/') {
3860 *line++ = '\0';
3861 break;
3862 }
3863
3864 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003865 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003866 return NULL;
3867 }
3868
3869 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003870 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003871 return NULL;
3872 }
3873
Willy Tarreauc3914d42020-09-24 08:39:22 +02003874 if (px->disabled) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003875 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003876 return NULL;
3877 }
3878
3879 return sv;
3880}
3881
William Lallemand222baf22016-11-19 02:00:33 +01003882
Willy Tarreau46b7f532018-08-21 11:54:26 +02003883/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003884static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01003885{
3886 struct server *sv;
3887 const char *warning;
3888
3889 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3890 return 1;
3891
3892 sv = cli_find_server(appctx, args[2]);
3893 if (!sv)
3894 return 1;
3895
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003896 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02003897
William Lallemand222baf22016-11-19 02:00:33 +01003898 if (strcmp(args[3], "weight") == 0) {
3899 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003900 if (warning)
3901 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003902 }
3903 else if (strcmp(args[3], "state") == 0) {
3904 if (strcmp(args[4], "ready") == 0)
3905 srv_adm_set_ready(sv);
3906 else if (strcmp(args[4], "drain") == 0)
3907 srv_adm_set_drain(sv);
3908 else if (strcmp(args[4], "maint") == 0)
3909 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02003910 else
3911 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003912 }
3913 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003914 if (sv->track)
3915 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003916 else if (strcmp(args[4], "up") == 0) {
3917 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003918 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003919 }
3920 else if (strcmp(args[4], "stopping") == 0) {
3921 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003922 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003923 }
3924 else if (strcmp(args[4], "down") == 0) {
3925 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003926 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003927 }
Willy Tarreau9d008692019-08-09 11:21:01 +02003928 else
3929 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003930 }
3931 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003932 if (!(sv->agent.state & CHK_ST_ENABLED))
3933 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003934 else if (strcmp(args[4], "up") == 0) {
3935 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003936 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003937 }
3938 else if (strcmp(args[4], "down") == 0) {
3939 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003940 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003941 }
Willy Tarreau9d008692019-08-09 11:21:01 +02003942 else
3943 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003944 }
Misiek2da082d2017-01-09 09:40:42 +01003945 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01003946 char *addr = NULL;
3947 char *port = NULL;
3948 if (strlen(args[4]) == 0) {
3949 cli_err(appctx, "set server <b>/<s> agent-addr requires"
3950 " an address and optionally a port.\n");
3951 goto out_unlock;
3952 }
3953 addr = args[4];
3954 if (strcmp(args[5], "port") == 0)
3955 port = args[6];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003956 warning = srv_update_agent_addr_port(sv, addr, port);
William Dauchy7cabc062021-02-11 22:51:24 +01003957 if (warning)
3958 cli_msg(appctx, LOG_WARNING, warning);
3959 }
3960 else if (strcmp(args[3], "agent-port") == 0) {
3961 char *port = NULL;
3962 if (strlen(args[4]) == 0) {
3963 cli_err(appctx, "set server <b>/<s> agent-port requires"
3964 " a port.\n");
3965 goto out_unlock;
3966 }
3967 port = args[4];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003968 warning = srv_update_agent_addr_port(sv, NULL, port);
William Dauchy7cabc062021-02-11 22:51:24 +01003969 if (warning)
3970 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01003971 }
3972 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003973 if (!(sv->agent.state & CHK_ST_ENABLED))
3974 cli_err(appctx, "agent checks are not enabled on this server.\n");
3975 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02003976 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02003977 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01003978 }
3979 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003980 else if (strcmp(args[3], "check-addr") == 0) {
3981 char *addr = NULL;
3982 char *port = NULL;
3983 if (strlen(args[4]) == 0) {
3984 cli_err(appctx, "set server <b>/<s> check-addr requires"
3985 " an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01003986 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01003987 }
William Dauchyb456e1f2021-02-11 22:51:23 +01003988 addr = args[4];
3989 if (strcmp(args[5], "port") == 0)
3990 port = args[6];
Christopher Faulet69beaa92021-02-16 12:07:47 +01003991 warning = srv_update_check_addr_port(sv, addr, port);
William Dauchyb456e1f2021-02-11 22:51:23 +01003992 if (warning)
3993 cli_msg(appctx, LOG_WARNING, warning);
3994 }
3995 else if (strcmp(args[3], "check-port") == 0) {
3996 char *port = NULL;
3997 if (strlen(args[4]) == 0) {
3998 cli_err(appctx, "set server <b>/<s> check-port requires"
3999 " a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004000 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004001 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004002 port = args[4];
Christopher Faulet69beaa92021-02-16 12:07:47 +01004003 warning = srv_update_check_addr_port(sv, NULL, port);
William Dauchyb456e1f2021-02-11 22:51:23 +01004004 if (warning)
4005 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004006 }
4007 else if (strcmp(args[3], "addr") == 0) {
4008 char *addr = NULL;
4009 char *port = NULL;
4010 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004011 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004012 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004013 }
4014 else {
4015 addr = args[4];
4016 }
4017 if (strcmp(args[5], "port") == 0) {
4018 port = args[6];
4019 }
Christopher Faulet69beaa92021-02-16 12:07:47 +01004020 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004021 if (warning)
4022 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004023 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4024 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004025 else if (strcmp(args[3], "fqdn") == 0) {
4026 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004027 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004028 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004029 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004030 /* ensure runtime resolver will process this new fqdn */
4031 if (sv->flags & SRV_F_NO_RESOLUTION) {
4032 sv->flags &= ~SRV_F_NO_RESOLUTION;
4033 }
Christopher Faulet69beaa92021-02-16 12:07:47 +01004034 warning = srv_update_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004035 if (warning)
4036 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004037 }
William Dauchyf6370442020-11-14 19:25:33 +01004038 else if (strcmp(args[3], "ssl") == 0) {
4039#ifdef USE_OPENSSL
4040 if (sv->ssl_ctx.ctx == NULL) {
4041 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4042 " default-server should define ssl settings\n");
4043 goto out_unlock;
4044 } else if (strcmp(args[4], "on") == 0) {
4045 ssl_sock_set_srv(sv, 1);
4046 } else if (strcmp(args[4], "off") == 0) {
4047 ssl_sock_set_srv(sv, 0);
4048 } else {
4049 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
4050 goto out_unlock;
4051 }
4052 srv_cleanup_connections(sv);
4053 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4054#else
4055 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4056#endif
4057 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004058 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004059 "usage: set server <backend>/<server> "
4060 "addr | agent | agent-addr | agent-port | agent-send | "
4061 "check-addr | check-port | fqdn | health | ssl | "
4062 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004063 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004064 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004065 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004066 return 1;
4067}
4068
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004069static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004070{
4071 struct stream_interface *si = appctx->owner;
4072 struct proxy *px;
4073 struct server *sv;
4074 char *line;
4075
4076
4077 /* split "backend/server" and make <line> point to server */
4078 for (line = args[2]; *line; line++)
4079 if (*line == '/') {
4080 *line++ = '\0';
4081 break;
4082 }
4083
Willy Tarreau9d008692019-08-09 11:21:01 +02004084 if (!*line)
4085 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004086
Willy Tarreau9d008692019-08-09 11:21:01 +02004087 if (!get_backend_server(args[2], line, &px, &sv))
4088 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004089
4090 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004091 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4092 sv->iweight);
4093 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004094 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004095 return 0;
4096 }
William Lallemand6b160942016-11-22 12:34:35 +01004097 return 1;
4098}
4099
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004100/* Parse a "set weight" command.
4101 *
4102 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004103 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004104static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004105{
4106 struct server *sv;
4107 const char *warning;
4108
4109 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4110 return 1;
4111
4112 sv = cli_find_server(appctx, args[2]);
4113 if (!sv)
4114 return 1;
4115
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004116 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4117
William Lallemand6b160942016-11-22 12:34:35 +01004118 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004119 if (warning)
4120 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004121
4122 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4123
William Lallemand6b160942016-11-22 12:34:35 +01004124 return 1;
4125}
4126
Willy Tarreau46b7f532018-08-21 11:54:26 +02004127/* parse a "set maxconn server" command. It always returns 1.
4128 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004129 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004130 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004131static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004132{
4133 struct server *sv;
4134 const char *warning;
4135
4136 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4137 return 1;
4138
4139 sv = cli_find_server(appctx, args[3]);
4140 if (!sv)
4141 return 1;
4142
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004143 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4144
Willy Tarreaub8026272016-11-23 11:26:56 +01004145 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004146 if (warning)
4147 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004148
4149 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4150
Willy Tarreaub8026272016-11-23 11:26:56 +01004151 return 1;
4152}
William Lallemand6b160942016-11-22 12:34:35 +01004153
Willy Tarreau46b7f532018-08-21 11:54:26 +02004154/* parse a "disable agent" command. It always returns 1.
4155 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004156 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004157 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004158static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004159{
4160 struct server *sv;
4161
4162 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4163 return 1;
4164
4165 sv = cli_find_server(appctx, args[2]);
4166 if (!sv)
4167 return 1;
4168
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004169 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004170 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004171 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004172 return 1;
4173}
4174
Willy Tarreau46b7f532018-08-21 11:54:26 +02004175/* parse a "disable health" command. It always returns 1.
4176 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004177 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004178 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004179static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004180{
4181 struct server *sv;
4182
4183 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4184 return 1;
4185
4186 sv = cli_find_server(appctx, args[2]);
4187 if (!sv)
4188 return 1;
4189
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004190 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004191 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004192 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004193 return 1;
4194}
4195
Willy Tarreau46b7f532018-08-21 11:54:26 +02004196/* parse a "disable server" command. It always returns 1.
4197 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004198 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004199 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004200static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004201{
4202 struct server *sv;
4203
4204 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4205 return 1;
4206
4207 sv = cli_find_server(appctx, args[2]);
4208 if (!sv)
4209 return 1;
4210
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004211 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004212 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004213 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004214 return 1;
4215}
4216
Willy Tarreau46b7f532018-08-21 11:54:26 +02004217/* parse a "enable agent" command. It always returns 1.
4218 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004219 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004220 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004221static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004222{
4223 struct server *sv;
4224
4225 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4226 return 1;
4227
4228 sv = cli_find_server(appctx, args[2]);
4229 if (!sv)
4230 return 1;
4231
Willy Tarreau9d008692019-08-09 11:21:01 +02004232 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4233 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004234
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004235 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004236 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004237 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004238 return 1;
4239}
4240
Willy Tarreau46b7f532018-08-21 11:54:26 +02004241/* parse a "enable health" command. It always returns 1.
4242 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004243 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004244 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004245static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004246{
4247 struct server *sv;
4248
4249 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4250 return 1;
4251
4252 sv = cli_find_server(appctx, args[2]);
4253 if (!sv)
4254 return 1;
4255
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004256 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004257 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004258 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004259 return 1;
4260}
4261
Willy Tarreau46b7f532018-08-21 11:54:26 +02004262/* parse a "enable server" command. It always returns 1.
4263 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004264 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004265 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004266static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004267{
4268 struct server *sv;
4269
4270 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4271 return 1;
4272
4273 sv = cli_find_server(appctx, args[2]);
4274 if (!sv)
4275 return 1;
4276
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004277 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004278 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004279 if (!(sv->flags & SRV_F_COOKIESET)
4280 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4281 sv->cookie)
4282 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004283 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004284 return 1;
4285}
4286
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004287/* Allocates data structure related to load balancing for the server <sv>. It
4288 * is only required for dynamic servers.
4289 *
4290 * At the moment, the server lock is not used as this function is only called
4291 * for a dynamic server not yet registered.
4292 *
4293 * Returns 1 on success, 0 on allocation failure.
4294 */
4295static int srv_alloc_lb(struct server *sv, struct proxy *be)
4296{
4297 int node;
4298
4299 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4300 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4301 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4302 sv->lb_nodes_now = 0;
4303
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004304 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4305 ((be->lbprm.algo & (BE_LB_KIND | BE_LB_HASH_TYPE)) == (BE_LB_KIND_HI | BE_LB_HASH_CONS))) {
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004306 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4307
4308 if (!sv->lb_nodes)
4309 return 0;
4310
4311 for (node = 0; node < sv->lb_nodes_tot; node++) {
4312 sv->lb_nodes[node].server = sv;
4313 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4314 }
4315 }
4316
4317 return 1;
4318}
4319
4320/* Parse a "add server" command
4321 * Returns 0 if the server has been successfully initialized, 1 on failure.
4322 */
4323static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4324{
4325 struct proxy *be;
4326 struct server *srv;
4327 char *be_name, *sv_name;
4328 char *errmsg = NULL;
4329 int errcode, argc;
4330 int i;
4331 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4332
4333 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4334 return 1;
4335
4336 ++args;
4337
4338 sv_name = be_name = args[1];
4339 /* split backend/server arg */
4340 while (*sv_name && *(++sv_name)) {
4341 if (*sv_name == '/') {
4342 *sv_name = '\0';
4343 ++sv_name;
4344 break;
4345 }
4346 }
4347
4348 if (!*sv_name)
4349 return cli_err(appctx, "Require 'backend/server'.");
4350
Amaury Denoyellecece9182021-04-20 17:09:08 +02004351 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004352 if (!be)
4353 return cli_err(appctx, "No such backend.");
4354
4355 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004356 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004357 return 1;
4358 }
4359
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004360 args[1] = sv_name;
4361 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags, &errmsg);
4362 if (errcode) {
4363 if (errmsg)
4364 cli_dynerr(appctx, errmsg);
4365 goto out;
4366 }
4367
4368 while (*args[argc]) {
4369 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags, &errmsg);
4370
4371 if (errcode) {
4372 if (errmsg)
4373 cli_dynerr(appctx, errmsg);
4374 goto out;
4375 }
4376 }
4377
4378 _srv_parse_finalize(args, argc, srv, be, parse_flags, &errmsg);
4379 if (errmsg) {
4380 cli_dynerr(appctx, errmsg);
4381 goto out;
4382 }
4383
Amaury Denoyelle30467232021-03-12 18:03:27 +01004384 if (srv->mux_proto) {
4385 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
4386 cli_err(appctx, "MUX protocol is not usable for server.");
4387 goto out;
4388 }
4389 }
4390
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004391 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
4392 if (!srv->per_thr) {
4393 cli_err(appctx, "failed to allocate per-thread lists for server.");
4394 goto out;
4395 }
4396
4397 for (i = 0; i < global.nbthread; i++) {
4398 srv->per_thr[i].idle_conns = EB_ROOT;
4399 srv->per_thr[i].safe_conns = EB_ROOT;
4400 srv->per_thr[i].avail_conns = EB_ROOT;
4401 MT_LIST_INIT(&srv->per_thr[i].streams);
4402 }
4403
4404 if (srv->max_idle_conns != 0) {
4405 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4406 if (!srv->curr_idle_thr) {
4407 cli_err(appctx, "failed to allocate counters for server.");
4408 goto out;
4409 }
4410 }
4411
4412 if (!srv_alloc_lb(srv, be)) {
4413 cli_err(appctx, "Failed to initialize load-balancing data.");
4414 goto out;
4415 }
4416
4417 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4418 COUNTERS_SV,
4419 STATS_PX_CAP_SRV)) {
4420 cli_err(appctx, "failed to allocate extra counters for server.");
4421 goto out;
4422 }
4423
Amaury Denoyellecece9182021-04-20 17:09:08 +02004424 /* Attach the server to the end of the proxy linked list. The proxy
4425 * servers list is currently not protected by a lock, so this requires
4426 * thread_isolate/release.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004427 *
Amaury Denoyellecece9182021-04-20 17:09:08 +02004428 * If a server with the same name is found, reject the new one. This
4429 * operation requires thread-safety and thus cannot be executed at the
4430 * beginning without having server allocation under locks/isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004431 */
4432 thread_isolate();
Amaury Denoyellecece9182021-04-20 17:09:08 +02004433
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004434 /* TODO use a double-linked list for px->srv */
4435 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004436 struct server *next = be->srv;
4437
4438 while (1) {
4439 /* check for duplicate server */
4440 if (!strcmp(srv->id, next->id)) {
4441 thread_release();
4442 cli_err(appctx, "Already exists a server with the same name in backend.");
4443 goto out;
4444 }
4445
4446 if (!next->next)
4447 break;
4448
4449 next = next->next;
4450 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004451
4452 next->next = srv;
4453 }
4454 else {
4455 srv->next = be->srv;
4456 be->srv = srv;
4457 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004458
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004459 thread_release();
4460
Amaury Denoyelled38e7fa2021-04-20 18:35:19 +02004461 ha_notice("New server %s/%s registered.\n", be->id, srv->id);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004462 cli_msg(appctx, LOG_INFO, "New server registered.");
4463
4464 return 0;
4465
4466out:
4467 if (srv)
4468 free_server(srv);
4469 return 1;
4470}
4471
Amaury Denoyellee5580432021-04-15 14:41:20 +02004472/* Parse a "del server" command
4473 * Returns 0 if the server has been successfully initialized, 1 on failure.
4474 */
4475static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
4476{
4477 struct proxy *be;
4478 struct server *srv;
4479 char *be_name, *sv_name;
4480
4481 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4482 return 1;
4483
4484 ++args;
4485
4486 sv_name = be_name = args[1];
4487 /* split backend/server arg */
4488 while (*sv_name && *(++sv_name)) {
4489 if (*sv_name == '/') {
4490 *sv_name = '\0';
4491 ++sv_name;
4492 break;
4493 }
4494 }
4495
4496 if (!*sv_name)
4497 return cli_err(appctx, "Require 'backend/server'.");
4498
4499 /* The proxy servers list is currently not protected by a lock so this
4500 * requires thread isolation.
4501 */
4502
4503 /* WARNING there is maybe a potential violation of the thread isolation
4504 * mechanism by the pool allocator. The allocator marks the thread as
4505 * harmless before the allocation, but a processing outside of it could
4506 * relies on a particular server triggered at the same time by a
4507 * 'delete server'. Currently, it is unknown if such case is present in
4508 * the current code. If it happens to be, the thread isolation
4509 * mechanism should be improved, maybe with a differentiation between
4510 * read and read+write safe sections.
4511 */
4512 thread_isolate();
4513
4514 get_backend_server(be_name, sv_name, &be, &srv);
4515 if (!be) {
4516 cli_err(appctx, "No such backend.");
4517 goto out;
4518 }
4519
4520 if (!srv) {
4521 cli_err(appctx, "No such server.");
4522 goto out;
4523 }
4524
4525 if (!(srv->flags & SRV_F_DYNAMIC)) {
4526 cli_err(appctx, "Only servers added at runtime via <add server> CLI cmd can be deleted.");
4527 goto out;
4528 }
4529
4530 /* Only servers in maintenance can be deleted. This ensures that the
4531 * server is not present anymore in the lb structures (through
4532 * lbprm.set_server_status_down).
4533 */
4534 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
4535 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
4536 goto out;
4537 }
4538
4539 /* Ensure that there is no active/idle/pending connection on the server.
4540 *
4541 * TODO idle connections should not prevent server deletion. A proper
4542 * cleanup function should be implemented to be used here.
4543 */
4544 if (srv->cur_sess || srv->curr_idle_conns ||
4545 !eb_is_empty(&srv->pendconns)) {
4546 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
4547 goto out;
4548 }
4549
4550 /* TODO remove server for check list once 'check' will be implemented for
4551 * dynamic servers
4552 */
4553
4554 /* detach the server from the proxy linked list
4555 * The proxy servers list is currently not protected by a lock, so this
4556 * requires thread_isolate/release.
4557 */
4558
4559 /* be->srv cannot be empty since we have already found the server with
4560 * get_backend_server */
4561 BUG_ON(!be->srv);
4562 if (be->srv == srv) {
4563 be->srv = srv->next;
4564 }
4565 else {
4566 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02004567 for (next = be->srv; srv != next->next; next = next->next) {
4568 /* srv cannot be not found since we have already found
4569 * it with get_backend_server */
4570 BUG_ON(!next);
4571 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02004572
Amaury Denoyellee5580432021-04-15 14:41:20 +02004573 next->next = srv->next;
4574 }
4575
4576 /* remove srv from addr_node tree */
4577 ebpt_delete(&srv->addr_node);
4578
4579 /* remove srv from idle_node tree for idle conn cleanup */
4580 eb32_delete(&srv->idle_node);
4581
4582 thread_release();
4583
4584 ha_notice("Server %s/%s deleted.\n", be->id, srv->id);
4585 free_server(srv);
4586
4587 cli_msg(appctx, LOG_INFO, "Server deleted.");
4588
4589 return 0;
4590
4591out:
4592 thread_release();
4593
4594 return 1;
4595}
4596
William Lallemand222baf22016-11-19 02:00:33 +01004597/* register cli keywords */
4598static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004599 { { "disable", "agent", NULL }, "disable agent (DEPRECATED) : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
4600 { { "disable", "health", NULL }, "disable health (DEPRECATED) : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
4601 { { "disable", "server", NULL }, "disable server (DEPRECATED) : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL },
4602 { { "enable", "agent", NULL }, "enable agent (DEPRECATED) : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
4603 { { "enable", "health", NULL }, "enable health (DEPRECATED) : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
4604 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
4605 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
4606 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
4607 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
4608 { { "set", "weight", NULL }, "set weight <bk>/<srv> (DEPRECATED) : change a server's weight (use 'set server' instead)", cli_parse_set_weight },
4609 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server (EXPERIMENTAL)", cli_parse_add_server, NULL, NULL, NULL, ACCESS_EXPERIMENTAL },
4610 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server (EXPERIMENTAL)", cli_parse_delete_server, NULL, NULL, NULL, ACCESS_EXPERIMENTAL },
William Lallemand222baf22016-11-19 02:00:33 +01004611 {{},}
4612}};
4613
Willy Tarreau0108d902018-11-25 19:14:37 +01004614INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004615
Emeric Brun64cc49c2017-10-03 14:46:45 +02004616/*
4617 * This function applies server's status changes, it is
4618 * is designed to be called asynchronously.
4619 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004620 * Must be called with the server lock held. This may also be called at init
4621 * time as the result of parsing the state file, in which case no lock will be
4622 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004623 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004624static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004625{
4626 struct check *check = &s->check;
4627 int xferred;
4628 struct proxy *px = s->proxy;
4629 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4630 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4631 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004632 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004633
Emeric Brun64cc49c2017-10-03 14:46:45 +02004634 /* If currently main is not set we try to apply pending state changes */
4635 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4636 int next_admin;
4637
4638 /* Backup next admin */
4639 next_admin = s->next_admin;
4640
4641 /* restore current admin state */
4642 s->next_admin = s->cur_admin;
4643
4644 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4645 s->last_change = now.tv_sec;
4646 if (s->proxy->lbprm.set_server_status_down)
4647 s->proxy->lbprm.set_server_status_down(s);
4648
4649 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4650 srv_shutdown_streams(s, SF_ERR_DOWN);
4651
4652 /* we might have streams queued on this server and waiting for
4653 * a connection. Those which are redispatchable will be queued
4654 * to another server or to the proxy itself.
4655 */
4656 xferred = pendconn_redistribute(s);
4657
4658 tmptrash = alloc_trash_chunk();
4659 if (tmptrash) {
4660 chunk_printf(tmptrash,
4661 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4662 s->proxy->id, s->id);
4663
Emeric Brun5a133512017-10-19 14:42:30 +02004664 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004665 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004666
4667 /* we don't send an alert if the server was previously paused */
4668 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004669 send_log(s->proxy, log_level, "%s.\n",
4670 tmptrash->area);
4671 send_email_alert(s, log_level, "%s",
4672 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004673 free_trash_chunk(tmptrash);
4674 tmptrash = NULL;
4675 }
4676 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4677 set_backend_down(s->proxy);
4678
4679 s->counters.down_trans++;
4680 }
4681 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4682 s->last_change = now.tv_sec;
4683 if (s->proxy->lbprm.set_server_status_down)
4684 s->proxy->lbprm.set_server_status_down(s);
4685
4686 /* we might have streams queued on this server and waiting for
4687 * a connection. Those which are redispatchable will be queued
4688 * to another server or to the proxy itself.
4689 */
4690 xferred = pendconn_redistribute(s);
4691
4692 tmptrash = alloc_trash_chunk();
4693 if (tmptrash) {
4694 chunk_printf(tmptrash,
4695 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4696 s->proxy->id, s->id);
4697
Emeric Brun5a133512017-10-19 14:42:30 +02004698 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004699
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004700 ha_warning("%s.\n", tmptrash->area);
4701 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4702 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004703 free_trash_chunk(tmptrash);
4704 tmptrash = NULL;
4705 }
4706
4707 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4708 set_backend_down(s->proxy);
4709 }
4710 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4711 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4712 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4713 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4714 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4715 s->proxy->last_change = now.tv_sec;
4716 }
4717
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01004718 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02004719 s->down_time += now.tv_sec - s->last_change;
4720
4721 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004722 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004723 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4724
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004725 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004726 /* now propagate the status change to any LB algorithms */
4727 if (px->lbprm.update_server_eweight)
4728 px->lbprm.update_server_eweight(s);
4729 else if (srv_willbe_usable(s)) {
4730 if (px->lbprm.set_server_status_up)
4731 px->lbprm.set_server_status_up(s);
4732 }
4733 else {
4734 if (px->lbprm.set_server_status_down)
4735 px->lbprm.set_server_status_down(s);
4736 }
4737
4738 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4739 * and it's not a backup server and its effective weight is > 0,
4740 * then it can accept new connections, so we shut down all streams
4741 * on all backup servers.
4742 */
4743 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4744 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4745 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4746
4747 /* check if we can handle some connections queued at the proxy. We
4748 * will take as many as we can handle.
4749 */
4750 xferred = pendconn_grab_from_px(s);
4751
4752 tmptrash = alloc_trash_chunk();
4753 if (tmptrash) {
4754 chunk_printf(tmptrash,
4755 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4756 s->proxy->id, s->id);
4757
Emeric Brun5a133512017-10-19 14:42:30 +02004758 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004759 ha_warning("%s.\n", tmptrash->area);
4760 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4761 tmptrash->area);
4762 send_email_alert(s, LOG_NOTICE, "%s",
4763 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004764 free_trash_chunk(tmptrash);
4765 tmptrash = NULL;
4766 }
4767
4768 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4769 set_backend_down(s->proxy);
4770 }
4771 else if (s->cur_eweight != s->next_eweight) {
4772 /* now propagate the status change to any LB algorithms */
4773 if (px->lbprm.update_server_eweight)
4774 px->lbprm.update_server_eweight(s);
4775 else if (srv_willbe_usable(s)) {
4776 if (px->lbprm.set_server_status_up)
4777 px->lbprm.set_server_status_up(s);
4778 }
4779 else {
4780 if (px->lbprm.set_server_status_down)
4781 px->lbprm.set_server_status_down(s);
4782 }
4783
4784 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4785 set_backend_down(s->proxy);
4786 }
4787
4788 s->next_admin = next_admin;
4789 }
4790
Emeric Brun5a133512017-10-19 14:42:30 +02004791 /* reset operational state change */
4792 *s->op_st_chg.reason = 0;
4793 s->op_st_chg.status = s->op_st_chg.code = -1;
4794 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004795
4796 /* Now we try to apply pending admin changes */
4797
4798 /* Maintenance must also disable health checks */
4799 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4800 if (s->check.state & CHK_ST_ENABLED) {
4801 s->check.state |= CHK_ST_PAUSED;
4802 check->health = 0;
4803 }
4804
4805 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004806 tmptrash = alloc_trash_chunk();
4807 if (tmptrash) {
4808 chunk_printf(tmptrash,
4809 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4810 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4811 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004812
Olivier Houchard796a2b32017-10-24 17:42:47 +02004813 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004814
Olivier Houchard796a2b32017-10-24 17:42:47 +02004815 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004816 ha_warning("%s.\n", tmptrash->area);
4817 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4818 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004819 }
4820 free_trash_chunk(tmptrash);
4821 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004822 }
Emeric Brun8f298292017-12-06 16:47:17 +01004823 /* commit new admin status */
4824
4825 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004826 }
4827 else { /* server was still running */
4828 check->health = 0; /* failure */
4829 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004830
4831 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004832 if (s->proxy->lbprm.set_server_status_down)
4833 s->proxy->lbprm.set_server_status_down(s);
4834
Emeric Brun64cc49c2017-10-03 14:46:45 +02004835 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4836 srv_shutdown_streams(s, SF_ERR_DOWN);
4837
William Dauchy6318d332020-05-02 21:52:36 +02004838 /* force connection cleanup on the given server */
4839 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004840 /* we might have streams queued on this server and waiting for
4841 * a connection. Those which are redispatchable will be queued
4842 * to another server or to the proxy itself.
4843 */
4844 xferred = pendconn_redistribute(s);
4845
4846 tmptrash = alloc_trash_chunk();
4847 if (tmptrash) {
4848 chunk_printf(tmptrash,
4849 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4850 s->flags & SRV_F_BACKUP ? "Backup " : "",
4851 s->proxy->id, s->id,
4852 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4853
4854 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4855
4856 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004857 ha_warning("%s.\n", tmptrash->area);
4858 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4859 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004860 }
4861 free_trash_chunk(tmptrash);
4862 tmptrash = NULL;
4863 }
4864 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4865 set_backend_down(s->proxy);
4866
4867 s->counters.down_trans++;
4868 }
4869 }
4870 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4871 /* OK here we're leaving maintenance, we have many things to check,
4872 * because the server might possibly be coming back up depending on
4873 * its state. In practice, leaving maintenance means that we should
4874 * immediately turn to UP (more or less the slowstart) under the
4875 * following conditions :
4876 * - server is neither checked nor tracked
4877 * - server tracks another server which is not checked
4878 * - server tracks another server which is already up
4879 * Which sums up as something simpler :
4880 * "either the tracking server is up or the server's checks are disabled
4881 * or up". Otherwise we only re-enable health checks. There's a special
4882 * case associated to the stopping state which can be inherited. Note
4883 * that the server might still be in drain mode, which is naturally dealt
4884 * with by the lower level functions.
4885 */
4886
4887 if (s->check.state & CHK_ST_ENABLED) {
4888 s->check.state &= ~CHK_ST_PAUSED;
4889 check->health = check->rise; /* start OK but check immediately */
4890 }
4891
4892 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4893 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4894 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4895 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4896 s->next_state = SRV_ST_STOPPING;
4897 }
4898 else {
4899 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004900 if (s->slowstart > 0) {
4901 if (s->warmup)
4902 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4903 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02004904 else
4905 s->next_state = SRV_ST_RUNNING;
4906 }
4907
4908 }
4909
4910 tmptrash = alloc_trash_chunk();
4911 if (tmptrash) {
4912 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4913 chunk_printf(tmptrash,
4914 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4915 s->flags & SRV_F_BACKUP ? "Backup " : "",
4916 s->proxy->id, s->id,
4917 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4918 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4919 }
4920 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4921 chunk_printf(tmptrash,
4922 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4923 s->flags & SRV_F_BACKUP ? "Backup " : "",
4924 s->proxy->id, s->id, s->hostname,
4925 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4926 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4927 }
4928 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4929 chunk_printf(tmptrash,
4930 "%sServer %s/%s is %s/%s (leaving maintenance)",
4931 s->flags & SRV_F_BACKUP ? "Backup " : "",
4932 s->proxy->id, s->id,
4933 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4934 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4935 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004936 ha_warning("%s.\n", tmptrash->area);
4937 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4938 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004939 free_trash_chunk(tmptrash);
4940 tmptrash = NULL;
4941 }
4942
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004943 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004944 /* now propagate the status change to any LB algorithms */
4945 if (px->lbprm.update_server_eweight)
4946 px->lbprm.update_server_eweight(s);
4947 else if (srv_willbe_usable(s)) {
4948 if (px->lbprm.set_server_status_up)
4949 px->lbprm.set_server_status_up(s);
4950 }
4951 else {
4952 if (px->lbprm.set_server_status_down)
4953 px->lbprm.set_server_status_down(s);
4954 }
4955
4956 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4957 set_backend_down(s->proxy);
4958
Willy Tarreau6a78e612018-08-07 10:14:53 +02004959 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4960 * and it's not a backup server and its effective weight is > 0,
4961 * then it can accept new connections, so we shut down all streams
4962 * on all backup servers.
4963 */
4964 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4965 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4966 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4967
4968 /* check if we can handle some connections queued at the proxy. We
4969 * will take as many as we can handle.
4970 */
4971 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004972 }
4973 else if (s->next_admin & SRV_ADMF_MAINT) {
4974 /* remaining in maintenance mode, let's inform precisely about the
4975 * situation.
4976 */
4977 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4978 tmptrash = alloc_trash_chunk();
4979 if (tmptrash) {
4980 chunk_printf(tmptrash,
4981 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4982 s->flags & SRV_F_BACKUP ? "Backup " : "",
4983 s->proxy->id, s->id);
4984
4985 if (s->track) /* normally it's mandatory here */
4986 chunk_appendf(tmptrash, " via %s/%s",
4987 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004988 ha_warning("%s.\n", tmptrash->area);
4989 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4990 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004991 free_trash_chunk(tmptrash);
4992 tmptrash = NULL;
4993 }
4994 }
4995 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4996 tmptrash = alloc_trash_chunk();
4997 if (tmptrash) {
4998 chunk_printf(tmptrash,
4999 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5000 s->flags & SRV_F_BACKUP ? "Backup " : "",
5001 s->proxy->id, s->id, s->hostname);
5002
5003 if (s->track) /* normally it's mandatory here */
5004 chunk_appendf(tmptrash, " via %s/%s",
5005 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005006 ha_warning("%s.\n", tmptrash->area);
5007 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5008 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005009 free_trash_chunk(tmptrash);
5010 tmptrash = NULL;
5011 }
5012 }
5013 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5014 tmptrash = alloc_trash_chunk();
5015 if (tmptrash) {
5016 chunk_printf(tmptrash,
5017 "%sServer %s/%s remains in forced maintenance",
5018 s->flags & SRV_F_BACKUP ? "Backup " : "",
5019 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005020 ha_warning("%s.\n", tmptrash->area);
5021 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5022 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005023 free_trash_chunk(tmptrash);
5024 tmptrash = NULL;
5025 }
5026 }
5027 /* don't report anything when leaving drain mode and remaining in maintenance */
5028
5029 s->cur_admin = s->next_admin;
5030 }
5031
5032 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5033 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5034 /* drain state is applied only if not yet in maint */
5035
5036 s->last_change = now.tv_sec;
5037 if (px->lbprm.set_server_status_down)
5038 px->lbprm.set_server_status_down(s);
5039
5040 /* we might have streams queued on this server and waiting for
5041 * a connection. Those which are redispatchable will be queued
5042 * to another server or to the proxy itself.
5043 */
5044 xferred = pendconn_redistribute(s);
5045
5046 tmptrash = alloc_trash_chunk();
5047 if (tmptrash) {
5048 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5049 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5050 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5051
5052 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5053
5054 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005055 ha_warning("%s.\n", tmptrash->area);
5056 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5057 tmptrash->area);
5058 send_email_alert(s, LOG_NOTICE, "%s",
5059 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005060 }
5061 free_trash_chunk(tmptrash);
5062 tmptrash = NULL;
5063 }
5064
5065 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5066 set_backend_down(s->proxy);
5067 }
5068 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5069 /* OK completely leaving drain mode */
5070 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5071 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5072 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5073 s->proxy->last_change = now.tv_sec;
5074 }
5075
5076 if (s->last_change < now.tv_sec) // ignore negative times
5077 s->down_time += now.tv_sec - s->last_change;
5078 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005079 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005080
5081 tmptrash = alloc_trash_chunk();
5082 if (tmptrash) {
5083 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5084 chunk_printf(tmptrash,
5085 "%sServer %s/%s is %s (leaving forced drain)",
5086 s->flags & SRV_F_BACKUP ? "Backup " : "",
5087 s->proxy->id, s->id,
5088 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5089 }
5090 else {
5091 chunk_printf(tmptrash,
5092 "%sServer %s/%s is %s (leaving drain)",
5093 s->flags & SRV_F_BACKUP ? "Backup " : "",
5094 s->proxy->id, s->id,
5095 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5096 if (s->track) /* normally it's mandatory here */
5097 chunk_appendf(tmptrash, " via %s/%s",
5098 s->track->proxy->id, s->track->id);
5099 }
5100
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005101 ha_warning("%s.\n", tmptrash->area);
5102 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5103 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005104 free_trash_chunk(tmptrash);
5105 tmptrash = NULL;
5106 }
5107
5108 /* now propagate the status change to any LB algorithms */
5109 if (px->lbprm.update_server_eweight)
5110 px->lbprm.update_server_eweight(s);
5111 else if (srv_willbe_usable(s)) {
5112 if (px->lbprm.set_server_status_up)
5113 px->lbprm.set_server_status_up(s);
5114 }
5115 else {
5116 if (px->lbprm.set_server_status_down)
5117 px->lbprm.set_server_status_down(s);
5118 }
5119 }
5120 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5121 /* remaining in drain mode after removing one of its flags */
5122
5123 tmptrash = alloc_trash_chunk();
5124 if (tmptrash) {
5125 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5126 chunk_printf(tmptrash,
5127 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5128 s->flags & SRV_F_BACKUP ? "Backup " : "",
5129 s->proxy->id, s->id);
5130
5131 if (s->track) /* normally it's mandatory here */
5132 chunk_appendf(tmptrash, " via %s/%s",
5133 s->track->proxy->id, s->track->id);
5134 }
5135 else {
5136 chunk_printf(tmptrash,
5137 "%sServer %s/%s remains in forced drain mode",
5138 s->flags & SRV_F_BACKUP ? "Backup " : "",
5139 s->proxy->id, s->id);
5140 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005141 ha_warning("%s.\n", tmptrash->area);
5142 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5143 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005144 free_trash_chunk(tmptrash);
5145 tmptrash = NULL;
5146 }
5147
5148 /* commit new admin status */
5149
5150 s->cur_admin = s->next_admin;
5151 }
5152 }
5153
5154 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005155 *s->adm_st_chg_cause = 0;
5156}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005157
Willy Tarreau144f84a2021-03-02 16:09:26 +01005158struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005159{
5160 struct connection *conn;
5161
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005162 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005163 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005164 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005165 }
5166
5167 return task;
5168}
5169
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005170/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005171 * moving them all.
5172 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005173 *
5174 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005175 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005176static 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 +02005177{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005178 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005179 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005180 int i = 0;
5181
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005182 node = eb_first(idle_tree);
5183 while (node) {
5184 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005185 if (toremove_nb != -1 && i >= toremove_nb)
5186 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005187
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005188 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005189 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005190 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005191 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005192
5193 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005194 }
5195 return i;
5196}
William Dauchy6318d332020-05-02 21:52:36 +02005197/* cleanup connections for a given server
5198 * might be useful when going on forced maintenance or live changing ip/port
5199 */
William Dauchy707ad322020-05-04 13:52:40 +02005200static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005201{
William Dauchy6318d332020-05-02 21:52:36 +02005202 int did_remove;
5203 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005204
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005205 /* nothing to do if pool-max-conn is null */
5206 if (!srv->max_idle_conns)
5207 return;
5208
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005209 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005210 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005211 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005212 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005213 if (srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02005214 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005215 if (srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, -1) > 0)
Olivier Houchardff1d0922020-06-29 20:15:59 +02005216 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005217 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005218 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005219 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005220
5221 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5222 break;
William Dauchy6318d332020-05-02 21:52:36 +02005223 }
William Dauchy6318d332020-05-02 21:52:36 +02005224}
5225
Willy Tarreau144f84a2021-03-02 16:09:26 +01005226struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005227{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005228 struct server *srv;
5229 struct eb32_node *eb;
5230 int i;
5231 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005232
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005233 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005234 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5235 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005236 int exceed_conns;
5237 int to_kill;
5238 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005239
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005240 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5241 if (!eb) {
5242 /* we might have reached the end of the tree, typically because
5243 * <now_ms> is in the first half and we're first scanning the last
5244 * half. Let's loop back to the beginning of the tree now.
5245 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005246
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005247 eb = eb32_first(&idle_conn_srv);
5248 if (likely(!eb))
5249 break;
5250 }
5251 if (tick_is_lt(now_ms, eb->key)) {
5252 /* timer not expired yet, revisit it later */
5253 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005254 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005255 }
5256 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005257
5258 /* Calculate how many idle connections we want to kill :
5259 * we want to remove half the difference between the total
5260 * of established connections (used or idle) and the max
5261 * number of used connections.
5262 */
5263 curr_idle = srv->curr_idle_conns;
5264 if (curr_idle == 0)
5265 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005266 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005267 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005268
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005269 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005270 if (srv->est_need_conns < srv->max_used_conns)
5271 srv->est_need_conns = srv->max_used_conns;
5272
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005273 srv->max_used_conns = srv->curr_used_conns;
5274
Willy Tarreau18ed7892020-07-02 19:05:30 +02005275 if (exceed_conns <= 0)
5276 goto remove;
5277
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005278 /* check all threads starting with ours */
5279 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005280 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005281 int j;
5282 int did_remove = 0;
5283
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005284 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5285 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005286
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005287 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005288 j = srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, max_conn);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005289 if (j > 0)
5290 did_remove = 1;
5291 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005292 srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, max_conn - j) > 0)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005293 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005294 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005295
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005296 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005297 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005298
5299 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5300 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005301 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005302remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005303 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005304
5305 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005306 /* There are still more idle connections, add the
5307 * server back in the tree.
5308 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005309 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005310 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005311 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005312 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005313 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005314 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5315
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005316 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005317 return task;
5318}
Olivier Houchard88698d92019-04-16 19:07:22 +02005319
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02005320/* Close remaining idle connections. This functions is designed to be run on
5321 * process shutdown. This guarantees a proper socket shutdown to avoid
5322 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
5323 * bypassed.
5324 *
5325 * This function is not thread-safe so it must only be called via a global
5326 * deinit function.
5327 */
5328static void srv_close_idle_conns(struct server *srv)
5329{
5330 struct eb_root **cleaned_tree;
5331 int i;
5332
5333 for (i = 0; i < global.nbthread; ++i) {
5334 struct eb_root *conn_trees[] = {
5335 &srv->per_thr[i].idle_conns,
5336 &srv->per_thr[i].safe_conns,
5337 &srv->per_thr[i].avail_conns,
5338 NULL
5339 };
5340
5341 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
5342 while (!eb_is_empty(*cleaned_tree)) {
5343 struct ebmb_node *node = ebmb_first(*cleaned_tree);
5344 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
5345 struct connection *conn = conn_hash_node->conn;
5346
5347 if (conn->ctrl->ctrl_close)
5348 conn->ctrl->ctrl_close(conn);
5349 ebmb_delete(node);
5350 }
5351 }
5352 }
5353}
5354
5355REGISTER_SERVER_DEINIT(srv_close_idle_conns);
5356
Willy Tarreau76cc6992020-07-01 18:49:24 +02005357/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
5358static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01005359 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02005360 char **err)
5361{
5362 if (too_many_args(1, args, err, NULL))
5363 return -1;
5364
5365 if (strcmp(args[1], "on") == 0)
5366 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
5367 else if (strcmp(args[1], "off") == 0)
5368 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
5369 else {
5370 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
5371 return -1;
5372 }
5373 return 0;
5374}
5375
Olivier Houchard88698d92019-04-16 19:07:22 +02005376/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5377static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01005378 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02005379 char **err)
5380{
5381 int arg = -1;
5382
5383 if (too_many_args(1, args, err, NULL))
5384 return -1;
5385
5386 if (*(args[1]) != 0)
5387 arg = atoi(args[1]);
5388
5389 if (arg < 0 || arg > 100) {
5390 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5391 return -1;
5392 }
5393
5394 if (args[0][10] == 'h')
5395 global.tune.pool_high_ratio = arg;
5396 else
5397 global.tune.pool_low_ratio = arg;
5398 return 0;
5399}
5400
5401/* config keyword parsers */
5402static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02005403 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02005404 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5405 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5406 { 0, NULL, NULL }
5407}};
5408
5409INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5410
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005411/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005412 * Local variables:
5413 * c-indent-level: 8
5414 * c-basic-offset: 8
5415 * End:
5416 */