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