blob: 1fd29046e841f418bc2ee2c98dd6e087d5a667af [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 Tarreaub2551052020-06-09 09:07:15 +020019#include <import/ebsttree.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <import/xxhash.h>
21
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020023#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020024#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020025#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020026#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020028#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020029#include <haproxy/dict-t.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020030#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020032#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020033#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020034#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020035#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/port_range.h>
37#include <haproxy/protocol.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020038#include <haproxy/queue.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020040#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020041#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020044#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020045#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020046#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020047
Baptiste Assmannda29fe22019-06-13 13:24:29 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020050static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010052static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020053static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
54static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020055static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau21faa912012-10-10 08:27:36 +020057/* List head of all known server keywords */
58static struct srv_kw_list srv_keywords = {
59 .list = LIST_HEAD_INIT(srv_keywords.list)
60};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061
Willy Tarreauaf613e82020-06-05 08:40:51 +020062__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010063struct eb_root idle_conn_srv = EB_ROOT;
64struct task *idle_conn_task = NULL;
65struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020066struct mt_list toremove_connections[MAX_THREADS];
Willy Tarreauaf613e82020-06-05 08:40:51 +020067__decl_thread(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010068
Frédéric Lécaille7da71292019-05-20 09:47:07 +020069/* The server names dictionary */
70struct dict server_name_dict = {
71 .name = "server names",
72 .values = EB_ROOT_UNIQUE,
73};
74
Baptiste Assmannda29fe22019-06-13 13:24:29 +020075/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010076struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020077
Simon Hormana3608442013-11-01 16:46:15 +090078int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020079{
Emeric Brun52a91d32017-08-31 14:41:55 +020080 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020081 return s->down_time;
82
83 return now.tv_sec - s->last_change + s->down_time;
84}
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050086int srv_lastsession(const struct server *s)
87{
88 if (s->counters.last_sess)
89 return now.tv_sec - s->counters.last_sess;
90
91 return -1;
92}
93
Simon Horman4a741432013-02-23 15:35:38 +090094int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020095{
Simon Horman4a741432013-02-23 15:35:38 +090096 const struct server *s = check->server;
97
Willy Tarreauff5ae352013-12-11 20:36:34 +010098 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090099 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100100
Emeric Brun52a91d32017-08-31 14:41:55 +0200101 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900102 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100103
Simon Horman4a741432013-02-23 15:35:38 +0900104 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100105}
106
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100107/*
108 * Check that we did not get a hash collision.
109 * Unlikely, but it can happen.
110 */
111static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100112{
113 struct proxy *p = s->proxy;
114 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100115
116 for (tmpserv = p->srv; tmpserv != NULL;
117 tmpserv = tmpserv->next) {
118 if (tmpserv == s)
119 continue;
120 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
121 continue;
122 if (tmpserv->cookie &&
123 strcmp(tmpserv->cookie, s->cookie) == 0) {
124 ha_warning("We generated two equal cookies for two different servers.\n"
125 "Please change the secret key for '%s'.\n",
126 s->proxy->id);
127 }
128 }
129
130}
131
Willy Tarreau46b7f532018-08-21 11:54:26 +0200132/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200133 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200134 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100135void srv_set_dyncookie(struct server *s)
136{
137 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100138 char *tmpbuf;
139 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100140 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100141 size_t buffer_len;
142 int addr_len;
143 int port;
144
Willy Tarreau5e83d992019-07-30 11:59:34 +0200145 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
146
Olivier Houchard4e694042017-03-14 20:01:29 +0100147 if ((s->flags & SRV_F_COOKIESET) ||
148 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
149 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200150 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100151 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100152
153 if (s->addr.ss_family != AF_INET &&
154 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200155 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100156 /*
157 * Buffer to calculate the cookie value.
158 * The buffer contains the secret key + the server IP address
159 * + the TCP port.
160 */
161 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
162 /*
163 * The TCP port should use only 2 bytes, but is stored in
164 * an unsigned int in struct server, so let's use 4, to be
165 * on the safe side.
166 */
167 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200168 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100169 memcpy(tmpbuf, p->dyncookie_key, key_len);
170 memcpy(&(tmpbuf[key_len]),
171 s->addr.ss_family == AF_INET ?
172 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
173 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
174 addr_len);
175 /*
176 * Make sure it's the same across all the load balancers,
177 * no matter their endianness.
178 */
179 port = htonl(s->svc_port);
180 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
181 hash_value = XXH64(tmpbuf, buffer_len, 0);
182 memprintf(&s->cookie, "%016llx", hash_value);
183 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200184 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100185 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100186
187 /* Don't bother checking if the dyncookie is duplicated if
188 * the server is marked as "disabled", maybe it doesn't have
189 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100190 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100191 if (!(s->next_admin & SRV_ADMF_FMAINT))
192 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200193 out:
194 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100195}
196
Willy Tarreau21faa912012-10-10 08:27:36 +0200197/*
198 * Registers the server keyword list <kwl> as a list of valid keywords for next
199 * parsing sessions.
200 */
201void srv_register_keywords(struct srv_kw_list *kwl)
202{
203 LIST_ADDQ(&srv_keywords.list, &kwl->list);
204}
205
206/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
207 * keyword is found with a NULL ->parse() function, then an attempt is made to
208 * find one with a valid ->parse() function. This way it is possible to declare
209 * platform-dependant, known keywords as NULL, then only declare them as valid
210 * if some options are met. Note that if the requested keyword contains an
211 * opening parenthesis, everything from this point is ignored.
212 */
213struct srv_kw *srv_find_kw(const char *kw)
214{
215 int index;
216 const char *kwend;
217 struct srv_kw_list *kwl;
218 struct srv_kw *ret = NULL;
219
220 kwend = strchr(kw, '(');
221 if (!kwend)
222 kwend = kw + strlen(kw);
223
224 list_for_each_entry(kwl, &srv_keywords.list, list) {
225 for (index = 0; kwl->kw[index].kw != NULL; index++) {
226 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
227 kwl->kw[index].kw[kwend-kw] == 0) {
228 if (kwl->kw[index].parse)
229 return &kwl->kw[index]; /* found it !*/
230 else
231 ret = &kwl->kw[index]; /* may be OK */
232 }
233 }
234 }
235 return ret;
236}
237
238/* Dumps all registered "server" keywords to the <out> string pointer. The
239 * unsupported keywords are only dumped if their supported form was not
240 * found.
241 */
242void srv_dump_kws(char **out)
243{
244 struct srv_kw_list *kwl;
245 int index;
246
Christopher Faulet784063e2020-05-18 12:14:18 +0200247 if (!out)
248 return;
249
Willy Tarreau21faa912012-10-10 08:27:36 +0200250 *out = NULL;
251 list_for_each_entry(kwl, &srv_keywords.list, list) {
252 for (index = 0; kwl->kw[index].kw != NULL; index++) {
253 if (kwl->kw[index].parse ||
254 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
255 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
256 kwl->scope,
257 kwl->kw[index].kw,
258 kwl->kw[index].skip ? " <arg>" : "",
259 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
260 kwl->kw[index].parse ? "" : " (not supported)");
261 }
262 }
263 }
264}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100265
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100266/* Parse the "backup" server keyword */
267static int srv_parse_backup(char **args, int *cur_arg,
268 struct proxy *curproxy, struct server *newsrv, char **err)
269{
270 newsrv->flags |= SRV_F_BACKUP;
271 return 0;
272}
273
Alexander Liu2a54bb72019-05-22 19:44:48 +0800274
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100275/* Parse the "cookie" server keyword */
276static int srv_parse_cookie(char **args, int *cur_arg,
277 struct proxy *curproxy, struct server *newsrv, char **err)
278{
279 char *arg;
280
281 arg = args[*cur_arg + 1];
282 if (!*arg) {
283 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
284 return ERR_ALERT | ERR_FATAL;
285 }
286
287 free(newsrv->cookie);
288 newsrv->cookie = strdup(arg);
289 newsrv->cklen = strlen(arg);
290 newsrv->flags |= SRV_F_COOKIESET;
291 return 0;
292}
293
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100294/* Parse the "disabled" server keyword */
295static int srv_parse_disabled(char **args, int *cur_arg,
296 struct proxy *curproxy, struct server *newsrv, char **err)
297{
Emeric Brun52a91d32017-08-31 14:41:55 +0200298 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
299 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100300 newsrv->check.state |= CHK_ST_PAUSED;
301 newsrv->check.health = 0;
302 return 0;
303}
304
305/* Parse the "enabled" server keyword */
306static int srv_parse_enabled(char **args, int *cur_arg,
307 struct proxy *curproxy, struct server *newsrv, char **err)
308{
Emeric Brun52a91d32017-08-31 14:41:55 +0200309 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
310 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100311 newsrv->check.state &= ~CHK_ST_PAUSED;
312 newsrv->check.health = newsrv->check.rise;
313 return 0;
314}
315
Willy Tarreau9c538e02019-01-23 10:21:49 +0100316static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
317{
318 char *arg;
319
320 arg = args[*cur_arg + 1];
321 if (!*arg) {
322 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
323 return ERR_ALERT | ERR_FATAL;
324 }
325 newsrv->max_reuse = atoi(arg);
326
327 return 0;
328}
329
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100330static 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 +0100331{
332 const char *res;
333 char *arg;
334 unsigned int time;
335
336 arg = args[*cur_arg + 1];
337 if (!*arg) {
338 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
339 return ERR_ALERT | ERR_FATAL;
340 }
341 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200342 if (res == PARSE_TIME_OVER) {
343 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
344 args[*cur_arg+1], args[*cur_arg]);
345 return ERR_ALERT | ERR_FATAL;
346 }
347 else if (res == PARSE_TIME_UNDER) {
348 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
349 args[*cur_arg+1], args[*cur_arg]);
350 return ERR_ALERT | ERR_FATAL;
351 }
352 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100353 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
354 *res, args[*cur_arg]);
355 return ERR_ALERT | ERR_FATAL;
356 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100357 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100358
359 return 0;
360}
361
Olivier Houchard006e3102018-12-10 18:30:32 +0100362static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
363{
364 char *arg;
365
366 arg = args[*cur_arg + 1];
367 if (!*arg) {
368 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
369 return ERR_ALERT | ERR_FATAL;
370 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100371
Olivier Houchard006e3102018-12-10 18:30:32 +0100372 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100373 if ((int)newsrv->max_idle_conns < -1) {
374 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
375 return ERR_ALERT | ERR_FATAL;
376 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100377
378 return 0;
379}
380
Willy Tarreaudff55432012-10-10 17:51:05 +0200381/* parse the "id" server keyword */
382static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
383{
384 struct eb32_node *node;
385
386 if (!*args[*cur_arg + 1]) {
387 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
388 return ERR_ALERT | ERR_FATAL;
389 }
390
391 newsrv->puid = atol(args[*cur_arg + 1]);
392 newsrv->conf.id.key = newsrv->puid;
393
394 if (newsrv->puid <= 0) {
395 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
396 return ERR_ALERT | ERR_FATAL;
397 }
398
399 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
400 if (node) {
401 struct server *target = container_of(node, struct server, conf.id);
402 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
403 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
404 target->id);
405 return ERR_ALERT | ERR_FATAL;
406 }
407
408 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200409 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200410 return 0;
411}
412
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100413/* Parse the "namespace" server keyword */
414static int srv_parse_namespace(char **args, int *cur_arg,
415 struct proxy *curproxy, struct server *newsrv, char **err)
416{
Willy Tarreaue5733232019-05-22 19:24:06 +0200417#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100418 char *arg;
419
420 arg = args[*cur_arg + 1];
421 if (!*arg) {
422 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
423 return ERR_ALERT | ERR_FATAL;
424 }
425
426 if (!strcmp(arg, "*")) {
427 /* Use the namespace associated with the connection (if present). */
428 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
429 return 0;
430 }
431
432 /*
433 * As this parser may be called several times for the same 'default-server'
434 * object, or for a new 'server' instance deriving from a 'default-server'
435 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
436 */
437 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
438
439 newsrv->netns = netns_store_lookup(arg, strlen(arg));
440 if (!newsrv->netns)
441 newsrv->netns = netns_store_insert(arg);
442
443 if (!newsrv->netns) {
444 memprintf(err, "Cannot open namespace '%s'", arg);
445 return ERR_ALERT | ERR_FATAL;
446 }
447
448 return 0;
449#else
450 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
451 return ERR_ALERT | ERR_FATAL;
452#endif
453}
454
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100455/* Parse the "no-backup" server keyword */
456static int srv_parse_no_backup(char **args, int *cur_arg,
457 struct proxy *curproxy, struct server *newsrv, char **err)
458{
459 newsrv->flags &= ~SRV_F_BACKUP;
460 return 0;
461}
462
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100463
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100464/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200465static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100466{
467 srv->pp_opts &= ~flags;
468 return 0;
469}
470
471/* Parse the "no-send-proxy" server keyword */
472static int srv_parse_no_send_proxy(char **args, int *cur_arg,
473 struct proxy *curproxy, struct server *newsrv, char **err)
474{
475 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
476}
477
478/* Parse the "no-send-proxy-v2" server keyword */
479static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
480 struct proxy *curproxy, struct server *newsrv, char **err)
481{
482 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
483}
484
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200485/* Parse the "no-tfo" server keyword */
486static int srv_parse_no_tfo(char **args, int *cur_arg,
487 struct proxy *curproxy, struct server *newsrv, char **err)
488{
489 newsrv->flags &= ~SRV_F_FASTOPEN;
490 return 0;
491}
492
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100493/* Parse the "non-stick" server keyword */
494static int srv_parse_non_stick(char **args, int *cur_arg,
495 struct proxy *curproxy, struct server *newsrv, char **err)
496{
497 newsrv->flags |= SRV_F_NON_STICK;
498 return 0;
499}
500
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100501/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200502static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100503{
504 srv->pp_opts |= flags;
505 return 0;
506}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200507/* parse the "proto" server keyword */
508static int srv_parse_proto(char **args, int *cur_arg,
509 struct proxy *px, struct server *newsrv, char **err)
510{
511 struct ist proto;
512
513 if (!*args[*cur_arg + 1]) {
514 memprintf(err, "'%s' : missing value", args[*cur_arg]);
515 return ERR_ALERT | ERR_FATAL;
516 }
517 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
518 newsrv->mux_proto = get_mux_proto(proto);
519 if (!newsrv->mux_proto) {
520 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
521 return ERR_ALERT | ERR_FATAL;
522 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200523 return 0;
524}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100525
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100526/* parse the "proxy-v2-options" */
527static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
528 struct proxy *px, struct server *newsrv, char **err)
529{
530 char *p, *n;
531 for (p = args[*cur_arg+1]; p; p = n) {
532 n = strchr(p, ',');
533 if (n)
534 *n++ = '\0';
535 if (!strcmp(p, "ssl")) {
536 newsrv->pp_opts |= SRV_PP_V2_SSL;
537 } else if (!strcmp(p, "cert-cn")) {
538 newsrv->pp_opts |= SRV_PP_V2_SSL;
539 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100540 } else if (!strcmp(p, "cert-key")) {
541 newsrv->pp_opts |= SRV_PP_V2_SSL;
542 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
543 } else if (!strcmp(p, "cert-sig")) {
544 newsrv->pp_opts |= SRV_PP_V2_SSL;
545 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
546 } else if (!strcmp(p, "ssl-cipher")) {
547 newsrv->pp_opts |= SRV_PP_V2_SSL;
548 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100549 } else if (!strcmp(p, "authority")) {
550 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100551 } else if (!strcmp(p, "crc32c")) {
552 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100553 } else if (!strcmp(p, "unique-id")) {
554 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100555 } else
556 goto fail;
557 }
558 return 0;
559 fail:
560 if (err)
561 memprintf(err, "'%s' : proxy v2 option not implemented", p);
562 return ERR_ALERT | ERR_FATAL;
563}
564
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100565/* Parse the "observe" server keyword */
566static int srv_parse_observe(char **args, int *cur_arg,
567 struct proxy *curproxy, struct server *newsrv, char **err)
568{
569 char *arg;
570
571 arg = args[*cur_arg + 1];
572 if (!*arg) {
573 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
574 return ERR_ALERT | ERR_FATAL;
575 }
576
577 if (!strcmp(arg, "none")) {
578 newsrv->observe = HANA_OBS_NONE;
579 }
580 else if (!strcmp(arg, "layer4")) {
581 newsrv->observe = HANA_OBS_LAYER4;
582 }
583 else if (!strcmp(arg, "layer7")) {
584 if (curproxy->mode != PR_MODE_HTTP) {
585 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
586 return ERR_ALERT;
587 }
588 newsrv->observe = HANA_OBS_LAYER7;
589 }
590 else {
591 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
592 "but got '%s'\n", args[*cur_arg], arg);
593 return ERR_ALERT | ERR_FATAL;
594 }
595
596 return 0;
597}
598
Frédéric Lécaille16186232017-03-14 16:42:49 +0100599/* Parse the "redir" server keyword */
600static int srv_parse_redir(char **args, int *cur_arg,
601 struct proxy *curproxy, struct server *newsrv, char **err)
602{
603 char *arg;
604
605 arg = args[*cur_arg + 1];
606 if (!*arg) {
607 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
608 return ERR_ALERT | ERR_FATAL;
609 }
610
611 free(newsrv->rdr_pfx);
612 newsrv->rdr_pfx = strdup(arg);
613 newsrv->rdr_len = strlen(arg);
614
615 return 0;
616}
617
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100618/* Parse the "send-proxy" server keyword */
619static int srv_parse_send_proxy(char **args, int *cur_arg,
620 struct proxy *curproxy, struct server *newsrv, char **err)
621{
622 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
623}
624
625/* Parse the "send-proxy-v2" server keyword */
626static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
627 struct proxy *curproxy, struct server *newsrv, char **err)
628{
629 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
630}
631
Frédéric Lécailledba97072017-03-17 15:33:50 +0100632
633/* Parse the "source" server keyword */
634static int srv_parse_source(char **args, int *cur_arg,
635 struct proxy *curproxy, struct server *newsrv, char **err)
636{
637 char *errmsg;
638 int port_low, port_high;
639 struct sockaddr_storage *sk;
640 struct protocol *proto;
641
642 errmsg = NULL;
643
644 if (!*args[*cur_arg + 1]) {
645 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
646 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
647 goto err;
648 }
649
650 /* 'sk' is statically allocated (no need to be freed). */
651 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
652 if (!sk) {
653 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
654 goto err;
655 }
656
657 proto = protocol_by_family(sk->ss_family);
658 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100659 ha_alert("'%s %s' : connect() not supported for this address family.\n",
660 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100661 goto err;
662 }
663
664 newsrv->conn_src.opts |= CO_SRC_BIND;
665 newsrv->conn_src.source_addr = *sk;
666
667 if (port_low != port_high) {
668 int i;
669
670 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100671 ha_alert("'%s' does not support port offsets (found '%s').\n",
672 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100673 goto err;
674 }
675
676 if (port_low <= 0 || port_low > 65535 ||
677 port_high <= 0 || port_high > 65535 ||
678 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100679 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100680 goto err;
681 }
682 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
683 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
684 newsrv->conn_src.sport_range->ports[i] = port_low + i;
685 }
686
687 *cur_arg += 2;
688 while (*(args[*cur_arg])) {
689 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
690#if defined(CONFIG_HAP_TRANSPARENT)
691 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100692 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
693 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100694 goto err;
695 }
696 if (!strcmp(args[*cur_arg + 1], "client")) {
697 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
698 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
699 }
700 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
701 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
702 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
703 }
704 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
705 char *name, *end;
706
707 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100708 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100709 name++;
710
711 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100712 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100713 end++;
714
715 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
716 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
717 free(newsrv->conn_src.bind_hdr_name);
718 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
719 newsrv->conn_src.bind_hdr_len = end - name;
720 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
721 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
722 newsrv->conn_src.bind_hdr_occ = -1;
723
724 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100725 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100726 end++;
727 if (*end == ',') {
728 end++;
729 name = end;
730 if (*end == '-')
731 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100732 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100733 end++;
734 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
735 }
736
737 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100738 ha_alert("usesrc hdr_ip(name,num) does not support negative"
739 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100740 goto err;
741 }
742 }
743 else {
744 struct sockaddr_storage *sk;
745 int port1, port2;
746
747 /* 'sk' is statically allocated (no need to be freed). */
748 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
749 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100750 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100751 goto err;
752 }
753
754 proto = protocol_by_family(sk->ss_family);
755 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100756 ha_alert("'%s %s' : connect() not supported for this address family.\n",
757 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100758 goto err;
759 }
760
761 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100762 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
763 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100764 goto err;
765 }
766 newsrv->conn_src.tproxy_addr = *sk;
767 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
768 }
769 global.last_checks |= LSTCHK_NETADM;
770 *cur_arg += 2;
771 continue;
772#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100773 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 +0100774 goto err;
775#endif /* defined(CONFIG_HAP_TRANSPARENT) */
776 } /* "usesrc" */
777
778 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
779#ifdef SO_BINDTODEVICE
780 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100781 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100782 goto err;
783 }
784 free(newsrv->conn_src.iface_name);
785 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
786 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
787 global.last_checks |= LSTCHK_NETADM;
788#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100789 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100790 goto err;
791#endif
792 *cur_arg += 2;
793 continue;
794 }
795 /* this keyword in not an option of "source" */
796 break;
797 } /* while */
798
799 return 0;
800
801 err:
802 free(errmsg);
803 return ERR_ALERT | ERR_FATAL;
804}
805
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100806/* Parse the "stick" server keyword */
807static int srv_parse_stick(char **args, int *cur_arg,
808 struct proxy *curproxy, struct server *newsrv, char **err)
809{
810 newsrv->flags &= ~SRV_F_NON_STICK;
811 return 0;
812}
813
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100814/* Parse the "track" server keyword */
815static int srv_parse_track(char **args, int *cur_arg,
816 struct proxy *curproxy, struct server *newsrv, char **err)
817{
818 char *arg;
819
820 arg = args[*cur_arg + 1];
821 if (!*arg) {
822 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
823 return ERR_ALERT | ERR_FATAL;
824 }
825
826 free(newsrv->trackit);
827 newsrv->trackit = strdup(arg);
828
829 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800830}
831
832/* Parse the "socks4" server keyword */
833static int srv_parse_socks4(char **args, int *cur_arg,
834 struct proxy *curproxy, struct server *newsrv, char **err)
835{
836 char *errmsg;
837 int port_low, port_high;
838 struct sockaddr_storage *sk;
839 struct protocol *proto;
840
841 errmsg = NULL;
842
843 if (!*args[*cur_arg + 1]) {
844 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
845 goto err;
846 }
847
848 /* 'sk' is statically allocated (no need to be freed). */
849 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
850 if (!sk) {
851 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
852 goto err;
853 }
854
855 proto = protocol_by_family(sk->ss_family);
856 if (!proto || !proto->connect) {
857 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
858 goto err;
859 }
860
861 newsrv->flags |= SRV_F_SOCKS4_PROXY;
862 newsrv->socks4_addr = *sk;
863
864 if (port_low != port_high) {
865 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
866 goto err;
867 }
868
869 if (!port_low) {
870 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
871 goto err;
872 }
873
874 return 0;
875
876 err:
877 free(errmsg);
878 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100879}
880
Frédéric Lécailledba97072017-03-17 15:33:50 +0100881
Willy Tarreau034c88c2017-01-23 23:36:45 +0100882/* parse the "tfo" server keyword */
883static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
884{
885 newsrv->flags |= SRV_F_FASTOPEN;
886 return 0;
887}
888
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200889/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200890 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200891 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200892 *
893 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200894 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200895void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200896{
Willy Tarreau87b09662015-04-03 00:22:06 +0200897 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200898
Willy Tarreau87b09662015-04-03 00:22:06 +0200899 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
900 if (stream->srv_conn == srv)
901 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200902}
903
904/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200905 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200906 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200907 *
908 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200909 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200910void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200911{
912 struct server *srv;
913
914 for (srv = px->srv; srv != NULL; srv = srv->next)
915 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200916 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200917}
918
Willy Tarreaubda92272014-05-20 21:55:30 +0200919/* Appends some information to a message string related to a server going UP or
920 * DOWN. If both <forced> and <reason> are null and the server tracks another
921 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200922 * If <check> is non-null, an entire string describing the check result will be
923 * appended after a comma and a space (eg: to report some information from the
924 * check that changed the state). In the other case, the string will be built
925 * using the check results stored into the struct server if present.
926 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200927 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200928 *
929 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200930 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200931void srv_append_status(struct buffer *msg, struct server *s,
932 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200933{
Emeric Brun5a133512017-10-19 14:42:30 +0200934 short status = s->op_st_chg.status;
935 short code = s->op_st_chg.code;
936 long duration = s->op_st_chg.duration;
937 char *desc = s->op_st_chg.reason;
938
939 if (check) {
940 status = check->status;
941 code = check->code;
942 duration = check->duration;
943 desc = check->desc;
944 }
945
946 if (status != -1) {
947 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
948
949 if (status >= HCHK_STATUS_L57DATA)
950 chunk_appendf(msg, ", code: %d", code);
951
952 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200953 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200954
955 chunk_appendf(msg, ", info: \"");
956
957 chunk_initlen(&src, desc, 0, strlen(desc));
958 chunk_asciiencode(msg, &src, '"');
959
960 chunk_appendf(msg, "\"");
961 }
962
963 if (duration >= 0)
964 chunk_appendf(msg, ", check duration: %ldms", duration);
965 }
966 else if (desc && *desc) {
967 chunk_appendf(msg, ", %s", desc);
968 }
969 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200970 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200971 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200972
973 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200974 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200975 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
976 " %d sessions active, %d requeued, %d remaining in queue",
977 s->proxy->srv_act, s->proxy->srv_bck,
978 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
979 s->cur_sess, xferred, s->nbpend);
980 else
981 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
982 " %d sessions requeued, %d total in queue",
983 s->proxy->srv_act, s->proxy->srv_bck,
984 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
985 xferred, s->nbpend);
986 }
987}
988
Emeric Brun5a133512017-10-19 14:42:30 +0200989/* Marks server <s> down, regardless of its checks' statuses. The server is
990 * registered in a list to postpone the counting of the remaining servers on
991 * the proxy and transfers queued streams whenever possible to other servers at
992 * a sync point. Maintenance servers are ignored. It stores the <reason> if
993 * non-null as the reason for going down or the available data from the check
994 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200995 *
996 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200997 */
Emeric Brun5a133512017-10-19 14:42:30 +0200998void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200999{
1000 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001001
Emeric Brun64cc49c2017-10-03 14:46:45 +02001002 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001003 return;
1004
Emeric Brun52a91d32017-08-31 14:41:55 +02001005 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001006 *s->op_st_chg.reason = 0;
1007 s->op_st_chg.status = -1;
1008 if (reason) {
1009 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1010 }
1011 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001012 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001013 s->op_st_chg.code = check->code;
1014 s->op_st_chg.status = check->status;
1015 s->op_st_chg.duration = check->duration;
1016 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001017
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001018 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001019 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001020
Emeric Brun9f0b4582017-10-23 14:39:51 +02001021 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001022 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001023 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001024 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001025 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001026}
1027
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001028/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001029 * in maintenance. The server is registered in a list to postpone the counting
1030 * of the remaining servers on the proxy and tries to grab requests from the
1031 * proxy at a sync point. Maintenance servers are ignored. It stores the
1032 * <reason> if non-null as the reason for going down or the available data
1033 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001034 *
1035 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001036 */
Emeric Brun5a133512017-10-19 14:42:30 +02001037void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038{
1039 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001040
Emeric Brun64cc49c2017-10-03 14:46:45 +02001041 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001042 return;
1043
Emeric Brun52a91d32017-08-31 14:41:55 +02001044 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001045 return;
1046
Emeric Brun52a91d32017-08-31 14:41:55 +02001047 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001048 *s->op_st_chg.reason = 0;
1049 s->op_st_chg.status = -1;
1050 if (reason) {
1051 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1052 }
1053 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001054 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001055 s->op_st_chg.code = check->code;
1056 s->op_st_chg.status = check->status;
1057 s->op_st_chg.duration = check->duration;
1058 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001059
Emeric Brun64cc49c2017-10-03 14:46:45 +02001060 if (s->slowstart <= 0)
1061 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001062
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001063 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001064 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001065
Emeric Brun9f0b4582017-10-23 14:39:51 +02001066 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001067 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001068 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001069 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001070 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001071}
1072
Willy Tarreau8eb77842014-05-21 13:54:57 +02001073/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001074 * isn't in maintenance. The server is registered in a list to postpone the
1075 * counting of the remaining servers on the proxy and tries to grab requests
1076 * from the proxy. Maintenance servers are ignored. It stores the
1077 * <reason> if non-null as the reason for going down or the available data
1078 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001079 * up. Note that it makes use of the trash to build the log strings, so <reason>
1080 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001081 *
1082 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001083 */
Emeric Brun5a133512017-10-19 14:42:30 +02001084void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085{
1086 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001087
Emeric Brun64cc49c2017-10-03 14:46:45 +02001088 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001089 return;
1090
Emeric Brun52a91d32017-08-31 14:41:55 +02001091 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001092 return;
1093
Emeric Brun52a91d32017-08-31 14:41:55 +02001094 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001095 *s->op_st_chg.reason = 0;
1096 s->op_st_chg.status = -1;
1097 if (reason) {
1098 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1099 }
1100 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001101 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001102 s->op_st_chg.code = check->code;
1103 s->op_st_chg.status = check->status;
1104 s->op_st_chg.duration = check->duration;
1105 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001106
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001107 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001108 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001109
Emeric Brun9f0b4582017-10-23 14:39:51 +02001110 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001111 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001112 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001113 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001114 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001115}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001116
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001117/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1118 * enforce either maint mode or drain mode. It is not allowed to set more than
1119 * one flag at once. The equivalent "inherited" flag is propagated to all
1120 * tracking servers. Maintenance mode disables health checks (but not agent
1121 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001122 * is done. If <cause> is non-null, it will be displayed at the end of the log
1123 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001124 *
1125 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001127void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001128{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001130
1131 if (!mode)
1132 return;
1133
1134 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001135 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001136 return;
1137
Emeric Brun52a91d32017-08-31 14:41:55 +02001138 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001139 if (cause)
1140 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1141
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001142 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001143 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001144
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001145 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001146 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1147 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001148 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001149
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001150 /* compute the inherited flag to propagate */
1151 if (mode & SRV_ADMF_MAINT)
1152 mode = SRV_ADMF_IMAINT;
1153 else if (mode & SRV_ADMF_DRAIN)
1154 mode = SRV_ADMF_IDRAIN;
1155
Emeric Brun9f0b4582017-10-23 14:39:51 +02001156 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001157 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001158 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001159 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001160 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001161}
1162
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001163/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1164 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1165 * than one flag at once. The equivalent "inherited" flag is propagated to all
1166 * tracking servers. Leaving maintenance mode re-enables health checks. When
1167 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001168 *
1169 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001171void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001172{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001173 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001174
1175 if (!mode)
1176 return;
1177
1178 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001179 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001180 return;
1181
Emeric Brun52a91d32017-08-31 14:41:55 +02001182 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001183
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001184 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001185 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001186
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001187 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001188 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1189 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001190 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001191
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001192 if (mode & SRV_ADMF_MAINT)
1193 mode = SRV_ADMF_IMAINT;
1194 else if (mode & SRV_ADMF_DRAIN)
1195 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001196
Emeric Brun9f0b4582017-10-23 14:39:51 +02001197 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001198 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001199 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001200 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001201 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001202}
1203
Willy Tarreau757478e2016-11-03 19:22:19 +01001204/* principle: propagate maint and drain to tracking servers. This is useful
1205 * upon startup so that inherited states are correct.
1206 */
1207static void srv_propagate_admin_state(struct server *srv)
1208{
1209 struct server *srv2;
1210
1211 if (!srv->trackers)
1212 return;
1213
1214 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001215 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001216 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001217 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001218
Emeric Brun52a91d32017-08-31 14:41:55 +02001219 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001220 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001221 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001222 }
1223}
1224
1225/* Compute and propagate the admin states for all servers in proxy <px>.
1226 * Only servers *not* tracking another one are considered, because other
1227 * ones will be handled when the server they track is visited.
1228 */
1229void srv_compute_all_admin_states(struct proxy *px)
1230{
1231 struct server *srv;
1232
1233 for (srv = px->srv; srv; srv = srv->next) {
1234 if (srv->track)
1235 continue;
1236 srv_propagate_admin_state(srv);
1237 }
1238}
1239
Willy Tarreaudff55432012-10-10 17:51:05 +02001240/* Note: must not be declared <const> as its list will be overwritten.
1241 * Please take care of keeping this list alphabetically sorted, doing so helps
1242 * all code contributors.
1243 * Optional keywords are also declared with a NULL ->parse() function so that
1244 * the config parser can report an appropriate error when a known keyword was
1245 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001246 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001247 */
1248static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001249 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001250 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001251 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1252 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001253 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001254 { "max-reuse", srv_parse_max_reuse, 1, 1 }, /* Set the max number of requests on a connection, -1 means unlimited */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001255 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001256 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001257 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1258 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001259 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001260 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001261 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001262 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1 }, /* Set the max number of orphan idle connections, 0 means unlimited */
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001263 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001264 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001265 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001266 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001267 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1268 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001269 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001270 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001271 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001272 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001273 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001274 { NULL, NULL, 0 },
1275}};
1276
Willy Tarreau0108d902018-11-25 19:14:37 +01001277INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001278
Willy Tarreau004e0452013-11-21 11:22:01 +01001279/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001280 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001281 * state is automatically disabled if the time is elapsed. If <must_update> is
1282 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001283 *
1284 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001285 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001286void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001287{
1288 struct proxy *px = sv->proxy;
1289 unsigned w;
1290
1291 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1292 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001293 if (sv->next_state == SRV_ST_STARTING)
1294 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001295 }
1296
1297 /* We must take care of not pushing the server to full throttle during slow starts.
1298 * It must also start immediately, at least at the minimal step when leaving maintenance.
1299 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001300 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001301 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1302 else
1303 w = px->lbprm.wdiv;
1304
Emeric Brun52a91d32017-08-31 14:41:55 +02001305 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001306
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001307 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001308 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001309 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001310}
1311
Willy Tarreaubaaee002006-06-26 02:48:02 +02001312/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001313 * Parses weight_str and configures sv accordingly.
1314 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001315 *
1316 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001317 */
1318const char *server_parse_weight_change_request(struct server *sv,
1319 const char *weight_str)
1320{
1321 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001322 long int w;
1323 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001324
1325 px = sv->proxy;
1326
1327 /* if the weight is terminated with '%', it is set relative to
1328 * the initial weight, otherwise it is absolute.
1329 */
1330 if (!*weight_str)
1331 return "Require <weight> or <weight%>.\n";
1332
Simon Hormanb796afa2013-02-12 10:45:53 +09001333 w = strtol(weight_str, &end, 10);
1334 if (end == weight_str)
1335 return "Empty weight string empty or preceded by garbage";
1336 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001337 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001338 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001339 /* Avoid integer overflow */
1340 if (w > 25600)
1341 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001342 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001343 if (w > 256)
1344 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001345 }
1346 else if (w < 0 || w > 256)
1347 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001348 else if (end[0] != '\0')
1349 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001350
1351 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1352 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1353
1354 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001355 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001356
1357 return NULL;
1358}
1359
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001360/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001361 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1362 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001363 * Returns:
1364 * - error string on error
1365 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001366 *
1367 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001368 */
1369const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001370 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001371{
1372 unsigned char ip[INET6_ADDRSTRLEN];
1373
1374 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001375 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001376 return NULL;
1377 }
1378 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001379 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001380 return NULL;
1381 }
1382
1383 return "Could not understand IP address format.\n";
1384}
1385
Willy Tarreau46b7f532018-08-21 11:54:26 +02001386/*
1387 * Must be called with the server lock held.
1388 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001389const char *server_parse_maxconn_change_request(struct server *sv,
1390 const char *maxconn_str)
1391{
1392 long int v;
1393 char *end;
1394
1395 if (!*maxconn_str)
1396 return "Require <maxconn>.\n";
1397
1398 v = strtol(maxconn_str, &end, 10);
1399 if (end == maxconn_str)
1400 return "maxconn string empty or preceded by garbage";
1401 else if (end[0] != '\0')
1402 return "Trailing garbage in maxconn string";
1403
1404 if (sv->maxconn == sv->minconn) { // static maxconn
1405 sv->maxconn = sv->minconn = v;
1406 } else { // dynamic maxconn
1407 sv->maxconn = v;
1408 }
1409
1410 if (may_dequeue_tasks(sv, sv->proxy))
1411 process_srv_queue(sv);
1412
1413 return NULL;
1414}
1415
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001416#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001417static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1418 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001419{
1420 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001421 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001422 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001423 NULL,
1424 };
1425
1426 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001427 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001428
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001429 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001430}
1431
1432static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1433{
1434 struct sample_expr *expr;
1435
1436 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 +01001437 if (!expr) {
1438 memprintf(err, "error detected while parsing sni expression : %s", *err);
1439 return ERR_ALERT | ERR_FATAL;
1440 }
1441
1442 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1443 memprintf(err, "error detected while parsing sni expression : "
1444 " fetch method '%s' extracts information from '%s', "
1445 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001446 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001447 return ERR_ALERT | ERR_FATAL;
1448 }
1449
1450 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1451 release_sample_expr(newsrv->ssl_ctx.sni);
1452 newsrv->ssl_ctx.sni = expr;
1453
1454 return 0;
1455}
1456#endif
1457
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001458static 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 +01001459{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001460 char *msg = "error encountered while processing ";
1461 char *quote = "'";
1462 char *token = args[cur_arg];
1463
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001464 if (err && *err) {
1465 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001466 msg = *err;
1467 quote = "";
1468 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001469 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001470
1471 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1472 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1473 file, linenum, args[0], args[1],
1474 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001475 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001476 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1477 file, linenum, args[0], args[1],
1478 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001479}
1480
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001481static void srv_conn_src_sport_range_cpy(struct server *srv,
1482 struct server *src)
1483{
1484 int range_sz;
1485
1486 range_sz = src->conn_src.sport_range->size;
1487 if (range_sz > 0) {
1488 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1489 if (srv->conn_src.sport_range != NULL) {
1490 int i;
1491
1492 for (i = 0; i < range_sz; i++) {
1493 srv->conn_src.sport_range->ports[i] =
1494 src->conn_src.sport_range->ports[i];
1495 }
1496 }
1497 }
1498}
1499
1500/*
1501 * Copy <src> server connection source settings to <srv> server everything needed.
1502 */
1503static void srv_conn_src_cpy(struct server *srv, struct server *src)
1504{
1505 srv->conn_src.opts = src->conn_src.opts;
1506 srv->conn_src.source_addr = src->conn_src.source_addr;
1507
1508 /* Source port range copy. */
1509 if (src->conn_src.sport_range != NULL)
1510 srv_conn_src_sport_range_cpy(srv, src);
1511
1512#ifdef CONFIG_HAP_TRANSPARENT
1513 if (src->conn_src.bind_hdr_name != NULL) {
1514 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1515 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1516 }
1517 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1518 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1519#endif
1520 if (src->conn_src.iface_name != NULL)
1521 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1522}
1523
1524/*
1525 * Copy <src> server SSL settings to <srv> server allocating
1526 * everything needed.
1527 */
1528#if defined(USE_OPENSSL)
1529static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1530{
1531 if (src->ssl_ctx.ca_file != NULL)
1532 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1533 if (src->ssl_ctx.crl_file != NULL)
1534 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1535 if (src->ssl_ctx.client_crt != NULL)
1536 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1537
1538 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1539
1540 if (src->ssl_ctx.verify_host != NULL)
1541 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1542 if (src->ssl_ctx.ciphers != NULL)
1543 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001544 if (src->ssl_ctx.options)
1545 srv->ssl_ctx.options = src->ssl_ctx.options;
1546 if (src->ssl_ctx.methods.flags)
1547 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1548 if (src->ssl_ctx.methods.min)
1549 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1550 if (src->ssl_ctx.methods.max)
1551 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1552
Willy Tarreau5db847a2019-05-09 14:13:35 +02001553#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001554 if (src->ssl_ctx.ciphersuites != NULL)
1555 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1556#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001557 if (src->sni_expr != NULL)
1558 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001559
1560#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1561 if (src->ssl_ctx.alpn_str) {
1562 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1563 if (srv->ssl_ctx.alpn_str) {
1564 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1565 src->ssl_ctx.alpn_len);
1566 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1567 }
1568 }
1569#endif
1570#ifdef OPENSSL_NPN_NEGOTIATED
1571 if (src->ssl_ctx.npn_str) {
1572 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1573 if (srv->ssl_ctx.npn_str) {
1574 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1575 src->ssl_ctx.npn_len);
1576 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1577 }
1578 }
1579#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001580}
1581#endif
1582
1583/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001584 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001585 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001586 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001587 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001588static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001589{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001590 char *hostname_dn;
1591 int hostname_len, hostname_dn_len;
1592
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001593 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001594 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001595
Christopher Faulet67957bd2017-09-27 11:00:59 +02001596 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001597 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001598 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1599 hostname_dn, trash.size);
1600 if (hostname_dn_len == -1)
1601 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001602
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001603
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604 free(srv->hostname);
1605 free(srv->hostname_dn);
1606 srv->hostname = strdup(hostname);
1607 srv->hostname_dn = strdup(hostname_dn);
1608 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001609 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001610 goto err;
1611
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001612 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001613
1614 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001615 free(srv->hostname); srv->hostname = NULL;
1616 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001617 return -1;
1618}
1619
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001620/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001621 * Copy <src> server settings to <srv> server allocating
1622 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001623 * This function is not supposed to be called at any time, but only
1624 * during server settings parsing or during server allocations from
1625 * a server template, and just after having calloc()'ed a new server.
1626 * So, <src> may only be a default server (when parsing server settings)
1627 * or a server template (during server allocations from a server template).
1628 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1629 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001630 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001631static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001632{
1633 /* Connection source settings copy */
1634 srv_conn_src_cpy(srv, src);
1635
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001636 if (srv_tmpl) {
1637 srv->addr = src->addr;
1638 srv->svc_port = src->svc_port;
1639 }
1640
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001641 srv->pp_opts = src->pp_opts;
1642 if (src->rdr_pfx != NULL) {
1643 srv->rdr_pfx = strdup(src->rdr_pfx);
1644 srv->rdr_len = src->rdr_len;
1645 }
1646 if (src->cookie != NULL) {
1647 srv->cookie = strdup(src->cookie);
1648 srv->cklen = src->cklen;
1649 }
1650 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001651 srv->check.addr = src->check.addr;
1652 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001653 srv->check.use_ssl = src->check.use_ssl;
1654 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001655 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001656 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001657 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001658 /* Note: 'flags' field has potentially been already initialized. */
1659 srv->flags |= src->flags;
1660 srv->do_check = src->do_check;
1661 srv->do_agent = src->do_agent;
1662 if (srv->check.port)
1663 srv->flags |= SRV_F_CHECKPORT;
1664 srv->check.inter = src->check.inter;
1665 srv->check.fastinter = src->check.fastinter;
1666 srv->check.downinter = src->check.downinter;
1667 srv->agent.use_ssl = src->agent.use_ssl;
1668 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001669
1670 if (src->agent.tcpcheck_rules) {
1671 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1672 if (srv->agent.tcpcheck_rules) {
1673 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1674 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1675 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1676 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1677 &src->agent.tcpcheck_rules->preset_vars);
1678 }
1679 }
1680
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001681 srv->agent.inter = src->agent.inter;
1682 srv->agent.fastinter = src->agent.fastinter;
1683 srv->agent.downinter = src->agent.downinter;
1684 srv->maxqueue = src->maxqueue;
1685 srv->minconn = src->minconn;
1686 srv->maxconn = src->maxconn;
1687 srv->slowstart = src->slowstart;
1688 srv->observe = src->observe;
1689 srv->onerror = src->onerror;
1690 srv->onmarkeddown = src->onmarkeddown;
1691 srv->onmarkedup = src->onmarkedup;
1692 if (src->trackit != NULL)
1693 srv->trackit = strdup(src->trackit);
1694 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1695 srv->uweight = srv->iweight = src->iweight;
1696
1697 srv->check.send_proxy = src->check.send_proxy;
1698 /* health: up, but will fall down at first failure */
1699 srv->check.rise = srv->check.health = src->check.rise;
1700 srv->check.fall = src->check.fall;
1701
1702 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1704 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1705 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001706 srv->check.state |= CHK_ST_PAUSED;
1707 srv->check.health = 0;
1708 }
1709
1710 /* health: up but will fall down at first failure */
1711 srv->agent.rise = srv->agent.health = src->agent.rise;
1712 srv->agent.fall = src->agent.fall;
1713
1714 if (src->resolvers_id != NULL)
1715 srv->resolvers_id = strdup(src->resolvers_id);
1716 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001717 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001718 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001719 if (srv->dns_opts.family_prio == AF_UNSPEC)
1720 srv->dns_opts.family_prio = AF_INET6;
1721 memcpy(srv->dns_opts.pref_net,
1722 src->dns_opts.pref_net,
1723 sizeof srv->dns_opts.pref_net);
1724 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1725
1726 srv->init_addr_methods = src->init_addr_methods;
1727 srv->init_addr = src->init_addr;
1728#if defined(USE_OPENSSL)
1729 srv_ssl_settings_cpy(srv, src);
1730#endif
1731#ifdef TCP_USER_TIMEOUT
1732 srv->tcp_ut = src->tcp_ut;
1733#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001734 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001735 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001736 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001737 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001738
Olivier Houchard8da5f982017-08-04 18:35:36 +02001739 if (srv_tmpl)
1740 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001741
1742 srv->check.via_socks4 = src->check.via_socks4;
1743 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001744}
1745
William Lallemand313bfd12018-10-26 14:47:32 +02001746struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001747{
1748 struct server *srv;
1749
1750 srv = calloc(1, sizeof *srv);
1751 if (!srv)
1752 return NULL;
1753
1754 srv->obj_type = OBJ_TYPE_SERVER;
1755 srv->proxy = proxy;
1756 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001757 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001758
Emeric Brun52a91d32017-08-31 14:41:55 +02001759 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001760 srv->last_change = now.tv_sec;
1761
Christopher Faulet38290462020-04-21 11:46:40 +02001762 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001763 srv->check.status = HCHK_STATUS_INI;
1764 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001765 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001766 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001767
Christopher Faulet38290462020-04-21 11:46:40 +02001768 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001769 srv->agent.status = HCHK_STATUS_INI;
1770 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001771 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001772 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1773
Willy Tarreau975b1552019-06-06 16:25:55 +02001774 /* please don't put default server settings here, they are set in
1775 * init_default_instance().
1776 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001777 return srv;
1778}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001779
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001780#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1781static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1782 struct server *srv, struct proxy *proxy)
1783{
1784 int ret;
1785 char *err = NULL;
1786
1787 if (!srv->sni_expr)
1788 return 0;
1789
1790 ret = server_parse_sni_expr(srv, proxy, &err);
1791 if (!ret)
1792 return 0;
1793
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001794 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001795 free(err);
1796
1797 return ret;
1798}
1799#endif
1800
1801/*
1802 * Server initializations finalization.
1803 * Initialize health check, agent check and SNI expression if enabled.
1804 * Must not be called for a default server instance.
1805 */
1806static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1807 struct server *srv, struct proxy *px)
1808{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001809#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001810 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001811#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001812
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001813 if (srv->do_check && srv->trackit) {
1814 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1815 file, linenum);
1816 return ERR_ALERT | ERR_FATAL;
1817 }
1818
1819 if (srv->do_agent && !srv->agent.port) {
1820 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1821 file, linenum, srv->id);
1822 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001823 }
1824
1825#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1826 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1827 return ret;
1828#endif
1829
1830 if (srv->flags & SRV_F_BACKUP)
1831 px->srv_bck++;
1832 else
1833 px->srv_act++;
1834 srv_lb_commit_status(srv);
1835
1836 return 0;
1837}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001838
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001839/*
1840 * Parse as much as possible such a range string argument: low[-high]
1841 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1842 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1843 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1844 * Returns 0 if succeeded, -1 if not.
1845 */
1846static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1847{
1848 char *nb_high_arg;
1849
1850 *nb_high = 0;
1851 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001852 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001853
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001854 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001855 *nb_high_arg++ = '\0';
1856 *nb_high = atoi(nb_high_arg);
1857 }
1858 else {
1859 *nb_high += *nb_low;
1860 *nb_low = 1;
1861 }
1862
1863 if (*nb_low < 0 || *nb_high < *nb_low)
1864 return -1;
1865
1866 return 0;
1867}
1868
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001869static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1870{
1871 chunk_printf(&trash, "%s%d", prefix, nb);
1872 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001873 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001874}
1875
1876/*
1877 * Initialize as much as possible servers from <srv> server template.
1878 * Note that a server template is a special server with
1879 * a few different parameters than a server which has
1880 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001881 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001882 * 'srv' template included.
1883 */
1884static int server_template_init(struct server *srv, struct proxy *px)
1885{
1886 int i;
1887 struct server *newsrv;
1888
1889 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 +02001890 newsrv = new_server(px);
1891 if (!newsrv)
1892 goto err;
1893
1894 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001895 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001896#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1897 if (newsrv->sni_expr) {
1898 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1899 if (!newsrv->ssl_ctx.sni)
1900 goto err;
1901 }
1902#endif
1903 /* Set this new server ID. */
1904 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1905
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001906 /* Linked backwards first. This will be restablished after parsing. */
1907 newsrv->next = px->srv;
1908 px->srv = newsrv;
1909 }
1910 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1911
1912 return i - srv->tmpl_info.nb_low;
1913
1914 err:
1915 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1916 if (newsrv) {
1917#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1918 release_sample_expr(newsrv->ssl_ctx.sni);
1919#endif
1920 free_check(&newsrv->agent);
1921 free_check(&newsrv->check);
1922 }
1923 free(newsrv);
1924 return i - srv->tmpl_info.nb_low;
1925}
1926
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001927int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr, int in_peers_section)
Willy Tarreau272adea2014-03-31 10:39:59 +02001928{
1929 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001930 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001931 char *errmsg = NULL;
1932 int err_code = 0;
1933 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001934 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001935
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001936 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001937 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001938 !strcmp(args[0], "default-server") ||
1939 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001940 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001941 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001942 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001943 int srv_tmpl = !defsrv && !srv;
1944 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001945
1946 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001947 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001948 err_code |= ERR_ALERT | ERR_FATAL;
1949 goto out;
1950 }
1951 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001952 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001953
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001955 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001956 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001957 if (in_peers_section) {
1958 return 0;
1959 }
1960 else {
1961 /* 'server' line number of argument check. */
1962 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1963 file, linenum, args[0]);
1964 err_code |= ERR_ALERT | ERR_FATAL;
1965 goto out;
1966 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 }
1968
1969 err = invalid_char(args[1]);
1970 }
1971 else if (srv_tmpl) {
1972 if (!*args[3]) {
1973 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001974 ha_alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001975 file, linenum, args[0]);
1976 err_code |= ERR_ALERT | ERR_FATAL;
1977 goto out;
1978 }
1979
1980 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001981 }
1982
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001983 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001984 ha_alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001985 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001986 err_code |= ERR_ALERT | ERR_FATAL;
1987 goto out;
1988 }
1989
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001990 cur_arg = 2;
1991 if (srv_tmpl) {
1992 /* Parse server-template <nb | range> arg. */
1993 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001994 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001995 file, linenum, args[0], args[cur_arg]);
1996 err_code |= ERR_ALERT | ERR_FATAL;
1997 goto out;
1998 }
1999 cur_arg++;
2000 }
2001
Willy Tarreau272adea2014-03-31 10:39:59 +02002002 if (!defsrv) {
2003 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002004 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002005 struct protocol *proto;
2006
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002007 newsrv = new_server(curproxy);
2008 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002009 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002010 err_code |= ERR_ALERT | ERR_ABORT;
2011 goto out;
2012 }
2013
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002014 if (srv_tmpl) {
2015 newsrv->tmpl_info.nb_low = tmpl_range_low;
2016 newsrv->tmpl_info.nb_high = tmpl_range_high;
2017 }
2018
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 /* the servers are linked backwards first */
2020 newsrv->next = curproxy->srv;
2021 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002022 newsrv->conf.file = strdup(file);
2023 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002024 /* Note: for a server template, its id is its prefix.
2025 * This is a temporary id which will be used for server allocations to come
2026 * after parsing.
2027 */
2028 if (srv)
2029 newsrv->id = strdup(args[1]);
2030 else
2031 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002032
2033 /* several ways to check the port component :
2034 * - IP => port=+0, relative (IPv4 only)
2035 * - IP: => port=+0, relative
2036 * - IP:N => port=N, absolute
2037 * - IP:+N => port=+N, relative
2038 * - IP:-N => port=-N, relative
2039 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002040 if (!parse_addr)
2041 goto skip_addr;
2042
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002043 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002044 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002045 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002046 err_code |= ERR_ALERT | ERR_FATAL;
2047 goto out;
2048 }
2049
2050 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002051 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002052 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002053 file, linenum, args[0], args[1]);
2054 err_code |= ERR_ALERT | ERR_FATAL;
2055 goto out;
2056 }
2057
2058 if (!port1 || !port2) {
2059 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002060 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002061 }
2062 else if (port1 != port2) {
2063 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002064 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002065 file, linenum, args[0], args[1], args[2]);
2066 err_code |= ERR_ALERT | ERR_FATAL;
2067 goto out;
2068 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002069
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002070 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002071 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002072 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002073 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002074 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2075 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2076 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002077 err_code |= ERR_ALERT | ERR_FATAL;
2078 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002079 }
2080 }
2081 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002082 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002083 file, linenum, newsrv->id);
2084 err_code |= ERR_ALERT | ERR_FATAL;
2085 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002086 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002087 }
2088
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002090 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002091
Olivier Houchard8da5f982017-08-04 18:35:36 +02002092 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002093 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002094 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002095 err_code |= ERR_ALERT | ERR_FATAL;
2096 goto out;
2097 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002098
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002099 cur_arg++;
2100 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002101 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002102 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002103 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002104 } else {
2105 newsrv = &curproxy->defsrv;
2106 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002107 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002108 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002109 }
2110
2111 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002112 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002113 char *p, *end;
2114 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002115 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002116
2117 newsrv->init_addr_methods = 0;
2118 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2119
2120 for (p = args[cur_arg + 1]; *p; p = end) {
2121 /* cut on next comma */
2122 for (end = p; *end && *end != ','; end++);
2123 if (*end)
2124 *(end++) = 0;
2125
Willy Tarreau4310d362016-11-02 15:05:56 +01002126 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002127 if (!strcmp(p, "libc")) {
2128 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2129 }
2130 else if (!strcmp(p, "last")) {
2131 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2132 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002133 else if (!strcmp(p, "none")) {
2134 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2135 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002136 else if (str2ip2(p, &sa, 0)) {
2137 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002138 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002139 file, linenum, args[cur_arg], p);
2140 err_code |= ERR_ALERT | ERR_FATAL;
2141 goto out;
2142 }
2143 newsrv->init_addr = sa;
2144 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2145 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002146 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002147 ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002148 file, linenum, args[cur_arg], p);
2149 err_code |= ERR_ALERT | ERR_FATAL;
2150 goto out;
2151 }
2152 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002153 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002154 file, linenum, args[cur_arg], p);
2155 err_code |= ERR_ALERT | ERR_FATAL;
2156 goto out;
2157 }
2158 }
2159 cur_arg += 2;
2160 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002161 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002162 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002163 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2164 cur_arg += 2;
2165 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002166 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2167 char *p, *end;
2168
2169 for (p = args[cur_arg + 1]; *p; p = end) {
2170 /* cut on next comma */
2171 for (end = p; *end && *end != ','; end++);
2172 if (*end)
2173 *(end++) = 0;
2174
2175 if (!strcmp(p, "allow-dup-ip")) {
2176 newsrv->dns_opts.accept_duplicate_ip = 1;
2177 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002178 else if (!strcmp(p, "ignore-weight")) {
2179 newsrv->dns_opts.ignore_weight = 1;
2180 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002181 else if (!strcmp(p, "prevent-dup-ip")) {
2182 newsrv->dns_opts.accept_duplicate_ip = 0;
2183 }
2184 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002185 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.\n",
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002186 file, linenum, args[cur_arg], p);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
2189 }
2190 }
2191
2192 cur_arg += 2;
2193 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002194 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2195 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002196 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002197 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002198 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002199 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002200 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002201 file, linenum, args[cur_arg]);
2202 err_code |= ERR_ALERT | ERR_FATAL;
2203 goto out;
2204 }
2205 cur_arg += 2;
2206 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002207 else if (!strcmp(args[cur_arg], "resolve-net")) {
2208 char *p, *e;
2209 unsigned char mask;
2210 struct dns_options *opt;
2211
2212 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002213 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002214 file, linenum, args[cur_arg]);
2215 err_code |= ERR_ALERT | ERR_FATAL;
2216 goto out;
2217 }
2218
2219 opt = &newsrv->dns_opts;
2220
2221 /* Split arguments by comma, and convert it from ipv4 or ipv6
2222 * string network in in_addr or in6_addr.
2223 */
2224 p = args[cur_arg + 1];
2225 e = p;
2226 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002227 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002228 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002229 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002230 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 goto out;
2233 }
2234 /* look for end or comma. */
2235 while (*e != ',' && *e != '\0')
2236 e++;
2237 if (*e == ',') {
2238 *e = '\0';
2239 e++;
2240 }
2241 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2242 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2243 /* Try to convert input string from ipv4 or ipv6 network. */
2244 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2245 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2246 &mask)) {
2247 /* Try to convert input string from ipv6 network. */
2248 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2249 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2250 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002251 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002252 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002253 file, linenum, args[cur_arg], p);
2254 err_code |= ERR_ALERT | ERR_FATAL;
2255 goto out;
2256 }
2257 opt->pref_net_nb++;
2258 p = e;
2259 }
2260
2261 cur_arg += 2;
2262 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002263 else if (!strcmp(args[cur_arg], "weight")) {
2264 int w;
2265 w = atol(args[cur_arg + 1]);
2266 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002267 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002268 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2269 err_code |= ERR_ALERT | ERR_FATAL;
2270 goto out;
2271 }
2272 newsrv->uweight = newsrv->iweight = w;
2273 cur_arg += 2;
2274 }
Emeric Brun97556472020-05-30 01:42:45 +02002275 else if (!strcmp(args[cur_arg], "log-proto")) {
2276 if (!strcmp(args[cur_arg + 1], "legacy"))
2277 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2278 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2279 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2280 else {
2281 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2282 "'octet-count' but got '%s'\n",
2283 file, linenum, args[cur_arg], args[cur_arg + 1]);
2284 err_code |= ERR_ALERT | ERR_FATAL;
2285 goto out;
2286 }
2287 cur_arg += 2;
2288 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002289 else if (!strcmp(args[cur_arg], "minconn")) {
2290 newsrv->minconn = atol(args[cur_arg + 1]);
2291 cur_arg += 2;
2292 }
2293 else if (!strcmp(args[cur_arg], "maxconn")) {
2294 newsrv->maxconn = atol(args[cur_arg + 1]);
2295 cur_arg += 2;
2296 }
2297 else if (!strcmp(args[cur_arg], "maxqueue")) {
2298 newsrv->maxqueue = atol(args[cur_arg + 1]);
2299 cur_arg += 2;
2300 }
2301 else if (!strcmp(args[cur_arg], "slowstart")) {
2302 /* slowstart is stored in seconds */
2303 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002304
2305 if (err == PARSE_TIME_OVER) {
2306 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2307 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2308 err_code |= ERR_ALERT | ERR_FATAL;
2309 goto out;
2310 }
2311 else if (err == PARSE_TIME_UNDER) {
2312 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2313 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2314 err_code |= ERR_ALERT | ERR_FATAL;
2315 goto out;
2316 }
2317 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002318 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002319 file, linenum, *err, newsrv->id);
2320 err_code |= ERR_ALERT | ERR_FATAL;
2321 goto out;
2322 }
2323 newsrv->slowstart = (val + 999) / 1000;
2324 cur_arg += 2;
2325 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 else if (!strcmp(args[cur_arg], "on-error")) {
2327 if (!strcmp(args[cur_arg + 1], "fastinter"))
2328 newsrv->onerror = HANA_ONERR_FASTINTER;
2329 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2330 newsrv->onerror = HANA_ONERR_FAILCHK;
2331 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2332 newsrv->onerror = HANA_ONERR_SUDDTH;
2333 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2334 newsrv->onerror = HANA_ONERR_MARKDWN;
2335 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002336 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002337 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2338 file, linenum, args[cur_arg], args[cur_arg + 1]);
2339 err_code |= ERR_ALERT | ERR_FATAL;
2340 goto out;
2341 }
2342
2343 cur_arg += 2;
2344 }
2345 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2346 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2347 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2348 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002349 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 file, linenum, args[cur_arg], args[cur_arg + 1]);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354
2355 cur_arg += 2;
2356 }
2357 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2358 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2359 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2360 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002361 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002362 file, linenum, args[cur_arg], args[cur_arg + 1]);
2363 err_code |= ERR_ALERT | ERR_FATAL;
2364 goto out;
2365 }
2366
2367 cur_arg += 2;
2368 }
2369 else if (!strcmp(args[cur_arg], "error-limit")) {
2370 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002371 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002372 file, linenum, args[cur_arg]);
2373 err_code |= ERR_ALERT | ERR_FATAL;
2374 goto out;
2375 }
2376
2377 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2378
2379 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002380 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002381 file, linenum, args[cur_arg]);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 cur_arg += 2;
2386 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002387 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002388 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002389 file, linenum, "usesrc", "source");
2390 err_code |= ERR_ALERT | ERR_FATAL;
2391 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002392 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 else {
2394 static int srv_dumped;
2395 struct srv_kw *kw;
2396 char *err;
2397
2398 kw = srv_find_kw(args[cur_arg]);
2399 if (kw) {
2400 char *err = NULL;
2401 int code;
2402
2403 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002404 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002405 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002406 if (kw->skip != -1)
2407 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002408 err_code |= ERR_ALERT | ERR_FATAL;
2409 goto out;
2410 }
2411
2412 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002413 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002414 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002415 if (kw->skip != -1)
2416 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002417 err_code |= ERR_ALERT;
2418 continue;
2419 }
2420
2421 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2422 err_code |= code;
2423
2424 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002425 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 if (code & ERR_FATAL) {
2427 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002428 if (kw->skip != -1)
2429 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002430 goto out;
2431 }
2432 }
2433 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002434 if (kw->skip != -1)
2435 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002436 continue;
2437 }
2438
2439 err = NULL;
2440 if (!srv_dumped) {
2441 srv_dump_kws(&err);
2442 indent_msg(&err, 4);
2443 srv_dumped = 1;
2444 }
2445
Christopher Faulet767a84b2017-11-24 16:50:31 +01002446 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002447 file, linenum, args[0], args[1], args[cur_arg],
2448 err ? " Registered keywords :" : "", err ? err : "");
2449 free(err);
2450
2451 err_code |= ERR_ALERT | ERR_FATAL;
2452 goto out;
2453 }
2454 }
2455
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002456 if (!defsrv)
2457 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2458 if (err_code & ERR_FATAL)
2459 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002460 if (srv_tmpl)
2461 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002463 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 return 0;
2465
2466 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002467 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002468 free(errmsg);
2469 return err_code;
2470}
2471
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002472/* Returns a pointer to the first server matching either id <id>.
2473 * NULL is returned if no match is found.
2474 * the lookup is performed in the backend <bk>
2475 */
2476struct server *server_find_by_id(struct proxy *bk, int id)
2477{
2478 struct eb32_node *eb32;
2479 struct server *curserver;
2480
2481 if (!bk || (id ==0))
2482 return NULL;
2483
2484 /* <bk> has no backend capabilities, so it can't have a server */
2485 if (!(bk->cap & PR_CAP_BE))
2486 return NULL;
2487
2488 curserver = NULL;
2489
2490 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2491 if (eb32)
2492 curserver = container_of(eb32, struct server, conf.id);
2493
2494 return curserver;
2495}
2496
2497/* Returns a pointer to the first server matching either name <name>, or id
2498 * if <name> starts with a '#'. NULL is returned if no match is found.
2499 * the lookup is performed in the backend <bk>
2500 */
2501struct server *server_find_by_name(struct proxy *bk, const char *name)
2502{
2503 struct server *curserver;
2504
2505 if (!bk || !name)
2506 return NULL;
2507
2508 /* <bk> has no backend capabilities, so it can't have a server */
2509 if (!(bk->cap & PR_CAP_BE))
2510 return NULL;
2511
2512 curserver = NULL;
2513 if (*name == '#') {
2514 curserver = server_find_by_id(bk, atoi(name + 1));
2515 if (curserver)
2516 return curserver;
2517 }
2518 else {
2519 curserver = bk->srv;
2520
2521 while (curserver && (strcmp(curserver->id, name) != 0))
2522 curserver = curserver->next;
2523
2524 if (curserver)
2525 return curserver;
2526 }
2527
2528 return NULL;
2529}
2530
2531struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2532{
2533 struct server *byname;
2534 struct server *byid;
2535
2536 if (!name && !id)
2537 return NULL;
2538
2539 if (diff)
2540 *diff = 0;
2541
2542 byname = byid = NULL;
2543
2544 if (name) {
2545 byname = server_find_by_name(bk, name);
2546 if (byname && (!id || byname->puid == id))
2547 return byname;
2548 }
2549
2550 /* remaining possibilities :
2551 * - name not set
2552 * - name set but not found
2553 * - name found but ID doesn't match
2554 */
2555 if (id) {
2556 byid = server_find_by_id(bk, id);
2557 if (byid) {
2558 if (byname) {
2559 /* use id only if forced by configuration */
2560 if (byid->flags & SRV_F_FORCED_ID) {
2561 if (diff)
2562 *diff |= 2;
2563 return byid;
2564 }
2565 else {
2566 if (diff)
2567 *diff |= 1;
2568 return byname;
2569 }
2570 }
2571
2572 /* remaining possibilities:
2573 * - name not set
2574 * - name set but not found
2575 */
2576 if (name && diff)
2577 *diff |= 2;
2578 return byid;
2579 }
2580
2581 /* id bot found */
2582 if (byname) {
2583 if (diff)
2584 *diff |= 1;
2585 return byname;
2586 }
2587 }
2588
2589 return NULL;
2590}
2591
Willy Tarreau46b7f532018-08-21 11:54:26 +02002592/* Update a server state using the parameters available in the params list.
2593 *
2594 * Grabs the server lock during operation.
2595 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002596static void srv_update_state(struct server *srv, int version, char **params)
2597{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002598 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002599 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002600
2601 /* fields since version 1
2602 * and common to all other upcoming versions
2603 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002604 enum srv_state srv_op_state;
2605 enum srv_admin srv_admin_state;
2606 unsigned srv_uweight, srv_iweight;
2607 unsigned long srv_last_time_change;
2608 short srv_check_status;
2609 enum chk_result srv_check_result;
2610 int srv_check_health;
2611 int srv_check_state, srv_agent_state;
2612 int bk_f_forced_id;
2613 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002614 int fqdn_set_by_cli;
2615 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002616 const char *port_str;
2617 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002618 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002619
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002620 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002621 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002622 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002623 switch (version) {
2624 case 1:
2625 /*
2626 * now we can proceed with server's state update:
2627 * srv_addr: params[0]
2628 * srv_op_state: params[1]
2629 * srv_admin_state: params[2]
2630 * srv_uweight: params[3]
2631 * srv_iweight: params[4]
2632 * srv_last_time_change: params[5]
2633 * srv_check_status: params[6]
2634 * srv_check_result: params[7]
2635 * srv_check_health: params[8]
2636 * srv_check_state: params[9]
2637 * srv_agent_state: params[10]
2638 * bk_f_forced_id: params[11]
2639 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002640 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002641 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002642 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002643 */
2644
2645 /* validating srv_op_state */
2646 p = NULL;
2647 errno = 0;
2648 srv_op_state = strtol(params[1], &p, 10);
2649 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2650 (srv_op_state != SRV_ST_STOPPED &&
2651 srv_op_state != SRV_ST_STARTING &&
2652 srv_op_state != SRV_ST_RUNNING &&
2653 srv_op_state != SRV_ST_STOPPING)) {
2654 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2655 }
2656
2657 /* validating srv_admin_state */
2658 p = NULL;
2659 errno = 0;
2660 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002661 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002662
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002663 /* inherited statuses will be recomputed later.
2664 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2665 */
2666 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002667
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002668 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2669 (srv_admin_state != 0 &&
2670 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002671 srv_admin_state != SRV_ADMF_CMAINT &&
2672 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002673 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002674 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002675 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2676 }
2677
2678 /* validating srv_uweight */
2679 p = NULL;
2680 errno = 0;
2681 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002682 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002683 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2684
2685 /* validating srv_iweight */
2686 p = NULL;
2687 errno = 0;
2688 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002689 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002690 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2691
2692 /* validating srv_last_time_change */
2693 p = NULL;
2694 errno = 0;
2695 srv_last_time_change = strtol(params[5], &p, 10);
2696 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2697 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2698
2699 /* validating srv_check_status */
2700 p = NULL;
2701 errno = 0;
2702 srv_check_status = strtol(params[6], &p, 10);
2703 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2704 (srv_check_status >= HCHK_STATUS_SIZE))
2705 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2706
2707 /* validating srv_check_result */
2708 p = NULL;
2709 errno = 0;
2710 srv_check_result = strtol(params[7], &p, 10);
2711 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2712 (srv_check_result != CHK_RES_UNKNOWN &&
2713 srv_check_result != CHK_RES_NEUTRAL &&
2714 srv_check_result != CHK_RES_FAILED &&
2715 srv_check_result != CHK_RES_PASSED &&
2716 srv_check_result != CHK_RES_CONDPASS)) {
2717 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2718 }
2719
2720 /* validating srv_check_health */
2721 p = NULL;
2722 errno = 0;
2723 srv_check_health = strtol(params[8], &p, 10);
2724 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2725 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2726
2727 /* validating srv_check_state */
2728 p = NULL;
2729 errno = 0;
2730 srv_check_state = strtol(params[9], &p, 10);
2731 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2732 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2733 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2734
2735 /* validating srv_agent_state */
2736 p = NULL;
2737 errno = 0;
2738 srv_agent_state = strtol(params[10], &p, 10);
2739 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2740 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2741 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2742
2743 /* validating bk_f_forced_id */
2744 p = NULL;
2745 errno = 0;
2746 bk_f_forced_id = strtol(params[11], &p, 10);
2747 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2748 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2749
2750 /* validating srv_f_forced_id */
2751 p = NULL;
2752 errno = 0;
2753 srv_f_forced_id = strtol(params[12], &p, 10);
2754 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2755 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2756
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002757 /* validating srv_fqdn */
2758 fqdn = params[13];
2759 if (fqdn && *fqdn == '-')
2760 fqdn = NULL;
2761 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2762 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2763 fqdn = NULL;
2764 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002765
Frédéric Lécaille31694712017-08-01 08:47:19 +02002766 port_str = params[14];
2767 if (port_str) {
2768 port = strl2uic(port_str, strlen(port_str));
2769 if (port > USHRT_MAX) {
2770 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2771 port_str = NULL;
2772 }
2773 }
2774
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002775 /* SRV record
2776 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2777 */
2778 srvrecord = params[15];
2779 if (srvrecord && *srvrecord != '_')
2780 srvrecord = NULL;
2781
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002782 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002783 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002784 goto out;
2785
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002786 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787 /* recover operational state and apply it to this server
2788 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002789 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002790 switch (srv_op_state) {
2791 case SRV_ST_STOPPED:
2792 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002793 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002794 break;
2795 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002796 /* If rise == 1 there is no STARTING state, let's switch to
2797 * RUNNING
2798 */
2799 if (srv->check.rise == 1) {
2800 srv->check.health = srv->check.rise + srv->check.fall - 1;
2801 srv_set_running(srv, "", NULL);
2802 break;
2803 }
2804 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2805 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002806 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002807 break;
2808 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002809 /* If fall == 1 there is no STOPPING state, let's switch to
2810 * STOPPED
2811 */
2812 if (srv->check.fall == 1) {
2813 srv->check.health = 0;
2814 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2815 break;
2816 }
2817 if (srv->check.health < srv->check.rise ||
2818 srv->check.health > srv->check.rise + srv->check.fall - 2)
2819 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002820 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002821 break;
2822 case SRV_ST_RUNNING:
2823 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002824 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002825 break;
2826 }
2827
2828 /* When applying server state, the following rules apply:
2829 * - in case of a configuration change, we apply the setting from the new
2830 * configuration, regardless of old running state
2831 * - if no configuration change, we apply old running state only if old running
2832 * state is different from new configuration state
2833 */
2834 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002835 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2836 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002837 srv_adm_set_maint(srv);
2838 else
2839 srv_adm_set_ready(srv);
2840 }
2841 /* configuration is the same, let's compate old running state and new conf state */
2842 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002843 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002844 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002845 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002846 srv_adm_set_ready(srv);
2847 }
2848 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002849 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002850 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002851 * (srv->iweight is the weight set up in configuration).
2852 * There are two possible reasons for FDRAIN to have been present :
2853 * - previous config weight was zero
2854 * - "set server b/s drain" was sent to the CLI
2855 *
2856 * In the first case, we simply want to drop this drain state
2857 * if the new weight is not zero anymore, meaning the administrator
2858 * has intentionally turned the weight back to a positive value to
2859 * enable the server again after an operation. In the second case,
2860 * the drain state was forced on the CLI regardless of the config's
2861 * weight so we don't want a change to the config weight to lose this
2862 * status. What this means is :
2863 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2864 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002865 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002866 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002867 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002868 }
2869
2870 srv->last_change = date.tv_sec - srv_last_time_change;
2871 srv->check.status = srv_check_status;
2872 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002873
2874 /* Only case we want to apply is removing ENABLED flag which could have been
2875 * done by the "disable health" command over the stats socket
2876 */
2877 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2878 (srv_check_state & CHK_ST_CONFIGURED) &&
2879 !(srv_check_state & CHK_ST_ENABLED))
2880 srv->check.state &= ~CHK_ST_ENABLED;
2881
2882 /* Only case we want to apply is removing ENABLED flag which could have been
2883 * done by the "disable agent" command over the stats socket
2884 */
2885 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2886 (srv_agent_state & CHK_ST_CONFIGURED) &&
2887 !(srv_agent_state & CHK_ST_ENABLED))
2888 srv->agent.state &= ~CHK_ST_ENABLED;
2889
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002890 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2891 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002892 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002893 * It means that a configuration file change has precedence over a unix socket change
2894 * for server's weight
2895 *
2896 * by default, HAProxy applies the following weight when parsing the configuration
2897 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002898 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002899 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002900 srv->uweight = srv_uweight;
2901 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002902 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002903
Willy Tarreaue5a60682016-11-09 14:54:53 +01002904 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002905 if (strcmp(params[0], "-"))
2906 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002907
2908 if (fqdn && srv->hostname) {
2909 if (!strcmp(srv->hostname, fqdn)) {
2910 /* Here we reset the 'set from stats socket FQDN' flag
2911 * to support such transitions:
2912 * Let's say initial FQDN value is foo1 (in configuration file).
2913 * - FQDN changed from stats socket, from foo1 to foo2 value,
2914 * - FQDN changed again from file configuration (with the same previous value
2915 set from stats socket, from foo1 to foo2 value),
2916 * - reload for any other reason than a FQDN modification,
2917 * the configuration file FQDN matches the fqdn server state file value.
2918 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002919 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002920 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002921 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002922 }
2923 else {
2924 /* If the FDQN has been changed from stats socket,
2925 * apply fqdn state file value (which is the value set
2926 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002927 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002928 */
2929 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002930 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002931 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002932 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002933 }
2934 }
2935 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002936 /* If all the conditions below are validated, this means
2937 * we're evaluating a server managed by SRV resolution
2938 */
2939 else if (fqdn && !srv->hostname && srvrecord) {
2940 int res;
2941
2942 /* we can't apply previous state if SRV record has changed */
2943 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2944 chunk_appendf(msg, ", SRV record mismatch between configuration ('%s') and state file ('%s) for server '%s'. Previous state not applied", srv->srvrq->name, srvrecord, srv->id);
2945 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2946 goto out;
2947 }
2948
2949 /* create or find a SRV resolution for this srv record */
2950 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2951 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2952 if (srv->srvrq == NULL) {
2953 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2954 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2955 goto out;
2956 }
2957
2958 /* prepare DNS resolution for this server */
2959 res = srv_prepare_for_resolution(srv, fqdn);
2960 if (res == -1) {
2961 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2962 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2963 goto out;
2964 }
2965
2966 /* configure check.port accordingly */
2967 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2968 !(srv->flags & SRV_F_CHECKPORT))
2969 srv->check.port = port;
2970
2971 /* Unset SRV_F_MAPPORTS for SRV records.
2972 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2973 * because no ports are provided in the configuration file.
2974 * This is because HAProxy will use the port found into the SRV record.
2975 */
2976 srv->flags &= ~SRV_F_MAPPORTS;
2977 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002978
Frédéric Lécaille31694712017-08-01 08:47:19 +02002979 if (port_str)
2980 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002981 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002982
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002983 break;
2984 default:
2985 chunk_appendf(msg, ", version '%d' not supported", version);
2986 }
2987
2988 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002989 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002990 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002991 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002992 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002993 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002994}
2995
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002996
2997/*
2998 * read next line from file <f> and return the server state version if one found.
2999 * If no version is found, then 0 is returned
3000 * Note that this should be the first read on <f>
3001 */
3002static int srv_state_get_version(FILE *f) {
3003 char buf[2];
3004 int ret;
3005
3006 /* first character of first line of the file must contain the version of the export */
3007 if (fgets(buf, 2, f) == NULL) {
3008 return 0;
3009 }
3010
3011 ret = atoi(buf);
3012 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3013 (ret > SRV_STATE_FILE_VERSION_MAX))
3014 return 0;
3015
3016 return ret;
3017}
3018
3019
3020/*
3021 * parses server state line stored in <buf> and supposedly in version <version>.
3022 * Set <params> and <srv_params> accordingly.
3023 * In case of error, params[0] is set to NULL.
3024 */
3025static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3026{
3027 int buflen, arg, srv_arg;
3028 char *cur, *end;
3029
3030 buflen = strlen(buf);
3031 cur = buf;
3032 end = cur + buflen;
3033
3034 /* we need at least one character */
3035 if (buflen == 0) {
3036 params[0] = NULL;
3037 return;
3038 }
3039
3040 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003041 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003042 ++cur;
3043
3044 /* Ignore empty or commented lines */
3045 if (cur == end || *cur == '#') {
3046 params[0] = NULL;
3047 return;
3048 }
3049
3050 /* truncated lines */
3051 if (buf[buflen - 1] != '\n') {
3052 //ha_warning("server-state file '%s': truncated line\n", filepath);
3053 params[0] = NULL;
3054 return;
3055 }
3056
3057 /* Removes trailing '\n' */
3058 buf[buflen - 1] = '\0';
3059
3060 /* we're now ready to move the line into *srv_params[] */
3061 params[0] = cur;
3062 arg = 1;
3063 srv_arg = 0;
3064 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003065 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003066 *cur = '\0';
3067 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003068 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003069 ++cur;
3070 switch (version) {
3071 case 1:
3072 /*
3073 * srv_addr: params[4] => srv_params[0]
3074 * srv_op_state: params[5] => srv_params[1]
3075 * srv_admin_state: params[6] => srv_params[2]
3076 * srv_uweight: params[7] => srv_params[3]
3077 * srv_iweight: params[8] => srv_params[4]
3078 * srv_last_time_change: params[9] => srv_params[5]
3079 * srv_check_status: params[10] => srv_params[6]
3080 * srv_check_result: params[11] => srv_params[7]
3081 * srv_check_health: params[12] => srv_params[8]
3082 * srv_check_state: params[13] => srv_params[9]
3083 * srv_agent_state: params[14] => srv_params[10]
3084 * bk_f_forced_id: params[15] => srv_params[11]
3085 * srv_f_forced_id: params[16] => srv_params[12]
3086 * srv_fqdn: params[17] => srv_params[13]
3087 * srv_port: params[18] => srv_params[14]
3088 * srvrecord: params[19] => srv_params[15]
3089 */
3090 if (arg >= 4) {
3091 srv_params[srv_arg] = cur;
3092 ++srv_arg;
3093 }
3094 break;
3095 }
3096
3097 params[arg] = cur;
3098 ++arg;
3099 }
3100 else {
3101 ++cur;
3102 }
3103 }
3104
3105 /* if line is incomplete line, then ignore it.
3106 * otherwise, update useful flags */
3107 switch (version) {
3108 case 1:
3109 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3110 params[0] = NULL;
3111 return;
3112 }
3113 break;
3114 }
3115
3116 return;
3117}
3118
3119
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003120/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3121 * For each proxy, it does the following:
3122 * - opens its server state file (either one or local one)
3123 * - read whole file, line by line
3124 * - analyse each line to check if it matches our current backend:
3125 * - backend name matches
3126 * - backend id matches if id is forced and name doesn't match
3127 * - if the server pointed by the line is found, then state is applied
3128 *
3129 * If the running backend uuid or id differs from the state file, then HAProxy reports
3130 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003131 *
3132 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003133 */
3134void apply_server_state(void)
3135{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003136 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003137 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3138 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003139 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003140 FILE *f;
3141 char *filepath;
3142 char globalfilepath[MAXPATHLEN + 1];
3143 char localfilepath[MAXPATHLEN + 1];
3144 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003145 struct proxy *curproxy, *bk;
3146 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003147 char *line;
3148 char *bkname, *srvname;
3149 struct state_line *st;
3150 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003151
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003152
3153 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003154 globalfilepathlen = 0;
3155 /* create the globalfilepath variable */
3156 if (global.server_state_file) {
3157 /* absolute path or no base directory provided */
3158 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3159 len = strlen(global.server_state_file);
3160 if (len > MAXPATHLEN) {
3161 globalfilepathlen = 0;
3162 goto globalfileerror;
3163 }
3164 memcpy(globalfilepath, global.server_state_file, len);
3165 globalfilepath[len] = '\0';
3166 globalfilepathlen = len;
3167 }
3168 else if (global.server_state_base) {
3169 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003170 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003171 globalfilepathlen = 0;
3172 goto globalfileerror;
3173 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003174 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003175 globalfilepath[len] = 0;
3176 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003177
3178 /* append a slash if needed */
3179 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3180 if (globalfilepathlen + 1 > MAXPATHLEN) {
3181 globalfilepathlen = 0;
3182 goto globalfileerror;
3183 }
3184 globalfilepath[globalfilepathlen++] = '/';
3185 }
3186
3187 len = strlen(global.server_state_file);
3188 if (globalfilepathlen + len > MAXPATHLEN) {
3189 globalfilepathlen = 0;
3190 goto globalfileerror;
3191 }
3192 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3193 globalfilepathlen += len;
3194 globalfilepath[globalfilepathlen++] = 0;
3195 }
3196 }
3197 globalfileerror:
3198 if (globalfilepathlen == 0)
3199 globalfilepath[0] = '\0';
3200
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003201 /* Load global server state in a tree */
3202 if (globalfilepathlen > 0) {
3203 errno = 0;
3204 f = fopen(globalfilepath, "r");
3205 if (errno)
3206 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003207 if (!f)
3208 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003209
3210 global_file_version = srv_state_get_version(f);
3211 if (global_file_version == 0)
3212 goto out_load_server_state_in_tree;
3213
3214 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3215 line = NULL;
3216
3217 line = strdup(mybuf);
3218 if (line == NULL)
3219 continue;
3220
3221 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3222 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003223 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003224
3225 /* bkname */
3226 bkname = params[1];
3227 /* srvname */
3228 srvname = params[3];
3229
3230 /* key */
3231 chunk_printf(&trash, "%s %s", bkname, srvname);
3232
3233 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003234 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003235 if (st == NULL) {
3236 goto nextline;
3237 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003238 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003239 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3240 /* this is a duplicate key, probably a hand-crafted file,
3241 * drop it!
3242 */
3243 goto nextline;
3244 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003245
3246 /* save line */
3247 st->line = line;
3248
3249 continue;
3250
3251 nextline:
3252 /* free up memory in case of error during the processing of the line */
3253 free(line);
3254 }
3255 }
3256 out_load_server_state_in_tree:
3257
3258 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003259 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003260 /* servers are only in backends */
3261 if (!(curproxy->cap & PR_CAP_BE))
3262 continue;
3263 fileopenerr = 0;
3264 filepath = NULL;
3265
3266 /* search server state file path and name */
3267 switch (curproxy->load_server_state_from_file) {
3268 /* read servers state from global file */
3269 case PR_SRV_STATE_FILE_GLOBAL:
3270 /* there was an error while generating global server state file path */
3271 if (globalfilepathlen == 0)
3272 continue;
3273 filepath = globalfilepath;
3274 fileopenerr = 1;
3275 break;
3276 /* this backend has its own file */
3277 case PR_SRV_STATE_FILE_LOCAL:
3278 localfilepathlen = 0;
3279 localfilepath[0] = '\0';
3280 len = 0;
3281 /* create the localfilepath variable */
3282 /* absolute path or no base directory provided */
3283 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3284 len = strlen(curproxy->server_state_file_name);
3285 if (len > MAXPATHLEN) {
3286 localfilepathlen = 0;
3287 goto localfileerror;
3288 }
3289 memcpy(localfilepath, curproxy->server_state_file_name, len);
3290 localfilepath[len] = '\0';
3291 localfilepathlen = len;
3292 }
3293 else if (global.server_state_base) {
3294 len = strlen(global.server_state_base);
3295 localfilepathlen += len;
3296
3297 if (localfilepathlen > MAXPATHLEN) {
3298 localfilepathlen = 0;
3299 goto localfileerror;
3300 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003301 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003302 localfilepath[localfilepathlen] = 0;
3303
3304 /* append a slash if needed */
3305 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3306 if (localfilepathlen + 1 > MAXPATHLEN) {
3307 localfilepathlen = 0;
3308 goto localfileerror;
3309 }
3310 localfilepath[localfilepathlen++] = '/';
3311 }
3312
3313 len = strlen(curproxy->server_state_file_name);
3314 if (localfilepathlen + len > MAXPATHLEN) {
3315 localfilepathlen = 0;
3316 goto localfileerror;
3317 }
3318 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3319 localfilepathlen += len;
3320 localfilepath[localfilepathlen++] = 0;
3321 }
3322 filepath = localfilepath;
3323 localfileerror:
3324 if (localfilepathlen == 0)
3325 localfilepath[0] = '\0';
3326
3327 break;
3328 case PR_SRV_STATE_FILE_NONE:
3329 default:
3330 continue;
3331 }
3332
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003333 /* when global file is used, we get data from the tree
3334 * Note that in such case we don't check backend name neither uuid.
3335 * Backend name can't be wrong since it's used as a key to retrieve the server state
3336 * line from the tree.
3337 */
3338 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3339 struct server *srv = curproxy->srv;
3340 while (srv) {
3341 struct ebmb_node *node;
3342 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003343
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003344 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3345 node = ebst_lookup(&state_file, trash.area);
3346 if (!node)
3347 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003348
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003349 st = container_of(node, struct state_line, name_name);
3350 memcpy(mybuf, st->line, strlen(st->line));
3351 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003352
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003353 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3354 if (params[0] == NULL)
3355 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003356
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003357 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003358
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003359 next:
3360 srv = srv->next;
3361 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003362
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003363 continue; /* next proxy in list */
3364 }
3365 else {
3366 /* load 'local' state file */
3367 errno = 0;
3368 f = fopen(filepath, "r");
3369 if (errno && fileopenerr)
3370 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3371 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003372 continue;
3373
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003374 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003375
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003376 /* first character of first line of the file must contain the version of the export */
3377 version = srv_state_get_version(f);
3378 if (version == 0) {
3379 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3380 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003381 }
3382
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003383 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3384 int bk_f_forced_id = 0;
3385 int check_id = 0;
3386 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003387
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003388 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003389
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003390 if (params[0] == NULL) {
3391 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003392 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003393
3394 /* if line is incomplete line, then ignore it.
3395 * otherwise, update useful flags */
3396 switch (version) {
3397 case 1:
3398 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3399 check_id = (atoi(params[0]) == curproxy->uuid);
3400 check_name = (strcmp(curproxy->id, params[1]) == 0);
3401 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003402 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003403
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003404 bk = curproxy;
3405
3406 /* if backend can't be found, let's continue */
3407 if (!check_id && !check_name)
3408 continue;
3409 else if (!check_id && check_name) {
3410 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3411 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3412 }
3413 else if (check_id && !check_name) {
3414 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3415 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3416 /* if name doesn't match, we still want to update curproxy if the backend id
3417 * was forced in previous the previous configuration */
3418 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003419 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003420 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003421
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003422 /* look for the server by its name: param[3] */
3423 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003424
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003425 if (!srv) {
3426 /* if no server found, then warning and continue with next line */
3427 ha_warning("can't find server '%s' in backend '%s'\n",
3428 params[3], params[1]);
3429 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3430 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003431 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003432 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003433
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003434 /* now we can proceed with server's state update */
3435 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003436 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003437 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003438fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003439 fclose(f);
3440 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003441
3442 /* now free memory allocated for the tree */
3443 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3444 node;
3445 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3446 st = container_of(node, struct state_line, name_name);
3447 ebmb_delete(&st->name_name);
3448 free(st->line);
3449 free(st);
3450 }
3451
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003452}
3453
Simon Horman7d09b9a2013-02-12 10:45:51 +09003454/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003455 * update a server's current IP address.
3456 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3457 * ip is in network format.
3458 * updater is a string which contains an information about the requester of the update.
3459 * updater is used if not NULL.
3460 *
3461 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003462 *
3463 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003464 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003465int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003466{
3467 /* generates a log line and a warning on stderr */
3468 if (1) {
3469 /* book enough space for both IPv4 and IPv6 */
3470 char oldip[INET6_ADDRSTRLEN];
3471 char newip[INET6_ADDRSTRLEN];
3472
3473 memset(oldip, '\0', INET6_ADDRSTRLEN);
3474 memset(newip, '\0', INET6_ADDRSTRLEN);
3475
3476 /* copy old IP address in a string */
3477 switch (s->addr.ss_family) {
3478 case AF_INET:
3479 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3480 break;
3481 case AF_INET6:
3482 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3483 break;
3484 };
3485
3486 /* copy new IP address in a string */
3487 switch (ip_sin_family) {
3488 case AF_INET:
3489 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3490 break;
3491 case AF_INET6:
3492 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3493 break;
3494 };
3495
3496 /* save log line into a buffer */
3497 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3498 s->proxy->id, s->id, oldip, newip, updater);
3499
3500 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003501 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003502
3503 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003504 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003505 }
3506
3507 /* save the new IP family */
3508 s->addr.ss_family = ip_sin_family;
3509 /* save the new IP address */
3510 switch (ip_sin_family) {
3511 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003512 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003513 break;
3514 case AF_INET6:
3515 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3516 break;
3517 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003518 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003519
3520 return 0;
3521}
3522
3523/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003524 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3525 *
3526 * Caller can pass its name through <updater> to get it integrated in the response message
3527 * returned by the function.
3528 *
3529 * The function first does the following, in that order:
3530 * - validates the new addr and/or port
3531 * - checks if an update is required (new IP or port is different than current ones)
3532 * - checks the update is allowed:
3533 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3534 * - allow all changes if no CHECKS are configured
3535 * - if CHECK is configured:
3536 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3537 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3538 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003539 *
3540 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003541 */
3542const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3543{
3544 struct sockaddr_storage sa;
3545 int ret, port_change_required;
3546 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003547 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003548 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003549 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003550
3551 msg = get_trash_chunk();
3552 chunk_reset(msg);
3553
3554 if (addr) {
3555 memset(&sa, 0, sizeof(struct sockaddr_storage));
3556 if (str2ip2(addr, &sa, 0) == NULL) {
3557 chunk_printf(msg, "Invalid addr '%s'", addr);
3558 goto out;
3559 }
3560
3561 /* changes are allowed on AF_INET* families only */
3562 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3563 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3564 goto out;
3565 }
3566
3567 /* collecting data currently setup */
3568 memset(current_addr, '\0', sizeof(current_addr));
3569 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3570 /* changes are allowed on AF_INET* families only */
3571 if ((ret != AF_INET) && (ret != AF_INET6)) {
3572 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3573 goto out;
3574 }
3575
3576 /* applying ADDR changes if required and allowed
3577 * ipcmp returns 0 when both ADDR are the same
3578 */
3579 if (ipcmp(&s->addr, &sa) == 0) {
3580 chunk_appendf(msg, "no need to change the addr");
3581 goto port;
3582 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003583 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003584 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003585
3586 /* we also need to update check's ADDR only if it uses the server's one */
3587 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003588 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003589 }
3590
3591 /* we also need to update agent ADDR only if it use the server's one */
3592 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003593 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003594 }
3595
3596 /* update report for caller */
3597 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3598 }
3599
3600 port:
3601 if (port) {
3602 char sign = '\0';
3603 char *endptr;
3604
3605 if (addr)
3606 chunk_appendf(msg, ", ");
3607
3608 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003609 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003610
3611 /* check if PORT change is required */
3612 port_change_required = 0;
3613
3614 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003615 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003616 new_port = strtol(port, &endptr, 10);
3617 if ((errno != 0) || (port == endptr)) {
3618 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3619 goto out;
3620 }
3621
3622 /* check if caller triggers a port mapped or offset */
3623 if (sign == '-' || (sign == '+')) {
3624 /* check if server currently uses port map */
3625 if (!(s->flags & SRV_F_MAPPORTS)) {
3626 /* switch from fixed port to port map mandatorily triggers
3627 * a port change */
3628 port_change_required = 1;
3629 /* check is configured
3630 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3631 * prevent PORT change if check doesn't have it's dedicated port while switching
3632 * to port mapping */
3633 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3634 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.");
3635 goto out;
3636 }
3637 }
3638 /* we're already using port maps */
3639 else {
3640 port_change_required = current_port != new_port;
3641 }
3642 }
3643 /* fixed port */
3644 else {
3645 port_change_required = current_port != new_port;
3646 }
3647
3648 /* applying PORT changes if required and update response message */
3649 if (port_change_required) {
3650 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003651 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003652 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003653
3654 /* prepare message */
3655 chunk_appendf(msg, "port changed from '");
3656 if (s->flags & SRV_F_MAPPORTS)
3657 chunk_appendf(msg, "+");
3658 chunk_appendf(msg, "%d' to '", current_port);
3659
3660 if (sign == '-') {
3661 s->flags |= SRV_F_MAPPORTS;
3662 chunk_appendf(msg, "%c", sign);
3663 /* just use for result output */
3664 new_port = -new_port;
3665 }
3666 else if (sign == '+') {
3667 s->flags |= SRV_F_MAPPORTS;
3668 chunk_appendf(msg, "%c", sign);
3669 }
3670 else {
3671 s->flags &= ~SRV_F_MAPPORTS;
3672 }
3673
3674 chunk_appendf(msg, "%d'", new_port);
3675
3676 /* we also need to update health checks port only if it uses server's realport */
3677 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3678 s->check.port = new_port;
3679 }
3680 }
3681 else {
3682 chunk_appendf(msg, "no need to change the port");
3683 }
3684 }
3685
3686out:
William Dauchy6318d332020-05-02 21:52:36 +02003687 if (changed) {
3688 /* force connection cleanup on the given server */
3689 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003690 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003691 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003692 if (updater)
3693 chunk_appendf(msg, " by '%s'", updater);
3694 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003695 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003696}
3697
3698
3699/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003700 * update server status based on result of name resolution
3701 * returns:
3702 * 0 if server status is updated
3703 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003704 *
3705 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003706 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003707int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003708{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003709 struct dns_resolvers *resolvers = s->resolvers;
3710 struct dns_resolution *resolution = s->dns_requester->resolution;
3711 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003712
3713 switch (resolution->status) {
3714 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003715 /* status when HAProxy has just (re)started.
3716 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003717 break;
3718
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003719 case RSLV_STATUS_VALID:
3720 /*
3721 * resume health checks
3722 * server will be turned back on if health check is safe
3723 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003724 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003725 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003726 return 1;
3727 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3728 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003729 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003730 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003731
Emeric Brun52a91d32017-08-31 14:41:55 +02003732 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003733 return 1;
3734 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3735 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3736 s->proxy->id, s->id);
3737
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003738 ha_warning("%s.\n", trash.area);
3739 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003740 return 0;
3741
3742 case RSLV_STATUS_NX:
3743 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003744 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3745 if (!tick_is_expired(exp, now_ms))
3746 break;
3747
3748 if (s->next_admin & SRV_ADMF_RMAINT)
3749 return 1;
3750 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3751 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003752
3753 case RSLV_STATUS_TIMEOUT:
3754 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003755 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3756 if (!tick_is_expired(exp, now_ms))
3757 break;
3758
3759 if (s->next_admin & SRV_ADMF_RMAINT)
3760 return 1;
3761 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3762 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003763
3764 case RSLV_STATUS_REFUSED:
3765 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003766 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3767 if (!tick_is_expired(exp, now_ms))
3768 break;
3769
3770 if (s->next_admin & SRV_ADMF_RMAINT)
3771 return 1;
3772 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3773 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003774
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003775 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003776 /* stop server if resolution failed for a long enough period */
3777 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3778 if (!tick_is_expired(exp, now_ms))
3779 break;
3780
3781 if (s->next_admin & SRV_ADMF_RMAINT)
3782 return 1;
3783 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3784 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003785 }
3786
3787 return 1;
3788}
3789
3790/*
3791 * Server Name Resolution valid response callback
3792 * It expects:
3793 * - <nameserver>: the name server which answered the valid response
3794 * - <response>: buffer containing a valid DNS response
3795 * - <response_len>: size of <response>
3796 * It performs the following actions:
3797 * - ignore response if current ip found and server family not met
3798 * - update with first new ip found if family is met and current IP is not found
3799 * returns:
3800 * 0 on error
3801 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003802 *
3803 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003804 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003805int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003806{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003807 struct server *s = NULL;
3808 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003809 void *serverip, *firstip;
3810 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003811 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003812 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003813 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003814
Christopher Faulet67957bd2017-09-27 11:00:59 +02003815 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003816 if (!s)
3817 return 1;
3818
Christopher Faulet67957bd2017-09-27 11:00:59 +02003819 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003820
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003821 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003822 firstip = NULL; /* pointer to the first valid response found */
3823 /* it will be used as the new IP if a change is required */
3824 firstip_sin_family = AF_UNSPEC;
3825 serverip = NULL; /* current server IP address */
3826
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003827 /* initializing server IP pointer */
3828 server_sin_family = s->addr.ss_family;
3829 switch (server_sin_family) {
3830 case AF_INET:
3831 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3832 break;
3833
3834 case AF_INET6:
3835 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3836 break;
3837
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003838 case AF_UNSPEC:
3839 break;
3840
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003841 default:
3842 goto invalid;
3843 }
3844
Baptiste Assmann729c9012017-05-22 15:13:10 +02003845 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003846 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003847 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003848
3849 switch (ret) {
3850 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003851 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003852
3853 case DNS_UPD_SRVIP_NOT_FOUND:
3854 goto save_ip;
3855
3856 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003857 goto invalid;
3858
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003859 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003860 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003861 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003862
Baptiste Assmannfad03182015-10-28 02:03:32 +01003863 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003864 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003865 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003866 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003867
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003868 default:
3869 goto invalid;
3870
3871 }
3872
3873 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003874 if (nameserver) {
3875 nameserver->counters.update++;
3876 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003877 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003878 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003879 else
3880 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003881 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003882
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003883 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003884 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003886
3887 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003888 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003889 nameserver->counters.invalid++;
3890 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003891 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003892 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003893 return 0;
3894}
3895
3896/*
3897 * Server Name Resolution error management callback
3898 * returns:
3899 * 0 on error
3900 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003901 *
3902 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003903 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003904int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003905{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003906 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907
Christopher Faulet67957bd2017-09-27 11:00:59 +02003908 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003909 if (!s)
3910 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003911 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003912 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003913 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003914 return 1;
3915}
3916
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003917/*
3918 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003919 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003920 * It returns a pointer to the first server found or NULL if <ip> is not yet
3921 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003922 *
3923 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003924 */
3925struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3926{
3927 struct server *tmpsrv;
3928 struct proxy *be;
3929
3930 if (!srv)
3931 return NULL;
3932
3933 be = srv->proxy;
3934 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003935 /* we found the current server is the same, ignore it */
3936 if (srv == tmpsrv)
3937 continue;
3938
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003939 /* We want to compare the IP in the record with the IP of the servers in the
3940 * same backend, only if:
3941 * * DNS resolution is enabled on the server
3942 * * the hostname used for the resolution by our server is the same than the
3943 * one used for the server found in the backend
3944 * * the server found in the backend is not our current server
3945 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003946 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003947 if ((tmpsrv->hostname_dn == NULL) ||
3948 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3949 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003950 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003951 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003952 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003953 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003954
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003955 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003956 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003957 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003958 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003959 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003960
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003961 /* At this point, we have 2 different servers using the same DNS hostname
3962 * for their respective resolution.
3963 */
3964 if (*ip_family == tmpsrv->addr.ss_family &&
3965 ((tmpsrv->addr.ss_family == AF_INET &&
3966 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3967 (tmpsrv->addr.ss_family == AF_INET6 &&
3968 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003969 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003970 return tmpsrv;
3971 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003972 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003973 }
3974
Emeric Brune9fd6b52017-11-02 17:20:39 +01003975
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003976 return NULL;
3977}
3978
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003979/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3980 * resolver. This is suited for initial address configuration. Returns 0 on
3981 * success otherwise a non-zero error code. In case of error, *err_code, if
3982 * not NULL, is filled up.
3983 */
3984int srv_set_addr_via_libc(struct server *srv, int *err_code)
3985{
3986 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003987 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003988 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003989 return 1;
3990 }
3991 return 0;
3992}
3993
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003994/* Set the server's FDQN (->hostname) from <hostname>.
3995 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003996 *
3997 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003998 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003999int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004000{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004001 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004002 char *hostname_dn;
4003 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004004
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004005 /* Note that the server lock is already held. */
4006 if (!srv->resolvers)
4007 return -1;
4008
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004009 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004010 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004011 /* run time DNS resolution was not active for this server
4012 * and we can't enable it at run time for now.
4013 */
4014 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004015 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004016
4017 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004018 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004019 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004020 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4021 hostname_dn, trash.size);
4022 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004023 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004024
Christopher Faulet67957bd2017-09-27 11:00:59 +02004025 resolution = srv->dns_requester->resolution;
4026 if (resolution &&
4027 resolution->hostname_dn &&
4028 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004029 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004030
Christopher Faulet67957bd2017-09-27 11:00:59 +02004031 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004032
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004033 free(srv->hostname);
4034 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004035 srv->hostname = strdup(hostname);
4036 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004037 srv->hostname_dn_len = hostname_dn_len;
4038 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004039 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004040
Baptiste Assmann13a92322019-06-07 09:40:55 +02004041 if (srv->flags & SRV_F_NO_RESOLUTION)
4042 goto end;
4043
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004044 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004045 goto err;
4046
4047 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004048 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004049 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004050 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004051
4052 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004053 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004054 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004055 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004056}
4057
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004058/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4059 * from the state file. This is suited for initial address configuration.
4060 * Returns 0 on success otherwise a non-zero error code. In case of error,
4061 * *err_code, if not NULL, is filled up.
4062 */
4063static int srv_apply_lastaddr(struct server *srv, int *err_code)
4064{
4065 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4066 if (err_code)
4067 *err_code |= ERR_WARN;
4068 return 1;
4069 }
4070 return 0;
4071}
4072
Willy Tarreau25e51522016-11-04 15:10:17 +01004073/* returns 0 if no error, otherwise a combination of ERR_* flags */
4074static int srv_iterate_initaddr(struct server *srv)
4075{
4076 int return_code = 0;
4077 int err_code;
4078 unsigned int methods;
4079
4080 methods = srv->init_addr_methods;
4081 if (!methods) { // default to "last,libc"
4082 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4083 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4084 }
4085
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004086 /* "-dr" : always append "none" so that server addresses resolution
4087 * failures are silently ignored, this is convenient to validate some
4088 * configs out of their environment.
4089 */
4090 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4091 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4092
Willy Tarreau25e51522016-11-04 15:10:17 +01004093 while (methods) {
4094 err_code = 0;
4095 switch (srv_get_next_initaddr(&methods)) {
4096 case SRV_IADDR_LAST:
4097 if (!srv->lastaddr)
4098 continue;
4099 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004100 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004101 return_code |= err_code;
4102 break;
4103
4104 case SRV_IADDR_LIBC:
4105 if (!srv->hostname)
4106 continue;
4107 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004108 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004109 return_code |= err_code;
4110 break;
4111
Willy Tarreau37ebe122016-11-04 15:17:58 +01004112 case SRV_IADDR_NONE:
4113 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004114 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004115 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4116 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004117 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004118 return return_code;
4119
Willy Tarreau4310d362016-11-02 15:05:56 +01004120 case SRV_IADDR_IP:
4121 ipcpy(&srv->init_addr, &srv->addr);
4122 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004123 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4124 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004125 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004126 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004127
Willy Tarreau25e51522016-11-04 15:10:17 +01004128 default: /* unhandled method */
4129 break;
4130 }
4131 }
4132
4133 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004134 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004135 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4136 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004137 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004138 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004139 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4140 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004141
4142 return_code |= ERR_ALERT | ERR_FATAL;
4143 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004144out:
4145 srv_set_dyncookie(srv);
4146 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004147}
4148
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004149/*
4150 * This function parses all backends and all servers within each backend
4151 * and performs servers' addr resolution based on information provided by:
4152 * - configuration file
4153 * - server-state file (states provided by an 'old' haproxy process)
4154 *
4155 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4156 */
4157int srv_init_addr(void)
4158{
4159 struct proxy *curproxy;
4160 int return_code = 0;
4161
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004162 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004163 while (curproxy) {
4164 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004165
4166 /* servers are in backend only */
4167 if (!(curproxy->cap & PR_CAP_BE))
4168 goto srv_init_addr_next;
4169
Willy Tarreau25e51522016-11-04 15:10:17 +01004170 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004171 if (srv->hostname)
4172 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004173
4174 srv_init_addr_next:
4175 curproxy = curproxy->next;
4176 }
4177
4178 return return_code;
4179}
4180
Willy Tarreau46b7f532018-08-21 11:54:26 +02004181/*
4182 * Must be called with the server lock held.
4183 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004184const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004185{
4186
Willy Tarreau83061a82018-07-13 11:56:34 +02004187 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004188
4189 msg = get_trash_chunk();
4190 chunk_reset(msg);
4191
Olivier Houchard8da5f982017-08-04 18:35:36 +02004192 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004193 chunk_appendf(msg, "no need to change the FDQN");
4194 goto out;
4195 }
4196
4197 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4198 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4199 goto out;
4200 }
4201
4202 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4203 server->proxy->id, server->id, server->hostname, fqdn);
4204
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004205 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004206 chunk_reset(msg);
4207 chunk_appendf(msg, "could not update %s/%s FQDN",
4208 server->proxy->id, server->id);
4209 goto out;
4210 }
4211
4212 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004213 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004214
4215 out:
4216 if (updater)
4217 chunk_appendf(msg, " by '%s'", updater);
4218 chunk_appendf(msg, "\n");
4219
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004220 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004221}
4222
4223
Willy Tarreau21b069d2016-11-23 17:15:08 +01004224/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4225 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004226 * 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 +01004227 * used for CLI commands requiring a server name.
4228 * Important: the <arg> is modified to remove the '/'.
4229 */
4230struct server *cli_find_server(struct appctx *appctx, char *arg)
4231{
4232 struct proxy *px;
4233 struct server *sv;
4234 char *line;
4235
4236 /* split "backend/server" and make <line> point to server */
4237 for (line = arg; *line; line++)
4238 if (*line == '/') {
4239 *line++ = '\0';
4240 break;
4241 }
4242
4243 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004244 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004245 return NULL;
4246 }
4247
4248 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004249 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004250 return NULL;
4251 }
4252
4253 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004254 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004255 return NULL;
4256 }
4257
4258 return sv;
4259}
4260
William Lallemand222baf22016-11-19 02:00:33 +01004261
Willy Tarreau46b7f532018-08-21 11:54:26 +02004262/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004263static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004264{
4265 struct server *sv;
4266 const char *warning;
4267
4268 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4269 return 1;
4270
4271 sv = cli_find_server(appctx, args[2]);
4272 if (!sv)
4273 return 1;
4274
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004275 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004276
William Lallemand222baf22016-11-19 02:00:33 +01004277 if (strcmp(args[3], "weight") == 0) {
4278 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004279 if (warning)
4280 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004281 }
4282 else if (strcmp(args[3], "state") == 0) {
4283 if (strcmp(args[4], "ready") == 0)
4284 srv_adm_set_ready(sv);
4285 else if (strcmp(args[4], "drain") == 0)
4286 srv_adm_set_drain(sv);
4287 else if (strcmp(args[4], "maint") == 0)
4288 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004289 else
4290 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004291 }
4292 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004293 if (sv->track)
4294 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004295 else if (strcmp(args[4], "up") == 0) {
4296 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004297 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004298 }
4299 else if (strcmp(args[4], "stopping") == 0) {
4300 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004301 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004302 }
4303 else if (strcmp(args[4], "down") == 0) {
4304 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004305 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004306 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004307 else
4308 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004309 }
4310 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004311 if (!(sv->agent.state & CHK_ST_ENABLED))
4312 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004313 else if (strcmp(args[4], "up") == 0) {
4314 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004315 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004316 }
4317 else if (strcmp(args[4], "down") == 0) {
4318 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004319 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004320 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004321 else
4322 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004323 }
Misiek2da082d2017-01-09 09:40:42 +01004324 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004325 if (!(sv->agent.state & CHK_ST_ENABLED))
4326 cli_err(appctx, "agent checks are not enabled on this server.\n");
4327 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4328 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004329 }
4330 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004331 if (!(sv->agent.state & CHK_ST_ENABLED))
4332 cli_err(appctx, "agent checks are not enabled on this server.\n");
4333 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004334 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004335 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004336 }
4337 }
William Lallemand222baf22016-11-19 02:00:33 +01004338 else if (strcmp(args[3], "check-port") == 0) {
4339 int i = 0;
4340 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004341 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004342 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004343 }
4344 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004345 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004346 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004347 }
4348 /* prevent the update of port to 0 if MAPPORTS are in use */
4349 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004350 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004351 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004352 }
4353 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004354 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004355 }
4356 else if (strcmp(args[3], "addr") == 0) {
4357 char *addr = NULL;
4358 char *port = NULL;
4359 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004360 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004361 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004362 }
4363 else {
4364 addr = args[4];
4365 }
4366 if (strcmp(args[5], "port") == 0) {
4367 port = args[6];
4368 }
4369 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004370 if (warning)
4371 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004372 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4373 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004374 else if (strcmp(args[3], "fqdn") == 0) {
4375 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004376 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004377 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004378 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004379 /* ensure runtime resolver will process this new fqdn */
4380 if (sv->flags & SRV_F_NO_RESOLUTION) {
4381 sv->flags &= ~SRV_F_NO_RESOLUTION;
4382 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004383 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004384 if (warning)
4385 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004386 }
William Lallemand222baf22016-11-19 02:00:33 +01004387 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004388 cli_err(appctx,
4389 "'set server <srv>' only supports 'agent', 'health', 'state',"
4390 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004391 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004392 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004393 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004394 return 1;
4395}
4396
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004397static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004398{
4399 struct stream_interface *si = appctx->owner;
4400 struct proxy *px;
4401 struct server *sv;
4402 char *line;
4403
4404
4405 /* split "backend/server" and make <line> point to server */
4406 for (line = args[2]; *line; line++)
4407 if (*line == '/') {
4408 *line++ = '\0';
4409 break;
4410 }
4411
Willy Tarreau9d008692019-08-09 11:21:01 +02004412 if (!*line)
4413 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004414
Willy Tarreau9d008692019-08-09 11:21:01 +02004415 if (!get_backend_server(args[2], line, &px, &sv))
4416 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004417
4418 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004419 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4420 sv->iweight);
4421 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004422 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004423 return 0;
4424 }
William Lallemand6b160942016-11-22 12:34:35 +01004425 return 1;
4426}
4427
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004428/* Parse a "set weight" command.
4429 *
4430 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004431 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004432static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004433{
4434 struct server *sv;
4435 const char *warning;
4436
4437 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4438 return 1;
4439
4440 sv = cli_find_server(appctx, args[2]);
4441 if (!sv)
4442 return 1;
4443
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004444 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4445
William Lallemand6b160942016-11-22 12:34:35 +01004446 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004447 if (warning)
4448 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004449
4450 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4451
William Lallemand6b160942016-11-22 12:34:35 +01004452 return 1;
4453}
4454
Willy Tarreau46b7f532018-08-21 11:54:26 +02004455/* parse a "set maxconn server" command. It always returns 1.
4456 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004457 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004458 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004459static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004460{
4461 struct server *sv;
4462 const char *warning;
4463
4464 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4465 return 1;
4466
4467 sv = cli_find_server(appctx, args[3]);
4468 if (!sv)
4469 return 1;
4470
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004471 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4472
Willy Tarreaub8026272016-11-23 11:26:56 +01004473 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004474 if (warning)
4475 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004476
4477 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4478
Willy Tarreaub8026272016-11-23 11:26:56 +01004479 return 1;
4480}
William Lallemand6b160942016-11-22 12:34:35 +01004481
Willy Tarreau46b7f532018-08-21 11:54:26 +02004482/* parse a "disable agent" command. It always returns 1.
4483 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004484 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004485 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004486static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004487{
4488 struct server *sv;
4489
4490 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4491 return 1;
4492
4493 sv = cli_find_server(appctx, args[2]);
4494 if (!sv)
4495 return 1;
4496
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004497 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004498 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004499 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004500 return 1;
4501}
4502
Willy Tarreau46b7f532018-08-21 11:54:26 +02004503/* parse a "disable health" command. It always returns 1.
4504 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004505 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004506 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004507static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004508{
4509 struct server *sv;
4510
4511 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4512 return 1;
4513
4514 sv = cli_find_server(appctx, args[2]);
4515 if (!sv)
4516 return 1;
4517
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004518 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004519 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004520 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004521 return 1;
4522}
4523
Willy Tarreau46b7f532018-08-21 11:54:26 +02004524/* parse a "disable server" command. It always returns 1.
4525 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004526 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004527 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004528static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004529{
4530 struct server *sv;
4531
4532 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4533 return 1;
4534
4535 sv = cli_find_server(appctx, args[2]);
4536 if (!sv)
4537 return 1;
4538
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004539 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004540 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004541 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004542 return 1;
4543}
4544
Willy Tarreau46b7f532018-08-21 11:54:26 +02004545/* parse a "enable agent" command. It always returns 1.
4546 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004547 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004548 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004549static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004550{
4551 struct server *sv;
4552
4553 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4554 return 1;
4555
4556 sv = cli_find_server(appctx, args[2]);
4557 if (!sv)
4558 return 1;
4559
Willy Tarreau9d008692019-08-09 11:21:01 +02004560 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4561 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004562
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004563 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004564 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004565 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004566 return 1;
4567}
4568
Willy Tarreau46b7f532018-08-21 11:54:26 +02004569/* parse a "enable health" command. It always returns 1.
4570 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004571 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004572 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004573static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004574{
4575 struct server *sv;
4576
4577 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4578 return 1;
4579
4580 sv = cli_find_server(appctx, args[2]);
4581 if (!sv)
4582 return 1;
4583
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004584 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004585 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004586 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004587 return 1;
4588}
4589
Willy Tarreau46b7f532018-08-21 11:54:26 +02004590/* parse a "enable server" command. It always returns 1.
4591 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004592 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004593 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004594static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004595{
4596 struct server *sv;
4597
4598 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4599 return 1;
4600
4601 sv = cli_find_server(appctx, args[2]);
4602 if (!sv)
4603 return 1;
4604
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004605 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004606 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004607 if (!(sv->flags & SRV_F_COOKIESET)
4608 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4609 sv->cookie)
4610 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004611 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004612 return 1;
4613}
4614
William Lallemand222baf22016-11-19 02:00:33 +01004615/* register cli keywords */
4616static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004617 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004618 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004619 { { "disable", "server", NULL }, "disable server : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL },
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004620 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004621 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004622 { { "enable", "server", NULL }, "enable server : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
Willy Tarreaub8026272016-11-23 11:26:56 +01004623 { { "set", "maxconn", "server", NULL }, "set maxconn server : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01004624 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004625 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4626 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4627
William Lallemand222baf22016-11-19 02:00:33 +01004628 {{},}
4629}};
4630
Willy Tarreau0108d902018-11-25 19:14:37 +01004631INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004632
Emeric Brun64cc49c2017-10-03 14:46:45 +02004633/*
4634 * This function applies server's status changes, it is
4635 * is designed to be called asynchronously.
4636 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004637 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004638 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004639static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004640{
4641 struct check *check = &s->check;
4642 int xferred;
4643 struct proxy *px = s->proxy;
4644 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4645 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4646 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004647 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004648
Emeric Brun64cc49c2017-10-03 14:46:45 +02004649 /* If currently main is not set we try to apply pending state changes */
4650 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4651 int next_admin;
4652
4653 /* Backup next admin */
4654 next_admin = s->next_admin;
4655
4656 /* restore current admin state */
4657 s->next_admin = s->cur_admin;
4658
4659 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4660 s->last_change = now.tv_sec;
4661 if (s->proxy->lbprm.set_server_status_down)
4662 s->proxy->lbprm.set_server_status_down(s);
4663
4664 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4665 srv_shutdown_streams(s, SF_ERR_DOWN);
4666
4667 /* we might have streams queued on this server and waiting for
4668 * a connection. Those which are redispatchable will be queued
4669 * to another server or to the proxy itself.
4670 */
4671 xferred = pendconn_redistribute(s);
4672
4673 tmptrash = alloc_trash_chunk();
4674 if (tmptrash) {
4675 chunk_printf(tmptrash,
4676 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4677 s->proxy->id, s->id);
4678
Emeric Brun5a133512017-10-19 14:42:30 +02004679 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004680 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004681
4682 /* we don't send an alert if the server was previously paused */
4683 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004684 send_log(s->proxy, log_level, "%s.\n",
4685 tmptrash->area);
4686 send_email_alert(s, log_level, "%s",
4687 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004688 free_trash_chunk(tmptrash);
4689 tmptrash = NULL;
4690 }
4691 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4692 set_backend_down(s->proxy);
4693
4694 s->counters.down_trans++;
4695 }
4696 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4697 s->last_change = now.tv_sec;
4698 if (s->proxy->lbprm.set_server_status_down)
4699 s->proxy->lbprm.set_server_status_down(s);
4700
4701 /* we might have streams queued on this server and waiting for
4702 * a connection. Those which are redispatchable will be queued
4703 * to another server or to the proxy itself.
4704 */
4705 xferred = pendconn_redistribute(s);
4706
4707 tmptrash = alloc_trash_chunk();
4708 if (tmptrash) {
4709 chunk_printf(tmptrash,
4710 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4711 s->proxy->id, s->id);
4712
Emeric Brun5a133512017-10-19 14:42:30 +02004713 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004714
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004715 ha_warning("%s.\n", tmptrash->area);
4716 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4717 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004718 free_trash_chunk(tmptrash);
4719 tmptrash = NULL;
4720 }
4721
4722 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4723 set_backend_down(s->proxy);
4724 }
4725 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4726 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4727 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4728 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4729 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4730 s->proxy->last_change = now.tv_sec;
4731 }
4732
4733 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4734 s->down_time += now.tv_sec - s->last_change;
4735
4736 s->last_change = now.tv_sec;
4737 if (s->next_state == SRV_ST_STARTING)
4738 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4739
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004740 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004741 /* now propagate the status change to any LB algorithms */
4742 if (px->lbprm.update_server_eweight)
4743 px->lbprm.update_server_eweight(s);
4744 else if (srv_willbe_usable(s)) {
4745 if (px->lbprm.set_server_status_up)
4746 px->lbprm.set_server_status_up(s);
4747 }
4748 else {
4749 if (px->lbprm.set_server_status_down)
4750 px->lbprm.set_server_status_down(s);
4751 }
4752
4753 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4754 * and it's not a backup server and its effective weight is > 0,
4755 * then it can accept new connections, so we shut down all streams
4756 * on all backup servers.
4757 */
4758 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4759 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4760 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4761
4762 /* check if we can handle some connections queued at the proxy. We
4763 * will take as many as we can handle.
4764 */
4765 xferred = pendconn_grab_from_px(s);
4766
4767 tmptrash = alloc_trash_chunk();
4768 if (tmptrash) {
4769 chunk_printf(tmptrash,
4770 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4771 s->proxy->id, s->id);
4772
Emeric Brun5a133512017-10-19 14:42:30 +02004773 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004774 ha_warning("%s.\n", tmptrash->area);
4775 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4776 tmptrash->area);
4777 send_email_alert(s, LOG_NOTICE, "%s",
4778 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004779 free_trash_chunk(tmptrash);
4780 tmptrash = NULL;
4781 }
4782
4783 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4784 set_backend_down(s->proxy);
4785 }
4786 else if (s->cur_eweight != s->next_eweight) {
4787 /* now propagate the status change to any LB algorithms */
4788 if (px->lbprm.update_server_eweight)
4789 px->lbprm.update_server_eweight(s);
4790 else if (srv_willbe_usable(s)) {
4791 if (px->lbprm.set_server_status_up)
4792 px->lbprm.set_server_status_up(s);
4793 }
4794 else {
4795 if (px->lbprm.set_server_status_down)
4796 px->lbprm.set_server_status_down(s);
4797 }
4798
4799 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4800 set_backend_down(s->proxy);
4801 }
4802
4803 s->next_admin = next_admin;
4804 }
4805
Emeric Brun5a133512017-10-19 14:42:30 +02004806 /* reset operational state change */
4807 *s->op_st_chg.reason = 0;
4808 s->op_st_chg.status = s->op_st_chg.code = -1;
4809 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004810
4811 /* Now we try to apply pending admin changes */
4812
4813 /* Maintenance must also disable health checks */
4814 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4815 if (s->check.state & CHK_ST_ENABLED) {
4816 s->check.state |= CHK_ST_PAUSED;
4817 check->health = 0;
4818 }
4819
4820 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004821 tmptrash = alloc_trash_chunk();
4822 if (tmptrash) {
4823 chunk_printf(tmptrash,
4824 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4825 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4826 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004827
Olivier Houchard796a2b32017-10-24 17:42:47 +02004828 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004829
Olivier Houchard796a2b32017-10-24 17:42:47 +02004830 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004831 ha_warning("%s.\n", tmptrash->area);
4832 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4833 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004834 }
4835 free_trash_chunk(tmptrash);
4836 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004837 }
Emeric Brun8f298292017-12-06 16:47:17 +01004838 /* commit new admin status */
4839
4840 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004841 }
4842 else { /* server was still running */
4843 check->health = 0; /* failure */
4844 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004845
4846 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004847 if (s->proxy->lbprm.set_server_status_down)
4848 s->proxy->lbprm.set_server_status_down(s);
4849
Emeric Brun64cc49c2017-10-03 14:46:45 +02004850 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4851 srv_shutdown_streams(s, SF_ERR_DOWN);
4852
William Dauchy6318d332020-05-02 21:52:36 +02004853 /* force connection cleanup on the given server */
4854 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004855 /* we might have streams queued on this server and waiting for
4856 * a connection. Those which are redispatchable will be queued
4857 * to another server or to the proxy itself.
4858 */
4859 xferred = pendconn_redistribute(s);
4860
4861 tmptrash = alloc_trash_chunk();
4862 if (tmptrash) {
4863 chunk_printf(tmptrash,
4864 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4865 s->flags & SRV_F_BACKUP ? "Backup " : "",
4866 s->proxy->id, s->id,
4867 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4868
4869 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4870
4871 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004872 ha_warning("%s.\n", tmptrash->area);
4873 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4874 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004875 }
4876 free_trash_chunk(tmptrash);
4877 tmptrash = NULL;
4878 }
4879 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4880 set_backend_down(s->proxy);
4881
4882 s->counters.down_trans++;
4883 }
4884 }
4885 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4886 /* OK here we're leaving maintenance, we have many things to check,
4887 * because the server might possibly be coming back up depending on
4888 * its state. In practice, leaving maintenance means that we should
4889 * immediately turn to UP (more or less the slowstart) under the
4890 * following conditions :
4891 * - server is neither checked nor tracked
4892 * - server tracks another server which is not checked
4893 * - server tracks another server which is already up
4894 * Which sums up as something simpler :
4895 * "either the tracking server is up or the server's checks are disabled
4896 * or up". Otherwise we only re-enable health checks. There's a special
4897 * case associated to the stopping state which can be inherited. Note
4898 * that the server might still be in drain mode, which is naturally dealt
4899 * with by the lower level functions.
4900 */
4901
4902 if (s->check.state & CHK_ST_ENABLED) {
4903 s->check.state &= ~CHK_ST_PAUSED;
4904 check->health = check->rise; /* start OK but check immediately */
4905 }
4906
4907 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4908 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4909 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4910 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4911 s->next_state = SRV_ST_STOPPING;
4912 }
4913 else {
4914 s->next_state = SRV_ST_STARTING;
4915 if (s->slowstart > 0)
4916 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4917 else
4918 s->next_state = SRV_ST_RUNNING;
4919 }
4920
4921 }
4922
4923 tmptrash = alloc_trash_chunk();
4924 if (tmptrash) {
4925 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4926 chunk_printf(tmptrash,
4927 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4928 s->flags & SRV_F_BACKUP ? "Backup " : "",
4929 s->proxy->id, s->id,
4930 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4931 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4932 }
4933 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4934 chunk_printf(tmptrash,
4935 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4936 s->flags & SRV_F_BACKUP ? "Backup " : "",
4937 s->proxy->id, s->id, s->hostname,
4938 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4939 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4940 }
4941 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4942 chunk_printf(tmptrash,
4943 "%sServer %s/%s is %s/%s (leaving maintenance)",
4944 s->flags & SRV_F_BACKUP ? "Backup " : "",
4945 s->proxy->id, s->id,
4946 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4947 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4948 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004949 ha_warning("%s.\n", tmptrash->area);
4950 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4951 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004952 free_trash_chunk(tmptrash);
4953 tmptrash = NULL;
4954 }
4955
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004956 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004957 /* now propagate the status change to any LB algorithms */
4958 if (px->lbprm.update_server_eweight)
4959 px->lbprm.update_server_eweight(s);
4960 else if (srv_willbe_usable(s)) {
4961 if (px->lbprm.set_server_status_up)
4962 px->lbprm.set_server_status_up(s);
4963 }
4964 else {
4965 if (px->lbprm.set_server_status_down)
4966 px->lbprm.set_server_status_down(s);
4967 }
4968
4969 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4970 set_backend_down(s->proxy);
4971
Willy Tarreau6a78e612018-08-07 10:14:53 +02004972 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4973 * and it's not a backup server and its effective weight is > 0,
4974 * then it can accept new connections, so we shut down all streams
4975 * on all backup servers.
4976 */
4977 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4978 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4979 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4980
4981 /* check if we can handle some connections queued at the proxy. We
4982 * will take as many as we can handle.
4983 */
4984 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004985 }
4986 else if (s->next_admin & SRV_ADMF_MAINT) {
4987 /* remaining in maintenance mode, let's inform precisely about the
4988 * situation.
4989 */
4990 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4991 tmptrash = alloc_trash_chunk();
4992 if (tmptrash) {
4993 chunk_printf(tmptrash,
4994 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4995 s->flags & SRV_F_BACKUP ? "Backup " : "",
4996 s->proxy->id, s->id);
4997
4998 if (s->track) /* normally it's mandatory here */
4999 chunk_appendf(tmptrash, " via %s/%s",
5000 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005001 ha_warning("%s.\n", tmptrash->area);
5002 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5003 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005004 free_trash_chunk(tmptrash);
5005 tmptrash = NULL;
5006 }
5007 }
5008 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5009 tmptrash = alloc_trash_chunk();
5010 if (tmptrash) {
5011 chunk_printf(tmptrash,
5012 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5013 s->flags & SRV_F_BACKUP ? "Backup " : "",
5014 s->proxy->id, s->id, s->hostname);
5015
5016 if (s->track) /* normally it's mandatory here */
5017 chunk_appendf(tmptrash, " via %s/%s",
5018 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005019 ha_warning("%s.\n", tmptrash->area);
5020 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5021 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005022 free_trash_chunk(tmptrash);
5023 tmptrash = NULL;
5024 }
5025 }
5026 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5027 tmptrash = alloc_trash_chunk();
5028 if (tmptrash) {
5029 chunk_printf(tmptrash,
5030 "%sServer %s/%s remains in forced maintenance",
5031 s->flags & SRV_F_BACKUP ? "Backup " : "",
5032 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005033 ha_warning("%s.\n", tmptrash->area);
5034 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5035 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005036 free_trash_chunk(tmptrash);
5037 tmptrash = NULL;
5038 }
5039 }
5040 /* don't report anything when leaving drain mode and remaining in maintenance */
5041
5042 s->cur_admin = s->next_admin;
5043 }
5044
5045 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5046 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5047 /* drain state is applied only if not yet in maint */
5048
5049 s->last_change = now.tv_sec;
5050 if (px->lbprm.set_server_status_down)
5051 px->lbprm.set_server_status_down(s);
5052
5053 /* we might have streams queued on this server and waiting for
5054 * a connection. Those which are redispatchable will be queued
5055 * to another server or to the proxy itself.
5056 */
5057 xferred = pendconn_redistribute(s);
5058
5059 tmptrash = alloc_trash_chunk();
5060 if (tmptrash) {
5061 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5062 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5063 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5064
5065 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5066
5067 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005068 ha_warning("%s.\n", tmptrash->area);
5069 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5070 tmptrash->area);
5071 send_email_alert(s, LOG_NOTICE, "%s",
5072 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005073 }
5074 free_trash_chunk(tmptrash);
5075 tmptrash = NULL;
5076 }
5077
5078 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5079 set_backend_down(s->proxy);
5080 }
5081 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5082 /* OK completely leaving drain mode */
5083 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5084 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5085 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5086 s->proxy->last_change = now.tv_sec;
5087 }
5088
5089 if (s->last_change < now.tv_sec) // ignore negative times
5090 s->down_time += now.tv_sec - s->last_change;
5091 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005092 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005093
5094 tmptrash = alloc_trash_chunk();
5095 if (tmptrash) {
5096 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5097 chunk_printf(tmptrash,
5098 "%sServer %s/%s is %s (leaving forced drain)",
5099 s->flags & SRV_F_BACKUP ? "Backup " : "",
5100 s->proxy->id, s->id,
5101 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5102 }
5103 else {
5104 chunk_printf(tmptrash,
5105 "%sServer %s/%s is %s (leaving drain)",
5106 s->flags & SRV_F_BACKUP ? "Backup " : "",
5107 s->proxy->id, s->id,
5108 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5109 if (s->track) /* normally it's mandatory here */
5110 chunk_appendf(tmptrash, " via %s/%s",
5111 s->track->proxy->id, s->track->id);
5112 }
5113
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005114 ha_warning("%s.\n", tmptrash->area);
5115 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5116 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005117 free_trash_chunk(tmptrash);
5118 tmptrash = NULL;
5119 }
5120
5121 /* now propagate the status change to any LB algorithms */
5122 if (px->lbprm.update_server_eweight)
5123 px->lbprm.update_server_eweight(s);
5124 else if (srv_willbe_usable(s)) {
5125 if (px->lbprm.set_server_status_up)
5126 px->lbprm.set_server_status_up(s);
5127 }
5128 else {
5129 if (px->lbprm.set_server_status_down)
5130 px->lbprm.set_server_status_down(s);
5131 }
5132 }
5133 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5134 /* remaining in drain mode after removing one of its flags */
5135
5136 tmptrash = alloc_trash_chunk();
5137 if (tmptrash) {
5138 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5139 chunk_printf(tmptrash,
5140 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5141 s->flags & SRV_F_BACKUP ? "Backup " : "",
5142 s->proxy->id, s->id);
5143
5144 if (s->track) /* normally it's mandatory here */
5145 chunk_appendf(tmptrash, " via %s/%s",
5146 s->track->proxy->id, s->track->id);
5147 }
5148 else {
5149 chunk_printf(tmptrash,
5150 "%sServer %s/%s remains in forced drain mode",
5151 s->flags & SRV_F_BACKUP ? "Backup " : "",
5152 s->proxy->id, s->id);
5153 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005154 ha_warning("%s.\n", tmptrash->area);
5155 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5156 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005157 free_trash_chunk(tmptrash);
5158 tmptrash = NULL;
5159 }
5160
5161 /* commit new admin status */
5162
5163 s->cur_admin = s->next_admin;
5164 }
5165 }
5166
5167 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005168 *s->adm_st_chg_cause = 0;
5169}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005170
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005171struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5172{
5173 struct connection *conn;
5174
Olivier Houchard859dc802019-08-08 15:47:21 +02005175 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005176 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005177 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005178 }
5179
5180 return task;
5181}
5182
William Dauchy6318d332020-05-02 21:52:36 +02005183/* cleanup connections for a given server
5184 * might be useful when going on forced maintenance or live changing ip/port
5185 */
William Dauchy707ad322020-05-04 13:52:40 +02005186static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005187{
5188 struct connection *conn;
5189 int did_remove;
5190 int i;
5191 int j;
5192
5193 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5194 for (i = 0; i < global.nbthread; i++) {
5195 did_remove = 0;
5196 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
5197 for (j = 0; j < srv->curr_idle_conns; j++) {
5198 conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5199 if (!conn)
5200 conn = MT_LIST_POP(&srv->safe_conns[i],
5201 struct connection *, list);
5202 if (!conn)
5203 break;
5204 did_remove = 1;
5205 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
5206 }
5207 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
5208 if (did_remove)
5209 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5210 }
5211 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5212}
5213
Willy Tarreau980855b2019-02-07 14:59:29 +01005214struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005215{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005216 struct server *srv;
5217 struct eb32_node *eb;
5218 int i;
5219 unsigned int next_wakeup;
5220 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005221
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005222 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5223 while (1) {
5224 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005225 int exceed_conns;
5226 int to_kill;
5227 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005228
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005229 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5230 if (!eb) {
5231 /* we might have reached the end of the tree, typically because
5232 * <now_ms> is in the first half and we're first scanning the last
5233 * half. Let's loop back to the beginning of the tree now.
5234 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005235
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005236 eb = eb32_first(&idle_conn_srv);
5237 if (likely(!eb))
5238 break;
5239 }
5240 if (tick_is_lt(now_ms, eb->key)) {
5241 /* timer not expired yet, revisit it later */
5242 next_wakeup = eb->key;
5243 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005244 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005245 }
5246 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005247
5248 /* Calculate how many idle connections we want to kill :
5249 * we want to remove half the difference between the total
5250 * of established connections (used or idle) and the max
5251 * number of used connections.
5252 */
5253 curr_idle = srv->curr_idle_conns;
5254 if (curr_idle == 0)
5255 goto remove;
5256 exceed_conns = srv->curr_used_conns + curr_idle -
5257 srv->max_used_conns;
5258 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5259 srv->max_used_conns = srv->curr_used_conns;
5260
5261 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5262 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005263 int j;
5264 int did_remove = 0;
5265
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005266 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5267 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005268 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005269 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005270 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5271 if (!conn)
5272 conn = MT_LIST_POP(&srv->safe_conns[i],
5273 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005274 if (!conn)
5275 break;
5276 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005277 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005278 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005279 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005280 if (did_remove && max_conn < srv->curr_idle_thr[i])
5281 srv_is_empty = 0;
5282 if (did_remove)
5283 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5284 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005285remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005286 eb32_delete(&srv->idle_node);
5287 if (!srv_is_empty) {
5288 /* There are still more idle connections, add the
5289 * server back in the tree.
5290 */
5291 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5292 now_ms);
5293 eb32_insert(&idle_conn_srv, &srv->idle_node);
5294 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005295 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005296 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5297
5298 if (need_wakeup)
5299 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005300 else
5301 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005302
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005303 return task;
5304}
Olivier Houchard88698d92019-04-16 19:07:22 +02005305
5306/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5307static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5308 struct proxy *defpx, const char *file, int line,
5309 char **err)
5310{
5311 int arg = -1;
5312
5313 if (too_many_args(1, args, err, NULL))
5314 return -1;
5315
5316 if (*(args[1]) != 0)
5317 arg = atoi(args[1]);
5318
5319 if (arg < 0 || arg > 100) {
5320 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5321 return -1;
5322 }
5323
5324 if (args[0][10] == 'h')
5325 global.tune.pool_high_ratio = arg;
5326 else
5327 global.tune.pool_low_ratio = arg;
5328 return 0;
5329}
5330
5331/* config keyword parsers */
5332static struct cfg_kw_list cfg_kws = {ILH, {
5333 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5334 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5335 { 0, NULL, NULL }
5336}};
5337
5338INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5339
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005340/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005341 * Local variables:
5342 * c-indent-level: 8
5343 * c-basic-offset: 8
5344 * End:
5345 */