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