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