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