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