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