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