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