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