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