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