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