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