Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Server management functions. |
| 3 | * |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 5 | * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 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 Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 16 | |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 17 | #include <import/xxhash.h> |
| 18 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 19 | #include <common/cfgparse.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 20 | #include <common/config.h> |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 21 | #include <common/errors.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 22 | #include <common/initcall.h> |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 23 | #include <common/namespace.h> |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 24 | #include <common/time.h> |
| 25 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 26 | #include <types/applet.h> |
| 27 | #include <types/cli.h> |
Frédéric Lécaille | 7da7129 | 2019-05-20 09:47:07 +0200 | [diff] [blame] | 28 | #include <types/dict.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 29 | #include <types/global.h> |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 30 | #include <types/cli.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 31 | #include <types/dns.h> |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 32 | #include <types/stats.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 33 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 34 | #include <proto/applet.h> |
| 35 | #include <proto/cli.h> |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 36 | #include <proto/checks.h> |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 37 | #include <proto/connection.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 38 | #include <proto/port_range.h> |
| 39 | #include <proto/protocol.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 40 | #include <proto/queue.h> |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 41 | #include <proto/sample.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 42 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 43 | #include <proto/stream.h> |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 44 | #include <proto/stream_interface.h> |
| 45 | #include <proto/stats.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 46 | #include <proto/task.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 47 | #include <proto/dns.h> |
David Carlier | 6f18208 | 2017-04-03 21:58:04 +0100 | [diff] [blame] | 48 | #include <netinet/tcp.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 50 | static void srv_update_status(struct server *s); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 51 | static void srv_update_state(struct server *srv, int version, char **params); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 52 | static int srv_apply_lastaddr(struct server *srv, int *err_code); |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 53 | static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 55 | /* List head of all known server keywords */ |
| 56 | static struct srv_kw_list srv_keywords = { |
| 57 | .list = LIST_HEAD_INIT(srv_keywords.list) |
| 58 | }; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 59 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 60 | __decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock); |
| 61 | struct eb_root idle_conn_srv = EB_ROOT; |
| 62 | struct task *idle_conn_task = NULL; |
| 63 | struct task *idle_conn_cleanup[MAX_THREADS] = { NULL }; |
| 64 | struct list toremove_connections[MAX_THREADS]; |
Olivier Houchard | ba936e5 | 2019-07-11 15:49:00 +0200 | [diff] [blame] | 65 | __decl_hathreads(HA_SPINLOCK_T toremove_lock[MAX_THREADS]); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 66 | |
Frédéric Lécaille | 7da7129 | 2019-05-20 09:47:07 +0200 | [diff] [blame] | 67 | /* The server names dictionary */ |
| 68 | struct dict server_name_dict = { |
| 69 | .name = "server names", |
| 70 | .values = EB_ROOT_UNIQUE, |
| 71 | }; |
| 72 | |
Simon Horman | a360844 | 2013-11-01 16:46:15 +0900 | [diff] [blame] | 73 | int srv_downtime(const struct server *s) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 74 | { |
Amaury Denoyelle | 13ed503 | 2020-10-29 15:59:05 +0100 | [diff] [blame] | 75 | if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 76 | return s->down_time; |
| 77 | |
| 78 | return now.tv_sec - s->last_change + s->down_time; |
| 79 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 81 | int srv_lastsession(const struct server *s) |
| 82 | { |
| 83 | if (s->counters.last_sess) |
| 84 | return now.tv_sec - s->counters.last_sess; |
| 85 | |
| 86 | return -1; |
| 87 | } |
| 88 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 89 | int srv_getinter(const struct check *check) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 90 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 91 | const struct server *s = check->server; |
| 92 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 93 | if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 94 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 95 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 96 | if ((s->next_state == SRV_ST_STOPPED) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 97 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 98 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 99 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 102 | /* |
| 103 | * Check that we did not get a hash collision. |
| 104 | * Unlikely, but it can happen. |
| 105 | */ |
| 106 | static inline void srv_check_for_dup_dyncookie(struct server *s) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 107 | { |
| 108 | struct proxy *p = s->proxy; |
| 109 | struct server *tmpserv; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 110 | |
| 111 | for (tmpserv = p->srv; tmpserv != NULL; |
| 112 | tmpserv = tmpserv->next) { |
| 113 | if (tmpserv == s) |
| 114 | continue; |
| 115 | if (tmpserv->next_admin & SRV_ADMF_FMAINT) |
| 116 | continue; |
| 117 | if (tmpserv->cookie && |
| 118 | strcmp(tmpserv->cookie, s->cookie) == 0) { |
| 119 | ha_warning("We generated two equal cookies for two different servers.\n" |
| 120 | "Please change the secret key for '%s'.\n", |
| 121 | s->proxy->id); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } |
| 126 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 127 | /* |
Willy Tarreau | 0ff395c | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 128 | * Must be called with the server lock held, and will grab the proxy lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 129 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 130 | void srv_set_dyncookie(struct server *s) |
| 131 | { |
| 132 | struct proxy *p = s->proxy; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 133 | char *tmpbuf; |
| 134 | unsigned long long hash_value; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 135 | size_t key_len; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 136 | size_t buffer_len; |
| 137 | int addr_len; |
| 138 | int port; |
| 139 | |
Willy Tarreau | 0ff395c | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 140 | HA_SPIN_LOCK(PROXY_LOCK, &p->lock); |
| 141 | |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 142 | if ((s->flags & SRV_F_COOKIESET) || |
| 143 | !(s->proxy->ck_opts & PR_CK_DYNAMIC) || |
| 144 | s->proxy->dyncookie_key == NULL) |
Willy Tarreau | 0ff395c | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 145 | goto out; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 146 | key_len = strlen(p->dyncookie_key); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 147 | |
| 148 | if (s->addr.ss_family != AF_INET && |
| 149 | s->addr.ss_family != AF_INET6) |
Willy Tarreau | 0ff395c | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 150 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 151 | /* |
| 152 | * Buffer to calculate the cookie value. |
| 153 | * The buffer contains the secret key + the server IP address |
| 154 | * + the TCP port. |
| 155 | */ |
| 156 | addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16; |
| 157 | /* |
| 158 | * The TCP port should use only 2 bytes, but is stored in |
| 159 | * an unsigned int in struct server, so let's use 4, to be |
| 160 | * on the safe side. |
| 161 | */ |
| 162 | buffer_len = key_len + addr_len + 4; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 163 | tmpbuf = trash.area; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 164 | memcpy(tmpbuf, p->dyncookie_key, key_len); |
| 165 | memcpy(&(tmpbuf[key_len]), |
| 166 | s->addr.ss_family == AF_INET ? |
| 167 | (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr : |
| 168 | (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr), |
| 169 | addr_len); |
| 170 | /* |
| 171 | * Make sure it's the same across all the load balancers, |
| 172 | * no matter their endianness. |
| 173 | */ |
| 174 | port = htonl(s->svc_port); |
| 175 | memcpy(&tmpbuf[key_len + addr_len], &port, 4); |
| 176 | hash_value = XXH64(tmpbuf, buffer_len, 0); |
| 177 | memprintf(&s->cookie, "%016llx", hash_value); |
| 178 | if (!s->cookie) |
Willy Tarreau | 0ff395c | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 179 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 180 | s->cklen = 16; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 181 | |
| 182 | /* Don't bother checking if the dyncookie is duplicated if |
| 183 | * the server is marked as "disabled", maybe it doesn't have |
| 184 | * its real IP yet, but just a place holder. |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 185 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 186 | if (!(s->next_admin & SRV_ADMF_FMAINT)) |
| 187 | srv_check_for_dup_dyncookie(s); |
Willy Tarreau | 0ff395c | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 188 | out: |
| 189 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 192 | /* |
| 193 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 194 | * parsing sessions. |
| 195 | */ |
| 196 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 197 | { |
| 198 | LIST_ADDQ(&srv_keywords.list, &kwl->list); |
| 199 | } |
| 200 | |
| 201 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 202 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 203 | * find one with a valid ->parse() function. This way it is possible to declare |
| 204 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 205 | * if some options are met. Note that if the requested keyword contains an |
| 206 | * opening parenthesis, everything from this point is ignored. |
| 207 | */ |
| 208 | struct srv_kw *srv_find_kw(const char *kw) |
| 209 | { |
| 210 | int index; |
| 211 | const char *kwend; |
| 212 | struct srv_kw_list *kwl; |
| 213 | struct srv_kw *ret = NULL; |
| 214 | |
| 215 | kwend = strchr(kw, '('); |
| 216 | if (!kwend) |
| 217 | kwend = kw + strlen(kw); |
| 218 | |
| 219 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 220 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 221 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 222 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 223 | if (kwl->kw[index].parse) |
| 224 | return &kwl->kw[index]; /* found it !*/ |
| 225 | else |
| 226 | ret = &kwl->kw[index]; /* may be OK */ |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 234 | * unsupported keywords are only dumped if their supported form was not |
| 235 | * found. |
| 236 | */ |
| 237 | void srv_dump_kws(char **out) |
| 238 | { |
| 239 | struct srv_kw_list *kwl; |
| 240 | int index; |
| 241 | |
| 242 | *out = NULL; |
| 243 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 244 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 245 | if (kwl->kw[index].parse || |
| 246 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 247 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 248 | kwl->scope, |
| 249 | kwl->kw[index].kw, |
| 250 | kwl->kw[index].skip ? " <arg>" : "", |
| 251 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 252 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 257 | |
Frédéric Lécaille | 6e5e0d8 | 2017-03-20 16:30:18 +0100 | [diff] [blame] | 258 | /* Parse the "addr" server keyword */ |
| 259 | static int srv_parse_addr(char **args, int *cur_arg, |
| 260 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 261 | { |
| 262 | char *errmsg, *arg; |
| 263 | struct sockaddr_storage *sk; |
| 264 | int port1, port2; |
| 265 | struct protocol *proto; |
| 266 | |
| 267 | errmsg = NULL; |
| 268 | arg = args[*cur_arg + 1]; |
| 269 | |
| 270 | if (!*arg) { |
| 271 | memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]); |
| 272 | goto err; |
| 273 | } |
| 274 | |
| 275 | sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1); |
| 276 | if (!sk) { |
| 277 | memprintf(err, "'%s' : %s", args[*cur_arg], errmsg); |
| 278 | goto err; |
| 279 | } |
| 280 | |
| 281 | proto = protocol_by_family(sk->ss_family); |
| 282 | if (!proto || !proto->connect) { |
| 283 | memprintf(err, "'%s %s' : connect() not supported for this address family.\n", |
| 284 | args[*cur_arg], arg); |
| 285 | goto err; |
| 286 | } |
| 287 | |
| 288 | if (port1 != port2) { |
| 289 | memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n", |
| 290 | args[*cur_arg], arg); |
| 291 | goto err; |
| 292 | } |
| 293 | |
| 294 | newsrv->check.addr = newsrv->agent.addr = *sk; |
| 295 | newsrv->flags |= SRV_F_CHECKADDR; |
| 296 | newsrv->flags |= SRV_F_AGENTADDR; |
| 297 | |
| 298 | return 0; |
| 299 | |
| 300 | err: |
| 301 | free(errmsg); |
| 302 | return ERR_ALERT | ERR_FATAL; |
| 303 | } |
| 304 | |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 305 | /* Parse the "agent-check" server keyword */ |
| 306 | static int srv_parse_agent_check(char **args, int *cur_arg, |
| 307 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 308 | { |
| 309 | newsrv->do_agent = 1; |
| 310 | return 0; |
| 311 | } |
| 312 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 313 | /* Parse the "backup" server keyword */ |
| 314 | static int srv_parse_backup(char **args, int *cur_arg, |
| 315 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 316 | { |
| 317 | newsrv->flags |= SRV_F_BACKUP; |
| 318 | return 0; |
| 319 | } |
| 320 | |
Frédéric Lécaille | 65aa356 | 2017-03-14 11:20:13 +0100 | [diff] [blame] | 321 | /* Parse the "check" server keyword */ |
| 322 | static int srv_parse_check(char **args, int *cur_arg, |
| 323 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 324 | { |
| 325 | newsrv->do_check = 1; |
| 326 | return 0; |
| 327 | } |
| 328 | |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 329 | /* Parse the "check-send-proxy" server keyword */ |
| 330 | static int srv_parse_check_send_proxy(char **args, int *cur_arg, |
| 331 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 332 | { |
| 333 | newsrv->check.send_proxy = 1; |
| 334 | return 0; |
| 335 | } |
| 336 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 337 | /* Parse the "check-via-socks4" server keyword */ |
| 338 | static int srv_parse_check_via_socks4(char **args, int *cur_arg, |
| 339 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 340 | { |
| 341 | newsrv->check.via_socks4 = 1; |
| 342 | return 0; |
| 343 | } |
| 344 | |
Frédéric Lécaille | 9d1b95b | 2017-03-15 09:13:33 +0100 | [diff] [blame] | 345 | /* Parse the "cookie" server keyword */ |
| 346 | static int srv_parse_cookie(char **args, int *cur_arg, |
| 347 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 348 | { |
| 349 | char *arg; |
| 350 | |
| 351 | arg = args[*cur_arg + 1]; |
| 352 | if (!*arg) { |
| 353 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 354 | return ERR_ALERT | ERR_FATAL; |
| 355 | } |
| 356 | |
| 357 | free(newsrv->cookie); |
| 358 | newsrv->cookie = strdup(arg); |
| 359 | newsrv->cklen = strlen(arg); |
| 360 | newsrv->flags |= SRV_F_COOKIESET; |
| 361 | return 0; |
| 362 | } |
| 363 | |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 364 | /* Parse the "disabled" server keyword */ |
| 365 | static int srv_parse_disabled(char **args, int *cur_arg, |
| 366 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 367 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 368 | newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 369 | newsrv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 370 | newsrv->check.state |= CHK_ST_PAUSED; |
| 371 | newsrv->check.health = 0; |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /* Parse the "enabled" server keyword */ |
| 376 | static int srv_parse_enabled(char **args, int *cur_arg, |
| 377 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 378 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 379 | newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT; |
| 380 | newsrv->next_state = SRV_ST_RUNNING; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 381 | newsrv->check.state &= ~CHK_ST_PAUSED; |
| 382 | newsrv->check.health = newsrv->check.rise; |
| 383 | return 0; |
| 384 | } |
| 385 | |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 386 | static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 387 | { |
| 388 | char *arg; |
| 389 | |
| 390 | arg = args[*cur_arg + 1]; |
| 391 | if (!*arg) { |
| 392 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 393 | return ERR_ALERT | ERR_FATAL; |
| 394 | } |
| 395 | newsrv->max_reuse = atoi(arg); |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 400 | static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 401 | { |
| 402 | const char *res; |
| 403 | char *arg; |
| 404 | unsigned int time; |
| 405 | |
| 406 | arg = args[*cur_arg + 1]; |
| 407 | if (!*arg) { |
| 408 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 409 | return ERR_ALERT | ERR_FATAL; |
| 410 | } |
| 411 | res = parse_time_err(arg, &time, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 412 | if (res == PARSE_TIME_OVER) { |
| 413 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 414 | args[*cur_arg+1], args[*cur_arg]); |
| 415 | return ERR_ALERT | ERR_FATAL; |
| 416 | } |
| 417 | else if (res == PARSE_TIME_UNDER) { |
| 418 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 419 | args[*cur_arg+1], args[*cur_arg]); |
| 420 | return ERR_ALERT | ERR_FATAL; |
| 421 | } |
| 422 | else if (res) { |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 423 | memprintf(err, "unexpected character '%c' in argument to <%s>.\n", |
| 424 | *res, args[*cur_arg]); |
| 425 | return ERR_ALERT | ERR_FATAL; |
| 426 | } |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 427 | newsrv->pool_purge_delay = time; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 432 | static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 433 | { |
| 434 | char *arg; |
| 435 | |
| 436 | arg = args[*cur_arg + 1]; |
| 437 | if (!*arg) { |
| 438 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 439 | return ERR_ALERT | ERR_FATAL; |
| 440 | } |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 441 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 442 | newsrv->max_idle_conns = atoi(arg); |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 443 | if ((int)newsrv->max_idle_conns < -1) { |
| 444 | memprintf(err, "'%s' must be >= -1", args[*cur_arg]); |
| 445 | return ERR_ALERT | ERR_FATAL; |
| 446 | } |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 451 | /* parse the "id" server keyword */ |
| 452 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 453 | { |
| 454 | struct eb32_node *node; |
| 455 | |
| 456 | if (!*args[*cur_arg + 1]) { |
| 457 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 458 | return ERR_ALERT | ERR_FATAL; |
| 459 | } |
| 460 | |
| 461 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 462 | newsrv->conf.id.key = newsrv->puid; |
| 463 | |
| 464 | if (newsrv->puid <= 0) { |
| 465 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 466 | return ERR_ALERT | ERR_FATAL; |
| 467 | } |
| 468 | |
| 469 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 470 | if (node) { |
| 471 | struct server *target = container_of(node, struct server, conf.id); |
| 472 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 473 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 474 | target->id); |
| 475 | return ERR_ALERT | ERR_FATAL; |
| 476 | } |
| 477 | |
| 478 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
Baptiste Assmann | 7cc419a | 2015-07-07 22:02:20 +0200 | [diff] [blame] | 479 | newsrv->flags |= SRV_F_FORCED_ID; |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 483 | /* Parse the "namespace" server keyword */ |
| 484 | static int srv_parse_namespace(char **args, int *cur_arg, |
| 485 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 486 | { |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 487 | #ifdef USE_NS |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 488 | char *arg; |
| 489 | |
| 490 | arg = args[*cur_arg + 1]; |
| 491 | if (!*arg) { |
| 492 | memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]); |
| 493 | return ERR_ALERT | ERR_FATAL; |
| 494 | } |
| 495 | |
| 496 | if (!strcmp(arg, "*")) { |
| 497 | /* Use the namespace associated with the connection (if present). */ |
| 498 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * As this parser may be called several times for the same 'default-server' |
| 504 | * object, or for a new 'server' instance deriving from a 'default-server' |
| 505 | * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it. |
| 506 | */ |
| 507 | newsrv->flags &= ~SRV_F_USE_NS_FROM_PP; |
| 508 | |
| 509 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 510 | if (!newsrv->netns) |
| 511 | newsrv->netns = netns_store_insert(arg); |
| 512 | |
| 513 | if (!newsrv->netns) { |
| 514 | memprintf(err, "Cannot open namespace '%s'", arg); |
| 515 | return ERR_ALERT | ERR_FATAL; |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | #else |
| 520 | memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]); |
| 521 | return ERR_ALERT | ERR_FATAL; |
| 522 | #endif |
| 523 | } |
| 524 | |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 525 | /* Parse the "no-agent-check" server keyword */ |
| 526 | static int srv_parse_no_agent_check(char **args, int *cur_arg, |
| 527 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 528 | { |
| 529 | free_check(&newsrv->agent); |
| 530 | newsrv->agent.inter = 0; |
| 531 | newsrv->agent.port = 0; |
| 532 | newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT; |
| 533 | newsrv->do_agent = 0; |
| 534 | return 0; |
| 535 | } |
| 536 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 537 | /* Parse the "no-backup" server keyword */ |
| 538 | static int srv_parse_no_backup(char **args, int *cur_arg, |
| 539 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 540 | { |
| 541 | newsrv->flags &= ~SRV_F_BACKUP; |
| 542 | return 0; |
| 543 | } |
| 544 | |
Frédéric Lécaille | 65aa356 | 2017-03-14 11:20:13 +0100 | [diff] [blame] | 545 | /* Parse the "no-check" server keyword */ |
| 546 | static int srv_parse_no_check(char **args, int *cur_arg, |
| 547 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 548 | { |
| 549 | free_check(&newsrv->check); |
| 550 | newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED; |
| 551 | newsrv->do_check = 0; |
| 552 | return 0; |
| 553 | } |
| 554 | |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 555 | /* Parse the "no-check-send-proxy" server keyword */ |
| 556 | static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, |
| 557 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 558 | { |
| 559 | newsrv->check.send_proxy = 0; |
| 560 | return 0; |
| 561 | } |
| 562 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 563 | /* Disable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 564 | static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags) |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 565 | { |
| 566 | srv->pp_opts &= ~flags; |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /* Parse the "no-send-proxy" server keyword */ |
| 571 | static int srv_parse_no_send_proxy(char **args, int *cur_arg, |
| 572 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 573 | { |
| 574 | return srv_disable_pp_flags(newsrv, SRV_PP_V1); |
| 575 | } |
| 576 | |
| 577 | /* Parse the "no-send-proxy-v2" server keyword */ |
| 578 | static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg, |
| 579 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 580 | { |
| 581 | return srv_disable_pp_flags(newsrv, SRV_PP_V2); |
| 582 | } |
| 583 | |
Frédéric Lécaille | aeeb1c9 | 2019-07-04 14:19:06 +0200 | [diff] [blame] | 584 | /* Parse the "no-tfo" server keyword */ |
| 585 | static int srv_parse_no_tfo(char **args, int *cur_arg, |
| 586 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 587 | { |
| 588 | newsrv->flags &= ~SRV_F_FASTOPEN; |
| 589 | return 0; |
| 590 | } |
| 591 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 592 | /* Parse the "non-stick" server keyword */ |
| 593 | static int srv_parse_non_stick(char **args, int *cur_arg, |
| 594 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 595 | { |
| 596 | newsrv->flags |= SRV_F_NON_STICK; |
| 597 | return 0; |
| 598 | } |
| 599 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 600 | /* Enable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 601 | static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags) |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 602 | { |
| 603 | srv->pp_opts |= flags; |
| 604 | return 0; |
| 605 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 606 | /* parse the "proto" server keyword */ |
| 607 | static int srv_parse_proto(char **args, int *cur_arg, |
| 608 | struct proxy *px, struct server *newsrv, char **err) |
| 609 | { |
| 610 | struct ist proto; |
| 611 | |
| 612 | if (!*args[*cur_arg + 1]) { |
| 613 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 614 | return ERR_ALERT | ERR_FATAL; |
| 615 | } |
| 616 | proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1])); |
| 617 | newsrv->mux_proto = get_mux_proto(proto); |
| 618 | if (!newsrv->mux_proto) { |
| 619 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 620 | return ERR_ALERT | ERR_FATAL; |
| 621 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 622 | return 0; |
| 623 | } |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 624 | |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 625 | /* parse the "proxy-v2-options" */ |
| 626 | static int srv_parse_proxy_v2_options(char **args, int *cur_arg, |
| 627 | struct proxy *px, struct server *newsrv, char **err) |
| 628 | { |
| 629 | char *p, *n; |
| 630 | for (p = args[*cur_arg+1]; p; p = n) { |
| 631 | n = strchr(p, ','); |
| 632 | if (n) |
| 633 | *n++ = '\0'; |
| 634 | if (!strcmp(p, "ssl")) { |
| 635 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 636 | } else if (!strcmp(p, "cert-cn")) { |
| 637 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 638 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 639 | } else if (!strcmp(p, "cert-key")) { |
| 640 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 641 | newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG; |
| 642 | } else if (!strcmp(p, "cert-sig")) { |
| 643 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 644 | newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG; |
| 645 | } else if (!strcmp(p, "ssl-cipher")) { |
| 646 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 647 | newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 648 | } else if (!strcmp(p, "authority")) { |
| 649 | newsrv->pp_opts |= SRV_PP_V2_AUTHORITY; |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 650 | } else if (!strcmp(p, "crc32c")) { |
| 651 | newsrv->pp_opts |= SRV_PP_V2_CRC32C; |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 652 | } else |
| 653 | goto fail; |
| 654 | } |
| 655 | return 0; |
| 656 | fail: |
| 657 | if (err) |
| 658 | memprintf(err, "'%s' : proxy v2 option not implemented", p); |
| 659 | return ERR_ALERT | ERR_FATAL; |
| 660 | } |
| 661 | |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 662 | /* Parse the "observe" server keyword */ |
| 663 | static int srv_parse_observe(char **args, int *cur_arg, |
| 664 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 665 | { |
| 666 | char *arg; |
| 667 | |
| 668 | arg = args[*cur_arg + 1]; |
| 669 | if (!*arg) { |
| 670 | memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]); |
| 671 | return ERR_ALERT | ERR_FATAL; |
| 672 | } |
| 673 | |
| 674 | if (!strcmp(arg, "none")) { |
| 675 | newsrv->observe = HANA_OBS_NONE; |
| 676 | } |
| 677 | else if (!strcmp(arg, "layer4")) { |
| 678 | newsrv->observe = HANA_OBS_LAYER4; |
| 679 | } |
| 680 | else if (!strcmp(arg, "layer7")) { |
| 681 | if (curproxy->mode != PR_MODE_HTTP) { |
| 682 | memprintf(err, "'%s' can only be used in http proxies.\n", arg); |
| 683 | return ERR_ALERT; |
| 684 | } |
| 685 | newsrv->observe = HANA_OBS_LAYER7; |
| 686 | } |
| 687 | else { |
| 688 | memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' " |
| 689 | "but got '%s'\n", args[*cur_arg], arg); |
| 690 | return ERR_ALERT | ERR_FATAL; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
Frédéric Lécaille | 1618623 | 2017-03-14 16:42:49 +0100 | [diff] [blame] | 696 | /* Parse the "redir" server keyword */ |
| 697 | static int srv_parse_redir(char **args, int *cur_arg, |
| 698 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 699 | { |
| 700 | char *arg; |
| 701 | |
| 702 | arg = args[*cur_arg + 1]; |
| 703 | if (!*arg) { |
| 704 | memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]); |
| 705 | return ERR_ALERT | ERR_FATAL; |
| 706 | } |
| 707 | |
| 708 | free(newsrv->rdr_pfx); |
| 709 | newsrv->rdr_pfx = strdup(arg); |
| 710 | newsrv->rdr_len = strlen(arg); |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 715 | /* Parse the "send-proxy" server keyword */ |
| 716 | static int srv_parse_send_proxy(char **args, int *cur_arg, |
| 717 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 718 | { |
| 719 | return srv_enable_pp_flags(newsrv, SRV_PP_V1); |
| 720 | } |
| 721 | |
| 722 | /* Parse the "send-proxy-v2" server keyword */ |
| 723 | static int srv_parse_send_proxy_v2(char **args, int *cur_arg, |
| 724 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 725 | { |
| 726 | return srv_enable_pp_flags(newsrv, SRV_PP_V2); |
| 727 | } |
| 728 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 729 | |
| 730 | /* Parse the "source" server keyword */ |
| 731 | static int srv_parse_source(char **args, int *cur_arg, |
| 732 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 733 | { |
| 734 | char *errmsg; |
| 735 | int port_low, port_high; |
| 736 | struct sockaddr_storage *sk; |
| 737 | struct protocol *proto; |
| 738 | |
| 739 | errmsg = NULL; |
| 740 | |
| 741 | if (!*args[*cur_arg + 1]) { |
| 742 | memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, " |
| 743 | "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface"); |
| 744 | goto err; |
| 745 | } |
| 746 | |
| 747 | /* 'sk' is statically allocated (no need to be freed). */ |
| 748 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1); |
| 749 | if (!sk) { |
| 750 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 751 | goto err; |
| 752 | } |
| 753 | |
| 754 | proto = protocol_by_family(sk->ss_family); |
| 755 | if (!proto || !proto->connect) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 756 | ha_alert("'%s %s' : connect() not supported for this address family.\n", |
| 757 | args[*cur_arg], args[*cur_arg + 1]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 758 | goto err; |
| 759 | } |
| 760 | |
| 761 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 762 | newsrv->conn_src.source_addr = *sk; |
| 763 | |
| 764 | if (port_low != port_high) { |
| 765 | int i; |
| 766 | |
| 767 | if (!port_low || !port_high) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 768 | ha_alert("'%s' does not support port offsets (found '%s').\n", |
| 769 | args[*cur_arg], args[*cur_arg + 1]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 770 | goto err; |
| 771 | } |
| 772 | |
| 773 | if (port_low <= 0 || port_low > 65535 || |
| 774 | port_high <= 0 || port_high > 65535 || |
| 775 | port_low > port_high) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 776 | ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 777 | goto err; |
| 778 | } |
| 779 | newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1); |
Remi Tricot-Le Breton | 75ffa1b | 2021-05-12 09:44:06 +0200 | [diff] [blame] | 780 | if (!newsrv->conn_src.sport_range) { |
| 781 | ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]); |
| 782 | goto err; |
| 783 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 784 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 785 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 786 | } |
| 787 | |
| 788 | *cur_arg += 2; |
| 789 | while (*(args[*cur_arg])) { |
| 790 | if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */ |
| 791 | #if defined(CONFIG_HAP_TRANSPARENT) |
| 792 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 793 | ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', " |
| 794 | "or 'hdr_ip(name,#)' as argument.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 795 | goto err; |
| 796 | } |
| 797 | if (!strcmp(args[*cur_arg + 1], "client")) { |
| 798 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 799 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 800 | } |
| 801 | else if (!strcmp(args[*cur_arg + 1], "clientip")) { |
| 802 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 803 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 804 | } |
| 805 | else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) { |
| 806 | char *name, *end; |
| 807 | |
| 808 | name = args[*cur_arg + 1] + 7; |
| 809 | while (isspace(*name)) |
| 810 | name++; |
| 811 | |
| 812 | end = name; |
| 813 | while (*end && !isspace(*end) && *end != ',' && *end != ')') |
| 814 | end++; |
| 815 | |
| 816 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 817 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 818 | free(newsrv->conn_src.bind_hdr_name); |
| 819 | newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1); |
Remi Tricot-Le Breton | 75ffa1b | 2021-05-12 09:44:06 +0200 | [diff] [blame] | 820 | if (!newsrv->conn_src.bind_hdr_name) { |
| 821 | ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]); |
| 822 | goto err; |
| 823 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 824 | newsrv->conn_src.bind_hdr_len = end - name; |
| 825 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 826 | newsrv->conn_src.bind_hdr_name[end - name] = '\0'; |
| 827 | newsrv->conn_src.bind_hdr_occ = -1; |
| 828 | |
| 829 | /* now look for an occurrence number */ |
| 830 | while (isspace(*end)) |
| 831 | end++; |
| 832 | if (*end == ',') { |
| 833 | end++; |
| 834 | name = end; |
| 835 | if (*end == '-') |
| 836 | end++; |
| 837 | while (isdigit((int)*end)) |
| 838 | end++; |
| 839 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name); |
| 840 | } |
| 841 | |
| 842 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 843 | ha_alert("usesrc hdr_ip(name,num) does not support negative" |
| 844 | " occurrences values smaller than %d.\n", MAX_HDR_HISTORY); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 845 | goto err; |
| 846 | } |
| 847 | } |
| 848 | else { |
| 849 | struct sockaddr_storage *sk; |
| 850 | int port1, port2; |
| 851 | |
| 852 | /* 'sk' is statically allocated (no need to be freed). */ |
| 853 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1); |
| 854 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 855 | ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 856 | goto err; |
| 857 | } |
| 858 | |
| 859 | proto = protocol_by_family(sk->ss_family); |
| 860 | if (!proto || !proto->connect) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 861 | ha_alert("'%s %s' : connect() not supported for this address family.\n", |
| 862 | args[*cur_arg], args[*cur_arg + 1]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 863 | goto err; |
| 864 | } |
| 865 | |
| 866 | if (port1 != port2) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 867 | ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n", |
| 868 | args[*cur_arg], args[*cur_arg + 1]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 869 | goto err; |
| 870 | } |
| 871 | newsrv->conn_src.tproxy_addr = *sk; |
| 872 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 873 | } |
| 874 | global.last_checks |= LSTCHK_NETADM; |
| 875 | *cur_arg += 2; |
| 876 | continue; |
| 877 | #else /* no TPROXY support */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 878 | ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 879 | goto err; |
| 880 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
| 881 | } /* "usesrc" */ |
| 882 | |
| 883 | if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */ |
| 884 | #ifdef SO_BINDTODEVICE |
| 885 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 886 | ha_alert("'%s' : missing interface name.\n", args[0]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 887 | goto err; |
| 888 | } |
| 889 | free(newsrv->conn_src.iface_name); |
| 890 | newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]); |
| 891 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 892 | global.last_checks |= LSTCHK_NETADM; |
| 893 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 894 | ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 895 | goto err; |
| 896 | #endif |
| 897 | *cur_arg += 2; |
| 898 | continue; |
| 899 | } |
| 900 | /* this keyword in not an option of "source" */ |
| 901 | break; |
| 902 | } /* while */ |
| 903 | |
| 904 | return 0; |
| 905 | |
| 906 | err: |
| 907 | free(errmsg); |
| 908 | return ERR_ALERT | ERR_FATAL; |
| 909 | } |
| 910 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 911 | /* Parse the "stick" server keyword */ |
| 912 | static int srv_parse_stick(char **args, int *cur_arg, |
| 913 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 914 | { |
| 915 | newsrv->flags &= ~SRV_F_NON_STICK; |
| 916 | return 0; |
| 917 | } |
| 918 | |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 919 | /* Parse the "track" server keyword */ |
| 920 | static int srv_parse_track(char **args, int *cur_arg, |
| 921 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 922 | { |
| 923 | char *arg; |
| 924 | |
| 925 | arg = args[*cur_arg + 1]; |
| 926 | if (!*arg) { |
| 927 | memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n"); |
| 928 | return ERR_ALERT | ERR_FATAL; |
| 929 | } |
| 930 | |
| 931 | free(newsrv->trackit); |
| 932 | newsrv->trackit = strdup(arg); |
| 933 | |
| 934 | return 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /* Parse the "socks4" server keyword */ |
| 938 | static int srv_parse_socks4(char **args, int *cur_arg, |
| 939 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 940 | { |
| 941 | char *errmsg; |
| 942 | int port_low, port_high; |
| 943 | struct sockaddr_storage *sk; |
| 944 | struct protocol *proto; |
| 945 | |
| 946 | errmsg = NULL; |
| 947 | |
| 948 | if (!*args[*cur_arg + 1]) { |
| 949 | memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]); |
| 950 | goto err; |
| 951 | } |
| 952 | |
| 953 | /* 'sk' is statically allocated (no need to be freed). */ |
| 954 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1); |
| 955 | if (!sk) { |
| 956 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 957 | goto err; |
| 958 | } |
| 959 | |
| 960 | proto = protocol_by_family(sk->ss_family); |
| 961 | if (!proto || !proto->connect) { |
| 962 | ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]); |
| 963 | goto err; |
| 964 | } |
| 965 | |
| 966 | newsrv->flags |= SRV_F_SOCKS4_PROXY; |
| 967 | newsrv->socks4_addr = *sk; |
| 968 | |
| 969 | if (port_low != port_high) { |
| 970 | ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]); |
| 971 | goto err; |
| 972 | } |
| 973 | |
Willy Tarreau | bc90776 | 2020-09-15 11:25:54 +0200 | [diff] [blame] | 974 | if (port_low <= 0 || port_low > 65535) { |
| 975 | ha_alert("'%s': invalid port %d.\n", args[*cur_arg], port_low); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 976 | goto err; |
| 977 | } |
| 978 | |
| 979 | return 0; |
| 980 | |
| 981 | err: |
| 982 | free(errmsg); |
| 983 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 984 | } |
| 985 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 986 | |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 987 | /* parse the "tfo" server keyword */ |
| 988 | static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 989 | { |
| 990 | newsrv->flags |= SRV_F_FASTOPEN; |
| 991 | return 0; |
| 992 | } |
| 993 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 994 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 995 | * code in <why>, which must be one of SF_ERR_* indicating the reason for the |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 996 | * shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 997 | * |
| 998 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 999 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1000 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1001 | { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1002 | struct stream *stream, *stream_bck; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1003 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1004 | list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv) |
| 1005 | if (stream->srv_conn == srv) |
| 1006 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /* Shutdown all connections of all backup servers of a proxy. The caller must |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1010 | * pass a termination code in <why>, which must be one of SF_ERR_* indicating |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1011 | * the reason for the shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1012 | * |
| 1013 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1014 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1015 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1016 | { |
| 1017 | struct server *srv; |
| 1018 | |
| 1019 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 1020 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1021 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1024 | /* Appends some information to a message string related to a server going UP or |
| 1025 | * DOWN. If both <forced> and <reason> are null and the server tracks another |
| 1026 | * one, a "via" information will be provided to know where the status came from. |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1027 | * If <check> is non-null, an entire string describing the check result will be |
| 1028 | * appended after a comma and a space (eg: to report some information from the |
| 1029 | * check that changed the state). In the other case, the string will be built |
| 1030 | * using the check results stored into the struct server if present. |
| 1031 | * If <xferred> is non-negative, some information about requeued sessions are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1032 | * provided. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1033 | * |
| 1034 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1035 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1036 | void srv_append_status(struct buffer *msg, struct server *s, |
| 1037 | struct check *check, int xferred, int forced) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1038 | { |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1039 | short status = s->op_st_chg.status; |
| 1040 | short code = s->op_st_chg.code; |
| 1041 | long duration = s->op_st_chg.duration; |
| 1042 | char *desc = s->op_st_chg.reason; |
| 1043 | |
| 1044 | if (check) { |
| 1045 | status = check->status; |
| 1046 | code = check->code; |
| 1047 | duration = check->duration; |
| 1048 | desc = check->desc; |
| 1049 | } |
| 1050 | |
| 1051 | if (status != -1) { |
| 1052 | chunk_appendf(msg, ", reason: %s", get_check_status_description(status)); |
| 1053 | |
| 1054 | if (status >= HCHK_STATUS_L57DATA) |
| 1055 | chunk_appendf(msg, ", code: %d", code); |
| 1056 | |
| 1057 | if (desc && *desc) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1058 | struct buffer src; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1059 | |
| 1060 | chunk_appendf(msg, ", info: \""); |
| 1061 | |
| 1062 | chunk_initlen(&src, desc, 0, strlen(desc)); |
| 1063 | chunk_asciiencode(msg, &src, '"'); |
| 1064 | |
| 1065 | chunk_appendf(msg, "\""); |
| 1066 | } |
| 1067 | |
| 1068 | if (duration >= 0) |
| 1069 | chunk_appendf(msg, ", check duration: %ldms", duration); |
| 1070 | } |
| 1071 | else if (desc && *desc) { |
| 1072 | chunk_appendf(msg, ", %s", desc); |
| 1073 | } |
| 1074 | else if (!forced && s->track) { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1075 | chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1076 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1077 | |
| 1078 | if (xferred >= 0) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1079 | if (s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1080 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 1081 | " %d sessions active, %d requeued, %d remaining in queue", |
| 1082 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1083 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 1084 | s->cur_sess, xferred, s->nbpend); |
| 1085 | else |
| 1086 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 1087 | " %d sessions requeued, %d total in queue", |
| 1088 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1089 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 1090 | xferred, s->nbpend); |
| 1091 | } |
| 1092 | } |
| 1093 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1094 | /* Marks server <s> down, regardless of its checks' statuses. The server is |
| 1095 | * registered in a list to postpone the counting of the remaining servers on |
| 1096 | * the proxy and transfers queued streams whenever possible to other servers at |
| 1097 | * a sync point. Maintenance servers are ignored. It stores the <reason> if |
| 1098 | * non-null as the reason for going down or the available data from the check |
| 1099 | * struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1100 | * |
| 1101 | * Must be called with the server lock held. |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1102 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1103 | void srv_set_stopped(struct server *s, const char *reason, struct check *check) |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1104 | { |
| 1105 | struct server *srv; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1106 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1107 | if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1108 | return; |
| 1109 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1110 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1111 | *s->op_st_chg.reason = 0; |
| 1112 | s->op_st_chg.status = -1; |
| 1113 | if (reason) { |
| 1114 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1115 | } |
| 1116 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1117 | strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason)); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1118 | s->op_st_chg.code = check->code; |
| 1119 | s->op_st_chg.status = check->status; |
| 1120 | s->op_st_chg.duration = check->duration; |
| 1121 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1122 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1123 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1124 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1125 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1126 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1127 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1128 | srv_set_stopped(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1129 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1130 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1133 | /* Marks server <s> up regardless of its checks' statuses and provided it isn't |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1134 | * in maintenance. The server is registered in a list to postpone the counting |
| 1135 | * of the remaining servers on the proxy and tries to grab requests from the |
| 1136 | * proxy at a sync point. Maintenance servers are ignored. It stores the |
| 1137 | * <reason> if non-null as the reason for going down or the available data |
| 1138 | * from the check struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1139 | * |
| 1140 | * Must be called with the server lock held. |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1141 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1142 | void srv_set_running(struct server *s, const char *reason, struct check *check) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1143 | { |
| 1144 | struct server *srv; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1145 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1146 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1147 | return; |
| 1148 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1149 | if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1150 | return; |
| 1151 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1152 | s->next_state = SRV_ST_STARTING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1153 | *s->op_st_chg.reason = 0; |
| 1154 | s->op_st_chg.status = -1; |
| 1155 | if (reason) { |
| 1156 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1157 | } |
| 1158 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1159 | strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason)); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1160 | s->op_st_chg.code = check->code; |
| 1161 | s->op_st_chg.status = check->status; |
| 1162 | s->op_st_chg.duration = check->duration; |
| 1163 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1164 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1165 | if (s->slowstart <= 0) |
| 1166 | s->next_state = SRV_ST_RUNNING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1167 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1168 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1169 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1170 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1171 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1172 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1173 | srv_set_running(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1174 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1175 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1178 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1179 | * isn't in maintenance. The server is registered in a list to postpone the |
| 1180 | * counting of the remaining servers on the proxy and tries to grab requests |
| 1181 | * from the proxy. Maintenance servers are ignored. It stores the |
| 1182 | * <reason> if non-null as the reason for going down or the available data |
| 1183 | * from the check struct to recompute this reason later. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1184 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 1185 | * must not be placed there. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1186 | * |
| 1187 | * Must be called with the server lock held. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1188 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1189 | void srv_set_stopping(struct server *s, const char *reason, struct check *check) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1190 | { |
| 1191 | struct server *srv; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1192 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1193 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1194 | return; |
| 1195 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1196 | if (s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1197 | return; |
| 1198 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1199 | s->next_state = SRV_ST_STOPPING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1200 | *s->op_st_chg.reason = 0; |
| 1201 | s->op_st_chg.status = -1; |
| 1202 | if (reason) { |
| 1203 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1204 | } |
| 1205 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1206 | strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason)); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1207 | s->op_st_chg.code = check->code; |
| 1208 | s->op_st_chg.status = check->status; |
| 1209 | s->op_st_chg.duration = check->duration; |
| 1210 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1211 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1212 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1213 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1214 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1215 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1216 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1217 | srv_set_stopping(srv, NULL, NULL); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1218 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1219 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1220 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1221 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1222 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1223 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 1224 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 1225 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 1226 | * checks). When either the flag is already set or no flag is passed, nothing |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1227 | * is done. If <cause> is non-null, it will be displayed at the end of the log |
| 1228 | * lines to justify the state change. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1229 | * |
| 1230 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1231 | */ |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1232 | void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1233 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1234 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1235 | |
| 1236 | if (!mode) |
| 1237 | return; |
| 1238 | |
| 1239 | /* stop going down as soon as we meet a server already in the same state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1240 | if (s->next_admin & mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1241 | return; |
| 1242 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1243 | s->next_admin |= mode; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1244 | if (cause) |
| 1245 | strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause)); |
| 1246 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1247 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1248 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1249 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1250 | /* stop going down if the equivalent flag was already present (forced or inherited) */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1251 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) || |
| 1252 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1253 | return; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1254 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1255 | /* compute the inherited flag to propagate */ |
| 1256 | if (mode & SRV_ADMF_MAINT) |
| 1257 | mode = SRV_ADMF_IMAINT; |
| 1258 | else if (mode & SRV_ADMF_DRAIN) |
| 1259 | mode = SRV_ADMF_IDRAIN; |
| 1260 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1261 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1262 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1263 | srv_set_admin_flag(srv, mode, cause); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1264 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1265 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1268 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1269 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 1270 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 1271 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 1272 | * either the flag is already cleared or no flag is passed, nothing is done. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1273 | * |
| 1274 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1275 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1276 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1277 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1278 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1279 | |
| 1280 | if (!mode) |
| 1281 | return; |
| 1282 | |
| 1283 | /* stop going down as soon as we see the flag is not there anymore */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1284 | if (!(s->next_admin & mode)) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1285 | return; |
| 1286 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1287 | s->next_admin &= ~mode; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1288 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1289 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1290 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1291 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1292 | /* stop going down if the equivalent flag is still present (forced or inherited) */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1293 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) || |
| 1294 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1295 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1296 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1297 | if (mode & SRV_ADMF_MAINT) |
| 1298 | mode = SRV_ADMF_IMAINT; |
| 1299 | else if (mode & SRV_ADMF_DRAIN) |
| 1300 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1301 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1302 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1303 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1304 | srv_clr_admin_flag(srv, mode); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1305 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1306 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1307 | } |
| 1308 | |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1309 | /* principle: propagate maint and drain to tracking servers. This is useful |
| 1310 | * upon startup so that inherited states are correct. |
| 1311 | */ |
| 1312 | static void srv_propagate_admin_state(struct server *srv) |
| 1313 | { |
| 1314 | struct server *srv2; |
| 1315 | |
| 1316 | if (!srv->trackers) |
| 1317 | return; |
| 1318 | |
| 1319 | for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1320 | HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1321 | if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT)) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1322 | srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1323 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1324 | if (srv->next_admin & SRV_ADMF_DRAIN) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1325 | srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1326 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /* Compute and propagate the admin states for all servers in proxy <px>. |
| 1331 | * Only servers *not* tracking another one are considered, because other |
| 1332 | * ones will be handled when the server they track is visited. |
| 1333 | */ |
| 1334 | void srv_compute_all_admin_states(struct proxy *px) |
| 1335 | { |
| 1336 | struct server *srv; |
| 1337 | |
| 1338 | for (srv = px->srv; srv; srv = srv->next) { |
| 1339 | if (srv->track) |
| 1340 | continue; |
| 1341 | srv_propagate_admin_state(srv); |
| 1342 | } |
| 1343 | } |
| 1344 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1345 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1346 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1347 | * all code contributors. |
| 1348 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1349 | * the config parser can report an appropriate error when a known keyword was |
| 1350 | * not enabled. |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1351 | * Note: -1 as ->skip value means that the number of arguments are variable. |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1352 | */ |
| 1353 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
Frédéric Lécaille | 6e5e0d8 | 2017-03-20 16:30:18 +0100 | [diff] [blame] | 1354 | { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */ |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 1355 | { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */ |
Frédéric Lécaille | 1502cfd | 2017-03-10 15:36:14 +0100 | [diff] [blame] | 1356 | { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */ |
Frédéric Lécaille | 65aa356 | 2017-03-14 11:20:13 +0100 | [diff] [blame] | 1357 | { "check", srv_parse_check, 0, 1 }, /* enable health checks */ |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 1358 | { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* enable PROXY protocol for health checks */ |
Frédéric Lécaille | 9d1b95b | 2017-03-15 09:13:33 +0100 | [diff] [blame] | 1359 | { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */ |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 1360 | { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */ |
| 1361 | { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */ |
Frédéric Lécaille | 1502cfd | 2017-03-10 15:36:14 +0100 | [diff] [blame] | 1362 | { "id", srv_parse_id, 1, 0 }, /* set id# of server */ |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 1363 | { "max-reuse", srv_parse_max_reuse, 1, 1 }, /* Set the max number of requests on a connection, -1 means unlimited */ |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 1364 | { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */ |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 1365 | { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */ |
Frédéric Lécaille | 1502cfd | 2017-03-10 15:36:14 +0100 | [diff] [blame] | 1366 | { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */ |
Frédéric Lécaille | 65aa356 | 2017-03-14 11:20:13 +0100 | [diff] [blame] | 1367 | { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */ |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 1368 | { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* disable PROXY protol for health checks */ |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1369 | { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */ |
| 1370 | { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */ |
Frédéric Lécaille | aeeb1c9 | 2019-07-04 14:19:06 +0200 | [diff] [blame] | 1371 | { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */ |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1372 | { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */ |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1373 | { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */ |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 1374 | { "pool-max-conn", srv_parse_pool_max_conn, 1, 1 }, /* Set the max number of orphan idle connections, 0 means unlimited */ |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 1375 | { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */ |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1376 | { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */ |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1377 | { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */ |
Frédéric Lécaille | 1618623 | 2017-03-14 16:42:49 +0100 | [diff] [blame] | 1378 | { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */ |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1379 | { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */ |
| 1380 | { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */ |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1381 | { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */ |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1382 | { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */ |
Olivier Houchard | 1a9c25f | 2019-07-04 13:34:10 +0200 | [diff] [blame] | 1383 | { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */ |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1384 | { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */ |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1385 | { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/ |
| 1386 | { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */ |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1387 | { NULL, NULL, 0 }, |
| 1388 | }}; |
| 1389 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1390 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1391 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1392 | /* Recomputes the server's eweight based on its state, uweight, the current time, |
| 1393 | * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1394 | * state is automatically disabled if the time is elapsed. If <must_update> is |
| 1395 | * not zero, the update will be propagated immediately. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1396 | * |
| 1397 | * Must be called with the server lock held. |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1398 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1399 | void server_recalc_eweight(struct server *sv, int must_update) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1400 | { |
| 1401 | struct proxy *px = sv->proxy; |
| 1402 | unsigned w; |
| 1403 | |
| 1404 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 1405 | /* go to full throttle if the slowstart interval is reached */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1406 | if (sv->next_state == SRV_ST_STARTING) |
| 1407 | sv->next_state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 1411 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 1412 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1413 | if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1414 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 1415 | else |
| 1416 | w = px->lbprm.wdiv; |
| 1417 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1418 | sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1420 | /* propagate changes only if needed (i.e. not recursively) */ |
Willy Tarreau | 49725a0 | 2018-08-21 19:54:09 +0200 | [diff] [blame] | 1421 | if (must_update) |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1422 | srv_update_status(sv); |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1425 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1426 | * Parses weight_str and configures sv accordingly. |
| 1427 | * Returns NULL on success, error message string otherwise. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1428 | * |
| 1429 | * Must be called with the server lock held. |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1430 | */ |
| 1431 | const char *server_parse_weight_change_request(struct server *sv, |
| 1432 | const char *weight_str) |
| 1433 | { |
| 1434 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1435 | long int w; |
| 1436 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1437 | |
| 1438 | px = sv->proxy; |
| 1439 | |
| 1440 | /* if the weight is terminated with '%', it is set relative to |
| 1441 | * the initial weight, otherwise it is absolute. |
| 1442 | */ |
| 1443 | if (!*weight_str) |
| 1444 | return "Require <weight> or <weight%>.\n"; |
| 1445 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1446 | w = strtol(weight_str, &end, 10); |
| 1447 | if (end == weight_str) |
| 1448 | return "Empty weight string empty or preceded by garbage"; |
| 1449 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1450 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1451 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1452 | /* Avoid integer overflow */ |
| 1453 | if (w > 25600) |
| 1454 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1455 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1456 | if (w > 256) |
| 1457 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1458 | } |
| 1459 | else if (w < 0 || w > 256) |
| 1460 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1461 | else if (end[0] != '\0') |
| 1462 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1463 | |
| 1464 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 1465 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 1466 | |
| 1467 | sv->uweight = w; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1468 | server_recalc_eweight(sv, 1); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1469 | |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1473 | /* |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1474 | * Parses <addr_str> and configures <sv> accordingly. <from> precise |
| 1475 | * the source of the change in the associated message log. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1476 | * Returns: |
| 1477 | * - error string on error |
| 1478 | * - NULL on success |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1479 | * |
| 1480 | * Must be called with the server lock held. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1481 | */ |
| 1482 | const char *server_parse_addr_change_request(struct server *sv, |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1483 | const char *addr_str, const char *updater) |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1484 | { |
| 1485 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 1486 | |
| 1487 | if (inet_pton(AF_INET6, addr_str, ip)) { |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1488 | update_server_addr(sv, ip, AF_INET6, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1489 | return NULL; |
| 1490 | } |
| 1491 | if (inet_pton(AF_INET, addr_str, ip)) { |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1492 | update_server_addr(sv, ip, AF_INET, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1493 | return NULL; |
| 1494 | } |
| 1495 | |
| 1496 | return "Could not understand IP address format.\n"; |
| 1497 | } |
| 1498 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1499 | /* |
| 1500 | * Must be called with the server lock held. |
| 1501 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1502 | const char *server_parse_maxconn_change_request(struct server *sv, |
| 1503 | const char *maxconn_str) |
| 1504 | { |
| 1505 | long int v; |
| 1506 | char *end; |
| 1507 | |
| 1508 | if (!*maxconn_str) |
| 1509 | return "Require <maxconn>.\n"; |
| 1510 | |
| 1511 | v = strtol(maxconn_str, &end, 10); |
| 1512 | if (end == maxconn_str) |
| 1513 | return "maxconn string empty or preceded by garbage"; |
| 1514 | else if (end[0] != '\0') |
| 1515 | return "Trailing garbage in maxconn string"; |
| 1516 | |
| 1517 | if (sv->maxconn == sv->minconn) { // static maxconn |
| 1518 | sv->maxconn = sv->minconn = v; |
| 1519 | } else { // dynamic maxconn |
| 1520 | sv->maxconn = v; |
| 1521 | } |
| 1522 | |
Amaury Denoyelle | b0fbda5 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 1523 | /* server_parse_maxconn_change_request requires the server lock held. |
| 1524 | * Specify it to process_srv_queue to prevent a deadlock. |
| 1525 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1526 | if (may_dequeue_tasks(sv, sv->proxy)) |
Amaury Denoyelle | b0fbda5 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 1527 | process_srv_queue(sv, 1); |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1528 | |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1532 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1533 | static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px, |
| 1534 | const char *file, int linenum, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1535 | { |
| 1536 | int idx; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1537 | const char *args[] = { |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1538 | srv->sni_expr, |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1539 | NULL, |
| 1540 | }; |
| 1541 | |
| 1542 | idx = 0; |
Olivier Houchard | 7d8e688 | 2017-04-20 18:21:17 +0200 | [diff] [blame] | 1543 | px->conf.args.ctx = ARGC_SRV; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1544 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1545 | return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args); |
| 1546 | } |
| 1547 | |
| 1548 | static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err) |
| 1549 | { |
| 1550 | struct sample_expr *expr; |
| 1551 | |
| 1552 | expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1553 | if (!expr) { |
| 1554 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 1555 | return ERR_ALERT | ERR_FATAL; |
| 1556 | } |
| 1557 | |
| 1558 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 1559 | memprintf(err, "error detected while parsing sni expression : " |
| 1560 | " fetch method '%s' extracts information from '%s', " |
| 1561 | "none of which is available here.\n", |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1562 | newsrv->sni_expr, sample_src_names(expr->fetch->use)); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1563 | return ERR_ALERT | ERR_FATAL; |
| 1564 | } |
| 1565 | |
| 1566 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 1567 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 1568 | newsrv->ssl_ctx.sni = expr; |
| 1569 | |
| 1570 | return 0; |
| 1571 | } |
| 1572 | #endif |
| 1573 | |
| 1574 | static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err) |
| 1575 | { |
| 1576 | if (err && *err) { |
| 1577 | indent_msg(err, 2); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1578 | ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1579 | } |
| 1580 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1581 | ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n", |
| 1582 | file, linenum, args[0], args[1], args[cur_arg]); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1585 | static void srv_conn_src_sport_range_cpy(struct server *srv, |
| 1586 | struct server *src) |
| 1587 | { |
| 1588 | int range_sz; |
| 1589 | |
| 1590 | range_sz = src->conn_src.sport_range->size; |
| 1591 | if (range_sz > 0) { |
| 1592 | srv->conn_src.sport_range = port_range_alloc_range(range_sz); |
| 1593 | if (srv->conn_src.sport_range != NULL) { |
| 1594 | int i; |
| 1595 | |
| 1596 | for (i = 0; i < range_sz; i++) { |
| 1597 | srv->conn_src.sport_range->ports[i] = |
| 1598 | src->conn_src.sport_range->ports[i]; |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | /* |
| 1605 | * Copy <src> server connection source settings to <srv> server everything needed. |
| 1606 | */ |
| 1607 | static void srv_conn_src_cpy(struct server *srv, struct server *src) |
| 1608 | { |
| 1609 | srv->conn_src.opts = src->conn_src.opts; |
| 1610 | srv->conn_src.source_addr = src->conn_src.source_addr; |
| 1611 | |
| 1612 | /* Source port range copy. */ |
| 1613 | if (src->conn_src.sport_range != NULL) |
| 1614 | srv_conn_src_sport_range_cpy(srv, src); |
| 1615 | |
| 1616 | #ifdef CONFIG_HAP_TRANSPARENT |
| 1617 | if (src->conn_src.bind_hdr_name != NULL) { |
| 1618 | srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name); |
| 1619 | srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name); |
| 1620 | } |
| 1621 | srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ; |
| 1622 | srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr; |
| 1623 | #endif |
| 1624 | if (src->conn_src.iface_name != NULL) |
| 1625 | srv->conn_src.iface_name = strdup(src->conn_src.iface_name); |
| 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * Copy <src> server SSL settings to <srv> server allocating |
| 1630 | * everything needed. |
| 1631 | */ |
| 1632 | #if defined(USE_OPENSSL) |
| 1633 | static void srv_ssl_settings_cpy(struct server *srv, struct server *src) |
| 1634 | { |
| 1635 | if (src->ssl_ctx.ca_file != NULL) |
| 1636 | srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file); |
| 1637 | if (src->ssl_ctx.crl_file != NULL) |
| 1638 | srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file); |
| 1639 | if (src->ssl_ctx.client_crt != NULL) |
| 1640 | srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt); |
| 1641 | |
| 1642 | srv->ssl_ctx.verify = src->ssl_ctx.verify; |
| 1643 | |
| 1644 | if (src->ssl_ctx.verify_host != NULL) |
| 1645 | srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host); |
| 1646 | if (src->ssl_ctx.ciphers != NULL) |
| 1647 | srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers); |
Jerome Magnin | 770bc8a | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 1648 | if (src->ssl_ctx.options) |
| 1649 | srv->ssl_ctx.options = src->ssl_ctx.options; |
| 1650 | if (src->ssl_ctx.methods.flags) |
| 1651 | srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags; |
| 1652 | if (src->ssl_ctx.methods.min) |
| 1653 | srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min; |
| 1654 | if (src->ssl_ctx.methods.max) |
| 1655 | srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max; |
| 1656 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1657 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 1658 | if (src->ssl_ctx.ciphersuites != NULL) |
| 1659 | srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites); |
| 1660 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1661 | if (src->sni_expr != NULL) |
| 1662 | srv->sni_expr = strdup(src->sni_expr); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1663 | |
| 1664 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 1665 | if (src->ssl_ctx.alpn_str) { |
| 1666 | srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len); |
| 1667 | if (srv->ssl_ctx.alpn_str) { |
| 1668 | memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str, |
| 1669 | src->ssl_ctx.alpn_len); |
| 1670 | srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len; |
| 1671 | } |
| 1672 | } |
| 1673 | #endif |
| 1674 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1675 | if (src->ssl_ctx.npn_str) { |
| 1676 | srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len); |
| 1677 | if (srv->ssl_ctx.npn_str) { |
| 1678 | memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str, |
| 1679 | src->ssl_ctx.npn_len); |
| 1680 | srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len; |
| 1681 | } |
| 1682 | } |
| 1683 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1684 | } |
| 1685 | #endif |
| 1686 | |
| 1687 | /* |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1688 | * Prepare <srv> for hostname resolution. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1689 | * May be safely called with a default server as <src> argument (without hostname). |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 1690 | * Returns -1 in case of any allocation failure, 0 if not. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1691 | */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1692 | static int srv_prepare_for_resolution(struct server *srv, const char *hostname) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1693 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1694 | char *hostname_dn; |
| 1695 | int hostname_len, hostname_dn_len; |
| 1696 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1697 | if (!hostname) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 1698 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1699 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1700 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1701 | hostname_dn = trash.area; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1702 | hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1, |
| 1703 | hostname_dn, trash.size); |
| 1704 | if (hostname_dn_len == -1) |
| 1705 | goto err; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 1706 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1707 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1708 | free(srv->hostname); |
| 1709 | free(srv->hostname_dn); |
| 1710 | srv->hostname = strdup(hostname); |
| 1711 | srv->hostname_dn = strdup(hostname_dn); |
| 1712 | srv->hostname_dn_len = hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1713 | if (!srv->hostname || !srv->hostname_dn) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1714 | goto err; |
| 1715 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 1716 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1717 | |
| 1718 | err: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1719 | free(srv->hostname); srv->hostname = NULL; |
| 1720 | free(srv->hostname_dn); srv->hostname_dn = NULL; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 1721 | return -1; |
| 1722 | } |
| 1723 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1724 | /* |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1725 | * Copy <src> server settings to <srv> server allocating |
| 1726 | * everything needed. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1727 | * This function is not supposed to be called at any time, but only |
| 1728 | * during server settings parsing or during server allocations from |
| 1729 | * a server template, and just after having calloc()'ed a new server. |
| 1730 | * So, <src> may only be a default server (when parsing server settings) |
| 1731 | * or a server template (during server allocations from a server template). |
| 1732 | * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template, |
| 1733 | * 0 if not). |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1734 | */ |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1735 | static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1736 | { |
| 1737 | /* Connection source settings copy */ |
| 1738 | srv_conn_src_cpy(srv, src); |
| 1739 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1740 | if (srv_tmpl) { |
| 1741 | srv->addr = src->addr; |
| 1742 | srv->svc_port = src->svc_port; |
| 1743 | } |
| 1744 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1745 | srv->pp_opts = src->pp_opts; |
| 1746 | if (src->rdr_pfx != NULL) { |
| 1747 | srv->rdr_pfx = strdup(src->rdr_pfx); |
| 1748 | srv->rdr_len = src->rdr_len; |
| 1749 | } |
| 1750 | if (src->cookie != NULL) { |
| 1751 | srv->cookie = strdup(src->cookie); |
| 1752 | srv->cklen = src->cklen; |
| 1753 | } |
| 1754 | srv->use_ssl = src->use_ssl; |
Willy Tarreau | fb38956 | 2019-12-11 15:43:45 +0100 | [diff] [blame] | 1755 | srv->check.addr = src->check.addr; |
| 1756 | srv->agent.addr = src->agent.addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1757 | srv->check.use_ssl = src->check.use_ssl; |
| 1758 | srv->check.port = src->check.port; |
Olivier Houchard | 2194401 | 2018-12-21 19:42:01 +0100 | [diff] [blame] | 1759 | srv->check.sni = src->check.sni; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 1760 | srv->check.alpn_str = src->check.alpn_str; |
Ilya Shipitsin | 0c50b1e | 2019-04-30 21:21:28 +0500 | [diff] [blame] | 1761 | srv->check.alpn_len = src->check.alpn_len; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1762 | /* Note: 'flags' field has potentially been already initialized. */ |
| 1763 | srv->flags |= src->flags; |
| 1764 | srv->do_check = src->do_check; |
| 1765 | srv->do_agent = src->do_agent; |
| 1766 | if (srv->check.port) |
| 1767 | srv->flags |= SRV_F_CHECKPORT; |
| 1768 | srv->check.inter = src->check.inter; |
| 1769 | srv->check.fastinter = src->check.fastinter; |
| 1770 | srv->check.downinter = src->check.downinter; |
| 1771 | srv->agent.use_ssl = src->agent.use_ssl; |
| 1772 | srv->agent.port = src->agent.port; |
| 1773 | if (src->agent.send_string != NULL) |
| 1774 | srv->agent.send_string = strdup(src->agent.send_string); |
| 1775 | srv->agent.send_string_len = src->agent.send_string_len; |
| 1776 | srv->agent.inter = src->agent.inter; |
| 1777 | srv->agent.fastinter = src->agent.fastinter; |
| 1778 | srv->agent.downinter = src->agent.downinter; |
| 1779 | srv->maxqueue = src->maxqueue; |
| 1780 | srv->minconn = src->minconn; |
| 1781 | srv->maxconn = src->maxconn; |
| 1782 | srv->slowstart = src->slowstart; |
| 1783 | srv->observe = src->observe; |
| 1784 | srv->onerror = src->onerror; |
| 1785 | srv->onmarkeddown = src->onmarkeddown; |
| 1786 | srv->onmarkedup = src->onmarkedup; |
| 1787 | if (src->trackit != NULL) |
| 1788 | srv->trackit = strdup(src->trackit); |
| 1789 | srv->consecutive_errors_limit = src->consecutive_errors_limit; |
| 1790 | srv->uweight = srv->iweight = src->iweight; |
| 1791 | |
| 1792 | srv->check.send_proxy = src->check.send_proxy; |
| 1793 | /* health: up, but will fall down at first failure */ |
| 1794 | srv->check.rise = srv->check.health = src->check.rise; |
| 1795 | srv->check.fall = src->check.fall; |
| 1796 | |
| 1797 | /* Here we check if 'disabled' is the default server state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1798 | if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) { |
| 1799 | srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 1800 | srv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1801 | srv->check.state |= CHK_ST_PAUSED; |
| 1802 | srv->check.health = 0; |
| 1803 | } |
| 1804 | |
| 1805 | /* health: up but will fall down at first failure */ |
| 1806 | srv->agent.rise = srv->agent.health = src->agent.rise; |
| 1807 | srv->agent.fall = src->agent.fall; |
| 1808 | |
| 1809 | if (src->resolvers_id != NULL) |
| 1810 | srv->resolvers_id = strdup(src->resolvers_id); |
| 1811 | srv->dns_opts.family_prio = src->dns_opts.family_prio; |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 1812 | srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1813 | if (srv->dns_opts.family_prio == AF_UNSPEC) |
| 1814 | srv->dns_opts.family_prio = AF_INET6; |
| 1815 | memcpy(srv->dns_opts.pref_net, |
| 1816 | src->dns_opts.pref_net, |
| 1817 | sizeof srv->dns_opts.pref_net); |
| 1818 | srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb; |
| 1819 | |
| 1820 | srv->init_addr_methods = src->init_addr_methods; |
| 1821 | srv->init_addr = src->init_addr; |
| 1822 | #if defined(USE_OPENSSL) |
| 1823 | srv_ssl_settings_cpy(srv, src); |
| 1824 | #endif |
| 1825 | #ifdef TCP_USER_TIMEOUT |
| 1826 | srv->tcp_ut = src->tcp_ut; |
| 1827 | #endif |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1828 | srv->mux_proto = src->mux_proto; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 1829 | srv->pool_purge_delay = src->pool_purge_delay; |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 1830 | srv->max_idle_conns = src->max_idle_conns; |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 1831 | srv->max_reuse = src->max_reuse; |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1832 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1833 | if (srv_tmpl) |
| 1834 | srv->srvrq = src->srvrq; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1835 | |
| 1836 | srv->check.via_socks4 = src->check.via_socks4; |
| 1837 | srv->socks4_addr = src->socks4_addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
William Lallemand | 313bfd1 | 2018-10-26 14:47:32 +0200 | [diff] [blame] | 1840 | struct server *new_server(struct proxy *proxy) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1841 | { |
| 1842 | struct server *srv; |
| 1843 | |
| 1844 | srv = calloc(1, sizeof *srv); |
| 1845 | if (!srv) |
| 1846 | return NULL; |
| 1847 | |
| 1848 | srv->obj_type = OBJ_TYPE_SERVER; |
| 1849 | srv->proxy = proxy; |
| 1850 | LIST_INIT(&srv->actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 1851 | srv->pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 1852 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1853 | srv->next_state = SRV_ST_RUNNING; /* early server setup */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1854 | srv->last_change = now.tv_sec; |
| 1855 | |
| 1856 | srv->check.status = HCHK_STATUS_INI; |
| 1857 | srv->check.server = srv; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 1858 | srv->check.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1859 | srv->check.tcpcheck_rules = &proxy->tcpcheck_rules; |
| 1860 | |
| 1861 | srv->agent.status = HCHK_STATUS_INI; |
| 1862 | srv->agent.server = srv; |
Willy Tarreau | 1ba3203 | 2019-01-21 07:48:26 +0100 | [diff] [blame] | 1863 | srv->agent.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1864 | srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW); |
| 1865 | |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 1866 | /* please don't put default server settings here, they are set in |
| 1867 | * init_default_instance(). |
| 1868 | */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1869 | return srv; |
| 1870 | } |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1871 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1872 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1873 | static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg, |
| 1874 | struct server *srv, struct proxy *proxy) |
| 1875 | { |
| 1876 | int ret; |
| 1877 | char *err = NULL; |
| 1878 | |
| 1879 | if (!srv->sni_expr) |
| 1880 | return 0; |
| 1881 | |
| 1882 | ret = server_parse_sni_expr(srv, proxy, &err); |
| 1883 | if (!ret) |
| 1884 | return 0; |
| 1885 | |
| 1886 | display_parser_err(file, linenum, args, cur_arg, &err); |
| 1887 | free(err); |
| 1888 | |
| 1889 | return ret; |
| 1890 | } |
| 1891 | #endif |
| 1892 | |
| 1893 | /* |
| 1894 | * Server initializations finalization. |
| 1895 | * Initialize health check, agent check and SNI expression if enabled. |
| 1896 | * Must not be called for a default server instance. |
| 1897 | */ |
| 1898 | static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg, |
| 1899 | struct server *srv, struct proxy *px) |
| 1900 | { |
Christopher Faulet | 1ebb12c | 2020-04-27 11:17:10 +0200 | [diff] [blame] | 1901 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1902 | int ret; |
Christopher Faulet | 1ebb12c | 2020-04-27 11:17:10 +0200 | [diff] [blame] | 1903 | #endif |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1904 | |
Christopher Faulet | c6e0770 | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 1905 | if (srv->do_check && srv->trackit) { |
| 1906 | ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n", |
| 1907 | file, linenum); |
| 1908 | return ERR_ALERT | ERR_FATAL; |
| 1909 | } |
| 1910 | |
| 1911 | if (srv->do_agent && !srv->agent.port) { |
| 1912 | ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n", |
| 1913 | file, linenum, srv->id); |
| 1914 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1918 | if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0) |
| 1919 | return ret; |
| 1920 | #endif |
| 1921 | |
| 1922 | if (srv->flags & SRV_F_BACKUP) |
| 1923 | px->srv_bck++; |
| 1924 | else |
| 1925 | px->srv_act++; |
| 1926 | srv_lb_commit_status(srv); |
| 1927 | |
| 1928 | return 0; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 1929 | err: |
| 1930 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1931 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1932 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 1933 | /* |
| 1934 | * Parse as much as possible such a range string argument: low[-high] |
| 1935 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 1936 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 1937 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 1938 | * Returns 0 if succeeded, -1 if not. |
| 1939 | */ |
| 1940 | static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high) |
| 1941 | { |
| 1942 | char *nb_high_arg; |
| 1943 | |
| 1944 | *nb_high = 0; |
| 1945 | chunk_printf(&trash, "%s", arg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1946 | *nb_low = atoi(trash.area); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 1947 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1948 | if ((nb_high_arg = strchr(trash.area, '-'))) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 1949 | *nb_high_arg++ = '\0'; |
| 1950 | *nb_high = atoi(nb_high_arg); |
| 1951 | } |
| 1952 | else { |
| 1953 | *nb_high += *nb_low; |
| 1954 | *nb_low = 1; |
| 1955 | } |
| 1956 | |
| 1957 | if (*nb_low < 0 || *nb_high < *nb_low) |
| 1958 | return -1; |
| 1959 | |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1963 | static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb) |
| 1964 | { |
| 1965 | chunk_printf(&trash, "%s%d", prefix, nb); |
| 1966 | free(srv->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1967 | srv->id = strdup(trash.area); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /* |
| 1971 | * Initialize as much as possible servers from <srv> server template. |
| 1972 | * Note that a server template is a special server with |
| 1973 | * a few different parameters than a server which has |
| 1974 | * been parsed mostly the same way as a server. |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 1975 | * Returns the number of servers successfully allocated, |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1976 | * 'srv' template included. |
| 1977 | */ |
| 1978 | static int server_template_init(struct server *srv, struct proxy *px) |
| 1979 | { |
| 1980 | int i; |
| 1981 | struct server *newsrv; |
| 1982 | |
| 1983 | for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) { |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1984 | newsrv = new_server(px); |
| 1985 | if (!newsrv) |
| 1986 | goto err; |
| 1987 | |
Christopher Faulet | fa31ae9 | 2020-11-02 22:04:55 +0100 | [diff] [blame] | 1988 | newsrv->conf.file = strdup(srv->conf.file); |
| 1989 | newsrv->conf.line = srv->conf.line; |
| 1990 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1991 | srv_settings_cpy(newsrv, srv, 1); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1992 | srv_prepare_for_resolution(newsrv, srv->hostname); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1993 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1994 | if (newsrv->sni_expr) { |
| 1995 | newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL); |
| 1996 | if (!newsrv->ssl_ctx.sni) |
| 1997 | goto err; |
| 1998 | } |
| 1999 | #endif |
| 2000 | /* Set this new server ID. */ |
| 2001 | srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i); |
| 2002 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2003 | /* Linked backwards first. This will be restablished after parsing. */ |
| 2004 | newsrv->next = px->srv; |
| 2005 | px->srv = newsrv; |
| 2006 | } |
| 2007 | srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low); |
| 2008 | |
| 2009 | return i - srv->tmpl_info.nb_low; |
| 2010 | |
| 2011 | err: |
| 2012 | srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low); |
| 2013 | if (newsrv) { |
| 2014 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2015 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 2016 | #endif |
| 2017 | free_check(&newsrv->agent); |
| 2018 | free_check(&newsrv->check); |
| 2019 | } |
| 2020 | free(newsrv); |
| 2021 | return i - srv->tmpl_info.nb_low; |
| 2022 | } |
| 2023 | |
Emeric Brun | 33f0a73 | 2020-07-21 16:54:36 +0200 | [diff] [blame] | 2024 | int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, |
| 2025 | struct proxy *defproxy, int parse_addr, int in_peers_section, int initial_resolve) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2026 | { |
| 2027 | struct server *newsrv = NULL; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2028 | const char *err = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2029 | char *errmsg = NULL; |
| 2030 | int err_code = 0; |
| 2031 | unsigned val; |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2032 | char *fqdn = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2033 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2034 | if (!strcmp(args[0], "server") || |
Frédéric Lécaille | c06b5d4 | 2018-04-26 10:06:41 +0200 | [diff] [blame] | 2035 | !strcmp(args[0], "peer") || |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2036 | !strcmp(args[0], "default-server") || |
| 2037 | !strcmp(args[0], "server-template")) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2038 | int cur_arg; |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 2039 | int defsrv = (*args[0] == 'd'); |
Frédéric Lécaille | c06b5d4 | 2018-04-26 10:06:41 +0200 | [diff] [blame] | 2040 | int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server")); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2041 | int srv_tmpl = !defsrv && !srv; |
| 2042 | int tmpl_range_low = 0, tmpl_range_high = 0; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2043 | |
| 2044 | if (!defsrv && curproxy == defproxy) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2045 | ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2046 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2047 | goto out; |
| 2048 | } |
| 2049 | else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) |
Olivier Houchard | 306e653 | 2018-07-24 16:48:59 +0200 | [diff] [blame] | 2050 | err_code |= ERR_WARN; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2051 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2052 | /* There is no mandatory first arguments for default server. */ |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2053 | if (srv && parse_addr) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2054 | if (!*args[2]) { |
Frédéric Lécaille | 9b0d59d | 2020-04-03 09:43:47 +0200 | [diff] [blame] | 2055 | if (in_peers_section) { |
| 2056 | return 0; |
| 2057 | } |
| 2058 | else { |
| 2059 | /* 'server' line number of argument check. */ |
| 2060 | ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 2061 | file, linenum, args[0]); |
| 2062 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2063 | goto out; |
| 2064 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | err = invalid_char(args[1]); |
| 2068 | } |
| 2069 | else if (srv_tmpl) { |
| 2070 | if (!*args[3]) { |
| 2071 | /* 'server-template' line number of argument check. */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2072 | ha_alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n", |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2073 | file, linenum, args[0]); |
| 2074 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2075 | goto out; |
| 2076 | } |
| 2077 | |
| 2078 | err = invalid_prefix_char(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2081 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2082 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n", |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2083 | file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2084 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2085 | goto out; |
| 2086 | } |
| 2087 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2088 | cur_arg = 2; |
| 2089 | if (srv_tmpl) { |
| 2090 | /* Parse server-template <nb | range> arg. */ |
| 2091 | if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2092 | ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n", |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2093 | file, linenum, args[0], args[cur_arg]); |
| 2094 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2095 | goto out; |
| 2096 | } |
| 2097 | cur_arg++; |
| 2098 | } |
| 2099 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2100 | if (!defsrv) { |
| 2101 | struct sockaddr_storage *sk; |
Willy Tarreau | 6ecb10a | 2017-01-06 18:36:06 +0100 | [diff] [blame] | 2102 | int port1, port2, port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2103 | struct protocol *proto; |
| 2104 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2105 | newsrv = new_server(curproxy); |
| 2106 | if (!newsrv) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2107 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2108 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2109 | goto out; |
| 2110 | } |
| 2111 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2112 | if (srv_tmpl) { |
| 2113 | newsrv->tmpl_info.nb_low = tmpl_range_low; |
| 2114 | newsrv->tmpl_info.nb_high = tmpl_range_high; |
| 2115 | } |
| 2116 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2117 | /* the servers are linked backwards first */ |
| 2118 | newsrv->next = curproxy->srv; |
| 2119 | curproxy->srv = newsrv; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2120 | newsrv->conf.file = strdup(file); |
| 2121 | newsrv->conf.line = linenum; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2122 | /* Note: for a server template, its id is its prefix. |
| 2123 | * This is a temporary id which will be used for server allocations to come |
| 2124 | * after parsing. |
| 2125 | */ |
| 2126 | if (srv) |
| 2127 | newsrv->id = strdup(args[1]); |
| 2128 | else |
| 2129 | newsrv->tmpl_info.prefix = strdup(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2130 | |
| 2131 | /* several ways to check the port component : |
| 2132 | * - IP => port=+0, relative (IPv4 only) |
| 2133 | * - IP: => port=+0, relative |
| 2134 | * - IP:N => port=N, absolute |
| 2135 | * - IP:+N => port=+N, relative |
| 2136 | * - IP:-N => port=-N, relative |
| 2137 | */ |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2138 | if (!parse_addr) |
| 2139 | goto skip_addr; |
| 2140 | |
Emeric Brun | 33f0a73 | 2020-07-21 16:54:36 +0200 | [diff] [blame] | 2141 | sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, initial_resolve); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2142 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2143 | ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2144 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2145 | goto out; |
| 2146 | } |
| 2147 | |
| 2148 | proto = protocol_by_family(sk->ss_family); |
Willy Tarreau | 9698f4b | 2017-01-06 18:42:57 +0100 | [diff] [blame] | 2149 | if (!fqdn && (!proto || !proto->connect)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2150 | ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2151 | file, linenum, args[0], args[1]); |
| 2152 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2153 | goto out; |
| 2154 | } |
| 2155 | |
| 2156 | if (!port1 || !port2) { |
| 2157 | /* no port specified, +offset, -offset */ |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 2158 | newsrv->flags |= SRV_F_MAPPORTS; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2159 | } |
| 2160 | else if (port1 != port2) { |
| 2161 | /* port range */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2162 | ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2163 | file, linenum, args[0], args[1], args[2]); |
| 2164 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2165 | goto out; |
| 2166 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2167 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2168 | /* save hostname and create associated name resolution */ |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2169 | if (fqdn) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2170 | if (fqdn[0] == '_') { /* SRV record */ |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2171 | /* Check if a SRV request already exists, and if not, create it */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2172 | if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL) |
| 2173 | newsrv->srvrq = new_dns_srvrq(newsrv, fqdn); |
| 2174 | if (newsrv->srvrq == NULL) { |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2175 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2176 | goto out; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2180 | ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n", |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2181 | file, linenum, newsrv->id); |
| 2182 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2183 | goto out; |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2184 | } |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2185 | } |
| 2186 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2187 | newsrv->addr = *sk; |
Willy Tarreau | 6ecb10a | 2017-01-06 18:36:06 +0100 | [diff] [blame] | 2188 | newsrv->svc_port = port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2189 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2190 | if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2191 | ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n", |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2192 | file, linenum, newsrv->addr.ss_family, args[cur_arg]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2193 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2194 | goto out; |
| 2195 | } |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 2196 | |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2197 | cur_arg++; |
| 2198 | skip_addr: |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2199 | /* Copy default server settings to new server settings. */ |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2200 | srv_settings_cpy(newsrv, &curproxy->defsrv, 0); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2201 | HA_SPIN_INIT(&newsrv->lock); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2202 | } else { |
| 2203 | newsrv = &curproxy->defsrv; |
| 2204 | cur_arg = 1; |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 2205 | newsrv->dns_opts.family_prio = AF_INET6; |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 2206 | newsrv->dns_opts.accept_duplicate_ip = 0; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | while (*args[cur_arg]) { |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 2210 | if (!strcmp(args[cur_arg], "agent-inter")) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2211 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2212 | |
| 2213 | if (err == PARSE_TIME_OVER) { |
| 2214 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2215 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2216 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2217 | goto out; |
| 2218 | } |
| 2219 | else if (err == PARSE_TIME_UNDER) { |
| 2220 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2221 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2222 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2223 | goto out; |
| 2224 | } |
| 2225 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2226 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2227 | file, linenum, *err, newsrv->id); |
| 2228 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2229 | goto out; |
| 2230 | } |
| 2231 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2232 | ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2233 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2234 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2235 | goto out; |
| 2236 | } |
| 2237 | newsrv->agent.inter = val; |
| 2238 | cur_arg += 2; |
| 2239 | } |
Misiek | ea84933 | 2017-01-09 09:39:51 +0100 | [diff] [blame] | 2240 | else if (!strcmp(args[cur_arg], "agent-addr")) { |
| 2241 | if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2242 | ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]); |
Misiek | ea84933 | 2017-01-09 09:39:51 +0100 | [diff] [blame] | 2243 | goto out; |
| 2244 | } |
| 2245 | |
| 2246 | cur_arg += 2; |
| 2247 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2248 | else if (!strcmp(args[cur_arg], "agent-port")) { |
| 2249 | global.maxsock++; |
| 2250 | newsrv->agent.port = atol(args[cur_arg + 1]); |
| 2251 | cur_arg += 2; |
| 2252 | } |
James Brown | 55f9ff1 | 2015-10-21 18:19:05 -0700 | [diff] [blame] | 2253 | else if (!strcmp(args[cur_arg], "agent-send")) { |
| 2254 | global.maxsock++; |
| 2255 | free(newsrv->agent.send_string); |
| 2256 | newsrv->agent.send_string_len = strlen(args[cur_arg + 1]); |
| 2257 | newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1); |
| 2258 | memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len); |
| 2259 | cur_arg += 2; |
| 2260 | } |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2261 | else if (!strcmp(args[cur_arg], "init-addr")) { |
| 2262 | char *p, *end; |
| 2263 | int done; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 2264 | struct sockaddr_storage sa; |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2265 | |
| 2266 | newsrv->init_addr_methods = 0; |
| 2267 | memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr)); |
| 2268 | |
| 2269 | for (p = args[cur_arg + 1]; *p; p = end) { |
| 2270 | /* cut on next comma */ |
| 2271 | for (end = p; *end && *end != ','; end++); |
| 2272 | if (*end) |
| 2273 | *(end++) = 0; |
| 2274 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 2275 | memset(&sa, 0, sizeof(sa)); |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2276 | if (!strcmp(p, "libc")) { |
| 2277 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC); |
| 2278 | } |
| 2279 | else if (!strcmp(p, "last")) { |
| 2280 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST); |
| 2281 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 2282 | else if (!strcmp(p, "none")) { |
| 2283 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE); |
| 2284 | } |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 2285 | else if (str2ip2(p, &sa, 0)) { |
| 2286 | if (is_addr(&newsrv->init_addr)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2287 | ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n", |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 2288 | file, linenum, args[cur_arg], p); |
| 2289 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2290 | goto out; |
| 2291 | } |
| 2292 | newsrv->init_addr = sa; |
| 2293 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP); |
| 2294 | } |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2295 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2296 | ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n", |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2297 | file, linenum, args[cur_arg], p); |
| 2298 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2299 | goto out; |
| 2300 | } |
| 2301 | if (!done) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2302 | ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n", |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2303 | file, linenum, args[cur_arg], p); |
| 2304 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2305 | goto out; |
| 2306 | } |
| 2307 | } |
| 2308 | cur_arg += 2; |
| 2309 | } |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2310 | else if (!strcmp(args[cur_arg], "resolvers")) { |
Frédéric Lécaille | daa2fe6 | 2017-04-20 12:17:50 +0200 | [diff] [blame] | 2311 | free(newsrv->resolvers_id); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2312 | newsrv->resolvers_id = strdup(args[cur_arg + 1]); |
| 2313 | cur_arg += 2; |
| 2314 | } |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 2315 | else if (!strcmp(args[cur_arg], "resolve-opts")) { |
| 2316 | char *p, *end; |
| 2317 | |
| 2318 | for (p = args[cur_arg + 1]; *p; p = end) { |
| 2319 | /* cut on next comma */ |
| 2320 | for (end = p; *end && *end != ','; end++); |
| 2321 | if (*end) |
| 2322 | *(end++) = 0; |
| 2323 | |
| 2324 | if (!strcmp(p, "allow-dup-ip")) { |
| 2325 | newsrv->dns_opts.accept_duplicate_ip = 1; |
| 2326 | } |
| 2327 | else if (!strcmp(p, "prevent-dup-ip")) { |
| 2328 | newsrv->dns_opts.accept_duplicate_ip = 0; |
| 2329 | } |
| 2330 | else { |
| 2331 | ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n", |
| 2332 | file, linenum, args[cur_arg], p); |
| 2333 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2334 | goto out; |
| 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | cur_arg += 2; |
| 2339 | } |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2340 | else if (!strcmp(args[cur_arg], "resolve-prefer")) { |
| 2341 | if (!strcmp(args[cur_arg + 1], "ipv4")) |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 2342 | newsrv->dns_opts.family_prio = AF_INET; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2343 | else if (!strcmp(args[cur_arg + 1], "ipv6")) |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 2344 | newsrv->dns_opts.family_prio = AF_INET6; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2345 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2346 | ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n", |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2347 | file, linenum, args[cur_arg]); |
| 2348 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2349 | goto out; |
| 2350 | } |
| 2351 | cur_arg += 2; |
| 2352 | } |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2353 | else if (!strcmp(args[cur_arg], "resolve-net")) { |
| 2354 | char *p, *e; |
| 2355 | unsigned char mask; |
| 2356 | struct dns_options *opt; |
| 2357 | |
| 2358 | if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2359 | ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n", |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2360 | file, linenum, args[cur_arg]); |
| 2361 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2362 | goto out; |
| 2363 | } |
| 2364 | |
| 2365 | opt = &newsrv->dns_opts; |
| 2366 | |
| 2367 | /* Split arguments by comma, and convert it from ipv4 or ipv6 |
| 2368 | * string network in in_addr or in6_addr. |
| 2369 | */ |
| 2370 | p = args[cur_arg + 1]; |
| 2371 | e = p; |
| 2372 | while (*p != '\0') { |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 2373 | /* If no room available, return error. */ |
David Carlier | d10025c | 2016-04-08 10:26:44 +0100 | [diff] [blame] | 2374 | if (opt->pref_net_nb >= SRV_MAX_PREF_NET) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2375 | ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n", |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2376 | file, linenum, args[cur_arg], SRV_MAX_PREF_NET); |
| 2377 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2378 | goto out; |
| 2379 | } |
| 2380 | /* look for end or comma. */ |
| 2381 | while (*e != ',' && *e != '\0') |
| 2382 | e++; |
| 2383 | if (*e == ',') { |
| 2384 | *e = '\0'; |
| 2385 | e++; |
| 2386 | } |
| 2387 | if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4, |
| 2388 | &opt->pref_net[opt->pref_net_nb].mask.in4)) { |
| 2389 | /* Try to convert input string from ipv4 or ipv6 network. */ |
| 2390 | opt->pref_net[opt->pref_net_nb].family = AF_INET; |
| 2391 | } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6, |
| 2392 | &mask)) { |
| 2393 | /* Try to convert input string from ipv6 network. */ |
| 2394 | len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6); |
| 2395 | opt->pref_net[opt->pref_net_nb].family = AF_INET6; |
| 2396 | } else { |
| 2397 | /* All network conversions fail, retrun error. */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2398 | ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n", |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2399 | file, linenum, args[cur_arg], p); |
| 2400 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2401 | goto out; |
| 2402 | } |
| 2403 | opt->pref_net_nb++; |
| 2404 | p = e; |
| 2405 | } |
| 2406 | |
| 2407 | cur_arg += 2; |
| 2408 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2409 | else if (!strcmp(args[cur_arg], "rise")) { |
| 2410 | if (!*args[cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2411 | ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2412 | file, linenum, args[cur_arg]); |
| 2413 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2414 | goto out; |
| 2415 | } |
| 2416 | |
| 2417 | newsrv->check.rise = atol(args[cur_arg + 1]); |
| 2418 | if (newsrv->check.rise <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2419 | ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2420 | file, linenum, args[cur_arg]); |
| 2421 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2422 | goto out; |
| 2423 | } |
| 2424 | |
| 2425 | if (newsrv->check.health) |
| 2426 | newsrv->check.health = newsrv->check.rise; |
| 2427 | cur_arg += 2; |
| 2428 | } |
| 2429 | else if (!strcmp(args[cur_arg], "fall")) { |
| 2430 | newsrv->check.fall = atol(args[cur_arg + 1]); |
| 2431 | |
| 2432 | if (!*args[cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2433 | ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2434 | file, linenum, args[cur_arg]); |
| 2435 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2436 | goto out; |
| 2437 | } |
| 2438 | |
| 2439 | if (newsrv->check.fall <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2440 | ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2441 | file, linenum, args[cur_arg]); |
| 2442 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2443 | goto out; |
| 2444 | } |
| 2445 | |
| 2446 | cur_arg += 2; |
| 2447 | } |
| 2448 | else if (!strcmp(args[cur_arg], "inter")) { |
| 2449 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2450 | |
| 2451 | if (err == PARSE_TIME_OVER) { |
| 2452 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2453 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2454 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2455 | goto out; |
| 2456 | } |
| 2457 | else if (err == PARSE_TIME_UNDER) { |
| 2458 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2459 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2460 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2461 | goto out; |
| 2462 | } |
| 2463 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2464 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2465 | file, linenum, *err, newsrv->id); |
| 2466 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2467 | goto out; |
| 2468 | } |
| 2469 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2470 | ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2471 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2472 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2473 | goto out; |
| 2474 | } |
| 2475 | newsrv->check.inter = val; |
| 2476 | cur_arg += 2; |
| 2477 | } |
| 2478 | else if (!strcmp(args[cur_arg], "fastinter")) { |
| 2479 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2480 | |
| 2481 | if (err == PARSE_TIME_OVER) { |
| 2482 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2483 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2484 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2485 | goto out; |
| 2486 | } |
| 2487 | else if (err == PARSE_TIME_UNDER) { |
| 2488 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2489 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2490 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2491 | goto out; |
| 2492 | } |
| 2493 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2494 | ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2495 | file, linenum, *err, newsrv->id); |
| 2496 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2497 | goto out; |
| 2498 | } |
| 2499 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2500 | ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2501 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2502 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2503 | goto out; |
| 2504 | } |
| 2505 | newsrv->check.fastinter = val; |
| 2506 | cur_arg += 2; |
| 2507 | } |
| 2508 | else if (!strcmp(args[cur_arg], "downinter")) { |
| 2509 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2510 | |
| 2511 | if (err == PARSE_TIME_OVER) { |
| 2512 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2513 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2514 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2515 | goto out; |
| 2516 | } |
| 2517 | else if (err == PARSE_TIME_UNDER) { |
| 2518 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2519 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2520 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2521 | goto out; |
| 2522 | } |
| 2523 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2524 | ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2525 | file, linenum, *err, newsrv->id); |
| 2526 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2527 | goto out; |
| 2528 | } |
| 2529 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2530 | ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2531 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2532 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2533 | goto out; |
| 2534 | } |
| 2535 | newsrv->check.downinter = val; |
| 2536 | cur_arg += 2; |
| 2537 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2538 | else if (!strcmp(args[cur_arg], "port")) { |
| 2539 | newsrv->check.port = atol(args[cur_arg + 1]); |
Baptiste Assmann | 6b453f1 | 2016-08-11 23:12:18 +0200 | [diff] [blame] | 2540 | newsrv->flags |= SRV_F_CHECKPORT; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2541 | cur_arg += 2; |
| 2542 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2543 | else if (!strcmp(args[cur_arg], "weight")) { |
| 2544 | int w; |
| 2545 | w = atol(args[cur_arg + 1]); |
| 2546 | if (w < 0 || w > SRV_UWGHT_MAX) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2547 | ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2548 | file, linenum, newsrv->id, SRV_UWGHT_MAX, w); |
| 2549 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2550 | goto out; |
| 2551 | } |
| 2552 | newsrv->uweight = newsrv->iweight = w; |
| 2553 | cur_arg += 2; |
| 2554 | } |
| 2555 | else if (!strcmp(args[cur_arg], "minconn")) { |
| 2556 | newsrv->minconn = atol(args[cur_arg + 1]); |
| 2557 | cur_arg += 2; |
| 2558 | } |
| 2559 | else if (!strcmp(args[cur_arg], "maxconn")) { |
| 2560 | newsrv->maxconn = atol(args[cur_arg + 1]); |
| 2561 | cur_arg += 2; |
| 2562 | } |
| 2563 | else if (!strcmp(args[cur_arg], "maxqueue")) { |
| 2564 | newsrv->maxqueue = atol(args[cur_arg + 1]); |
| 2565 | cur_arg += 2; |
| 2566 | } |
| 2567 | else if (!strcmp(args[cur_arg], "slowstart")) { |
| 2568 | /* slowstart is stored in seconds */ |
| 2569 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2570 | |
| 2571 | if (err == PARSE_TIME_OVER) { |
| 2572 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2573 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2574 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2575 | goto out; |
| 2576 | } |
| 2577 | else if (err == PARSE_TIME_UNDER) { |
| 2578 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2579 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2580 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2581 | goto out; |
| 2582 | } |
| 2583 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2584 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2585 | file, linenum, *err, newsrv->id); |
| 2586 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2587 | goto out; |
| 2588 | } |
| 2589 | newsrv->slowstart = (val + 999) / 1000; |
| 2590 | cur_arg += 2; |
| 2591 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2592 | else if (!strcmp(args[cur_arg], "on-error")) { |
| 2593 | if (!strcmp(args[cur_arg + 1], "fastinter")) |
| 2594 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 2595 | else if (!strcmp(args[cur_arg + 1], "fail-check")) |
| 2596 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 2597 | else if (!strcmp(args[cur_arg + 1], "sudden-death")) |
| 2598 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 2599 | else if (!strcmp(args[cur_arg + 1], "mark-down")) |
| 2600 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 2601 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2602 | ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', " |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2603 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n", |
| 2604 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 2605 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2606 | goto out; |
| 2607 | } |
| 2608 | |
| 2609 | cur_arg += 2; |
| 2610 | } |
| 2611 | else if (!strcmp(args[cur_arg], "on-marked-down")) { |
| 2612 | if (!strcmp(args[cur_arg + 1], "shutdown-sessions")) |
| 2613 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 2614 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2615 | ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2616 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 2617 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2618 | goto out; |
| 2619 | } |
| 2620 | |
| 2621 | cur_arg += 2; |
| 2622 | } |
| 2623 | else if (!strcmp(args[cur_arg], "on-marked-up")) { |
| 2624 | if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions")) |
| 2625 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 2626 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2627 | ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2628 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 2629 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2630 | goto out; |
| 2631 | } |
| 2632 | |
| 2633 | cur_arg += 2; |
| 2634 | } |
| 2635 | else if (!strcmp(args[cur_arg], "error-limit")) { |
| 2636 | if (!*args[cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2637 | ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2638 | file, linenum, args[cur_arg]); |
| 2639 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2640 | goto out; |
| 2641 | } |
| 2642 | |
| 2643 | newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]); |
| 2644 | |
| 2645 | if (newsrv->consecutive_errors_limit <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2646 | ha_alert("parsing [%s:%d]: %s has to be > 0.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2647 | file, linenum, args[cur_arg]); |
| 2648 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2649 | goto out; |
| 2650 | } |
| 2651 | cur_arg += 2; |
| 2652 | } |
Frédéric Lécaille | 8d083ed | 2017-04-14 15:19:56 +0200 | [diff] [blame] | 2653 | else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2654 | ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2655 | file, linenum, "usesrc", "source"); |
| 2656 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2657 | goto out; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 2658 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2659 | else { |
| 2660 | static int srv_dumped; |
| 2661 | struct srv_kw *kw; |
| 2662 | char *err; |
| 2663 | |
| 2664 | kw = srv_find_kw(args[cur_arg]); |
| 2665 | if (kw) { |
| 2666 | char *err = NULL; |
| 2667 | int code; |
| 2668 | |
| 2669 | if (!kw->parse) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2670 | ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2671 | file, linenum, args[0], args[1], args[cur_arg]); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2672 | if (kw->skip != -1) |
| 2673 | cur_arg += 1 + kw->skip ; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2674 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2675 | goto out; |
| 2676 | } |
| 2677 | |
| 2678 | if (defsrv && !kw->default_ok) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2679 | ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2680 | file, linenum, args[0], args[1], args[cur_arg]); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2681 | if (kw->skip != -1) |
| 2682 | cur_arg += 1 + kw->skip ; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2683 | err_code |= ERR_ALERT; |
| 2684 | continue; |
| 2685 | } |
| 2686 | |
| 2687 | code = kw->parse(args, &cur_arg, curproxy, newsrv, &err); |
| 2688 | err_code |= code; |
| 2689 | |
| 2690 | if (code) { |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2691 | display_parser_err(file, linenum, args, cur_arg, &err); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2692 | if (code & ERR_FATAL) { |
| 2693 | free(err); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2694 | if (kw->skip != -1) |
| 2695 | cur_arg += 1 + kw->skip; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2696 | goto out; |
| 2697 | } |
| 2698 | } |
| 2699 | free(err); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2700 | if (kw->skip != -1) |
| 2701 | cur_arg += 1 + kw->skip; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2702 | continue; |
| 2703 | } |
| 2704 | |
| 2705 | err = NULL; |
| 2706 | if (!srv_dumped) { |
| 2707 | srv_dump_kws(&err); |
| 2708 | indent_msg(&err, 4); |
| 2709 | srv_dumped = 1; |
| 2710 | } |
| 2711 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2712 | ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2713 | file, linenum, args[0], args[1], args[cur_arg], |
| 2714 | err ? " Registered keywords :" : "", err ? err : ""); |
| 2715 | free(err); |
| 2716 | |
| 2717 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2718 | goto out; |
| 2719 | } |
| 2720 | } |
| 2721 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2722 | if (!defsrv) |
| 2723 | err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy); |
| 2724 | if (err_code & ERR_FATAL) |
| 2725 | goto out; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2726 | if (srv_tmpl) |
| 2727 | server_template_init(newsrv, curproxy); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2728 | } |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2729 | free(fqdn); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2730 | return 0; |
| 2731 | |
| 2732 | out: |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2733 | free(fqdn); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2734 | free(errmsg); |
| 2735 | return err_code; |
| 2736 | } |
| 2737 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 2738 | /* Returns a pointer to the first server matching either id <id>. |
| 2739 | * NULL is returned if no match is found. |
| 2740 | * the lookup is performed in the backend <bk> |
| 2741 | */ |
| 2742 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 2743 | { |
| 2744 | struct eb32_node *eb32; |
| 2745 | struct server *curserver; |
| 2746 | |
| 2747 | if (!bk || (id ==0)) |
| 2748 | return NULL; |
| 2749 | |
| 2750 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 2751 | if (!(bk->cap & PR_CAP_BE)) |
| 2752 | return NULL; |
| 2753 | |
| 2754 | curserver = NULL; |
| 2755 | |
| 2756 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 2757 | if (eb32) |
| 2758 | curserver = container_of(eb32, struct server, conf.id); |
| 2759 | |
| 2760 | return curserver; |
| 2761 | } |
| 2762 | |
| 2763 | /* Returns a pointer to the first server matching either name <name>, or id |
| 2764 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 2765 | * the lookup is performed in the backend <bk> |
| 2766 | */ |
| 2767 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 2768 | { |
| 2769 | struct server *curserver; |
| 2770 | |
| 2771 | if (!bk || !name) |
| 2772 | return NULL; |
| 2773 | |
| 2774 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 2775 | if (!(bk->cap & PR_CAP_BE)) |
| 2776 | return NULL; |
| 2777 | |
| 2778 | curserver = NULL; |
| 2779 | if (*name == '#') { |
| 2780 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 2781 | if (curserver) |
| 2782 | return curserver; |
| 2783 | } |
| 2784 | else { |
| 2785 | curserver = bk->srv; |
| 2786 | |
| 2787 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 2788 | curserver = curserver->next; |
| 2789 | |
| 2790 | if (curserver) |
| 2791 | return curserver; |
| 2792 | } |
| 2793 | |
| 2794 | return NULL; |
| 2795 | } |
| 2796 | |
| 2797 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 2798 | { |
| 2799 | struct server *byname; |
| 2800 | struct server *byid; |
| 2801 | |
| 2802 | if (!name && !id) |
| 2803 | return NULL; |
| 2804 | |
| 2805 | if (diff) |
| 2806 | *diff = 0; |
| 2807 | |
| 2808 | byname = byid = NULL; |
| 2809 | |
| 2810 | if (name) { |
| 2811 | byname = server_find_by_name(bk, name); |
| 2812 | if (byname && (!id || byname->puid == id)) |
| 2813 | return byname; |
| 2814 | } |
| 2815 | |
| 2816 | /* remaining possibilities : |
| 2817 | * - name not set |
| 2818 | * - name set but not found |
| 2819 | * - name found but ID doesn't match |
| 2820 | */ |
| 2821 | if (id) { |
| 2822 | byid = server_find_by_id(bk, id); |
| 2823 | if (byid) { |
| 2824 | if (byname) { |
| 2825 | /* use id only if forced by configuration */ |
| 2826 | if (byid->flags & SRV_F_FORCED_ID) { |
| 2827 | if (diff) |
| 2828 | *diff |= 2; |
| 2829 | return byid; |
| 2830 | } |
| 2831 | else { |
| 2832 | if (diff) |
| 2833 | *diff |= 1; |
| 2834 | return byname; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | /* remaining possibilities: |
| 2839 | * - name not set |
| 2840 | * - name set but not found |
| 2841 | */ |
| 2842 | if (name && diff) |
| 2843 | *diff |= 2; |
| 2844 | return byid; |
| 2845 | } |
| 2846 | |
| 2847 | /* id bot found */ |
| 2848 | if (byname) { |
| 2849 | if (diff) |
| 2850 | *diff |= 1; |
| 2851 | return byname; |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | return NULL; |
| 2856 | } |
| 2857 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 2858 | /* Update a server state using the parameters available in the params list. |
| 2859 | * |
| 2860 | * Grabs the server lock during operation. |
| 2861 | */ |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2862 | static void srv_update_state(struct server *srv, int version, char **params) |
| 2863 | { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2864 | char *p; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2865 | struct buffer *msg; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2866 | |
| 2867 | /* fields since version 1 |
| 2868 | * and common to all other upcoming versions |
| 2869 | */ |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2870 | enum srv_state srv_op_state; |
| 2871 | enum srv_admin srv_admin_state; |
| 2872 | unsigned srv_uweight, srv_iweight; |
| 2873 | unsigned long srv_last_time_change; |
| 2874 | short srv_check_status; |
| 2875 | enum chk_result srv_check_result; |
| 2876 | int srv_check_health; |
| 2877 | int srv_check_state, srv_agent_state; |
| 2878 | int bk_f_forced_id; |
| 2879 | int srv_f_forced_id; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2880 | int fqdn_set_by_cli; |
| 2881 | const char *fqdn; |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 2882 | const char *port_str; |
| 2883 | unsigned int port; |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 2884 | char *srvrecord; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2885 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2886 | fqdn = NULL; |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 2887 | port = 0; |
Willy Tarreau | 31138fa | 2015-09-29 18:38:47 +0200 | [diff] [blame] | 2888 | msg = get_trash_chunk(); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2889 | switch (version) { |
| 2890 | case 1: |
| 2891 | /* |
| 2892 | * now we can proceed with server's state update: |
| 2893 | * srv_addr: params[0] |
| 2894 | * srv_op_state: params[1] |
| 2895 | * srv_admin_state: params[2] |
| 2896 | * srv_uweight: params[3] |
| 2897 | * srv_iweight: params[4] |
| 2898 | * srv_last_time_change: params[5] |
| 2899 | * srv_check_status: params[6] |
| 2900 | * srv_check_result: params[7] |
| 2901 | * srv_check_health: params[8] |
| 2902 | * srv_check_state: params[9] |
| 2903 | * srv_agent_state: params[10] |
| 2904 | * bk_f_forced_id: params[11] |
| 2905 | * srv_f_forced_id: params[12] |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2906 | * srv_fqdn: params[13] |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 2907 | * srv_port: params[14] |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 2908 | * srvrecord: params[15] |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2909 | */ |
| 2910 | |
| 2911 | /* validating srv_op_state */ |
| 2912 | p = NULL; |
| 2913 | errno = 0; |
| 2914 | srv_op_state = strtol(params[1], &p, 10); |
| 2915 | if ((p == params[1]) || errno == EINVAL || errno == ERANGE || |
| 2916 | (srv_op_state != SRV_ST_STOPPED && |
| 2917 | srv_op_state != SRV_ST_STARTING && |
| 2918 | srv_op_state != SRV_ST_RUNNING && |
| 2919 | srv_op_state != SRV_ST_STOPPING)) { |
| 2920 | chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]); |
| 2921 | } |
| 2922 | |
| 2923 | /* validating srv_admin_state */ |
| 2924 | p = NULL; |
| 2925 | errno = 0; |
| 2926 | srv_admin_state = strtol(params[2], &p, 10); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2927 | fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 2928 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2929 | /* inherited statuses will be recomputed later. |
| 2930 | * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn). |
| 2931 | */ |
Christopher Faulet | f02bcb9 | 2021-02-12 17:36:08 +0100 | [diff] [blame] | 2932 | srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT & ~SRV_ADMF_RMAINT; |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 2933 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2934 | if ((p == params[2]) || errno == EINVAL || errno == ERANGE || |
| 2935 | (srv_admin_state != 0 && |
| 2936 | srv_admin_state != SRV_ADMF_FMAINT && |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2937 | srv_admin_state != SRV_ADMF_CMAINT && |
| 2938 | srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) && |
Willy Tarreau | e1bde14 | 2016-11-03 18:33:25 +0100 | [diff] [blame] | 2939 | srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) && |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 2940 | srv_admin_state != SRV_ADMF_FDRAIN)) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2941 | chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]); |
| 2942 | } |
| 2943 | |
| 2944 | /* validating srv_uweight */ |
| 2945 | p = NULL; |
| 2946 | errno = 0; |
| 2947 | srv_uweight = strtol(params[3], &p, 10); |
Willy Tarreau | e1aebb2 | 2015-09-29 18:32:57 +0200 | [diff] [blame] | 2948 | if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX)) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2949 | chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]); |
| 2950 | |
| 2951 | /* validating srv_iweight */ |
| 2952 | p = NULL; |
| 2953 | errno = 0; |
| 2954 | srv_iweight = strtol(params[4], &p, 10); |
Willy Tarreau | e1aebb2 | 2015-09-29 18:32:57 +0200 | [diff] [blame] | 2955 | if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX)) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2956 | chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]); |
| 2957 | |
| 2958 | /* validating srv_last_time_change */ |
| 2959 | p = NULL; |
| 2960 | errno = 0; |
| 2961 | srv_last_time_change = strtol(params[5], &p, 10); |
| 2962 | if ((p == params[5]) || errno == EINVAL || errno == ERANGE) |
| 2963 | chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]); |
| 2964 | |
| 2965 | /* validating srv_check_status */ |
| 2966 | p = NULL; |
| 2967 | errno = 0; |
| 2968 | srv_check_status = strtol(params[6], &p, 10); |
| 2969 | if (p == params[6] || errno == EINVAL || errno == ERANGE || |
| 2970 | (srv_check_status >= HCHK_STATUS_SIZE)) |
| 2971 | chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]); |
| 2972 | |
| 2973 | /* validating srv_check_result */ |
| 2974 | p = NULL; |
| 2975 | errno = 0; |
| 2976 | srv_check_result = strtol(params[7], &p, 10); |
| 2977 | if ((p == params[7]) || errno == EINVAL || errno == ERANGE || |
| 2978 | (srv_check_result != CHK_RES_UNKNOWN && |
| 2979 | srv_check_result != CHK_RES_NEUTRAL && |
| 2980 | srv_check_result != CHK_RES_FAILED && |
| 2981 | srv_check_result != CHK_RES_PASSED && |
| 2982 | srv_check_result != CHK_RES_CONDPASS)) { |
| 2983 | chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]); |
| 2984 | } |
| 2985 | |
| 2986 | /* validating srv_check_health */ |
| 2987 | p = NULL; |
| 2988 | errno = 0; |
| 2989 | srv_check_health = strtol(params[8], &p, 10); |
| 2990 | if (p == params[8] || errno == EINVAL || errno == ERANGE) |
| 2991 | chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]); |
| 2992 | |
| 2993 | /* validating srv_check_state */ |
| 2994 | p = NULL; |
| 2995 | errno = 0; |
| 2996 | srv_check_state = strtol(params[9], &p, 10); |
| 2997 | if (p == params[9] || errno == EINVAL || errno == ERANGE || |
| 2998 | (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT))) |
| 2999 | chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]); |
| 3000 | |
| 3001 | /* validating srv_agent_state */ |
| 3002 | p = NULL; |
| 3003 | errno = 0; |
| 3004 | srv_agent_state = strtol(params[10], &p, 10); |
| 3005 | if (p == params[10] || errno == EINVAL || errno == ERANGE || |
| 3006 | (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT))) |
| 3007 | chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]); |
| 3008 | |
| 3009 | /* validating bk_f_forced_id */ |
| 3010 | p = NULL; |
| 3011 | errno = 0; |
| 3012 | bk_f_forced_id = strtol(params[11], &p, 10); |
| 3013 | if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1))) |
| 3014 | chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]); |
| 3015 | |
| 3016 | /* validating srv_f_forced_id */ |
| 3017 | p = NULL; |
| 3018 | errno = 0; |
| 3019 | srv_f_forced_id = strtol(params[12], &p, 10); |
| 3020 | if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1))) |
| 3021 | chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]); |
| 3022 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3023 | /* validating srv_fqdn */ |
| 3024 | fqdn = params[13]; |
| 3025 | if (fqdn && *fqdn == '-') |
| 3026 | fqdn = NULL; |
| 3027 | if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) { |
| 3028 | chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]); |
| 3029 | fqdn = NULL; |
| 3030 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3031 | |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 3032 | port_str = params[14]; |
| 3033 | if (port_str) { |
| 3034 | port = strl2uic(port_str, strlen(port_str)); |
| 3035 | if (port > USHRT_MAX) { |
| 3036 | chunk_appendf(msg, ", invalid srv_port value '%s'", port_str); |
| 3037 | port_str = NULL; |
| 3038 | } |
| 3039 | } |
| 3040 | |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 3041 | /* SRV record |
| 3042 | * NOTE: in HAProxy, SRV records must start with an underscore '_' |
| 3043 | */ |
| 3044 | srvrecord = params[15]; |
| 3045 | if (srvrecord && *srvrecord != '_') |
| 3046 | srvrecord = NULL; |
| 3047 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3048 | /* don't apply anything if one error has been detected */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3049 | if (msg->data) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3050 | goto out; |
| 3051 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3052 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3053 | /* recover operational state and apply it to this server |
| 3054 | * and all servers tracking this one */ |
Jérôme Magnin | f57afa4 | 2019-01-20 11:27:40 +0100 | [diff] [blame] | 3055 | srv->check.health = srv_check_health; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3056 | switch (srv_op_state) { |
| 3057 | case SRV_ST_STOPPED: |
| 3058 | srv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3059 | srv_set_stopped(srv, "changed from server-state after a reload", NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3060 | break; |
| 3061 | case SRV_ST_STARTING: |
Jérôme Magnin | f57afa4 | 2019-01-20 11:27:40 +0100 | [diff] [blame] | 3062 | /* If rise == 1 there is no STARTING state, let's switch to |
| 3063 | * RUNNING |
| 3064 | */ |
| 3065 | if (srv->check.rise == 1) { |
| 3066 | srv->check.health = srv->check.rise + srv->check.fall - 1; |
| 3067 | srv_set_running(srv, "", NULL); |
| 3068 | break; |
| 3069 | } |
| 3070 | if (srv->check.health < 1 || srv->check.health >= srv->check.rise) |
| 3071 | srv->check.health = srv->check.rise - 1; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3072 | srv->next_state = srv_op_state; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3073 | break; |
| 3074 | case SRV_ST_STOPPING: |
Jérôme Magnin | f57afa4 | 2019-01-20 11:27:40 +0100 | [diff] [blame] | 3075 | /* If fall == 1 there is no STOPPING state, let's switch to |
| 3076 | * STOPPED |
| 3077 | */ |
| 3078 | if (srv->check.fall == 1) { |
| 3079 | srv->check.health = 0; |
| 3080 | srv_set_stopped(srv, "changed from server-state after a reload", NULL); |
| 3081 | break; |
| 3082 | } |
| 3083 | if (srv->check.health < srv->check.rise || |
| 3084 | srv->check.health > srv->check.rise + srv->check.fall - 2) |
| 3085 | srv->check.health = srv->check.rise; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3086 | srv_set_stopping(srv, "changed from server-state after a reload", NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3087 | break; |
| 3088 | case SRV_ST_RUNNING: |
| 3089 | srv->check.health = srv->check.rise + srv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3090 | srv_set_running(srv, "", NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | |
| 3094 | /* When applying server state, the following rules apply: |
| 3095 | * - in case of a configuration change, we apply the setting from the new |
| 3096 | * configuration, regardless of old running state |
| 3097 | * - if no configuration change, we apply old running state only if old running |
| 3098 | * state is different from new configuration state |
| 3099 | */ |
| 3100 | /* configuration has changed */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3101 | if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) { |
| 3102 | if (srv->next_admin & SRV_ADMF_CMAINT) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3103 | srv_adm_set_maint(srv); |
| 3104 | else |
| 3105 | srv_adm_set_ready(srv); |
| 3106 | } |
| 3107 | /* configuration is the same, let's compate old running state and new conf state */ |
| 3108 | else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3109 | if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT)) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3110 | srv_adm_set_maint(srv); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3111 | else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT)) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3112 | srv_adm_set_ready(srv); |
| 3113 | } |
| 3114 | /* apply drain mode if server is currently enabled */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3115 | if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3116 | /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0 |
Willy Tarreau | 22cace2 | 2016-11-03 18:19:49 +0100 | [diff] [blame] | 3117 | * (srv->iweight is the weight set up in configuration). |
| 3118 | * There are two possible reasons for FDRAIN to have been present : |
| 3119 | * - previous config weight was zero |
| 3120 | * - "set server b/s drain" was sent to the CLI |
| 3121 | * |
| 3122 | * In the first case, we simply want to drop this drain state |
| 3123 | * if the new weight is not zero anymore, meaning the administrator |
| 3124 | * has intentionally turned the weight back to a positive value to |
| 3125 | * enable the server again after an operation. In the second case, |
| 3126 | * the drain state was forced on the CLI regardless of the config's |
| 3127 | * weight so we don't want a change to the config weight to lose this |
| 3128 | * status. What this means is : |
| 3129 | * - if previous weight was 0 and new one is >0, drop the DRAIN state. |
| 3130 | * - if the previous weight was >0, keep it. |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3131 | */ |
Willy Tarreau | 22cace2 | 2016-11-03 18:19:49 +0100 | [diff] [blame] | 3132 | if (srv_iweight > 0 || srv->iweight == 0) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3133 | srv_adm_set_drain(srv); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | srv->last_change = date.tv_sec - srv_last_time_change; |
| 3137 | srv->check.status = srv_check_status; |
| 3138 | srv->check.result = srv_check_result; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3139 | |
| 3140 | /* Only case we want to apply is removing ENABLED flag which could have been |
| 3141 | * done by the "disable health" command over the stats socket |
| 3142 | */ |
| 3143 | if ((srv->check.state & CHK_ST_CONFIGURED) && |
| 3144 | (srv_check_state & CHK_ST_CONFIGURED) && |
| 3145 | !(srv_check_state & CHK_ST_ENABLED)) |
| 3146 | srv->check.state &= ~CHK_ST_ENABLED; |
| 3147 | |
| 3148 | /* Only case we want to apply is removing ENABLED flag which could have been |
| 3149 | * done by the "disable agent" command over the stats socket |
| 3150 | */ |
| 3151 | if ((srv->agent.state & CHK_ST_CONFIGURED) && |
| 3152 | (srv_agent_state & CHK_ST_CONFIGURED) && |
| 3153 | !(srv_agent_state & CHK_ST_ENABLED)) |
| 3154 | srv->agent.state &= ~CHK_ST_ENABLED; |
| 3155 | |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 3156 | /* We want to apply the previous 'running' weight (srv_uweight) only if there |
| 3157 | * was no change in the configuration: both previous and new iweight are equals |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3158 | * |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 3159 | * It means that a configuration file change has precedence over a unix socket change |
| 3160 | * for server's weight |
| 3161 | * |
| 3162 | * by default, HAProxy applies the following weight when parsing the configuration |
| 3163 | * srv->uweight = srv->iweight |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3164 | */ |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 3165 | if (srv_iweight == srv->iweight) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3166 | srv->uweight = srv_uweight; |
| 3167 | } |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 3168 | server_recalc_eweight(srv, 1); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3169 | |
Willy Tarreau | e5a6068 | 2016-11-09 14:54:53 +0100 | [diff] [blame] | 3170 | /* load server IP address */ |
Daniel Corbett | 9215ffa | 2018-05-19 19:43:24 -0400 | [diff] [blame] | 3171 | if (strcmp(params[0], "-")) |
| 3172 | srv->lastaddr = strdup(params[0]); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3173 | |
| 3174 | if (fqdn && srv->hostname) { |
| 3175 | if (!strcmp(srv->hostname, fqdn)) { |
| 3176 | /* Here we reset the 'set from stats socket FQDN' flag |
| 3177 | * to support such transitions: |
| 3178 | * Let's say initial FQDN value is foo1 (in configuration file). |
| 3179 | * - FQDN changed from stats socket, from foo1 to foo2 value, |
| 3180 | * - FQDN changed again from file configuration (with the same previous value |
| 3181 | set from stats socket, from foo1 to foo2 value), |
| 3182 | * - reload for any other reason than a FQDN modification, |
| 3183 | * the configuration file FQDN matches the fqdn server state file value. |
| 3184 | * So we must reset the 'set from stats socket FQDN' flag to be consistent with |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 3185 | * any further FQDN modification. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3186 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3187 | srv->next_admin &= ~SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3188 | } |
| 3189 | else { |
| 3190 | /* If the FDQN has been changed from stats socket, |
| 3191 | * apply fqdn state file value (which is the value set |
| 3192 | * from stats socket). |
| 3193 | */ |
| 3194 | if (fqdn_set_by_cli) { |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 3195 | srv_set_fqdn(srv, fqdn, 0); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3196 | srv->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3197 | } |
| 3198 | } |
| 3199 | } |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 3200 | /* If all the conditions below are validated, this means |
| 3201 | * we're evaluating a server managed by SRV resolution |
| 3202 | */ |
| 3203 | else if (fqdn && !srv->hostname && srvrecord) { |
| 3204 | int res; |
| 3205 | |
| 3206 | /* we can't apply previous state if SRV record has changed */ |
| 3207 | if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) { |
| 3208 | 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); |
| 3209 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 3210 | goto out; |
| 3211 | } |
| 3212 | |
| 3213 | /* create or find a SRV resolution for this srv record */ |
| 3214 | if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL) |
| 3215 | srv->srvrq = new_dns_srvrq(srv, srvrecord); |
| 3216 | if (srv->srvrq == NULL) { |
| 3217 | chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id); |
| 3218 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 3219 | goto out; |
| 3220 | } |
| 3221 | |
| 3222 | /* prepare DNS resolution for this server */ |
| 3223 | res = srv_prepare_for_resolution(srv, fqdn); |
| 3224 | if (res == -1) { |
| 3225 | chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id); |
| 3226 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 3227 | goto out; |
| 3228 | } |
| 3229 | |
| 3230 | /* configure check.port accordingly */ |
| 3231 | if ((srv->check.state & CHK_ST_CONFIGURED) && |
| 3232 | !(srv->flags & SRV_F_CHECKPORT)) |
| 3233 | srv->check.port = port; |
| 3234 | |
| 3235 | /* Unset SRV_F_MAPPORTS for SRV records. |
| 3236 | * SRV_F_MAPPORTS is unfortunately set by parse_server() |
| 3237 | * because no ports are provided in the configuration file. |
| 3238 | * This is because HAProxy will use the port found into the SRV record. |
| 3239 | */ |
| 3240 | srv->flags &= ~SRV_F_MAPPORTS; |
| 3241 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3242 | |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 3243 | if (port_str) |
| 3244 | srv->svc_port = port; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3245 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 3246 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3247 | break; |
| 3248 | default: |
| 3249 | chunk_appendf(msg, ", version '%d' not supported", version); |
| 3250 | } |
| 3251 | |
| 3252 | out: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3253 | if (msg->data) { |
Baptiste Assmann | 0821bb9 | 2016-01-21 00:20:50 +0100 | [diff] [blame] | 3254 | chunk_appendf(msg, "\n"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3255 | ha_warning("server-state application failed for server '%s/%s'%s", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3256 | srv->proxy->id, srv->id, msg->area); |
Baptiste Assmann | 0821bb9 | 2016-01-21 00:20:50 +0100 | [diff] [blame] | 3257 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | /* This function parses all the proxies and only take care of the backends (since we're looking for server) |
| 3261 | * For each proxy, it does the following: |
| 3262 | * - opens its server state file (either one or local one) |
| 3263 | * - read whole file, line by line |
| 3264 | * - analyse each line to check if it matches our current backend: |
| 3265 | * - backend name matches |
| 3266 | * - backend id matches if id is forced and name doesn't match |
| 3267 | * - if the server pointed by the line is found, then state is applied |
| 3268 | * |
| 3269 | * If the running backend uuid or id differs from the state file, then HAProxy reports |
| 3270 | * a warning. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3271 | * |
| 3272 | * Grabs the server's lock via srv_update_state(). |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3273 | */ |
| 3274 | void apply_server_state(void) |
| 3275 | { |
| 3276 | char *cur, *end; |
| 3277 | char mybuf[SRV_STATE_LINE_MAXLEN]; |
| 3278 | int mybuflen; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3279 | char *params[SRV_STATE_FILE_MAX_FIELDS] = {0}; |
| 3280 | char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0}; |
Baptiste Assmann | 95c2c01 | 2019-06-11 14:51:49 +0200 | [diff] [blame] | 3281 | int arg, srv_arg, version; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3282 | FILE *f; |
| 3283 | char *filepath; |
| 3284 | char globalfilepath[MAXPATHLEN + 1]; |
| 3285 | char localfilepath[MAXPATHLEN + 1]; |
| 3286 | int len, fileopenerr, globalfilepathlen, localfilepathlen; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3287 | struct proxy *curproxy, *bk; |
| 3288 | struct server *srv; |
| 3289 | |
| 3290 | globalfilepathlen = 0; |
| 3291 | /* create the globalfilepath variable */ |
| 3292 | if (global.server_state_file) { |
| 3293 | /* absolute path or no base directory provided */ |
| 3294 | if ((global.server_state_file[0] == '/') || (!global.server_state_base)) { |
| 3295 | len = strlen(global.server_state_file); |
| 3296 | if (len > MAXPATHLEN) { |
| 3297 | globalfilepathlen = 0; |
| 3298 | goto globalfileerror; |
| 3299 | } |
| 3300 | memcpy(globalfilepath, global.server_state_file, len); |
| 3301 | globalfilepath[len] = '\0'; |
| 3302 | globalfilepathlen = len; |
| 3303 | } |
| 3304 | else if (global.server_state_base) { |
| 3305 | len = strlen(global.server_state_base); |
Willy Tarreau | 5dfb6c4 | 2018-10-16 19:26:12 +0200 | [diff] [blame] | 3306 | if (len > MAXPATHLEN) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3307 | globalfilepathlen = 0; |
| 3308 | goto globalfileerror; |
| 3309 | } |
Olivier Houchard | 17f8b90 | 2018-10-16 18:35:01 +0200 | [diff] [blame] | 3310 | memcpy(globalfilepath, global.server_state_base, len); |
Willy Tarreau | 5dfb6c4 | 2018-10-16 19:26:12 +0200 | [diff] [blame] | 3311 | globalfilepath[len] = 0; |
| 3312 | globalfilepathlen = len; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3313 | |
| 3314 | /* append a slash if needed */ |
| 3315 | if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') { |
| 3316 | if (globalfilepathlen + 1 > MAXPATHLEN) { |
| 3317 | globalfilepathlen = 0; |
| 3318 | goto globalfileerror; |
| 3319 | } |
| 3320 | globalfilepath[globalfilepathlen++] = '/'; |
| 3321 | } |
| 3322 | |
| 3323 | len = strlen(global.server_state_file); |
| 3324 | if (globalfilepathlen + len > MAXPATHLEN) { |
| 3325 | globalfilepathlen = 0; |
| 3326 | goto globalfileerror; |
| 3327 | } |
| 3328 | memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len); |
| 3329 | globalfilepathlen += len; |
| 3330 | globalfilepath[globalfilepathlen++] = 0; |
| 3331 | } |
| 3332 | } |
| 3333 | globalfileerror: |
| 3334 | if (globalfilepathlen == 0) |
| 3335 | globalfilepath[0] = '\0'; |
| 3336 | |
| 3337 | /* read servers state from local file */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 3338 | for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3339 | /* servers are only in backends */ |
| 3340 | if (!(curproxy->cap & PR_CAP_BE)) |
| 3341 | continue; |
| 3342 | fileopenerr = 0; |
| 3343 | filepath = NULL; |
| 3344 | |
| 3345 | /* search server state file path and name */ |
| 3346 | switch (curproxy->load_server_state_from_file) { |
| 3347 | /* read servers state from global file */ |
| 3348 | case PR_SRV_STATE_FILE_GLOBAL: |
| 3349 | /* there was an error while generating global server state file path */ |
| 3350 | if (globalfilepathlen == 0) |
| 3351 | continue; |
| 3352 | filepath = globalfilepath; |
| 3353 | fileopenerr = 1; |
| 3354 | break; |
| 3355 | /* this backend has its own file */ |
| 3356 | case PR_SRV_STATE_FILE_LOCAL: |
| 3357 | localfilepathlen = 0; |
| 3358 | localfilepath[0] = '\0'; |
| 3359 | len = 0; |
| 3360 | /* create the localfilepath variable */ |
| 3361 | /* absolute path or no base directory provided */ |
| 3362 | if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) { |
| 3363 | len = strlen(curproxy->server_state_file_name); |
| 3364 | if (len > MAXPATHLEN) { |
| 3365 | localfilepathlen = 0; |
| 3366 | goto localfileerror; |
| 3367 | } |
| 3368 | memcpy(localfilepath, curproxy->server_state_file_name, len); |
| 3369 | localfilepath[len] = '\0'; |
| 3370 | localfilepathlen = len; |
| 3371 | } |
| 3372 | else if (global.server_state_base) { |
| 3373 | len = strlen(global.server_state_base); |
| 3374 | localfilepathlen += len; |
| 3375 | |
| 3376 | if (localfilepathlen > MAXPATHLEN) { |
| 3377 | localfilepathlen = 0; |
| 3378 | goto localfileerror; |
| 3379 | } |
Olivier Houchard | 17f8b90 | 2018-10-16 18:35:01 +0200 | [diff] [blame] | 3380 | memcpy(localfilepath, global.server_state_base, len); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3381 | localfilepath[localfilepathlen] = 0; |
| 3382 | |
| 3383 | /* append a slash if needed */ |
| 3384 | if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') { |
| 3385 | if (localfilepathlen + 1 > MAXPATHLEN) { |
| 3386 | localfilepathlen = 0; |
| 3387 | goto localfileerror; |
| 3388 | } |
| 3389 | localfilepath[localfilepathlen++] = '/'; |
| 3390 | } |
| 3391 | |
| 3392 | len = strlen(curproxy->server_state_file_name); |
| 3393 | if (localfilepathlen + len > MAXPATHLEN) { |
| 3394 | localfilepathlen = 0; |
| 3395 | goto localfileerror; |
| 3396 | } |
| 3397 | memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len); |
| 3398 | localfilepathlen += len; |
| 3399 | localfilepath[localfilepathlen++] = 0; |
| 3400 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3401 | localfileerror: |
| 3402 | if (localfilepathlen == 0) |
Christopher Faulet | 559a6d3 | 2021-02-12 16:31:03 +0100 | [diff] [blame] | 3403 | continue; |
| 3404 | filepath = localfilepath; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3405 | |
| 3406 | break; |
| 3407 | case PR_SRV_STATE_FILE_NONE: |
| 3408 | default: |
| 3409 | continue; |
| 3410 | } |
| 3411 | |
| 3412 | /* preload global state file */ |
| 3413 | errno = 0; |
| 3414 | f = fopen(filepath, "r"); |
| 3415 | if (errno && fileopenerr) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3416 | ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno)); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3417 | if (!f) |
| 3418 | continue; |
| 3419 | |
| 3420 | mybuf[0] = '\0'; |
| 3421 | mybuflen = 0; |
| 3422 | version = 0; |
| 3423 | |
| 3424 | /* first character of first line of the file must contain the version of the export */ |
Dragan Dosen | cf4fb03 | 2015-11-04 23:03:26 +0100 | [diff] [blame] | 3425 | if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3426 | ha_warning("Can't read first line of the server state file '%s'\n", filepath); |
Dragan Dosen | cf4fb03 | 2015-11-04 23:03:26 +0100 | [diff] [blame] | 3427 | goto fileclose; |
| 3428 | } |
| 3429 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3430 | cur = mybuf; |
| 3431 | version = atoi(cur); |
| 3432 | if ((version < SRV_STATE_FILE_VERSION_MIN) || |
| 3433 | (version > SRV_STATE_FILE_VERSION_MAX)) |
Dragan Dosen | cf4fb03 | 2015-11-04 23:03:26 +0100 | [diff] [blame] | 3434 | goto fileclose; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3435 | |
| 3436 | while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) { |
| 3437 | int bk_f_forced_id = 0; |
| 3438 | int check_id = 0; |
| 3439 | int check_name = 0; |
| 3440 | |
| 3441 | mybuflen = strlen(mybuf); |
| 3442 | cur = mybuf; |
| 3443 | end = cur + mybuflen; |
| 3444 | |
| 3445 | bk = NULL; |
| 3446 | srv = NULL; |
| 3447 | |
| 3448 | /* we need at least one character */ |
| 3449 | if (mybuflen == 0) |
| 3450 | continue; |
| 3451 | |
| 3452 | /* ignore blank characters at the beginning of the line */ |
| 3453 | while (isspace(*cur)) |
| 3454 | ++cur; |
| 3455 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3456 | /* Ignore empty or commented lines */ |
| 3457 | if (cur == end || *cur == '#') |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3458 | continue; |
| 3459 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3460 | /* truncated lines */ |
| 3461 | if (mybuf[mybuflen - 1] != '\n') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3462 | ha_warning("server-state file '%s': truncated line\n", filepath); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3463 | continue; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | /* Removes trailing '\n' */ |
| 3467 | mybuf[mybuflen - 1] = '\0'; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3468 | |
| 3469 | /* we're now ready to move the line into *srv_params[] */ |
Christopher Faulet | 6656972 | 2021-02-19 16:47:11 +0100 | [diff] [blame] | 3470 | memset(params, 0, SRV_STATE_FILE_MAX_FIELDS * sizeof(*params)); |
| 3471 | memset(srv_params, 0, SRV_STATE_FILE_MAX_FIELDS * sizeof(*srv_params)); |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3472 | |
| 3473 | arg = 0; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3474 | srv_arg = 0; |
| 3475 | while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) { |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3476 | /* Search begining of the current field */ |
| 3477 | while (isspace((unsigned char)*cur)) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3478 | ++cur; |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3479 | if (!*cur) |
| 3480 | goto end_loop; |
| 3481 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3482 | |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3483 | /* |
| 3484 | * v1 |
| 3485 | * srv_addr: params[4] => srv_params[0] |
| 3486 | * srv_op_state: params[5] => srv_params[1] |
| 3487 | * srv_admin_state: params[6] => srv_params[2] |
| 3488 | * srv_uweight: params[7] => srv_params[3] |
| 3489 | * srv_iweight: params[8] => srv_params[4] |
| 3490 | * srv_last_time_change: params[9] => srv_params[5] |
| 3491 | * srv_check_status: params[10] => srv_params[6] |
| 3492 | * srv_check_result: params[11] => srv_params[7] |
| 3493 | * srv_check_health: params[12] => srv_params[8] |
| 3494 | * srv_check_state: params[13] => srv_params[9] |
| 3495 | * srv_agent_state: params[14] => srv_params[10] |
| 3496 | * bk_f_forced_id: params[15] => srv_params[11] |
| 3497 | * srv_f_forced_id: params[16] => srv_params[12] |
| 3498 | * srv_fqdn: params[17] => srv_params[13] |
| 3499 | * srv_port: params[18] => srv_params[14] |
| 3500 | * srvrecord: params[19] => srv_params[15] |
| 3501 | */ |
| 3502 | if (version == 1 && arg >= 4) { |
| 3503 | srv_params[srv_arg] = cur; |
| 3504 | ++srv_arg; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3505 | } |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3506 | params[arg] = cur; |
| 3507 | ++arg; |
| 3508 | |
| 3509 | /* Search end of the current field: first space or \0 */ |
| 3510 | /* Search begining of the current field */ |
| 3511 | while (!isspace((unsigned char)*cur)) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3512 | ++cur; |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3513 | if (!*cur) |
| 3514 | goto end_loop; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3515 | } |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3516 | *cur++ = '\0'; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3517 | } |
| 3518 | |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3519 | end_loop: |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3520 | /* if line is incomplete line, then ignore it. |
| 3521 | * otherwise, update useful flags */ |
| 3522 | switch (version) { |
| 3523 | case 1: |
| 3524 | if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) |
| 3525 | continue; |
| 3526 | bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID); |
| 3527 | check_id = (atoi(params[0]) == curproxy->uuid); |
| 3528 | check_name = (strcmp(curproxy->id, params[1]) == 0); |
| 3529 | break; |
| 3530 | } |
| 3531 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3532 | bk = curproxy; |
| 3533 | |
| 3534 | /* if backend can't be found, let's continue */ |
| 3535 | if (!check_id && !check_name) |
| 3536 | continue; |
| 3537 | else if (!check_id && check_name) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3538 | ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3539 | send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid); |
| 3540 | } |
| 3541 | else if (check_id && !check_name) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3542 | ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3543 | send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id); |
| 3544 | /* if name doesn't match, we still want to update curproxy if the backend id |
| 3545 | * was forced in previous the previous configuration */ |
| 3546 | if (!bk_f_forced_id) |
| 3547 | continue; |
| 3548 | } |
| 3549 | |
Baptiste Assmann | 95c2c01 | 2019-06-11 14:51:49 +0200 | [diff] [blame] | 3550 | /* look for the server by its name: param[3] */ |
| 3551 | srv = server_find_best_match(bk, params[3], 0, NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3552 | |
| 3553 | if (!srv) { |
| 3554 | /* if no server found, then warning and continue with next line */ |
Baptiste Assmann | 95c2c01 | 2019-06-11 14:51:49 +0200 | [diff] [blame] | 3555 | ha_warning("can't find server '%s' in backend '%s'\n", |
| 3556 | params[3], params[1]); |
| 3557 | send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n", |
| 3558 | params[3], params[1]); |
Frédéric Lécaille | 0bedb8a | 2017-06-15 14:09:10 +0200 | [diff] [blame] | 3559 | continue; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | /* now we can proceed with server's state update */ |
| 3563 | srv_update_state(srv, version, srv_params); |
| 3564 | } |
Dragan Dosen | cf4fb03 | 2015-11-04 23:03:26 +0100 | [diff] [blame] | 3565 | fileclose: |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3566 | fclose(f); |
| 3567 | } |
| 3568 | } |
| 3569 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 3570 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3571 | * update a server's current IP address. |
| 3572 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 3573 | * ip is in network format. |
| 3574 | * updater is a string which contains an information about the requester of the update. |
| 3575 | * updater is used if not NULL. |
| 3576 | * |
| 3577 | * A log line and a stderr warning message is generated based on server's backend options. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3578 | * |
| 3579 | * Must be called with the server lock held. |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3580 | */ |
Thierry Fournier | d35b7a6 | 2016-02-24 08:23:22 +0100 | [diff] [blame] | 3581 | int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater) |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3582 | { |
| 3583 | /* generates a log line and a warning on stderr */ |
| 3584 | if (1) { |
| 3585 | /* book enough space for both IPv4 and IPv6 */ |
| 3586 | char oldip[INET6_ADDRSTRLEN]; |
| 3587 | char newip[INET6_ADDRSTRLEN]; |
| 3588 | |
| 3589 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 3590 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 3591 | |
| 3592 | /* copy old IP address in a string */ |
| 3593 | switch (s->addr.ss_family) { |
| 3594 | case AF_INET: |
| 3595 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 3596 | break; |
| 3597 | case AF_INET6: |
| 3598 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 3599 | break; |
| 3600 | }; |
| 3601 | |
| 3602 | /* copy new IP address in a string */ |
| 3603 | switch (ip_sin_family) { |
| 3604 | case AF_INET: |
| 3605 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 3606 | break; |
| 3607 | case AF_INET6: |
| 3608 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 3609 | break; |
| 3610 | }; |
| 3611 | |
| 3612 | /* save log line into a buffer */ |
| 3613 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 3614 | s->proxy->id, s->id, oldip, newip, updater); |
| 3615 | |
| 3616 | /* write the buffer on stderr */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3617 | ha_warning("%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3618 | |
| 3619 | /* send a log */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3620 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | /* save the new IP family */ |
| 3624 | s->addr.ss_family = ip_sin_family; |
| 3625 | /* save the new IP address */ |
| 3626 | switch (ip_sin_family) { |
| 3627 | case AF_INET: |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3628 | memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3629 | break; |
| 3630 | case AF_INET6: |
| 3631 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 3632 | break; |
| 3633 | }; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3634 | srv_set_dyncookie(s); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3635 | |
| 3636 | return 0; |
| 3637 | } |
| 3638 | |
| 3639 | /* |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3640 | * This function update a server's addr and port only for AF_INET and AF_INET6 families. |
| 3641 | * |
| 3642 | * Caller can pass its name through <updater> to get it integrated in the response message |
| 3643 | * returned by the function. |
| 3644 | * |
| 3645 | * The function first does the following, in that order: |
| 3646 | * - validates the new addr and/or port |
| 3647 | * - checks if an update is required (new IP or port is different than current ones) |
| 3648 | * - checks the update is allowed: |
| 3649 | * - don't switch from/to a family other than AF_INET4 and AF_INET6 |
| 3650 | * - allow all changes if no CHECKS are configured |
| 3651 | * - if CHECK is configured: |
| 3652 | * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports |
| 3653 | * - applies required changes to both ADDR and PORT if both 'required' and 'allowed' |
| 3654 | * conditions are met |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3655 | * |
| 3656 | * Must be called with the server lock held. |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3657 | */ |
| 3658 | const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater) |
| 3659 | { |
| 3660 | struct sockaddr_storage sa; |
| 3661 | int ret, port_change_required; |
| 3662 | char current_addr[INET6_ADDRSTRLEN]; |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3663 | uint16_t current_port, new_port; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3664 | struct buffer *msg; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3665 | int changed = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3666 | |
| 3667 | msg = get_trash_chunk(); |
| 3668 | chunk_reset(msg); |
| 3669 | |
| 3670 | if (addr) { |
| 3671 | memset(&sa, 0, sizeof(struct sockaddr_storage)); |
| 3672 | if (str2ip2(addr, &sa, 0) == NULL) { |
| 3673 | chunk_printf(msg, "Invalid addr '%s'", addr); |
| 3674 | goto out; |
| 3675 | } |
| 3676 | |
| 3677 | /* changes are allowed on AF_INET* families only */ |
| 3678 | if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) { |
| 3679 | chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file"); |
| 3680 | goto out; |
| 3681 | } |
| 3682 | |
| 3683 | /* collecting data currently setup */ |
| 3684 | memset(current_addr, '\0', sizeof(current_addr)); |
| 3685 | ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr)); |
| 3686 | /* changes are allowed on AF_INET* families only */ |
| 3687 | if ((ret != AF_INET) && (ret != AF_INET6)) { |
| 3688 | chunk_printf(msg, "Update for the current server address family is only supported through configuration file"); |
| 3689 | goto out; |
| 3690 | } |
| 3691 | |
| 3692 | /* applying ADDR changes if required and allowed |
| 3693 | * ipcmp returns 0 when both ADDR are the same |
| 3694 | */ |
| 3695 | if (ipcmp(&s->addr, &sa) == 0) { |
| 3696 | chunk_appendf(msg, "no need to change the addr"); |
| 3697 | goto port; |
| 3698 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3699 | ipcpy(&sa, &s->addr); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3700 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3701 | |
| 3702 | /* we also need to update check's ADDR only if it uses the server's one */ |
| 3703 | if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3704 | ipcpy(&sa, &s->check.addr); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | /* we also need to update agent ADDR only if it use the server's one */ |
| 3708 | if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3709 | ipcpy(&sa, &s->agent.addr); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | /* update report for caller */ |
| 3713 | chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr); |
| 3714 | } |
| 3715 | |
| 3716 | port: |
| 3717 | if (port) { |
| 3718 | char sign = '\0'; |
| 3719 | char *endptr; |
| 3720 | |
| 3721 | if (addr) |
| 3722 | chunk_appendf(msg, ", "); |
| 3723 | |
| 3724 | /* collecting data currently setup */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3725 | current_port = s->svc_port; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3726 | |
| 3727 | /* check if PORT change is required */ |
| 3728 | port_change_required = 0; |
| 3729 | |
| 3730 | sign = *port; |
Ryabin Sergey | 77ee752 | 2017-01-11 19:39:55 +0400 | [diff] [blame] | 3731 | errno = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3732 | new_port = strtol(port, &endptr, 10); |
| 3733 | if ((errno != 0) || (port == endptr)) { |
| 3734 | chunk_appendf(msg, "problem converting port '%s' to an int", port); |
| 3735 | goto out; |
| 3736 | } |
| 3737 | |
| 3738 | /* check if caller triggers a port mapped or offset */ |
| 3739 | if (sign == '-' || (sign == '+')) { |
| 3740 | /* check if server currently uses port map */ |
| 3741 | if (!(s->flags & SRV_F_MAPPORTS)) { |
| 3742 | /* switch from fixed port to port map mandatorily triggers |
| 3743 | * a port change */ |
| 3744 | port_change_required = 1; |
| 3745 | /* check is configured |
| 3746 | * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port |
| 3747 | * prevent PORT change if check doesn't have it's dedicated port while switching |
| 3748 | * to port mapping */ |
| 3749 | if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) { |
| 3750 | 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."); |
| 3751 | goto out; |
| 3752 | } |
| 3753 | } |
| 3754 | /* we're already using port maps */ |
| 3755 | else { |
| 3756 | port_change_required = current_port != new_port; |
| 3757 | } |
| 3758 | } |
| 3759 | /* fixed port */ |
| 3760 | else { |
| 3761 | port_change_required = current_port != new_port; |
| 3762 | } |
| 3763 | |
| 3764 | /* applying PORT changes if required and update response message */ |
| 3765 | if (port_change_required) { |
| 3766 | /* apply new port */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3767 | s->svc_port = new_port; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3768 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3769 | |
| 3770 | /* prepare message */ |
| 3771 | chunk_appendf(msg, "port changed from '"); |
| 3772 | if (s->flags & SRV_F_MAPPORTS) |
| 3773 | chunk_appendf(msg, "+"); |
| 3774 | chunk_appendf(msg, "%d' to '", current_port); |
| 3775 | |
| 3776 | if (sign == '-') { |
| 3777 | s->flags |= SRV_F_MAPPORTS; |
| 3778 | chunk_appendf(msg, "%c", sign); |
| 3779 | /* just use for result output */ |
| 3780 | new_port = -new_port; |
| 3781 | } |
| 3782 | else if (sign == '+') { |
| 3783 | s->flags |= SRV_F_MAPPORTS; |
| 3784 | chunk_appendf(msg, "%c", sign); |
| 3785 | } |
| 3786 | else { |
| 3787 | s->flags &= ~SRV_F_MAPPORTS; |
| 3788 | } |
| 3789 | |
| 3790 | chunk_appendf(msg, "%d'", new_port); |
| 3791 | |
| 3792 | /* we also need to update health checks port only if it uses server's realport */ |
| 3793 | if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) { |
| 3794 | s->check.port = new_port; |
| 3795 | } |
| 3796 | } |
| 3797 | else { |
| 3798 | chunk_appendf(msg, "no need to change the port"); |
| 3799 | } |
| 3800 | } |
| 3801 | |
| 3802 | out: |
William Dauchy | 00da86b | 2020-06-02 16:03:58 +0200 | [diff] [blame] | 3803 | if (changed) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3804 | srv_set_dyncookie(s); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3805 | if (updater) |
| 3806 | chunk_appendf(msg, " by '%s'", updater); |
| 3807 | chunk_appendf(msg, "\n"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3808 | return msg->area; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3809 | } |
| 3810 | |
Christopher Faulet | deb03c1 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3811 | /* |
| 3812 | * update server status based on result of SRV resolution |
| 3813 | * returns: |
| 3814 | * 0 if server status is updated |
| 3815 | * 1 if server status has not changed |
| 3816 | * |
| 3817 | * Must be called with the server lock held. |
| 3818 | */ |
| 3819 | int srvrq_update_srv_status(struct server *s, int has_no_ip) |
| 3820 | { |
| 3821 | if (!s->srvrq) |
| 3822 | return 1; |
| 3823 | |
| 3824 | /* since this server has an IP, it can go back in production */ |
| 3825 | if (has_no_ip == 0) { |
| 3826 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3827 | return 1; |
| 3828 | } |
| 3829 | |
| 3830 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3831 | return 1; |
| 3832 | |
| 3833 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record"); |
| 3834 | return 0; |
| 3835 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3836 | |
| 3837 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3838 | * update server status based on result of name resolution |
| 3839 | * returns: |
| 3840 | * 0 if server status is updated |
| 3841 | * 1 if server status has not changed |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3842 | * |
| 3843 | * Must be called with the server lock held. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3844 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3845 | int snr_update_srv_status(struct server *s, int has_no_ip) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3846 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3847 | struct dns_resolvers *resolvers = s->resolvers; |
| 3848 | struct dns_resolution *resolution = s->dns_requester->resolution; |
| 3849 | int exp; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3850 | |
Jerome Magnin | 7cf3024 | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3851 | /* If resolution is NULL we're dealing with SRV records Additional records */ |
Christopher Faulet | deb03c1 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3852 | if (resolution == NULL) |
| 3853 | return srvrq_update_srv_status(s, has_no_ip); |
Jerome Magnin | 7cf3024 | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3854 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3855 | switch (resolution->status) { |
| 3856 | case RSLV_STATUS_NONE: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3857 | /* status when HAProxy has just (re)started. |
| 3858 | * Nothing to do, since the task is already automatically started */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3859 | break; |
| 3860 | |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3861 | case RSLV_STATUS_VALID: |
| 3862 | /* |
| 3863 | * resume health checks |
| 3864 | * server will be turned back on if health check is safe |
| 3865 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3866 | if (has_no_ip) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3867 | if (s->next_admin & SRV_ADMF_RMAINT) |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3868 | return 1; |
| 3869 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, |
| 3870 | "No IP for server "); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3871 | return 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3872 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3873 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3874 | if (!(s->next_admin & SRV_ADMF_RMAINT)) |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3875 | return 1; |
| 3876 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3877 | chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", |
| 3878 | s->proxy->id, s->id); |
| 3879 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3880 | ha_warning("%s.\n", trash.area); |
| 3881 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3882 | return 0; |
| 3883 | |
| 3884 | case RSLV_STATUS_NX: |
| 3885 | /* stop server if resolution is NX for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3886 | exp = tick_add(resolution->last_valid, resolvers->hold.nx); |
| 3887 | if (!tick_is_expired(exp, now_ms)) |
| 3888 | break; |
| 3889 | |
| 3890 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3891 | return 1; |
| 3892 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status"); |
| 3893 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3894 | |
| 3895 | case RSLV_STATUS_TIMEOUT: |
| 3896 | /* stop server if resolution is TIMEOUT for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3897 | exp = tick_add(resolution->last_valid, resolvers->hold.timeout); |
| 3898 | if (!tick_is_expired(exp, now_ms)) |
| 3899 | break; |
| 3900 | |
| 3901 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3902 | return 1; |
| 3903 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status"); |
| 3904 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3905 | |
| 3906 | case RSLV_STATUS_REFUSED: |
| 3907 | /* stop server if resolution is REFUSED for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3908 | exp = tick_add(resolution->last_valid, resolvers->hold.refused); |
| 3909 | if (!tick_is_expired(exp, now_ms)) |
| 3910 | break; |
| 3911 | |
| 3912 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3913 | return 1; |
| 3914 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status"); |
| 3915 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3916 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3917 | default: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3918 | /* stop server if resolution failed for a long enough period */ |
| 3919 | exp = tick_add(resolution->last_valid, resolvers->hold.other); |
| 3920 | if (!tick_is_expired(exp, now_ms)) |
| 3921 | break; |
| 3922 | |
| 3923 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3924 | return 1; |
| 3925 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error"); |
| 3926 | return 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | return 1; |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * Server Name Resolution valid response callback |
| 3934 | * It expects: |
| 3935 | * - <nameserver>: the name server which answered the valid response |
| 3936 | * - <response>: buffer containing a valid DNS response |
| 3937 | * - <response_len>: size of <response> |
| 3938 | * It performs the following actions: |
| 3939 | * - ignore response if current ip found and server family not met |
| 3940 | * - update with first new ip found if family is met and current IP is not found |
| 3941 | * returns: |
| 3942 | * 0 on error |
| 3943 | * 1 when no error or safe ignore |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3944 | * |
| 3945 | * Must be called with server lock held |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3946 | */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3947 | int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3948 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3949 | struct server *s = NULL; |
| 3950 | struct dns_resolution *resolution = NULL; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3951 | void *serverip, *firstip; |
| 3952 | short server_sin_family, firstip_sin_family; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3953 | int ret; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3954 | struct buffer *chk = get_trash_chunk(); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3955 | int has_no_ip = 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3956 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3957 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3958 | if (!s) |
| 3959 | return 1; |
| 3960 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3961 | resolution = s->dns_requester->resolution; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3962 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3963 | /* initializing variables */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3964 | firstip = NULL; /* pointer to the first valid response found */ |
| 3965 | /* it will be used as the new IP if a change is required */ |
| 3966 | firstip_sin_family = AF_UNSPEC; |
| 3967 | serverip = NULL; /* current server IP address */ |
| 3968 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3969 | /* initializing server IP pointer */ |
| 3970 | server_sin_family = s->addr.ss_family; |
| 3971 | switch (server_sin_family) { |
| 3972 | case AF_INET: |
| 3973 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 3974 | break; |
| 3975 | |
| 3976 | case AF_INET6: |
| 3977 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 3978 | break; |
| 3979 | |
Willy Tarreau | 3acfcd1 | 2017-01-06 19:18:32 +0100 | [diff] [blame] | 3980 | case AF_UNSPEC: |
| 3981 | break; |
| 3982 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3983 | default: |
| 3984 | goto invalid; |
| 3985 | } |
| 3986 | |
Baptiste Assmann | 729c901 | 2017-05-22 15:13:10 +0200 | [diff] [blame] | 3987 | ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts, |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 3988 | serverip, server_sin_family, &firstip, |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3989 | &firstip_sin_family, s); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3990 | |
| 3991 | switch (ret) { |
| 3992 | case DNS_UPD_NO: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3993 | goto update_status; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3994 | |
| 3995 | case DNS_UPD_SRVIP_NOT_FOUND: |
| 3996 | goto save_ip; |
| 3997 | |
| 3998 | case DNS_UPD_CNAME: |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3999 | goto invalid; |
| 4000 | |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 4001 | case DNS_UPD_NO_IP_FOUND: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4002 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4003 | goto update_status; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 4004 | |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 4005 | case DNS_UPD_NAME_ERROR: |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 4006 | /* update resolution status to OTHER error type */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4007 | resolution->status = RSLV_STATUS_OTHER; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4008 | goto update_status; |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 4009 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4010 | default: |
| 4011 | goto invalid; |
| 4012 | |
| 4013 | } |
| 4014 | |
| 4015 | save_ip: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4016 | if (nameserver) { |
| 4017 | nameserver->counters.update++; |
| 4018 | /* save the first ip we found */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4019 | chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4020 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4021 | else |
| 4022 | chunk_printf(chk, "DNS cache"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4023 | update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4024 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4025 | update_status: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4026 | snr_update_srv_status(s, has_no_ip); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4027 | return 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4028 | |
| 4029 | invalid: |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 4030 | if (nameserver) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4031 | nameserver->counters.invalid++; |
| 4032 | goto update_status; |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 4033 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4034 | snr_update_srv_status(s, has_no_ip); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4035 | return 0; |
| 4036 | } |
| 4037 | |
| 4038 | /* |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4039 | * SRV record error management callback |
| 4040 | * returns: |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4041 | * 0 if we can trash answser items. |
| 4042 | * 1 when safely ignored and we must kept answer items |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4043 | * |
| 4044 | * Grabs the server's lock. |
| 4045 | */ |
| 4046 | int srvrq_resolution_error_cb(struct dns_requester *requester, int error_code) |
| 4047 | { |
| 4048 | struct server *s; |
| 4049 | struct dns_srvrq *srvrq; |
| 4050 | struct dns_resolution *res; |
| 4051 | struct dns_resolvers *resolvers; |
| 4052 | int exp; |
| 4053 | |
| 4054 | /* SRV records */ |
| 4055 | srvrq = objt_dns_srvrq(requester->owner); |
| 4056 | if (!srvrq) |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4057 | return 0; |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4058 | |
| 4059 | resolvers = srvrq->resolvers; |
| 4060 | res = requester->resolution; |
| 4061 | |
| 4062 | switch (res->status) { |
| 4063 | |
| 4064 | case RSLV_STATUS_NX: |
| 4065 | /* stop server if resolution is NX for a long enough period */ |
| 4066 | exp = tick_add(res->last_valid, resolvers->hold.nx); |
| 4067 | if (!tick_is_expired(exp, now_ms)) |
| 4068 | return 1; |
| 4069 | break; |
| 4070 | |
| 4071 | case RSLV_STATUS_TIMEOUT: |
| 4072 | /* stop server if resolution is TIMEOUT for a long enough period */ |
| 4073 | exp = tick_add(res->last_valid, resolvers->hold.timeout); |
| 4074 | if (!tick_is_expired(exp, now_ms)) |
| 4075 | return 1; |
| 4076 | break; |
| 4077 | |
| 4078 | case RSLV_STATUS_REFUSED: |
| 4079 | /* stop server if resolution is REFUSED for a long enough period */ |
| 4080 | exp = tick_add(res->last_valid, resolvers->hold.refused); |
| 4081 | if (!tick_is_expired(exp, now_ms)) |
| 4082 | return 1; |
| 4083 | break; |
| 4084 | |
| 4085 | default: |
| 4086 | /* stop server if resolution failed for a long enough period */ |
| 4087 | exp = tick_add(res->last_valid, resolvers->hold.other); |
| 4088 | if (!tick_is_expired(exp, now_ms)) |
| 4089 | return 1; |
| 4090 | } |
| 4091 | |
| 4092 | /* Remove any associated server */ |
| 4093 | for (s = srvrq->proxy->srv; s != NULL; s = s->next) { |
| 4094 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 4095 | if (s->srvrq == srvrq) { |
Christopher Faulet | 325a436 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 4096 | dns_unlink_resolution(s->dns_requester, 1); |
Christopher Faulet | 64ab028 | 2021-03-11 18:06:23 +0100 | [diff] [blame] | 4097 | srvrq_update_srv_status(s, 1); |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4098 | free(s->hostname); |
| 4099 | free(s->hostname_dn); |
| 4100 | s->hostname = NULL; |
| 4101 | s->hostname_dn = NULL; |
| 4102 | s->hostname_dn_len = 0; |
Christopher Faulet | bbb6378 | 2021-02-23 12:24:09 +0100 | [diff] [blame] | 4103 | memset(&s->addr, 0, sizeof(s->addr)); |
| 4104 | s->svc_port = 0; |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4105 | } |
| 4106 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4107 | } |
| 4108 | |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4109 | return 0; |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4113 | * Server Name Resolution error management callback |
| 4114 | * returns: |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4115 | * 0 if we can trash answser items. |
| 4116 | * 1 when safely ignored and we must kept answer items |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4117 | * |
| 4118 | * Grabs the server's lock. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4119 | */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4120 | int snr_resolution_error_cb(struct dns_requester *requester, int error_code) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4121 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4122 | struct server *s; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4123 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4124 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4125 | if (!s) |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4126 | return 0; |
| 4127 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4128 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4129 | if (!snr_update_srv_status(s, 1)) { |
Christopher Faulet | 9469af0 | 2021-03-10 20:31:40 +0100 | [diff] [blame] | 4130 | memset(&s->addr, 0, sizeof(s->addr)); |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4131 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4132 | return 0; |
| 4133 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4134 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | a289367 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 4135 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4136 | return 1; |
| 4137 | } |
| 4138 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4139 | /* |
| 4140 | * Function to check if <ip> is already affected to a server in the backend |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4141 | * which owns <srv> and is up. |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4142 | * It returns a pointer to the first server found or NULL if <ip> is not yet |
| 4143 | * assigned. |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 4144 | * |
| 4145 | * Must be called with server lock held |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4146 | */ |
| 4147 | struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family) |
| 4148 | { |
| 4149 | struct server *tmpsrv; |
| 4150 | struct proxy *be; |
| 4151 | |
| 4152 | if (!srv) |
| 4153 | return NULL; |
| 4154 | |
| 4155 | be = srv->proxy; |
| 4156 | for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) { |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4157 | /* we found the current server is the same, ignore it */ |
| 4158 | if (srv == tmpsrv) |
| 4159 | continue; |
| 4160 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4161 | /* We want to compare the IP in the record with the IP of the servers in the |
| 4162 | * same backend, only if: |
| 4163 | * * DNS resolution is enabled on the server |
| 4164 | * * the hostname used for the resolution by our server is the same than the |
| 4165 | * one used for the server found in the backend |
| 4166 | * * the server found in the backend is not our current server |
| 4167 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4168 | HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4169 | if ((tmpsrv->hostname_dn == NULL) || |
| 4170 | (srv->hostname_dn_len != tmpsrv->hostname_dn_len) || |
Christopher Faulet | eb22113 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 4171 | (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) || |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4172 | (srv->puid == tmpsrv->puid)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4173 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4174 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4175 | } |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4176 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4177 | /* If the server has been taken down, don't consider it */ |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4178 | if (tmpsrv->next_admin & SRV_ADMF_RMAINT) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4179 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4180 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4181 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4182 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4183 | /* At this point, we have 2 different servers using the same DNS hostname |
| 4184 | * for their respective resolution. |
| 4185 | */ |
| 4186 | if (*ip_family == tmpsrv->addr.ss_family && |
| 4187 | ((tmpsrv->addr.ss_family == AF_INET && |
| 4188 | memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) || |
| 4189 | (tmpsrv->addr.ss_family == AF_INET6 && |
| 4190 | memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4191 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4192 | return tmpsrv; |
| 4193 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4194 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4195 | } |
| 4196 | |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4197 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4198 | return NULL; |
| 4199 | } |
| 4200 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4201 | /* Sets the server's address (srv->addr) from srv->hostname using the libc's |
| 4202 | * resolver. This is suited for initial address configuration. Returns 0 on |
| 4203 | * success otherwise a non-zero error code. In case of error, *err_code, if |
| 4204 | * not NULL, is filled up. |
| 4205 | */ |
| 4206 | int srv_set_addr_via_libc(struct server *srv, int *err_code) |
| 4207 | { |
| 4208 | if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) { |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4209 | if (err_code) |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4210 | *err_code |= ERR_WARN; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4211 | return 1; |
| 4212 | } |
| 4213 | return 0; |
| 4214 | } |
| 4215 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4216 | /* Set the server's FDQN (->hostname) from <hostname>. |
| 4217 | * Returns -1 if failed, 0 if not. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4218 | * |
| 4219 | * Must be called with the server lock held. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4220 | */ |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4221 | int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4222 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4223 | struct dns_resolution *resolution; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4224 | char *hostname_dn; |
| 4225 | int hostname_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4226 | |
Frédéric Lécaille | 5afb3cf | 2018-08-21 15:04:23 +0200 | [diff] [blame] | 4227 | /* Note that the server lock is already held. */ |
| 4228 | if (!srv->resolvers) |
| 4229 | return -1; |
| 4230 | |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4231 | if (!dns_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4232 | HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4233 | /* run time DNS resolution was not active for this server |
| 4234 | * and we can't enable it at run time for now. |
| 4235 | */ |
| 4236 | if (!srv->dns_requester) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4237 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4238 | |
| 4239 | chunk_reset(&trash); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4240 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4241 | hostname_dn = trash.area; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4242 | hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1, |
| 4243 | hostname_dn, trash.size); |
| 4244 | if (hostname_dn_len == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4245 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4246 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4247 | resolution = srv->dns_requester->resolution; |
| 4248 | if (resolution && |
| 4249 | resolution->hostname_dn && |
Christopher Faulet | eb22113 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 4250 | resolution->hostname_dn_len == hostname_dn_len && |
| 4251 | strcasecmp(resolution->hostname_dn, hostname_dn) == 0) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4252 | goto end; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4253 | |
Christopher Faulet | 325a436 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 4254 | dns_unlink_resolution(srv->dns_requester, 0); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4255 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4256 | free(srv->hostname); |
| 4257 | free(srv->hostname_dn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4258 | srv->hostname = strdup(hostname); |
| 4259 | srv->hostname_dn = strdup(hostname_dn); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4260 | srv->hostname_dn_len = hostname_dn_len; |
| 4261 | if (!srv->hostname || !srv->hostname_dn) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4262 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4263 | |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 4264 | if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4265 | goto err; |
| 4266 | |
| 4267 | end: |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4268 | if (!dns_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4269 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4270 | return 0; |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4271 | |
| 4272 | err: |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4273 | if (!dns_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4274 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4275 | return -1; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4276 | } |
| 4277 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4278 | /* Sets the server's address (srv->addr) from srv->lastaddr which was filled |
| 4279 | * from the state file. This is suited for initial address configuration. |
| 4280 | * Returns 0 on success otherwise a non-zero error code. In case of error, |
| 4281 | * *err_code, if not NULL, is filled up. |
| 4282 | */ |
| 4283 | static int srv_apply_lastaddr(struct server *srv, int *err_code) |
| 4284 | { |
| 4285 | if (!str2ip2(srv->lastaddr, &srv->addr, 0)) { |
| 4286 | if (err_code) |
| 4287 | *err_code |= ERR_WARN; |
| 4288 | return 1; |
| 4289 | } |
| 4290 | return 0; |
| 4291 | } |
| 4292 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4293 | /* returns 0 if no error, otherwise a combination of ERR_* flags */ |
| 4294 | static int srv_iterate_initaddr(struct server *srv) |
| 4295 | { |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4296 | char *name = srv->hostname; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4297 | int return_code = 0; |
| 4298 | int err_code; |
| 4299 | unsigned int methods; |
| 4300 | |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4301 | /* If no addr and no hostname set, get the name from the DNS SRV request */ |
| 4302 | if (!name && srv->srvrq) |
| 4303 | name = srv->srvrq->name; |
| 4304 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4305 | methods = srv->init_addr_methods; |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4306 | if (!methods) { |
| 4307 | /* otherwise default to "last,libc" */ |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4308 | srv_append_initaddr(&methods, SRV_IADDR_LAST); |
| 4309 | srv_append_initaddr(&methods, SRV_IADDR_LIBC); |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4310 | if (srv->resolvers_id) { |
| 4311 | /* dns resolution is configured, add "none" to not fail on startup */ |
| 4312 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4313 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4314 | } |
| 4315 | |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 4316 | /* "-dr" : always append "none" so that server addresses resolution |
| 4317 | * failures are silently ignored, this is convenient to validate some |
| 4318 | * configs out of their environment. |
| 4319 | */ |
| 4320 | if (global.tune.options & GTUNE_RESOLVE_DONTFAIL) |
| 4321 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4322 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4323 | while (methods) { |
| 4324 | err_code = 0; |
| 4325 | switch (srv_get_next_initaddr(&methods)) { |
| 4326 | case SRV_IADDR_LAST: |
| 4327 | if (!srv->lastaddr) |
| 4328 | continue; |
| 4329 | if (srv_apply_lastaddr(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4330 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4331 | return_code |= err_code; |
| 4332 | break; |
| 4333 | |
| 4334 | case SRV_IADDR_LIBC: |
| 4335 | if (!srv->hostname) |
| 4336 | continue; |
| 4337 | if (srv_set_addr_via_libc(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4338 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4339 | return_code |= err_code; |
| 4340 | break; |
| 4341 | |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4342 | case SRV_IADDR_NONE: |
| 4343 | srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4344 | if (return_code) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4345 | ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n", |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4346 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4347 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4348 | return return_code; |
| 4349 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4350 | case SRV_IADDR_IP: |
| 4351 | ipcpy(&srv->init_addr, &srv->addr); |
| 4352 | if (return_code) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4353 | ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n", |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4354 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4355 | } |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4356 | goto out; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4357 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4358 | default: /* unhandled method */ |
| 4359 | break; |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | if (!return_code) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4364 | ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n", |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4365 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4366 | } |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4367 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4368 | ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n", |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4369 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4370 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4371 | |
| 4372 | return_code |= ERR_ALERT | ERR_FATAL; |
| 4373 | return return_code; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4374 | out: |
| 4375 | srv_set_dyncookie(srv); |
| 4376 | return return_code; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4379 | /* |
| 4380 | * This function parses all backends and all servers within each backend |
| 4381 | * and performs servers' addr resolution based on information provided by: |
| 4382 | * - configuration file |
| 4383 | * - server-state file (states provided by an 'old' haproxy process) |
| 4384 | * |
| 4385 | * Returns 0 if no error, otherwise, a combination of ERR_ flags. |
| 4386 | */ |
| 4387 | int srv_init_addr(void) |
| 4388 | { |
| 4389 | struct proxy *curproxy; |
| 4390 | int return_code = 0; |
| 4391 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 4392 | curproxy = proxies_list; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4393 | while (curproxy) { |
| 4394 | struct server *srv; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4395 | |
| 4396 | /* servers are in backend only */ |
Amaury Denoyelle | 2f55daf | 2021-01-06 14:28:50 +0100 | [diff] [blame] | 4397 | if (!(curproxy->cap & PR_CAP_BE) || curproxy->state == PR_STSTOPPED) |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4398 | goto srv_init_addr_next; |
| 4399 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4400 | for (srv = curproxy->srv; srv; srv = srv->next) |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4401 | if (srv->hostname || srv->srvrq) |
Willy Tarreau | 3d609a7 | 2017-09-06 14:22:45 +0200 | [diff] [blame] | 4402 | return_code |= srv_iterate_initaddr(srv); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4403 | |
| 4404 | srv_init_addr_next: |
| 4405 | curproxy = curproxy->next; |
| 4406 | } |
| 4407 | |
| 4408 | return return_code; |
| 4409 | } |
| 4410 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4411 | /* |
| 4412 | * Must be called with the server lock held. |
| 4413 | */ |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4414 | const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater, int dns_locked) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4415 | { |
| 4416 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4417 | struct buffer *msg; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4418 | |
| 4419 | msg = get_trash_chunk(); |
| 4420 | chunk_reset(msg); |
| 4421 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 4422 | if (server->hostname && !strcmp(fqdn, server->hostname)) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4423 | chunk_appendf(msg, "no need to change the FDQN"); |
| 4424 | goto out; |
| 4425 | } |
| 4426 | |
| 4427 | if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) { |
| 4428 | chunk_appendf(msg, "invalid fqdn '%s'", fqdn); |
| 4429 | goto out; |
| 4430 | } |
| 4431 | |
| 4432 | chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s", |
| 4433 | server->proxy->id, server->id, server->hostname, fqdn); |
| 4434 | |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4435 | if (srv_set_fqdn(server, fqdn, dns_locked) < 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4436 | chunk_reset(msg); |
| 4437 | chunk_appendf(msg, "could not update %s/%s FQDN", |
| 4438 | server->proxy->id, server->id); |
| 4439 | goto out; |
| 4440 | } |
| 4441 | |
| 4442 | /* Flag as FQDN set from stats socket. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 4443 | server->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4444 | |
| 4445 | out: |
| 4446 | if (updater) |
| 4447 | chunk_appendf(msg, " by '%s'", updater); |
| 4448 | chunk_appendf(msg, "\n"); |
| 4449 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4450 | return msg->area; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4454 | /* Expects to find a backend and a server in <arg> under the form <backend>/<server>, |
| 4455 | * and returns the pointer to the server. Otherwise, display adequate error messages |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4456 | * on the CLI, sets the CLI's state to CLI_ST_PRINT and returns NULL. This is only |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4457 | * used for CLI commands requiring a server name. |
| 4458 | * Important: the <arg> is modified to remove the '/'. |
| 4459 | */ |
| 4460 | struct server *cli_find_server(struct appctx *appctx, char *arg) |
| 4461 | { |
| 4462 | struct proxy *px; |
| 4463 | struct server *sv; |
| 4464 | char *line; |
| 4465 | |
| 4466 | /* split "backend/server" and make <line> point to server */ |
| 4467 | for (line = arg; *line; line++) |
| 4468 | if (*line == '/') { |
| 4469 | *line++ = '\0'; |
| 4470 | break; |
| 4471 | } |
| 4472 | |
| 4473 | if (!*line || !*arg) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4474 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4475 | appctx->ctx.cli.msg = "Require 'backend/server'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4476 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4477 | return NULL; |
| 4478 | } |
| 4479 | |
| 4480 | if (!get_backend_server(arg, line, &px, &sv)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4481 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4482 | appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4483 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4484 | return NULL; |
| 4485 | } |
| 4486 | |
| 4487 | if (px->state == PR_STSTOPPED) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4488 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4489 | appctx->ctx.cli.msg = "Proxy is disabled.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4490 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4491 | return NULL; |
| 4492 | } |
| 4493 | |
| 4494 | return sv; |
| 4495 | } |
| 4496 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4497 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4498 | /* grabs the server lock */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4499 | static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4500 | { |
| 4501 | struct server *sv; |
| 4502 | const char *warning; |
| 4503 | |
| 4504 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4505 | return 1; |
| 4506 | |
| 4507 | sv = cli_find_server(appctx, args[2]); |
| 4508 | if (!sv) |
| 4509 | return 1; |
| 4510 | |
| 4511 | if (strcmp(args[3], "weight") == 0) { |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4512 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4513 | warning = server_parse_weight_change_request(sv, args[4]); |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4514 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4515 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4516 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4517 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4518 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4519 | } |
| 4520 | } |
| 4521 | else if (strcmp(args[3], "state") == 0) { |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4522 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4523 | if (strcmp(args[4], "ready") == 0) |
| 4524 | srv_adm_set_ready(sv); |
| 4525 | else if (strcmp(args[4], "drain") == 0) |
| 4526 | srv_adm_set_drain(sv); |
| 4527 | else if (strcmp(args[4], "maint") == 0) |
| 4528 | srv_adm_set_maint(sv); |
| 4529 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4530 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4531 | appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4532 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4533 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4534 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4535 | } |
| 4536 | else if (strcmp(args[3], "health") == 0) { |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4537 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4538 | if (sv->track) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4539 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4540 | appctx->ctx.cli.msg = "cannot change health on a tracking server.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4541 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4542 | } |
| 4543 | else if (strcmp(args[4], "up") == 0) { |
| 4544 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4545 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4546 | } |
| 4547 | else if (strcmp(args[4], "stopping") == 0) { |
| 4548 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4549 | srv_set_stopping(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4550 | } |
| 4551 | else if (strcmp(args[4], "down") == 0) { |
| 4552 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4553 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4554 | } |
| 4555 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4556 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4557 | appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4558 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4559 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4560 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4561 | } |
| 4562 | else if (strcmp(args[3], "agent") == 0) { |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4563 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4564 | if (!(sv->agent.state & CHK_ST_ENABLED)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4565 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4566 | appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4567 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4568 | } |
| 4569 | else if (strcmp(args[4], "up") == 0) { |
| 4570 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4571 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4572 | } |
| 4573 | else if (strcmp(args[4], "down") == 0) { |
| 4574 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4575 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4576 | } |
| 4577 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4578 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4579 | appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4580 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4581 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4582 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4583 | } |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4584 | else if (strcmp(args[3], "agent-addr") == 0) { |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4585 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4586 | if (!(sv->agent.state & CHK_ST_ENABLED)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4587 | appctx->ctx.cli.severity = LOG_ERR; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4588 | appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; |
| 4589 | appctx->st0 = CLI_ST_PRINT; |
| 4590 | } else { |
| 4591 | if (str2ip(args[4], &sv->agent.addr) == NULL) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4592 | appctx->ctx.cli.severity = LOG_ERR; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4593 | appctx->ctx.cli.msg = "incorrect addr address given for agent.\n"; |
| 4594 | appctx->st0 = CLI_ST_PRINT; |
| 4595 | } |
| 4596 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4597 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4598 | } |
| 4599 | else if (strcmp(args[3], "agent-send") == 0) { |
Christopher Faulet | aec948b | 2021-06-18 08:47:14 +0200 | [diff] [blame^] | 4600 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4601 | if (!(sv->agent.state & CHK_ST_ENABLED)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4602 | appctx->ctx.cli.severity = LOG_ERR; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4603 | appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; |
| 4604 | appctx->st0 = CLI_ST_PRINT; |
| 4605 | } else { |
| 4606 | char *nss = strdup(args[4]); |
| 4607 | if (!nss) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4608 | appctx->ctx.cli.severity = LOG_ERR; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4609 | appctx->ctx.cli.msg = "cannot allocate memory for new string.\n"; |
| 4610 | appctx->st0 = CLI_ST_PRINT; |
| 4611 | } else { |
| 4612 | free(sv->agent.send_string); |
| 4613 | sv->agent.send_string = nss; |
| 4614 | sv->agent.send_string_len = strlen(args[4]); |
| 4615 | } |
| 4616 | } |
Christopher Faulet | aec948b | 2021-06-18 08:47:14 +0200 | [diff] [blame^] | 4617 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4618 | } |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4619 | else if (strcmp(args[3], "check-port") == 0) { |
| 4620 | int i = 0; |
| 4621 | if (strl2irc(args[4], strlen(args[4]), &i) != 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4622 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4623 | appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4624 | appctx->st0 = CLI_ST_PRINT; |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4625 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4626 | } |
| 4627 | if ((i < 0) || (i > 65535)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4628 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4629 | appctx->ctx.cli.msg = "provided port is not valid.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4630 | appctx->st0 = CLI_ST_PRINT; |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4631 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4632 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4633 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4634 | /* prevent the update of port to 0 if MAPPORTS are in use */ |
| 4635 | if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4636 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4637 | appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4638 | appctx->st0 = CLI_ST_PRINT; |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4639 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4640 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4641 | } |
| 4642 | sv->check.port = i; |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4643 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4644 | appctx->ctx.cli.severity = LOG_NOTICE; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4645 | appctx->ctx.cli.msg = "health check port updated.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4646 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4647 | } |
| 4648 | else if (strcmp(args[3], "addr") == 0) { |
| 4649 | char *addr = NULL; |
| 4650 | char *port = NULL; |
| 4651 | if (strlen(args[4]) == 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4652 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4653 | appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4654 | appctx->st0 = CLI_ST_PRINT; |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4655 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4656 | } |
| 4657 | else { |
| 4658 | addr = args[4]; |
| 4659 | } |
| 4660 | if (strcmp(args[5], "port") == 0) { |
| 4661 | port = args[6]; |
| 4662 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4663 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4664 | warning = update_server_addr_port(sv, addr, port, "stats socket command"); |
| 4665 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4666 | appctx->ctx.cli.severity = LOG_WARNING; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4667 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4668 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4669 | } |
| 4670 | srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4671 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4672 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4673 | else if (strcmp(args[3], "fqdn") == 0) { |
| 4674 | if (!*args[4]) { |
Willy Tarreau | a075258 | 2017-11-05 10:17:49 +0100 | [diff] [blame] | 4675 | appctx->ctx.cli.severity = LOG_ERR; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4676 | appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n"; |
| 4677 | appctx->st0 = CLI_ST_PRINT; |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4678 | goto out; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4679 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4680 | if (!sv->resolvers) { |
| 4681 | appctx->ctx.cli.severity = LOG_ERR; |
| 4682 | appctx->ctx.cli.msg = "set server <b>/<s> fqdn failed because no resolution is configured.\n"; |
| 4683 | appctx->st0 = CLI_ST_PRINT; |
| 4684 | goto out; |
| 4685 | } |
| 4686 | HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock); |
| 4687 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4688 | warning = update_server_fqdn(sv, args[4], "stats socket command", 1); |
Christopher Faulet | aec948b | 2021-06-18 08:47:14 +0200 | [diff] [blame^] | 4689 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4690 | HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4691 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4692 | appctx->ctx.cli.severity = LOG_WARNING; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4693 | appctx->ctx.cli.msg = warning; |
| 4694 | appctx->st0 = CLI_ST_PRINT; |
| 4695 | } |
| 4696 | } |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4697 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4698 | appctx->ctx.cli.severity = LOG_ERR; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4699 | appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr', 'fqdn' and 'check-port'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4700 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4701 | } |
Christopher Faulet | 8ae8d34 | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4702 | out: |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4703 | return 1; |
| 4704 | } |
| 4705 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4706 | static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4707 | { |
| 4708 | struct stream_interface *si = appctx->owner; |
| 4709 | struct proxy *px; |
| 4710 | struct server *sv; |
| 4711 | char *line; |
| 4712 | |
| 4713 | |
| 4714 | /* split "backend/server" and make <line> point to server */ |
| 4715 | for (line = args[2]; *line; line++) |
| 4716 | if (*line == '/') { |
| 4717 | *line++ = '\0'; |
| 4718 | break; |
| 4719 | } |
| 4720 | |
| 4721 | if (!*line) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4722 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4723 | appctx->ctx.cli.msg = "Require 'backend/server'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4724 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4725 | return 1; |
| 4726 | } |
| 4727 | |
| 4728 | if (!get_backend_server(args[2], line, &px, &sv)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4729 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4730 | appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4731 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4732 | return 1; |
| 4733 | } |
| 4734 | |
| 4735 | /* return server's effective weight at the moment */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4736 | snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, |
| 4737 | sv->iweight); |
| 4738 | if (ci_putstr(si_ic(si), trash.area) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4739 | si_rx_room_blk(si); |
Christopher Faulet | 90b5abe | 2016-12-05 14:25:08 +0100 | [diff] [blame] | 4740 | return 0; |
| 4741 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4742 | return 1; |
| 4743 | } |
| 4744 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4745 | /* Parse a "set weight" command. |
| 4746 | * |
| 4747 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4748 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4749 | static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4750 | { |
| 4751 | struct server *sv; |
| 4752 | const char *warning; |
| 4753 | |
| 4754 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4755 | return 1; |
| 4756 | |
| 4757 | sv = cli_find_server(appctx, args[2]); |
| 4758 | if (!sv) |
| 4759 | return 1; |
| 4760 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4761 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4762 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4763 | warning = server_parse_weight_change_request(sv, args[3]); |
| 4764 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4765 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4766 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4767 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4768 | } |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4769 | |
| 4770 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4771 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4772 | return 1; |
| 4773 | } |
| 4774 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4775 | /* parse a "set maxconn server" command. It always returns 1. |
| 4776 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4777 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4778 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4779 | static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4780 | { |
| 4781 | struct server *sv; |
| 4782 | const char *warning; |
| 4783 | |
| 4784 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4785 | return 1; |
| 4786 | |
| 4787 | sv = cli_find_server(appctx, args[3]); |
| 4788 | if (!sv) |
| 4789 | return 1; |
| 4790 | |
Amaury Denoyelle | b0fbda5 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4791 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4792 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4793 | warning = server_parse_maxconn_change_request(sv, args[4]); |
| 4794 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4795 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4796 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4797 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4798 | } |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4799 | |
Amaury Denoyelle | b0fbda5 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4800 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4801 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4802 | return 1; |
| 4803 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4804 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4805 | /* parse a "disable agent" command. It always returns 1. |
| 4806 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4807 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4808 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4809 | static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4810 | { |
| 4811 | struct server *sv; |
| 4812 | |
| 4813 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4814 | return 1; |
| 4815 | |
| 4816 | sv = cli_find_server(appctx, args[2]); |
| 4817 | if (!sv) |
| 4818 | return 1; |
| 4819 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4820 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4821 | sv->agent.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4822 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4823 | return 1; |
| 4824 | } |
| 4825 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4826 | /* parse a "disable health" command. It always returns 1. |
| 4827 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4828 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4829 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4830 | static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4831 | { |
| 4832 | struct server *sv; |
| 4833 | |
| 4834 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4835 | return 1; |
| 4836 | |
| 4837 | sv = cli_find_server(appctx, args[2]); |
| 4838 | if (!sv) |
| 4839 | return 1; |
| 4840 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4841 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4842 | sv->check.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4843 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4844 | return 1; |
| 4845 | } |
| 4846 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4847 | /* parse a "disable server" command. It always returns 1. |
| 4848 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4849 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4850 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4851 | static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4852 | { |
| 4853 | struct server *sv; |
| 4854 | |
| 4855 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4856 | return 1; |
| 4857 | |
| 4858 | sv = cli_find_server(appctx, args[2]); |
| 4859 | if (!sv) |
| 4860 | return 1; |
| 4861 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4862 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4863 | srv_adm_set_maint(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4864 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4865 | return 1; |
| 4866 | } |
| 4867 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4868 | /* parse a "enable agent" command. It always returns 1. |
| 4869 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4870 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4871 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4872 | static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4873 | { |
| 4874 | struct server *sv; |
| 4875 | |
| 4876 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4877 | return 1; |
| 4878 | |
| 4879 | sv = cli_find_server(appctx, args[2]); |
| 4880 | if (!sv) |
| 4881 | return 1; |
| 4882 | |
| 4883 | if (!(sv->agent.state & CHK_ST_CONFIGURED)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4884 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4885 | appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4886 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4887 | return 1; |
| 4888 | } |
| 4889 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4890 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4891 | sv->agent.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4892 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4893 | return 1; |
| 4894 | } |
| 4895 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4896 | /* parse a "enable health" command. It always returns 1. |
| 4897 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4898 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4899 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4900 | static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4901 | { |
| 4902 | struct server *sv; |
| 4903 | |
| 4904 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4905 | return 1; |
| 4906 | |
| 4907 | sv = cli_find_server(appctx, args[2]); |
| 4908 | if (!sv) |
| 4909 | return 1; |
| 4910 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4911 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4912 | sv->check.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4913 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4914 | return 1; |
| 4915 | } |
| 4916 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4917 | /* parse a "enable server" command. It always returns 1. |
| 4918 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4919 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4920 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4921 | static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4922 | { |
| 4923 | struct server *sv; |
| 4924 | |
| 4925 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4926 | return 1; |
| 4927 | |
| 4928 | sv = cli_find_server(appctx, args[2]); |
| 4929 | if (!sv) |
| 4930 | return 1; |
| 4931 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4932 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4933 | srv_adm_set_ready(sv); |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 4934 | if (!(sv->flags & SRV_F_COOKIESET) |
| 4935 | && (sv->proxy->ck_opts & PR_CK_DYNAMIC) && |
| 4936 | sv->cookie) |
| 4937 | srv_check_for_dup_dyncookie(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4938 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4939 | return 1; |
| 4940 | } |
| 4941 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4942 | /* register cli keywords */ |
| 4943 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4944 | { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL }, |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4945 | { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL }, |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4946 | { { "disable", "server", NULL }, "disable server : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL }, |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4947 | { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL }, |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4948 | { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL }, |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4949 | { { "enable", "server", NULL }, "enable server : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL }, |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4950 | { { "set", "maxconn", "server", NULL }, "set maxconn server : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL }, |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4951 | { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server }, |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4952 | { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight }, |
| 4953 | { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight }, |
| 4954 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4955 | {{},} |
| 4956 | }}; |
| 4957 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4958 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4959 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4960 | /* |
| 4961 | * This function applies server's status changes, it is |
| 4962 | * is designed to be called asynchronously. |
| 4963 | * |
Willy Tarreau | 228d60e | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 4964 | * Must be called with the server lock held. This may also be called at init |
| 4965 | * time as the result of parsing the state file, in which case no lock will be |
| 4966 | * held, and the server's warmup task can be null. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4967 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 4968 | static void srv_update_status(struct server *s) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4969 | { |
| 4970 | struct check *check = &s->check; |
| 4971 | int xferred; |
| 4972 | struct proxy *px = s->proxy; |
| 4973 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 4974 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 4975 | int log_level; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4976 | struct buffer *tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4977 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4978 | /* If currently main is not set we try to apply pending state changes */ |
| 4979 | if (!(s->cur_admin & SRV_ADMF_MAINT)) { |
| 4980 | int next_admin; |
| 4981 | |
| 4982 | /* Backup next admin */ |
| 4983 | next_admin = s->next_admin; |
| 4984 | |
| 4985 | /* restore current admin state */ |
| 4986 | s->next_admin = s->cur_admin; |
| 4987 | |
| 4988 | if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) { |
| 4989 | s->last_change = now.tv_sec; |
| 4990 | if (s->proxy->lbprm.set_server_status_down) |
| 4991 | s->proxy->lbprm.set_server_status_down(s); |
| 4992 | |
| 4993 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 4994 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 4995 | |
| 4996 | /* we might have streams queued on this server and waiting for |
| 4997 | * a connection. Those which are redispatchable will be queued |
| 4998 | * to another server or to the proxy itself. |
| 4999 | */ |
| 5000 | xferred = pendconn_redistribute(s); |
| 5001 | |
| 5002 | tmptrash = alloc_trash_chunk(); |
| 5003 | if (tmptrash) { |
| 5004 | chunk_printf(tmptrash, |
| 5005 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5006 | s->proxy->id, s->id); |
| 5007 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5008 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5009 | ha_warning("%s.\n", tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5010 | |
| 5011 | /* we don't send an alert if the server was previously paused */ |
| 5012 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5013 | send_log(s->proxy, log_level, "%s.\n", |
| 5014 | tmptrash->area); |
| 5015 | send_email_alert(s, log_level, "%s", |
| 5016 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5017 | free_trash_chunk(tmptrash); |
| 5018 | tmptrash = NULL; |
| 5019 | } |
| 5020 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5021 | set_backend_down(s->proxy); |
| 5022 | |
| 5023 | s->counters.down_trans++; |
| 5024 | } |
| 5025 | else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) { |
| 5026 | s->last_change = now.tv_sec; |
| 5027 | if (s->proxy->lbprm.set_server_status_down) |
| 5028 | s->proxy->lbprm.set_server_status_down(s); |
| 5029 | |
| 5030 | /* we might have streams queued on this server and waiting for |
| 5031 | * a connection. Those which are redispatchable will be queued |
| 5032 | * to another server or to the proxy itself. |
| 5033 | */ |
| 5034 | xferred = pendconn_redistribute(s); |
| 5035 | |
| 5036 | tmptrash = alloc_trash_chunk(); |
| 5037 | if (tmptrash) { |
| 5038 | chunk_printf(tmptrash, |
| 5039 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5040 | s->proxy->id, s->id); |
| 5041 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5042 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5043 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5044 | ha_warning("%s.\n", tmptrash->area); |
| 5045 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5046 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5047 | free_trash_chunk(tmptrash); |
| 5048 | tmptrash = NULL; |
| 5049 | } |
| 5050 | |
| 5051 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5052 | set_backend_down(s->proxy); |
| 5053 | } |
| 5054 | else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING)) |
| 5055 | || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) { |
| 5056 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5057 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5058 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5059 | s->proxy->last_change = now.tv_sec; |
| 5060 | } |
| 5061 | |
Amaury Denoyelle | c1bd4bd | 2020-10-29 15:59:04 +0100 | [diff] [blame] | 5062 | if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5063 | s->down_time += now.tv_sec - s->last_change; |
| 5064 | |
| 5065 | s->last_change = now.tv_sec; |
Willy Tarreau | 228d60e | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5066 | if (s->next_state == SRV_ST_STARTING && s->warmup) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5067 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5068 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5069 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5070 | /* now propagate the status change to any LB algorithms */ |
| 5071 | if (px->lbprm.update_server_eweight) |
| 5072 | px->lbprm.update_server_eweight(s); |
| 5073 | else if (srv_willbe_usable(s)) { |
| 5074 | if (px->lbprm.set_server_status_up) |
| 5075 | px->lbprm.set_server_status_up(s); |
| 5076 | } |
| 5077 | else { |
| 5078 | if (px->lbprm.set_server_status_down) |
| 5079 | px->lbprm.set_server_status_down(s); |
| 5080 | } |
| 5081 | |
| 5082 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5083 | * and it's not a backup server and its effective weight is > 0, |
| 5084 | * then it can accept new connections, so we shut down all streams |
| 5085 | * on all backup servers. |
| 5086 | */ |
| 5087 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5088 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5089 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5090 | |
| 5091 | /* check if we can handle some connections queued at the proxy. We |
| 5092 | * will take as many as we can handle. |
| 5093 | */ |
| 5094 | xferred = pendconn_grab_from_px(s); |
| 5095 | |
| 5096 | tmptrash = alloc_trash_chunk(); |
| 5097 | if (tmptrash) { |
| 5098 | chunk_printf(tmptrash, |
| 5099 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5100 | s->proxy->id, s->id); |
| 5101 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5102 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5103 | ha_warning("%s.\n", tmptrash->area); |
| 5104 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5105 | tmptrash->area); |
| 5106 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5107 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5108 | free_trash_chunk(tmptrash); |
| 5109 | tmptrash = NULL; |
| 5110 | } |
| 5111 | |
| 5112 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5113 | set_backend_down(s->proxy); |
| 5114 | } |
| 5115 | else if (s->cur_eweight != s->next_eweight) { |
| 5116 | /* now propagate the status change to any LB algorithms */ |
| 5117 | if (px->lbprm.update_server_eweight) |
| 5118 | px->lbprm.update_server_eweight(s); |
| 5119 | else if (srv_willbe_usable(s)) { |
| 5120 | if (px->lbprm.set_server_status_up) |
| 5121 | px->lbprm.set_server_status_up(s); |
| 5122 | } |
| 5123 | else { |
| 5124 | if (px->lbprm.set_server_status_down) |
| 5125 | px->lbprm.set_server_status_down(s); |
| 5126 | } |
| 5127 | |
| 5128 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5129 | set_backend_down(s->proxy); |
| 5130 | } |
| 5131 | |
| 5132 | s->next_admin = next_admin; |
| 5133 | } |
| 5134 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5135 | /* reset operational state change */ |
| 5136 | *s->op_st_chg.reason = 0; |
| 5137 | s->op_st_chg.status = s->op_st_chg.code = -1; |
| 5138 | s->op_st_chg.duration = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5139 | |
| 5140 | /* Now we try to apply pending admin changes */ |
| 5141 | |
| 5142 | /* Maintenance must also disable health checks */ |
| 5143 | if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) { |
| 5144 | if (s->check.state & CHK_ST_ENABLED) { |
| 5145 | s->check.state |= CHK_ST_PAUSED; |
| 5146 | check->health = 0; |
| 5147 | } |
| 5148 | |
| 5149 | if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */ |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5150 | tmptrash = alloc_trash_chunk(); |
| 5151 | if (tmptrash) { |
| 5152 | chunk_printf(tmptrash, |
| 5153 | "%sServer %s/%s was DOWN and now enters maintenance%s%s%s", |
| 5154 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5155 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5156 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5157 | srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5158 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5159 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5160 | ha_warning("%s.\n", tmptrash->area); |
| 5161 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5162 | tmptrash->area); |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5163 | } |
| 5164 | free_trash_chunk(tmptrash); |
| 5165 | tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5166 | } |
Emeric Brun | 8f29829 | 2017-12-06 16:47:17 +0100 | [diff] [blame] | 5167 | /* commit new admin status */ |
| 5168 | |
| 5169 | s->cur_admin = s->next_admin; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5170 | } |
| 5171 | else { /* server was still running */ |
| 5172 | check->health = 0; /* failure */ |
| 5173 | s->last_change = now.tv_sec; |
Emeric Brun | e311480 | 2017-12-21 14:42:26 +0100 | [diff] [blame] | 5174 | |
| 5175 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5176 | if (s->proxy->lbprm.set_server_status_down) |
| 5177 | s->proxy->lbprm.set_server_status_down(s); |
| 5178 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5179 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5180 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5181 | |
| 5182 | /* we might have streams queued on this server and waiting for |
| 5183 | * a connection. Those which are redispatchable will be queued |
| 5184 | * to another server or to the proxy itself. |
| 5185 | */ |
| 5186 | xferred = pendconn_redistribute(s); |
| 5187 | |
| 5188 | tmptrash = alloc_trash_chunk(); |
| 5189 | if (tmptrash) { |
| 5190 | chunk_printf(tmptrash, |
| 5191 | "%sServer %s/%s is going DOWN for maintenance%s%s%s", |
| 5192 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5193 | s->proxy->id, s->id, |
| 5194 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5195 | |
| 5196 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT)); |
| 5197 | |
| 5198 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5199 | ha_warning("%s.\n", tmptrash->area); |
| 5200 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", |
| 5201 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5202 | } |
| 5203 | free_trash_chunk(tmptrash); |
| 5204 | tmptrash = NULL; |
| 5205 | } |
| 5206 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5207 | set_backend_down(s->proxy); |
| 5208 | |
| 5209 | s->counters.down_trans++; |
| 5210 | } |
| 5211 | } |
| 5212 | else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) { |
| 5213 | /* OK here we're leaving maintenance, we have many things to check, |
| 5214 | * because the server might possibly be coming back up depending on |
| 5215 | * its state. In practice, leaving maintenance means that we should |
| 5216 | * immediately turn to UP (more or less the slowstart) under the |
| 5217 | * following conditions : |
| 5218 | * - server is neither checked nor tracked |
| 5219 | * - server tracks another server which is not checked |
| 5220 | * - server tracks another server which is already up |
| 5221 | * Which sums up as something simpler : |
| 5222 | * "either the tracking server is up or the server's checks are disabled |
| 5223 | * or up". Otherwise we only re-enable health checks. There's a special |
| 5224 | * case associated to the stopping state which can be inherited. Note |
| 5225 | * that the server might still be in drain mode, which is naturally dealt |
| 5226 | * with by the lower level functions. |
| 5227 | */ |
| 5228 | |
| 5229 | if (s->check.state & CHK_ST_ENABLED) { |
| 5230 | s->check.state &= ~CHK_ST_PAUSED; |
| 5231 | check->health = check->rise; /* start OK but check immediately */ |
| 5232 | } |
| 5233 | |
| 5234 | if ((!s->track || s->track->next_state != SRV_ST_STOPPED) && |
| 5235 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 5236 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 5237 | if (s->track && s->track->next_state == SRV_ST_STOPPING) { |
| 5238 | s->next_state = SRV_ST_STOPPING; |
| 5239 | } |
| 5240 | else { |
| 5241 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | 228d60e | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5242 | if (s->slowstart > 0) { |
| 5243 | if (s->warmup) |
| 5244 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5245 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5246 | else |
| 5247 | s->next_state = SRV_ST_RUNNING; |
| 5248 | } |
| 5249 | |
| 5250 | } |
| 5251 | |
| 5252 | tmptrash = alloc_trash_chunk(); |
| 5253 | if (tmptrash) { |
| 5254 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5255 | chunk_printf(tmptrash, |
| 5256 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 5257 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5258 | s->proxy->id, s->id, |
| 5259 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5260 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5261 | } |
| 5262 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5263 | chunk_printf(tmptrash, |
| 5264 | "%sServer %s/%s ('%s') is %s/%s (resolves again)", |
| 5265 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5266 | s->proxy->id, s->id, s->hostname, |
| 5267 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5268 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5269 | } |
| 5270 | if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5271 | chunk_printf(tmptrash, |
| 5272 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 5273 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5274 | s->proxy->id, s->id, |
| 5275 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5276 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5277 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5278 | ha_warning("%s.\n", tmptrash->area); |
| 5279 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5280 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5281 | free_trash_chunk(tmptrash); |
| 5282 | tmptrash = NULL; |
| 5283 | } |
| 5284 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5285 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5286 | /* now propagate the status change to any LB algorithms */ |
| 5287 | if (px->lbprm.update_server_eweight) |
| 5288 | px->lbprm.update_server_eweight(s); |
| 5289 | else if (srv_willbe_usable(s)) { |
| 5290 | if (px->lbprm.set_server_status_up) |
| 5291 | px->lbprm.set_server_status_up(s); |
| 5292 | } |
| 5293 | else { |
| 5294 | if (px->lbprm.set_server_status_down) |
| 5295 | px->lbprm.set_server_status_down(s); |
| 5296 | } |
| 5297 | |
| 5298 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5299 | set_backend_down(s->proxy); |
| 5300 | |
Willy Tarreau | 6a78e61 | 2018-08-07 10:14:53 +0200 | [diff] [blame] | 5301 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5302 | * and it's not a backup server and its effective weight is > 0, |
| 5303 | * then it can accept new connections, so we shut down all streams |
| 5304 | * on all backup servers. |
| 5305 | */ |
| 5306 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5307 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5308 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5309 | |
| 5310 | /* check if we can handle some connections queued at the proxy. We |
| 5311 | * will take as many as we can handle. |
| 5312 | */ |
| 5313 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5314 | } |
| 5315 | else if (s->next_admin & SRV_ADMF_MAINT) { |
| 5316 | /* remaining in maintenance mode, let's inform precisely about the |
| 5317 | * situation. |
| 5318 | */ |
| 5319 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5320 | tmptrash = alloc_trash_chunk(); |
| 5321 | if (tmptrash) { |
| 5322 | chunk_printf(tmptrash, |
| 5323 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 5324 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5325 | s->proxy->id, s->id); |
| 5326 | |
| 5327 | if (s->track) /* normally it's mandatory here */ |
| 5328 | chunk_appendf(tmptrash, " via %s/%s", |
| 5329 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5330 | ha_warning("%s.\n", tmptrash->area); |
| 5331 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5332 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5333 | free_trash_chunk(tmptrash); |
| 5334 | tmptrash = NULL; |
| 5335 | } |
| 5336 | } |
| 5337 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5338 | tmptrash = alloc_trash_chunk(); |
| 5339 | if (tmptrash) { |
| 5340 | chunk_printf(tmptrash, |
| 5341 | "%sServer %s/%s ('%s') resolves again but remains in maintenance", |
| 5342 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5343 | s->proxy->id, s->id, s->hostname); |
| 5344 | |
| 5345 | if (s->track) /* normally it's mandatory here */ |
| 5346 | chunk_appendf(tmptrash, " via %s/%s", |
| 5347 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5348 | ha_warning("%s.\n", tmptrash->area); |
| 5349 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5350 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5351 | free_trash_chunk(tmptrash); |
| 5352 | tmptrash = NULL; |
| 5353 | } |
| 5354 | } |
| 5355 | else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5356 | tmptrash = alloc_trash_chunk(); |
| 5357 | if (tmptrash) { |
| 5358 | chunk_printf(tmptrash, |
| 5359 | "%sServer %s/%s remains in forced maintenance", |
| 5360 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5361 | s->proxy->id, s->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5362 | ha_warning("%s.\n", tmptrash->area); |
| 5363 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5364 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5365 | free_trash_chunk(tmptrash); |
| 5366 | tmptrash = NULL; |
| 5367 | } |
| 5368 | } |
| 5369 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
| 5370 | |
| 5371 | s->cur_admin = s->next_admin; |
| 5372 | } |
| 5373 | |
| 5374 | if (!(s->next_admin & SRV_ADMF_MAINT)) { |
| 5375 | if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) { |
| 5376 | /* drain state is applied only if not yet in maint */ |
| 5377 | |
| 5378 | s->last_change = now.tv_sec; |
| 5379 | if (px->lbprm.set_server_status_down) |
| 5380 | px->lbprm.set_server_status_down(s); |
| 5381 | |
| 5382 | /* we might have streams queued on this server and waiting for |
| 5383 | * a connection. Those which are redispatchable will be queued |
| 5384 | * to another server or to the proxy itself. |
| 5385 | */ |
| 5386 | xferred = pendconn_redistribute(s); |
| 5387 | |
| 5388 | tmptrash = alloc_trash_chunk(); |
| 5389 | if (tmptrash) { |
| 5390 | chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s", |
| 5391 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5392 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5393 | |
| 5394 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN)); |
| 5395 | |
| 5396 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5397 | ha_warning("%s.\n", tmptrash->area); |
| 5398 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5399 | tmptrash->area); |
| 5400 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5401 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5402 | } |
| 5403 | free_trash_chunk(tmptrash); |
| 5404 | tmptrash = NULL; |
| 5405 | } |
| 5406 | |
| 5407 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5408 | set_backend_down(s->proxy); |
| 5409 | } |
| 5410 | else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) { |
| 5411 | /* OK completely leaving drain mode */ |
| 5412 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5413 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5414 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5415 | s->proxy->last_change = now.tv_sec; |
| 5416 | } |
| 5417 | |
| 5418 | if (s->last_change < now.tv_sec) // ignore negative times |
| 5419 | s->down_time += now.tv_sec - s->last_change; |
| 5420 | s->last_change = now.tv_sec; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5421 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5422 | |
| 5423 | tmptrash = alloc_trash_chunk(); |
| 5424 | if (tmptrash) { |
| 5425 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5426 | chunk_printf(tmptrash, |
| 5427 | "%sServer %s/%s is %s (leaving forced drain)", |
| 5428 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5429 | s->proxy->id, s->id, |
| 5430 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5431 | } |
| 5432 | else { |
| 5433 | chunk_printf(tmptrash, |
| 5434 | "%sServer %s/%s is %s (leaving drain)", |
| 5435 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5436 | s->proxy->id, s->id, |
| 5437 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5438 | if (s->track) /* normally it's mandatory here */ |
| 5439 | chunk_appendf(tmptrash, " via %s/%s", |
| 5440 | s->track->proxy->id, s->track->id); |
| 5441 | } |
| 5442 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5443 | ha_warning("%s.\n", tmptrash->area); |
| 5444 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5445 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5446 | free_trash_chunk(tmptrash); |
| 5447 | tmptrash = NULL; |
| 5448 | } |
| 5449 | |
| 5450 | /* now propagate the status change to any LB algorithms */ |
| 5451 | if (px->lbprm.update_server_eweight) |
| 5452 | px->lbprm.update_server_eweight(s); |
| 5453 | else if (srv_willbe_usable(s)) { |
| 5454 | if (px->lbprm.set_server_status_up) |
| 5455 | px->lbprm.set_server_status_up(s); |
| 5456 | } |
| 5457 | else { |
| 5458 | if (px->lbprm.set_server_status_down) |
| 5459 | px->lbprm.set_server_status_down(s); |
| 5460 | } |
| 5461 | } |
| 5462 | else if ((s->next_admin & SRV_ADMF_DRAIN)) { |
| 5463 | /* remaining in drain mode after removing one of its flags */ |
| 5464 | |
| 5465 | tmptrash = alloc_trash_chunk(); |
| 5466 | if (tmptrash) { |
| 5467 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5468 | chunk_printf(tmptrash, |
| 5469 | "%sServer %s/%s is leaving forced drain but remains in drain mode", |
| 5470 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5471 | s->proxy->id, s->id); |
| 5472 | |
| 5473 | if (s->track) /* normally it's mandatory here */ |
| 5474 | chunk_appendf(tmptrash, " via %s/%s", |
| 5475 | s->track->proxy->id, s->track->id); |
| 5476 | } |
| 5477 | else { |
| 5478 | chunk_printf(tmptrash, |
| 5479 | "%sServer %s/%s remains in forced drain mode", |
| 5480 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5481 | s->proxy->id, s->id); |
| 5482 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5483 | ha_warning("%s.\n", tmptrash->area); |
| 5484 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5485 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5486 | free_trash_chunk(tmptrash); |
| 5487 | tmptrash = NULL; |
| 5488 | } |
| 5489 | |
| 5490 | /* commit new admin status */ |
| 5491 | |
| 5492 | s->cur_admin = s->next_admin; |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | /* Re-set log strings to empty */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5497 | *s->adm_st_chg_cause = 0; |
| 5498 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5499 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5500 | struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state) |
| 5501 | { |
| 5502 | struct connection *conn; |
| 5503 | |
| 5504 | while ((conn = LIST_POP_LOCKED(&toremove_connections[tid], |
| 5505 | struct connection *, list)) != NULL) { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 5506 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | return task; |
| 5510 | } |
| 5511 | |
Willy Tarreau | 980855b | 2019-02-07 14:59:29 +0100 | [diff] [blame] | 5512 | struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state) |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5513 | { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5514 | struct server *srv; |
| 5515 | struct eb32_node *eb; |
| 5516 | int i; |
| 5517 | unsigned int next_wakeup; |
| 5518 | int need_wakeup = 0; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5519 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5520 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5521 | while (1) { |
| 5522 | int srv_is_empty = 1; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5523 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5524 | eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK); |
| 5525 | if (!eb) { |
| 5526 | /* we might have reached the end of the tree, typically because |
| 5527 | * <now_ms> is in the first half and we're first scanning the last |
| 5528 | * half. Let's loop back to the beginning of the tree now. |
| 5529 | */ |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5530 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5531 | eb = eb32_first(&idle_conn_srv); |
| 5532 | if (likely(!eb)) |
| 5533 | break; |
| 5534 | } |
| 5535 | if (tick_is_lt(now_ms, eb->key)) { |
| 5536 | /* timer not expired yet, revisit it later */ |
| 5537 | next_wakeup = eb->key; |
| 5538 | need_wakeup = 1; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5539 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5540 | } |
| 5541 | srv = eb32_entry(eb, struct server, idle_node); |
| 5542 | for (i = 0; i < global.nbthread; i++) { |
| 5543 | int max_conn = (srv->curr_idle_thr[i] / 2) + |
| 5544 | (srv->curr_idle_thr[i] & 1); |
| 5545 | int j; |
| 5546 | int did_remove = 0; |
| 5547 | |
Olivier Houchard | ba936e5 | 2019-07-11 15:49:00 +0200 | [diff] [blame] | 5548 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5549 | for (j = 0; j < max_conn; j++) { |
| 5550 | struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list); |
| 5551 | if (!conn) |
| 5552 | break; |
| 5553 | did_remove = 1; |
| 5554 | LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list); |
| 5555 | } |
Olivier Houchard | ba936e5 | 2019-07-11 15:49:00 +0200 | [diff] [blame] | 5556 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5557 | if (did_remove && max_conn < srv->curr_idle_thr[i]) |
| 5558 | srv_is_empty = 0; |
| 5559 | if (did_remove) |
| 5560 | task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER); |
| 5561 | } |
| 5562 | eb32_delete(&srv->idle_node); |
| 5563 | if (!srv_is_empty) { |
| 5564 | /* There are still more idle connections, add the |
| 5565 | * server back in the tree. |
| 5566 | */ |
| 5567 | srv->idle_node.key = tick_add(srv->pool_purge_delay, |
| 5568 | now_ms); |
| 5569 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
| 5570 | } |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5571 | } |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5572 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5573 | |
| 5574 | if (need_wakeup) |
| 5575 | task->expire = next_wakeup; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5576 | else |
| 5577 | task->expire = TICK_ETERNITY; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5578 | |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5579 | return task; |
| 5580 | } |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 5581 | |
| 5582 | /* config parser for global "tune.pool-{low,high}-fd-ratio" */ |
| 5583 | static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx, |
| 5584 | struct proxy *defpx, const char *file, int line, |
| 5585 | char **err) |
| 5586 | { |
| 5587 | int arg = -1; |
| 5588 | |
| 5589 | if (too_many_args(1, args, err, NULL)) |
| 5590 | return -1; |
| 5591 | |
| 5592 | if (*(args[1]) != 0) |
| 5593 | arg = atoi(args[1]); |
| 5594 | |
| 5595 | if (arg < 0 || arg > 100) { |
| 5596 | memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]); |
| 5597 | return -1; |
| 5598 | } |
| 5599 | |
| 5600 | if (args[0][10] == 'h') |
| 5601 | global.tune.pool_high_ratio = arg; |
| 5602 | else |
| 5603 | global.tune.pool_low_ratio = arg; |
| 5604 | return 0; |
| 5605 | } |
| 5606 | |
| 5607 | /* config keyword parsers */ |
| 5608 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 5609 | { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 5610 | { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 5611 | { 0, NULL, NULL } |
| 5612 | }}; |
| 5613 | |
| 5614 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 5615 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 5616 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5617 | * Local variables: |
| 5618 | * c-indent-level: 8 |
| 5619 | * c-basic-offset: 8 |
| 5620 | * End: |
| 5621 | */ |