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