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