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