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