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