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