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