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