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