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