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