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