blob: 442756985bfd4131f0aa156dd7390f8ab082a3b6 [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 Tarreaub2551052020-06-09 09:07:15 +020014#include <netinet/tcp.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020015#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020016#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020017
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <import/ebsttree.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <import/xxhash.h>
20
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020022#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020023#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020025#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020027#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020028#include <haproxy/dict-t.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020029#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020032#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020033#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020034#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/port_range.h>
36#include <haproxy/protocol.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020037#include <haproxy/queue.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020038#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020039#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020040#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020041#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020042#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020043#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020044#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020045#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020046
Baptiste Assmannda29fe22019-06-13 13:24:29 +020047
Willy Tarreau3ff577e2018-08-02 11:48:52 +020048static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020049static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010050static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010051static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020052static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
53static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020054static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau21faa912012-10-10 08:27:36 +020056/* List head of all known server keywords */
57static struct srv_kw_list srv_keywords = {
58 .list = LIST_HEAD_INIT(srv_keywords.list)
59};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020060
Willy Tarreauaf613e82020-06-05 08:40:51 +020061__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010062struct eb_root idle_conn_srv = EB_ROOT;
63struct task *idle_conn_task = NULL;
64struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020065struct mt_list toremove_connections[MAX_THREADS];
Willy Tarreauaf613e82020-06-05 08:40:51 +020066__decl_thread(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010067
Frédéric Lécaille7da71292019-05-20 09:47:07 +020068/* The server names dictionary */
69struct dict server_name_dict = {
70 .name = "server names",
71 .values = EB_ROOT_UNIQUE,
72};
73
Baptiste Assmannda29fe22019-06-13 13:24:29 +020074/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010075struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020076
Simon Hormana3608442013-11-01 16:46:15 +090077int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020078{
Emeric Brun52a91d32017-08-31 14:41:55 +020079 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020080 return s->down_time;
81
82 return now.tv_sec - s->last_change + s->down_time;
83}
Willy Tarreaubaaee002006-06-26 02:48:02 +020084
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050085int srv_lastsession(const struct server *s)
86{
87 if (s->counters.last_sess)
88 return now.tv_sec - s->counters.last_sess;
89
90 return -1;
91}
92
Simon Horman4a741432013-02-23 15:35:38 +090093int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020094{
Simon Horman4a741432013-02-23 15:35:38 +090095 const struct server *s = check->server;
96
Willy Tarreauff5ae352013-12-11 20:36:34 +010097 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090098 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010099
Emeric Brun52a91d32017-08-31 14:41:55 +0200100 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900101 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100102
Simon Horman4a741432013-02-23 15:35:38 +0900103 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100104}
105
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100106/*
107 * Check that we did not get a hash collision.
108 * Unlikely, but it can happen.
109 */
110static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100111{
112 struct proxy *p = s->proxy;
113 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100114
115 for (tmpserv = p->srv; tmpserv != NULL;
116 tmpserv = tmpserv->next) {
117 if (tmpserv == s)
118 continue;
119 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
120 continue;
121 if (tmpserv->cookie &&
122 strcmp(tmpserv->cookie, s->cookie) == 0) {
123 ha_warning("We generated two equal cookies for two different servers.\n"
124 "Please change the secret key for '%s'.\n",
125 s->proxy->id);
126 }
127 }
128
129}
130
Willy Tarreau46b7f532018-08-21 11:54:26 +0200131/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200132 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200133 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100134void srv_set_dyncookie(struct server *s)
135{
136 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100137 char *tmpbuf;
138 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100139 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100140 size_t buffer_len;
141 int addr_len;
142 int port;
143
Willy Tarreau5e83d992019-07-30 11:59:34 +0200144 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
145
Olivier Houchard4e694042017-03-14 20:01:29 +0100146 if ((s->flags & SRV_F_COOKIESET) ||
147 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
148 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200149 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100150 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100151
152 if (s->addr.ss_family != AF_INET &&
153 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200154 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100155 /*
156 * Buffer to calculate the cookie value.
157 * The buffer contains the secret key + the server IP address
158 * + the TCP port.
159 */
160 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
161 /*
162 * The TCP port should use only 2 bytes, but is stored in
163 * an unsigned int in struct server, so let's use 4, to be
164 * on the safe side.
165 */
166 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200167 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100168 memcpy(tmpbuf, p->dyncookie_key, key_len);
169 memcpy(&(tmpbuf[key_len]),
170 s->addr.ss_family == AF_INET ?
171 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
172 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
173 addr_len);
174 /*
175 * Make sure it's the same across all the load balancers,
176 * no matter their endianness.
177 */
178 port = htonl(s->svc_port);
179 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
180 hash_value = XXH64(tmpbuf, buffer_len, 0);
181 memprintf(&s->cookie, "%016llx", hash_value);
182 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200183 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100184 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100185
186 /* Don't bother checking if the dyncookie is duplicated if
187 * the server is marked as "disabled", maybe it doesn't have
188 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100189 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100190 if (!(s->next_admin & SRV_ADMF_FMAINT))
191 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200192 out:
193 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100194}
195
Willy Tarreau21faa912012-10-10 08:27:36 +0200196/*
197 * Registers the server keyword list <kwl> as a list of valid keywords for next
198 * parsing sessions.
199 */
200void srv_register_keywords(struct srv_kw_list *kwl)
201{
202 LIST_ADDQ(&srv_keywords.list, &kwl->list);
203}
204
205/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
206 * keyword is found with a NULL ->parse() function, then an attempt is made to
207 * find one with a valid ->parse() function. This way it is possible to declare
208 * platform-dependant, known keywords as NULL, then only declare them as valid
209 * if some options are met. Note that if the requested keyword contains an
210 * opening parenthesis, everything from this point is ignored.
211 */
212struct srv_kw *srv_find_kw(const char *kw)
213{
214 int index;
215 const char *kwend;
216 struct srv_kw_list *kwl;
217 struct srv_kw *ret = NULL;
218
219 kwend = strchr(kw, '(');
220 if (!kwend)
221 kwend = kw + strlen(kw);
222
223 list_for_each_entry(kwl, &srv_keywords.list, list) {
224 for (index = 0; kwl->kw[index].kw != NULL; index++) {
225 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
226 kwl->kw[index].kw[kwend-kw] == 0) {
227 if (kwl->kw[index].parse)
228 return &kwl->kw[index]; /* found it !*/
229 else
230 ret = &kwl->kw[index]; /* may be OK */
231 }
232 }
233 }
234 return ret;
235}
236
237/* Dumps all registered "server" keywords to the <out> string pointer. The
238 * unsupported keywords are only dumped if their supported form was not
239 * found.
240 */
241void srv_dump_kws(char **out)
242{
243 struct srv_kw_list *kwl;
244 int index;
245
Christopher Faulet784063e2020-05-18 12:14:18 +0200246 if (!out)
247 return;
248
Willy Tarreau21faa912012-10-10 08:27:36 +0200249 *out = NULL;
250 list_for_each_entry(kwl, &srv_keywords.list, list) {
251 for (index = 0; kwl->kw[index].kw != NULL; index++) {
252 if (kwl->kw[index].parse ||
253 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
254 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
255 kwl->scope,
256 kwl->kw[index].kw,
257 kwl->kw[index].skip ? " <arg>" : "",
258 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
259 kwl->kw[index].parse ? "" : " (not supported)");
260 }
261 }
262 }
263}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100264
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100265/* Parse the "backup" server keyword */
266static int srv_parse_backup(char **args, int *cur_arg,
267 struct proxy *curproxy, struct server *newsrv, char **err)
268{
269 newsrv->flags |= SRV_F_BACKUP;
270 return 0;
271}
272
Alexander Liu2a54bb72019-05-22 19:44:48 +0800273
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100274/* Parse the "cookie" server keyword */
275static int srv_parse_cookie(char **args, int *cur_arg,
276 struct proxy *curproxy, struct server *newsrv, char **err)
277{
278 char *arg;
279
280 arg = args[*cur_arg + 1];
281 if (!*arg) {
282 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
283 return ERR_ALERT | ERR_FATAL;
284 }
285
286 free(newsrv->cookie);
287 newsrv->cookie = strdup(arg);
288 newsrv->cklen = strlen(arg);
289 newsrv->flags |= SRV_F_COOKIESET;
290 return 0;
291}
292
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100293/* Parse the "disabled" server keyword */
294static int srv_parse_disabled(char **args, int *cur_arg,
295 struct proxy *curproxy, struct server *newsrv, char **err)
296{
Emeric Brun52a91d32017-08-31 14:41:55 +0200297 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
298 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100299 newsrv->check.state |= CHK_ST_PAUSED;
300 newsrv->check.health = 0;
301 return 0;
302}
303
304/* Parse the "enabled" server keyword */
305static int srv_parse_enabled(char **args, int *cur_arg,
306 struct proxy *curproxy, struct server *newsrv, char **err)
307{
Emeric Brun52a91d32017-08-31 14:41:55 +0200308 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
309 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100310 newsrv->check.state &= ~CHK_ST_PAUSED;
311 newsrv->check.health = newsrv->check.rise;
312 return 0;
313}
314
Willy Tarreau9c538e02019-01-23 10:21:49 +0100315static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
316{
317 char *arg;
318
319 arg = args[*cur_arg + 1];
320 if (!*arg) {
321 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
322 return ERR_ALERT | ERR_FATAL;
323 }
324 newsrv->max_reuse = atoi(arg);
325
326 return 0;
327}
328
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100329static 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 +0100330{
331 const char *res;
332 char *arg;
333 unsigned int time;
334
335 arg = args[*cur_arg + 1];
336 if (!*arg) {
337 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
338 return ERR_ALERT | ERR_FATAL;
339 }
340 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200341 if (res == PARSE_TIME_OVER) {
342 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
343 args[*cur_arg+1], args[*cur_arg]);
344 return ERR_ALERT | ERR_FATAL;
345 }
346 else if (res == PARSE_TIME_UNDER) {
347 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
348 args[*cur_arg+1], args[*cur_arg]);
349 return ERR_ALERT | ERR_FATAL;
350 }
351 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100352 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
353 *res, args[*cur_arg]);
354 return ERR_ALERT | ERR_FATAL;
355 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100356 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100357
358 return 0;
359}
360
Olivier Houchard006e3102018-12-10 18:30:32 +0100361static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
362{
363 char *arg;
364
365 arg = args[*cur_arg + 1];
366 if (!*arg) {
367 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
368 return ERR_ALERT | ERR_FATAL;
369 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100370
Olivier Houchard006e3102018-12-10 18:30:32 +0100371 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100372 if ((int)newsrv->max_idle_conns < -1) {
373 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
374 return ERR_ALERT | ERR_FATAL;
375 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100376
377 return 0;
378}
379
Willy Tarreaudff55432012-10-10 17:51:05 +0200380/* parse the "id" server keyword */
381static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
382{
383 struct eb32_node *node;
384
385 if (!*args[*cur_arg + 1]) {
386 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
387 return ERR_ALERT | ERR_FATAL;
388 }
389
390 newsrv->puid = atol(args[*cur_arg + 1]);
391 newsrv->conf.id.key = newsrv->puid;
392
393 if (newsrv->puid <= 0) {
394 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
395 return ERR_ALERT | ERR_FATAL;
396 }
397
398 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
399 if (node) {
400 struct server *target = container_of(node, struct server, conf.id);
401 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
402 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
403 target->id);
404 return ERR_ALERT | ERR_FATAL;
405 }
406
407 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200408 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200409 return 0;
410}
411
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100412/* Parse the "namespace" server keyword */
413static int srv_parse_namespace(char **args, int *cur_arg,
414 struct proxy *curproxy, struct server *newsrv, char **err)
415{
Willy Tarreaue5733232019-05-22 19:24:06 +0200416#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100417 char *arg;
418
419 arg = args[*cur_arg + 1];
420 if (!*arg) {
421 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
422 return ERR_ALERT | ERR_FATAL;
423 }
424
425 if (!strcmp(arg, "*")) {
426 /* Use the namespace associated with the connection (if present). */
427 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
428 return 0;
429 }
430
431 /*
432 * As this parser may be called several times for the same 'default-server'
433 * object, or for a new 'server' instance deriving from a 'default-server'
434 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
435 */
436 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
437
438 newsrv->netns = netns_store_lookup(arg, strlen(arg));
439 if (!newsrv->netns)
440 newsrv->netns = netns_store_insert(arg);
441
442 if (!newsrv->netns) {
443 memprintf(err, "Cannot open namespace '%s'", arg);
444 return ERR_ALERT | ERR_FATAL;
445 }
446
447 return 0;
448#else
449 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
450 return ERR_ALERT | ERR_FATAL;
451#endif
452}
453
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100454/* Parse the "no-backup" server keyword */
455static int srv_parse_no_backup(char **args, int *cur_arg,
456 struct proxy *curproxy, struct server *newsrv, char **err)
457{
458 newsrv->flags &= ~SRV_F_BACKUP;
459 return 0;
460}
461
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100462
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100463/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200464static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100465{
466 srv->pp_opts &= ~flags;
467 return 0;
468}
469
470/* Parse the "no-send-proxy" server keyword */
471static int srv_parse_no_send_proxy(char **args, int *cur_arg,
472 struct proxy *curproxy, struct server *newsrv, char **err)
473{
474 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
475}
476
477/* Parse the "no-send-proxy-v2" server keyword */
478static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
479 struct proxy *curproxy, struct server *newsrv, char **err)
480{
481 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
482}
483
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200484/* Parse the "no-tfo" server keyword */
485static int srv_parse_no_tfo(char **args, int *cur_arg,
486 struct proxy *curproxy, struct server *newsrv, char **err)
487{
488 newsrv->flags &= ~SRV_F_FASTOPEN;
489 return 0;
490}
491
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100492/* Parse the "non-stick" server keyword */
493static int srv_parse_non_stick(char **args, int *cur_arg,
494 struct proxy *curproxy, struct server *newsrv, char **err)
495{
496 newsrv->flags |= SRV_F_NON_STICK;
497 return 0;
498}
499
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100500/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200501static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100502{
503 srv->pp_opts |= flags;
504 return 0;
505}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200506/* parse the "proto" server keyword */
507static int srv_parse_proto(char **args, int *cur_arg,
508 struct proxy *px, struct server *newsrv, char **err)
509{
510 struct ist proto;
511
512 if (!*args[*cur_arg + 1]) {
513 memprintf(err, "'%s' : missing value", args[*cur_arg]);
514 return ERR_ALERT | ERR_FATAL;
515 }
516 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
517 newsrv->mux_proto = get_mux_proto(proto);
518 if (!newsrv->mux_proto) {
519 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
520 return ERR_ALERT | ERR_FATAL;
521 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200522 return 0;
523}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100524
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100525/* parse the "proxy-v2-options" */
526static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
527 struct proxy *px, struct server *newsrv, char **err)
528{
529 char *p, *n;
530 for (p = args[*cur_arg+1]; p; p = n) {
531 n = strchr(p, ',');
532 if (n)
533 *n++ = '\0';
534 if (!strcmp(p, "ssl")) {
535 newsrv->pp_opts |= SRV_PP_V2_SSL;
536 } else if (!strcmp(p, "cert-cn")) {
537 newsrv->pp_opts |= SRV_PP_V2_SSL;
538 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100539 } else if (!strcmp(p, "cert-key")) {
540 newsrv->pp_opts |= SRV_PP_V2_SSL;
541 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
542 } else if (!strcmp(p, "cert-sig")) {
543 newsrv->pp_opts |= SRV_PP_V2_SSL;
544 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
545 } else if (!strcmp(p, "ssl-cipher")) {
546 newsrv->pp_opts |= SRV_PP_V2_SSL;
547 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100548 } else if (!strcmp(p, "authority")) {
549 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100550 } else if (!strcmp(p, "crc32c")) {
551 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100552 } else if (!strcmp(p, "unique-id")) {
553 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100554 } else
555 goto fail;
556 }
557 return 0;
558 fail:
559 if (err)
560 memprintf(err, "'%s' : proxy v2 option not implemented", p);
561 return ERR_ALERT | ERR_FATAL;
562}
563
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100564/* Parse the "observe" server keyword */
565static int srv_parse_observe(char **args, int *cur_arg,
566 struct proxy *curproxy, struct server *newsrv, char **err)
567{
568 char *arg;
569
570 arg = args[*cur_arg + 1];
571 if (!*arg) {
572 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
573 return ERR_ALERT | ERR_FATAL;
574 }
575
576 if (!strcmp(arg, "none")) {
577 newsrv->observe = HANA_OBS_NONE;
578 }
579 else if (!strcmp(arg, "layer4")) {
580 newsrv->observe = HANA_OBS_LAYER4;
581 }
582 else if (!strcmp(arg, "layer7")) {
583 if (curproxy->mode != PR_MODE_HTTP) {
584 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
585 return ERR_ALERT;
586 }
587 newsrv->observe = HANA_OBS_LAYER7;
588 }
589 else {
590 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
591 "but got '%s'\n", args[*cur_arg], arg);
592 return ERR_ALERT | ERR_FATAL;
593 }
594
595 return 0;
596}
597
Frédéric Lécaille16186232017-03-14 16:42:49 +0100598/* Parse the "redir" server keyword */
599static int srv_parse_redir(char **args, int *cur_arg,
600 struct proxy *curproxy, struct server *newsrv, char **err)
601{
602 char *arg;
603
604 arg = args[*cur_arg + 1];
605 if (!*arg) {
606 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
607 return ERR_ALERT | ERR_FATAL;
608 }
609
610 free(newsrv->rdr_pfx);
611 newsrv->rdr_pfx = strdup(arg);
612 newsrv->rdr_len = strlen(arg);
613
614 return 0;
615}
616
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100617/* Parse the "send-proxy" server keyword */
618static int srv_parse_send_proxy(char **args, int *cur_arg,
619 struct proxy *curproxy, struct server *newsrv, char **err)
620{
621 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
622}
623
624/* Parse the "send-proxy-v2" server keyword */
625static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
626 struct proxy *curproxy, struct server *newsrv, char **err)
627{
628 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
629}
630
Frédéric Lécailledba97072017-03-17 15:33:50 +0100631
632/* Parse the "source" server keyword */
633static int srv_parse_source(char **args, int *cur_arg,
634 struct proxy *curproxy, struct server *newsrv, char **err)
635{
636 char *errmsg;
637 int port_low, port_high;
638 struct sockaddr_storage *sk;
639 struct protocol *proto;
640
641 errmsg = NULL;
642
643 if (!*args[*cur_arg + 1]) {
644 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
645 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
646 goto err;
647 }
648
649 /* 'sk' is statically allocated (no need to be freed). */
650 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
651 if (!sk) {
652 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
653 goto err;
654 }
655
656 proto = protocol_by_family(sk->ss_family);
657 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100658 ha_alert("'%s %s' : connect() not supported for this address family.\n",
659 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100660 goto err;
661 }
662
663 newsrv->conn_src.opts |= CO_SRC_BIND;
664 newsrv->conn_src.source_addr = *sk;
665
666 if (port_low != port_high) {
667 int i;
668
669 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100670 ha_alert("'%s' does not support port offsets (found '%s').\n",
671 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100672 goto err;
673 }
674
675 if (port_low <= 0 || port_low > 65535 ||
676 port_high <= 0 || port_high > 65535 ||
677 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100678 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 +0100679 goto err;
680 }
681 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
682 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
683 newsrv->conn_src.sport_range->ports[i] = port_low + i;
684 }
685
686 *cur_arg += 2;
687 while (*(args[*cur_arg])) {
688 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
689#if defined(CONFIG_HAP_TRANSPARENT)
690 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100691 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
692 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100693 goto err;
694 }
695 if (!strcmp(args[*cur_arg + 1], "client")) {
696 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
697 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
698 }
699 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
700 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
701 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
702 }
703 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
704 char *name, *end;
705
706 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100707 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100708 name++;
709
710 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100711 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100712 end++;
713
714 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
715 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
716 free(newsrv->conn_src.bind_hdr_name);
717 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
718 newsrv->conn_src.bind_hdr_len = end - name;
719 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
720 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
721 newsrv->conn_src.bind_hdr_occ = -1;
722
723 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100724 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100725 end++;
726 if (*end == ',') {
727 end++;
728 name = end;
729 if (*end == '-')
730 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100731 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100732 end++;
733 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
734 }
735
736 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100737 ha_alert("usesrc hdr_ip(name,num) does not support negative"
738 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100739 goto err;
740 }
741 }
742 else {
743 struct sockaddr_storage *sk;
744 int port1, port2;
745
746 /* 'sk' is statically allocated (no need to be freed). */
747 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
748 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100749 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100750 goto err;
751 }
752
753 proto = protocol_by_family(sk->ss_family);
754 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100755 ha_alert("'%s %s' : connect() not supported for this address family.\n",
756 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100757 goto err;
758 }
759
760 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100761 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
762 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100763 goto err;
764 }
765 newsrv->conn_src.tproxy_addr = *sk;
766 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
767 }
768 global.last_checks |= LSTCHK_NETADM;
769 *cur_arg += 2;
770 continue;
771#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100772 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 +0100773 goto err;
774#endif /* defined(CONFIG_HAP_TRANSPARENT) */
775 } /* "usesrc" */
776
777 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
778#ifdef SO_BINDTODEVICE
779 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100780 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100781 goto err;
782 }
783 free(newsrv->conn_src.iface_name);
784 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
785 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
786 global.last_checks |= LSTCHK_NETADM;
787#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100788 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100789 goto err;
790#endif
791 *cur_arg += 2;
792 continue;
793 }
794 /* this keyword in not an option of "source" */
795 break;
796 } /* while */
797
798 return 0;
799
800 err:
801 free(errmsg);
802 return ERR_ALERT | ERR_FATAL;
803}
804
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100805/* Parse the "stick" server keyword */
806static int srv_parse_stick(char **args, int *cur_arg,
807 struct proxy *curproxy, struct server *newsrv, char **err)
808{
809 newsrv->flags &= ~SRV_F_NON_STICK;
810 return 0;
811}
812
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100813/* Parse the "track" server keyword */
814static int srv_parse_track(char **args, int *cur_arg,
815 struct proxy *curproxy, struct server *newsrv, char **err)
816{
817 char *arg;
818
819 arg = args[*cur_arg + 1];
820 if (!*arg) {
821 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
822 return ERR_ALERT | ERR_FATAL;
823 }
824
825 free(newsrv->trackit);
826 newsrv->trackit = strdup(arg);
827
828 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800829}
830
831/* Parse the "socks4" server keyword */
832static int srv_parse_socks4(char **args, int *cur_arg,
833 struct proxy *curproxy, struct server *newsrv, char **err)
834{
835 char *errmsg;
836 int port_low, port_high;
837 struct sockaddr_storage *sk;
838 struct protocol *proto;
839
840 errmsg = NULL;
841
842 if (!*args[*cur_arg + 1]) {
843 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
844 goto err;
845 }
846
847 /* 'sk' is statically allocated (no need to be freed). */
848 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
849 if (!sk) {
850 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
851 goto err;
852 }
853
854 proto = protocol_by_family(sk->ss_family);
855 if (!proto || !proto->connect) {
856 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
857 goto err;
858 }
859
860 newsrv->flags |= SRV_F_SOCKS4_PROXY;
861 newsrv->socks4_addr = *sk;
862
863 if (port_low != port_high) {
864 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
865 goto err;
866 }
867
868 if (!port_low) {
869 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
870 goto err;
871 }
872
873 return 0;
874
875 err:
876 free(errmsg);
877 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100878}
879
Frédéric Lécailledba97072017-03-17 15:33:50 +0100880
Willy Tarreau034c88c2017-01-23 23:36:45 +0100881/* parse the "tfo" server keyword */
882static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
883{
884 newsrv->flags |= SRV_F_FASTOPEN;
885 return 0;
886}
887
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200888/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200889 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200890 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200891 *
892 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200893 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200894void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200895{
Willy Tarreau87b09662015-04-03 00:22:06 +0200896 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200897
Willy Tarreau87b09662015-04-03 00:22:06 +0200898 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
899 if (stream->srv_conn == srv)
900 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200901}
902
903/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200904 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200905 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200906 *
907 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200908 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200909void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200910{
911 struct server *srv;
912
913 for (srv = px->srv; srv != NULL; srv = srv->next)
914 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200915 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200916}
917
Willy Tarreaubda92272014-05-20 21:55:30 +0200918/* Appends some information to a message string related to a server going UP or
919 * DOWN. If both <forced> and <reason> are null and the server tracks another
920 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200921 * If <check> is non-null, an entire string describing the check result will be
922 * appended after a comma and a space (eg: to report some information from the
923 * check that changed the state). In the other case, the string will be built
924 * using the check results stored into the struct server if present.
925 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200926 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200927 *
928 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200929 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200930void srv_append_status(struct buffer *msg, struct server *s,
931 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200932{
Emeric Brun5a133512017-10-19 14:42:30 +0200933 short status = s->op_st_chg.status;
934 short code = s->op_st_chg.code;
935 long duration = s->op_st_chg.duration;
936 char *desc = s->op_st_chg.reason;
937
938 if (check) {
939 status = check->status;
940 code = check->code;
941 duration = check->duration;
942 desc = check->desc;
943 }
944
945 if (status != -1) {
946 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
947
948 if (status >= HCHK_STATUS_L57DATA)
949 chunk_appendf(msg, ", code: %d", code);
950
951 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200952 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200953
954 chunk_appendf(msg, ", info: \"");
955
956 chunk_initlen(&src, desc, 0, strlen(desc));
957 chunk_asciiencode(msg, &src, '"');
958
959 chunk_appendf(msg, "\"");
960 }
961
962 if (duration >= 0)
963 chunk_appendf(msg, ", check duration: %ldms", duration);
964 }
965 else if (desc && *desc) {
966 chunk_appendf(msg, ", %s", desc);
967 }
968 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200969 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200970 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200971
972 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200973 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200974 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
975 " %d sessions active, %d requeued, %d remaining in queue",
976 s->proxy->srv_act, s->proxy->srv_bck,
977 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
978 s->cur_sess, xferred, s->nbpend);
979 else
980 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
981 " %d sessions requeued, %d total in queue",
982 s->proxy->srv_act, s->proxy->srv_bck,
983 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
984 xferred, s->nbpend);
985 }
986}
987
Emeric Brun5a133512017-10-19 14:42:30 +0200988/* Marks server <s> down, regardless of its checks' statuses. The server is
989 * registered in a list to postpone the counting of the remaining servers on
990 * the proxy and transfers queued streams whenever possible to other servers at
991 * a sync point. Maintenance servers are ignored. It stores the <reason> if
992 * non-null as the reason for going down or the available data from the check
993 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200994 *
995 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200996 */
Emeric Brun5a133512017-10-19 14:42:30 +0200997void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200998{
999 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001000
Emeric Brun64cc49c2017-10-03 14:46:45 +02001001 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001002 return;
1003
Emeric Brun52a91d32017-08-31 14:41:55 +02001004 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001005 *s->op_st_chg.reason = 0;
1006 s->op_st_chg.status = -1;
1007 if (reason) {
1008 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1009 }
1010 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001011 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001012 s->op_st_chg.code = check->code;
1013 s->op_st_chg.status = check->status;
1014 s->op_st_chg.duration = check->duration;
1015 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001016
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001017 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001018 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001019
Emeric Brun9f0b4582017-10-23 14:39:51 +02001020 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001021 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001022 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001023 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001024 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001025}
1026
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001027/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001028 * in maintenance. The server is registered in a list to postpone the counting
1029 * of the remaining servers on the proxy and tries to grab requests from the
1030 * proxy at a sync point. Maintenance servers are ignored. It stores the
1031 * <reason> if non-null as the reason for going down or the available data
1032 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001033 *
1034 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001035 */
Emeric Brun5a133512017-10-19 14:42:30 +02001036void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001037{
1038 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001039
Emeric Brun64cc49c2017-10-03 14:46:45 +02001040 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001041 return;
1042
Emeric Brun52a91d32017-08-31 14:41:55 +02001043 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001044 return;
1045
Emeric Brun52a91d32017-08-31 14:41:55 +02001046 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001047 *s->op_st_chg.reason = 0;
1048 s->op_st_chg.status = -1;
1049 if (reason) {
1050 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1051 }
1052 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001053 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001054 s->op_st_chg.code = check->code;
1055 s->op_st_chg.status = check->status;
1056 s->op_st_chg.duration = check->duration;
1057 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001058
Emeric Brun64cc49c2017-10-03 14:46:45 +02001059 if (s->slowstart <= 0)
1060 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001061
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001062 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001063 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001064
Emeric Brun9f0b4582017-10-23 14:39:51 +02001065 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001066 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001067 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001068 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001069 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001070}
1071
Willy Tarreau8eb77842014-05-21 13:54:57 +02001072/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001073 * isn't in maintenance. The server is registered in a list to postpone the
1074 * counting of the remaining servers on the proxy and tries to grab requests
1075 * from the proxy. Maintenance servers are ignored. It stores the
1076 * <reason> if non-null as the reason for going down or the available data
1077 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001078 * up. Note that it makes use of the trash to build the log strings, so <reason>
1079 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001080 *
1081 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001082 */
Emeric Brun5a133512017-10-19 14:42:30 +02001083void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001084{
1085 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001086
Emeric Brun64cc49c2017-10-03 14:46:45 +02001087 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001088 return;
1089
Emeric Brun52a91d32017-08-31 14:41:55 +02001090 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001091 return;
1092
Emeric Brun52a91d32017-08-31 14:41:55 +02001093 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001094 *s->op_st_chg.reason = 0;
1095 s->op_st_chg.status = -1;
1096 if (reason) {
1097 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1098 }
1099 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001100 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001101 s->op_st_chg.code = check->code;
1102 s->op_st_chg.status = check->status;
1103 s->op_st_chg.duration = check->duration;
1104 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001105
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001106 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001107 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001108
Emeric Brun9f0b4582017-10-23 14:39:51 +02001109 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001110 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001111 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001112 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001113 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001114}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001115
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001116/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1117 * enforce either maint mode or drain mode. It is not allowed to set more than
1118 * one flag at once. The equivalent "inherited" flag is propagated to all
1119 * tracking servers. Maintenance mode disables health checks (but not agent
1120 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001121 * is done. If <cause> is non-null, it will be displayed at the end of the log
1122 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001123 *
1124 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001125 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001126void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001127{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001128 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129
1130 if (!mode)
1131 return;
1132
1133 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001135 return;
1136
Emeric Brun52a91d32017-08-31 14:41:55 +02001137 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001138 if (cause)
1139 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1140
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001141 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001142 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001143
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001144 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001145 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1146 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001147 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001148
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001149 /* compute the inherited flag to propagate */
1150 if (mode & SRV_ADMF_MAINT)
1151 mode = SRV_ADMF_IMAINT;
1152 else if (mode & SRV_ADMF_DRAIN)
1153 mode = SRV_ADMF_IDRAIN;
1154
Emeric Brun9f0b4582017-10-23 14:39:51 +02001155 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001156 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001157 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001158 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001159 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001160}
1161
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001162/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1163 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1164 * than one flag at once. The equivalent "inherited" flag is propagated to all
1165 * tracking servers. Leaving maintenance mode re-enables health checks. When
1166 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001167 *
1168 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001169 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001170void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001171{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001172 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001173
1174 if (!mode)
1175 return;
1176
1177 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001178 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001179 return;
1180
Emeric Brun52a91d32017-08-31 14:41:55 +02001181 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001182
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001183 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001184 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001185
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001186 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001187 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1188 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001189 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001190
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001191 if (mode & SRV_ADMF_MAINT)
1192 mode = SRV_ADMF_IMAINT;
1193 else if (mode & SRV_ADMF_DRAIN)
1194 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001195
Emeric Brun9f0b4582017-10-23 14:39:51 +02001196 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001197 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001198 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001199 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001200 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001201}
1202
Willy Tarreau757478e2016-11-03 19:22:19 +01001203/* principle: propagate maint and drain to tracking servers. This is useful
1204 * upon startup so that inherited states are correct.
1205 */
1206static void srv_propagate_admin_state(struct server *srv)
1207{
1208 struct server *srv2;
1209
1210 if (!srv->trackers)
1211 return;
1212
1213 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001214 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001215 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001216 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001217
Emeric Brun52a91d32017-08-31 14:41:55 +02001218 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001219 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001220 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001221 }
1222}
1223
1224/* Compute and propagate the admin states for all servers in proxy <px>.
1225 * Only servers *not* tracking another one are considered, because other
1226 * ones will be handled when the server they track is visited.
1227 */
1228void srv_compute_all_admin_states(struct proxy *px)
1229{
1230 struct server *srv;
1231
1232 for (srv = px->srv; srv; srv = srv->next) {
1233 if (srv->track)
1234 continue;
1235 srv_propagate_admin_state(srv);
1236 }
1237}
1238
Willy Tarreaudff55432012-10-10 17:51:05 +02001239/* Note: must not be declared <const> as its list will be overwritten.
1240 * Please take care of keeping this list alphabetically sorted, doing so helps
1241 * all code contributors.
1242 * Optional keywords are also declared with a NULL ->parse() function so that
1243 * the config parser can report an appropriate error when a known keyword was
1244 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001245 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001246 */
1247static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001248 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001249 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001250 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1251 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001252 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001253 { "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 +01001254 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001255 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001256 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1257 { "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 +02001258 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001259 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001260 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001261 { "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 +01001262 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001263 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001264 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001265 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001266 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1267 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001268 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001269 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001270 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001271 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001272 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001273 { NULL, NULL, 0 },
1274}};
1275
Willy Tarreau0108d902018-11-25 19:14:37 +01001276INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001277
Willy Tarreau004e0452013-11-21 11:22:01 +01001278/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001279 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001280 * state is automatically disabled if the time is elapsed. If <must_update> is
1281 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001282 *
1283 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001284 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001285void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001286{
1287 struct proxy *px = sv->proxy;
1288 unsigned w;
1289
1290 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1291 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001292 if (sv->next_state == SRV_ST_STARTING)
1293 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001294 }
1295
1296 /* We must take care of not pushing the server to full throttle during slow starts.
1297 * It must also start immediately, at least at the minimal step when leaving maintenance.
1298 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001299 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001300 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1301 else
1302 w = px->lbprm.wdiv;
1303
Emeric Brun52a91d32017-08-31 14:41:55 +02001304 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001305
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001306 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001307 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001308 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001309}
1310
Willy Tarreaubaaee002006-06-26 02:48:02 +02001311/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001312 * Parses weight_str and configures sv accordingly.
1313 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001314 *
1315 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001316 */
1317const char *server_parse_weight_change_request(struct server *sv,
1318 const char *weight_str)
1319{
1320 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001321 long int w;
1322 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001323
1324 px = sv->proxy;
1325
1326 /* if the weight is terminated with '%', it is set relative to
1327 * the initial weight, otherwise it is absolute.
1328 */
1329 if (!*weight_str)
1330 return "Require <weight> or <weight%>.\n";
1331
Simon Hormanb796afa2013-02-12 10:45:53 +09001332 w = strtol(weight_str, &end, 10);
1333 if (end == weight_str)
1334 return "Empty weight string empty or preceded by garbage";
1335 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001336 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001337 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001338 /* Avoid integer overflow */
1339 if (w > 25600)
1340 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001341 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001342 if (w > 256)
1343 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001344 }
1345 else if (w < 0 || w > 256)
1346 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001347 else if (end[0] != '\0')
1348 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001349
1350 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1351 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1352
1353 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001354 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001355
1356 return NULL;
1357}
1358
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001359/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001360 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1361 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001362 * Returns:
1363 * - error string on error
1364 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001365 *
1366 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001367 */
1368const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001369 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001370{
1371 unsigned char ip[INET6_ADDRSTRLEN];
1372
1373 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001374 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001375 return NULL;
1376 }
1377 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001378 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001379 return NULL;
1380 }
1381
1382 return "Could not understand IP address format.\n";
1383}
1384
Willy Tarreau46b7f532018-08-21 11:54:26 +02001385/*
1386 * Must be called with the server lock held.
1387 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001388const char *server_parse_maxconn_change_request(struct server *sv,
1389 const char *maxconn_str)
1390{
1391 long int v;
1392 char *end;
1393
1394 if (!*maxconn_str)
1395 return "Require <maxconn>.\n";
1396
1397 v = strtol(maxconn_str, &end, 10);
1398 if (end == maxconn_str)
1399 return "maxconn string empty or preceded by garbage";
1400 else if (end[0] != '\0')
1401 return "Trailing garbage in maxconn string";
1402
1403 if (sv->maxconn == sv->minconn) { // static maxconn
1404 sv->maxconn = sv->minconn = v;
1405 } else { // dynamic maxconn
1406 sv->maxconn = v;
1407 }
1408
1409 if (may_dequeue_tasks(sv, sv->proxy))
1410 process_srv_queue(sv);
1411
1412 return NULL;
1413}
1414
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001415#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001416static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1417 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001418{
1419 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001420 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001421 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001422 NULL,
1423 };
1424
1425 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001426 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001427
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001428 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001429}
1430
1431static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1432{
1433 struct sample_expr *expr;
1434
1435 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001436 if (!expr) {
1437 memprintf(err, "error detected while parsing sni expression : %s", *err);
1438 return ERR_ALERT | ERR_FATAL;
1439 }
1440
1441 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1442 memprintf(err, "error detected while parsing sni expression : "
1443 " fetch method '%s' extracts information from '%s', "
1444 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001445 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001446 return ERR_ALERT | ERR_FATAL;
1447 }
1448
1449 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1450 release_sample_expr(newsrv->ssl_ctx.sni);
1451 newsrv->ssl_ctx.sni = expr;
1452
1453 return 0;
1454}
1455#endif
1456
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001457static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, int err_code, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001458{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001459 char *msg = "error encountered while processing ";
1460 char *quote = "'";
1461 char *token = args[cur_arg];
1462
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001463 if (err && *err) {
1464 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001465 msg = *err;
1466 quote = "";
1467 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001468 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001469
1470 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1471 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1472 file, linenum, args[0], args[1],
1473 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001474 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001475 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1476 file, linenum, args[0], args[1],
1477 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001478}
1479
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001480static void srv_conn_src_sport_range_cpy(struct server *srv,
1481 struct server *src)
1482{
1483 int range_sz;
1484
1485 range_sz = src->conn_src.sport_range->size;
1486 if (range_sz > 0) {
1487 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1488 if (srv->conn_src.sport_range != NULL) {
1489 int i;
1490
1491 for (i = 0; i < range_sz; i++) {
1492 srv->conn_src.sport_range->ports[i] =
1493 src->conn_src.sport_range->ports[i];
1494 }
1495 }
1496 }
1497}
1498
1499/*
1500 * Copy <src> server connection source settings to <srv> server everything needed.
1501 */
1502static void srv_conn_src_cpy(struct server *srv, struct server *src)
1503{
1504 srv->conn_src.opts = src->conn_src.opts;
1505 srv->conn_src.source_addr = src->conn_src.source_addr;
1506
1507 /* Source port range copy. */
1508 if (src->conn_src.sport_range != NULL)
1509 srv_conn_src_sport_range_cpy(srv, src);
1510
1511#ifdef CONFIG_HAP_TRANSPARENT
1512 if (src->conn_src.bind_hdr_name != NULL) {
1513 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1514 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1515 }
1516 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1517 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1518#endif
1519 if (src->conn_src.iface_name != NULL)
1520 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1521}
1522
1523/*
1524 * Copy <src> server SSL settings to <srv> server allocating
1525 * everything needed.
1526 */
1527#if defined(USE_OPENSSL)
1528static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1529{
1530 if (src->ssl_ctx.ca_file != NULL)
1531 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1532 if (src->ssl_ctx.crl_file != NULL)
1533 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1534 if (src->ssl_ctx.client_crt != NULL)
1535 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1536
1537 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1538
1539 if (src->ssl_ctx.verify_host != NULL)
1540 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1541 if (src->ssl_ctx.ciphers != NULL)
1542 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001543 if (src->ssl_ctx.options)
1544 srv->ssl_ctx.options = src->ssl_ctx.options;
1545 if (src->ssl_ctx.methods.flags)
1546 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1547 if (src->ssl_ctx.methods.min)
1548 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1549 if (src->ssl_ctx.methods.max)
1550 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1551
Willy Tarreau5db847a2019-05-09 14:13:35 +02001552#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001553 if (src->ssl_ctx.ciphersuites != NULL)
1554 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1555#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001556 if (src->sni_expr != NULL)
1557 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001558
1559#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1560 if (src->ssl_ctx.alpn_str) {
1561 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1562 if (srv->ssl_ctx.alpn_str) {
1563 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1564 src->ssl_ctx.alpn_len);
1565 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1566 }
1567 }
1568#endif
1569#ifdef OPENSSL_NPN_NEGOTIATED
1570 if (src->ssl_ctx.npn_str) {
1571 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1572 if (srv->ssl_ctx.npn_str) {
1573 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1574 src->ssl_ctx.npn_len);
1575 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1576 }
1577 }
1578#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001579}
1580#endif
1581
1582/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001583 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001584 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001585 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001586 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001587static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001588{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001589 char *hostname_dn;
1590 int hostname_len, hostname_dn_len;
1591
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001592 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001593 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001594
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001596 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001597 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1598 hostname_dn, trash.size);
1599 if (hostname_dn_len == -1)
1600 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001601
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001602
Christopher Faulet67957bd2017-09-27 11:00:59 +02001603 free(srv->hostname);
1604 free(srv->hostname_dn);
1605 srv->hostname = strdup(hostname);
1606 srv->hostname_dn = strdup(hostname_dn);
1607 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001608 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001609 goto err;
1610
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001611 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001612
1613 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001614 free(srv->hostname); srv->hostname = NULL;
1615 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001616 return -1;
1617}
1618
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001619/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001620 * Copy <src> server settings to <srv> server allocating
1621 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001622 * This function is not supposed to be called at any time, but only
1623 * during server settings parsing or during server allocations from
1624 * a server template, and just after having calloc()'ed a new server.
1625 * So, <src> may only be a default server (when parsing server settings)
1626 * or a server template (during server allocations from a server template).
1627 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1628 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001629 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001630static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001631{
1632 /* Connection source settings copy */
1633 srv_conn_src_cpy(srv, src);
1634
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001635 if (srv_tmpl) {
1636 srv->addr = src->addr;
1637 srv->svc_port = src->svc_port;
1638 }
1639
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001640 srv->pp_opts = src->pp_opts;
1641 if (src->rdr_pfx != NULL) {
1642 srv->rdr_pfx = strdup(src->rdr_pfx);
1643 srv->rdr_len = src->rdr_len;
1644 }
1645 if (src->cookie != NULL) {
1646 srv->cookie = strdup(src->cookie);
1647 srv->cklen = src->cklen;
1648 }
1649 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001650 srv->check.addr = src->check.addr;
1651 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001652 srv->check.use_ssl = src->check.use_ssl;
1653 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001654 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001655 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001656 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001657 /* Note: 'flags' field has potentially been already initialized. */
1658 srv->flags |= src->flags;
1659 srv->do_check = src->do_check;
1660 srv->do_agent = src->do_agent;
1661 if (srv->check.port)
1662 srv->flags |= SRV_F_CHECKPORT;
1663 srv->check.inter = src->check.inter;
1664 srv->check.fastinter = src->check.fastinter;
1665 srv->check.downinter = src->check.downinter;
1666 srv->agent.use_ssl = src->agent.use_ssl;
1667 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001668
1669 if (src->agent.tcpcheck_rules) {
1670 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1671 if (srv->agent.tcpcheck_rules) {
1672 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1673 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1674 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1675 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1676 &src->agent.tcpcheck_rules->preset_vars);
1677 }
1678 }
1679
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001680 srv->agent.inter = src->agent.inter;
1681 srv->agent.fastinter = src->agent.fastinter;
1682 srv->agent.downinter = src->agent.downinter;
1683 srv->maxqueue = src->maxqueue;
1684 srv->minconn = src->minconn;
1685 srv->maxconn = src->maxconn;
1686 srv->slowstart = src->slowstart;
1687 srv->observe = src->observe;
1688 srv->onerror = src->onerror;
1689 srv->onmarkeddown = src->onmarkeddown;
1690 srv->onmarkedup = src->onmarkedup;
1691 if (src->trackit != NULL)
1692 srv->trackit = strdup(src->trackit);
1693 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1694 srv->uweight = srv->iweight = src->iweight;
1695
1696 srv->check.send_proxy = src->check.send_proxy;
1697 /* health: up, but will fall down at first failure */
1698 srv->check.rise = srv->check.health = src->check.rise;
1699 srv->check.fall = src->check.fall;
1700
1701 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001702 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1703 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1704 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001705 srv->check.state |= CHK_ST_PAUSED;
1706 srv->check.health = 0;
1707 }
1708
1709 /* health: up but will fall down at first failure */
1710 srv->agent.rise = srv->agent.health = src->agent.rise;
1711 srv->agent.fall = src->agent.fall;
1712
1713 if (src->resolvers_id != NULL)
1714 srv->resolvers_id = strdup(src->resolvers_id);
1715 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001716 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001717 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001718 if (srv->dns_opts.family_prio == AF_UNSPEC)
1719 srv->dns_opts.family_prio = AF_INET6;
1720 memcpy(srv->dns_opts.pref_net,
1721 src->dns_opts.pref_net,
1722 sizeof srv->dns_opts.pref_net);
1723 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1724
1725 srv->init_addr_methods = src->init_addr_methods;
1726 srv->init_addr = src->init_addr;
1727#if defined(USE_OPENSSL)
1728 srv_ssl_settings_cpy(srv, src);
1729#endif
1730#ifdef TCP_USER_TIMEOUT
1731 srv->tcp_ut = src->tcp_ut;
1732#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001733 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001734 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001735 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001736 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001737
Olivier Houchard8da5f982017-08-04 18:35:36 +02001738 if (srv_tmpl)
1739 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001740
1741 srv->check.via_socks4 = src->check.via_socks4;
1742 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001743}
1744
William Lallemand313bfd12018-10-26 14:47:32 +02001745struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001746{
1747 struct server *srv;
1748
1749 srv = calloc(1, sizeof *srv);
1750 if (!srv)
1751 return NULL;
1752
1753 srv->obj_type = OBJ_TYPE_SERVER;
1754 srv->proxy = proxy;
1755 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001756 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001757
Emeric Brun52a91d32017-08-31 14:41:55 +02001758 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001759 srv->last_change = now.tv_sec;
1760
Christopher Faulet38290462020-04-21 11:46:40 +02001761 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001762 srv->check.status = HCHK_STATUS_INI;
1763 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001764 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001765 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001766
Christopher Faulet38290462020-04-21 11:46:40 +02001767 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001768 srv->agent.status = HCHK_STATUS_INI;
1769 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001770 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001771 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1772
Willy Tarreau975b1552019-06-06 16:25:55 +02001773 /* please don't put default server settings here, they are set in
1774 * init_default_instance().
1775 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001776 return srv;
1777}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001778
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001779#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1780static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1781 struct server *srv, struct proxy *proxy)
1782{
1783 int ret;
1784 char *err = NULL;
1785
1786 if (!srv->sni_expr)
1787 return 0;
1788
1789 ret = server_parse_sni_expr(srv, proxy, &err);
1790 if (!ret)
1791 return 0;
1792
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001793 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001794 free(err);
1795
1796 return ret;
1797}
1798#endif
1799
1800/*
1801 * Server initializations finalization.
1802 * Initialize health check, agent check and SNI expression if enabled.
1803 * Must not be called for a default server instance.
1804 */
1805static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1806 struct server *srv, struct proxy *px)
1807{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001808#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001809 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001810#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001811
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001812 if (srv->do_check && srv->trackit) {
1813 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1814 file, linenum);
1815 return ERR_ALERT | ERR_FATAL;
1816 }
1817
1818 if (srv->do_agent && !srv->agent.port) {
1819 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1820 file, linenum, srv->id);
1821 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001822 }
1823
1824#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1825 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1826 return ret;
1827#endif
1828
1829 if (srv->flags & SRV_F_BACKUP)
1830 px->srv_bck++;
1831 else
1832 px->srv_act++;
1833 srv_lb_commit_status(srv);
1834
1835 return 0;
1836}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001837
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001838/*
1839 * Parse as much as possible such a range string argument: low[-high]
1840 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1841 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1842 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1843 * Returns 0 if succeeded, -1 if not.
1844 */
1845static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1846{
1847 char *nb_high_arg;
1848
1849 *nb_high = 0;
1850 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001851 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001852
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001854 *nb_high_arg++ = '\0';
1855 *nb_high = atoi(nb_high_arg);
1856 }
1857 else {
1858 *nb_high += *nb_low;
1859 *nb_low = 1;
1860 }
1861
1862 if (*nb_low < 0 || *nb_high < *nb_low)
1863 return -1;
1864
1865 return 0;
1866}
1867
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001868static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1869{
1870 chunk_printf(&trash, "%s%d", prefix, nb);
1871 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001872 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001873}
1874
1875/*
1876 * Initialize as much as possible servers from <srv> server template.
1877 * Note that a server template is a special server with
1878 * a few different parameters than a server which has
1879 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001880 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001881 * 'srv' template included.
1882 */
1883static int server_template_init(struct server *srv, struct proxy *px)
1884{
1885 int i;
1886 struct server *newsrv;
1887
1888 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 +02001889 newsrv = new_server(px);
1890 if (!newsrv)
1891 goto err;
1892
1893 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001894 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001895#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1896 if (newsrv->sni_expr) {
1897 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1898 if (!newsrv->ssl_ctx.sni)
1899 goto err;
1900 }
1901#endif
1902 /* Set this new server ID. */
1903 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1904
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001905 /* Linked backwards first. This will be restablished after parsing. */
1906 newsrv->next = px->srv;
1907 px->srv = newsrv;
1908 }
1909 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1910
1911 return i - srv->tmpl_info.nb_low;
1912
1913 err:
1914 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1915 if (newsrv) {
1916#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1917 release_sample_expr(newsrv->ssl_ctx.sni);
1918#endif
1919 free_check(&newsrv->agent);
1920 free_check(&newsrv->check);
1921 }
1922 free(newsrv);
1923 return i - srv->tmpl_info.nb_low;
1924}
1925
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001926int 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 +02001927{
1928 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001929 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001930 char *errmsg = NULL;
1931 int err_code = 0;
1932 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001933 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001934
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001935 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001936 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001937 !strcmp(args[0], "default-server") ||
1938 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001939 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001940 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001941 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001942 int srv_tmpl = !defsrv && !srv;
1943 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001944
1945 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001946 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001947 err_code |= ERR_ALERT | ERR_FATAL;
1948 goto out;
1949 }
1950 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001951 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001952
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001953 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001954 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001955 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001956 if (in_peers_section) {
1957 return 0;
1958 }
1959 else {
1960 /* 'server' line number of argument check. */
1961 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1962 file, linenum, args[0]);
1963 err_code |= ERR_ALERT | ERR_FATAL;
1964 goto out;
1965 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001966 }
1967
1968 err = invalid_char(args[1]);
1969 }
1970 else if (srv_tmpl) {
1971 if (!*args[3]) {
1972 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001973 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 +02001974 file, linenum, args[0]);
1975 err_code |= ERR_ALERT | ERR_FATAL;
1976 goto out;
1977 }
1978
1979 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001980 }
1981
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001982 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001983 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 +02001984 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 err_code |= ERR_ALERT | ERR_FATAL;
1986 goto out;
1987 }
1988
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001989 cur_arg = 2;
1990 if (srv_tmpl) {
1991 /* Parse server-template <nb | range> arg. */
1992 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001993 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001994 file, linenum, args[0], args[cur_arg]);
1995 err_code |= ERR_ALERT | ERR_FATAL;
1996 goto out;
1997 }
1998 cur_arg++;
1999 }
2000
Willy Tarreau272adea2014-03-31 10:39:59 +02002001 if (!defsrv) {
2002 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002003 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002004 struct protocol *proto;
2005
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002006 newsrv = new_server(curproxy);
2007 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002008 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002009 err_code |= ERR_ALERT | ERR_ABORT;
2010 goto out;
2011 }
2012
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002013 if (srv_tmpl) {
2014 newsrv->tmpl_info.nb_low = tmpl_range_low;
2015 newsrv->tmpl_info.nb_high = tmpl_range_high;
2016 }
2017
Willy Tarreau272adea2014-03-31 10:39:59 +02002018 /* the servers are linked backwards first */
2019 newsrv->next = curproxy->srv;
2020 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002021 newsrv->conf.file = strdup(file);
2022 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002023 /* Note: for a server template, its id is its prefix.
2024 * This is a temporary id which will be used for server allocations to come
2025 * after parsing.
2026 */
2027 if (srv)
2028 newsrv->id = strdup(args[1]);
2029 else
2030 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002031
2032 /* several ways to check the port component :
2033 * - IP => port=+0, relative (IPv4 only)
2034 * - IP: => port=+0, relative
2035 * - IP:N => port=N, absolute
2036 * - IP:+N => port=+N, relative
2037 * - IP:-N => port=-N, relative
2038 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002039 if (!parse_addr)
2040 goto skip_addr;
2041
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002042 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002043 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002044 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002045 err_code |= ERR_ALERT | ERR_FATAL;
2046 goto out;
2047 }
2048
2049 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002050 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002051 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002052 file, linenum, args[0], args[1]);
2053 err_code |= ERR_ALERT | ERR_FATAL;
2054 goto out;
2055 }
2056
2057 if (!port1 || !port2) {
2058 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002059 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002060 }
2061 else if (port1 != port2) {
2062 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002063 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002064 file, linenum, args[0], args[1], args[2]);
2065 err_code |= ERR_ALERT | ERR_FATAL;
2066 goto out;
2067 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002068
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002069 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002070 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002071 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002072 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002073 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2074 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2075 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002076 err_code |= ERR_ALERT | ERR_FATAL;
2077 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002078 }
2079 }
2080 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002081 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002082 file, linenum, newsrv->id);
2083 err_code |= ERR_ALERT | ERR_FATAL;
2084 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002085 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002086 }
2087
Willy Tarreau272adea2014-03-31 10:39:59 +02002088 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002089 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002090
Olivier Houchard8da5f982017-08-04 18:35:36 +02002091 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002092 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002093 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002094 err_code |= ERR_ALERT | ERR_FATAL;
2095 goto out;
2096 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002097
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002098 cur_arg++;
2099 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002100 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002101 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002102 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002103 } else {
2104 newsrv = &curproxy->defsrv;
2105 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002106 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002107 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002108 }
2109
2110 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002111 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002112 char *p, *end;
2113 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002114 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002115
2116 newsrv->init_addr_methods = 0;
2117 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2118
2119 for (p = args[cur_arg + 1]; *p; p = end) {
2120 /* cut on next comma */
2121 for (end = p; *end && *end != ','; end++);
2122 if (*end)
2123 *(end++) = 0;
2124
Willy Tarreau4310d362016-11-02 15:05:56 +01002125 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002126 if (!strcmp(p, "libc")) {
2127 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2128 }
2129 else if (!strcmp(p, "last")) {
2130 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2131 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002132 else if (!strcmp(p, "none")) {
2133 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2134 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002135 else if (str2ip2(p, &sa, 0)) {
2136 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002137 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002138 file, linenum, args[cur_arg], p);
2139 err_code |= ERR_ALERT | ERR_FATAL;
2140 goto out;
2141 }
2142 newsrv->init_addr = sa;
2143 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2144 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002145 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002146 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 +02002147 file, linenum, args[cur_arg], p);
2148 err_code |= ERR_ALERT | ERR_FATAL;
2149 goto out;
2150 }
2151 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002152 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002153 file, linenum, args[cur_arg], p);
2154 err_code |= ERR_ALERT | ERR_FATAL;
2155 goto out;
2156 }
2157 }
2158 cur_arg += 2;
2159 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002160 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002161 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002162 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2163 cur_arg += 2;
2164 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002165 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2166 char *p, *end;
2167
2168 for (p = args[cur_arg + 1]; *p; p = end) {
2169 /* cut on next comma */
2170 for (end = p; *end && *end != ','; end++);
2171 if (*end)
2172 *(end++) = 0;
2173
2174 if (!strcmp(p, "allow-dup-ip")) {
2175 newsrv->dns_opts.accept_duplicate_ip = 1;
2176 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002177 else if (!strcmp(p, "ignore-weight")) {
2178 newsrv->dns_opts.ignore_weight = 1;
2179 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002180 else if (!strcmp(p, "prevent-dup-ip")) {
2181 newsrv->dns_opts.accept_duplicate_ip = 0;
2182 }
2183 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002184 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 +02002185 file, linenum, args[cur_arg], p);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189 }
2190
2191 cur_arg += 2;
2192 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002193 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2194 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002195 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002196 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002197 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002198 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002199 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002200 file, linenum, args[cur_arg]);
2201 err_code |= ERR_ALERT | ERR_FATAL;
2202 goto out;
2203 }
2204 cur_arg += 2;
2205 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002206 else if (!strcmp(args[cur_arg], "resolve-net")) {
2207 char *p, *e;
2208 unsigned char mask;
2209 struct dns_options *opt;
2210
2211 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002212 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002213 file, linenum, args[cur_arg]);
2214 err_code |= ERR_ALERT | ERR_FATAL;
2215 goto out;
2216 }
2217
2218 opt = &newsrv->dns_opts;
2219
2220 /* Split arguments by comma, and convert it from ipv4 or ipv6
2221 * string network in in_addr or in6_addr.
2222 */
2223 p = args[cur_arg + 1];
2224 e = p;
2225 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002226 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002227 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002228 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002229 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2230 err_code |= ERR_ALERT | ERR_FATAL;
2231 goto out;
2232 }
2233 /* look for end or comma. */
2234 while (*e != ',' && *e != '\0')
2235 e++;
2236 if (*e == ',') {
2237 *e = '\0';
2238 e++;
2239 }
2240 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2241 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2242 /* Try to convert input string from ipv4 or ipv6 network. */
2243 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2244 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2245 &mask)) {
2246 /* Try to convert input string from ipv6 network. */
2247 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2248 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2249 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002250 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002251 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002252 file, linenum, args[cur_arg], p);
2253 err_code |= ERR_ALERT | ERR_FATAL;
2254 goto out;
2255 }
2256 opt->pref_net_nb++;
2257 p = e;
2258 }
2259
2260 cur_arg += 2;
2261 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002262 else if (!strcmp(args[cur_arg], "weight")) {
2263 int w;
2264 w = atol(args[cur_arg + 1]);
2265 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002266 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002267 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2268 err_code |= ERR_ALERT | ERR_FATAL;
2269 goto out;
2270 }
2271 newsrv->uweight = newsrv->iweight = w;
2272 cur_arg += 2;
2273 }
Emeric Brun97556472020-05-30 01:42:45 +02002274 else if (!strcmp(args[cur_arg], "log-proto")) {
2275 if (!strcmp(args[cur_arg + 1], "legacy"))
2276 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2277 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2278 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2279 else {
2280 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2281 "'octet-count' but got '%s'\n",
2282 file, linenum, args[cur_arg], args[cur_arg + 1]);
2283 err_code |= ERR_ALERT | ERR_FATAL;
2284 goto out;
2285 }
2286 cur_arg += 2;
2287 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 else if (!strcmp(args[cur_arg], "minconn")) {
2289 newsrv->minconn = atol(args[cur_arg + 1]);
2290 cur_arg += 2;
2291 }
2292 else if (!strcmp(args[cur_arg], "maxconn")) {
2293 newsrv->maxconn = atol(args[cur_arg + 1]);
2294 cur_arg += 2;
2295 }
2296 else if (!strcmp(args[cur_arg], "maxqueue")) {
2297 newsrv->maxqueue = atol(args[cur_arg + 1]);
2298 cur_arg += 2;
2299 }
2300 else if (!strcmp(args[cur_arg], "slowstart")) {
2301 /* slowstart is stored in seconds */
2302 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002303
2304 if (err == PARSE_TIME_OVER) {
2305 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2306 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2307 err_code |= ERR_ALERT | ERR_FATAL;
2308 goto out;
2309 }
2310 else if (err == PARSE_TIME_UNDER) {
2311 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2312 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2313 err_code |= ERR_ALERT | ERR_FATAL;
2314 goto out;
2315 }
2316 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002317 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002318 file, linenum, *err, newsrv->id);
2319 err_code |= ERR_ALERT | ERR_FATAL;
2320 goto out;
2321 }
2322 newsrv->slowstart = (val + 999) / 1000;
2323 cur_arg += 2;
2324 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002325 else if (!strcmp(args[cur_arg], "on-error")) {
2326 if (!strcmp(args[cur_arg + 1], "fastinter"))
2327 newsrv->onerror = HANA_ONERR_FASTINTER;
2328 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2329 newsrv->onerror = HANA_ONERR_FAILCHK;
2330 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2331 newsrv->onerror = HANA_ONERR_SUDDTH;
2332 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2333 newsrv->onerror = HANA_ONERR_MARKDWN;
2334 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002335 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002336 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2337 file, linenum, args[cur_arg], args[cur_arg + 1]);
2338 err_code |= ERR_ALERT | ERR_FATAL;
2339 goto out;
2340 }
2341
2342 cur_arg += 2;
2343 }
2344 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2345 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2346 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2347 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002348 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002349 file, linenum, args[cur_arg], args[cur_arg + 1]);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
2352 }
2353
2354 cur_arg += 2;
2355 }
2356 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2357 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2358 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2359 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002360 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002361 file, linenum, args[cur_arg], args[cur_arg + 1]);
2362 err_code |= ERR_ALERT | ERR_FATAL;
2363 goto out;
2364 }
2365
2366 cur_arg += 2;
2367 }
2368 else if (!strcmp(args[cur_arg], "error-limit")) {
2369 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002370 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002371 file, linenum, args[cur_arg]);
2372 err_code |= ERR_ALERT | ERR_FATAL;
2373 goto out;
2374 }
2375
2376 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2377
2378 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002379 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002380 file, linenum, args[cur_arg]);
2381 err_code |= ERR_ALERT | ERR_FATAL;
2382 goto out;
2383 }
2384 cur_arg += 2;
2385 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002386 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002387 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002388 file, linenum, "usesrc", "source");
2389 err_code |= ERR_ALERT | ERR_FATAL;
2390 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002391 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002392 else {
2393 static int srv_dumped;
2394 struct srv_kw *kw;
2395 char *err;
2396
2397 kw = srv_find_kw(args[cur_arg]);
2398 if (kw) {
2399 char *err = NULL;
2400 int code;
2401
2402 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002403 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 +02002404 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002405 if (kw->skip != -1)
2406 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002407 err_code |= ERR_ALERT | ERR_FATAL;
2408 goto out;
2409 }
2410
2411 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002412 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002413 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002414 if (kw->skip != -1)
2415 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002416 err_code |= ERR_ALERT;
2417 continue;
2418 }
2419
2420 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2421 err_code |= code;
2422
2423 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002424 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002425 if (code & ERR_FATAL) {
2426 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002427 if (kw->skip != -1)
2428 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002429 goto out;
2430 }
2431 }
2432 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002433 if (kw->skip != -1)
2434 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002435 continue;
2436 }
2437
2438 err = NULL;
2439 if (!srv_dumped) {
2440 srv_dump_kws(&err);
2441 indent_msg(&err, 4);
2442 srv_dumped = 1;
2443 }
2444
Christopher Faulet767a84b2017-11-24 16:50:31 +01002445 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002446 file, linenum, args[0], args[1], args[cur_arg],
2447 err ? " Registered keywords :" : "", err ? err : "");
2448 free(err);
2449
2450 err_code |= ERR_ALERT | ERR_FATAL;
2451 goto out;
2452 }
2453 }
2454
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002455 if (!defsrv)
2456 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2457 if (err_code & ERR_FATAL)
2458 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002459 if (srv_tmpl)
2460 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002461 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002462 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002463 return 0;
2464
2465 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002466 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002467 free(errmsg);
2468 return err_code;
2469}
2470
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002471/* Returns a pointer to the first server matching either id <id>.
2472 * NULL is returned if no match is found.
2473 * the lookup is performed in the backend <bk>
2474 */
2475struct server *server_find_by_id(struct proxy *bk, int id)
2476{
2477 struct eb32_node *eb32;
2478 struct server *curserver;
2479
2480 if (!bk || (id ==0))
2481 return NULL;
2482
2483 /* <bk> has no backend capabilities, so it can't have a server */
2484 if (!(bk->cap & PR_CAP_BE))
2485 return NULL;
2486
2487 curserver = NULL;
2488
2489 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2490 if (eb32)
2491 curserver = container_of(eb32, struct server, conf.id);
2492
2493 return curserver;
2494}
2495
2496/* Returns a pointer to the first server matching either name <name>, or id
2497 * if <name> starts with a '#'. NULL is returned if no match is found.
2498 * the lookup is performed in the backend <bk>
2499 */
2500struct server *server_find_by_name(struct proxy *bk, const char *name)
2501{
2502 struct server *curserver;
2503
2504 if (!bk || !name)
2505 return NULL;
2506
2507 /* <bk> has no backend capabilities, so it can't have a server */
2508 if (!(bk->cap & PR_CAP_BE))
2509 return NULL;
2510
2511 curserver = NULL;
2512 if (*name == '#') {
2513 curserver = server_find_by_id(bk, atoi(name + 1));
2514 if (curserver)
2515 return curserver;
2516 }
2517 else {
2518 curserver = bk->srv;
2519
2520 while (curserver && (strcmp(curserver->id, name) != 0))
2521 curserver = curserver->next;
2522
2523 if (curserver)
2524 return curserver;
2525 }
2526
2527 return NULL;
2528}
2529
2530struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2531{
2532 struct server *byname;
2533 struct server *byid;
2534
2535 if (!name && !id)
2536 return NULL;
2537
2538 if (diff)
2539 *diff = 0;
2540
2541 byname = byid = NULL;
2542
2543 if (name) {
2544 byname = server_find_by_name(bk, name);
2545 if (byname && (!id || byname->puid == id))
2546 return byname;
2547 }
2548
2549 /* remaining possibilities :
2550 * - name not set
2551 * - name set but not found
2552 * - name found but ID doesn't match
2553 */
2554 if (id) {
2555 byid = server_find_by_id(bk, id);
2556 if (byid) {
2557 if (byname) {
2558 /* use id only if forced by configuration */
2559 if (byid->flags & SRV_F_FORCED_ID) {
2560 if (diff)
2561 *diff |= 2;
2562 return byid;
2563 }
2564 else {
2565 if (diff)
2566 *diff |= 1;
2567 return byname;
2568 }
2569 }
2570
2571 /* remaining possibilities:
2572 * - name not set
2573 * - name set but not found
2574 */
2575 if (name && diff)
2576 *diff |= 2;
2577 return byid;
2578 }
2579
2580 /* id bot found */
2581 if (byname) {
2582 if (diff)
2583 *diff |= 1;
2584 return byname;
2585 }
2586 }
2587
2588 return NULL;
2589}
2590
Willy Tarreau46b7f532018-08-21 11:54:26 +02002591/* Update a server state using the parameters available in the params list.
2592 *
2593 * Grabs the server lock during operation.
2594 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002595static void srv_update_state(struct server *srv, int version, char **params)
2596{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002597 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002598 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002599
2600 /* fields since version 1
2601 * and common to all other upcoming versions
2602 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002603 enum srv_state srv_op_state;
2604 enum srv_admin srv_admin_state;
2605 unsigned srv_uweight, srv_iweight;
2606 unsigned long srv_last_time_change;
2607 short srv_check_status;
2608 enum chk_result srv_check_result;
2609 int srv_check_health;
2610 int srv_check_state, srv_agent_state;
2611 int bk_f_forced_id;
2612 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002613 int fqdn_set_by_cli;
2614 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002615 const char *port_str;
2616 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002617 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002618
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002619 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002620 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002621 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002622 switch (version) {
2623 case 1:
2624 /*
2625 * now we can proceed with server's state update:
2626 * srv_addr: params[0]
2627 * srv_op_state: params[1]
2628 * srv_admin_state: params[2]
2629 * srv_uweight: params[3]
2630 * srv_iweight: params[4]
2631 * srv_last_time_change: params[5]
2632 * srv_check_status: params[6]
2633 * srv_check_result: params[7]
2634 * srv_check_health: params[8]
2635 * srv_check_state: params[9]
2636 * srv_agent_state: params[10]
2637 * bk_f_forced_id: params[11]
2638 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002639 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002640 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002641 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002642 */
2643
2644 /* validating srv_op_state */
2645 p = NULL;
2646 errno = 0;
2647 srv_op_state = strtol(params[1], &p, 10);
2648 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2649 (srv_op_state != SRV_ST_STOPPED &&
2650 srv_op_state != SRV_ST_STARTING &&
2651 srv_op_state != SRV_ST_RUNNING &&
2652 srv_op_state != SRV_ST_STOPPING)) {
2653 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2654 }
2655
2656 /* validating srv_admin_state */
2657 p = NULL;
2658 errno = 0;
2659 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002660 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002661
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002662 /* inherited statuses will be recomputed later.
2663 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2664 */
2665 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002666
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002667 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2668 (srv_admin_state != 0 &&
2669 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002670 srv_admin_state != SRV_ADMF_CMAINT &&
2671 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002672 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002673 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002674 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2675 }
2676
2677 /* validating srv_uweight */
2678 p = NULL;
2679 errno = 0;
2680 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002681 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002682 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2683
2684 /* validating srv_iweight */
2685 p = NULL;
2686 errno = 0;
2687 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002688 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002689 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2690
2691 /* validating srv_last_time_change */
2692 p = NULL;
2693 errno = 0;
2694 srv_last_time_change = strtol(params[5], &p, 10);
2695 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2696 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2697
2698 /* validating srv_check_status */
2699 p = NULL;
2700 errno = 0;
2701 srv_check_status = strtol(params[6], &p, 10);
2702 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2703 (srv_check_status >= HCHK_STATUS_SIZE))
2704 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2705
2706 /* validating srv_check_result */
2707 p = NULL;
2708 errno = 0;
2709 srv_check_result = strtol(params[7], &p, 10);
2710 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2711 (srv_check_result != CHK_RES_UNKNOWN &&
2712 srv_check_result != CHK_RES_NEUTRAL &&
2713 srv_check_result != CHK_RES_FAILED &&
2714 srv_check_result != CHK_RES_PASSED &&
2715 srv_check_result != CHK_RES_CONDPASS)) {
2716 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2717 }
2718
2719 /* validating srv_check_health */
2720 p = NULL;
2721 errno = 0;
2722 srv_check_health = strtol(params[8], &p, 10);
2723 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2724 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2725
2726 /* validating srv_check_state */
2727 p = NULL;
2728 errno = 0;
2729 srv_check_state = strtol(params[9], &p, 10);
2730 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2731 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2732 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2733
2734 /* validating srv_agent_state */
2735 p = NULL;
2736 errno = 0;
2737 srv_agent_state = strtol(params[10], &p, 10);
2738 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2739 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2740 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2741
2742 /* validating bk_f_forced_id */
2743 p = NULL;
2744 errno = 0;
2745 bk_f_forced_id = strtol(params[11], &p, 10);
2746 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2747 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2748
2749 /* validating srv_f_forced_id */
2750 p = NULL;
2751 errno = 0;
2752 srv_f_forced_id = strtol(params[12], &p, 10);
2753 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2754 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2755
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002756 /* validating srv_fqdn */
2757 fqdn = params[13];
2758 if (fqdn && *fqdn == '-')
2759 fqdn = NULL;
2760 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2761 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2762 fqdn = NULL;
2763 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002764
Frédéric Lécaille31694712017-08-01 08:47:19 +02002765 port_str = params[14];
2766 if (port_str) {
2767 port = strl2uic(port_str, strlen(port_str));
2768 if (port > USHRT_MAX) {
2769 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2770 port_str = NULL;
2771 }
2772 }
2773
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002774 /* SRV record
2775 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2776 */
2777 srvrecord = params[15];
2778 if (srvrecord && *srvrecord != '_')
2779 srvrecord = NULL;
2780
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002781 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002782 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002783 goto out;
2784
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002785 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002786 /* recover operational state and apply it to this server
2787 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002788 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002789 switch (srv_op_state) {
2790 case SRV_ST_STOPPED:
2791 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002792 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002793 break;
2794 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002795 /* If rise == 1 there is no STARTING state, let's switch to
2796 * RUNNING
2797 */
2798 if (srv->check.rise == 1) {
2799 srv->check.health = srv->check.rise + srv->check.fall - 1;
2800 srv_set_running(srv, "", NULL);
2801 break;
2802 }
2803 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2804 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002805 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002806 break;
2807 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002808 /* If fall == 1 there is no STOPPING state, let's switch to
2809 * STOPPED
2810 */
2811 if (srv->check.fall == 1) {
2812 srv->check.health = 0;
2813 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2814 break;
2815 }
2816 if (srv->check.health < srv->check.rise ||
2817 srv->check.health > srv->check.rise + srv->check.fall - 2)
2818 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002819 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002820 break;
2821 case SRV_ST_RUNNING:
2822 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002823 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002824 break;
2825 }
2826
2827 /* When applying server state, the following rules apply:
2828 * - in case of a configuration change, we apply the setting from the new
2829 * configuration, regardless of old running state
2830 * - if no configuration change, we apply old running state only if old running
2831 * state is different from new configuration state
2832 */
2833 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002834 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2835 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002836 srv_adm_set_maint(srv);
2837 else
2838 srv_adm_set_ready(srv);
2839 }
2840 /* configuration is the same, let's compate old running state and new conf state */
2841 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002842 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002843 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002844 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002845 srv_adm_set_ready(srv);
2846 }
2847 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002848 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002849 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002850 * (srv->iweight is the weight set up in configuration).
2851 * There are two possible reasons for FDRAIN to have been present :
2852 * - previous config weight was zero
2853 * - "set server b/s drain" was sent to the CLI
2854 *
2855 * In the first case, we simply want to drop this drain state
2856 * if the new weight is not zero anymore, meaning the administrator
2857 * has intentionally turned the weight back to a positive value to
2858 * enable the server again after an operation. In the second case,
2859 * the drain state was forced on the CLI regardless of the config's
2860 * weight so we don't want a change to the config weight to lose this
2861 * status. What this means is :
2862 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2863 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002864 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002865 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002866 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002867 }
2868
2869 srv->last_change = date.tv_sec - srv_last_time_change;
2870 srv->check.status = srv_check_status;
2871 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002872
2873 /* Only case we want to apply is removing ENABLED flag which could have been
2874 * done by the "disable health" command over the stats socket
2875 */
2876 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2877 (srv_check_state & CHK_ST_CONFIGURED) &&
2878 !(srv_check_state & CHK_ST_ENABLED))
2879 srv->check.state &= ~CHK_ST_ENABLED;
2880
2881 /* Only case we want to apply is removing ENABLED flag which could have been
2882 * done by the "disable agent" command over the stats socket
2883 */
2884 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2885 (srv_agent_state & CHK_ST_CONFIGURED) &&
2886 !(srv_agent_state & CHK_ST_ENABLED))
2887 srv->agent.state &= ~CHK_ST_ENABLED;
2888
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002889 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2890 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002891 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002892 * It means that a configuration file change has precedence over a unix socket change
2893 * for server's weight
2894 *
2895 * by default, HAProxy applies the following weight when parsing the configuration
2896 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002897 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002898 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002899 srv->uweight = srv_uweight;
2900 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002901 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002902
Willy Tarreaue5a60682016-11-09 14:54:53 +01002903 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002904 if (strcmp(params[0], "-"))
2905 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002906
2907 if (fqdn && srv->hostname) {
2908 if (!strcmp(srv->hostname, fqdn)) {
2909 /* Here we reset the 'set from stats socket FQDN' flag
2910 * to support such transitions:
2911 * Let's say initial FQDN value is foo1 (in configuration file).
2912 * - FQDN changed from stats socket, from foo1 to foo2 value,
2913 * - FQDN changed again from file configuration (with the same previous value
2914 set from stats socket, from foo1 to foo2 value),
2915 * - reload for any other reason than a FQDN modification,
2916 * the configuration file FQDN matches the fqdn server state file value.
2917 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002918 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002919 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002920 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002921 }
2922 else {
2923 /* If the FDQN has been changed from stats socket,
2924 * apply fqdn state file value (which is the value set
2925 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002926 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002927 */
2928 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002929 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002930 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002931 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002932 }
2933 }
2934 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002935 /* If all the conditions below are validated, this means
2936 * we're evaluating a server managed by SRV resolution
2937 */
2938 else if (fqdn && !srv->hostname && srvrecord) {
2939 int res;
2940
2941 /* we can't apply previous state if SRV record has changed */
2942 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2943 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);
2944 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2945 goto out;
2946 }
2947
2948 /* create or find a SRV resolution for this srv record */
2949 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2950 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2951 if (srv->srvrq == NULL) {
2952 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2953 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2954 goto out;
2955 }
2956
2957 /* prepare DNS resolution for this server */
2958 res = srv_prepare_for_resolution(srv, fqdn);
2959 if (res == -1) {
2960 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2961 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2962 goto out;
2963 }
2964
2965 /* configure check.port accordingly */
2966 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2967 !(srv->flags & SRV_F_CHECKPORT))
2968 srv->check.port = port;
2969
2970 /* Unset SRV_F_MAPPORTS for SRV records.
2971 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2972 * because no ports are provided in the configuration file.
2973 * This is because HAProxy will use the port found into the SRV record.
2974 */
2975 srv->flags &= ~SRV_F_MAPPORTS;
2976 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002977
Frédéric Lécaille31694712017-08-01 08:47:19 +02002978 if (port_str)
2979 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002980 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002981
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002982 break;
2983 default:
2984 chunk_appendf(msg, ", version '%d' not supported", version);
2985 }
2986
2987 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002988 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002989 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002990 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002991 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002992 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002993}
2994
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002995
2996/*
2997 * read next line from file <f> and return the server state version if one found.
2998 * If no version is found, then 0 is returned
2999 * Note that this should be the first read on <f>
3000 */
3001static int srv_state_get_version(FILE *f) {
3002 char buf[2];
3003 int ret;
3004
3005 /* first character of first line of the file must contain the version of the export */
3006 if (fgets(buf, 2, f) == NULL) {
3007 return 0;
3008 }
3009
3010 ret = atoi(buf);
3011 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3012 (ret > SRV_STATE_FILE_VERSION_MAX))
3013 return 0;
3014
3015 return ret;
3016}
3017
3018
3019/*
3020 * parses server state line stored in <buf> and supposedly in version <version>.
3021 * Set <params> and <srv_params> accordingly.
3022 * In case of error, params[0] is set to NULL.
3023 */
3024static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3025{
3026 int buflen, arg, srv_arg;
3027 char *cur, *end;
3028
3029 buflen = strlen(buf);
3030 cur = buf;
3031 end = cur + buflen;
3032
3033 /* we need at least one character */
3034 if (buflen == 0) {
3035 params[0] = NULL;
3036 return;
3037 }
3038
3039 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003040 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003041 ++cur;
3042
3043 /* Ignore empty or commented lines */
3044 if (cur == end || *cur == '#') {
3045 params[0] = NULL;
3046 return;
3047 }
3048
3049 /* truncated lines */
3050 if (buf[buflen - 1] != '\n') {
3051 //ha_warning("server-state file '%s': truncated line\n", filepath);
3052 params[0] = NULL;
3053 return;
3054 }
3055
3056 /* Removes trailing '\n' */
3057 buf[buflen - 1] = '\0';
3058
3059 /* we're now ready to move the line into *srv_params[] */
3060 params[0] = cur;
3061 arg = 1;
3062 srv_arg = 0;
3063 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003064 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003065 *cur = '\0';
3066 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003067 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003068 ++cur;
3069 switch (version) {
3070 case 1:
3071 /*
3072 * srv_addr: params[4] => srv_params[0]
3073 * srv_op_state: params[5] => srv_params[1]
3074 * srv_admin_state: params[6] => srv_params[2]
3075 * srv_uweight: params[7] => srv_params[3]
3076 * srv_iweight: params[8] => srv_params[4]
3077 * srv_last_time_change: params[9] => srv_params[5]
3078 * srv_check_status: params[10] => srv_params[6]
3079 * srv_check_result: params[11] => srv_params[7]
3080 * srv_check_health: params[12] => srv_params[8]
3081 * srv_check_state: params[13] => srv_params[9]
3082 * srv_agent_state: params[14] => srv_params[10]
3083 * bk_f_forced_id: params[15] => srv_params[11]
3084 * srv_f_forced_id: params[16] => srv_params[12]
3085 * srv_fqdn: params[17] => srv_params[13]
3086 * srv_port: params[18] => srv_params[14]
3087 * srvrecord: params[19] => srv_params[15]
3088 */
3089 if (arg >= 4) {
3090 srv_params[srv_arg] = cur;
3091 ++srv_arg;
3092 }
3093 break;
3094 }
3095
3096 params[arg] = cur;
3097 ++arg;
3098 }
3099 else {
3100 ++cur;
3101 }
3102 }
3103
3104 /* if line is incomplete line, then ignore it.
3105 * otherwise, update useful flags */
3106 switch (version) {
3107 case 1:
3108 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3109 params[0] = NULL;
3110 return;
3111 }
3112 break;
3113 }
3114
3115 return;
3116}
3117
3118
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003119/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3120 * For each proxy, it does the following:
3121 * - opens its server state file (either one or local one)
3122 * - read whole file, line by line
3123 * - analyse each line to check if it matches our current backend:
3124 * - backend name matches
3125 * - backend id matches if id is forced and name doesn't match
3126 * - if the server pointed by the line is found, then state is applied
3127 *
3128 * If the running backend uuid or id differs from the state file, then HAProxy reports
3129 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003130 *
3131 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003132 */
3133void apply_server_state(void)
3134{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003135 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003136 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3137 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003138 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003139 FILE *f;
3140 char *filepath;
3141 char globalfilepath[MAXPATHLEN + 1];
3142 char localfilepath[MAXPATHLEN + 1];
3143 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003144 struct proxy *curproxy, *bk;
3145 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003146 char *line;
3147 char *bkname, *srvname;
3148 struct state_line *st;
3149 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003150
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003151
3152 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003153 globalfilepathlen = 0;
3154 /* create the globalfilepath variable */
3155 if (global.server_state_file) {
3156 /* absolute path or no base directory provided */
3157 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3158 len = strlen(global.server_state_file);
3159 if (len > MAXPATHLEN) {
3160 globalfilepathlen = 0;
3161 goto globalfileerror;
3162 }
3163 memcpy(globalfilepath, global.server_state_file, len);
3164 globalfilepath[len] = '\0';
3165 globalfilepathlen = len;
3166 }
3167 else if (global.server_state_base) {
3168 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003169 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 globalfilepathlen = 0;
3171 goto globalfileerror;
3172 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003173 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003174 globalfilepath[len] = 0;
3175 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003176
3177 /* append a slash if needed */
3178 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3179 if (globalfilepathlen + 1 > MAXPATHLEN) {
3180 globalfilepathlen = 0;
3181 goto globalfileerror;
3182 }
3183 globalfilepath[globalfilepathlen++] = '/';
3184 }
3185
3186 len = strlen(global.server_state_file);
3187 if (globalfilepathlen + len > MAXPATHLEN) {
3188 globalfilepathlen = 0;
3189 goto globalfileerror;
3190 }
3191 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3192 globalfilepathlen += len;
3193 globalfilepath[globalfilepathlen++] = 0;
3194 }
3195 }
3196 globalfileerror:
3197 if (globalfilepathlen == 0)
3198 globalfilepath[0] = '\0';
3199
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003200 /* Load global server state in a tree */
3201 if (globalfilepathlen > 0) {
3202 errno = 0;
3203 f = fopen(globalfilepath, "r");
3204 if (errno)
3205 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003206 if (!f)
3207 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003208
3209 global_file_version = srv_state_get_version(f);
3210 if (global_file_version == 0)
3211 goto out_load_server_state_in_tree;
3212
3213 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3214 line = NULL;
3215
3216 line = strdup(mybuf);
3217 if (line == NULL)
3218 continue;
3219
3220 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3221 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003222 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003223
3224 /* bkname */
3225 bkname = params[1];
3226 /* srvname */
3227 srvname = params[3];
3228
3229 /* key */
3230 chunk_printf(&trash, "%s %s", bkname, srvname);
3231
3232 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003233 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003234 if (st == NULL) {
3235 goto nextline;
3236 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003237 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003238 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3239 /* this is a duplicate key, probably a hand-crafted file,
3240 * drop it!
3241 */
3242 goto nextline;
3243 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003244
3245 /* save line */
3246 st->line = line;
3247
3248 continue;
3249
3250 nextline:
3251 /* free up memory in case of error during the processing of the line */
3252 free(line);
3253 }
3254 }
3255 out_load_server_state_in_tree:
3256
3257 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003258 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003259 /* servers are only in backends */
3260 if (!(curproxy->cap & PR_CAP_BE))
3261 continue;
3262 fileopenerr = 0;
3263 filepath = NULL;
3264
3265 /* search server state file path and name */
3266 switch (curproxy->load_server_state_from_file) {
3267 /* read servers state from global file */
3268 case PR_SRV_STATE_FILE_GLOBAL:
3269 /* there was an error while generating global server state file path */
3270 if (globalfilepathlen == 0)
3271 continue;
3272 filepath = globalfilepath;
3273 fileopenerr = 1;
3274 break;
3275 /* this backend has its own file */
3276 case PR_SRV_STATE_FILE_LOCAL:
3277 localfilepathlen = 0;
3278 localfilepath[0] = '\0';
3279 len = 0;
3280 /* create the localfilepath variable */
3281 /* absolute path or no base directory provided */
3282 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3283 len = strlen(curproxy->server_state_file_name);
3284 if (len > MAXPATHLEN) {
3285 localfilepathlen = 0;
3286 goto localfileerror;
3287 }
3288 memcpy(localfilepath, curproxy->server_state_file_name, len);
3289 localfilepath[len] = '\0';
3290 localfilepathlen = len;
3291 }
3292 else if (global.server_state_base) {
3293 len = strlen(global.server_state_base);
3294 localfilepathlen += len;
3295
3296 if (localfilepathlen > MAXPATHLEN) {
3297 localfilepathlen = 0;
3298 goto localfileerror;
3299 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003300 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003301 localfilepath[localfilepathlen] = 0;
3302
3303 /* append a slash if needed */
3304 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3305 if (localfilepathlen + 1 > MAXPATHLEN) {
3306 localfilepathlen = 0;
3307 goto localfileerror;
3308 }
3309 localfilepath[localfilepathlen++] = '/';
3310 }
3311
3312 len = strlen(curproxy->server_state_file_name);
3313 if (localfilepathlen + len > MAXPATHLEN) {
3314 localfilepathlen = 0;
3315 goto localfileerror;
3316 }
3317 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3318 localfilepathlen += len;
3319 localfilepath[localfilepathlen++] = 0;
3320 }
3321 filepath = localfilepath;
3322 localfileerror:
3323 if (localfilepathlen == 0)
3324 localfilepath[0] = '\0';
3325
3326 break;
3327 case PR_SRV_STATE_FILE_NONE:
3328 default:
3329 continue;
3330 }
3331
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003332 /* when global file is used, we get data from the tree
3333 * Note that in such case we don't check backend name neither uuid.
3334 * Backend name can't be wrong since it's used as a key to retrieve the server state
3335 * line from the tree.
3336 */
3337 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3338 struct server *srv = curproxy->srv;
3339 while (srv) {
3340 struct ebmb_node *node;
3341 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003342
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003343 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3344 node = ebst_lookup(&state_file, trash.area);
3345 if (!node)
3346 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003347
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003348 st = container_of(node, struct state_line, name_name);
3349 memcpy(mybuf, st->line, strlen(st->line));
3350 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003351
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003352 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3353 if (params[0] == NULL)
3354 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003355
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003356 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003357
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003358 next:
3359 srv = srv->next;
3360 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003361
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003362 continue; /* next proxy in list */
3363 }
3364 else {
3365 /* load 'local' state file */
3366 errno = 0;
3367 f = fopen(filepath, "r");
3368 if (errno && fileopenerr)
3369 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3370 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003371 continue;
3372
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003373 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003374
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003375 /* first character of first line of the file must contain the version of the export */
3376 version = srv_state_get_version(f);
3377 if (version == 0) {
3378 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3379 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003380 }
3381
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003382 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3383 int bk_f_forced_id = 0;
3384 int check_id = 0;
3385 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003386
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003387 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003388
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003389 if (params[0] == NULL) {
3390 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003391 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003392
3393 /* if line is incomplete line, then ignore it.
3394 * otherwise, update useful flags */
3395 switch (version) {
3396 case 1:
3397 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3398 check_id = (atoi(params[0]) == curproxy->uuid);
3399 check_name = (strcmp(curproxy->id, params[1]) == 0);
3400 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003401 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003402
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003403 bk = curproxy;
3404
3405 /* if backend can't be found, let's continue */
3406 if (!check_id && !check_name)
3407 continue;
3408 else if (!check_id && check_name) {
3409 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3410 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3411 }
3412 else if (check_id && !check_name) {
3413 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3414 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3415 /* if name doesn't match, we still want to update curproxy if the backend id
3416 * was forced in previous the previous configuration */
3417 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003418 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003419 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003420
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003421 /* look for the server by its name: param[3] */
3422 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003424 if (!srv) {
3425 /* if no server found, then warning and continue with next line */
3426 ha_warning("can't find server '%s' in backend '%s'\n",
3427 params[3], params[1]);
3428 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3429 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003430 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003431 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003432
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003433 /* now we can proceed with server's state update */
3434 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003435 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003436 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003437fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003438 fclose(f);
3439 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003440
3441 /* now free memory allocated for the tree */
3442 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3443 node;
3444 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3445 st = container_of(node, struct state_line, name_name);
3446 ebmb_delete(&st->name_name);
3447 free(st->line);
3448 free(st);
3449 }
3450
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003451}
3452
Simon Horman7d09b9a2013-02-12 10:45:51 +09003453/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003454 * update a server's current IP address.
3455 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3456 * ip is in network format.
3457 * updater is a string which contains an information about the requester of the update.
3458 * updater is used if not NULL.
3459 *
3460 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003461 *
3462 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003463 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003464int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003465{
3466 /* generates a log line and a warning on stderr */
3467 if (1) {
3468 /* book enough space for both IPv4 and IPv6 */
3469 char oldip[INET6_ADDRSTRLEN];
3470 char newip[INET6_ADDRSTRLEN];
3471
3472 memset(oldip, '\0', INET6_ADDRSTRLEN);
3473 memset(newip, '\0', INET6_ADDRSTRLEN);
3474
3475 /* copy old IP address in a string */
3476 switch (s->addr.ss_family) {
3477 case AF_INET:
3478 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3479 break;
3480 case AF_INET6:
3481 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3482 break;
3483 };
3484
3485 /* copy new IP address in a string */
3486 switch (ip_sin_family) {
3487 case AF_INET:
3488 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3489 break;
3490 case AF_INET6:
3491 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3492 break;
3493 };
3494
3495 /* save log line into a buffer */
3496 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3497 s->proxy->id, s->id, oldip, newip, updater);
3498
3499 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003500 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003501
3502 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003503 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003504 }
3505
3506 /* save the new IP family */
3507 s->addr.ss_family = ip_sin_family;
3508 /* save the new IP address */
3509 switch (ip_sin_family) {
3510 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003511 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003512 break;
3513 case AF_INET6:
3514 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3515 break;
3516 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003517 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003518
3519 return 0;
3520}
3521
3522/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003523 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3524 *
3525 * Caller can pass its name through <updater> to get it integrated in the response message
3526 * returned by the function.
3527 *
3528 * The function first does the following, in that order:
3529 * - validates the new addr and/or port
3530 * - checks if an update is required (new IP or port is different than current ones)
3531 * - checks the update is allowed:
3532 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3533 * - allow all changes if no CHECKS are configured
3534 * - if CHECK is configured:
3535 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3536 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3537 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003538 *
3539 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003540 */
3541const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3542{
3543 struct sockaddr_storage sa;
3544 int ret, port_change_required;
3545 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003546 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003547 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003548 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003549
3550 msg = get_trash_chunk();
3551 chunk_reset(msg);
3552
3553 if (addr) {
3554 memset(&sa, 0, sizeof(struct sockaddr_storage));
3555 if (str2ip2(addr, &sa, 0) == NULL) {
3556 chunk_printf(msg, "Invalid addr '%s'", addr);
3557 goto out;
3558 }
3559
3560 /* changes are allowed on AF_INET* families only */
3561 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3562 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3563 goto out;
3564 }
3565
3566 /* collecting data currently setup */
3567 memset(current_addr, '\0', sizeof(current_addr));
3568 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3569 /* changes are allowed on AF_INET* families only */
3570 if ((ret != AF_INET) && (ret != AF_INET6)) {
3571 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3572 goto out;
3573 }
3574
3575 /* applying ADDR changes if required and allowed
3576 * ipcmp returns 0 when both ADDR are the same
3577 */
3578 if (ipcmp(&s->addr, &sa) == 0) {
3579 chunk_appendf(msg, "no need to change the addr");
3580 goto port;
3581 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003582 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003583 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003584
3585 /* we also need to update check's ADDR only if it uses the server's one */
3586 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003587 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003588 }
3589
3590 /* we also need to update agent ADDR only if it use the server's one */
3591 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003592 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003593 }
3594
3595 /* update report for caller */
3596 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3597 }
3598
3599 port:
3600 if (port) {
3601 char sign = '\0';
3602 char *endptr;
3603
3604 if (addr)
3605 chunk_appendf(msg, ", ");
3606
3607 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003608 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003609
3610 /* check if PORT change is required */
3611 port_change_required = 0;
3612
3613 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003614 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003615 new_port = strtol(port, &endptr, 10);
3616 if ((errno != 0) || (port == endptr)) {
3617 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3618 goto out;
3619 }
3620
3621 /* check if caller triggers a port mapped or offset */
3622 if (sign == '-' || (sign == '+')) {
3623 /* check if server currently uses port map */
3624 if (!(s->flags & SRV_F_MAPPORTS)) {
3625 /* switch from fixed port to port map mandatorily triggers
3626 * a port change */
3627 port_change_required = 1;
3628 /* check is configured
3629 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3630 * prevent PORT change if check doesn't have it's dedicated port while switching
3631 * to port mapping */
3632 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3633 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.");
3634 goto out;
3635 }
3636 }
3637 /* we're already using port maps */
3638 else {
3639 port_change_required = current_port != new_port;
3640 }
3641 }
3642 /* fixed port */
3643 else {
3644 port_change_required = current_port != new_port;
3645 }
3646
3647 /* applying PORT changes if required and update response message */
3648 if (port_change_required) {
3649 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003650 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003651 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003652
3653 /* prepare message */
3654 chunk_appendf(msg, "port changed from '");
3655 if (s->flags & SRV_F_MAPPORTS)
3656 chunk_appendf(msg, "+");
3657 chunk_appendf(msg, "%d' to '", current_port);
3658
3659 if (sign == '-') {
3660 s->flags |= SRV_F_MAPPORTS;
3661 chunk_appendf(msg, "%c", sign);
3662 /* just use for result output */
3663 new_port = -new_port;
3664 }
3665 else if (sign == '+') {
3666 s->flags |= SRV_F_MAPPORTS;
3667 chunk_appendf(msg, "%c", sign);
3668 }
3669 else {
3670 s->flags &= ~SRV_F_MAPPORTS;
3671 }
3672
3673 chunk_appendf(msg, "%d'", new_port);
3674
3675 /* we also need to update health checks port only if it uses server's realport */
3676 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3677 s->check.port = new_port;
3678 }
3679 }
3680 else {
3681 chunk_appendf(msg, "no need to change the port");
3682 }
3683 }
3684
3685out:
William Dauchy6318d332020-05-02 21:52:36 +02003686 if (changed) {
3687 /* force connection cleanup on the given server */
3688 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003689 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003690 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003691 if (updater)
3692 chunk_appendf(msg, " by '%s'", updater);
3693 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003694 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003695}
3696
3697
3698/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003699 * update server status based on result of name resolution
3700 * returns:
3701 * 0 if server status is updated
3702 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003703 *
3704 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003705 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003706int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003707{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003708 struct dns_resolvers *resolvers = s->resolvers;
3709 struct dns_resolution *resolution = s->dns_requester->resolution;
3710 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003711
3712 switch (resolution->status) {
3713 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003714 /* status when HAProxy has just (re)started.
3715 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716 break;
3717
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003718 case RSLV_STATUS_VALID:
3719 /*
3720 * resume health checks
3721 * server will be turned back on if health check is safe
3722 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003723 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003724 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003725 return 1;
3726 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3727 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003728 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003729 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003730
Emeric Brun52a91d32017-08-31 14:41:55 +02003731 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003732 return 1;
3733 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3734 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3735 s->proxy->id, s->id);
3736
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003737 ha_warning("%s.\n", trash.area);
3738 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003739 return 0;
3740
3741 case RSLV_STATUS_NX:
3742 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003743 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3744 if (!tick_is_expired(exp, now_ms))
3745 break;
3746
3747 if (s->next_admin & SRV_ADMF_RMAINT)
3748 return 1;
3749 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3750 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003751
3752 case RSLV_STATUS_TIMEOUT:
3753 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003754 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3755 if (!tick_is_expired(exp, now_ms))
3756 break;
3757
3758 if (s->next_admin & SRV_ADMF_RMAINT)
3759 return 1;
3760 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3761 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003762
3763 case RSLV_STATUS_REFUSED:
3764 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003765 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3766 if (!tick_is_expired(exp, now_ms))
3767 break;
3768
3769 if (s->next_admin & SRV_ADMF_RMAINT)
3770 return 1;
3771 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3772 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003773
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003774 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003775 /* stop server if resolution failed for a long enough period */
3776 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3777 if (!tick_is_expired(exp, now_ms))
3778 break;
3779
3780 if (s->next_admin & SRV_ADMF_RMAINT)
3781 return 1;
3782 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3783 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003784 }
3785
3786 return 1;
3787}
3788
3789/*
3790 * Server Name Resolution valid response callback
3791 * It expects:
3792 * - <nameserver>: the name server which answered the valid response
3793 * - <response>: buffer containing a valid DNS response
3794 * - <response_len>: size of <response>
3795 * It performs the following actions:
3796 * - ignore response if current ip found and server family not met
3797 * - update with first new ip found if family is met and current IP is not found
3798 * returns:
3799 * 0 on error
3800 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003801 *
3802 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003803 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003804int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003805{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003806 struct server *s = NULL;
3807 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808 void *serverip, *firstip;
3809 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003810 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003811 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003812 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003813
Christopher Faulet67957bd2017-09-27 11:00:59 +02003814 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003815 if (!s)
3816 return 1;
3817
Christopher Faulet67957bd2017-09-27 11:00:59 +02003818 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003819
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003820 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003821 firstip = NULL; /* pointer to the first valid response found */
3822 /* it will be used as the new IP if a change is required */
3823 firstip_sin_family = AF_UNSPEC;
3824 serverip = NULL; /* current server IP address */
3825
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003826 /* initializing server IP pointer */
3827 server_sin_family = s->addr.ss_family;
3828 switch (server_sin_family) {
3829 case AF_INET:
3830 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3831 break;
3832
3833 case AF_INET6:
3834 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3835 break;
3836
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003837 case AF_UNSPEC:
3838 break;
3839
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003840 default:
3841 goto invalid;
3842 }
3843
Baptiste Assmann729c9012017-05-22 15:13:10 +02003844 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003845 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003846 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003847
3848 switch (ret) {
3849 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003850 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003851
3852 case DNS_UPD_SRVIP_NOT_FOUND:
3853 goto save_ip;
3854
3855 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003856 goto invalid;
3857
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003858 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003859 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003860 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003861
Baptiste Assmannfad03182015-10-28 02:03:32 +01003862 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003863 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003864 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003865 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003866
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003867 default:
3868 goto invalid;
3869
3870 }
3871
3872 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003873 if (nameserver) {
3874 nameserver->counters.update++;
3875 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003876 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003877 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003878 else
3879 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003880 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003881
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003882 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003883 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003884 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003885
3886 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003887 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003888 nameserver->counters.invalid++;
3889 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003890 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003891 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003892 return 0;
3893}
3894
3895/*
3896 * Server Name Resolution error management callback
3897 * returns:
3898 * 0 on error
3899 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003900 *
3901 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003902 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003903int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003904{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003905 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003906
Christopher Faulet67957bd2017-09-27 11:00:59 +02003907 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003908 if (!s)
3909 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003910 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003911 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003912 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003913 return 1;
3914}
3915
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003916/*
3917 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003918 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003919 * It returns a pointer to the first server found or NULL if <ip> is not yet
3920 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003921 *
3922 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003923 */
3924struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3925{
3926 struct server *tmpsrv;
3927 struct proxy *be;
3928
3929 if (!srv)
3930 return NULL;
3931
3932 be = srv->proxy;
3933 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003934 /* we found the current server is the same, ignore it */
3935 if (srv == tmpsrv)
3936 continue;
3937
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003938 /* We want to compare the IP in the record with the IP of the servers in the
3939 * same backend, only if:
3940 * * DNS resolution is enabled on the server
3941 * * the hostname used for the resolution by our server is the same than the
3942 * one used for the server found in the backend
3943 * * the server found in the backend is not our current server
3944 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003945 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003946 if ((tmpsrv->hostname_dn == NULL) ||
3947 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3948 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003949 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003950 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003951 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003952 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003953
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003954 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003955 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003956 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003957 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003958 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003959
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003960 /* At this point, we have 2 different servers using the same DNS hostname
3961 * for their respective resolution.
3962 */
3963 if (*ip_family == tmpsrv->addr.ss_family &&
3964 ((tmpsrv->addr.ss_family == AF_INET &&
3965 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3966 (tmpsrv->addr.ss_family == AF_INET6 &&
3967 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003968 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003969 return tmpsrv;
3970 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003971 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003972 }
3973
Emeric Brune9fd6b52017-11-02 17:20:39 +01003974
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003975 return NULL;
3976}
3977
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003978/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3979 * resolver. This is suited for initial address configuration. Returns 0 on
3980 * success otherwise a non-zero error code. In case of error, *err_code, if
3981 * not NULL, is filled up.
3982 */
3983int srv_set_addr_via_libc(struct server *srv, int *err_code)
3984{
3985 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003986 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003987 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003988 return 1;
3989 }
3990 return 0;
3991}
3992
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003993/* Set the server's FDQN (->hostname) from <hostname>.
3994 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003995 *
3996 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003997 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003998int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003999{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004000 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004001 char *hostname_dn;
4002 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004003
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004004 /* Note that the server lock is already held. */
4005 if (!srv->resolvers)
4006 return -1;
4007
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004008 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004009 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004010 /* run time DNS resolution was not active for this server
4011 * and we can't enable it at run time for now.
4012 */
4013 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004014 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004015
4016 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004017 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004018 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004019 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4020 hostname_dn, trash.size);
4021 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004022 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004023
Christopher Faulet67957bd2017-09-27 11:00:59 +02004024 resolution = srv->dns_requester->resolution;
4025 if (resolution &&
4026 resolution->hostname_dn &&
4027 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004028 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004029
Christopher Faulet67957bd2017-09-27 11:00:59 +02004030 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004031
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004032 free(srv->hostname);
4033 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004034 srv->hostname = strdup(hostname);
4035 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004036 srv->hostname_dn_len = hostname_dn_len;
4037 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004038 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004039
Baptiste Assmann13a92322019-06-07 09:40:55 +02004040 if (srv->flags & SRV_F_NO_RESOLUTION)
4041 goto end;
4042
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004043 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004044 goto err;
4045
4046 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004047 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004048 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004049 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004050
4051 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004052 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004053 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004054 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004055}
4056
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004057/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4058 * from the state file. This is suited for initial address configuration.
4059 * Returns 0 on success otherwise a non-zero error code. In case of error,
4060 * *err_code, if not NULL, is filled up.
4061 */
4062static int srv_apply_lastaddr(struct server *srv, int *err_code)
4063{
4064 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4065 if (err_code)
4066 *err_code |= ERR_WARN;
4067 return 1;
4068 }
4069 return 0;
4070}
4071
Willy Tarreau25e51522016-11-04 15:10:17 +01004072/* returns 0 if no error, otherwise a combination of ERR_* flags */
4073static int srv_iterate_initaddr(struct server *srv)
4074{
4075 int return_code = 0;
4076 int err_code;
4077 unsigned int methods;
4078
4079 methods = srv->init_addr_methods;
4080 if (!methods) { // default to "last,libc"
4081 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4082 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4083 }
4084
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004085 /* "-dr" : always append "none" so that server addresses resolution
4086 * failures are silently ignored, this is convenient to validate some
4087 * configs out of their environment.
4088 */
4089 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4090 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4091
Willy Tarreau25e51522016-11-04 15:10:17 +01004092 while (methods) {
4093 err_code = 0;
4094 switch (srv_get_next_initaddr(&methods)) {
4095 case SRV_IADDR_LAST:
4096 if (!srv->lastaddr)
4097 continue;
4098 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004099 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004100 return_code |= err_code;
4101 break;
4102
4103 case SRV_IADDR_LIBC:
4104 if (!srv->hostname)
4105 continue;
4106 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004107 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004108 return_code |= err_code;
4109 break;
4110
Willy Tarreau37ebe122016-11-04 15:17:58 +01004111 case SRV_IADDR_NONE:
4112 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004113 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004114 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4115 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004116 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004117 return return_code;
4118
Willy Tarreau4310d362016-11-02 15:05:56 +01004119 case SRV_IADDR_IP:
4120 ipcpy(&srv->init_addr, &srv->addr);
4121 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004122 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4123 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004124 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004125 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004126
Willy Tarreau25e51522016-11-04 15:10:17 +01004127 default: /* unhandled method */
4128 break;
4129 }
4130 }
4131
4132 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004133 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004134 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4135 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004136 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004137 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004138 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4139 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004140
4141 return_code |= ERR_ALERT | ERR_FATAL;
4142 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004143out:
4144 srv_set_dyncookie(srv);
4145 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004146}
4147
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004148/*
4149 * This function parses all backends and all servers within each backend
4150 * and performs servers' addr resolution based on information provided by:
4151 * - configuration file
4152 * - server-state file (states provided by an 'old' haproxy process)
4153 *
4154 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4155 */
4156int srv_init_addr(void)
4157{
4158 struct proxy *curproxy;
4159 int return_code = 0;
4160
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004161 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004162 while (curproxy) {
4163 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004164
4165 /* servers are in backend only */
4166 if (!(curproxy->cap & PR_CAP_BE))
4167 goto srv_init_addr_next;
4168
Willy Tarreau25e51522016-11-04 15:10:17 +01004169 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004170 if (srv->hostname)
4171 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004172
4173 srv_init_addr_next:
4174 curproxy = curproxy->next;
4175 }
4176
4177 return return_code;
4178}
4179
Willy Tarreau46b7f532018-08-21 11:54:26 +02004180/*
4181 * Must be called with the server lock held.
4182 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004183const 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 +02004184{
4185
Willy Tarreau83061a82018-07-13 11:56:34 +02004186 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004187
4188 msg = get_trash_chunk();
4189 chunk_reset(msg);
4190
Olivier Houchard8da5f982017-08-04 18:35:36 +02004191 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004192 chunk_appendf(msg, "no need to change the FDQN");
4193 goto out;
4194 }
4195
4196 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4197 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4198 goto out;
4199 }
4200
4201 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4202 server->proxy->id, server->id, server->hostname, fqdn);
4203
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004204 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004205 chunk_reset(msg);
4206 chunk_appendf(msg, "could not update %s/%s FQDN",
4207 server->proxy->id, server->id);
4208 goto out;
4209 }
4210
4211 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004212 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004213
4214 out:
4215 if (updater)
4216 chunk_appendf(msg, " by '%s'", updater);
4217 chunk_appendf(msg, "\n");
4218
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004219 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004220}
4221
4222
Willy Tarreau21b069d2016-11-23 17:15:08 +01004223/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4224 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004225 * 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 +01004226 * used for CLI commands requiring a server name.
4227 * Important: the <arg> is modified to remove the '/'.
4228 */
4229struct server *cli_find_server(struct appctx *appctx, char *arg)
4230{
4231 struct proxy *px;
4232 struct server *sv;
4233 char *line;
4234
4235 /* split "backend/server" and make <line> point to server */
4236 for (line = arg; *line; line++)
4237 if (*line == '/') {
4238 *line++ = '\0';
4239 break;
4240 }
4241
4242 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004243 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004244 return NULL;
4245 }
4246
4247 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004248 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004249 return NULL;
4250 }
4251
4252 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004253 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004254 return NULL;
4255 }
4256
4257 return sv;
4258}
4259
William Lallemand222baf22016-11-19 02:00:33 +01004260
Willy Tarreau46b7f532018-08-21 11:54:26 +02004261/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004262static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004263{
4264 struct server *sv;
4265 const char *warning;
4266
4267 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4268 return 1;
4269
4270 sv = cli_find_server(appctx, args[2]);
4271 if (!sv)
4272 return 1;
4273
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004274 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004275
William Lallemand222baf22016-11-19 02:00:33 +01004276 if (strcmp(args[3], "weight") == 0) {
4277 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004278 if (warning)
4279 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004280 }
4281 else if (strcmp(args[3], "state") == 0) {
4282 if (strcmp(args[4], "ready") == 0)
4283 srv_adm_set_ready(sv);
4284 else if (strcmp(args[4], "drain") == 0)
4285 srv_adm_set_drain(sv);
4286 else if (strcmp(args[4], "maint") == 0)
4287 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004288 else
4289 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004290 }
4291 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004292 if (sv->track)
4293 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004294 else if (strcmp(args[4], "up") == 0) {
4295 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004296 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004297 }
4298 else if (strcmp(args[4], "stopping") == 0) {
4299 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004300 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004301 }
4302 else if (strcmp(args[4], "down") == 0) {
4303 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004304 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004305 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004306 else
4307 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004308 }
4309 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004310 if (!(sv->agent.state & CHK_ST_ENABLED))
4311 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004312 else if (strcmp(args[4], "up") == 0) {
4313 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004314 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004315 }
4316 else if (strcmp(args[4], "down") == 0) {
4317 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004318 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004319 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004320 else
4321 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004322 }
Misiek2da082d2017-01-09 09:40:42 +01004323 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004324 if (!(sv->agent.state & CHK_ST_ENABLED))
4325 cli_err(appctx, "agent checks are not enabled on this server.\n");
4326 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4327 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004328 }
4329 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004330 if (!(sv->agent.state & CHK_ST_ENABLED))
4331 cli_err(appctx, "agent checks are not enabled on this server.\n");
4332 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004333 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004334 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004335 }
4336 }
William Lallemand222baf22016-11-19 02:00:33 +01004337 else if (strcmp(args[3], "check-port") == 0) {
4338 int i = 0;
4339 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004340 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004341 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004342 }
4343 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004344 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004345 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004346 }
4347 /* prevent the update of port to 0 if MAPPORTS are in use */
4348 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004349 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004350 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004351 }
4352 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004353 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004354 }
4355 else if (strcmp(args[3], "addr") == 0) {
4356 char *addr = NULL;
4357 char *port = NULL;
4358 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004359 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004360 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004361 }
4362 else {
4363 addr = args[4];
4364 }
4365 if (strcmp(args[5], "port") == 0) {
4366 port = args[6];
4367 }
4368 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004369 if (warning)
4370 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004371 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4372 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004373 else if (strcmp(args[3], "fqdn") == 0) {
4374 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004375 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004376 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004377 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004378 /* ensure runtime resolver will process this new fqdn */
4379 if (sv->flags & SRV_F_NO_RESOLUTION) {
4380 sv->flags &= ~SRV_F_NO_RESOLUTION;
4381 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004382 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004383 if (warning)
4384 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004385 }
William Lallemand222baf22016-11-19 02:00:33 +01004386 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004387 cli_err(appctx,
4388 "'set server <srv>' only supports 'agent', 'health', 'state',"
4389 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004390 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004391 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004392 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004393 return 1;
4394}
4395
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004396static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004397{
4398 struct stream_interface *si = appctx->owner;
4399 struct proxy *px;
4400 struct server *sv;
4401 char *line;
4402
4403
4404 /* split "backend/server" and make <line> point to server */
4405 for (line = args[2]; *line; line++)
4406 if (*line == '/') {
4407 *line++ = '\0';
4408 break;
4409 }
4410
Willy Tarreau9d008692019-08-09 11:21:01 +02004411 if (!*line)
4412 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004413
Willy Tarreau9d008692019-08-09 11:21:01 +02004414 if (!get_backend_server(args[2], line, &px, &sv))
4415 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004416
4417 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004418 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4419 sv->iweight);
4420 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004421 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004422 return 0;
4423 }
William Lallemand6b160942016-11-22 12:34:35 +01004424 return 1;
4425}
4426
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004427/* Parse a "set weight" command.
4428 *
4429 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004430 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004431static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004432{
4433 struct server *sv;
4434 const char *warning;
4435
4436 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4437 return 1;
4438
4439 sv = cli_find_server(appctx, args[2]);
4440 if (!sv)
4441 return 1;
4442
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004443 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4444
William Lallemand6b160942016-11-22 12:34:35 +01004445 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004446 if (warning)
4447 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004448
4449 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4450
William Lallemand6b160942016-11-22 12:34:35 +01004451 return 1;
4452}
4453
Willy Tarreau46b7f532018-08-21 11:54:26 +02004454/* parse a "set maxconn server" command. It always returns 1.
4455 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004456 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004457 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004458static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004459{
4460 struct server *sv;
4461 const char *warning;
4462
4463 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4464 return 1;
4465
4466 sv = cli_find_server(appctx, args[3]);
4467 if (!sv)
4468 return 1;
4469
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004470 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4471
Willy Tarreaub8026272016-11-23 11:26:56 +01004472 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004473 if (warning)
4474 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004475
4476 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4477
Willy Tarreaub8026272016-11-23 11:26:56 +01004478 return 1;
4479}
William Lallemand6b160942016-11-22 12:34:35 +01004480
Willy Tarreau46b7f532018-08-21 11:54:26 +02004481/* parse a "disable agent" command. It always returns 1.
4482 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004483 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004484 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004485static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004486{
4487 struct server *sv;
4488
4489 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4490 return 1;
4491
4492 sv = cli_find_server(appctx, args[2]);
4493 if (!sv)
4494 return 1;
4495
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004496 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004497 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004498 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004499 return 1;
4500}
4501
Willy Tarreau46b7f532018-08-21 11:54:26 +02004502/* parse a "disable health" command. It always returns 1.
4503 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004504 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004505 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004506static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004507{
4508 struct server *sv;
4509
4510 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4511 return 1;
4512
4513 sv = cli_find_server(appctx, args[2]);
4514 if (!sv)
4515 return 1;
4516
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004517 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004518 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004519 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004520 return 1;
4521}
4522
Willy Tarreau46b7f532018-08-21 11:54:26 +02004523/* parse a "disable server" command. It always returns 1.
4524 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004525 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004526 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004527static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004528{
4529 struct server *sv;
4530
4531 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4532 return 1;
4533
4534 sv = cli_find_server(appctx, args[2]);
4535 if (!sv)
4536 return 1;
4537
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004538 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004539 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004540 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004541 return 1;
4542}
4543
Willy Tarreau46b7f532018-08-21 11:54:26 +02004544/* parse a "enable agent" command. It always returns 1.
4545 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004546 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004547 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004548static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004549{
4550 struct server *sv;
4551
4552 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4553 return 1;
4554
4555 sv = cli_find_server(appctx, args[2]);
4556 if (!sv)
4557 return 1;
4558
Willy Tarreau9d008692019-08-09 11:21:01 +02004559 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4560 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004561
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004562 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004563 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004564 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004565 return 1;
4566}
4567
Willy Tarreau46b7f532018-08-21 11:54:26 +02004568/* parse a "enable health" command. It always returns 1.
4569 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004570 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004572static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004573{
4574 struct server *sv;
4575
4576 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4577 return 1;
4578
4579 sv = cli_find_server(appctx, args[2]);
4580 if (!sv)
4581 return 1;
4582
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004583 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004584 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004585 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004586 return 1;
4587}
4588
Willy Tarreau46b7f532018-08-21 11:54:26 +02004589/* parse a "enable server" command. It always returns 1.
4590 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004591 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004592 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004593static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004594{
4595 struct server *sv;
4596
4597 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4598 return 1;
4599
4600 sv = cli_find_server(appctx, args[2]);
4601 if (!sv)
4602 return 1;
4603
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004604 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004605 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004606 if (!(sv->flags & SRV_F_COOKIESET)
4607 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4608 sv->cookie)
4609 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004610 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004611 return 1;
4612}
4613
William Lallemand222baf22016-11-19 02:00:33 +01004614/* register cli keywords */
4615static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004616 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004617 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004618 { { "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 +01004619 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004620 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004621 { { "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 +01004622 { { "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 +01004623 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004624 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4625 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4626
William Lallemand222baf22016-11-19 02:00:33 +01004627 {{},}
4628}};
4629
Willy Tarreau0108d902018-11-25 19:14:37 +01004630INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004631
Emeric Brun64cc49c2017-10-03 14:46:45 +02004632/*
4633 * This function applies server's status changes, it is
4634 * is designed to be called asynchronously.
4635 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004636 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004637 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004638static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004639{
4640 struct check *check = &s->check;
4641 int xferred;
4642 struct proxy *px = s->proxy;
4643 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4644 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4645 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004646 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004647
Emeric Brun64cc49c2017-10-03 14:46:45 +02004648 /* If currently main is not set we try to apply pending state changes */
4649 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4650 int next_admin;
4651
4652 /* Backup next admin */
4653 next_admin = s->next_admin;
4654
4655 /* restore current admin state */
4656 s->next_admin = s->cur_admin;
4657
4658 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4659 s->last_change = now.tv_sec;
4660 if (s->proxy->lbprm.set_server_status_down)
4661 s->proxy->lbprm.set_server_status_down(s);
4662
4663 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4664 srv_shutdown_streams(s, SF_ERR_DOWN);
4665
4666 /* we might have streams queued on this server and waiting for
4667 * a connection. Those which are redispatchable will be queued
4668 * to another server or to the proxy itself.
4669 */
4670 xferred = pendconn_redistribute(s);
4671
4672 tmptrash = alloc_trash_chunk();
4673 if (tmptrash) {
4674 chunk_printf(tmptrash,
4675 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4676 s->proxy->id, s->id);
4677
Emeric Brun5a133512017-10-19 14:42:30 +02004678 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004679 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004680
4681 /* we don't send an alert if the server was previously paused */
4682 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004683 send_log(s->proxy, log_level, "%s.\n",
4684 tmptrash->area);
4685 send_email_alert(s, log_level, "%s",
4686 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004687 free_trash_chunk(tmptrash);
4688 tmptrash = NULL;
4689 }
4690 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4691 set_backend_down(s->proxy);
4692
4693 s->counters.down_trans++;
4694 }
4695 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4696 s->last_change = now.tv_sec;
4697 if (s->proxy->lbprm.set_server_status_down)
4698 s->proxy->lbprm.set_server_status_down(s);
4699
4700 /* we might have streams queued on this server and waiting for
4701 * a connection. Those which are redispatchable will be queued
4702 * to another server or to the proxy itself.
4703 */
4704 xferred = pendconn_redistribute(s);
4705
4706 tmptrash = alloc_trash_chunk();
4707 if (tmptrash) {
4708 chunk_printf(tmptrash,
4709 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4710 s->proxy->id, s->id);
4711
Emeric Brun5a133512017-10-19 14:42:30 +02004712 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004713
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004714 ha_warning("%s.\n", tmptrash->area);
4715 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4716 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004717 free_trash_chunk(tmptrash);
4718 tmptrash = NULL;
4719 }
4720
4721 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4722 set_backend_down(s->proxy);
4723 }
4724 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4725 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4726 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4727 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4728 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4729 s->proxy->last_change = now.tv_sec;
4730 }
4731
4732 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4733 s->down_time += now.tv_sec - s->last_change;
4734
4735 s->last_change = now.tv_sec;
4736 if (s->next_state == SRV_ST_STARTING)
4737 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4738
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004739 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004740 /* now propagate the status change to any LB algorithms */
4741 if (px->lbprm.update_server_eweight)
4742 px->lbprm.update_server_eweight(s);
4743 else if (srv_willbe_usable(s)) {
4744 if (px->lbprm.set_server_status_up)
4745 px->lbprm.set_server_status_up(s);
4746 }
4747 else {
4748 if (px->lbprm.set_server_status_down)
4749 px->lbprm.set_server_status_down(s);
4750 }
4751
4752 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4753 * and it's not a backup server and its effective weight is > 0,
4754 * then it can accept new connections, so we shut down all streams
4755 * on all backup servers.
4756 */
4757 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4758 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4759 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4760
4761 /* check if we can handle some connections queued at the proxy. We
4762 * will take as many as we can handle.
4763 */
4764 xferred = pendconn_grab_from_px(s);
4765
4766 tmptrash = alloc_trash_chunk();
4767 if (tmptrash) {
4768 chunk_printf(tmptrash,
4769 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4770 s->proxy->id, s->id);
4771
Emeric Brun5a133512017-10-19 14:42:30 +02004772 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004773 ha_warning("%s.\n", tmptrash->area);
4774 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4775 tmptrash->area);
4776 send_email_alert(s, LOG_NOTICE, "%s",
4777 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004778 free_trash_chunk(tmptrash);
4779 tmptrash = NULL;
4780 }
4781
4782 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4783 set_backend_down(s->proxy);
4784 }
4785 else if (s->cur_eweight != s->next_eweight) {
4786 /* now propagate the status change to any LB algorithms */
4787 if (px->lbprm.update_server_eweight)
4788 px->lbprm.update_server_eweight(s);
4789 else if (srv_willbe_usable(s)) {
4790 if (px->lbprm.set_server_status_up)
4791 px->lbprm.set_server_status_up(s);
4792 }
4793 else {
4794 if (px->lbprm.set_server_status_down)
4795 px->lbprm.set_server_status_down(s);
4796 }
4797
4798 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4799 set_backend_down(s->proxy);
4800 }
4801
4802 s->next_admin = next_admin;
4803 }
4804
Emeric Brun5a133512017-10-19 14:42:30 +02004805 /* reset operational state change */
4806 *s->op_st_chg.reason = 0;
4807 s->op_st_chg.status = s->op_st_chg.code = -1;
4808 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004809
4810 /* Now we try to apply pending admin changes */
4811
4812 /* Maintenance must also disable health checks */
4813 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4814 if (s->check.state & CHK_ST_ENABLED) {
4815 s->check.state |= CHK_ST_PAUSED;
4816 check->health = 0;
4817 }
4818
4819 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004820 tmptrash = alloc_trash_chunk();
4821 if (tmptrash) {
4822 chunk_printf(tmptrash,
4823 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4824 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4825 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004826
Olivier Houchard796a2b32017-10-24 17:42:47 +02004827 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004828
Olivier Houchard796a2b32017-10-24 17:42:47 +02004829 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004830 ha_warning("%s.\n", tmptrash->area);
4831 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4832 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004833 }
4834 free_trash_chunk(tmptrash);
4835 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004836 }
Emeric Brun8f298292017-12-06 16:47:17 +01004837 /* commit new admin status */
4838
4839 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004840 }
4841 else { /* server was still running */
4842 check->health = 0; /* failure */
4843 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004844
4845 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004846 if (s->proxy->lbprm.set_server_status_down)
4847 s->proxy->lbprm.set_server_status_down(s);
4848
Emeric Brun64cc49c2017-10-03 14:46:45 +02004849 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4850 srv_shutdown_streams(s, SF_ERR_DOWN);
4851
William Dauchy6318d332020-05-02 21:52:36 +02004852 /* force connection cleanup on the given server */
4853 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004854 /* we might have streams queued on this server and waiting for
4855 * a connection. Those which are redispatchable will be queued
4856 * to another server or to the proxy itself.
4857 */
4858 xferred = pendconn_redistribute(s);
4859
4860 tmptrash = alloc_trash_chunk();
4861 if (tmptrash) {
4862 chunk_printf(tmptrash,
4863 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4864 s->flags & SRV_F_BACKUP ? "Backup " : "",
4865 s->proxy->id, s->id,
4866 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4867
4868 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4869
4870 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004871 ha_warning("%s.\n", tmptrash->area);
4872 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4873 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004874 }
4875 free_trash_chunk(tmptrash);
4876 tmptrash = NULL;
4877 }
4878 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4879 set_backend_down(s->proxy);
4880
4881 s->counters.down_trans++;
4882 }
4883 }
4884 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4885 /* OK here we're leaving maintenance, we have many things to check,
4886 * because the server might possibly be coming back up depending on
4887 * its state. In practice, leaving maintenance means that we should
4888 * immediately turn to UP (more or less the slowstart) under the
4889 * following conditions :
4890 * - server is neither checked nor tracked
4891 * - server tracks another server which is not checked
4892 * - server tracks another server which is already up
4893 * Which sums up as something simpler :
4894 * "either the tracking server is up or the server's checks are disabled
4895 * or up". Otherwise we only re-enable health checks. There's a special
4896 * case associated to the stopping state which can be inherited. Note
4897 * that the server might still be in drain mode, which is naturally dealt
4898 * with by the lower level functions.
4899 */
4900
4901 if (s->check.state & CHK_ST_ENABLED) {
4902 s->check.state &= ~CHK_ST_PAUSED;
4903 check->health = check->rise; /* start OK but check immediately */
4904 }
4905
4906 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4907 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4908 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4909 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4910 s->next_state = SRV_ST_STOPPING;
4911 }
4912 else {
4913 s->next_state = SRV_ST_STARTING;
4914 if (s->slowstart > 0)
4915 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4916 else
4917 s->next_state = SRV_ST_RUNNING;
4918 }
4919
4920 }
4921
4922 tmptrash = alloc_trash_chunk();
4923 if (tmptrash) {
4924 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4925 chunk_printf(tmptrash,
4926 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4927 s->flags & SRV_F_BACKUP ? "Backup " : "",
4928 s->proxy->id, s->id,
4929 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4930 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4931 }
4932 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4933 chunk_printf(tmptrash,
4934 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4935 s->flags & SRV_F_BACKUP ? "Backup " : "",
4936 s->proxy->id, s->id, s->hostname,
4937 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4938 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4939 }
4940 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4941 chunk_printf(tmptrash,
4942 "%sServer %s/%s is %s/%s (leaving maintenance)",
4943 s->flags & SRV_F_BACKUP ? "Backup " : "",
4944 s->proxy->id, s->id,
4945 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4946 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4947 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004948 ha_warning("%s.\n", tmptrash->area);
4949 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4950 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004951 free_trash_chunk(tmptrash);
4952 tmptrash = NULL;
4953 }
4954
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004955 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004956 /* now propagate the status change to any LB algorithms */
4957 if (px->lbprm.update_server_eweight)
4958 px->lbprm.update_server_eweight(s);
4959 else if (srv_willbe_usable(s)) {
4960 if (px->lbprm.set_server_status_up)
4961 px->lbprm.set_server_status_up(s);
4962 }
4963 else {
4964 if (px->lbprm.set_server_status_down)
4965 px->lbprm.set_server_status_down(s);
4966 }
4967
4968 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4969 set_backend_down(s->proxy);
4970
Willy Tarreau6a78e612018-08-07 10:14:53 +02004971 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4972 * and it's not a backup server and its effective weight is > 0,
4973 * then it can accept new connections, so we shut down all streams
4974 * on all backup servers.
4975 */
4976 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4977 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4978 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4979
4980 /* check if we can handle some connections queued at the proxy. We
4981 * will take as many as we can handle.
4982 */
4983 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004984 }
4985 else if (s->next_admin & SRV_ADMF_MAINT) {
4986 /* remaining in maintenance mode, let's inform precisely about the
4987 * situation.
4988 */
4989 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4990 tmptrash = alloc_trash_chunk();
4991 if (tmptrash) {
4992 chunk_printf(tmptrash,
4993 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4994 s->flags & SRV_F_BACKUP ? "Backup " : "",
4995 s->proxy->id, s->id);
4996
4997 if (s->track) /* normally it's mandatory here */
4998 chunk_appendf(tmptrash, " via %s/%s",
4999 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005000 ha_warning("%s.\n", tmptrash->area);
5001 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5002 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005003 free_trash_chunk(tmptrash);
5004 tmptrash = NULL;
5005 }
5006 }
5007 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5008 tmptrash = alloc_trash_chunk();
5009 if (tmptrash) {
5010 chunk_printf(tmptrash,
5011 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5012 s->flags & SRV_F_BACKUP ? "Backup " : "",
5013 s->proxy->id, s->id, s->hostname);
5014
5015 if (s->track) /* normally it's mandatory here */
5016 chunk_appendf(tmptrash, " via %s/%s",
5017 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005018 ha_warning("%s.\n", tmptrash->area);
5019 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5020 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005021 free_trash_chunk(tmptrash);
5022 tmptrash = NULL;
5023 }
5024 }
5025 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5026 tmptrash = alloc_trash_chunk();
5027 if (tmptrash) {
5028 chunk_printf(tmptrash,
5029 "%sServer %s/%s remains in forced maintenance",
5030 s->flags & SRV_F_BACKUP ? "Backup " : "",
5031 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005032 ha_warning("%s.\n", tmptrash->area);
5033 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5034 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005035 free_trash_chunk(tmptrash);
5036 tmptrash = NULL;
5037 }
5038 }
5039 /* don't report anything when leaving drain mode and remaining in maintenance */
5040
5041 s->cur_admin = s->next_admin;
5042 }
5043
5044 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5045 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5046 /* drain state is applied only if not yet in maint */
5047
5048 s->last_change = now.tv_sec;
5049 if (px->lbprm.set_server_status_down)
5050 px->lbprm.set_server_status_down(s);
5051
5052 /* we might have streams queued on this server and waiting for
5053 * a connection. Those which are redispatchable will be queued
5054 * to another server or to the proxy itself.
5055 */
5056 xferred = pendconn_redistribute(s);
5057
5058 tmptrash = alloc_trash_chunk();
5059 if (tmptrash) {
5060 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5061 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5062 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5063
5064 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5065
5066 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005067 ha_warning("%s.\n", tmptrash->area);
5068 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5069 tmptrash->area);
5070 send_email_alert(s, LOG_NOTICE, "%s",
5071 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005072 }
5073 free_trash_chunk(tmptrash);
5074 tmptrash = NULL;
5075 }
5076
5077 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5078 set_backend_down(s->proxy);
5079 }
5080 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5081 /* OK completely leaving drain mode */
5082 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5083 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5084 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5085 s->proxy->last_change = now.tv_sec;
5086 }
5087
5088 if (s->last_change < now.tv_sec) // ignore negative times
5089 s->down_time += now.tv_sec - s->last_change;
5090 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005091 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005092
5093 tmptrash = alloc_trash_chunk();
5094 if (tmptrash) {
5095 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5096 chunk_printf(tmptrash,
5097 "%sServer %s/%s is %s (leaving forced drain)",
5098 s->flags & SRV_F_BACKUP ? "Backup " : "",
5099 s->proxy->id, s->id,
5100 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5101 }
5102 else {
5103 chunk_printf(tmptrash,
5104 "%sServer %s/%s is %s (leaving drain)",
5105 s->flags & SRV_F_BACKUP ? "Backup " : "",
5106 s->proxy->id, s->id,
5107 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5108 if (s->track) /* normally it's mandatory here */
5109 chunk_appendf(tmptrash, " via %s/%s",
5110 s->track->proxy->id, s->track->id);
5111 }
5112
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005113 ha_warning("%s.\n", tmptrash->area);
5114 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5115 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005116 free_trash_chunk(tmptrash);
5117 tmptrash = NULL;
5118 }
5119
5120 /* now propagate the status change to any LB algorithms */
5121 if (px->lbprm.update_server_eweight)
5122 px->lbprm.update_server_eweight(s);
5123 else if (srv_willbe_usable(s)) {
5124 if (px->lbprm.set_server_status_up)
5125 px->lbprm.set_server_status_up(s);
5126 }
5127 else {
5128 if (px->lbprm.set_server_status_down)
5129 px->lbprm.set_server_status_down(s);
5130 }
5131 }
5132 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5133 /* remaining in drain mode after removing one of its flags */
5134
5135 tmptrash = alloc_trash_chunk();
5136 if (tmptrash) {
5137 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5138 chunk_printf(tmptrash,
5139 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5140 s->flags & SRV_F_BACKUP ? "Backup " : "",
5141 s->proxy->id, s->id);
5142
5143 if (s->track) /* normally it's mandatory here */
5144 chunk_appendf(tmptrash, " via %s/%s",
5145 s->track->proxy->id, s->track->id);
5146 }
5147 else {
5148 chunk_printf(tmptrash,
5149 "%sServer %s/%s remains in forced drain mode",
5150 s->flags & SRV_F_BACKUP ? "Backup " : "",
5151 s->proxy->id, s->id);
5152 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005153 ha_warning("%s.\n", tmptrash->area);
5154 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5155 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005156 free_trash_chunk(tmptrash);
5157 tmptrash = NULL;
5158 }
5159
5160 /* commit new admin status */
5161
5162 s->cur_admin = s->next_admin;
5163 }
5164 }
5165
5166 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005167 *s->adm_st_chg_cause = 0;
5168}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005169
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005170struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5171{
5172 struct connection *conn;
5173
Olivier Houchard859dc802019-08-08 15:47:21 +02005174 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005175 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005176 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005177 }
5178
5179 return task;
5180}
5181
William Dauchy6318d332020-05-02 21:52:36 +02005182/* cleanup connections for a given server
5183 * might be useful when going on forced maintenance or live changing ip/port
5184 */
William Dauchy707ad322020-05-04 13:52:40 +02005185static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005186{
5187 struct connection *conn;
5188 int did_remove;
5189 int i;
5190 int j;
5191
5192 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5193 for (i = 0; i < global.nbthread; i++) {
5194 did_remove = 0;
5195 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
5196 for (j = 0; j < srv->curr_idle_conns; j++) {
5197 conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5198 if (!conn)
5199 conn = MT_LIST_POP(&srv->safe_conns[i],
5200 struct connection *, list);
5201 if (!conn)
5202 break;
5203 did_remove = 1;
5204 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
5205 }
5206 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
5207 if (did_remove)
5208 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5209 }
5210 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5211}
5212
Willy Tarreau980855b2019-02-07 14:59:29 +01005213struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005214{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005215 struct server *srv;
5216 struct eb32_node *eb;
5217 int i;
5218 unsigned int next_wakeup;
5219 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005220
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005221 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5222 while (1) {
5223 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005224 int exceed_conns;
5225 int to_kill;
5226 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005227
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005228 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5229 if (!eb) {
5230 /* we might have reached the end of the tree, typically because
5231 * <now_ms> is in the first half and we're first scanning the last
5232 * half. Let's loop back to the beginning of the tree now.
5233 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005234
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005235 eb = eb32_first(&idle_conn_srv);
5236 if (likely(!eb))
5237 break;
5238 }
5239 if (tick_is_lt(now_ms, eb->key)) {
5240 /* timer not expired yet, revisit it later */
5241 next_wakeup = eb->key;
5242 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005243 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005244 }
5245 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005246
5247 /* Calculate how many idle connections we want to kill :
5248 * we want to remove half the difference between the total
5249 * of established connections (used or idle) and the max
5250 * number of used connections.
5251 */
5252 curr_idle = srv->curr_idle_conns;
5253 if (curr_idle == 0)
5254 goto remove;
5255 exceed_conns = srv->curr_used_conns + curr_idle -
5256 srv->max_used_conns;
5257 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5258 srv->max_used_conns = srv->curr_used_conns;
5259
5260 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5261 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005262 int j;
5263 int did_remove = 0;
5264
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005265 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5266 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005267 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005268 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005269 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5270 if (!conn)
5271 conn = MT_LIST_POP(&srv->safe_conns[i],
5272 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005273 if (!conn)
5274 break;
5275 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005276 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005277 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005278 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005279 if (did_remove && max_conn < srv->curr_idle_thr[i])
5280 srv_is_empty = 0;
5281 if (did_remove)
5282 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5283 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005284remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005285 eb32_delete(&srv->idle_node);
5286 if (!srv_is_empty) {
5287 /* There are still more idle connections, add the
5288 * server back in the tree.
5289 */
5290 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5291 now_ms);
5292 eb32_insert(&idle_conn_srv, &srv->idle_node);
5293 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005294 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005295 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5296
5297 if (need_wakeup)
5298 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005299 else
5300 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005301
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005302 return task;
5303}
Olivier Houchard88698d92019-04-16 19:07:22 +02005304
5305/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5306static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5307 struct proxy *defpx, const char *file, int line,
5308 char **err)
5309{
5310 int arg = -1;
5311
5312 if (too_many_args(1, args, err, NULL))
5313 return -1;
5314
5315 if (*(args[1]) != 0)
5316 arg = atoi(args[1]);
5317
5318 if (arg < 0 || arg > 100) {
5319 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5320 return -1;
5321 }
5322
5323 if (args[0][10] == 'h')
5324 global.tune.pool_high_ratio = arg;
5325 else
5326 global.tune.pool_low_ratio = arg;
5327 return 0;
5328}
5329
5330/* config keyword parsers */
5331static struct cfg_kw_list cfg_kws = {ILH, {
5332 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5333 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5334 { 0, NULL, NULL }
5335}};
5336
5337INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5338
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005339/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005340 * Local variables:
5341 * c-indent-level: 8
5342 * c-basic-offset: 8
5343 * End:
5344 */