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