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); |
| 780 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 781 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 782 | } |
| 783 | |
| 784 | *cur_arg += 2; |
| 785 | while (*(args[*cur_arg])) { |
| 786 | if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */ |
| 787 | #if defined(CONFIG_HAP_TRANSPARENT) |
| 788 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 789 | ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', " |
| 790 | "or 'hdr_ip(name,#)' as argument.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 791 | goto err; |
| 792 | } |
| 793 | if (!strcmp(args[*cur_arg + 1], "client")) { |
| 794 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 795 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 796 | } |
| 797 | else if (!strcmp(args[*cur_arg + 1], "clientip")) { |
| 798 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 799 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 800 | } |
| 801 | else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) { |
| 802 | char *name, *end; |
| 803 | |
| 804 | name = args[*cur_arg + 1] + 7; |
| 805 | while (isspace(*name)) |
| 806 | name++; |
| 807 | |
| 808 | end = name; |
| 809 | while (*end && !isspace(*end) && *end != ',' && *end != ')') |
| 810 | end++; |
| 811 | |
| 812 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 813 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 814 | free(newsrv->conn_src.bind_hdr_name); |
| 815 | newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1); |
| 816 | newsrv->conn_src.bind_hdr_len = end - name; |
| 817 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 818 | newsrv->conn_src.bind_hdr_name[end - name] = '\0'; |
| 819 | newsrv->conn_src.bind_hdr_occ = -1; |
| 820 | |
| 821 | /* now look for an occurrence number */ |
| 822 | while (isspace(*end)) |
| 823 | end++; |
| 824 | if (*end == ',') { |
| 825 | end++; |
| 826 | name = end; |
| 827 | if (*end == '-') |
| 828 | end++; |
| 829 | while (isdigit((int)*end)) |
| 830 | end++; |
| 831 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name); |
| 832 | } |
| 833 | |
| 834 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 835 | ha_alert("usesrc hdr_ip(name,num) does not support negative" |
| 836 | " occurrences values smaller than %d.\n", MAX_HDR_HISTORY); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 837 | goto err; |
| 838 | } |
| 839 | } |
| 840 | else { |
| 841 | struct sockaddr_storage *sk; |
| 842 | int port1, port2; |
| 843 | |
| 844 | /* 'sk' is statically allocated (no need to be freed). */ |
| 845 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1); |
| 846 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 847 | 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] | 848 | goto err; |
| 849 | } |
| 850 | |
| 851 | proto = protocol_by_family(sk->ss_family); |
| 852 | if (!proto || !proto->connect) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 853 | ha_alert("'%s %s' : connect() not supported for this address family.\n", |
| 854 | args[*cur_arg], args[*cur_arg + 1]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 855 | goto err; |
| 856 | } |
| 857 | |
| 858 | if (port1 != port2) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 859 | ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n", |
| 860 | args[*cur_arg], args[*cur_arg + 1]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 861 | goto err; |
| 862 | } |
| 863 | newsrv->conn_src.tproxy_addr = *sk; |
| 864 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 865 | } |
| 866 | global.last_checks |= LSTCHK_NETADM; |
| 867 | *cur_arg += 2; |
| 868 | continue; |
| 869 | #else /* no TPROXY support */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 870 | 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] | 871 | goto err; |
| 872 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
| 873 | } /* "usesrc" */ |
| 874 | |
| 875 | if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */ |
| 876 | #ifdef SO_BINDTODEVICE |
| 877 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 878 | ha_alert("'%s' : missing interface name.\n", args[0]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 879 | goto err; |
| 880 | } |
| 881 | free(newsrv->conn_src.iface_name); |
| 882 | newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]); |
| 883 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 884 | global.last_checks |= LSTCHK_NETADM; |
| 885 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 886 | 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] | 887 | goto err; |
| 888 | #endif |
| 889 | *cur_arg += 2; |
| 890 | continue; |
| 891 | } |
| 892 | /* this keyword in not an option of "source" */ |
| 893 | break; |
| 894 | } /* while */ |
| 895 | |
| 896 | return 0; |
| 897 | |
| 898 | err: |
| 899 | free(errmsg); |
| 900 | return ERR_ALERT | ERR_FATAL; |
| 901 | } |
| 902 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 903 | /* Parse the "stick" server keyword */ |
| 904 | static int srv_parse_stick(char **args, int *cur_arg, |
| 905 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 906 | { |
| 907 | newsrv->flags &= ~SRV_F_NON_STICK; |
| 908 | return 0; |
| 909 | } |
| 910 | |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 911 | /* Parse the "track" server keyword */ |
| 912 | static int srv_parse_track(char **args, int *cur_arg, |
| 913 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 914 | { |
| 915 | char *arg; |
| 916 | |
| 917 | arg = args[*cur_arg + 1]; |
| 918 | if (!*arg) { |
| 919 | memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n"); |
| 920 | return ERR_ALERT | ERR_FATAL; |
| 921 | } |
| 922 | |
| 923 | free(newsrv->trackit); |
| 924 | newsrv->trackit = strdup(arg); |
| 925 | |
| 926 | return 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /* Parse the "socks4" server keyword */ |
| 930 | static int srv_parse_socks4(char **args, int *cur_arg, |
| 931 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 932 | { |
| 933 | char *errmsg; |
| 934 | int port_low, port_high; |
| 935 | struct sockaddr_storage *sk; |
| 936 | struct protocol *proto; |
| 937 | |
| 938 | errmsg = NULL; |
| 939 | |
| 940 | if (!*args[*cur_arg + 1]) { |
| 941 | memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]); |
| 942 | goto err; |
| 943 | } |
| 944 | |
| 945 | /* 'sk' is statically allocated (no need to be freed). */ |
| 946 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1); |
| 947 | if (!sk) { |
| 948 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 949 | goto err; |
| 950 | } |
| 951 | |
| 952 | proto = protocol_by_family(sk->ss_family); |
| 953 | if (!proto || !proto->connect) { |
| 954 | ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]); |
| 955 | goto err; |
| 956 | } |
| 957 | |
| 958 | newsrv->flags |= SRV_F_SOCKS4_PROXY; |
| 959 | newsrv->socks4_addr = *sk; |
| 960 | |
| 961 | if (port_low != port_high) { |
| 962 | ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]); |
| 963 | goto err; |
| 964 | } |
| 965 | |
Willy Tarreau | bc90776 | 2020-09-15 11:25:54 +0200 | [diff] [blame] | 966 | if (port_low <= 0 || port_low > 65535) { |
| 967 | ha_alert("'%s': invalid port %d.\n", args[*cur_arg], port_low); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 968 | goto err; |
| 969 | } |
| 970 | |
| 971 | return 0; |
| 972 | |
| 973 | err: |
| 974 | free(errmsg); |
| 975 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 978 | |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 979 | /* parse the "tfo" server keyword */ |
| 980 | static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 981 | { |
| 982 | newsrv->flags |= SRV_F_FASTOPEN; |
| 983 | return 0; |
| 984 | } |
| 985 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 986 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 987 | * 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] | 988 | * shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 989 | * |
| 990 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 991 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 992 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 993 | { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 994 | struct stream *stream, *stream_bck; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 995 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 996 | list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv) |
| 997 | if (stream->srv_conn == srv) |
| 998 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* 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] | 1002 | * 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] | 1003 | * the reason for the shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1004 | * |
| 1005 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1006 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1007 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1008 | { |
| 1009 | struct server *srv; |
| 1010 | |
| 1011 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 1012 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1013 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1016 | /* Appends some information to a message string related to a server going UP or |
| 1017 | * DOWN. If both <forced> and <reason> are null and the server tracks another |
| 1018 | * 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] | 1019 | * If <check> is non-null, an entire string describing the check result will be |
| 1020 | * appended after a comma and a space (eg: to report some information from the |
| 1021 | * check that changed the state). In the other case, the string will be built |
| 1022 | * using the check results stored into the struct server if present. |
| 1023 | * If <xferred> is non-negative, some information about requeued sessions are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1024 | * provided. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1025 | * |
| 1026 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1027 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1028 | void srv_append_status(struct buffer *msg, struct server *s, |
| 1029 | struct check *check, int xferred, int forced) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1030 | { |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1031 | short status = s->op_st_chg.status; |
| 1032 | short code = s->op_st_chg.code; |
| 1033 | long duration = s->op_st_chg.duration; |
| 1034 | char *desc = s->op_st_chg.reason; |
| 1035 | |
| 1036 | if (check) { |
| 1037 | status = check->status; |
| 1038 | code = check->code; |
| 1039 | duration = check->duration; |
| 1040 | desc = check->desc; |
| 1041 | } |
| 1042 | |
| 1043 | if (status != -1) { |
| 1044 | chunk_appendf(msg, ", reason: %s", get_check_status_description(status)); |
| 1045 | |
| 1046 | if (status >= HCHK_STATUS_L57DATA) |
| 1047 | chunk_appendf(msg, ", code: %d", code); |
| 1048 | |
| 1049 | if (desc && *desc) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1050 | struct buffer src; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1051 | |
| 1052 | chunk_appendf(msg, ", info: \""); |
| 1053 | |
| 1054 | chunk_initlen(&src, desc, 0, strlen(desc)); |
| 1055 | chunk_asciiencode(msg, &src, '"'); |
| 1056 | |
| 1057 | chunk_appendf(msg, "\""); |
| 1058 | } |
| 1059 | |
| 1060 | if (duration >= 0) |
| 1061 | chunk_appendf(msg, ", check duration: %ldms", duration); |
| 1062 | } |
| 1063 | else if (desc && *desc) { |
| 1064 | chunk_appendf(msg, ", %s", desc); |
| 1065 | } |
| 1066 | else if (!forced && s->track) { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1067 | 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] | 1068 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1069 | |
| 1070 | if (xferred >= 0) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1071 | if (s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1072 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 1073 | " %d sessions active, %d requeued, %d remaining in queue", |
| 1074 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1075 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 1076 | s->cur_sess, xferred, s->nbpend); |
| 1077 | else |
| 1078 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 1079 | " %d sessions requeued, %d total in queue", |
| 1080 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1081 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 1082 | xferred, s->nbpend); |
| 1083 | } |
| 1084 | } |
| 1085 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1086 | /* Marks server <s> down, regardless of its checks' statuses. The server is |
| 1087 | * registered in a list to postpone the counting of the remaining servers on |
| 1088 | * the proxy and transfers queued streams whenever possible to other servers at |
| 1089 | * a sync point. Maintenance servers are ignored. It stores the <reason> if |
| 1090 | * non-null as the reason for going down or the available data from the check |
| 1091 | * struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1092 | * |
| 1093 | * Must be called with the server lock held. |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1094 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1095 | 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] | 1096 | { |
| 1097 | struct server *srv; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1098 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1099 | 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] | 1100 | return; |
| 1101 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1102 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1103 | *s->op_st_chg.reason = 0; |
| 1104 | s->op_st_chg.status = -1; |
| 1105 | if (reason) { |
| 1106 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1107 | } |
| 1108 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1109 | 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] | 1110 | s->op_st_chg.code = check->code; |
| 1111 | s->op_st_chg.status = check->status; |
| 1112 | s->op_st_chg.duration = check->duration; |
| 1113 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1114 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1115 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1116 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1117 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1118 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1119 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1120 | srv_set_stopped(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1121 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1122 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1125 | /* 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] | 1126 | * in maintenance. The server is registered in a list to postpone the counting |
| 1127 | * of the remaining servers on the proxy and tries to grab requests from the |
| 1128 | * proxy at a sync point. Maintenance servers are ignored. It stores the |
| 1129 | * <reason> if non-null as the reason for going down or the available data |
| 1130 | * from the check struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1131 | * |
| 1132 | * Must be called with the server lock held. |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1133 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1134 | 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] | 1135 | { |
| 1136 | struct server *srv; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1137 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1138 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1139 | return; |
| 1140 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1141 | 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] | 1142 | return; |
| 1143 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1144 | s->next_state = SRV_ST_STARTING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1145 | *s->op_st_chg.reason = 0; |
| 1146 | s->op_st_chg.status = -1; |
| 1147 | if (reason) { |
| 1148 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1149 | } |
| 1150 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1151 | 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] | 1152 | s->op_st_chg.code = check->code; |
| 1153 | s->op_st_chg.status = check->status; |
| 1154 | s->op_st_chg.duration = check->duration; |
| 1155 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1156 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1157 | if (s->slowstart <= 0) |
| 1158 | s->next_state = SRV_ST_RUNNING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1159 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1160 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1161 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1162 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1163 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1164 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1165 | srv_set_running(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1166 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1167 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1170 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1171 | * isn't in maintenance. The server is registered in a list to postpone the |
| 1172 | * counting of the remaining servers on the proxy and tries to grab requests |
| 1173 | * from the proxy. Maintenance servers are ignored. It stores the |
| 1174 | * <reason> if non-null as the reason for going down or the available data |
| 1175 | * from the check struct to recompute this reason later. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1176 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 1177 | * must not be placed there. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1178 | * |
| 1179 | * Must be called with the server lock held. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1180 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1181 | 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] | 1182 | { |
| 1183 | struct server *srv; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1184 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1185 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1186 | return; |
| 1187 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1188 | if (s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1189 | return; |
| 1190 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1191 | s->next_state = SRV_ST_STOPPING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1192 | *s->op_st_chg.reason = 0; |
| 1193 | s->op_st_chg.status = -1; |
| 1194 | if (reason) { |
| 1195 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1196 | } |
| 1197 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1198 | 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] | 1199 | s->op_st_chg.code = check->code; |
| 1200 | s->op_st_chg.status = check->status; |
| 1201 | s->op_st_chg.duration = check->duration; |
| 1202 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1203 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1204 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1205 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1206 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1207 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1208 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1209 | srv_set_stopping(srv, NULL, NULL); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1210 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1211 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1212 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1213 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1214 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1215 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 1216 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 1217 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 1218 | * 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] | 1219 | * is done. If <cause> is non-null, it will be displayed at the end of the log |
| 1220 | * lines to justify the state change. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1221 | * |
| 1222 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1223 | */ |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1224 | 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] | 1225 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1226 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1227 | |
| 1228 | if (!mode) |
| 1229 | return; |
| 1230 | |
| 1231 | /* 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] | 1232 | if (s->next_admin & mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1233 | return; |
| 1234 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1235 | s->next_admin |= mode; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1236 | if (cause) |
| 1237 | strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause)); |
| 1238 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1239 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1240 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1241 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1242 | /* 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] | 1243 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) || |
| 1244 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1245 | return; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1246 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1247 | /* compute the inherited flag to propagate */ |
| 1248 | if (mode & SRV_ADMF_MAINT) |
| 1249 | mode = SRV_ADMF_IMAINT; |
| 1250 | else if (mode & SRV_ADMF_DRAIN) |
| 1251 | mode = SRV_ADMF_IDRAIN; |
| 1252 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1253 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1254 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1255 | srv_set_admin_flag(srv, mode, cause); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1256 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1257 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1260 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1261 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 1262 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 1263 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 1264 | * 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] | 1265 | * |
| 1266 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1267 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1268 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1269 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1270 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1271 | |
| 1272 | if (!mode) |
| 1273 | return; |
| 1274 | |
| 1275 | /* 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] | 1276 | if (!(s->next_admin & mode)) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1277 | return; |
| 1278 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1279 | s->next_admin &= ~mode; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1280 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1281 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1282 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1283 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1284 | /* 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] | 1285 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) || |
| 1286 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1287 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1288 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1289 | if (mode & SRV_ADMF_MAINT) |
| 1290 | mode = SRV_ADMF_IMAINT; |
| 1291 | else if (mode & SRV_ADMF_DRAIN) |
| 1292 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1293 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1294 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1295 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1296 | srv_clr_admin_flag(srv, mode); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1297 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1298 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1301 | /* principle: propagate maint and drain to tracking servers. This is useful |
| 1302 | * upon startup so that inherited states are correct. |
| 1303 | */ |
| 1304 | static void srv_propagate_admin_state(struct server *srv) |
| 1305 | { |
| 1306 | struct server *srv2; |
| 1307 | |
| 1308 | if (!srv->trackers) |
| 1309 | return; |
| 1310 | |
| 1311 | for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1312 | HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1313 | if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT)) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1314 | srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1315 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1316 | if (srv->next_admin & SRV_ADMF_DRAIN) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1317 | srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1318 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | /* Compute and propagate the admin states for all servers in proxy <px>. |
| 1323 | * Only servers *not* tracking another one are considered, because other |
| 1324 | * ones will be handled when the server they track is visited. |
| 1325 | */ |
| 1326 | void srv_compute_all_admin_states(struct proxy *px) |
| 1327 | { |
| 1328 | struct server *srv; |
| 1329 | |
| 1330 | for (srv = px->srv; srv; srv = srv->next) { |
| 1331 | if (srv->track) |
| 1332 | continue; |
| 1333 | srv_propagate_admin_state(srv); |
| 1334 | } |
| 1335 | } |
| 1336 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1337 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1338 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1339 | * all code contributors. |
| 1340 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1341 | * the config parser can report an appropriate error when a known keyword was |
| 1342 | * not enabled. |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1343 | * 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] | 1344 | */ |
| 1345 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
Frédéric Lécaille | 6e5e0d8 | 2017-03-20 16:30:18 +0100 | [diff] [blame] | 1346 | { "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] | 1347 | { "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] | 1348 | { "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] | 1349 | { "check", srv_parse_check, 0, 1 }, /* enable health checks */ |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 1350 | { "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] | 1351 | { "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] | 1352 | { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */ |
| 1353 | { "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] | 1354 | { "id", srv_parse_id, 1, 0 }, /* set id# of server */ |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 1355 | { "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] | 1356 | { "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] | 1357 | { "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] | 1358 | { "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] | 1359 | { "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] | 1360 | { "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] | 1361 | { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */ |
| 1362 | { "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] | 1363 | { "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] | 1364 | { "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] | 1365 | { "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] | 1366 | { "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] | 1367 | { "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] | 1368 | { "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] | 1369 | { "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] | 1370 | { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */ |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1371 | { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */ |
| 1372 | { "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] | 1373 | { "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] | 1374 | { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */ |
Olivier Houchard | 1a9c25f | 2019-07-04 13:34:10 +0200 | [diff] [blame] | 1375 | { "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] | 1376 | { "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] | 1377 | { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/ |
| 1378 | { "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] | 1379 | { NULL, NULL, 0 }, |
| 1380 | }}; |
| 1381 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1382 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1383 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1384 | /* Recomputes the server's eweight based on its state, uweight, the current time, |
| 1385 | * 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] | 1386 | * state is automatically disabled if the time is elapsed. If <must_update> is |
| 1387 | * not zero, the update will be propagated immediately. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1388 | * |
| 1389 | * Must be called with the server lock held. |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1390 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1391 | void server_recalc_eweight(struct server *sv, int must_update) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1392 | { |
| 1393 | struct proxy *px = sv->proxy; |
| 1394 | unsigned w; |
| 1395 | |
| 1396 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 1397 | /* go to full throttle if the slowstart interval is reached */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1398 | if (sv->next_state == SRV_ST_STARTING) |
| 1399 | sv->next_state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 1403 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 1404 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1405 | 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] | 1406 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 1407 | else |
| 1408 | w = px->lbprm.wdiv; |
| 1409 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1410 | 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] | 1411 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1412 | /* propagate changes only if needed (i.e. not recursively) */ |
Willy Tarreau | 49725a0 | 2018-08-21 19:54:09 +0200 | [diff] [blame] | 1413 | if (must_update) |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1414 | srv_update_status(sv); |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1415 | } |
| 1416 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1417 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1418 | * Parses weight_str and configures sv accordingly. |
| 1419 | * Returns NULL on success, error message string otherwise. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1420 | * |
| 1421 | * Must be called with the server lock held. |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1422 | */ |
| 1423 | const char *server_parse_weight_change_request(struct server *sv, |
| 1424 | const char *weight_str) |
| 1425 | { |
| 1426 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1427 | long int w; |
| 1428 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1429 | |
| 1430 | px = sv->proxy; |
| 1431 | |
| 1432 | /* if the weight is terminated with '%', it is set relative to |
| 1433 | * the initial weight, otherwise it is absolute. |
| 1434 | */ |
| 1435 | if (!*weight_str) |
| 1436 | return "Require <weight> or <weight%>.\n"; |
| 1437 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1438 | w = strtol(weight_str, &end, 10); |
| 1439 | if (end == weight_str) |
| 1440 | return "Empty weight string empty or preceded by garbage"; |
| 1441 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1442 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1443 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1444 | /* Avoid integer overflow */ |
| 1445 | if (w > 25600) |
| 1446 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1447 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1448 | if (w > 256) |
| 1449 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1450 | } |
| 1451 | else if (w < 0 || w > 256) |
| 1452 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1453 | else if (end[0] != '\0') |
| 1454 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1455 | |
| 1456 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 1457 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 1458 | |
| 1459 | sv->uweight = w; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1460 | server_recalc_eweight(sv, 1); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1461 | |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1465 | /* |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1466 | * Parses <addr_str> and configures <sv> accordingly. <from> precise |
| 1467 | * the source of the change in the associated message log. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1468 | * Returns: |
| 1469 | * - error string on error |
| 1470 | * - NULL on success |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1471 | * |
| 1472 | * Must be called with the server lock held. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1473 | */ |
| 1474 | const char *server_parse_addr_change_request(struct server *sv, |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1475 | const char *addr_str, const char *updater) |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1476 | { |
| 1477 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 1478 | |
| 1479 | if (inet_pton(AF_INET6, addr_str, ip)) { |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1480 | update_server_addr(sv, ip, AF_INET6, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1481 | return NULL; |
| 1482 | } |
| 1483 | if (inet_pton(AF_INET, addr_str, ip)) { |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1484 | update_server_addr(sv, ip, AF_INET, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1485 | return NULL; |
| 1486 | } |
| 1487 | |
| 1488 | return "Could not understand IP address format.\n"; |
| 1489 | } |
| 1490 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1491 | /* |
| 1492 | * Must be called with the server lock held. |
| 1493 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1494 | const char *server_parse_maxconn_change_request(struct server *sv, |
| 1495 | const char *maxconn_str) |
| 1496 | { |
| 1497 | long int v; |
| 1498 | char *end; |
| 1499 | |
| 1500 | if (!*maxconn_str) |
| 1501 | return "Require <maxconn>.\n"; |
| 1502 | |
| 1503 | v = strtol(maxconn_str, &end, 10); |
| 1504 | if (end == maxconn_str) |
| 1505 | return "maxconn string empty or preceded by garbage"; |
| 1506 | else if (end[0] != '\0') |
| 1507 | return "Trailing garbage in maxconn string"; |
| 1508 | |
| 1509 | if (sv->maxconn == sv->minconn) { // static maxconn |
| 1510 | sv->maxconn = sv->minconn = v; |
| 1511 | } else { // dynamic maxconn |
| 1512 | sv->maxconn = v; |
| 1513 | } |
| 1514 | |
| 1515 | if (may_dequeue_tasks(sv, sv->proxy)) |
| 1516 | process_srv_queue(sv); |
| 1517 | |
| 1518 | return NULL; |
| 1519 | } |
| 1520 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1521 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1522 | static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px, |
| 1523 | const char *file, int linenum, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1524 | { |
| 1525 | int idx; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1526 | const char *args[] = { |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1527 | srv->sni_expr, |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1528 | NULL, |
| 1529 | }; |
| 1530 | |
| 1531 | idx = 0; |
Olivier Houchard | 7d8e688 | 2017-04-20 18:21:17 +0200 | [diff] [blame] | 1532 | px->conf.args.ctx = ARGC_SRV; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1533 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1534 | return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args); |
| 1535 | } |
| 1536 | |
| 1537 | static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err) |
| 1538 | { |
| 1539 | struct sample_expr *expr; |
| 1540 | |
| 1541 | 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] | 1542 | if (!expr) { |
| 1543 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 1544 | return ERR_ALERT | ERR_FATAL; |
| 1545 | } |
| 1546 | |
| 1547 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 1548 | memprintf(err, "error detected while parsing sni expression : " |
| 1549 | " fetch method '%s' extracts information from '%s', " |
| 1550 | "none of which is available here.\n", |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1551 | newsrv->sni_expr, sample_src_names(expr->fetch->use)); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1552 | return ERR_ALERT | ERR_FATAL; |
| 1553 | } |
| 1554 | |
| 1555 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 1556 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 1557 | newsrv->ssl_ctx.sni = expr; |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | #endif |
| 1562 | |
| 1563 | static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err) |
| 1564 | { |
| 1565 | if (err && *err) { |
| 1566 | indent_msg(err, 2); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1567 | 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] | 1568 | } |
| 1569 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1570 | ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n", |
| 1571 | file, linenum, args[0], args[1], args[cur_arg]); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1574 | static void srv_conn_src_sport_range_cpy(struct server *srv, |
| 1575 | struct server *src) |
| 1576 | { |
| 1577 | int range_sz; |
| 1578 | |
| 1579 | range_sz = src->conn_src.sport_range->size; |
| 1580 | if (range_sz > 0) { |
| 1581 | srv->conn_src.sport_range = port_range_alloc_range(range_sz); |
| 1582 | if (srv->conn_src.sport_range != NULL) { |
| 1583 | int i; |
| 1584 | |
| 1585 | for (i = 0; i < range_sz; i++) { |
| 1586 | srv->conn_src.sport_range->ports[i] = |
| 1587 | src->conn_src.sport_range->ports[i]; |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * Copy <src> server connection source settings to <srv> server everything needed. |
| 1595 | */ |
| 1596 | static void srv_conn_src_cpy(struct server *srv, struct server *src) |
| 1597 | { |
| 1598 | srv->conn_src.opts = src->conn_src.opts; |
| 1599 | srv->conn_src.source_addr = src->conn_src.source_addr; |
| 1600 | |
| 1601 | /* Source port range copy. */ |
| 1602 | if (src->conn_src.sport_range != NULL) |
| 1603 | srv_conn_src_sport_range_cpy(srv, src); |
| 1604 | |
| 1605 | #ifdef CONFIG_HAP_TRANSPARENT |
| 1606 | if (src->conn_src.bind_hdr_name != NULL) { |
| 1607 | srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name); |
| 1608 | srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name); |
| 1609 | } |
| 1610 | srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ; |
| 1611 | srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr; |
| 1612 | #endif |
| 1613 | if (src->conn_src.iface_name != NULL) |
| 1614 | srv->conn_src.iface_name = strdup(src->conn_src.iface_name); |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * Copy <src> server SSL settings to <srv> server allocating |
| 1619 | * everything needed. |
| 1620 | */ |
| 1621 | #if defined(USE_OPENSSL) |
| 1622 | static void srv_ssl_settings_cpy(struct server *srv, struct server *src) |
| 1623 | { |
| 1624 | if (src->ssl_ctx.ca_file != NULL) |
| 1625 | srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file); |
| 1626 | if (src->ssl_ctx.crl_file != NULL) |
| 1627 | srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file); |
| 1628 | if (src->ssl_ctx.client_crt != NULL) |
| 1629 | srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt); |
| 1630 | |
| 1631 | srv->ssl_ctx.verify = src->ssl_ctx.verify; |
| 1632 | |
| 1633 | if (src->ssl_ctx.verify_host != NULL) |
| 1634 | srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host); |
| 1635 | if (src->ssl_ctx.ciphers != NULL) |
| 1636 | srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers); |
Jerome Magnin | 770bc8a | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 1637 | if (src->ssl_ctx.options) |
| 1638 | srv->ssl_ctx.options = src->ssl_ctx.options; |
| 1639 | if (src->ssl_ctx.methods.flags) |
| 1640 | srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags; |
| 1641 | if (src->ssl_ctx.methods.min) |
| 1642 | srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min; |
| 1643 | if (src->ssl_ctx.methods.max) |
| 1644 | srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max; |
| 1645 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1646 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 1647 | if (src->ssl_ctx.ciphersuites != NULL) |
| 1648 | srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites); |
| 1649 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1650 | if (src->sni_expr != NULL) |
| 1651 | srv->sni_expr = strdup(src->sni_expr); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1652 | |
| 1653 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 1654 | if (src->ssl_ctx.alpn_str) { |
| 1655 | srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len); |
| 1656 | if (srv->ssl_ctx.alpn_str) { |
| 1657 | memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str, |
| 1658 | src->ssl_ctx.alpn_len); |
| 1659 | srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len; |
| 1660 | } |
| 1661 | } |
| 1662 | #endif |
| 1663 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1664 | if (src->ssl_ctx.npn_str) { |
| 1665 | srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len); |
| 1666 | if (srv->ssl_ctx.npn_str) { |
| 1667 | memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str, |
| 1668 | src->ssl_ctx.npn_len); |
| 1669 | srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len; |
| 1670 | } |
| 1671 | } |
| 1672 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1673 | } |
| 1674 | #endif |
| 1675 | |
| 1676 | /* |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1677 | * Prepare <srv> for hostname resolution. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1678 | * 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] | 1679 | * 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] | 1680 | */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1681 | 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] | 1682 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1683 | char *hostname_dn; |
| 1684 | int hostname_len, hostname_dn_len; |
| 1685 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1686 | if (!hostname) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 1687 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1688 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1689 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1690 | hostname_dn = trash.area; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1691 | hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1, |
| 1692 | hostname_dn, trash.size); |
| 1693 | if (hostname_dn_len == -1) |
| 1694 | goto err; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 1695 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1696 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1697 | free(srv->hostname); |
| 1698 | free(srv->hostname_dn); |
| 1699 | srv->hostname = strdup(hostname); |
| 1700 | srv->hostname_dn = strdup(hostname_dn); |
| 1701 | srv->hostname_dn_len = hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1702 | if (!srv->hostname || !srv->hostname_dn) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1703 | goto err; |
| 1704 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 1705 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1706 | |
| 1707 | err: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1708 | free(srv->hostname); srv->hostname = NULL; |
| 1709 | free(srv->hostname_dn); srv->hostname_dn = NULL; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 1710 | return -1; |
| 1711 | } |
| 1712 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1713 | /* |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1714 | * Copy <src> server settings to <srv> server allocating |
| 1715 | * everything needed. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1716 | * This function is not supposed to be called at any time, but only |
| 1717 | * during server settings parsing or during server allocations from |
| 1718 | * a server template, and just after having calloc()'ed a new server. |
| 1719 | * So, <src> may only be a default server (when parsing server settings) |
| 1720 | * or a server template (during server allocations from a server template). |
| 1721 | * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template, |
| 1722 | * 0 if not). |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1723 | */ |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1724 | 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] | 1725 | { |
| 1726 | /* Connection source settings copy */ |
| 1727 | srv_conn_src_cpy(srv, src); |
| 1728 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1729 | if (srv_tmpl) { |
| 1730 | srv->addr = src->addr; |
| 1731 | srv->svc_port = src->svc_port; |
| 1732 | } |
| 1733 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1734 | srv->pp_opts = src->pp_opts; |
| 1735 | if (src->rdr_pfx != NULL) { |
| 1736 | srv->rdr_pfx = strdup(src->rdr_pfx); |
| 1737 | srv->rdr_len = src->rdr_len; |
| 1738 | } |
| 1739 | if (src->cookie != NULL) { |
| 1740 | srv->cookie = strdup(src->cookie); |
| 1741 | srv->cklen = src->cklen; |
| 1742 | } |
| 1743 | srv->use_ssl = src->use_ssl; |
Willy Tarreau | fb38956 | 2019-12-11 15:43:45 +0100 | [diff] [blame] | 1744 | srv->check.addr = src->check.addr; |
| 1745 | srv->agent.addr = src->agent.addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1746 | srv->check.use_ssl = src->check.use_ssl; |
| 1747 | srv->check.port = src->check.port; |
Olivier Houchard | 2194401 | 2018-12-21 19:42:01 +0100 | [diff] [blame] | 1748 | srv->check.sni = src->check.sni; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 1749 | srv->check.alpn_str = src->check.alpn_str; |
Ilya Shipitsin | 0c50b1e | 2019-04-30 21:21:28 +0500 | [diff] [blame] | 1750 | srv->check.alpn_len = src->check.alpn_len; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1751 | /* Note: 'flags' field has potentially been already initialized. */ |
| 1752 | srv->flags |= src->flags; |
| 1753 | srv->do_check = src->do_check; |
| 1754 | srv->do_agent = src->do_agent; |
| 1755 | if (srv->check.port) |
| 1756 | srv->flags |= SRV_F_CHECKPORT; |
| 1757 | srv->check.inter = src->check.inter; |
| 1758 | srv->check.fastinter = src->check.fastinter; |
| 1759 | srv->check.downinter = src->check.downinter; |
| 1760 | srv->agent.use_ssl = src->agent.use_ssl; |
| 1761 | srv->agent.port = src->agent.port; |
| 1762 | if (src->agent.send_string != NULL) |
| 1763 | srv->agent.send_string = strdup(src->agent.send_string); |
| 1764 | srv->agent.send_string_len = src->agent.send_string_len; |
| 1765 | srv->agent.inter = src->agent.inter; |
| 1766 | srv->agent.fastinter = src->agent.fastinter; |
| 1767 | srv->agent.downinter = src->agent.downinter; |
| 1768 | srv->maxqueue = src->maxqueue; |
| 1769 | srv->minconn = src->minconn; |
| 1770 | srv->maxconn = src->maxconn; |
| 1771 | srv->slowstart = src->slowstart; |
| 1772 | srv->observe = src->observe; |
| 1773 | srv->onerror = src->onerror; |
| 1774 | srv->onmarkeddown = src->onmarkeddown; |
| 1775 | srv->onmarkedup = src->onmarkedup; |
| 1776 | if (src->trackit != NULL) |
| 1777 | srv->trackit = strdup(src->trackit); |
| 1778 | srv->consecutive_errors_limit = src->consecutive_errors_limit; |
| 1779 | srv->uweight = srv->iweight = src->iweight; |
| 1780 | |
| 1781 | srv->check.send_proxy = src->check.send_proxy; |
| 1782 | /* health: up, but will fall down at first failure */ |
| 1783 | srv->check.rise = srv->check.health = src->check.rise; |
| 1784 | srv->check.fall = src->check.fall; |
| 1785 | |
| 1786 | /* Here we check if 'disabled' is the default server state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1787 | if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) { |
| 1788 | srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 1789 | srv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1790 | srv->check.state |= CHK_ST_PAUSED; |
| 1791 | srv->check.health = 0; |
| 1792 | } |
| 1793 | |
| 1794 | /* health: up but will fall down at first failure */ |
| 1795 | srv->agent.rise = srv->agent.health = src->agent.rise; |
| 1796 | srv->agent.fall = src->agent.fall; |
| 1797 | |
| 1798 | if (src->resolvers_id != NULL) |
| 1799 | srv->resolvers_id = strdup(src->resolvers_id); |
| 1800 | srv->dns_opts.family_prio = src->dns_opts.family_prio; |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 1801 | 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] | 1802 | if (srv->dns_opts.family_prio == AF_UNSPEC) |
| 1803 | srv->dns_opts.family_prio = AF_INET6; |
| 1804 | memcpy(srv->dns_opts.pref_net, |
| 1805 | src->dns_opts.pref_net, |
| 1806 | sizeof srv->dns_opts.pref_net); |
| 1807 | srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb; |
| 1808 | |
| 1809 | srv->init_addr_methods = src->init_addr_methods; |
| 1810 | srv->init_addr = src->init_addr; |
| 1811 | #if defined(USE_OPENSSL) |
| 1812 | srv_ssl_settings_cpy(srv, src); |
| 1813 | #endif |
| 1814 | #ifdef TCP_USER_TIMEOUT |
| 1815 | srv->tcp_ut = src->tcp_ut; |
| 1816 | #endif |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1817 | srv->mux_proto = src->mux_proto; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 1818 | srv->pool_purge_delay = src->pool_purge_delay; |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 1819 | srv->max_idle_conns = src->max_idle_conns; |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 1820 | srv->max_reuse = src->max_reuse; |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1821 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1822 | if (srv_tmpl) |
| 1823 | srv->srvrq = src->srvrq; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1824 | |
| 1825 | srv->check.via_socks4 = src->check.via_socks4; |
| 1826 | srv->socks4_addr = src->socks4_addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1827 | } |
| 1828 | |
William Lallemand | 313bfd1 | 2018-10-26 14:47:32 +0200 | [diff] [blame] | 1829 | struct server *new_server(struct proxy *proxy) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1830 | { |
| 1831 | struct server *srv; |
| 1832 | |
| 1833 | srv = calloc(1, sizeof *srv); |
| 1834 | if (!srv) |
| 1835 | return NULL; |
| 1836 | |
| 1837 | srv->obj_type = OBJ_TYPE_SERVER; |
| 1838 | srv->proxy = proxy; |
| 1839 | LIST_INIT(&srv->actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 1840 | srv->pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 1841 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1842 | srv->next_state = SRV_ST_RUNNING; /* early server setup */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1843 | srv->last_change = now.tv_sec; |
| 1844 | |
| 1845 | srv->check.status = HCHK_STATUS_INI; |
| 1846 | srv->check.server = srv; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 1847 | srv->check.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1848 | srv->check.tcpcheck_rules = &proxy->tcpcheck_rules; |
| 1849 | |
| 1850 | srv->agent.status = HCHK_STATUS_INI; |
| 1851 | srv->agent.server = srv; |
Willy Tarreau | 1ba3203 | 2019-01-21 07:48:26 +0100 | [diff] [blame] | 1852 | srv->agent.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1853 | srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW); |
| 1854 | |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 1855 | /* please don't put default server settings here, they are set in |
| 1856 | * init_default_instance(). |
| 1857 | */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1858 | return srv; |
| 1859 | } |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1860 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1861 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1862 | static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg, |
| 1863 | struct server *srv, struct proxy *proxy) |
| 1864 | { |
| 1865 | int ret; |
| 1866 | char *err = NULL; |
| 1867 | |
| 1868 | if (!srv->sni_expr) |
| 1869 | return 0; |
| 1870 | |
| 1871 | ret = server_parse_sni_expr(srv, proxy, &err); |
| 1872 | if (!ret) |
| 1873 | return 0; |
| 1874 | |
| 1875 | display_parser_err(file, linenum, args, cur_arg, &err); |
| 1876 | free(err); |
| 1877 | |
| 1878 | return ret; |
| 1879 | } |
| 1880 | #endif |
| 1881 | |
| 1882 | /* |
| 1883 | * Server initializations finalization. |
| 1884 | * Initialize health check, agent check and SNI expression if enabled. |
| 1885 | * Must not be called for a default server instance. |
| 1886 | */ |
| 1887 | static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg, |
| 1888 | struct server *srv, struct proxy *px) |
| 1889 | { |
Christopher Faulet | 1ebb12c | 2020-04-27 11:17:10 +0200 | [diff] [blame] | 1890 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1891 | int ret; |
Christopher Faulet | 1ebb12c | 2020-04-27 11:17:10 +0200 | [diff] [blame] | 1892 | #endif |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1893 | |
Christopher Faulet | c6e0770 | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 1894 | if (srv->do_check && srv->trackit) { |
| 1895 | ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n", |
| 1896 | file, linenum); |
| 1897 | return ERR_ALERT | ERR_FATAL; |
| 1898 | } |
| 1899 | |
| 1900 | if (srv->do_agent && !srv->agent.port) { |
| 1901 | ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n", |
| 1902 | file, linenum, srv->id); |
| 1903 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1907 | if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0) |
| 1908 | return ret; |
| 1909 | #endif |
| 1910 | |
| 1911 | if (srv->flags & SRV_F_BACKUP) |
| 1912 | px->srv_bck++; |
| 1913 | else |
| 1914 | px->srv_act++; |
| 1915 | srv_lb_commit_status(srv); |
| 1916 | |
| 1917 | return 0; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 1918 | err: |
| 1919 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1920 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 1921 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 1922 | /* |
| 1923 | * Parse as much as possible such a range string argument: low[-high] |
| 1924 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 1925 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 1926 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 1927 | * Returns 0 if succeeded, -1 if not. |
| 1928 | */ |
| 1929 | static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high) |
| 1930 | { |
| 1931 | char *nb_high_arg; |
| 1932 | |
| 1933 | *nb_high = 0; |
| 1934 | chunk_printf(&trash, "%s", arg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1935 | *nb_low = atoi(trash.area); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 1936 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1937 | if ((nb_high_arg = strchr(trash.area, '-'))) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 1938 | *nb_high_arg++ = '\0'; |
| 1939 | *nb_high = atoi(nb_high_arg); |
| 1940 | } |
| 1941 | else { |
| 1942 | *nb_high += *nb_low; |
| 1943 | *nb_low = 1; |
| 1944 | } |
| 1945 | |
| 1946 | if (*nb_low < 0 || *nb_high < *nb_low) |
| 1947 | return -1; |
| 1948 | |
| 1949 | return 0; |
| 1950 | } |
| 1951 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1952 | static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb) |
| 1953 | { |
| 1954 | chunk_printf(&trash, "%s%d", prefix, nb); |
| 1955 | free(srv->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1956 | srv->id = strdup(trash.area); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | /* |
| 1960 | * Initialize as much as possible servers from <srv> server template. |
| 1961 | * Note that a server template is a special server with |
| 1962 | * a few different parameters than a server which has |
| 1963 | * been parsed mostly the same way as a server. |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 1964 | * Returns the number of servers successfully allocated, |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1965 | * 'srv' template included. |
| 1966 | */ |
| 1967 | static int server_template_init(struct server *srv, struct proxy *px) |
| 1968 | { |
| 1969 | int i; |
| 1970 | struct server *newsrv; |
| 1971 | |
| 1972 | 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] | 1973 | newsrv = new_server(px); |
| 1974 | if (!newsrv) |
| 1975 | goto err; |
| 1976 | |
Christopher Faulet | fa31ae9 | 2020-11-02 22:04:55 +0100 | [diff] [blame] | 1977 | newsrv->conf.file = strdup(srv->conf.file); |
| 1978 | newsrv->conf.line = srv->conf.line; |
| 1979 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1980 | srv_settings_cpy(newsrv, srv, 1); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1981 | srv_prepare_for_resolution(newsrv, srv->hostname); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1982 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1983 | if (newsrv->sni_expr) { |
| 1984 | newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL); |
| 1985 | if (!newsrv->ssl_ctx.sni) |
| 1986 | goto err; |
| 1987 | } |
| 1988 | #endif |
| 1989 | /* Set this new server ID. */ |
| 1990 | srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i); |
| 1991 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 1992 | /* Linked backwards first. This will be restablished after parsing. */ |
| 1993 | newsrv->next = px->srv; |
| 1994 | px->srv = newsrv; |
| 1995 | } |
| 1996 | srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low); |
| 1997 | |
| 1998 | return i - srv->tmpl_info.nb_low; |
| 1999 | |
| 2000 | err: |
| 2001 | srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low); |
| 2002 | if (newsrv) { |
| 2003 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2004 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 2005 | #endif |
| 2006 | free_check(&newsrv->agent); |
| 2007 | free_check(&newsrv->check); |
| 2008 | } |
| 2009 | free(newsrv); |
| 2010 | return i - srv->tmpl_info.nb_low; |
| 2011 | } |
| 2012 | |
Emeric Brun | 33f0a73 | 2020-07-21 16:54:36 +0200 | [diff] [blame] | 2013 | int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, |
| 2014 | 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] | 2015 | { |
| 2016 | struct server *newsrv = NULL; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2017 | const char *err = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2018 | char *errmsg = NULL; |
| 2019 | int err_code = 0; |
| 2020 | unsigned val; |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2021 | char *fqdn = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2022 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2023 | if (!strcmp(args[0], "server") || |
Frédéric Lécaille | c06b5d4 | 2018-04-26 10:06:41 +0200 | [diff] [blame] | 2024 | !strcmp(args[0], "peer") || |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2025 | !strcmp(args[0], "default-server") || |
| 2026 | !strcmp(args[0], "server-template")) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2027 | int cur_arg; |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 2028 | int defsrv = (*args[0] == 'd'); |
Frédéric Lécaille | c06b5d4 | 2018-04-26 10:06:41 +0200 | [diff] [blame] | 2029 | 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] | 2030 | int srv_tmpl = !defsrv && !srv; |
| 2031 | int tmpl_range_low = 0, tmpl_range_high = 0; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2032 | |
| 2033 | if (!defsrv && curproxy == defproxy) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2034 | 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] | 2035 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2036 | goto out; |
| 2037 | } |
| 2038 | else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) |
Olivier Houchard | 306e653 | 2018-07-24 16:48:59 +0200 | [diff] [blame] | 2039 | err_code |= ERR_WARN; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2040 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2041 | /* There is no mandatory first arguments for default server. */ |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2042 | if (srv && parse_addr) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2043 | if (!*args[2]) { |
Frédéric Lécaille | 9b0d59d | 2020-04-03 09:43:47 +0200 | [diff] [blame] | 2044 | if (in_peers_section) { |
| 2045 | return 0; |
| 2046 | } |
| 2047 | else { |
| 2048 | /* 'server' line number of argument check. */ |
| 2049 | ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 2050 | file, linenum, args[0]); |
| 2051 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2052 | goto out; |
| 2053 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | err = invalid_char(args[1]); |
| 2057 | } |
| 2058 | else if (srv_tmpl) { |
| 2059 | if (!*args[3]) { |
| 2060 | /* 'server-template' line number of argument check. */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2061 | 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] | 2062 | file, linenum, args[0]); |
| 2063 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2064 | goto out; |
| 2065 | } |
| 2066 | |
| 2067 | err = invalid_prefix_char(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2070 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2071 | 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] | 2072 | file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2073 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2074 | goto out; |
| 2075 | } |
| 2076 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2077 | cur_arg = 2; |
| 2078 | if (srv_tmpl) { |
| 2079 | /* Parse server-template <nb | range> arg. */ |
| 2080 | 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] | 2081 | 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] | 2082 | file, linenum, args[0], args[cur_arg]); |
| 2083 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2084 | goto out; |
| 2085 | } |
| 2086 | cur_arg++; |
| 2087 | } |
| 2088 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2089 | if (!defsrv) { |
| 2090 | struct sockaddr_storage *sk; |
Willy Tarreau | 6ecb10a | 2017-01-06 18:36:06 +0100 | [diff] [blame] | 2091 | int port1, port2, port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2092 | struct protocol *proto; |
| 2093 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2094 | newsrv = new_server(curproxy); |
| 2095 | if (!newsrv) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2096 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2097 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2098 | goto out; |
| 2099 | } |
| 2100 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2101 | if (srv_tmpl) { |
| 2102 | newsrv->tmpl_info.nb_low = tmpl_range_low; |
| 2103 | newsrv->tmpl_info.nb_high = tmpl_range_high; |
| 2104 | } |
| 2105 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2106 | /* the servers are linked backwards first */ |
| 2107 | newsrv->next = curproxy->srv; |
| 2108 | curproxy->srv = newsrv; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2109 | newsrv->conf.file = strdup(file); |
| 2110 | newsrv->conf.line = linenum; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2111 | /* Note: for a server template, its id is its prefix. |
| 2112 | * This is a temporary id which will be used for server allocations to come |
| 2113 | * after parsing. |
| 2114 | */ |
| 2115 | if (srv) |
| 2116 | newsrv->id = strdup(args[1]); |
| 2117 | else |
| 2118 | newsrv->tmpl_info.prefix = strdup(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2119 | |
| 2120 | /* several ways to check the port component : |
| 2121 | * - IP => port=+0, relative (IPv4 only) |
| 2122 | * - IP: => port=+0, relative |
| 2123 | * - IP:N => port=N, absolute |
| 2124 | * - IP:+N => port=+N, relative |
| 2125 | * - IP:-N => port=-N, relative |
| 2126 | */ |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2127 | if (!parse_addr) |
| 2128 | goto skip_addr; |
| 2129 | |
Emeric Brun | 33f0a73 | 2020-07-21 16:54:36 +0200 | [diff] [blame] | 2130 | 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] | 2131 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2132 | 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] | 2133 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2134 | goto out; |
| 2135 | } |
| 2136 | |
| 2137 | proto = protocol_by_family(sk->ss_family); |
Willy Tarreau | 9698f4b | 2017-01-06 18:42:57 +0100 | [diff] [blame] | 2138 | if (!fqdn && (!proto || !proto->connect)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2139 | 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] | 2140 | file, linenum, args[0], args[1]); |
| 2141 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2142 | goto out; |
| 2143 | } |
| 2144 | |
| 2145 | if (!port1 || !port2) { |
| 2146 | /* no port specified, +offset, -offset */ |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 2147 | newsrv->flags |= SRV_F_MAPPORTS; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2148 | } |
| 2149 | else if (port1 != port2) { |
| 2150 | /* port range */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2151 | 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] | 2152 | file, linenum, args[0], args[1], args[2]); |
| 2153 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2154 | goto out; |
| 2155 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2156 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2157 | /* save hostname and create associated name resolution */ |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2158 | if (fqdn) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2159 | if (fqdn[0] == '_') { /* SRV record */ |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2160 | /* Check if a SRV request already exists, and if not, create it */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2161 | if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL) |
| 2162 | newsrv->srvrq = new_dns_srvrq(newsrv, fqdn); |
| 2163 | if (newsrv->srvrq == NULL) { |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2164 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2165 | goto out; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2169 | 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] | 2170 | file, linenum, newsrv->id); |
| 2171 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2172 | goto out; |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2173 | } |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2174 | } |
| 2175 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2176 | newsrv->addr = *sk; |
Willy Tarreau | 6ecb10a | 2017-01-06 18:36:06 +0100 | [diff] [blame] | 2177 | newsrv->svc_port = port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2178 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2179 | if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2180 | 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] | 2181 | file, linenum, newsrv->addr.ss_family, args[cur_arg]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2182 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2183 | goto out; |
| 2184 | } |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 2185 | |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2186 | cur_arg++; |
| 2187 | skip_addr: |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2188 | /* Copy default server settings to new server settings. */ |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2189 | srv_settings_cpy(newsrv, &curproxy->defsrv, 0); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2190 | HA_SPIN_INIT(&newsrv->lock); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2191 | } else { |
| 2192 | newsrv = &curproxy->defsrv; |
| 2193 | cur_arg = 1; |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 2194 | newsrv->dns_opts.family_prio = AF_INET6; |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 2195 | newsrv->dns_opts.accept_duplicate_ip = 0; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | while (*args[cur_arg]) { |
Frédéric Lécaille | 6e0843c | 2017-03-21 16:39:15 +0100 | [diff] [blame] | 2199 | if (!strcmp(args[cur_arg], "agent-inter")) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2200 | 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] | 2201 | |
| 2202 | if (err == PARSE_TIME_OVER) { |
| 2203 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2204 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2205 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2206 | goto out; |
| 2207 | } |
| 2208 | else if (err == PARSE_TIME_UNDER) { |
| 2209 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2210 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2211 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2212 | goto out; |
| 2213 | } |
| 2214 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2215 | 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] | 2216 | file, linenum, *err, newsrv->id); |
| 2217 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2218 | goto out; |
| 2219 | } |
| 2220 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2221 | 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] | 2222 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2223 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2224 | goto out; |
| 2225 | } |
| 2226 | newsrv->agent.inter = val; |
| 2227 | cur_arg += 2; |
| 2228 | } |
Misiek | ea84933 | 2017-01-09 09:39:51 +0100 | [diff] [blame] | 2229 | else if (!strcmp(args[cur_arg], "agent-addr")) { |
| 2230 | if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2231 | 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] | 2232 | goto out; |
| 2233 | } |
| 2234 | |
| 2235 | cur_arg += 2; |
| 2236 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2237 | else if (!strcmp(args[cur_arg], "agent-port")) { |
| 2238 | global.maxsock++; |
| 2239 | newsrv->agent.port = atol(args[cur_arg + 1]); |
| 2240 | cur_arg += 2; |
| 2241 | } |
James Brown | 55f9ff1 | 2015-10-21 18:19:05 -0700 | [diff] [blame] | 2242 | else if (!strcmp(args[cur_arg], "agent-send")) { |
| 2243 | global.maxsock++; |
| 2244 | free(newsrv->agent.send_string); |
| 2245 | newsrv->agent.send_string_len = strlen(args[cur_arg + 1]); |
| 2246 | newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1); |
| 2247 | memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len); |
| 2248 | cur_arg += 2; |
| 2249 | } |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2250 | else if (!strcmp(args[cur_arg], "init-addr")) { |
| 2251 | char *p, *end; |
| 2252 | int done; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 2253 | struct sockaddr_storage sa; |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2254 | |
| 2255 | newsrv->init_addr_methods = 0; |
| 2256 | memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr)); |
| 2257 | |
| 2258 | for (p = args[cur_arg + 1]; *p; p = end) { |
| 2259 | /* cut on next comma */ |
| 2260 | for (end = p; *end && *end != ','; end++); |
| 2261 | if (*end) |
| 2262 | *(end++) = 0; |
| 2263 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 2264 | memset(&sa, 0, sizeof(sa)); |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2265 | if (!strcmp(p, "libc")) { |
| 2266 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC); |
| 2267 | } |
| 2268 | else if (!strcmp(p, "last")) { |
| 2269 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST); |
| 2270 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 2271 | else if (!strcmp(p, "none")) { |
| 2272 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE); |
| 2273 | } |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 2274 | else if (str2ip2(p, &sa, 0)) { |
| 2275 | if (is_addr(&newsrv->init_addr)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2276 | 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] | 2277 | file, linenum, args[cur_arg], p); |
| 2278 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2279 | goto out; |
| 2280 | } |
| 2281 | newsrv->init_addr = sa; |
| 2282 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP); |
| 2283 | } |
Baptiste Assmann | 2593827 | 2016-09-21 20:26:16 +0200 | [diff] [blame] | 2284 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2285 | 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] | 2286 | file, linenum, args[cur_arg], p); |
| 2287 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2288 | goto out; |
| 2289 | } |
| 2290 | if (!done) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2291 | 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] | 2292 | file, linenum, args[cur_arg], p); |
| 2293 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2294 | goto out; |
| 2295 | } |
| 2296 | } |
| 2297 | cur_arg += 2; |
| 2298 | } |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2299 | else if (!strcmp(args[cur_arg], "resolvers")) { |
Frédéric Lécaille | daa2fe6 | 2017-04-20 12:17:50 +0200 | [diff] [blame] | 2300 | free(newsrv->resolvers_id); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2301 | newsrv->resolvers_id = strdup(args[cur_arg + 1]); |
| 2302 | cur_arg += 2; |
| 2303 | } |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 2304 | else if (!strcmp(args[cur_arg], "resolve-opts")) { |
| 2305 | char *p, *end; |
| 2306 | |
| 2307 | for (p = args[cur_arg + 1]; *p; p = end) { |
| 2308 | /* cut on next comma */ |
| 2309 | for (end = p; *end && *end != ','; end++); |
| 2310 | if (*end) |
| 2311 | *(end++) = 0; |
| 2312 | |
| 2313 | if (!strcmp(p, "allow-dup-ip")) { |
| 2314 | newsrv->dns_opts.accept_duplicate_ip = 1; |
| 2315 | } |
| 2316 | else if (!strcmp(p, "prevent-dup-ip")) { |
| 2317 | newsrv->dns_opts.accept_duplicate_ip = 0; |
| 2318 | } |
| 2319 | else { |
| 2320 | ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n", |
| 2321 | file, linenum, args[cur_arg], p); |
| 2322 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2323 | goto out; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | cur_arg += 2; |
| 2328 | } |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2329 | else if (!strcmp(args[cur_arg], "resolve-prefer")) { |
| 2330 | if (!strcmp(args[cur_arg + 1], "ipv4")) |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 2331 | newsrv->dns_opts.family_prio = AF_INET; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2332 | else if (!strcmp(args[cur_arg + 1], "ipv6")) |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 2333 | newsrv->dns_opts.family_prio = AF_INET6; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2334 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2335 | 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] | 2336 | file, linenum, args[cur_arg]); |
| 2337 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2338 | goto out; |
| 2339 | } |
| 2340 | cur_arg += 2; |
| 2341 | } |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2342 | else if (!strcmp(args[cur_arg], "resolve-net")) { |
| 2343 | char *p, *e; |
| 2344 | unsigned char mask; |
| 2345 | struct dns_options *opt; |
| 2346 | |
| 2347 | if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2348 | ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n", |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2349 | file, linenum, args[cur_arg]); |
| 2350 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2351 | goto out; |
| 2352 | } |
| 2353 | |
| 2354 | opt = &newsrv->dns_opts; |
| 2355 | |
| 2356 | /* Split arguments by comma, and convert it from ipv4 or ipv6 |
| 2357 | * string network in in_addr or in6_addr. |
| 2358 | */ |
| 2359 | p = args[cur_arg + 1]; |
| 2360 | e = p; |
| 2361 | while (*p != '\0') { |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 2362 | /* If no room available, return error. */ |
David Carlier | d10025c | 2016-04-08 10:26:44 +0100 | [diff] [blame] | 2363 | if (opt->pref_net_nb >= SRV_MAX_PREF_NET) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2364 | ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n", |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2365 | file, linenum, args[cur_arg], SRV_MAX_PREF_NET); |
| 2366 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2367 | goto out; |
| 2368 | } |
| 2369 | /* look for end or comma. */ |
| 2370 | while (*e != ',' && *e != '\0') |
| 2371 | e++; |
| 2372 | if (*e == ',') { |
| 2373 | *e = '\0'; |
| 2374 | e++; |
| 2375 | } |
| 2376 | if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4, |
| 2377 | &opt->pref_net[opt->pref_net_nb].mask.in4)) { |
| 2378 | /* Try to convert input string from ipv4 or ipv6 network. */ |
| 2379 | opt->pref_net[opt->pref_net_nb].family = AF_INET; |
| 2380 | } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6, |
| 2381 | &mask)) { |
| 2382 | /* Try to convert input string from ipv6 network. */ |
| 2383 | len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6); |
| 2384 | opt->pref_net[opt->pref_net_nb].family = AF_INET6; |
| 2385 | } else { |
| 2386 | /* All network conversions fail, retrun error. */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2387 | ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n", |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 2388 | file, linenum, args[cur_arg], p); |
| 2389 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2390 | goto out; |
| 2391 | } |
| 2392 | opt->pref_net_nb++; |
| 2393 | p = e; |
| 2394 | } |
| 2395 | |
| 2396 | cur_arg += 2; |
| 2397 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2398 | else if (!strcmp(args[cur_arg], "rise")) { |
| 2399 | if (!*args[cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2400 | ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2401 | file, linenum, args[cur_arg]); |
| 2402 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2403 | goto out; |
| 2404 | } |
| 2405 | |
| 2406 | newsrv->check.rise = atol(args[cur_arg + 1]); |
| 2407 | if (newsrv->check.rise <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2408 | ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2409 | file, linenum, args[cur_arg]); |
| 2410 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2411 | goto out; |
| 2412 | } |
| 2413 | |
| 2414 | if (newsrv->check.health) |
| 2415 | newsrv->check.health = newsrv->check.rise; |
| 2416 | cur_arg += 2; |
| 2417 | } |
| 2418 | else if (!strcmp(args[cur_arg], "fall")) { |
| 2419 | newsrv->check.fall = atol(args[cur_arg + 1]); |
| 2420 | |
| 2421 | if (!*args[cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2422 | ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2423 | file, linenum, args[cur_arg]); |
| 2424 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2425 | goto out; |
| 2426 | } |
| 2427 | |
| 2428 | if (newsrv->check.fall <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2429 | ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2430 | file, linenum, args[cur_arg]); |
| 2431 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2432 | goto out; |
| 2433 | } |
| 2434 | |
| 2435 | cur_arg += 2; |
| 2436 | } |
| 2437 | else if (!strcmp(args[cur_arg], "inter")) { |
| 2438 | 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] | 2439 | |
| 2440 | if (err == PARSE_TIME_OVER) { |
| 2441 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2442 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2443 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2444 | goto out; |
| 2445 | } |
| 2446 | else if (err == PARSE_TIME_UNDER) { |
| 2447 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2448 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2449 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2450 | goto out; |
| 2451 | } |
| 2452 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2453 | 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] | 2454 | file, linenum, *err, newsrv->id); |
| 2455 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2456 | goto out; |
| 2457 | } |
| 2458 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2459 | 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] | 2460 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2461 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2462 | goto out; |
| 2463 | } |
| 2464 | newsrv->check.inter = val; |
| 2465 | cur_arg += 2; |
| 2466 | } |
| 2467 | else if (!strcmp(args[cur_arg], "fastinter")) { |
| 2468 | 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] | 2469 | |
| 2470 | if (err == PARSE_TIME_OVER) { |
| 2471 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2472 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2473 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2474 | goto out; |
| 2475 | } |
| 2476 | else if (err == PARSE_TIME_UNDER) { |
| 2477 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2478 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2479 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2480 | goto out; |
| 2481 | } |
| 2482 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2483 | 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] | 2484 | file, linenum, *err, newsrv->id); |
| 2485 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2486 | goto out; |
| 2487 | } |
| 2488 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2489 | 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] | 2490 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2491 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2492 | goto out; |
| 2493 | } |
| 2494 | newsrv->check.fastinter = val; |
| 2495 | cur_arg += 2; |
| 2496 | } |
| 2497 | else if (!strcmp(args[cur_arg], "downinter")) { |
| 2498 | 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] | 2499 | |
| 2500 | if (err == PARSE_TIME_OVER) { |
| 2501 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2502 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2503 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2504 | goto out; |
| 2505 | } |
| 2506 | else if (err == PARSE_TIME_UNDER) { |
| 2507 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2508 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2509 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2510 | goto out; |
| 2511 | } |
| 2512 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2513 | 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] | 2514 | file, linenum, *err, newsrv->id); |
| 2515 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2516 | goto out; |
| 2517 | } |
| 2518 | if (val <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2519 | 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] | 2520 | file, linenum, val, args[cur_arg], newsrv->id); |
| 2521 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2522 | goto out; |
| 2523 | } |
| 2524 | newsrv->check.downinter = val; |
| 2525 | cur_arg += 2; |
| 2526 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2527 | else if (!strcmp(args[cur_arg], "port")) { |
| 2528 | newsrv->check.port = atol(args[cur_arg + 1]); |
Baptiste Assmann | 6b453f1 | 2016-08-11 23:12:18 +0200 | [diff] [blame] | 2529 | newsrv->flags |= SRV_F_CHECKPORT; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2530 | cur_arg += 2; |
| 2531 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2532 | else if (!strcmp(args[cur_arg], "weight")) { |
| 2533 | int w; |
| 2534 | w = atol(args[cur_arg + 1]); |
| 2535 | if (w < 0 || w > SRV_UWGHT_MAX) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2536 | 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] | 2537 | file, linenum, newsrv->id, SRV_UWGHT_MAX, w); |
| 2538 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2539 | goto out; |
| 2540 | } |
| 2541 | newsrv->uweight = newsrv->iweight = w; |
| 2542 | cur_arg += 2; |
| 2543 | } |
| 2544 | else if (!strcmp(args[cur_arg], "minconn")) { |
| 2545 | newsrv->minconn = atol(args[cur_arg + 1]); |
| 2546 | cur_arg += 2; |
| 2547 | } |
| 2548 | else if (!strcmp(args[cur_arg], "maxconn")) { |
| 2549 | newsrv->maxconn = atol(args[cur_arg + 1]); |
| 2550 | cur_arg += 2; |
| 2551 | } |
| 2552 | else if (!strcmp(args[cur_arg], "maxqueue")) { |
| 2553 | newsrv->maxqueue = atol(args[cur_arg + 1]); |
| 2554 | cur_arg += 2; |
| 2555 | } |
| 2556 | else if (!strcmp(args[cur_arg], "slowstart")) { |
| 2557 | /* slowstart is stored in seconds */ |
| 2558 | 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] | 2559 | |
| 2560 | if (err == PARSE_TIME_OVER) { |
| 2561 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n", |
| 2562 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2563 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2564 | goto out; |
| 2565 | } |
| 2566 | else if (err == PARSE_TIME_UNDER) { |
| 2567 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n", |
| 2568 | file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id); |
| 2569 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2570 | goto out; |
| 2571 | } |
| 2572 | else if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2573 | 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] | 2574 | file, linenum, *err, newsrv->id); |
| 2575 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2576 | goto out; |
| 2577 | } |
| 2578 | newsrv->slowstart = (val + 999) / 1000; |
| 2579 | cur_arg += 2; |
| 2580 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2581 | else if (!strcmp(args[cur_arg], "on-error")) { |
| 2582 | if (!strcmp(args[cur_arg + 1], "fastinter")) |
| 2583 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 2584 | else if (!strcmp(args[cur_arg + 1], "fail-check")) |
| 2585 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 2586 | else if (!strcmp(args[cur_arg + 1], "sudden-death")) |
| 2587 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 2588 | else if (!strcmp(args[cur_arg + 1], "mark-down")) |
| 2589 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 2590 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2591 | ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', " |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2592 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n", |
| 2593 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 2594 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2595 | goto out; |
| 2596 | } |
| 2597 | |
| 2598 | cur_arg += 2; |
| 2599 | } |
| 2600 | else if (!strcmp(args[cur_arg], "on-marked-down")) { |
| 2601 | if (!strcmp(args[cur_arg + 1], "shutdown-sessions")) |
| 2602 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 2603 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2604 | 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] | 2605 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 2606 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2607 | goto out; |
| 2608 | } |
| 2609 | |
| 2610 | cur_arg += 2; |
| 2611 | } |
| 2612 | else if (!strcmp(args[cur_arg], "on-marked-up")) { |
| 2613 | if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions")) |
| 2614 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 2615 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2616 | 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] | 2617 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 2618 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2619 | goto out; |
| 2620 | } |
| 2621 | |
| 2622 | cur_arg += 2; |
| 2623 | } |
| 2624 | else if (!strcmp(args[cur_arg], "error-limit")) { |
| 2625 | if (!*args[cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2626 | ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2627 | file, linenum, args[cur_arg]); |
| 2628 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2629 | goto out; |
| 2630 | } |
| 2631 | |
| 2632 | newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]); |
| 2633 | |
| 2634 | if (newsrv->consecutive_errors_limit <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2635 | ha_alert("parsing [%s:%d]: %s has to be > 0.\n", |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2636 | file, linenum, args[cur_arg]); |
| 2637 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2638 | goto out; |
| 2639 | } |
| 2640 | cur_arg += 2; |
| 2641 | } |
Frédéric Lécaille | 8d083ed | 2017-04-14 15:19:56 +0200 | [diff] [blame] | 2642 | 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] | 2643 | 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] | 2644 | file, linenum, "usesrc", "source"); |
| 2645 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2646 | goto out; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 2647 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2648 | else { |
| 2649 | static int srv_dumped; |
| 2650 | struct srv_kw *kw; |
| 2651 | char *err; |
| 2652 | |
| 2653 | kw = srv_find_kw(args[cur_arg]); |
| 2654 | if (kw) { |
| 2655 | char *err = NULL; |
| 2656 | int code; |
| 2657 | |
| 2658 | if (!kw->parse) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2659 | 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] | 2660 | file, linenum, args[0], args[1], args[cur_arg]); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2661 | if (kw->skip != -1) |
| 2662 | cur_arg += 1 + kw->skip ; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2663 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2664 | goto out; |
| 2665 | } |
| 2666 | |
| 2667 | if (defsrv && !kw->default_ok) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2668 | 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] | 2669 | file, linenum, args[0], args[1], args[cur_arg]); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2670 | if (kw->skip != -1) |
| 2671 | cur_arg += 1 + kw->skip ; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2672 | err_code |= ERR_ALERT; |
| 2673 | continue; |
| 2674 | } |
| 2675 | |
| 2676 | code = kw->parse(args, &cur_arg, curproxy, newsrv, &err); |
| 2677 | err_code |= code; |
| 2678 | |
| 2679 | if (code) { |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2680 | display_parser_err(file, linenum, args, cur_arg, &err); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2681 | if (code & ERR_FATAL) { |
| 2682 | free(err); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2683 | if (kw->skip != -1) |
| 2684 | cur_arg += 1 + kw->skip; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2685 | goto out; |
| 2686 | } |
| 2687 | } |
| 2688 | free(err); |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 2689 | if (kw->skip != -1) |
| 2690 | cur_arg += 1 + kw->skip; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2691 | continue; |
| 2692 | } |
| 2693 | |
| 2694 | err = NULL; |
| 2695 | if (!srv_dumped) { |
| 2696 | srv_dump_kws(&err); |
| 2697 | indent_msg(&err, 4); |
| 2698 | srv_dumped = 1; |
| 2699 | } |
| 2700 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2701 | 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] | 2702 | file, linenum, args[0], args[1], args[cur_arg], |
| 2703 | err ? " Registered keywords :" : "", err ? err : ""); |
| 2704 | free(err); |
| 2705 | |
| 2706 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2707 | goto out; |
| 2708 | } |
| 2709 | } |
| 2710 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2711 | if (!defsrv) |
| 2712 | err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy); |
| 2713 | if (err_code & ERR_FATAL) |
| 2714 | goto out; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2715 | if (srv_tmpl) |
| 2716 | server_template_init(newsrv, curproxy); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2717 | } |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2718 | free(fqdn); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2719 | return 0; |
| 2720 | |
| 2721 | out: |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2722 | free(fqdn); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2723 | free(errmsg); |
| 2724 | return err_code; |
| 2725 | } |
| 2726 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 2727 | /* Returns a pointer to the first server matching either id <id>. |
| 2728 | * NULL is returned if no match is found. |
| 2729 | * the lookup is performed in the backend <bk> |
| 2730 | */ |
| 2731 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 2732 | { |
| 2733 | struct eb32_node *eb32; |
| 2734 | struct server *curserver; |
| 2735 | |
| 2736 | if (!bk || (id ==0)) |
| 2737 | return NULL; |
| 2738 | |
| 2739 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 2740 | if (!(bk->cap & PR_CAP_BE)) |
| 2741 | return NULL; |
| 2742 | |
| 2743 | curserver = NULL; |
| 2744 | |
| 2745 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 2746 | if (eb32) |
| 2747 | curserver = container_of(eb32, struct server, conf.id); |
| 2748 | |
| 2749 | return curserver; |
| 2750 | } |
| 2751 | |
| 2752 | /* Returns a pointer to the first server matching either name <name>, or id |
| 2753 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 2754 | * the lookup is performed in the backend <bk> |
| 2755 | */ |
| 2756 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 2757 | { |
| 2758 | struct server *curserver; |
| 2759 | |
| 2760 | if (!bk || !name) |
| 2761 | return NULL; |
| 2762 | |
| 2763 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 2764 | if (!(bk->cap & PR_CAP_BE)) |
| 2765 | return NULL; |
| 2766 | |
| 2767 | curserver = NULL; |
| 2768 | if (*name == '#') { |
| 2769 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 2770 | if (curserver) |
| 2771 | return curserver; |
| 2772 | } |
| 2773 | else { |
| 2774 | curserver = bk->srv; |
| 2775 | |
| 2776 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 2777 | curserver = curserver->next; |
| 2778 | |
| 2779 | if (curserver) |
| 2780 | return curserver; |
| 2781 | } |
| 2782 | |
| 2783 | return NULL; |
| 2784 | } |
| 2785 | |
| 2786 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 2787 | { |
| 2788 | struct server *byname; |
| 2789 | struct server *byid; |
| 2790 | |
| 2791 | if (!name && !id) |
| 2792 | return NULL; |
| 2793 | |
| 2794 | if (diff) |
| 2795 | *diff = 0; |
| 2796 | |
| 2797 | byname = byid = NULL; |
| 2798 | |
| 2799 | if (name) { |
| 2800 | byname = server_find_by_name(bk, name); |
| 2801 | if (byname && (!id || byname->puid == id)) |
| 2802 | return byname; |
| 2803 | } |
| 2804 | |
| 2805 | /* remaining possibilities : |
| 2806 | * - name not set |
| 2807 | * - name set but not found |
| 2808 | * - name found but ID doesn't match |
| 2809 | */ |
| 2810 | if (id) { |
| 2811 | byid = server_find_by_id(bk, id); |
| 2812 | if (byid) { |
| 2813 | if (byname) { |
| 2814 | /* use id only if forced by configuration */ |
| 2815 | if (byid->flags & SRV_F_FORCED_ID) { |
| 2816 | if (diff) |
| 2817 | *diff |= 2; |
| 2818 | return byid; |
| 2819 | } |
| 2820 | else { |
| 2821 | if (diff) |
| 2822 | *diff |= 1; |
| 2823 | return byname; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | /* remaining possibilities: |
| 2828 | * - name not set |
| 2829 | * - name set but not found |
| 2830 | */ |
| 2831 | if (name && diff) |
| 2832 | *diff |= 2; |
| 2833 | return byid; |
| 2834 | } |
| 2835 | |
| 2836 | /* id bot found */ |
| 2837 | if (byname) { |
| 2838 | if (diff) |
| 2839 | *diff |= 1; |
| 2840 | return byname; |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | return NULL; |
| 2845 | } |
| 2846 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 2847 | /* Update a server state using the parameters available in the params list. |
| 2848 | * |
| 2849 | * Grabs the server lock during operation. |
| 2850 | */ |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2851 | static void srv_update_state(struct server *srv, int version, char **params) |
| 2852 | { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2853 | char *p; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2854 | struct buffer *msg; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2855 | |
| 2856 | /* fields since version 1 |
| 2857 | * and common to all other upcoming versions |
| 2858 | */ |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2859 | enum srv_state srv_op_state; |
| 2860 | enum srv_admin srv_admin_state; |
| 2861 | unsigned srv_uweight, srv_iweight; |
| 2862 | unsigned long srv_last_time_change; |
| 2863 | short srv_check_status; |
| 2864 | enum chk_result srv_check_result; |
| 2865 | int srv_check_health; |
| 2866 | int srv_check_state, srv_agent_state; |
| 2867 | int bk_f_forced_id; |
| 2868 | int srv_f_forced_id; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2869 | int fqdn_set_by_cli; |
| 2870 | const char *fqdn; |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 2871 | const char *port_str; |
| 2872 | unsigned int port; |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 2873 | char *srvrecord; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2874 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2875 | fqdn = NULL; |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 2876 | port = 0; |
Willy Tarreau | 31138fa | 2015-09-29 18:38:47 +0200 | [diff] [blame] | 2877 | msg = get_trash_chunk(); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2878 | switch (version) { |
| 2879 | case 1: |
| 2880 | /* |
| 2881 | * now we can proceed with server's state update: |
| 2882 | * srv_addr: params[0] |
| 2883 | * srv_op_state: params[1] |
| 2884 | * srv_admin_state: params[2] |
| 2885 | * srv_uweight: params[3] |
| 2886 | * srv_iweight: params[4] |
| 2887 | * srv_last_time_change: params[5] |
| 2888 | * srv_check_status: params[6] |
| 2889 | * srv_check_result: params[7] |
| 2890 | * srv_check_health: params[8] |
| 2891 | * srv_check_state: params[9] |
| 2892 | * srv_agent_state: params[10] |
| 2893 | * bk_f_forced_id: params[11] |
| 2894 | * srv_f_forced_id: params[12] |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2895 | * srv_fqdn: params[13] |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 2896 | * srv_port: params[14] |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 2897 | * srvrecord: params[15] |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2898 | */ |
| 2899 | |
| 2900 | /* validating srv_op_state */ |
| 2901 | p = NULL; |
| 2902 | errno = 0; |
| 2903 | srv_op_state = strtol(params[1], &p, 10); |
| 2904 | if ((p == params[1]) || errno == EINVAL || errno == ERANGE || |
| 2905 | (srv_op_state != SRV_ST_STOPPED && |
| 2906 | srv_op_state != SRV_ST_STARTING && |
| 2907 | srv_op_state != SRV_ST_RUNNING && |
| 2908 | srv_op_state != SRV_ST_STOPPING)) { |
| 2909 | chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]); |
| 2910 | } |
| 2911 | |
| 2912 | /* validating srv_admin_state */ |
| 2913 | p = NULL; |
| 2914 | errno = 0; |
| 2915 | srv_admin_state = strtol(params[2], &p, 10); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2916 | fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 2917 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2918 | /* inherited statuses will be recomputed later. |
| 2919 | * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn). |
| 2920 | */ |
Christopher Faulet | f02bcb9 | 2021-02-12 17:36:08 +0100 | [diff] [blame] | 2921 | 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] | 2922 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2923 | if ((p == params[2]) || errno == EINVAL || errno == ERANGE || |
| 2924 | (srv_admin_state != 0 && |
| 2925 | srv_admin_state != SRV_ADMF_FMAINT && |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2926 | srv_admin_state != SRV_ADMF_CMAINT && |
| 2927 | srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) && |
Willy Tarreau | e1bde14 | 2016-11-03 18:33:25 +0100 | [diff] [blame] | 2928 | srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) && |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 2929 | srv_admin_state != SRV_ADMF_FDRAIN)) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 2930 | chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]); |
| 2931 | } |
| 2932 | |
| 2933 | /* validating srv_uweight */ |
| 2934 | p = NULL; |
| 2935 | errno = 0; |
| 2936 | srv_uweight = strtol(params[3], &p, 10); |
Willy Tarreau | e1aebb2 | 2015-09-29 18:32:57 +0200 | [diff] [blame] | 2937 | 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] | 2938 | chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]); |
| 2939 | |
| 2940 | /* validating srv_iweight */ |
| 2941 | p = NULL; |
| 2942 | errno = 0; |
| 2943 | srv_iweight = strtol(params[4], &p, 10); |
Willy Tarreau | e1aebb2 | 2015-09-29 18:32:57 +0200 | [diff] [blame] | 2944 | 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] | 2945 | chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]); |
| 2946 | |
| 2947 | /* validating srv_last_time_change */ |
| 2948 | p = NULL; |
| 2949 | errno = 0; |
| 2950 | srv_last_time_change = strtol(params[5], &p, 10); |
| 2951 | if ((p == params[5]) || errno == EINVAL || errno == ERANGE) |
| 2952 | chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]); |
| 2953 | |
| 2954 | /* validating srv_check_status */ |
| 2955 | p = NULL; |
| 2956 | errno = 0; |
| 2957 | srv_check_status = strtol(params[6], &p, 10); |
| 2958 | if (p == params[6] || errno == EINVAL || errno == ERANGE || |
| 2959 | (srv_check_status >= HCHK_STATUS_SIZE)) |
| 2960 | chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]); |
| 2961 | |
| 2962 | /* validating srv_check_result */ |
| 2963 | p = NULL; |
| 2964 | errno = 0; |
| 2965 | srv_check_result = strtol(params[7], &p, 10); |
| 2966 | if ((p == params[7]) || errno == EINVAL || errno == ERANGE || |
| 2967 | (srv_check_result != CHK_RES_UNKNOWN && |
| 2968 | srv_check_result != CHK_RES_NEUTRAL && |
| 2969 | srv_check_result != CHK_RES_FAILED && |
| 2970 | srv_check_result != CHK_RES_PASSED && |
| 2971 | srv_check_result != CHK_RES_CONDPASS)) { |
| 2972 | chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]); |
| 2973 | } |
| 2974 | |
| 2975 | /* validating srv_check_health */ |
| 2976 | p = NULL; |
| 2977 | errno = 0; |
| 2978 | srv_check_health = strtol(params[8], &p, 10); |
| 2979 | if (p == params[8] || errno == EINVAL || errno == ERANGE) |
| 2980 | chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]); |
| 2981 | |
| 2982 | /* validating srv_check_state */ |
| 2983 | p = NULL; |
| 2984 | errno = 0; |
| 2985 | srv_check_state = strtol(params[9], &p, 10); |
| 2986 | if (p == params[9] || errno == EINVAL || errno == ERANGE || |
| 2987 | (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT))) |
| 2988 | chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]); |
| 2989 | |
| 2990 | /* validating srv_agent_state */ |
| 2991 | p = NULL; |
| 2992 | errno = 0; |
| 2993 | srv_agent_state = strtol(params[10], &p, 10); |
| 2994 | if (p == params[10] || errno == EINVAL || errno == ERANGE || |
| 2995 | (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT))) |
| 2996 | chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]); |
| 2997 | |
| 2998 | /* validating bk_f_forced_id */ |
| 2999 | p = NULL; |
| 3000 | errno = 0; |
| 3001 | bk_f_forced_id = strtol(params[11], &p, 10); |
| 3002 | if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1))) |
| 3003 | chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]); |
| 3004 | |
| 3005 | /* validating srv_f_forced_id */ |
| 3006 | p = NULL; |
| 3007 | errno = 0; |
| 3008 | srv_f_forced_id = strtol(params[12], &p, 10); |
| 3009 | if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1))) |
| 3010 | chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]); |
| 3011 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3012 | /* validating srv_fqdn */ |
| 3013 | fqdn = params[13]; |
| 3014 | if (fqdn && *fqdn == '-') |
| 3015 | fqdn = NULL; |
| 3016 | if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) { |
| 3017 | chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]); |
| 3018 | fqdn = NULL; |
| 3019 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3020 | |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 3021 | port_str = params[14]; |
| 3022 | if (port_str) { |
| 3023 | port = strl2uic(port_str, strlen(port_str)); |
| 3024 | if (port > USHRT_MAX) { |
| 3025 | chunk_appendf(msg, ", invalid srv_port value '%s'", port_str); |
| 3026 | port_str = NULL; |
| 3027 | } |
| 3028 | } |
| 3029 | |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 3030 | /* SRV record |
| 3031 | * NOTE: in HAProxy, SRV records must start with an underscore '_' |
| 3032 | */ |
| 3033 | srvrecord = params[15]; |
| 3034 | if (srvrecord && *srvrecord != '_') |
| 3035 | srvrecord = NULL; |
| 3036 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3037 | /* don't apply anything if one error has been detected */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3038 | if (msg->data) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3039 | goto out; |
| 3040 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3041 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3042 | /* recover operational state and apply it to this server |
| 3043 | * and all servers tracking this one */ |
Jérôme Magnin | f57afa4 | 2019-01-20 11:27:40 +0100 | [diff] [blame] | 3044 | srv->check.health = srv_check_health; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3045 | switch (srv_op_state) { |
| 3046 | case SRV_ST_STOPPED: |
| 3047 | srv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3048 | srv_set_stopped(srv, "changed from server-state after a reload", NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3049 | break; |
| 3050 | case SRV_ST_STARTING: |
Jérôme Magnin | f57afa4 | 2019-01-20 11:27:40 +0100 | [diff] [blame] | 3051 | /* If rise == 1 there is no STARTING state, let's switch to |
| 3052 | * RUNNING |
| 3053 | */ |
| 3054 | if (srv->check.rise == 1) { |
| 3055 | srv->check.health = srv->check.rise + srv->check.fall - 1; |
| 3056 | srv_set_running(srv, "", NULL); |
| 3057 | break; |
| 3058 | } |
| 3059 | if (srv->check.health < 1 || srv->check.health >= srv->check.rise) |
| 3060 | srv->check.health = srv->check.rise - 1; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3061 | srv->next_state = srv_op_state; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3062 | break; |
| 3063 | case SRV_ST_STOPPING: |
Jérôme Magnin | f57afa4 | 2019-01-20 11:27:40 +0100 | [diff] [blame] | 3064 | /* If fall == 1 there is no STOPPING state, let's switch to |
| 3065 | * STOPPED |
| 3066 | */ |
| 3067 | if (srv->check.fall == 1) { |
| 3068 | srv->check.health = 0; |
| 3069 | srv_set_stopped(srv, "changed from server-state after a reload", NULL); |
| 3070 | break; |
| 3071 | } |
| 3072 | if (srv->check.health < srv->check.rise || |
| 3073 | srv->check.health > srv->check.rise + srv->check.fall - 2) |
| 3074 | srv->check.health = srv->check.rise; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3075 | srv_set_stopping(srv, "changed from server-state after a reload", NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3076 | break; |
| 3077 | case SRV_ST_RUNNING: |
| 3078 | srv->check.health = srv->check.rise + srv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3079 | srv_set_running(srv, "", NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3080 | break; |
| 3081 | } |
| 3082 | |
| 3083 | /* When applying server state, the following rules apply: |
| 3084 | * - in case of a configuration change, we apply the setting from the new |
| 3085 | * configuration, regardless of old running state |
| 3086 | * - if no configuration change, we apply old running state only if old running |
| 3087 | * state is different from new configuration state |
| 3088 | */ |
| 3089 | /* configuration has changed */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3090 | if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) { |
| 3091 | if (srv->next_admin & SRV_ADMF_CMAINT) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3092 | srv_adm_set_maint(srv); |
| 3093 | else |
| 3094 | srv_adm_set_ready(srv); |
| 3095 | } |
| 3096 | /* configuration is the same, let's compate old running state and new conf state */ |
| 3097 | else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3098 | 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] | 3099 | srv_adm_set_maint(srv); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3100 | 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] | 3101 | srv_adm_set_ready(srv); |
| 3102 | } |
| 3103 | /* apply drain mode if server is currently enabled */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3104 | 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] | 3105 | /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0 |
Willy Tarreau | 22cace2 | 2016-11-03 18:19:49 +0100 | [diff] [blame] | 3106 | * (srv->iweight is the weight set up in configuration). |
| 3107 | * There are two possible reasons for FDRAIN to have been present : |
| 3108 | * - previous config weight was zero |
| 3109 | * - "set server b/s drain" was sent to the CLI |
| 3110 | * |
| 3111 | * In the first case, we simply want to drop this drain state |
| 3112 | * if the new weight is not zero anymore, meaning the administrator |
| 3113 | * has intentionally turned the weight back to a positive value to |
| 3114 | * enable the server again after an operation. In the second case, |
| 3115 | * the drain state was forced on the CLI regardless of the config's |
| 3116 | * weight so we don't want a change to the config weight to lose this |
| 3117 | * status. What this means is : |
| 3118 | * - if previous weight was 0 and new one is >0, drop the DRAIN state. |
| 3119 | * - if the previous weight was >0, keep it. |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3120 | */ |
Willy Tarreau | 22cace2 | 2016-11-03 18:19:49 +0100 | [diff] [blame] | 3121 | if (srv_iweight > 0 || srv->iweight == 0) |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3122 | srv_adm_set_drain(srv); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | srv->last_change = date.tv_sec - srv_last_time_change; |
| 3126 | srv->check.status = srv_check_status; |
| 3127 | srv->check.result = srv_check_result; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3128 | |
| 3129 | /* Only case we want to apply is removing ENABLED flag which could have been |
| 3130 | * done by the "disable health" command over the stats socket |
| 3131 | */ |
| 3132 | if ((srv->check.state & CHK_ST_CONFIGURED) && |
| 3133 | (srv_check_state & CHK_ST_CONFIGURED) && |
| 3134 | !(srv_check_state & CHK_ST_ENABLED)) |
| 3135 | srv->check.state &= ~CHK_ST_ENABLED; |
| 3136 | |
| 3137 | /* Only case we want to apply is removing ENABLED flag which could have been |
| 3138 | * done by the "disable agent" command over the stats socket |
| 3139 | */ |
| 3140 | if ((srv->agent.state & CHK_ST_CONFIGURED) && |
| 3141 | (srv_agent_state & CHK_ST_CONFIGURED) && |
| 3142 | !(srv_agent_state & CHK_ST_ENABLED)) |
| 3143 | srv->agent.state &= ~CHK_ST_ENABLED; |
| 3144 | |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 3145 | /* We want to apply the previous 'running' weight (srv_uweight) only if there |
| 3146 | * 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] | 3147 | * |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 3148 | * It means that a configuration file change has precedence over a unix socket change |
| 3149 | * for server's weight |
| 3150 | * |
| 3151 | * by default, HAProxy applies the following weight when parsing the configuration |
| 3152 | * srv->uweight = srv->iweight |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3153 | */ |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 3154 | if (srv_iweight == srv->iweight) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3155 | srv->uweight = srv_uweight; |
| 3156 | } |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 3157 | server_recalc_eweight(srv, 1); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3158 | |
Willy Tarreau | e5a6068 | 2016-11-09 14:54:53 +0100 | [diff] [blame] | 3159 | /* load server IP address */ |
Daniel Corbett | 9215ffa | 2018-05-19 19:43:24 -0400 | [diff] [blame] | 3160 | if (strcmp(params[0], "-")) |
| 3161 | srv->lastaddr = strdup(params[0]); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3162 | |
| 3163 | if (fqdn && srv->hostname) { |
| 3164 | if (!strcmp(srv->hostname, fqdn)) { |
| 3165 | /* Here we reset the 'set from stats socket FQDN' flag |
| 3166 | * to support such transitions: |
| 3167 | * Let's say initial FQDN value is foo1 (in configuration file). |
| 3168 | * - FQDN changed from stats socket, from foo1 to foo2 value, |
| 3169 | * - FQDN changed again from file configuration (with the same previous value |
| 3170 | set from stats socket, from foo1 to foo2 value), |
| 3171 | * - reload for any other reason than a FQDN modification, |
| 3172 | * the configuration file FQDN matches the fqdn server state file value. |
| 3173 | * 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] | 3174 | * any further FQDN modification. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3175 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3176 | srv->next_admin &= ~SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3177 | } |
| 3178 | else { |
| 3179 | /* If the FDQN has been changed from stats socket, |
| 3180 | * apply fqdn state file value (which is the value set |
| 3181 | * from stats socket). |
| 3182 | */ |
| 3183 | if (fqdn_set_by_cli) { |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 3184 | srv_set_fqdn(srv, fqdn, 0); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3185 | srv->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | } |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 3189 | /* If all the conditions below are validated, this means |
| 3190 | * we're evaluating a server managed by SRV resolution |
| 3191 | */ |
| 3192 | else if (fqdn && !srv->hostname && srvrecord) { |
| 3193 | int res; |
| 3194 | |
| 3195 | /* we can't apply previous state if SRV record has changed */ |
| 3196 | if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) { |
| 3197 | 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); |
| 3198 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 3199 | goto out; |
| 3200 | } |
| 3201 | |
| 3202 | /* create or find a SRV resolution for this srv record */ |
| 3203 | if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL) |
| 3204 | srv->srvrq = new_dns_srvrq(srv, srvrecord); |
| 3205 | if (srv->srvrq == NULL) { |
| 3206 | chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id); |
| 3207 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 3208 | goto out; |
| 3209 | } |
| 3210 | |
| 3211 | /* prepare DNS resolution for this server */ |
| 3212 | res = srv_prepare_for_resolution(srv, fqdn); |
| 3213 | if (res == -1) { |
| 3214 | chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id); |
| 3215 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 3216 | goto out; |
| 3217 | } |
| 3218 | |
| 3219 | /* configure check.port accordingly */ |
| 3220 | if ((srv->check.state & CHK_ST_CONFIGURED) && |
| 3221 | !(srv->flags & SRV_F_CHECKPORT)) |
| 3222 | srv->check.port = port; |
| 3223 | |
| 3224 | /* Unset SRV_F_MAPPORTS for SRV records. |
| 3225 | * SRV_F_MAPPORTS is unfortunately set by parse_server() |
| 3226 | * because no ports are provided in the configuration file. |
| 3227 | * This is because HAProxy will use the port found into the SRV record. |
| 3228 | */ |
| 3229 | srv->flags &= ~SRV_F_MAPPORTS; |
| 3230 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3231 | |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 3232 | if (port_str) |
| 3233 | srv->svc_port = port; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3234 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Frédéric Lécaille | 3169471 | 2017-08-01 08:47:19 +0200 | [diff] [blame] | 3235 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3236 | break; |
| 3237 | default: |
| 3238 | chunk_appendf(msg, ", version '%d' not supported", version); |
| 3239 | } |
| 3240 | |
| 3241 | out: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3242 | if (msg->data) { |
Baptiste Assmann | 0821bb9 | 2016-01-21 00:20:50 +0100 | [diff] [blame] | 3243 | chunk_appendf(msg, "\n"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3244 | ha_warning("server-state application failed for server '%s/%s'%s", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3245 | srv->proxy->id, srv->id, msg->area); |
Baptiste Assmann | 0821bb9 | 2016-01-21 00:20:50 +0100 | [diff] [blame] | 3246 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | /* This function parses all the proxies and only take care of the backends (since we're looking for server) |
| 3250 | * For each proxy, it does the following: |
| 3251 | * - opens its server state file (either one or local one) |
| 3252 | * - read whole file, line by line |
| 3253 | * - analyse each line to check if it matches our current backend: |
| 3254 | * - backend name matches |
| 3255 | * - backend id matches if id is forced and name doesn't match |
| 3256 | * - if the server pointed by the line is found, then state is applied |
| 3257 | * |
| 3258 | * If the running backend uuid or id differs from the state file, then HAProxy reports |
| 3259 | * a warning. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3260 | * |
| 3261 | * Grabs the server's lock via srv_update_state(). |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3262 | */ |
| 3263 | void apply_server_state(void) |
| 3264 | { |
| 3265 | char *cur, *end; |
| 3266 | char mybuf[SRV_STATE_LINE_MAXLEN]; |
| 3267 | int mybuflen; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3268 | char *params[SRV_STATE_FILE_MAX_FIELDS] = {0}; |
| 3269 | char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0}; |
Baptiste Assmann | 95c2c01 | 2019-06-11 14:51:49 +0200 | [diff] [blame] | 3270 | int arg, srv_arg, version; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3271 | FILE *f; |
| 3272 | char *filepath; |
| 3273 | char globalfilepath[MAXPATHLEN + 1]; |
| 3274 | char localfilepath[MAXPATHLEN + 1]; |
| 3275 | int len, fileopenerr, globalfilepathlen, localfilepathlen; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3276 | struct proxy *curproxy, *bk; |
| 3277 | struct server *srv; |
| 3278 | |
| 3279 | globalfilepathlen = 0; |
| 3280 | /* create the globalfilepath variable */ |
| 3281 | if (global.server_state_file) { |
| 3282 | /* absolute path or no base directory provided */ |
| 3283 | if ((global.server_state_file[0] == '/') || (!global.server_state_base)) { |
| 3284 | len = strlen(global.server_state_file); |
| 3285 | if (len > MAXPATHLEN) { |
| 3286 | globalfilepathlen = 0; |
| 3287 | goto globalfileerror; |
| 3288 | } |
| 3289 | memcpy(globalfilepath, global.server_state_file, len); |
| 3290 | globalfilepath[len] = '\0'; |
| 3291 | globalfilepathlen = len; |
| 3292 | } |
| 3293 | else if (global.server_state_base) { |
| 3294 | len = strlen(global.server_state_base); |
Willy Tarreau | 5dfb6c4 | 2018-10-16 19:26:12 +0200 | [diff] [blame] | 3295 | if (len > MAXPATHLEN) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3296 | globalfilepathlen = 0; |
| 3297 | goto globalfileerror; |
| 3298 | } |
Olivier Houchard | 17f8b90 | 2018-10-16 18:35:01 +0200 | [diff] [blame] | 3299 | memcpy(globalfilepath, global.server_state_base, len); |
Willy Tarreau | 5dfb6c4 | 2018-10-16 19:26:12 +0200 | [diff] [blame] | 3300 | globalfilepath[len] = 0; |
| 3301 | globalfilepathlen = len; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3302 | |
| 3303 | /* append a slash if needed */ |
| 3304 | if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') { |
| 3305 | if (globalfilepathlen + 1 > MAXPATHLEN) { |
| 3306 | globalfilepathlen = 0; |
| 3307 | goto globalfileerror; |
| 3308 | } |
| 3309 | globalfilepath[globalfilepathlen++] = '/'; |
| 3310 | } |
| 3311 | |
| 3312 | len = strlen(global.server_state_file); |
| 3313 | if (globalfilepathlen + len > MAXPATHLEN) { |
| 3314 | globalfilepathlen = 0; |
| 3315 | goto globalfileerror; |
| 3316 | } |
| 3317 | memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len); |
| 3318 | globalfilepathlen += len; |
| 3319 | globalfilepath[globalfilepathlen++] = 0; |
| 3320 | } |
| 3321 | } |
| 3322 | globalfileerror: |
| 3323 | if (globalfilepathlen == 0) |
| 3324 | globalfilepath[0] = '\0'; |
| 3325 | |
| 3326 | /* read servers state from local file */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 3327 | for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3328 | /* servers are only in backends */ |
| 3329 | if (!(curproxy->cap & PR_CAP_BE)) |
| 3330 | continue; |
| 3331 | fileopenerr = 0; |
| 3332 | filepath = NULL; |
| 3333 | |
| 3334 | /* search server state file path and name */ |
| 3335 | switch (curproxy->load_server_state_from_file) { |
| 3336 | /* read servers state from global file */ |
| 3337 | case PR_SRV_STATE_FILE_GLOBAL: |
| 3338 | /* there was an error while generating global server state file path */ |
| 3339 | if (globalfilepathlen == 0) |
| 3340 | continue; |
| 3341 | filepath = globalfilepath; |
| 3342 | fileopenerr = 1; |
| 3343 | break; |
| 3344 | /* this backend has its own file */ |
| 3345 | case PR_SRV_STATE_FILE_LOCAL: |
| 3346 | localfilepathlen = 0; |
| 3347 | localfilepath[0] = '\0'; |
| 3348 | len = 0; |
| 3349 | /* create the localfilepath variable */ |
| 3350 | /* absolute path or no base directory provided */ |
| 3351 | if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) { |
| 3352 | len = strlen(curproxy->server_state_file_name); |
| 3353 | if (len > MAXPATHLEN) { |
| 3354 | localfilepathlen = 0; |
| 3355 | goto localfileerror; |
| 3356 | } |
| 3357 | memcpy(localfilepath, curproxy->server_state_file_name, len); |
| 3358 | localfilepath[len] = '\0'; |
| 3359 | localfilepathlen = len; |
| 3360 | } |
| 3361 | else if (global.server_state_base) { |
| 3362 | len = strlen(global.server_state_base); |
| 3363 | localfilepathlen += len; |
| 3364 | |
| 3365 | if (localfilepathlen > MAXPATHLEN) { |
| 3366 | localfilepathlen = 0; |
| 3367 | goto localfileerror; |
| 3368 | } |
Olivier Houchard | 17f8b90 | 2018-10-16 18:35:01 +0200 | [diff] [blame] | 3369 | memcpy(localfilepath, global.server_state_base, len); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3370 | localfilepath[localfilepathlen] = 0; |
| 3371 | |
| 3372 | /* append a slash if needed */ |
| 3373 | if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') { |
| 3374 | if (localfilepathlen + 1 > MAXPATHLEN) { |
| 3375 | localfilepathlen = 0; |
| 3376 | goto localfileerror; |
| 3377 | } |
| 3378 | localfilepath[localfilepathlen++] = '/'; |
| 3379 | } |
| 3380 | |
| 3381 | len = strlen(curproxy->server_state_file_name); |
| 3382 | if (localfilepathlen + len > MAXPATHLEN) { |
| 3383 | localfilepathlen = 0; |
| 3384 | goto localfileerror; |
| 3385 | } |
| 3386 | memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len); |
| 3387 | localfilepathlen += len; |
| 3388 | localfilepath[localfilepathlen++] = 0; |
| 3389 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3390 | localfileerror: |
| 3391 | if (localfilepathlen == 0) |
Christopher Faulet | 559a6d3 | 2021-02-12 16:31:03 +0100 | [diff] [blame] | 3392 | continue; |
| 3393 | filepath = localfilepath; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3394 | |
| 3395 | break; |
| 3396 | case PR_SRV_STATE_FILE_NONE: |
| 3397 | default: |
| 3398 | continue; |
| 3399 | } |
| 3400 | |
| 3401 | /* preload global state file */ |
| 3402 | errno = 0; |
| 3403 | f = fopen(filepath, "r"); |
| 3404 | if (errno && fileopenerr) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3405 | 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] | 3406 | if (!f) |
| 3407 | continue; |
| 3408 | |
| 3409 | mybuf[0] = '\0'; |
| 3410 | mybuflen = 0; |
| 3411 | version = 0; |
| 3412 | |
| 3413 | /* 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] | 3414 | if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3415 | 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] | 3416 | goto fileclose; |
| 3417 | } |
| 3418 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3419 | cur = mybuf; |
| 3420 | version = atoi(cur); |
| 3421 | if ((version < SRV_STATE_FILE_VERSION_MIN) || |
| 3422 | (version > SRV_STATE_FILE_VERSION_MAX)) |
Dragan Dosen | cf4fb03 | 2015-11-04 23:03:26 +0100 | [diff] [blame] | 3423 | goto fileclose; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3424 | |
| 3425 | while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) { |
| 3426 | int bk_f_forced_id = 0; |
| 3427 | int check_id = 0; |
| 3428 | int check_name = 0; |
| 3429 | |
| 3430 | mybuflen = strlen(mybuf); |
| 3431 | cur = mybuf; |
| 3432 | end = cur + mybuflen; |
| 3433 | |
| 3434 | bk = NULL; |
| 3435 | srv = NULL; |
| 3436 | |
| 3437 | /* we need at least one character */ |
| 3438 | if (mybuflen == 0) |
| 3439 | continue; |
| 3440 | |
| 3441 | /* ignore blank characters at the beginning of the line */ |
| 3442 | while (isspace(*cur)) |
| 3443 | ++cur; |
| 3444 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3445 | /* Ignore empty or commented lines */ |
| 3446 | if (cur == end || *cur == '#') |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3447 | continue; |
| 3448 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3449 | /* truncated lines */ |
| 3450 | if (mybuf[mybuflen - 1] != '\n') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3451 | ha_warning("server-state file '%s': truncated line\n", filepath); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3452 | continue; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | /* Removes trailing '\n' */ |
| 3456 | mybuf[mybuflen - 1] = '\0'; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3457 | |
| 3458 | /* we're now ready to move the line into *srv_params[] */ |
Christopher Faulet | 6656972 | 2021-02-19 16:47:11 +0100 | [diff] [blame] | 3459 | memset(params, 0, SRV_STATE_FILE_MAX_FIELDS * sizeof(*params)); |
| 3460 | memset(srv_params, 0, SRV_STATE_FILE_MAX_FIELDS * sizeof(*srv_params)); |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3461 | |
| 3462 | arg = 0; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3463 | srv_arg = 0; |
| 3464 | while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) { |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3465 | /* Search begining of the current field */ |
| 3466 | while (isspace((unsigned char)*cur)) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3467 | ++cur; |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3468 | if (!*cur) |
| 3469 | goto end_loop; |
| 3470 | } |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3471 | |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3472 | /* |
| 3473 | * v1 |
| 3474 | * srv_addr: params[4] => srv_params[0] |
| 3475 | * srv_op_state: params[5] => srv_params[1] |
| 3476 | * srv_admin_state: params[6] => srv_params[2] |
| 3477 | * srv_uweight: params[7] => srv_params[3] |
| 3478 | * srv_iweight: params[8] => srv_params[4] |
| 3479 | * srv_last_time_change: params[9] => srv_params[5] |
| 3480 | * srv_check_status: params[10] => srv_params[6] |
| 3481 | * srv_check_result: params[11] => srv_params[7] |
| 3482 | * srv_check_health: params[12] => srv_params[8] |
| 3483 | * srv_check_state: params[13] => srv_params[9] |
| 3484 | * srv_agent_state: params[14] => srv_params[10] |
| 3485 | * bk_f_forced_id: params[15] => srv_params[11] |
| 3486 | * srv_f_forced_id: params[16] => srv_params[12] |
| 3487 | * srv_fqdn: params[17] => srv_params[13] |
| 3488 | * srv_port: params[18] => srv_params[14] |
| 3489 | * srvrecord: params[19] => srv_params[15] |
| 3490 | */ |
| 3491 | if (version == 1 && arg >= 4) { |
| 3492 | srv_params[srv_arg] = cur; |
| 3493 | ++srv_arg; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3494 | } |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3495 | params[arg] = cur; |
| 3496 | ++arg; |
| 3497 | |
| 3498 | /* Search end of the current field: first space or \0 */ |
| 3499 | /* Search begining of the current field */ |
| 3500 | while (!isspace((unsigned char)*cur)) { |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3501 | ++cur; |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3502 | if (!*cur) |
| 3503 | goto end_loop; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3504 | } |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3505 | *cur++ = '\0'; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3506 | } |
| 3507 | |
Christopher Faulet | d54cc60 | 2021-02-19 16:57:20 +0100 | [diff] [blame] | 3508 | end_loop: |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3509 | /* if line is incomplete line, then ignore it. |
| 3510 | * otherwise, update useful flags */ |
| 3511 | switch (version) { |
| 3512 | case 1: |
| 3513 | if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) |
| 3514 | continue; |
| 3515 | bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID); |
| 3516 | check_id = (atoi(params[0]) == curproxy->uuid); |
| 3517 | check_name = (strcmp(curproxy->id, params[1]) == 0); |
| 3518 | break; |
| 3519 | } |
| 3520 | |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3521 | bk = curproxy; |
| 3522 | |
| 3523 | /* if backend can't be found, let's continue */ |
| 3524 | if (!check_id && !check_name) |
| 3525 | continue; |
| 3526 | else if (!check_id && check_name) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3527 | 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] | 3528 | send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid); |
| 3529 | } |
| 3530 | else if (check_id && !check_name) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3531 | 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] | 3532 | send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id); |
| 3533 | /* if name doesn't match, we still want to update curproxy if the backend id |
| 3534 | * was forced in previous the previous configuration */ |
| 3535 | if (!bk_f_forced_id) |
| 3536 | continue; |
| 3537 | } |
| 3538 | |
Baptiste Assmann | 95c2c01 | 2019-06-11 14:51:49 +0200 | [diff] [blame] | 3539 | /* look for the server by its name: param[3] */ |
| 3540 | srv = server_find_best_match(bk, params[3], 0, NULL); |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3541 | |
| 3542 | if (!srv) { |
| 3543 | /* if no server found, then warning and continue with next line */ |
Baptiste Assmann | 95c2c01 | 2019-06-11 14:51:49 +0200 | [diff] [blame] | 3544 | ha_warning("can't find server '%s' in backend '%s'\n", |
| 3545 | params[3], params[1]); |
| 3546 | send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n", |
| 3547 | params[3], params[1]); |
Frédéric Lécaille | 0bedb8a | 2017-06-15 14:09:10 +0200 | [diff] [blame] | 3548 | continue; |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | /* now we can proceed with server's state update */ |
| 3552 | srv_update_state(srv, version, srv_params); |
| 3553 | } |
Dragan Dosen | cf4fb03 | 2015-11-04 23:03:26 +0100 | [diff] [blame] | 3554 | fileclose: |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 3555 | fclose(f); |
| 3556 | } |
| 3557 | } |
| 3558 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 3559 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3560 | * update a server's current IP address. |
| 3561 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 3562 | * ip is in network format. |
| 3563 | * updater is a string which contains an information about the requester of the update. |
| 3564 | * updater is used if not NULL. |
| 3565 | * |
| 3566 | * 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] | 3567 | * |
| 3568 | * Must be called with the server lock held. |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3569 | */ |
Thierry Fournier | d35b7a6 | 2016-02-24 08:23:22 +0100 | [diff] [blame] | 3570 | 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] | 3571 | { |
| 3572 | /* generates a log line and a warning on stderr */ |
| 3573 | if (1) { |
| 3574 | /* book enough space for both IPv4 and IPv6 */ |
| 3575 | char oldip[INET6_ADDRSTRLEN]; |
| 3576 | char newip[INET6_ADDRSTRLEN]; |
| 3577 | |
| 3578 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 3579 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 3580 | |
| 3581 | /* copy old IP address in a string */ |
| 3582 | switch (s->addr.ss_family) { |
| 3583 | case AF_INET: |
| 3584 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 3585 | break; |
| 3586 | case AF_INET6: |
| 3587 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 3588 | break; |
| 3589 | }; |
| 3590 | |
| 3591 | /* copy new IP address in a string */ |
| 3592 | switch (ip_sin_family) { |
| 3593 | case AF_INET: |
| 3594 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 3595 | break; |
| 3596 | case AF_INET6: |
| 3597 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 3598 | break; |
| 3599 | }; |
| 3600 | |
| 3601 | /* save log line into a buffer */ |
| 3602 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 3603 | s->proxy->id, s->id, oldip, newip, updater); |
| 3604 | |
| 3605 | /* write the buffer on stderr */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3606 | ha_warning("%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3607 | |
| 3608 | /* send a log */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3609 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | /* save the new IP family */ |
| 3613 | s->addr.ss_family = ip_sin_family; |
| 3614 | /* save the new IP address */ |
| 3615 | switch (ip_sin_family) { |
| 3616 | case AF_INET: |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3617 | memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3618 | break; |
| 3619 | case AF_INET6: |
| 3620 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 3621 | break; |
| 3622 | }; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3623 | srv_set_dyncookie(s); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3624 | |
| 3625 | return 0; |
| 3626 | } |
| 3627 | |
| 3628 | /* |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3629 | * This function update a server's addr and port only for AF_INET and AF_INET6 families. |
| 3630 | * |
| 3631 | * Caller can pass its name through <updater> to get it integrated in the response message |
| 3632 | * returned by the function. |
| 3633 | * |
| 3634 | * The function first does the following, in that order: |
| 3635 | * - validates the new addr and/or port |
| 3636 | * - checks if an update is required (new IP or port is different than current ones) |
| 3637 | * - checks the update is allowed: |
| 3638 | * - don't switch from/to a family other than AF_INET4 and AF_INET6 |
| 3639 | * - allow all changes if no CHECKS are configured |
| 3640 | * - if CHECK is configured: |
| 3641 | * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports |
| 3642 | * - applies required changes to both ADDR and PORT if both 'required' and 'allowed' |
| 3643 | * conditions are met |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3644 | * |
| 3645 | * Must be called with the server lock held. |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3646 | */ |
| 3647 | const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater) |
| 3648 | { |
| 3649 | struct sockaddr_storage sa; |
| 3650 | int ret, port_change_required; |
| 3651 | char current_addr[INET6_ADDRSTRLEN]; |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3652 | uint16_t current_port, new_port; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3653 | struct buffer *msg; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3654 | int changed = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3655 | |
| 3656 | msg = get_trash_chunk(); |
| 3657 | chunk_reset(msg); |
| 3658 | |
| 3659 | if (addr) { |
| 3660 | memset(&sa, 0, sizeof(struct sockaddr_storage)); |
| 3661 | if (str2ip2(addr, &sa, 0) == NULL) { |
| 3662 | chunk_printf(msg, "Invalid addr '%s'", addr); |
| 3663 | goto out; |
| 3664 | } |
| 3665 | |
| 3666 | /* changes are allowed on AF_INET* families only */ |
| 3667 | if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) { |
| 3668 | chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file"); |
| 3669 | goto out; |
| 3670 | } |
| 3671 | |
| 3672 | /* collecting data currently setup */ |
| 3673 | memset(current_addr, '\0', sizeof(current_addr)); |
| 3674 | ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr)); |
| 3675 | /* changes are allowed on AF_INET* families only */ |
| 3676 | if ((ret != AF_INET) && (ret != AF_INET6)) { |
| 3677 | chunk_printf(msg, "Update for the current server address family is only supported through configuration file"); |
| 3678 | goto out; |
| 3679 | } |
| 3680 | |
| 3681 | /* applying ADDR changes if required and allowed |
| 3682 | * ipcmp returns 0 when both ADDR are the same |
| 3683 | */ |
| 3684 | if (ipcmp(&s->addr, &sa) == 0) { |
| 3685 | chunk_appendf(msg, "no need to change the addr"); |
| 3686 | goto port; |
| 3687 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3688 | ipcpy(&sa, &s->addr); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3689 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3690 | |
| 3691 | /* we also need to update check's ADDR only if it uses the server's one */ |
| 3692 | if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3693 | ipcpy(&sa, &s->check.addr); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | /* we also need to update agent ADDR only if it use the server's one */ |
| 3697 | if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3698 | ipcpy(&sa, &s->agent.addr); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | /* update report for caller */ |
| 3702 | chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr); |
| 3703 | } |
| 3704 | |
| 3705 | port: |
| 3706 | if (port) { |
| 3707 | char sign = '\0'; |
| 3708 | char *endptr; |
| 3709 | |
| 3710 | if (addr) |
| 3711 | chunk_appendf(msg, ", "); |
| 3712 | |
| 3713 | /* collecting data currently setup */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3714 | current_port = s->svc_port; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3715 | |
| 3716 | /* check if PORT change is required */ |
| 3717 | port_change_required = 0; |
| 3718 | |
| 3719 | sign = *port; |
Ryabin Sergey | 77ee752 | 2017-01-11 19:39:55 +0400 | [diff] [blame] | 3720 | errno = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3721 | new_port = strtol(port, &endptr, 10); |
| 3722 | if ((errno != 0) || (port == endptr)) { |
| 3723 | chunk_appendf(msg, "problem converting port '%s' to an int", port); |
| 3724 | goto out; |
| 3725 | } |
| 3726 | |
| 3727 | /* check if caller triggers a port mapped or offset */ |
| 3728 | if (sign == '-' || (sign == '+')) { |
| 3729 | /* check if server currently uses port map */ |
| 3730 | if (!(s->flags & SRV_F_MAPPORTS)) { |
| 3731 | /* switch from fixed port to port map mandatorily triggers |
| 3732 | * a port change */ |
| 3733 | port_change_required = 1; |
| 3734 | /* check is configured |
| 3735 | * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port |
| 3736 | * prevent PORT change if check doesn't have it's dedicated port while switching |
| 3737 | * to port mapping */ |
| 3738 | if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) { |
| 3739 | 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."); |
| 3740 | goto out; |
| 3741 | } |
| 3742 | } |
| 3743 | /* we're already using port maps */ |
| 3744 | else { |
| 3745 | port_change_required = current_port != new_port; |
| 3746 | } |
| 3747 | } |
| 3748 | /* fixed port */ |
| 3749 | else { |
| 3750 | port_change_required = current_port != new_port; |
| 3751 | } |
| 3752 | |
| 3753 | /* applying PORT changes if required and update response message */ |
| 3754 | if (port_change_required) { |
| 3755 | /* apply new port */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3756 | s->svc_port = new_port; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3757 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3758 | |
| 3759 | /* prepare message */ |
| 3760 | chunk_appendf(msg, "port changed from '"); |
| 3761 | if (s->flags & SRV_F_MAPPORTS) |
| 3762 | chunk_appendf(msg, "+"); |
| 3763 | chunk_appendf(msg, "%d' to '", current_port); |
| 3764 | |
| 3765 | if (sign == '-') { |
| 3766 | s->flags |= SRV_F_MAPPORTS; |
| 3767 | chunk_appendf(msg, "%c", sign); |
| 3768 | /* just use for result output */ |
| 3769 | new_port = -new_port; |
| 3770 | } |
| 3771 | else if (sign == '+') { |
| 3772 | s->flags |= SRV_F_MAPPORTS; |
| 3773 | chunk_appendf(msg, "%c", sign); |
| 3774 | } |
| 3775 | else { |
| 3776 | s->flags &= ~SRV_F_MAPPORTS; |
| 3777 | } |
| 3778 | |
| 3779 | chunk_appendf(msg, "%d'", new_port); |
| 3780 | |
| 3781 | /* we also need to update health checks port only if it uses server's realport */ |
| 3782 | if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) { |
| 3783 | s->check.port = new_port; |
| 3784 | } |
| 3785 | } |
| 3786 | else { |
| 3787 | chunk_appendf(msg, "no need to change the port"); |
| 3788 | } |
| 3789 | } |
| 3790 | |
| 3791 | out: |
William Dauchy | 00da86b | 2020-06-02 16:03:58 +0200 | [diff] [blame] | 3792 | if (changed) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3793 | srv_set_dyncookie(s); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3794 | if (updater) |
| 3795 | chunk_appendf(msg, " by '%s'", updater); |
| 3796 | chunk_appendf(msg, "\n"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3797 | return msg->area; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | |
| 3801 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3802 | * update server status based on result of name resolution |
| 3803 | * returns: |
| 3804 | * 0 if server status is updated |
| 3805 | * 1 if server status has not changed |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3806 | * |
| 3807 | * Must be called with the server lock held. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3808 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3809 | int snr_update_srv_status(struct server *s, int has_no_ip) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3810 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3811 | struct dns_resolvers *resolvers = s->resolvers; |
| 3812 | struct dns_resolution *resolution = s->dns_requester->resolution; |
| 3813 | int exp; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3814 | |
Jerome Magnin | 7cf3024 | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3815 | /* If resolution is NULL we're dealing with SRV records Additional records */ |
| 3816 | if (resolution == NULL) { |
Baptiste Assmann | c7e33c3 | 2020-08-04 10:57:21 +0200 | [diff] [blame] | 3817 | /* since this server has an IP, it can go back in production */ |
| 3818 | if (has_no_ip == 0) { |
| 3819 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3820 | return 1; |
| 3821 | } |
| 3822 | |
Jerome Magnin | 7cf3024 | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3823 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3824 | return 1; |
| 3825 | |
| 3826 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record"); |
| 3827 | return 0; |
| 3828 | } |
| 3829 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3830 | switch (resolution->status) { |
| 3831 | case RSLV_STATUS_NONE: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3832 | /* status when HAProxy has just (re)started. |
| 3833 | * Nothing to do, since the task is already automatically started */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3834 | break; |
| 3835 | |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3836 | case RSLV_STATUS_VALID: |
| 3837 | /* |
| 3838 | * resume health checks |
| 3839 | * server will be turned back on if health check is safe |
| 3840 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3841 | if (has_no_ip) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3842 | if (s->next_admin & SRV_ADMF_RMAINT) |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3843 | return 1; |
| 3844 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, |
| 3845 | "No IP for server "); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3846 | return 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3847 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3848 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3849 | if (!(s->next_admin & SRV_ADMF_RMAINT)) |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3850 | return 1; |
| 3851 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3852 | chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", |
| 3853 | s->proxy->id, s->id); |
| 3854 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3855 | ha_warning("%s.\n", trash.area); |
| 3856 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3857 | return 0; |
| 3858 | |
| 3859 | case RSLV_STATUS_NX: |
| 3860 | /* stop server if resolution is NX for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3861 | exp = tick_add(resolution->last_valid, resolvers->hold.nx); |
| 3862 | if (!tick_is_expired(exp, now_ms)) |
| 3863 | break; |
| 3864 | |
| 3865 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3866 | return 1; |
| 3867 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status"); |
| 3868 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3869 | |
| 3870 | case RSLV_STATUS_TIMEOUT: |
| 3871 | /* stop server if resolution is TIMEOUT for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3872 | exp = tick_add(resolution->last_valid, resolvers->hold.timeout); |
| 3873 | if (!tick_is_expired(exp, now_ms)) |
| 3874 | break; |
| 3875 | |
| 3876 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3877 | return 1; |
| 3878 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status"); |
| 3879 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3880 | |
| 3881 | case RSLV_STATUS_REFUSED: |
| 3882 | /* stop server if resolution is REFUSED for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3883 | exp = tick_add(resolution->last_valid, resolvers->hold.refused); |
| 3884 | if (!tick_is_expired(exp, now_ms)) |
| 3885 | break; |
| 3886 | |
| 3887 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3888 | return 1; |
| 3889 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status"); |
| 3890 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3891 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3892 | default: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3893 | /* stop server if resolution failed for a long enough period */ |
| 3894 | exp = tick_add(resolution->last_valid, resolvers->hold.other); |
| 3895 | if (!tick_is_expired(exp, now_ms)) |
| 3896 | break; |
| 3897 | |
| 3898 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3899 | return 1; |
| 3900 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error"); |
| 3901 | return 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | return 1; |
| 3905 | } |
| 3906 | |
| 3907 | /* |
| 3908 | * Server Name Resolution valid response callback |
| 3909 | * It expects: |
| 3910 | * - <nameserver>: the name server which answered the valid response |
| 3911 | * - <response>: buffer containing a valid DNS response |
| 3912 | * - <response_len>: size of <response> |
| 3913 | * It performs the following actions: |
| 3914 | * - ignore response if current ip found and server family not met |
| 3915 | * - update with first new ip found if family is met and current IP is not found |
| 3916 | * returns: |
| 3917 | * 0 on error |
| 3918 | * 1 when no error or safe ignore |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3919 | * |
| 3920 | * Must be called with server lock held |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3921 | */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3922 | int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3923 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3924 | struct server *s = NULL; |
| 3925 | struct dns_resolution *resolution = NULL; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3926 | void *serverip, *firstip; |
| 3927 | short server_sin_family, firstip_sin_family; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3928 | int ret; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3929 | struct buffer *chk = get_trash_chunk(); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3930 | int has_no_ip = 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3931 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3932 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3933 | if (!s) |
| 3934 | return 1; |
| 3935 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3936 | resolution = s->dns_requester->resolution; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3937 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3938 | /* initializing variables */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3939 | firstip = NULL; /* pointer to the first valid response found */ |
| 3940 | /* it will be used as the new IP if a change is required */ |
| 3941 | firstip_sin_family = AF_UNSPEC; |
| 3942 | serverip = NULL; /* current server IP address */ |
| 3943 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3944 | /* initializing server IP pointer */ |
| 3945 | server_sin_family = s->addr.ss_family; |
| 3946 | switch (server_sin_family) { |
| 3947 | case AF_INET: |
| 3948 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 3949 | break; |
| 3950 | |
| 3951 | case AF_INET6: |
| 3952 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 3953 | break; |
| 3954 | |
Willy Tarreau | 3acfcd1 | 2017-01-06 19:18:32 +0100 | [diff] [blame] | 3955 | case AF_UNSPEC: |
| 3956 | break; |
| 3957 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3958 | default: |
| 3959 | goto invalid; |
| 3960 | } |
| 3961 | |
Baptiste Assmann | 729c901 | 2017-05-22 15:13:10 +0200 | [diff] [blame] | 3962 | ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts, |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 3963 | serverip, server_sin_family, &firstip, |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3964 | &firstip_sin_family, s); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3965 | |
| 3966 | switch (ret) { |
| 3967 | case DNS_UPD_NO: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3968 | goto update_status; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3969 | |
| 3970 | case DNS_UPD_SRVIP_NOT_FOUND: |
| 3971 | goto save_ip; |
| 3972 | |
| 3973 | case DNS_UPD_CNAME: |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3974 | goto invalid; |
| 3975 | |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 3976 | case DNS_UPD_NO_IP_FOUND: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3977 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3978 | goto update_status; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 3979 | |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3980 | case DNS_UPD_NAME_ERROR: |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3981 | /* update resolution status to OTHER error type */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3982 | resolution->status = RSLV_STATUS_OTHER; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3983 | goto update_status; |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3984 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3985 | default: |
| 3986 | goto invalid; |
| 3987 | |
| 3988 | } |
| 3989 | |
| 3990 | save_ip: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3991 | if (nameserver) { |
| 3992 | nameserver->counters.update++; |
| 3993 | /* save the first ip we found */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3994 | chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3995 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3996 | else |
| 3997 | chunk_printf(chk, "DNS cache"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3998 | update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3999 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4000 | update_status: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4001 | snr_update_srv_status(s, has_no_ip); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4002 | return 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4003 | |
| 4004 | invalid: |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 4005 | if (nameserver) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4006 | nameserver->counters.invalid++; |
| 4007 | goto update_status; |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 4008 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4009 | snr_update_srv_status(s, has_no_ip); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4010 | return 0; |
| 4011 | } |
| 4012 | |
| 4013 | /* |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4014 | * SRV record error management callback |
| 4015 | * returns: |
| 4016 | * 0 on error |
| 4017 | * 1 when no error or safe ignore |
| 4018 | * |
| 4019 | * Grabs the server's lock. |
| 4020 | */ |
| 4021 | int srvrq_resolution_error_cb(struct dns_requester *requester, int error_code) |
| 4022 | { |
| 4023 | struct server *s; |
| 4024 | struct dns_srvrq *srvrq; |
| 4025 | struct dns_resolution *res; |
| 4026 | struct dns_resolvers *resolvers; |
| 4027 | int exp; |
| 4028 | |
| 4029 | /* SRV records */ |
| 4030 | srvrq = objt_dns_srvrq(requester->owner); |
| 4031 | if (!srvrq) |
| 4032 | return 1; |
| 4033 | |
| 4034 | resolvers = srvrq->resolvers; |
| 4035 | res = requester->resolution; |
| 4036 | |
| 4037 | switch (res->status) { |
| 4038 | |
| 4039 | case RSLV_STATUS_NX: |
| 4040 | /* stop server if resolution is NX for a long enough period */ |
| 4041 | exp = tick_add(res->last_valid, resolvers->hold.nx); |
| 4042 | if (!tick_is_expired(exp, now_ms)) |
| 4043 | return 1; |
| 4044 | break; |
| 4045 | |
| 4046 | case RSLV_STATUS_TIMEOUT: |
| 4047 | /* stop server if resolution is TIMEOUT for a long enough period */ |
| 4048 | exp = tick_add(res->last_valid, resolvers->hold.timeout); |
| 4049 | if (!tick_is_expired(exp, now_ms)) |
| 4050 | return 1; |
| 4051 | break; |
| 4052 | |
| 4053 | case RSLV_STATUS_REFUSED: |
| 4054 | /* stop server if resolution is REFUSED for a long enough period */ |
| 4055 | exp = tick_add(res->last_valid, resolvers->hold.refused); |
| 4056 | if (!tick_is_expired(exp, now_ms)) |
| 4057 | return 1; |
| 4058 | break; |
| 4059 | |
| 4060 | default: |
| 4061 | /* stop server if resolution failed for a long enough period */ |
| 4062 | exp = tick_add(res->last_valid, resolvers->hold.other); |
| 4063 | if (!tick_is_expired(exp, now_ms)) |
| 4064 | return 1; |
| 4065 | } |
| 4066 | |
| 4067 | /* Remove any associated server */ |
| 4068 | for (s = srvrq->proxy->srv; s != NULL; s = s->next) { |
| 4069 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 4070 | if (s->srvrq == srvrq) { |
Christopher Faulet | 52c10d7 | 2021-03-10 21:33:21 +0100 | [diff] [blame] | 4071 | dns_unlink_resolution(s->dns_requester); |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4072 | snr_update_srv_status(s, 1); |
| 4073 | free(s->hostname); |
| 4074 | free(s->hostname_dn); |
| 4075 | s->hostname = NULL; |
| 4076 | s->hostname_dn = NULL; |
| 4077 | s->hostname_dn_len = 0; |
Christopher Faulet | bbb6378 | 2021-02-23 12:24:09 +0100 | [diff] [blame] | 4078 | memset(&s->addr, 0, sizeof(s->addr)); |
| 4079 | s->svc_port = 0; |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4080 | } |
| 4081 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4082 | } |
| 4083 | |
Christopher Faulet | 738db5b | 2021-03-10 15:46:46 +0100 | [diff] [blame^] | 4084 | dns_purge_resolution_answer_records(res); |
| 4085 | |
Baptiste Assmann | 132d25f | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 4086 | return 1; |
| 4087 | } |
| 4088 | |
| 4089 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4090 | * Server Name Resolution error management callback |
| 4091 | * returns: |
| 4092 | * 0 on error |
| 4093 | * 1 when no error or safe ignore |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4094 | * |
| 4095 | * Grabs the server's lock. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4096 | */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4097 | int snr_resolution_error_cb(struct dns_requester *requester, int error_code) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4098 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4099 | struct server *s; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4100 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4101 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4102 | if (!s) |
| 4103 | return 1; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4104 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 9469af0 | 2021-03-10 20:31:40 +0100 | [diff] [blame] | 4105 | if (!snr_update_srv_status(s, 1)) |
| 4106 | memset(&s->addr, 0, sizeof(s->addr)); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4107 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 4108 | return 1; |
| 4109 | } |
| 4110 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4111 | /* |
| 4112 | * 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] | 4113 | * which owns <srv> and is up. |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4114 | * It returns a pointer to the first server found or NULL if <ip> is not yet |
| 4115 | * assigned. |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 4116 | * |
| 4117 | * Must be called with server lock held |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4118 | */ |
| 4119 | struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family) |
| 4120 | { |
| 4121 | struct server *tmpsrv; |
| 4122 | struct proxy *be; |
| 4123 | |
| 4124 | if (!srv) |
| 4125 | return NULL; |
| 4126 | |
| 4127 | be = srv->proxy; |
| 4128 | for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) { |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4129 | /* we found the current server is the same, ignore it */ |
| 4130 | if (srv == tmpsrv) |
| 4131 | continue; |
| 4132 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4133 | /* We want to compare the IP in the record with the IP of the servers in the |
| 4134 | * same backend, only if: |
| 4135 | * * DNS resolution is enabled on the server |
| 4136 | * * the hostname used for the resolution by our server is the same than the |
| 4137 | * one used for the server found in the backend |
| 4138 | * * the server found in the backend is not our current server |
| 4139 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4140 | HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4141 | if ((tmpsrv->hostname_dn == NULL) || |
| 4142 | (srv->hostname_dn_len != tmpsrv->hostname_dn_len) || |
Christopher Faulet | eb22113 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 4143 | (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) || |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4144 | (srv->puid == tmpsrv->puid)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4145 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4146 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4147 | } |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4148 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4149 | /* If the server has been taken down, don't consider it */ |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4150 | if (tmpsrv->next_admin & SRV_ADMF_RMAINT) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4151 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4152 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4153 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 4154 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4155 | /* At this point, we have 2 different servers using the same DNS hostname |
| 4156 | * for their respective resolution. |
| 4157 | */ |
| 4158 | if (*ip_family == tmpsrv->addr.ss_family && |
| 4159 | ((tmpsrv->addr.ss_family == AF_INET && |
| 4160 | memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) || |
| 4161 | (tmpsrv->addr.ss_family == AF_INET6 && |
| 4162 | memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4163 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4164 | return tmpsrv; |
| 4165 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4166 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 4169 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 4170 | return NULL; |
| 4171 | } |
| 4172 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4173 | /* Sets the server's address (srv->addr) from srv->hostname using the libc's |
| 4174 | * resolver. This is suited for initial address configuration. Returns 0 on |
| 4175 | * success otherwise a non-zero error code. In case of error, *err_code, if |
| 4176 | * not NULL, is filled up. |
| 4177 | */ |
| 4178 | int srv_set_addr_via_libc(struct server *srv, int *err_code) |
| 4179 | { |
| 4180 | if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) { |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4181 | if (err_code) |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4182 | *err_code |= ERR_WARN; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4183 | return 1; |
| 4184 | } |
| 4185 | return 0; |
| 4186 | } |
| 4187 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4188 | /* Set the server's FDQN (->hostname) from <hostname>. |
| 4189 | * Returns -1 if failed, 0 if not. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4190 | * |
| 4191 | * Must be called with the server lock held. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4192 | */ |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4193 | 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] | 4194 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4195 | struct dns_resolution *resolution; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4196 | char *hostname_dn; |
| 4197 | int hostname_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4198 | |
Frédéric Lécaille | 5afb3cf | 2018-08-21 15:04:23 +0200 | [diff] [blame] | 4199 | /* Note that the server lock is already held. */ |
| 4200 | if (!srv->resolvers) |
| 4201 | return -1; |
| 4202 | |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4203 | if (!dns_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4204 | HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4205 | /* run time DNS resolution was not active for this server |
| 4206 | * and we can't enable it at run time for now. |
| 4207 | */ |
| 4208 | if (!srv->dns_requester) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4209 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4210 | |
| 4211 | chunk_reset(&trash); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4212 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4213 | hostname_dn = trash.area; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4214 | hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1, |
| 4215 | hostname_dn, trash.size); |
| 4216 | if (hostname_dn_len == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4217 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4218 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4219 | resolution = srv->dns_requester->resolution; |
| 4220 | if (resolution && |
| 4221 | resolution->hostname_dn && |
Christopher Faulet | eb22113 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 4222 | resolution->hostname_dn_len == hostname_dn_len && |
| 4223 | strcasecmp(resolution->hostname_dn, hostname_dn) == 0) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4224 | goto end; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4225 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4226 | dns_unlink_resolution(srv->dns_requester); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4227 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4228 | free(srv->hostname); |
| 4229 | free(srv->hostname_dn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4230 | srv->hostname = strdup(hostname); |
| 4231 | srv->hostname_dn = strdup(hostname_dn); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4232 | srv->hostname_dn_len = hostname_dn_len; |
| 4233 | if (!srv->hostname || !srv->hostname_dn) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4234 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4235 | |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 4236 | if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4237 | goto err; |
| 4238 | |
| 4239 | end: |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4240 | if (!dns_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4241 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4242 | return 0; |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4243 | |
| 4244 | err: |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4245 | if (!dns_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4246 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4247 | return -1; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4248 | } |
| 4249 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4250 | /* Sets the server's address (srv->addr) from srv->lastaddr which was filled |
| 4251 | * from the state file. This is suited for initial address configuration. |
| 4252 | * Returns 0 on success otherwise a non-zero error code. In case of error, |
| 4253 | * *err_code, if not NULL, is filled up. |
| 4254 | */ |
| 4255 | static int srv_apply_lastaddr(struct server *srv, int *err_code) |
| 4256 | { |
| 4257 | if (!str2ip2(srv->lastaddr, &srv->addr, 0)) { |
| 4258 | if (err_code) |
| 4259 | *err_code |= ERR_WARN; |
| 4260 | return 1; |
| 4261 | } |
| 4262 | return 0; |
| 4263 | } |
| 4264 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4265 | /* returns 0 if no error, otherwise a combination of ERR_* flags */ |
| 4266 | static int srv_iterate_initaddr(struct server *srv) |
| 4267 | { |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4268 | char *name = srv->hostname; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4269 | int return_code = 0; |
| 4270 | int err_code; |
| 4271 | unsigned int methods; |
| 4272 | |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4273 | /* If no addr and no hostname set, get the name from the DNS SRV request */ |
| 4274 | if (!name && srv->srvrq) |
| 4275 | name = srv->srvrq->name; |
| 4276 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4277 | methods = srv->init_addr_methods; |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4278 | if (!methods) { |
| 4279 | /* otherwise default to "last,libc" */ |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4280 | srv_append_initaddr(&methods, SRV_IADDR_LAST); |
| 4281 | srv_append_initaddr(&methods, SRV_IADDR_LIBC); |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4282 | if (srv->resolvers_id) { |
| 4283 | /* dns resolution is configured, add "none" to not fail on startup */ |
| 4284 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4285 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4286 | } |
| 4287 | |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 4288 | /* "-dr" : always append "none" so that server addresses resolution |
| 4289 | * failures are silently ignored, this is convenient to validate some |
| 4290 | * configs out of their environment. |
| 4291 | */ |
| 4292 | if (global.tune.options & GTUNE_RESOLVE_DONTFAIL) |
| 4293 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4294 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4295 | while (methods) { |
| 4296 | err_code = 0; |
| 4297 | switch (srv_get_next_initaddr(&methods)) { |
| 4298 | case SRV_IADDR_LAST: |
| 4299 | if (!srv->lastaddr) |
| 4300 | continue; |
| 4301 | if (srv_apply_lastaddr(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4302 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4303 | return_code |= err_code; |
| 4304 | break; |
| 4305 | |
| 4306 | case SRV_IADDR_LIBC: |
| 4307 | if (!srv->hostname) |
| 4308 | continue; |
| 4309 | if (srv_set_addr_via_libc(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4310 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4311 | return_code |= err_code; |
| 4312 | break; |
| 4313 | |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4314 | case SRV_IADDR_NONE: |
| 4315 | srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4316 | if (return_code) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4317 | 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] | 4318 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4319 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4320 | return return_code; |
| 4321 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4322 | case SRV_IADDR_IP: |
| 4323 | ipcpy(&srv->init_addr, &srv->addr); |
| 4324 | if (return_code) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4325 | 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] | 4326 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4327 | } |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4328 | goto out; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4329 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4330 | default: /* unhandled method */ |
| 4331 | break; |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | if (!return_code) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4336 | 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] | 4337 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4338 | } |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4339 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4340 | 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] | 4341 | srv->conf.file, srv->conf.line, srv->id, name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4342 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4343 | |
| 4344 | return_code |= ERR_ALERT | ERR_FATAL; |
| 4345 | return return_code; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4346 | out: |
| 4347 | srv_set_dyncookie(srv); |
| 4348 | return return_code; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4349 | } |
| 4350 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4351 | /* |
| 4352 | * This function parses all backends and all servers within each backend |
| 4353 | * and performs servers' addr resolution based on information provided by: |
| 4354 | * - configuration file |
| 4355 | * - server-state file (states provided by an 'old' haproxy process) |
| 4356 | * |
| 4357 | * Returns 0 if no error, otherwise, a combination of ERR_ flags. |
| 4358 | */ |
| 4359 | int srv_init_addr(void) |
| 4360 | { |
| 4361 | struct proxy *curproxy; |
| 4362 | int return_code = 0; |
| 4363 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 4364 | curproxy = proxies_list; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4365 | while (curproxy) { |
| 4366 | struct server *srv; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4367 | |
| 4368 | /* servers are in backend only */ |
Amaury Denoyelle | 2f55daf | 2021-01-06 14:28:50 +0100 | [diff] [blame] | 4369 | if (!(curproxy->cap & PR_CAP_BE) || curproxy->state == PR_STSTOPPED) |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4370 | goto srv_init_addr_next; |
| 4371 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4372 | for (srv = curproxy->srv; srv; srv = srv->next) |
Christopher Faulet | f7474aa | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4373 | if (srv->hostname || srv->srvrq) |
Willy Tarreau | 3d609a7 | 2017-09-06 14:22:45 +0200 | [diff] [blame] | 4374 | return_code |= srv_iterate_initaddr(srv); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4375 | |
| 4376 | srv_init_addr_next: |
| 4377 | curproxy = curproxy->next; |
| 4378 | } |
| 4379 | |
| 4380 | return return_code; |
| 4381 | } |
| 4382 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4383 | /* |
| 4384 | * Must be called with the server lock held. |
| 4385 | */ |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4386 | 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] | 4387 | { |
| 4388 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4389 | struct buffer *msg; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4390 | |
| 4391 | msg = get_trash_chunk(); |
| 4392 | chunk_reset(msg); |
| 4393 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 4394 | if (server->hostname && !strcmp(fqdn, server->hostname)) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4395 | chunk_appendf(msg, "no need to change the FDQN"); |
| 4396 | goto out; |
| 4397 | } |
| 4398 | |
| 4399 | if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) { |
| 4400 | chunk_appendf(msg, "invalid fqdn '%s'", fqdn); |
| 4401 | goto out; |
| 4402 | } |
| 4403 | |
| 4404 | chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s", |
| 4405 | server->proxy->id, server->id, server->hostname, fqdn); |
| 4406 | |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4407 | if (srv_set_fqdn(server, fqdn, dns_locked) < 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4408 | chunk_reset(msg); |
| 4409 | chunk_appendf(msg, "could not update %s/%s FQDN", |
| 4410 | server->proxy->id, server->id); |
| 4411 | goto out; |
| 4412 | } |
| 4413 | |
| 4414 | /* Flag as FQDN set from stats socket. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 4415 | server->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4416 | |
| 4417 | out: |
| 4418 | if (updater) |
| 4419 | chunk_appendf(msg, " by '%s'", updater); |
| 4420 | chunk_appendf(msg, "\n"); |
| 4421 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4422 | return msg->area; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4426 | /* Expects to find a backend and a server in <arg> under the form <backend>/<server>, |
| 4427 | * and returns the pointer to the server. Otherwise, display adequate error messages |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4428 | * 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] | 4429 | * used for CLI commands requiring a server name. |
| 4430 | * Important: the <arg> is modified to remove the '/'. |
| 4431 | */ |
| 4432 | struct server *cli_find_server(struct appctx *appctx, char *arg) |
| 4433 | { |
| 4434 | struct proxy *px; |
| 4435 | struct server *sv; |
| 4436 | char *line; |
| 4437 | |
| 4438 | /* split "backend/server" and make <line> point to server */ |
| 4439 | for (line = arg; *line; line++) |
| 4440 | if (*line == '/') { |
| 4441 | *line++ = '\0'; |
| 4442 | break; |
| 4443 | } |
| 4444 | |
| 4445 | if (!*line || !*arg) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4446 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4447 | appctx->ctx.cli.msg = "Require 'backend/server'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4448 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4449 | return NULL; |
| 4450 | } |
| 4451 | |
| 4452 | if (!get_backend_server(arg, line, &px, &sv)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4453 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4454 | 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] | 4455 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4456 | return NULL; |
| 4457 | } |
| 4458 | |
| 4459 | if (px->state == PR_STSTOPPED) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4460 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4461 | appctx->ctx.cli.msg = "Proxy is disabled.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4462 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4463 | return NULL; |
| 4464 | } |
| 4465 | |
| 4466 | return sv; |
| 4467 | } |
| 4468 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4469 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4470 | /* grabs the server lock */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4471 | 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] | 4472 | { |
| 4473 | struct server *sv; |
| 4474 | const char *warning; |
| 4475 | |
| 4476 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4477 | return 1; |
| 4478 | |
| 4479 | sv = cli_find_server(appctx, args[2]); |
| 4480 | if (!sv) |
| 4481 | return 1; |
| 4482 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4483 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 4484 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4485 | if (strcmp(args[3], "weight") == 0) { |
| 4486 | warning = server_parse_weight_change_request(sv, args[4]); |
| 4487 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4488 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4489 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4490 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4491 | } |
| 4492 | } |
| 4493 | else if (strcmp(args[3], "state") == 0) { |
| 4494 | if (strcmp(args[4], "ready") == 0) |
| 4495 | srv_adm_set_ready(sv); |
| 4496 | else if (strcmp(args[4], "drain") == 0) |
| 4497 | srv_adm_set_drain(sv); |
| 4498 | else if (strcmp(args[4], "maint") == 0) |
| 4499 | srv_adm_set_maint(sv); |
| 4500 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4501 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4502 | 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] | 4503 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4504 | } |
| 4505 | } |
| 4506 | else if (strcmp(args[3], "health") == 0) { |
| 4507 | if (sv->track) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4508 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4509 | appctx->ctx.cli.msg = "cannot change health on a tracking server.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4510 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4511 | } |
| 4512 | else if (strcmp(args[4], "up") == 0) { |
| 4513 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4514 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4515 | } |
| 4516 | else if (strcmp(args[4], "stopping") == 0) { |
| 4517 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4518 | srv_set_stopping(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4519 | } |
| 4520 | else if (strcmp(args[4], "down") == 0) { |
| 4521 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4522 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4523 | } |
| 4524 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4525 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4526 | 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] | 4527 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4528 | } |
| 4529 | } |
| 4530 | else if (strcmp(args[3], "agent") == 0) { |
| 4531 | if (!(sv->agent.state & CHK_ST_ENABLED)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4532 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4533 | 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] | 4534 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4535 | } |
| 4536 | else if (strcmp(args[4], "up") == 0) { |
| 4537 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4538 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4539 | } |
| 4540 | else if (strcmp(args[4], "down") == 0) { |
| 4541 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4542 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4543 | } |
| 4544 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4545 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4546 | 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] | 4547 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4548 | } |
| 4549 | } |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4550 | else if (strcmp(args[3], "agent-addr") == 0) { |
| 4551 | if (!(sv->agent.state & CHK_ST_ENABLED)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4552 | appctx->ctx.cli.severity = LOG_ERR; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4553 | appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; |
| 4554 | appctx->st0 = CLI_ST_PRINT; |
| 4555 | } else { |
| 4556 | if (str2ip(args[4], &sv->agent.addr) == NULL) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4557 | appctx->ctx.cli.severity = LOG_ERR; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4558 | appctx->ctx.cli.msg = "incorrect addr address given for agent.\n"; |
| 4559 | appctx->st0 = CLI_ST_PRINT; |
| 4560 | } |
| 4561 | } |
| 4562 | } |
| 4563 | else if (strcmp(args[3], "agent-send") == 0) { |
| 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; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4566 | appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n"; |
| 4567 | appctx->st0 = CLI_ST_PRINT; |
| 4568 | } else { |
| 4569 | char *nss = strdup(args[4]); |
| 4570 | if (!nss) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4571 | appctx->ctx.cli.severity = LOG_ERR; |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4572 | appctx->ctx.cli.msg = "cannot allocate memory for new string.\n"; |
| 4573 | appctx->st0 = CLI_ST_PRINT; |
| 4574 | } else { |
| 4575 | free(sv->agent.send_string); |
| 4576 | sv->agent.send_string = nss; |
| 4577 | sv->agent.send_string_len = strlen(args[4]); |
| 4578 | } |
| 4579 | } |
| 4580 | } |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4581 | else if (strcmp(args[3], "check-port") == 0) { |
| 4582 | int i = 0; |
| 4583 | if (strl2irc(args[4], strlen(args[4]), &i) != 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4584 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4585 | 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] | 4586 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 6ce38f3 | 2017-11-05 10:19:23 +0100 | [diff] [blame] | 4587 | goto out_unlock; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4588 | } |
| 4589 | if ((i < 0) || (i > 65535)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4590 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4591 | appctx->ctx.cli.msg = "provided port is not valid.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4592 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 6ce38f3 | 2017-11-05 10:19:23 +0100 | [diff] [blame] | 4593 | goto out_unlock; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4594 | } |
| 4595 | /* prevent the update of port to 0 if MAPPORTS are in use */ |
| 4596 | if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4597 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4598 | 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] | 4599 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 6ce38f3 | 2017-11-05 10:19:23 +0100 | [diff] [blame] | 4600 | goto out_unlock; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4601 | } |
| 4602 | sv->check.port = i; |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4603 | appctx->ctx.cli.severity = LOG_NOTICE; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4604 | appctx->ctx.cli.msg = "health check port updated.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4605 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4606 | } |
| 4607 | else if (strcmp(args[3], "addr") == 0) { |
| 4608 | char *addr = NULL; |
| 4609 | char *port = NULL; |
| 4610 | if (strlen(args[4]) == 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4611 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4612 | 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] | 4613 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 6ce38f3 | 2017-11-05 10:19:23 +0100 | [diff] [blame] | 4614 | goto out_unlock; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4615 | } |
| 4616 | else { |
| 4617 | addr = args[4]; |
| 4618 | } |
| 4619 | if (strcmp(args[5], "port") == 0) { |
| 4620 | port = args[6]; |
| 4621 | } |
| 4622 | warning = update_server_addr_port(sv, addr, port, "stats socket command"); |
| 4623 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4624 | appctx->ctx.cli.severity = LOG_WARNING; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4625 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4626 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4627 | } |
| 4628 | srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); |
| 4629 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4630 | else if (strcmp(args[3], "fqdn") == 0) { |
| 4631 | if (!*args[4]) { |
Willy Tarreau | a075258 | 2017-11-05 10:17:49 +0100 | [diff] [blame] | 4632 | appctx->ctx.cli.severity = LOG_ERR; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4633 | appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n"; |
| 4634 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 6ce38f3 | 2017-11-05 10:19:23 +0100 | [diff] [blame] | 4635 | goto out_unlock; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4636 | } |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 4637 | warning = update_server_fqdn(sv, args[4], "stats socket command", 0); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4638 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4639 | appctx->ctx.cli.severity = LOG_WARNING; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4640 | appctx->ctx.cli.msg = warning; |
| 4641 | appctx->st0 = CLI_ST_PRINT; |
| 4642 | } |
| 4643 | } |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4644 | else { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4645 | appctx->ctx.cli.severity = LOG_ERR; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4646 | 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] | 4647 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4648 | } |
Willy Tarreau | 6ce38f3 | 2017-11-05 10:19:23 +0100 | [diff] [blame] | 4649 | out_unlock: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4650 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4651 | return 1; |
| 4652 | } |
| 4653 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4654 | 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] | 4655 | { |
| 4656 | struct stream_interface *si = appctx->owner; |
| 4657 | struct proxy *px; |
| 4658 | struct server *sv; |
| 4659 | char *line; |
| 4660 | |
| 4661 | |
| 4662 | /* split "backend/server" and make <line> point to server */ |
| 4663 | for (line = args[2]; *line; line++) |
| 4664 | if (*line == '/') { |
| 4665 | *line++ = '\0'; |
| 4666 | break; |
| 4667 | } |
| 4668 | |
| 4669 | if (!*line) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4670 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4671 | appctx->ctx.cli.msg = "Require 'backend/server'.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4672 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4673 | return 1; |
| 4674 | } |
| 4675 | |
| 4676 | if (!get_backend_server(args[2], line, &px, &sv)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4677 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4678 | 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] | 4679 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4680 | return 1; |
| 4681 | } |
| 4682 | |
| 4683 | /* return server's effective weight at the moment */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4684 | snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, |
| 4685 | sv->iweight); |
| 4686 | if (ci_putstr(si_ic(si), trash.area) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4687 | si_rx_room_blk(si); |
Christopher Faulet | 90b5abe | 2016-12-05 14:25:08 +0100 | [diff] [blame] | 4688 | return 0; |
| 4689 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4690 | return 1; |
| 4691 | } |
| 4692 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4693 | /* Parse a "set weight" command. |
| 4694 | * |
| 4695 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4696 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4697 | 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] | 4698 | { |
| 4699 | struct server *sv; |
| 4700 | const char *warning; |
| 4701 | |
| 4702 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4703 | return 1; |
| 4704 | |
| 4705 | sv = cli_find_server(appctx, args[2]); |
| 4706 | if (!sv) |
| 4707 | return 1; |
| 4708 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4709 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4710 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4711 | warning = server_parse_weight_change_request(sv, args[3]); |
| 4712 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4713 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4714 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4715 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4716 | } |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4717 | |
| 4718 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4719 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4720 | return 1; |
| 4721 | } |
| 4722 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4723 | /* parse a "set maxconn server" command. It always returns 1. |
| 4724 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4725 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4726 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4727 | 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] | 4728 | { |
| 4729 | struct server *sv; |
| 4730 | const char *warning; |
| 4731 | |
| 4732 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4733 | return 1; |
| 4734 | |
| 4735 | sv = cli_find_server(appctx, args[3]); |
| 4736 | if (!sv) |
| 4737 | return 1; |
| 4738 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4739 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4740 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4741 | warning = server_parse_maxconn_change_request(sv, args[4]); |
| 4742 | if (warning) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4743 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4744 | appctx->ctx.cli.msg = warning; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4745 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4746 | } |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4747 | |
| 4748 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4749 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4750 | return 1; |
| 4751 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4752 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4753 | /* parse a "disable agent" command. It always returns 1. |
| 4754 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4755 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4756 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4757 | 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] | 4758 | { |
| 4759 | struct server *sv; |
| 4760 | |
| 4761 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4762 | return 1; |
| 4763 | |
| 4764 | sv = cli_find_server(appctx, args[2]); |
| 4765 | if (!sv) |
| 4766 | return 1; |
| 4767 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4768 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4769 | sv->agent.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4770 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4771 | return 1; |
| 4772 | } |
| 4773 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4774 | /* parse a "disable health" command. It always returns 1. |
| 4775 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4776 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4777 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4778 | 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] | 4779 | { |
| 4780 | struct server *sv; |
| 4781 | |
| 4782 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4783 | return 1; |
| 4784 | |
| 4785 | sv = cli_find_server(appctx, args[2]); |
| 4786 | if (!sv) |
| 4787 | return 1; |
| 4788 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4789 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4790 | sv->check.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4791 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4792 | return 1; |
| 4793 | } |
| 4794 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4795 | /* parse a "disable server" command. It always returns 1. |
| 4796 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4797 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4798 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4799 | 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] | 4800 | { |
| 4801 | struct server *sv; |
| 4802 | |
| 4803 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4804 | return 1; |
| 4805 | |
| 4806 | sv = cli_find_server(appctx, args[2]); |
| 4807 | if (!sv) |
| 4808 | return 1; |
| 4809 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4810 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4811 | srv_adm_set_maint(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4812 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4813 | return 1; |
| 4814 | } |
| 4815 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4816 | /* parse a "enable agent" command. It always returns 1. |
| 4817 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4818 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4819 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4820 | 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] | 4821 | { |
| 4822 | struct server *sv; |
| 4823 | |
| 4824 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4825 | return 1; |
| 4826 | |
| 4827 | sv = cli_find_server(appctx, args[2]); |
| 4828 | if (!sv) |
| 4829 | return 1; |
| 4830 | |
| 4831 | if (!(sv->agent.state & CHK_ST_CONFIGURED)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4832 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4833 | 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] | 4834 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4835 | return 1; |
| 4836 | } |
| 4837 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4838 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4839 | sv->agent.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4840 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4841 | return 1; |
| 4842 | } |
| 4843 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4844 | /* parse a "enable health" command. It always returns 1. |
| 4845 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4846 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4847 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4848 | 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] | 4849 | { |
| 4850 | struct server *sv; |
| 4851 | |
| 4852 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4853 | return 1; |
| 4854 | |
| 4855 | sv = cli_find_server(appctx, args[2]); |
| 4856 | if (!sv) |
| 4857 | return 1; |
| 4858 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4859 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4860 | sv->check.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4861 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4862 | return 1; |
| 4863 | } |
| 4864 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4865 | /* parse a "enable server" command. It always returns 1. |
| 4866 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4867 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4868 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4869 | 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] | 4870 | { |
| 4871 | struct server *sv; |
| 4872 | |
| 4873 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4874 | return 1; |
| 4875 | |
| 4876 | sv = cli_find_server(appctx, args[2]); |
| 4877 | if (!sv) |
| 4878 | return 1; |
| 4879 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4880 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4881 | srv_adm_set_ready(sv); |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 4882 | if (!(sv->flags & SRV_F_COOKIESET) |
| 4883 | && (sv->proxy->ck_opts & PR_CK_DYNAMIC) && |
| 4884 | sv->cookie) |
| 4885 | srv_check_for_dup_dyncookie(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4886 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4887 | return 1; |
| 4888 | } |
| 4889 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4890 | /* register cli keywords */ |
| 4891 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4892 | { { "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] | 4893 | { { "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] | 4894 | { { "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] | 4895 | { { "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] | 4896 | { { "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] | 4897 | { { "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] | 4898 | { { "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] | 4899 | { { "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] | 4900 | { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight }, |
| 4901 | { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight }, |
| 4902 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4903 | {{},} |
| 4904 | }}; |
| 4905 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4906 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4907 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4908 | /* |
| 4909 | * This function applies server's status changes, it is |
| 4910 | * is designed to be called asynchronously. |
| 4911 | * |
Willy Tarreau | 228d60e | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 4912 | * Must be called with the server lock held. This may also be called at init |
| 4913 | * time as the result of parsing the state file, in which case no lock will be |
| 4914 | * held, and the server's warmup task can be null. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4915 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 4916 | static void srv_update_status(struct server *s) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4917 | { |
| 4918 | struct check *check = &s->check; |
| 4919 | int xferred; |
| 4920 | struct proxy *px = s->proxy; |
| 4921 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 4922 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 4923 | int log_level; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4924 | struct buffer *tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4925 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4926 | /* If currently main is not set we try to apply pending state changes */ |
| 4927 | if (!(s->cur_admin & SRV_ADMF_MAINT)) { |
| 4928 | int next_admin; |
| 4929 | |
| 4930 | /* Backup next admin */ |
| 4931 | next_admin = s->next_admin; |
| 4932 | |
| 4933 | /* restore current admin state */ |
| 4934 | s->next_admin = s->cur_admin; |
| 4935 | |
| 4936 | if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) { |
| 4937 | s->last_change = now.tv_sec; |
| 4938 | if (s->proxy->lbprm.set_server_status_down) |
| 4939 | s->proxy->lbprm.set_server_status_down(s); |
| 4940 | |
| 4941 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 4942 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 4943 | |
| 4944 | /* we might have streams queued on this server and waiting for |
| 4945 | * a connection. Those which are redispatchable will be queued |
| 4946 | * to another server or to the proxy itself. |
| 4947 | */ |
| 4948 | xferred = pendconn_redistribute(s); |
| 4949 | |
| 4950 | tmptrash = alloc_trash_chunk(); |
| 4951 | if (tmptrash) { |
| 4952 | chunk_printf(tmptrash, |
| 4953 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 4954 | s->proxy->id, s->id); |
| 4955 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4956 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4957 | ha_warning("%s.\n", tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4958 | |
| 4959 | /* we don't send an alert if the server was previously paused */ |
| 4960 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4961 | send_log(s->proxy, log_level, "%s.\n", |
| 4962 | tmptrash->area); |
| 4963 | send_email_alert(s, log_level, "%s", |
| 4964 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4965 | free_trash_chunk(tmptrash); |
| 4966 | tmptrash = NULL; |
| 4967 | } |
| 4968 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 4969 | set_backend_down(s->proxy); |
| 4970 | |
| 4971 | s->counters.down_trans++; |
| 4972 | } |
| 4973 | else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) { |
| 4974 | s->last_change = now.tv_sec; |
| 4975 | if (s->proxy->lbprm.set_server_status_down) |
| 4976 | s->proxy->lbprm.set_server_status_down(s); |
| 4977 | |
| 4978 | /* we might have streams queued on this server and waiting for |
| 4979 | * a connection. Those which are redispatchable will be queued |
| 4980 | * to another server or to the proxy itself. |
| 4981 | */ |
| 4982 | xferred = pendconn_redistribute(s); |
| 4983 | |
| 4984 | tmptrash = alloc_trash_chunk(); |
| 4985 | if (tmptrash) { |
| 4986 | chunk_printf(tmptrash, |
| 4987 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 4988 | s->proxy->id, s->id); |
| 4989 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4990 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4991 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4992 | ha_warning("%s.\n", tmptrash->area); |
| 4993 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 4994 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 4995 | free_trash_chunk(tmptrash); |
| 4996 | tmptrash = NULL; |
| 4997 | } |
| 4998 | |
| 4999 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5000 | set_backend_down(s->proxy); |
| 5001 | } |
| 5002 | else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING)) |
| 5003 | || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) { |
| 5004 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5005 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5006 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5007 | s->proxy->last_change = now.tv_sec; |
| 5008 | } |
| 5009 | |
Amaury Denoyelle | c1bd4bd | 2020-10-29 15:59:04 +0100 | [diff] [blame] | 5010 | 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] | 5011 | s->down_time += now.tv_sec - s->last_change; |
| 5012 | |
| 5013 | s->last_change = now.tv_sec; |
Willy Tarreau | 228d60e | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5014 | if (s->next_state == SRV_ST_STARTING && s->warmup) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5015 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5016 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5017 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5018 | /* now propagate the status change to any LB algorithms */ |
| 5019 | if (px->lbprm.update_server_eweight) |
| 5020 | px->lbprm.update_server_eweight(s); |
| 5021 | else if (srv_willbe_usable(s)) { |
| 5022 | if (px->lbprm.set_server_status_up) |
| 5023 | px->lbprm.set_server_status_up(s); |
| 5024 | } |
| 5025 | else { |
| 5026 | if (px->lbprm.set_server_status_down) |
| 5027 | px->lbprm.set_server_status_down(s); |
| 5028 | } |
| 5029 | |
| 5030 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5031 | * and it's not a backup server and its effective weight is > 0, |
| 5032 | * then it can accept new connections, so we shut down all streams |
| 5033 | * on all backup servers. |
| 5034 | */ |
| 5035 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5036 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5037 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5038 | |
| 5039 | /* check if we can handle some connections queued at the proxy. We |
| 5040 | * will take as many as we can handle. |
| 5041 | */ |
| 5042 | xferred = pendconn_grab_from_px(s); |
| 5043 | |
| 5044 | tmptrash = alloc_trash_chunk(); |
| 5045 | if (tmptrash) { |
| 5046 | chunk_printf(tmptrash, |
| 5047 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5048 | s->proxy->id, s->id); |
| 5049 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5050 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5051 | ha_warning("%s.\n", tmptrash->area); |
| 5052 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5053 | tmptrash->area); |
| 5054 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5055 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5056 | free_trash_chunk(tmptrash); |
| 5057 | tmptrash = NULL; |
| 5058 | } |
| 5059 | |
| 5060 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5061 | set_backend_down(s->proxy); |
| 5062 | } |
| 5063 | else if (s->cur_eweight != s->next_eweight) { |
| 5064 | /* now propagate the status change to any LB algorithms */ |
| 5065 | if (px->lbprm.update_server_eweight) |
| 5066 | px->lbprm.update_server_eweight(s); |
| 5067 | else if (srv_willbe_usable(s)) { |
| 5068 | if (px->lbprm.set_server_status_up) |
| 5069 | px->lbprm.set_server_status_up(s); |
| 5070 | } |
| 5071 | else { |
| 5072 | if (px->lbprm.set_server_status_down) |
| 5073 | px->lbprm.set_server_status_down(s); |
| 5074 | } |
| 5075 | |
| 5076 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5077 | set_backend_down(s->proxy); |
| 5078 | } |
| 5079 | |
| 5080 | s->next_admin = next_admin; |
| 5081 | } |
| 5082 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5083 | /* reset operational state change */ |
| 5084 | *s->op_st_chg.reason = 0; |
| 5085 | s->op_st_chg.status = s->op_st_chg.code = -1; |
| 5086 | s->op_st_chg.duration = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5087 | |
| 5088 | /* Now we try to apply pending admin changes */ |
| 5089 | |
| 5090 | /* Maintenance must also disable health checks */ |
| 5091 | if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) { |
| 5092 | if (s->check.state & CHK_ST_ENABLED) { |
| 5093 | s->check.state |= CHK_ST_PAUSED; |
| 5094 | check->health = 0; |
| 5095 | } |
| 5096 | |
| 5097 | if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */ |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5098 | tmptrash = alloc_trash_chunk(); |
| 5099 | if (tmptrash) { |
| 5100 | chunk_printf(tmptrash, |
| 5101 | "%sServer %s/%s was DOWN and now enters maintenance%s%s%s", |
| 5102 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5103 | *(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] | 5104 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5105 | 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] | 5106 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5107 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5108 | ha_warning("%s.\n", tmptrash->area); |
| 5109 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5110 | tmptrash->area); |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5111 | } |
| 5112 | free_trash_chunk(tmptrash); |
| 5113 | tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5114 | } |
Emeric Brun | 8f29829 | 2017-12-06 16:47:17 +0100 | [diff] [blame] | 5115 | /* commit new admin status */ |
| 5116 | |
| 5117 | s->cur_admin = s->next_admin; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5118 | } |
| 5119 | else { /* server was still running */ |
| 5120 | check->health = 0; /* failure */ |
| 5121 | s->last_change = now.tv_sec; |
Emeric Brun | e311480 | 2017-12-21 14:42:26 +0100 | [diff] [blame] | 5122 | |
| 5123 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5124 | if (s->proxy->lbprm.set_server_status_down) |
| 5125 | s->proxy->lbprm.set_server_status_down(s); |
| 5126 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5127 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5128 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5129 | |
| 5130 | /* we might have streams queued on this server and waiting for |
| 5131 | * a connection. Those which are redispatchable will be queued |
| 5132 | * to another server or to the proxy itself. |
| 5133 | */ |
| 5134 | xferred = pendconn_redistribute(s); |
| 5135 | |
| 5136 | tmptrash = alloc_trash_chunk(); |
| 5137 | if (tmptrash) { |
| 5138 | chunk_printf(tmptrash, |
| 5139 | "%sServer %s/%s is going DOWN for maintenance%s%s%s", |
| 5140 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5141 | s->proxy->id, s->id, |
| 5142 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5143 | |
| 5144 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT)); |
| 5145 | |
| 5146 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5147 | ha_warning("%s.\n", tmptrash->area); |
| 5148 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", |
| 5149 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5150 | } |
| 5151 | free_trash_chunk(tmptrash); |
| 5152 | tmptrash = NULL; |
| 5153 | } |
| 5154 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5155 | set_backend_down(s->proxy); |
| 5156 | |
| 5157 | s->counters.down_trans++; |
| 5158 | } |
| 5159 | } |
| 5160 | else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) { |
| 5161 | /* OK here we're leaving maintenance, we have many things to check, |
| 5162 | * because the server might possibly be coming back up depending on |
| 5163 | * its state. In practice, leaving maintenance means that we should |
| 5164 | * immediately turn to UP (more or less the slowstart) under the |
| 5165 | * following conditions : |
| 5166 | * - server is neither checked nor tracked |
| 5167 | * - server tracks another server which is not checked |
| 5168 | * - server tracks another server which is already up |
| 5169 | * Which sums up as something simpler : |
| 5170 | * "either the tracking server is up or the server's checks are disabled |
| 5171 | * or up". Otherwise we only re-enable health checks. There's a special |
| 5172 | * case associated to the stopping state which can be inherited. Note |
| 5173 | * that the server might still be in drain mode, which is naturally dealt |
| 5174 | * with by the lower level functions. |
| 5175 | */ |
| 5176 | |
| 5177 | if (s->check.state & CHK_ST_ENABLED) { |
| 5178 | s->check.state &= ~CHK_ST_PAUSED; |
| 5179 | check->health = check->rise; /* start OK but check immediately */ |
| 5180 | } |
| 5181 | |
| 5182 | if ((!s->track || s->track->next_state != SRV_ST_STOPPED) && |
| 5183 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 5184 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 5185 | if (s->track && s->track->next_state == SRV_ST_STOPPING) { |
| 5186 | s->next_state = SRV_ST_STOPPING; |
| 5187 | } |
| 5188 | else { |
| 5189 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | 228d60e | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5190 | if (s->slowstart > 0) { |
| 5191 | if (s->warmup) |
| 5192 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5193 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5194 | else |
| 5195 | s->next_state = SRV_ST_RUNNING; |
| 5196 | } |
| 5197 | |
| 5198 | } |
| 5199 | |
| 5200 | tmptrash = alloc_trash_chunk(); |
| 5201 | if (tmptrash) { |
| 5202 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5203 | chunk_printf(tmptrash, |
| 5204 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 5205 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5206 | s->proxy->id, s->id, |
| 5207 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5208 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5209 | } |
| 5210 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5211 | chunk_printf(tmptrash, |
| 5212 | "%sServer %s/%s ('%s') is %s/%s (resolves again)", |
| 5213 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5214 | s->proxy->id, s->id, s->hostname, |
| 5215 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5216 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5217 | } |
| 5218 | if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5219 | chunk_printf(tmptrash, |
| 5220 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 5221 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5222 | s->proxy->id, s->id, |
| 5223 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5224 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5225 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5226 | ha_warning("%s.\n", tmptrash->area); |
| 5227 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5228 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5229 | free_trash_chunk(tmptrash); |
| 5230 | tmptrash = NULL; |
| 5231 | } |
| 5232 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5233 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5234 | /* now propagate the status change to any LB algorithms */ |
| 5235 | if (px->lbprm.update_server_eweight) |
| 5236 | px->lbprm.update_server_eweight(s); |
| 5237 | else if (srv_willbe_usable(s)) { |
| 5238 | if (px->lbprm.set_server_status_up) |
| 5239 | px->lbprm.set_server_status_up(s); |
| 5240 | } |
| 5241 | else { |
| 5242 | if (px->lbprm.set_server_status_down) |
| 5243 | px->lbprm.set_server_status_down(s); |
| 5244 | } |
| 5245 | |
| 5246 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5247 | set_backend_down(s->proxy); |
| 5248 | |
Willy Tarreau | 6a78e61 | 2018-08-07 10:14:53 +0200 | [diff] [blame] | 5249 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5250 | * and it's not a backup server and its effective weight is > 0, |
| 5251 | * then it can accept new connections, so we shut down all streams |
| 5252 | * on all backup servers. |
| 5253 | */ |
| 5254 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5255 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5256 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5257 | |
| 5258 | /* check if we can handle some connections queued at the proxy. We |
| 5259 | * will take as many as we can handle. |
| 5260 | */ |
| 5261 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5262 | } |
| 5263 | else if (s->next_admin & SRV_ADMF_MAINT) { |
| 5264 | /* remaining in maintenance mode, let's inform precisely about the |
| 5265 | * situation. |
| 5266 | */ |
| 5267 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5268 | tmptrash = alloc_trash_chunk(); |
| 5269 | if (tmptrash) { |
| 5270 | chunk_printf(tmptrash, |
| 5271 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 5272 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5273 | s->proxy->id, s->id); |
| 5274 | |
| 5275 | if (s->track) /* normally it's mandatory here */ |
| 5276 | chunk_appendf(tmptrash, " via %s/%s", |
| 5277 | s->track->proxy->id, s->track->id); |
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 | } |
| 5285 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5286 | tmptrash = alloc_trash_chunk(); |
| 5287 | if (tmptrash) { |
| 5288 | chunk_printf(tmptrash, |
| 5289 | "%sServer %s/%s ('%s') resolves again but remains in maintenance", |
| 5290 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5291 | s->proxy->id, s->id, s->hostname); |
| 5292 | |
| 5293 | if (s->track) /* normally it's mandatory here */ |
| 5294 | chunk_appendf(tmptrash, " via %s/%s", |
| 5295 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5296 | ha_warning("%s.\n", tmptrash->area); |
| 5297 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5298 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5299 | free_trash_chunk(tmptrash); |
| 5300 | tmptrash = NULL; |
| 5301 | } |
| 5302 | } |
| 5303 | else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5304 | tmptrash = alloc_trash_chunk(); |
| 5305 | if (tmptrash) { |
| 5306 | chunk_printf(tmptrash, |
| 5307 | "%sServer %s/%s remains in forced maintenance", |
| 5308 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5309 | s->proxy->id, s->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5310 | ha_warning("%s.\n", tmptrash->area); |
| 5311 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5312 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5313 | free_trash_chunk(tmptrash); |
| 5314 | tmptrash = NULL; |
| 5315 | } |
| 5316 | } |
| 5317 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
| 5318 | |
| 5319 | s->cur_admin = s->next_admin; |
| 5320 | } |
| 5321 | |
| 5322 | if (!(s->next_admin & SRV_ADMF_MAINT)) { |
| 5323 | if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) { |
| 5324 | /* drain state is applied only if not yet in maint */ |
| 5325 | |
| 5326 | s->last_change = now.tv_sec; |
| 5327 | if (px->lbprm.set_server_status_down) |
| 5328 | px->lbprm.set_server_status_down(s); |
| 5329 | |
| 5330 | /* we might have streams queued on this server and waiting for |
| 5331 | * a connection. Those which are redispatchable will be queued |
| 5332 | * to another server or to the proxy itself. |
| 5333 | */ |
| 5334 | xferred = pendconn_redistribute(s); |
| 5335 | |
| 5336 | tmptrash = alloc_trash_chunk(); |
| 5337 | if (tmptrash) { |
| 5338 | chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s", |
| 5339 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5340 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5341 | |
| 5342 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN)); |
| 5343 | |
| 5344 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5345 | ha_warning("%s.\n", tmptrash->area); |
| 5346 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5347 | tmptrash->area); |
| 5348 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5349 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5350 | } |
| 5351 | free_trash_chunk(tmptrash); |
| 5352 | tmptrash = NULL; |
| 5353 | } |
| 5354 | |
| 5355 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5356 | set_backend_down(s->proxy); |
| 5357 | } |
| 5358 | else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) { |
| 5359 | /* OK completely leaving drain mode */ |
| 5360 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5361 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5362 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5363 | s->proxy->last_change = now.tv_sec; |
| 5364 | } |
| 5365 | |
| 5366 | if (s->last_change < now.tv_sec) // ignore negative times |
| 5367 | s->down_time += now.tv_sec - s->last_change; |
| 5368 | s->last_change = now.tv_sec; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5369 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5370 | |
| 5371 | tmptrash = alloc_trash_chunk(); |
| 5372 | if (tmptrash) { |
| 5373 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5374 | chunk_printf(tmptrash, |
| 5375 | "%sServer %s/%s is %s (leaving forced drain)", |
| 5376 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5377 | s->proxy->id, s->id, |
| 5378 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5379 | } |
| 5380 | else { |
| 5381 | chunk_printf(tmptrash, |
| 5382 | "%sServer %s/%s is %s (leaving drain)", |
| 5383 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5384 | s->proxy->id, s->id, |
| 5385 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5386 | if (s->track) /* normally it's mandatory here */ |
| 5387 | chunk_appendf(tmptrash, " via %s/%s", |
| 5388 | s->track->proxy->id, s->track->id); |
| 5389 | } |
| 5390 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5391 | ha_warning("%s.\n", tmptrash->area); |
| 5392 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5393 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5394 | free_trash_chunk(tmptrash); |
| 5395 | tmptrash = NULL; |
| 5396 | } |
| 5397 | |
| 5398 | /* now propagate the status change to any LB algorithms */ |
| 5399 | if (px->lbprm.update_server_eweight) |
| 5400 | px->lbprm.update_server_eweight(s); |
| 5401 | else if (srv_willbe_usable(s)) { |
| 5402 | if (px->lbprm.set_server_status_up) |
| 5403 | px->lbprm.set_server_status_up(s); |
| 5404 | } |
| 5405 | else { |
| 5406 | if (px->lbprm.set_server_status_down) |
| 5407 | px->lbprm.set_server_status_down(s); |
| 5408 | } |
| 5409 | } |
| 5410 | else if ((s->next_admin & SRV_ADMF_DRAIN)) { |
| 5411 | /* remaining in drain mode after removing one of its flags */ |
| 5412 | |
| 5413 | tmptrash = alloc_trash_chunk(); |
| 5414 | if (tmptrash) { |
| 5415 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5416 | chunk_printf(tmptrash, |
| 5417 | "%sServer %s/%s is leaving forced drain but remains in drain mode", |
| 5418 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5419 | s->proxy->id, s->id); |
| 5420 | |
| 5421 | if (s->track) /* normally it's mandatory here */ |
| 5422 | chunk_appendf(tmptrash, " via %s/%s", |
| 5423 | s->track->proxy->id, s->track->id); |
| 5424 | } |
| 5425 | else { |
| 5426 | chunk_printf(tmptrash, |
| 5427 | "%sServer %s/%s remains in forced drain mode", |
| 5428 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5429 | s->proxy->id, s->id); |
| 5430 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5431 | ha_warning("%s.\n", tmptrash->area); |
| 5432 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5433 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5434 | free_trash_chunk(tmptrash); |
| 5435 | tmptrash = NULL; |
| 5436 | } |
| 5437 | |
| 5438 | /* commit new admin status */ |
| 5439 | |
| 5440 | s->cur_admin = s->next_admin; |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | /* Re-set log strings to empty */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5445 | *s->adm_st_chg_cause = 0; |
| 5446 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5447 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5448 | struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state) |
| 5449 | { |
| 5450 | struct connection *conn; |
| 5451 | |
| 5452 | while ((conn = LIST_POP_LOCKED(&toremove_connections[tid], |
| 5453 | struct connection *, list)) != NULL) { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 5454 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5455 | } |
| 5456 | |
| 5457 | return task; |
| 5458 | } |
| 5459 | |
Willy Tarreau | 980855b | 2019-02-07 14:59:29 +0100 | [diff] [blame] | 5460 | 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] | 5461 | { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5462 | struct server *srv; |
| 5463 | struct eb32_node *eb; |
| 5464 | int i; |
| 5465 | unsigned int next_wakeup; |
| 5466 | int need_wakeup = 0; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5467 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5468 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5469 | while (1) { |
| 5470 | int srv_is_empty = 1; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5471 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5472 | eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK); |
| 5473 | if (!eb) { |
| 5474 | /* we might have reached the end of the tree, typically because |
| 5475 | * <now_ms> is in the first half and we're first scanning the last |
| 5476 | * half. Let's loop back to the beginning of the tree now. |
| 5477 | */ |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5478 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5479 | eb = eb32_first(&idle_conn_srv); |
| 5480 | if (likely(!eb)) |
| 5481 | break; |
| 5482 | } |
| 5483 | if (tick_is_lt(now_ms, eb->key)) { |
| 5484 | /* timer not expired yet, revisit it later */ |
| 5485 | next_wakeup = eb->key; |
| 5486 | need_wakeup = 1; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5487 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5488 | } |
| 5489 | srv = eb32_entry(eb, struct server, idle_node); |
| 5490 | for (i = 0; i < global.nbthread; i++) { |
| 5491 | int max_conn = (srv->curr_idle_thr[i] / 2) + |
| 5492 | (srv->curr_idle_thr[i] & 1); |
| 5493 | int j; |
| 5494 | int did_remove = 0; |
| 5495 | |
Olivier Houchard | ba936e5 | 2019-07-11 15:49:00 +0200 | [diff] [blame] | 5496 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5497 | for (j = 0; j < max_conn; j++) { |
| 5498 | struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list); |
| 5499 | if (!conn) |
| 5500 | break; |
| 5501 | did_remove = 1; |
| 5502 | LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list); |
| 5503 | } |
Olivier Houchard | ba936e5 | 2019-07-11 15:49:00 +0200 | [diff] [blame] | 5504 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5505 | if (did_remove && max_conn < srv->curr_idle_thr[i]) |
| 5506 | srv_is_empty = 0; |
| 5507 | if (did_remove) |
| 5508 | task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER); |
| 5509 | } |
| 5510 | eb32_delete(&srv->idle_node); |
| 5511 | if (!srv_is_empty) { |
| 5512 | /* There are still more idle connections, add the |
| 5513 | * server back in the tree. |
| 5514 | */ |
| 5515 | srv->idle_node.key = tick_add(srv->pool_purge_delay, |
| 5516 | now_ms); |
| 5517 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
| 5518 | } |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5519 | } |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5520 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5521 | |
| 5522 | if (need_wakeup) |
| 5523 | task->expire = next_wakeup; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5524 | else |
| 5525 | task->expire = TICK_ETERNITY; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5526 | |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5527 | return task; |
| 5528 | } |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 5529 | |
| 5530 | /* config parser for global "tune.pool-{low,high}-fd-ratio" */ |
| 5531 | static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx, |
| 5532 | struct proxy *defpx, const char *file, int line, |
| 5533 | char **err) |
| 5534 | { |
| 5535 | int arg = -1; |
| 5536 | |
| 5537 | if (too_many_args(1, args, err, NULL)) |
| 5538 | return -1; |
| 5539 | |
| 5540 | if (*(args[1]) != 0) |
| 5541 | arg = atoi(args[1]); |
| 5542 | |
| 5543 | if (arg < 0 || arg > 100) { |
| 5544 | memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]); |
| 5545 | return -1; |
| 5546 | } |
| 5547 | |
| 5548 | if (args[0][10] == 'h') |
| 5549 | global.tune.pool_high_ratio = arg; |
| 5550 | else |
| 5551 | global.tune.pool_low_ratio = arg; |
| 5552 | return 0; |
| 5553 | } |
| 5554 | |
| 5555 | /* config keyword parsers */ |
| 5556 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 5557 | { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 5558 | { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 5559 | { 0, NULL, NULL } |
| 5560 | }}; |
| 5561 | |
| 5562 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 5563 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 5564 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5565 | * Local variables: |
| 5566 | * c-indent-level: 8 |
| 5567 | * c-basic-offset: 8 |
| 5568 | * End: |
| 5569 | */ |