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