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> |
| 15 | |
| 16 | #include <common/cfgparse.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 17 | #include <common/config.h> |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 18 | #include <common/errors.h> |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 19 | #include <common/namespace.h> |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 20 | #include <common/time.h> |
| 21 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 22 | #include <types/global.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 23 | #include <types/dns.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 24 | |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 25 | #include <proto/checks.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 26 | #include <proto/port_range.h> |
| 27 | #include <proto/protocol.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 28 | #include <proto/queue.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 29 | #include <proto/raw_sock.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 30 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 31 | #include <proto/stream.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 32 | #include <proto/task.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 33 | #include <proto/dns.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 34 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 36 | /* List head of all known server keywords */ |
| 37 | static struct srv_kw_list srv_keywords = { |
| 38 | .list = LIST_HEAD_INIT(srv_keywords.list) |
| 39 | }; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 40 | |
Simon Horman | a360844 | 2013-11-01 16:46:15 +0900 | [diff] [blame] | 41 | int srv_downtime(const struct server *s) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 42 | { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 43 | 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] | 44 | return s->down_time; |
| 45 | |
| 46 | return now.tv_sec - s->last_change + s->down_time; |
| 47 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 49 | int srv_lastsession(const struct server *s) |
| 50 | { |
| 51 | if (s->counters.last_sess) |
| 52 | return now.tv_sec - s->counters.last_sess; |
| 53 | |
| 54 | return -1; |
| 55 | } |
| 56 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 57 | int srv_getinter(const struct check *check) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 58 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 59 | const struct server *s = check->server; |
| 60 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 61 | 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] | 62 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 63 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 64 | if ((s->state == SRV_ST_STOPPED) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 65 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 66 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 67 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 70 | /* |
| 71 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 72 | * parsing sessions. |
| 73 | */ |
| 74 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 75 | { |
| 76 | LIST_ADDQ(&srv_keywords.list, &kwl->list); |
| 77 | } |
| 78 | |
| 79 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 80 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 81 | * find one with a valid ->parse() function. This way it is possible to declare |
| 82 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 83 | * if some options are met. Note that if the requested keyword contains an |
| 84 | * opening parenthesis, everything from this point is ignored. |
| 85 | */ |
| 86 | struct srv_kw *srv_find_kw(const char *kw) |
| 87 | { |
| 88 | int index; |
| 89 | const char *kwend; |
| 90 | struct srv_kw_list *kwl; |
| 91 | struct srv_kw *ret = NULL; |
| 92 | |
| 93 | kwend = strchr(kw, '('); |
| 94 | if (!kwend) |
| 95 | kwend = kw + strlen(kw); |
| 96 | |
| 97 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 98 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 99 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 100 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 101 | if (kwl->kw[index].parse) |
| 102 | return &kwl->kw[index]; /* found it !*/ |
| 103 | else |
| 104 | ret = &kwl->kw[index]; /* may be OK */ |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 112 | * unsupported keywords are only dumped if their supported form was not |
| 113 | * found. |
| 114 | */ |
| 115 | void srv_dump_kws(char **out) |
| 116 | { |
| 117 | struct srv_kw_list *kwl; |
| 118 | int index; |
| 119 | |
| 120 | *out = NULL; |
| 121 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 122 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 123 | if (kwl->kw[index].parse || |
| 124 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 125 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 126 | kwl->scope, |
| 127 | kwl->kw[index].kw, |
| 128 | kwl->kw[index].skip ? " <arg>" : "", |
| 129 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 130 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 136 | /* parse the "id" server keyword */ |
| 137 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 138 | { |
| 139 | struct eb32_node *node; |
| 140 | |
| 141 | if (!*args[*cur_arg + 1]) { |
| 142 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 143 | return ERR_ALERT | ERR_FATAL; |
| 144 | } |
| 145 | |
| 146 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 147 | newsrv->conf.id.key = newsrv->puid; |
| 148 | |
| 149 | if (newsrv->puid <= 0) { |
| 150 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 151 | return ERR_ALERT | ERR_FATAL; |
| 152 | } |
| 153 | |
| 154 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 155 | if (node) { |
| 156 | struct server *target = container_of(node, struct server, conf.id); |
| 157 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 158 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 159 | target->id); |
| 160 | return ERR_ALERT | ERR_FATAL; |
| 161 | } |
| 162 | |
| 163 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
Baptiste Assmann | 7cc419a | 2015-07-07 22:02:20 +0200 | [diff] [blame] | 164 | newsrv->flags |= SRV_F_FORCED_ID; |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 168 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 169 | * 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] | 170 | * shutdown. |
| 171 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 172 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 173 | { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 174 | struct stream *stream, *stream_bck; |
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 | list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv) |
| 177 | if (stream->srv_conn == srv) |
| 178 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* 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] | 182 | * 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] | 183 | * the reason for the shutdown. |
| 184 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 185 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 186 | { |
| 187 | struct server *srv; |
| 188 | |
| 189 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 190 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 191 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 194 | /* Appends some information to a message string related to a server going UP or |
| 195 | * DOWN. If both <forced> and <reason> are null and the server tracks another |
| 196 | * one, a "via" information will be provided to know where the status came from. |
| 197 | * If <reason> is non-null, the entire string will be appended after a comma and |
| 198 | * 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] | 199 | * If <xferred> is non-negative, some information about requeued streams are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 200 | * provided. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 201 | */ |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 202 | 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] | 203 | { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 204 | if (reason) |
| 205 | chunk_appendf(msg, ", %s", reason); |
| 206 | else if (!forced && s->track) |
| 207 | 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] | 208 | |
| 209 | if (xferred >= 0) { |
| 210 | if (s->state == SRV_ST_STOPPED) |
| 211 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 212 | " %d sessions active, %d requeued, %d remaining in queue", |
| 213 | s->proxy->srv_act, s->proxy->srv_bck, |
| 214 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 215 | s->cur_sess, xferred, s->nbpend); |
| 216 | else |
| 217 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 218 | " %d sessions requeued, %d total in queue", |
| 219 | s->proxy->srv_act, s->proxy->srv_bck, |
| 220 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 221 | xferred, s->nbpend); |
| 222 | } |
| 223 | } |
| 224 | |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 225 | /* Marks server <s> down, regardless of its checks' statuses, notifies by all |
| 226 | * available means, recounts the remaining servers on the proxy and transfers |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 227 | * queued streams whenever possible to other servers. It automatically |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 228 | * recomputes the number of servers, but not the map. Maintenance servers are |
| 229 | * ignored. It reports <reason> if non-null as the reason for going down. Note |
| 230 | * that it makes use of the trash to build the log strings, so <reason> must |
| 231 | * not be placed there. |
| 232 | */ |
| 233 | void srv_set_stopped(struct server *s, const char *reason) |
| 234 | { |
| 235 | struct server *srv; |
| 236 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 237 | int srv_was_stopping = (s->state == SRV_ST_STOPPING); |
Simon Horman | 64e3416 | 2015-02-06 11:11:57 +0900 | [diff] [blame] | 238 | int log_level; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 239 | int xferred; |
| 240 | |
| 241 | if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED) |
| 242 | return; |
| 243 | |
| 244 | s->last_change = now.tv_sec; |
| 245 | s->state = SRV_ST_STOPPED; |
| 246 | if (s->proxy->lbprm.set_server_status_down) |
| 247 | s->proxy->lbprm.set_server_status_down(s); |
| 248 | |
| 249 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 250 | srv_shutdown_streams(s, SF_ERR_DOWN); |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 251 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 252 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 253 | * a connection. Those which are redispatchable will be queued |
| 254 | * to another server or to the proxy itself. |
| 255 | */ |
| 256 | xferred = pendconn_redistribute(s); |
| 257 | |
| 258 | chunk_printf(&trash, |
| 259 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 260 | s->proxy->id, s->id); |
| 261 | |
| 262 | srv_append_status(&trash, s, reason, xferred, 0); |
| 263 | Warning("%s.\n", trash.str); |
| 264 | |
| 265 | /* we don't send an alert if the server was previously paused */ |
Simon Horman | 64e3416 | 2015-02-06 11:11:57 +0900 | [diff] [blame] | 266 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
| 267 | send_log(s->proxy, log_level, "%s.\n", trash.str); |
| 268 | send_email_alert(s, log_level, "%s", trash.str); |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 269 | |
| 270 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 271 | set_backend_down(s->proxy); |
| 272 | |
| 273 | s->counters.down_trans++; |
| 274 | |
| 275 | for (srv = s->trackers; srv; srv = srv->tracknext) |
| 276 | srv_set_stopped(srv, NULL); |
| 277 | } |
| 278 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 279 | /* Marks server <s> up regardless of its checks' statuses and provided it isn't |
| 280 | * in maintenance. Notifies by all available means, recounts the remaining |
| 281 | * servers on the proxy and tries to grab requests from the proxy. It |
| 282 | * automatically recomputes the number of servers, but not the map. Maintenance |
| 283 | * servers are ignored. It reports <reason> if non-null as the reason for going |
| 284 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 285 | * must not be placed there. |
| 286 | */ |
| 287 | void srv_set_running(struct server *s, const char *reason) |
| 288 | { |
| 289 | struct server *srv; |
| 290 | int xferred; |
| 291 | |
| 292 | if (s->admin & SRV_ADMF_MAINT) |
| 293 | return; |
| 294 | |
| 295 | if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING) |
| 296 | return; |
| 297 | |
| 298 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 299 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 300 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 301 | s->proxy->last_change = now.tv_sec; |
| 302 | } |
| 303 | |
| 304 | if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times |
| 305 | s->down_time += now.tv_sec - s->last_change; |
| 306 | |
| 307 | s->last_change = now.tv_sec; |
| 308 | |
| 309 | s->state = SRV_ST_STARTING; |
| 310 | if (s->slowstart > 0) |
| 311 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 312 | else |
| 313 | s->state = SRV_ST_RUNNING; |
| 314 | |
| 315 | server_recalc_eweight(s); |
| 316 | |
| 317 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 318 | * 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] | 319 | * then it can accept new connections, so we shut down all streams |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 320 | * on all backup servers. |
| 321 | */ |
| 322 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 323 | !(s->flags & SRV_F_BACKUP) && s->eweight) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 324 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 325 | |
| 326 | /* check if we can handle some connections queued at the proxy. We |
| 327 | * will take as many as we can handle. |
| 328 | */ |
| 329 | xferred = pendconn_grab_from_px(s); |
| 330 | |
| 331 | chunk_printf(&trash, |
| 332 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 333 | s->proxy->id, s->id); |
| 334 | |
| 335 | srv_append_status(&trash, s, reason, xferred, 0); |
| 336 | Warning("%s.\n", trash.str); |
| 337 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Simon Horman | 4cd477f | 2015-04-30 13:10:34 +0900 | [diff] [blame] | 338 | send_email_alert(s, LOG_NOTICE, "%s", trash.str); |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 339 | |
| 340 | for (srv = s->trackers; srv; srv = srv->tracknext) |
| 341 | srv_set_running(srv, NULL); |
| 342 | } |
| 343 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 344 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
| 345 | * isn't in maintenance. Notifies by all available means, recounts the remaining |
| 346 | * servers on the proxy and tries to grab requests from the proxy. It |
| 347 | * automatically recomputes the number of servers, but not the map. Maintenance |
| 348 | * servers are ignored. It reports <reason> if non-null as the reason for going |
| 349 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 350 | * must not be placed there. |
| 351 | */ |
| 352 | void srv_set_stopping(struct server *s, const char *reason) |
| 353 | { |
| 354 | struct server *srv; |
| 355 | int xferred; |
| 356 | |
| 357 | if (s->admin & SRV_ADMF_MAINT) |
| 358 | return; |
| 359 | |
| 360 | if (s->state == SRV_ST_STOPPING) |
| 361 | return; |
| 362 | |
| 363 | s->last_change = now.tv_sec; |
| 364 | s->state = SRV_ST_STOPPING; |
| 365 | if (s->proxy->lbprm.set_server_status_down) |
| 366 | s->proxy->lbprm.set_server_status_down(s); |
| 367 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 368 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 369 | * a connection. Those which are redispatchable will be queued |
| 370 | * to another server or to the proxy itself. |
| 371 | */ |
| 372 | xferred = pendconn_redistribute(s); |
| 373 | |
| 374 | chunk_printf(&trash, |
| 375 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 376 | s->proxy->id, s->id); |
| 377 | |
| 378 | srv_append_status(&trash, s, reason, xferred, 0); |
| 379 | |
| 380 | Warning("%s.\n", trash.str); |
| 381 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 382 | |
| 383 | if (!s->proxy->srv_bck && !s->proxy->srv_act) |
| 384 | set_backend_down(s->proxy); |
| 385 | |
| 386 | for (srv = s->trackers; srv; srv = srv->tracknext) |
| 387 | srv_set_stopping(srv, NULL); |
| 388 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 389 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 390 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 391 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 392 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 393 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 394 | * checks). When either the flag is already set or no flag is passed, nothing |
| 395 | * is done. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 396 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 397 | void srv_set_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 398 | { |
| 399 | struct check *check = &s->check; |
| 400 | struct server *srv; |
| 401 | int xferred; |
| 402 | |
| 403 | if (!mode) |
| 404 | return; |
| 405 | |
| 406 | /* stop going down as soon as we meet a server already in the same state */ |
| 407 | if (s->admin & mode) |
| 408 | return; |
| 409 | |
| 410 | s->admin |= mode; |
| 411 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 412 | /* stop going down if the equivalent flag was already present (forced or inherited) */ |
| 413 | if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) || |
| 414 | ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN))) |
| 415 | return; |
| 416 | |
| 417 | /* Maintenance must also disable health checks */ |
| 418 | if (mode & SRV_ADMF_MAINT) { |
| 419 | if (s->check.state & CHK_ST_ENABLED) { |
| 420 | s->check.state |= CHK_ST_PAUSED; |
| 421 | check->health = 0; |
| 422 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 423 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 424 | if (s->state == SRV_ST_STOPPED) { /* server was already down */ |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 425 | chunk_printf(&trash, |
| 426 | "%sServer %s/%s was DOWN and now enters maintenance", |
| 427 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
| 428 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 429 | srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT)); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 430 | |
| 431 | Warning("%s.\n", trash.str); |
| 432 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 433 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 434 | else { /* server was still running */ |
| 435 | int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN); |
| 436 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 437 | |
| 438 | check->health = 0; /* failure */ |
| 439 | s->last_change = now.tv_sec; |
| 440 | s->state = SRV_ST_STOPPED; |
| 441 | if (s->proxy->lbprm.set_server_status_down) |
| 442 | s->proxy->lbprm.set_server_status_down(s); |
| 443 | |
| 444 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 445 | srv_shutdown_streams(s, SF_ERR_DOWN); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 446 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 447 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 448 | * a connection. Those which are redispatchable will be queued |
| 449 | * to another server or to the proxy itself. |
| 450 | */ |
| 451 | xferred = pendconn_redistribute(s); |
| 452 | |
| 453 | chunk_printf(&trash, |
| 454 | "%sServer %s/%s is going DOWN for maintenance", |
| 455 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 456 | s->proxy->id, s->id); |
| 457 | |
| 458 | srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT)); |
| 459 | |
| 460 | Warning("%s.\n", trash.str); |
| 461 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str); |
| 462 | |
| 463 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 464 | set_backend_down(s->proxy); |
| 465 | |
| 466 | s->counters.down_trans++; |
| 467 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 468 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 469 | |
| 470 | /* drain state is applied only if not yet in maint */ |
| 471 | if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 472 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 473 | |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 474 | s->last_change = now.tv_sec; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 475 | if (s->proxy->lbprm.set_server_status_down) |
| 476 | s->proxy->lbprm.set_server_status_down(s); |
| 477 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 478 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 479 | * a connection. Those which are redispatchable will be queued |
| 480 | * to another server or to the proxy itself. |
| 481 | */ |
| 482 | xferred = pendconn_redistribute(s); |
| 483 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 484 | chunk_printf(&trash, "%sServer %s/%s enters drain state", |
| 485 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 486 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 487 | srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN)); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 488 | |
| 489 | Warning("%s.\n", trash.str); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 490 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Simon Horman | 4cd477f | 2015-04-30 13:10:34 +0900 | [diff] [blame] | 491 | send_email_alert(s, LOG_NOTICE, "%s", trash.str); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 492 | |
| 493 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 494 | set_backend_down(s->proxy); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 497 | /* compute the inherited flag to propagate */ |
| 498 | if (mode & SRV_ADMF_MAINT) |
| 499 | mode = SRV_ADMF_IMAINT; |
| 500 | else if (mode & SRV_ADMF_DRAIN) |
| 501 | mode = SRV_ADMF_IDRAIN; |
| 502 | |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 503 | for (srv = s->trackers; srv; srv = srv->tracknext) |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 504 | srv_set_admin_flag(srv, mode); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 507 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 508 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 509 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 510 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 511 | * 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] | 512 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 513 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 514 | { |
| 515 | struct check *check = &s->check; |
| 516 | struct server *srv; |
| 517 | int xferred = -1; |
| 518 | |
| 519 | if (!mode) |
| 520 | return; |
| 521 | |
| 522 | /* stop going down as soon as we see the flag is not there anymore */ |
| 523 | if (!(s->admin & mode)) |
| 524 | return; |
| 525 | |
| 526 | s->admin &= ~mode; |
| 527 | |
| 528 | if (s->admin & SRV_ADMF_MAINT) { |
| 529 | /* remaining in maintenance mode, let's inform precisely about the |
| 530 | * situation. |
| 531 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 532 | if (mode & SRV_ADMF_FMAINT) { |
| 533 | chunk_printf(&trash, |
| 534 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 535 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 536 | s->proxy->id, s->id); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 537 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 538 | if (s->track) /* normally it's mandatory here */ |
| 539 | chunk_appendf(&trash, " via %s/%s", |
| 540 | s->track->proxy->id, s->track->id); |
| 541 | Warning("%s.\n", trash.str); |
| 542 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 543 | } |
| 544 | else if (mode & SRV_ADMF_IMAINT) { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 545 | chunk_printf(&trash, |
| 546 | "%sServer %s/%s remains in forced maintenance", |
| 547 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 548 | s->proxy->id, s->id); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 549 | Warning("%s.\n", trash.str); |
| 550 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 551 | } |
| 552 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
| 553 | } |
| 554 | else if (mode & SRV_ADMF_MAINT) { |
| 555 | /* OK here we're leaving maintenance, we have many things to check, |
| 556 | * because the server might possibly be coming back up depending on |
| 557 | * its state. In practice, leaving maintenance means that we should |
| 558 | * immediately turn to UP (more or less the slowstart) under the |
| 559 | * following conditions : |
| 560 | * - server is neither checked nor tracked |
| 561 | * - server tracks another server which is not checked |
| 562 | * - server tracks another server which is already up |
| 563 | * Which sums up as something simpler : |
| 564 | * "either the tracking server is up or the server's checks are disabled |
| 565 | * or up". Otherwise we only re-enable health checks. There's a special |
| 566 | * case associated to the stopping state which can be inherited. Note |
| 567 | * that the server might still be in drain mode, which is naturally dealt |
| 568 | * with by the lower level functions. |
| 569 | */ |
| 570 | |
| 571 | if (s->check.state & CHK_ST_ENABLED) { |
| 572 | s->check.state &= ~CHK_ST_PAUSED; |
| 573 | check->health = check->rise; /* start OK but check immediately */ |
| 574 | } |
| 575 | |
| 576 | if ((!s->track || s->track->state != SRV_ST_STOPPED) && |
| 577 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 578 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 579 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 580 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 581 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 582 | s->proxy->last_change = now.tv_sec; |
| 583 | } |
| 584 | |
| 585 | if (s->last_change < now.tv_sec) // ignore negative times |
| 586 | s->down_time += now.tv_sec - s->last_change; |
| 587 | s->last_change = now.tv_sec; |
| 588 | |
| 589 | if (s->track && s->track->state == SRV_ST_STOPPING) |
| 590 | s->state = SRV_ST_STOPPING; |
| 591 | else { |
| 592 | s->state = SRV_ST_STARTING; |
| 593 | if (s->slowstart > 0) |
| 594 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 595 | else |
| 596 | s->state = SRV_ST_RUNNING; |
| 597 | } |
| 598 | |
| 599 | server_recalc_eweight(s); |
| 600 | |
| 601 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 602 | * 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] | 603 | * then it can accept new connections, so we shut down all streams |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 604 | * on all backup servers. |
| 605 | */ |
| 606 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 607 | !(s->flags & SRV_F_BACKUP) && s->eweight) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 608 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 609 | |
| 610 | /* check if we can handle some connections queued at the proxy. We |
| 611 | * will take as many as we can handle. |
| 612 | */ |
| 613 | xferred = pendconn_grab_from_px(s); |
| 614 | } |
| 615 | |
| 616 | if (mode & SRV_ADMF_FMAINT) { |
| 617 | chunk_printf(&trash, |
| 618 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 619 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 620 | s->proxy->id, s->id, |
| 621 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 622 | (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 623 | } |
| 624 | else { |
| 625 | chunk_printf(&trash, |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 626 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 627 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 628 | s->proxy->id, s->id, |
| 629 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 630 | (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 631 | srv_append_status(&trash, s, NULL, xferred, 0); |
| 632 | } |
| 633 | Warning("%s.\n", trash.str); |
| 634 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 635 | } |
| 636 | else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) { |
| 637 | /* remaining in drain mode after removing one of its flags */ |
| 638 | |
| 639 | if (mode & SRV_ADMF_FDRAIN) { |
| 640 | chunk_printf(&trash, |
| 641 | "%sServer %s/%s is leaving forced drain but remains in drain mode", |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 642 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 643 | s->proxy->id, s->id); |
| 644 | |
| 645 | if (s->track) /* normally it's mandatory here */ |
| 646 | chunk_appendf(&trash, " via %s/%s", |
| 647 | s->track->proxy->id, s->track->id); |
| 648 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 649 | else { |
| 650 | chunk_printf(&trash, |
| 651 | "%sServer %s/%s remains in forced drain mode", |
| 652 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 653 | s->proxy->id, s->id); |
| 654 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 655 | Warning("%s.\n", trash.str); |
| 656 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 657 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 658 | else if (mode & SRV_ADMF_DRAIN) { |
| 659 | /* OK completely leaving drain mode */ |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 660 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 661 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 662 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 663 | s->proxy->last_change = now.tv_sec; |
| 664 | } |
| 665 | |
| 666 | if (s->last_change < now.tv_sec) // ignore negative times |
| 667 | s->down_time += now.tv_sec - s->last_change; |
| 668 | s->last_change = now.tv_sec; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 669 | server_recalc_eweight(s); |
| 670 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 671 | if (mode & SRV_ADMF_FDRAIN) { |
| 672 | chunk_printf(&trash, |
| 673 | "%sServer %s/%s is %s (leaving forced drain)", |
| 674 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 675 | s->proxy->id, s->id, |
| 676 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 677 | } |
| 678 | else { |
| 679 | chunk_printf(&trash, |
| 680 | "%sServer %s/%s is %s (leaving drain)", |
| 681 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 682 | s->proxy->id, s->id, |
| 683 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 684 | if (s->track) /* normally it's mandatory here */ |
| 685 | chunk_appendf(&trash, " via %s/%s", |
| 686 | s->track->proxy->id, s->track->id); |
| 687 | } |
| 688 | Warning("%s.\n", trash.str); |
| 689 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 692 | /* stop going down if the equivalent flag is still present (forced or inherited) */ |
| 693 | if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) || |
| 694 | ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN))) |
| 695 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 696 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 697 | if (mode & SRV_ADMF_MAINT) |
| 698 | mode = SRV_ADMF_IMAINT; |
| 699 | else if (mode & SRV_ADMF_DRAIN) |
| 700 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 701 | |
| 702 | for (srv = s->trackers; srv; srv = srv->tracknext) |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 703 | srv_clr_admin_flag(srv, mode); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 706 | /* Note: must not be declared <const> as its list will be overwritten. |
| 707 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 708 | * all code contributors. |
| 709 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 710 | * the config parser can report an appropriate error when a known keyword was |
| 711 | * not enabled. |
| 712 | */ |
| 713 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
| 714 | { "id", srv_parse_id, 1, 0 }, /* set id# of server */ |
| 715 | { NULL, NULL, 0 }, |
| 716 | }}; |
| 717 | |
| 718 | __attribute__((constructor)) |
| 719 | static void __listener_init(void) |
| 720 | { |
| 721 | srv_register_keywords(&srv_kws); |
| 722 | } |
| 723 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 724 | /* Recomputes the server's eweight based on its state, uweight, the current time, |
| 725 | * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup |
| 726 | * state is automatically disabled if the time is elapsed. |
| 727 | */ |
| 728 | void server_recalc_eweight(struct server *sv) |
| 729 | { |
| 730 | struct proxy *px = sv->proxy; |
| 731 | unsigned w; |
| 732 | |
| 733 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 734 | /* go to full throttle if the slowstart interval is reached */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 735 | if (sv->state == SRV_ST_STARTING) |
| 736 | sv->state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 740 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 741 | */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 742 | 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] | 743 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 744 | else |
| 745 | w = px->lbprm.wdiv; |
| 746 | |
| 747 | sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult; |
| 748 | |
| 749 | /* now propagate the status change to any LB algorithms */ |
| 750 | if (px->lbprm.update_server_eweight) |
| 751 | px->lbprm.update_server_eweight(sv); |
Willy Tarreau | 9943d31 | 2014-05-22 16:20:59 +0200 | [diff] [blame] | 752 | else if (srv_is_usable(sv)) { |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 753 | if (px->lbprm.set_server_status_up) |
| 754 | px->lbprm.set_server_status_up(sv); |
| 755 | } |
| 756 | else { |
| 757 | if (px->lbprm.set_server_status_down) |
| 758 | px->lbprm.set_server_status_down(sv); |
| 759 | } |
| 760 | } |
| 761 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 762 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 763 | * Parses weight_str and configures sv accordingly. |
| 764 | * Returns NULL on success, error message string otherwise. |
| 765 | */ |
| 766 | const char *server_parse_weight_change_request(struct server *sv, |
| 767 | const char *weight_str) |
| 768 | { |
| 769 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 770 | long int w; |
| 771 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 772 | |
| 773 | px = sv->proxy; |
| 774 | |
| 775 | /* if the weight is terminated with '%', it is set relative to |
| 776 | * the initial weight, otherwise it is absolute. |
| 777 | */ |
| 778 | if (!*weight_str) |
| 779 | return "Require <weight> or <weight%>.\n"; |
| 780 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 781 | w = strtol(weight_str, &end, 10); |
| 782 | if (end == weight_str) |
| 783 | return "Empty weight string empty or preceded by garbage"; |
| 784 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 785 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 786 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 787 | /* Avoid integer overflow */ |
| 788 | if (w > 25600) |
| 789 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 790 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 791 | if (w > 256) |
| 792 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 793 | } |
| 794 | else if (w < 0 || w > 256) |
| 795 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 796 | else if (end[0] != '\0') |
| 797 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 798 | |
| 799 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 800 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 801 | |
| 802 | sv->uweight = w; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 803 | server_recalc_eweight(sv); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 804 | |
| 805 | return NULL; |
| 806 | } |
| 807 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 808 | /* |
| 809 | * Parses <addr_str> and configures <sv> accordingly. |
| 810 | * Returns: |
| 811 | * - error string on error |
| 812 | * - NULL on success |
| 813 | */ |
| 814 | const char *server_parse_addr_change_request(struct server *sv, |
| 815 | const char *addr_str) |
| 816 | { |
| 817 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 818 | |
| 819 | if (inet_pton(AF_INET6, addr_str, ip)) { |
| 820 | update_server_addr(sv, ip, AF_INET6, "stats command\n"); |
| 821 | return NULL; |
| 822 | } |
| 823 | if (inet_pton(AF_INET, addr_str, ip)) { |
| 824 | update_server_addr(sv, ip, AF_INET, "stats command\n"); |
| 825 | return NULL; |
| 826 | } |
| 827 | |
| 828 | return "Could not understand IP address format.\n"; |
| 829 | } |
| 830 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 831 | int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy) |
| 832 | { |
| 833 | struct server *newsrv = NULL; |
| 834 | const char *err; |
| 835 | char *errmsg = NULL; |
| 836 | int err_code = 0; |
| 837 | unsigned val; |
| 838 | |
| 839 | if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */ |
| 840 | int cur_arg; |
| 841 | short realport = 0; |
| 842 | int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd'); |
| 843 | |
| 844 | if (!defsrv && curproxy == defproxy) { |
| 845 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 846 | err_code |= ERR_ALERT | ERR_FATAL; |
| 847 | goto out; |
| 848 | } |
| 849 | else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) |
| 850 | err_code |= ERR_ALERT | ERR_FATAL; |
| 851 | |
| 852 | if (!*args[2]) { |
| 853 | Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 854 | file, linenum, args[0]); |
| 855 | err_code |= ERR_ALERT | ERR_FATAL; |
| 856 | goto out; |
| 857 | } |
| 858 | |
| 859 | err = invalid_char(args[1]); |
| 860 | if (err && !defsrv) { |
| 861 | Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n", |
| 862 | file, linenum, *err, args[1]); |
| 863 | err_code |= ERR_ALERT | ERR_FATAL; |
| 864 | goto out; |
| 865 | } |
| 866 | |
| 867 | if (!defsrv) { |
| 868 | struct sockaddr_storage *sk; |
| 869 | int port1, port2; |
| 870 | struct protocol *proto; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 871 | struct dns_resolution *curr_resolution; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 872 | |
| 873 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 874 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 875 | err_code |= ERR_ALERT | ERR_ABORT; |
| 876 | goto out; |
| 877 | } |
| 878 | |
| 879 | /* the servers are linked backwards first */ |
| 880 | newsrv->next = curproxy->srv; |
| 881 | curproxy->srv = newsrv; |
| 882 | newsrv->proxy = curproxy; |
| 883 | newsrv->conf.file = strdup(file); |
| 884 | newsrv->conf.line = linenum; |
| 885 | |
| 886 | newsrv->obj_type = OBJ_TYPE_SERVER; |
| 887 | LIST_INIT(&newsrv->actconns); |
| 888 | LIST_INIT(&newsrv->pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 889 | LIST_INIT(&newsrv->priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 890 | LIST_INIT(&newsrv->idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 891 | LIST_INIT(&newsrv->safe_conns); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 892 | do_check = 0; |
| 893 | do_agent = 0; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 894 | newsrv->flags = 0; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 895 | newsrv->admin = 0; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 896 | newsrv->state = SRV_ST_RUNNING; /* early server setup */ |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 897 | newsrv->last_change = now.tv_sec; |
| 898 | newsrv->id = strdup(args[1]); |
| 899 | |
| 900 | /* several ways to check the port component : |
| 901 | * - IP => port=+0, relative (IPv4 only) |
| 902 | * - IP: => port=+0, relative |
| 903 | * - IP:N => port=N, absolute |
| 904 | * - IP:+N => port=+N, relative |
| 905 | * - IP:-N => port=-N, relative |
| 906 | */ |
| 907 | sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL); |
| 908 | if (!sk) { |
| 909 | Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); |
| 910 | err_code |= ERR_ALERT | ERR_FATAL; |
| 911 | goto out; |
| 912 | } |
| 913 | |
| 914 | proto = protocol_by_family(sk->ss_family); |
| 915 | if (!proto || !proto->connect) { |
| 916 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 917 | file, linenum, args[0], args[1]); |
| 918 | err_code |= ERR_ALERT | ERR_FATAL; |
| 919 | goto out; |
| 920 | } |
| 921 | |
| 922 | if (!port1 || !port2) { |
| 923 | /* no port specified, +offset, -offset */ |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 924 | newsrv->flags |= SRV_F_MAPPORTS; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 925 | } |
| 926 | else if (port1 != port2) { |
| 927 | /* port range */ |
| 928 | Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n", |
| 929 | file, linenum, args[0], args[1], args[2]); |
| 930 | err_code |= ERR_ALERT | ERR_FATAL; |
| 931 | goto out; |
| 932 | } |
| 933 | else { |
| 934 | /* used by checks */ |
| 935 | realport = port1; |
| 936 | } |
| 937 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 938 | /* save hostname and create associated name resolution */ |
| 939 | switch (sk->ss_family) { |
| 940 | case AF_INET: { |
| 941 | /* remove the port if any */ |
| 942 | char *c; |
| 943 | if ((c = rindex(args[2], ':')) != NULL) { |
| 944 | newsrv->hostname = my_strndup(args[2], c - args[2]); |
| 945 | } |
| 946 | else { |
| 947 | newsrv->hostname = strdup(args[2]); |
| 948 | } |
| 949 | } |
| 950 | break; |
| 951 | case AF_INET6: |
| 952 | newsrv->hostname = strdup(args[2]); |
| 953 | break; |
| 954 | default: |
| 955 | goto skip_name_resolution; |
| 956 | } |
| 957 | |
| 958 | /* no name resolution if an IP has been provided */ |
| 959 | if (inet_pton(sk->ss_family, newsrv->hostname, trash.str) == 1) |
| 960 | goto skip_name_resolution; |
| 961 | |
| 962 | if ((curr_resolution = calloc(1, sizeof(struct dns_resolution))) == NULL) |
| 963 | goto skip_name_resolution; |
| 964 | |
| 965 | curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname); |
| 966 | if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL) |
| 967 | goto skip_name_resolution; |
| 968 | if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) { |
| 969 | Alert("parsing [%s:%d] : Invalid hostname '%s'\n", |
| 970 | file, linenum, args[2]); |
| 971 | err_code |= ERR_ALERT | ERR_FATAL; |
| 972 | goto out; |
| 973 | } |
| 974 | |
| 975 | curr_resolution->requester = newsrv; |
| 976 | curr_resolution->requester_cb = snr_resolution_cb; |
| 977 | curr_resolution->requester_error_cb = snr_resolution_error_cb; |
| 978 | curr_resolution->status = RSLV_STATUS_NONE; |
| 979 | curr_resolution->step = RSLV_STEP_NONE; |
| 980 | /* a first resolution has been done by the configuration parser */ |
| 981 | curr_resolution->last_resolution = now_ms; |
| 982 | newsrv->resolution = curr_resolution; |
| 983 | |
| 984 | skip_name_resolution: |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 985 | newsrv->addr = *sk; |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 986 | newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 987 | |
Thierry FOURNIER | bb2ae64 | 2015-01-14 11:31:49 +0100 | [diff] [blame] | 988 | if (!protocol_by_family(newsrv->addr.ss_family)) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 989 | Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n", |
| 990 | file, linenum, newsrv->addr.ss_family, args[2]); |
| 991 | err_code |= ERR_ALERT | ERR_FATAL; |
| 992 | goto out; |
| 993 | } |
| 994 | |
| 995 | newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl; |
| 996 | newsrv->check.port = curproxy->defsrv.check.port; |
| 997 | newsrv->check.inter = curproxy->defsrv.check.inter; |
| 998 | newsrv->check.fastinter = curproxy->defsrv.check.fastinter; |
| 999 | newsrv->check.downinter = curproxy->defsrv.check.downinter; |
| 1000 | newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl; |
| 1001 | newsrv->agent.port = curproxy->defsrv.agent.port; |
| 1002 | newsrv->agent.inter = curproxy->defsrv.agent.inter; |
| 1003 | newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter; |
| 1004 | newsrv->agent.downinter = curproxy->defsrv.agent.downinter; |
| 1005 | newsrv->maxqueue = curproxy->defsrv.maxqueue; |
| 1006 | newsrv->minconn = curproxy->defsrv.minconn; |
| 1007 | newsrv->maxconn = curproxy->defsrv.maxconn; |
| 1008 | newsrv->slowstart = curproxy->defsrv.slowstart; |
| 1009 | newsrv->onerror = curproxy->defsrv.onerror; |
| 1010 | newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown; |
| 1011 | newsrv->onmarkedup = curproxy->defsrv.onmarkedup; |
| 1012 | newsrv->consecutive_errors_limit |
| 1013 | = curproxy->defsrv.consecutive_errors_limit; |
| 1014 | #ifdef OPENSSL |
| 1015 | newsrv->use_ssl = curproxy->defsrv.use_ssl; |
| 1016 | #endif |
| 1017 | newsrv->uweight = newsrv->iweight |
| 1018 | = curproxy->defsrv.iweight; |
| 1019 | |
| 1020 | newsrv->check.status = HCHK_STATUS_INI; |
| 1021 | newsrv->check.rise = curproxy->defsrv.check.rise; |
| 1022 | newsrv->check.fall = curproxy->defsrv.check.fall; |
| 1023 | newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */ |
| 1024 | newsrv->check.server = newsrv; |
Simon Horman | e16c1b3 | 2015-01-30 11:22:57 +0900 | [diff] [blame] | 1025 | newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1026 | |
| 1027 | newsrv->agent.status = HCHK_STATUS_INI; |
| 1028 | newsrv->agent.rise = curproxy->defsrv.agent.rise; |
| 1029 | newsrv->agent.fall = curproxy->defsrv.agent.fall; |
| 1030 | newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */ |
| 1031 | newsrv->agent.server = newsrv; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1032 | newsrv->resolver_family_priority = curproxy->defsrv.resolver_family_priority; |
| 1033 | if (newsrv->resolver_family_priority == AF_UNSPEC) |
| 1034 | newsrv->resolver_family_priority = AF_INET6; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1035 | |
| 1036 | cur_arg = 3; |
| 1037 | } else { |
| 1038 | newsrv = &curproxy->defsrv; |
| 1039 | cur_arg = 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1040 | newsrv->resolver_family_priority = AF_INET6; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | while (*args[cur_arg]) { |
| 1044 | if (!strcmp(args[cur_arg], "agent-check")) { |
| 1045 | global.maxsock++; |
| 1046 | do_agent = 1; |
| 1047 | cur_arg += 1; |
| 1048 | } else if (!strcmp(args[cur_arg], "agent-inter")) { |
| 1049 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1050 | if (err) { |
| 1051 | Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n", |
| 1052 | file, linenum, *err, newsrv->id); |
| 1053 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1054 | goto out; |
| 1055 | } |
| 1056 | if (val <= 0) { |
| 1057 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 1058 | file, linenum, val, args[cur_arg], newsrv->id); |
| 1059 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1060 | goto out; |
| 1061 | } |
| 1062 | newsrv->agent.inter = val; |
| 1063 | cur_arg += 2; |
| 1064 | } |
| 1065 | else if (!strcmp(args[cur_arg], "agent-port")) { |
| 1066 | global.maxsock++; |
| 1067 | newsrv->agent.port = atol(args[cur_arg + 1]); |
| 1068 | cur_arg += 2; |
| 1069 | } |
| 1070 | else if (!defsrv && !strcmp(args[cur_arg], "cookie")) { |
| 1071 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 1072 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 1073 | cur_arg += 2; |
| 1074 | } |
| 1075 | else if (!defsrv && !strcmp(args[cur_arg], "redir")) { |
| 1076 | newsrv->rdr_pfx = strdup(args[cur_arg + 1]); |
| 1077 | newsrv->rdr_len = strlen(args[cur_arg + 1]); |
| 1078 | cur_arg += 2; |
| 1079 | } |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1080 | else if (!strcmp(args[cur_arg], "resolvers")) { |
| 1081 | newsrv->resolvers_id = strdup(args[cur_arg + 1]); |
| 1082 | cur_arg += 2; |
| 1083 | } |
| 1084 | else if (!strcmp(args[cur_arg], "resolve-prefer")) { |
| 1085 | if (!strcmp(args[cur_arg + 1], "ipv4")) |
| 1086 | newsrv->resolver_family_priority = AF_INET; |
| 1087 | else if (!strcmp(args[cur_arg + 1], "ipv6")) |
| 1088 | newsrv->resolver_family_priority = AF_INET6; |
| 1089 | else { |
| 1090 | Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n", |
| 1091 | file, linenum, args[cur_arg]); |
| 1092 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1093 | goto out; |
| 1094 | } |
| 1095 | cur_arg += 2; |
| 1096 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1097 | else if (!strcmp(args[cur_arg], "rise")) { |
| 1098 | if (!*args[cur_arg + 1]) { |
| 1099 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 1100 | file, linenum, args[cur_arg]); |
| 1101 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1102 | goto out; |
| 1103 | } |
| 1104 | |
| 1105 | newsrv->check.rise = atol(args[cur_arg + 1]); |
| 1106 | if (newsrv->check.rise <= 0) { |
| 1107 | Alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
| 1108 | file, linenum, args[cur_arg]); |
| 1109 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1110 | goto out; |
| 1111 | } |
| 1112 | |
| 1113 | if (newsrv->check.health) |
| 1114 | newsrv->check.health = newsrv->check.rise; |
| 1115 | cur_arg += 2; |
| 1116 | } |
| 1117 | else if (!strcmp(args[cur_arg], "fall")) { |
| 1118 | newsrv->check.fall = atol(args[cur_arg + 1]); |
| 1119 | |
| 1120 | if (!*args[cur_arg + 1]) { |
| 1121 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 1122 | file, linenum, args[cur_arg]); |
| 1123 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1124 | goto out; |
| 1125 | } |
| 1126 | |
| 1127 | if (newsrv->check.fall <= 0) { |
| 1128 | Alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
| 1129 | file, linenum, args[cur_arg]); |
| 1130 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | cur_arg += 2; |
| 1135 | } |
| 1136 | else if (!strcmp(args[cur_arg], "inter")) { |
| 1137 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1138 | if (err) { |
| 1139 | Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n", |
| 1140 | file, linenum, *err, newsrv->id); |
| 1141 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1142 | goto out; |
| 1143 | } |
| 1144 | if (val <= 0) { |
| 1145 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 1146 | file, linenum, val, args[cur_arg], newsrv->id); |
| 1147 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1148 | goto out; |
| 1149 | } |
| 1150 | newsrv->check.inter = val; |
| 1151 | cur_arg += 2; |
| 1152 | } |
| 1153 | else if (!strcmp(args[cur_arg], "fastinter")) { |
| 1154 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1155 | if (err) { |
| 1156 | Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n", |
| 1157 | file, linenum, *err, newsrv->id); |
| 1158 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1159 | goto out; |
| 1160 | } |
| 1161 | if (val <= 0) { |
| 1162 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 1163 | file, linenum, val, args[cur_arg], newsrv->id); |
| 1164 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1165 | goto out; |
| 1166 | } |
| 1167 | newsrv->check.fastinter = val; |
| 1168 | cur_arg += 2; |
| 1169 | } |
| 1170 | else if (!strcmp(args[cur_arg], "downinter")) { |
| 1171 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1172 | if (err) { |
| 1173 | Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n", |
| 1174 | file, linenum, *err, newsrv->id); |
| 1175 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1176 | goto out; |
| 1177 | } |
| 1178 | if (val <= 0) { |
| 1179 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 1180 | file, linenum, val, args[cur_arg], newsrv->id); |
| 1181 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1182 | goto out; |
| 1183 | } |
| 1184 | newsrv->check.downinter = val; |
| 1185 | cur_arg += 2; |
| 1186 | } |
| 1187 | else if (!defsrv && !strcmp(args[cur_arg], "addr")) { |
| 1188 | struct sockaddr_storage *sk; |
| 1189 | int port1, port2; |
| 1190 | struct protocol *proto; |
| 1191 | |
| 1192 | sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL); |
| 1193 | if (!sk) { |
| 1194 | Alert("parsing [%s:%d] : '%s' : %s\n", |
| 1195 | file, linenum, args[cur_arg], errmsg); |
| 1196 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1197 | goto out; |
| 1198 | } |
| 1199 | |
| 1200 | proto = protocol_by_family(sk->ss_family); |
| 1201 | if (!proto || !proto->connect) { |
| 1202 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 1203 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1204 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1205 | goto out; |
| 1206 | } |
| 1207 | |
| 1208 | if (port1 != port2) { |
| 1209 | Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n", |
| 1210 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1211 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1212 | goto out; |
| 1213 | } |
| 1214 | |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1215 | newsrv->check.addr = newsrv->agent.addr = *sk; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1216 | cur_arg += 2; |
| 1217 | } |
| 1218 | else if (!strcmp(args[cur_arg], "port")) { |
| 1219 | newsrv->check.port = atol(args[cur_arg + 1]); |
| 1220 | cur_arg += 2; |
| 1221 | } |
| 1222 | else if (!defsrv && !strcmp(args[cur_arg], "backup")) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 1223 | newsrv->flags |= SRV_F_BACKUP; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1224 | cur_arg ++; |
| 1225 | } |
| 1226 | else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 1227 | newsrv->flags |= SRV_F_NON_STICK; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1228 | cur_arg ++; |
| 1229 | } |
| 1230 | else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) { |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1231 | newsrv->pp_opts |= SRV_PP_V1; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1232 | cur_arg ++; |
| 1233 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1234 | else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) { |
| 1235 | newsrv->pp_opts |= SRV_PP_V2; |
| 1236 | cur_arg ++; |
| 1237 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1238 | else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) { |
| 1239 | newsrv->check.send_proxy = 1; |
| 1240 | cur_arg ++; |
| 1241 | } |
| 1242 | else if (!strcmp(args[cur_arg], "weight")) { |
| 1243 | int w; |
| 1244 | w = atol(args[cur_arg + 1]); |
| 1245 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 1246 | Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n", |
| 1247 | file, linenum, newsrv->id, SRV_UWGHT_MAX, w); |
| 1248 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1249 | goto out; |
| 1250 | } |
| 1251 | newsrv->uweight = newsrv->iweight = w; |
| 1252 | cur_arg += 2; |
| 1253 | } |
| 1254 | else if (!strcmp(args[cur_arg], "minconn")) { |
| 1255 | newsrv->minconn = atol(args[cur_arg + 1]); |
| 1256 | cur_arg += 2; |
| 1257 | } |
| 1258 | else if (!strcmp(args[cur_arg], "maxconn")) { |
| 1259 | newsrv->maxconn = atol(args[cur_arg + 1]); |
| 1260 | cur_arg += 2; |
| 1261 | } |
| 1262 | else if (!strcmp(args[cur_arg], "maxqueue")) { |
| 1263 | newsrv->maxqueue = atol(args[cur_arg + 1]); |
| 1264 | cur_arg += 2; |
| 1265 | } |
| 1266 | else if (!strcmp(args[cur_arg], "slowstart")) { |
| 1267 | /* slowstart is stored in seconds */ |
| 1268 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1269 | if (err) { |
| 1270 | Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n", |
| 1271 | file, linenum, *err, newsrv->id); |
| 1272 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1273 | goto out; |
| 1274 | } |
| 1275 | newsrv->slowstart = (val + 999) / 1000; |
| 1276 | cur_arg += 2; |
| 1277 | } |
| 1278 | else if (!defsrv && !strcmp(args[cur_arg], "track")) { |
| 1279 | |
| 1280 | if (!*args[cur_arg + 1]) { |
| 1281 | Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n", |
| 1282 | file, linenum); |
| 1283 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1284 | goto out; |
| 1285 | } |
| 1286 | |
| 1287 | newsrv->trackit = strdup(args[cur_arg + 1]); |
| 1288 | |
| 1289 | cur_arg += 2; |
| 1290 | } |
| 1291 | else if (!defsrv && !strcmp(args[cur_arg], "check")) { |
| 1292 | global.maxsock++; |
| 1293 | do_check = 1; |
| 1294 | cur_arg += 1; |
| 1295 | } |
| 1296 | else if (!defsrv && !strcmp(args[cur_arg], "disabled")) { |
Baptiste Assmann | 9f5ada3 | 2015-08-08 15:49:13 +0200 | [diff] [blame] | 1297 | newsrv->admin |= SRV_ADMF_CMAINT; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1298 | newsrv->state = SRV_ST_STOPPED; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1299 | newsrv->check.state |= CHK_ST_PAUSED; |
| 1300 | newsrv->check.health = 0; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1301 | cur_arg += 1; |
| 1302 | } |
| 1303 | else if (!defsrv && !strcmp(args[cur_arg], "observe")) { |
| 1304 | if (!strcmp(args[cur_arg + 1], "none")) |
| 1305 | newsrv->observe = HANA_OBS_NONE; |
| 1306 | else if (!strcmp(args[cur_arg + 1], "layer4")) |
| 1307 | newsrv->observe = HANA_OBS_LAYER4; |
| 1308 | else if (!strcmp(args[cur_arg + 1], "layer7")) { |
| 1309 | if (curproxy->mode != PR_MODE_HTTP) { |
| 1310 | Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n", |
| 1311 | file, linenum, args[cur_arg + 1]); |
| 1312 | err_code |= ERR_ALERT; |
| 1313 | } |
| 1314 | newsrv->observe = HANA_OBS_LAYER7; |
| 1315 | } |
| 1316 | else { |
| 1317 | Alert("parsing [%s:%d]: '%s' expects one of 'none', " |
| 1318 | "'layer4', 'layer7' but got '%s'\n", |
| 1319 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1320 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1321 | goto out; |
| 1322 | } |
| 1323 | |
| 1324 | cur_arg += 2; |
| 1325 | } |
| 1326 | else if (!strcmp(args[cur_arg], "on-error")) { |
| 1327 | if (!strcmp(args[cur_arg + 1], "fastinter")) |
| 1328 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 1329 | else if (!strcmp(args[cur_arg + 1], "fail-check")) |
| 1330 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 1331 | else if (!strcmp(args[cur_arg + 1], "sudden-death")) |
| 1332 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 1333 | else if (!strcmp(args[cur_arg + 1], "mark-down")) |
| 1334 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 1335 | else { |
| 1336 | Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', " |
| 1337 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n", |
| 1338 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1339 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1340 | goto out; |
| 1341 | } |
| 1342 | |
| 1343 | cur_arg += 2; |
| 1344 | } |
| 1345 | else if (!strcmp(args[cur_arg], "on-marked-down")) { |
| 1346 | if (!strcmp(args[cur_arg + 1], "shutdown-sessions")) |
| 1347 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 1348 | else { |
| 1349 | Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n", |
| 1350 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1351 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1352 | goto out; |
| 1353 | } |
| 1354 | |
| 1355 | cur_arg += 2; |
| 1356 | } |
| 1357 | else if (!strcmp(args[cur_arg], "on-marked-up")) { |
| 1358 | if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions")) |
| 1359 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 1360 | else { |
| 1361 | Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n", |
| 1362 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1363 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1364 | goto out; |
| 1365 | } |
| 1366 | |
| 1367 | cur_arg += 2; |
| 1368 | } |
| 1369 | else if (!strcmp(args[cur_arg], "error-limit")) { |
| 1370 | if (!*args[cur_arg + 1]) { |
| 1371 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 1372 | file, linenum, args[cur_arg]); |
| 1373 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1374 | goto out; |
| 1375 | } |
| 1376 | |
| 1377 | newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]); |
| 1378 | |
| 1379 | if (newsrv->consecutive_errors_limit <= 0) { |
| 1380 | Alert("parsing [%s:%d]: %s has to be > 0.\n", |
| 1381 | file, linenum, args[cur_arg]); |
| 1382 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1383 | goto out; |
| 1384 | } |
| 1385 | cur_arg += 2; |
| 1386 | } |
| 1387 | else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 1388 | int port_low, port_high; |
| 1389 | struct sockaddr_storage *sk; |
| 1390 | struct protocol *proto; |
| 1391 | |
| 1392 | if (!*args[cur_arg + 1]) { |
| 1393 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n", |
| 1394 | file, linenum, "source", "usesrc", "interface"); |
| 1395 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1396 | goto out; |
| 1397 | } |
| 1398 | |
| 1399 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 1400 | sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL); |
| 1401 | if (!sk) { |
| 1402 | Alert("parsing [%s:%d] : '%s %s' : %s\n", |
| 1403 | file, linenum, args[cur_arg], args[cur_arg+1], errmsg); |
| 1404 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1405 | goto out; |
| 1406 | } |
| 1407 | |
| 1408 | proto = protocol_by_family(sk->ss_family); |
| 1409 | if (!proto || !proto->connect) { |
| 1410 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 1411 | file, linenum, args[cur_arg], args[cur_arg+1]); |
| 1412 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1413 | goto out; |
| 1414 | } |
| 1415 | |
| 1416 | newsrv->conn_src.source_addr = *sk; |
| 1417 | |
| 1418 | if (port_low != port_high) { |
| 1419 | int i; |
| 1420 | |
| 1421 | if (!port_low || !port_high) { |
| 1422 | Alert("parsing [%s:%d] : %s does not support port offsets (found '%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 | if (port_low <= 0 || port_low > 65535 || |
| 1429 | port_high <= 0 || port_high > 65535 || |
| 1430 | port_low > port_high) { |
| 1431 | Alert("parsing [%s:%d] : invalid source port range %d-%d.\n", |
| 1432 | file, linenum, port_low, port_high); |
| 1433 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1); |
| 1437 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 1438 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 1439 | } |
| 1440 | |
| 1441 | cur_arg += 2; |
| 1442 | while (*(args[cur_arg])) { |
| 1443 | if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */ |
Willy Tarreau | 29fbe51 | 2015-08-20 19:35:14 +0200 | [diff] [blame^] | 1444 | #if defined(CONFIG_HAP_TRANSPARENT) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1445 | if (!*args[cur_arg + 1]) { |
| 1446 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n", |
| 1447 | file, linenum, "usesrc"); |
| 1448 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1449 | goto out; |
| 1450 | } |
| 1451 | if (!strcmp(args[cur_arg + 1], "client")) { |
| 1452 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1453 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 1454 | } else if (!strcmp(args[cur_arg + 1], "clientip")) { |
| 1455 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1456 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 1457 | } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) { |
| 1458 | char *name, *end; |
| 1459 | |
| 1460 | name = args[cur_arg+1] + 7; |
| 1461 | while (isspace(*name)) |
| 1462 | name++; |
| 1463 | |
| 1464 | end = name; |
| 1465 | while (*end && !isspace(*end) && *end != ',' && *end != ')') |
| 1466 | end++; |
| 1467 | |
| 1468 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1469 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 1470 | newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1); |
| 1471 | newsrv->conn_src.bind_hdr_len = end - name; |
| 1472 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 1473 | newsrv->conn_src.bind_hdr_name[end-name] = '\0'; |
| 1474 | newsrv->conn_src.bind_hdr_occ = -1; |
| 1475 | |
| 1476 | /* now look for an occurrence number */ |
| 1477 | while (isspace(*end)) |
| 1478 | end++; |
| 1479 | if (*end == ',') { |
| 1480 | end++; |
| 1481 | name = end; |
| 1482 | if (*end == '-') |
| 1483 | end++; |
| 1484 | while (isdigit((int)*end)) |
| 1485 | end++; |
| 1486 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name); |
| 1487 | } |
| 1488 | |
| 1489 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
| 1490 | Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative" |
| 1491 | " occurrences values smaller than %d.\n", |
| 1492 | file, linenum, MAX_HDR_HISTORY); |
| 1493 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1494 | goto out; |
| 1495 | } |
| 1496 | } else { |
| 1497 | struct sockaddr_storage *sk; |
| 1498 | int port1, port2; |
| 1499 | |
| 1500 | sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL); |
| 1501 | if (!sk) { |
| 1502 | Alert("parsing [%s:%d] : '%s %s' : %s\n", |
| 1503 | file, linenum, args[cur_arg], args[cur_arg+1], errmsg); |
| 1504 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1505 | goto out; |
| 1506 | } |
| 1507 | |
| 1508 | proto = protocol_by_family(sk->ss_family); |
| 1509 | if (!proto || !proto->connect) { |
| 1510 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 1511 | file, linenum, args[cur_arg], args[cur_arg+1]); |
| 1512 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1513 | goto out; |
| 1514 | } |
| 1515 | |
| 1516 | if (port1 != port2) { |
| 1517 | Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n", |
| 1518 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1519 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1520 | goto out; |
| 1521 | } |
| 1522 | newsrv->conn_src.tproxy_addr = *sk; |
| 1523 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 1524 | } |
| 1525 | global.last_checks |= LSTCHK_NETADM; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1526 | cur_arg += 2; |
| 1527 | continue; |
| 1528 | #else /* no TPROXY support */ |
| 1529 | Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n", |
| 1530 | file, linenum, "usesrc"); |
| 1531 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1532 | goto out; |
Willy Tarreau | 29fbe51 | 2015-08-20 19:35:14 +0200 | [diff] [blame^] | 1533 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1534 | } /* "usesrc" */ |
| 1535 | |
| 1536 | if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */ |
| 1537 | #ifdef SO_BINDTODEVICE |
| 1538 | if (!*args[cur_arg + 1]) { |
| 1539 | Alert("parsing [%s:%d] : '%s' : missing interface name.\n", |
| 1540 | file, linenum, args[0]); |
| 1541 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1542 | goto out; |
| 1543 | } |
| 1544 | free(newsrv->conn_src.iface_name); |
| 1545 | newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]); |
| 1546 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 1547 | global.last_checks |= LSTCHK_NETADM; |
| 1548 | #else |
| 1549 | Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n", |
| 1550 | file, linenum, args[0], args[cur_arg]); |
| 1551 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1552 | goto out; |
| 1553 | #endif |
| 1554 | cur_arg += 2; |
| 1555 | continue; |
| 1556 | } |
| 1557 | /* this keyword in not an option of "source" */ |
| 1558 | break; |
| 1559 | } /* while */ |
| 1560 | } |
| 1561 | else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */ |
| 1562 | Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n", |
| 1563 | file, linenum, "usesrc", "source"); |
| 1564 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1565 | goto out; |
| 1566 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1567 | else if (!defsrv && !strcmp(args[cur_arg], "namespace")) { |
| 1568 | #ifdef CONFIG_HAP_NS |
| 1569 | char *arg = args[cur_arg + 1]; |
| 1570 | if (!strcmp(arg, "*")) { |
| 1571 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 1572 | } else { |
| 1573 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 1574 | |
| 1575 | if (newsrv->netns == NULL) |
| 1576 | newsrv->netns = netns_store_insert(arg); |
| 1577 | |
| 1578 | if (newsrv->netns == NULL) { |
| 1579 | Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]); |
| 1580 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1581 | goto out; |
| 1582 | } |
| 1583 | } |
| 1584 | #else |
| 1585 | Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n", |
| 1586 | file, linenum, args[0], args[cur_arg]); |
| 1587 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1588 | goto out; |
| 1589 | #endif |
| 1590 | cur_arg += 2; |
| 1591 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1592 | else { |
| 1593 | static int srv_dumped; |
| 1594 | struct srv_kw *kw; |
| 1595 | char *err; |
| 1596 | |
| 1597 | kw = srv_find_kw(args[cur_arg]); |
| 1598 | if (kw) { |
| 1599 | char *err = NULL; |
| 1600 | int code; |
| 1601 | |
| 1602 | if (!kw->parse) { |
| 1603 | Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n", |
| 1604 | file, linenum, args[0], args[1], args[cur_arg]); |
| 1605 | cur_arg += 1 + kw->skip ; |
| 1606 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1607 | goto out; |
| 1608 | } |
| 1609 | |
| 1610 | if (defsrv && !kw->default_ok) { |
| 1611 | Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n", |
| 1612 | file, linenum, args[0], args[1], args[cur_arg]); |
| 1613 | cur_arg += 1 + kw->skip ; |
| 1614 | err_code |= ERR_ALERT; |
| 1615 | continue; |
| 1616 | } |
| 1617 | |
| 1618 | code = kw->parse(args, &cur_arg, curproxy, newsrv, &err); |
| 1619 | err_code |= code; |
| 1620 | |
| 1621 | if (code) { |
| 1622 | if (err && *err) { |
| 1623 | indent_msg(&err, 2); |
| 1624 | Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err); |
| 1625 | } |
| 1626 | else |
| 1627 | Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n", |
| 1628 | file, linenum, args[0], args[1], args[cur_arg]); |
| 1629 | if (code & ERR_FATAL) { |
| 1630 | free(err); |
| 1631 | cur_arg += 1 + kw->skip; |
| 1632 | goto out; |
| 1633 | } |
| 1634 | } |
| 1635 | free(err); |
| 1636 | cur_arg += 1 + kw->skip; |
| 1637 | continue; |
| 1638 | } |
| 1639 | |
| 1640 | err = NULL; |
| 1641 | if (!srv_dumped) { |
| 1642 | srv_dump_kws(&err); |
| 1643 | indent_msg(&err, 4); |
| 1644 | srv_dumped = 1; |
| 1645 | } |
| 1646 | |
| 1647 | Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n", |
| 1648 | file, linenum, args[0], args[1], args[cur_arg], |
| 1649 | err ? " Registered keywords :" : "", err ? err : ""); |
| 1650 | free(err); |
| 1651 | |
| 1652 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1653 | goto out; |
| 1654 | } |
| 1655 | } |
| 1656 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1657 | if (do_check) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1658 | const char *ret; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1659 | |
| 1660 | if (newsrv->trackit) { |
| 1661 | Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n", |
| 1662 | file, linenum); |
| 1663 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1664 | goto out; |
| 1665 | } |
| 1666 | |
| 1667 | /* If neither a port nor an addr was specified and no check transport |
| 1668 | * layer is forced, then the transport layer used by the checks is the |
| 1669 | * same as for the production traffic. Otherwise we use raw_sock by |
| 1670 | * default, unless one is specified. |
| 1671 | */ |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1672 | if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1673 | #ifdef USE_OPENSSL |
| 1674 | newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL)); |
| 1675 | #endif |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1676 | newsrv->check.send_proxy |= (newsrv->pp_opts); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1677 | } |
| 1678 | /* try to get the port from check_core.addr if check.port not set */ |
| 1679 | if (!newsrv->check.port) |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1680 | newsrv->check.port = get_host_port(&newsrv->check.addr); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1681 | |
| 1682 | if (!newsrv->check.port) |
| 1683 | newsrv->check.port = realport; /* by default */ |
| 1684 | |
| 1685 | if (!newsrv->check.port) { |
| 1686 | /* not yet valid, because no port was set on |
| 1687 | * the server either. We'll check if we have |
| 1688 | * a known port on the first listener. |
| 1689 | */ |
| 1690 | struct listener *l; |
| 1691 | |
| 1692 | list_for_each_entry(l, &curproxy->conf.listeners, by_fe) { |
| 1693 | newsrv->check.port = get_host_port(&l->addr); |
| 1694 | if (newsrv->check.port) |
| 1695 | break; |
| 1696 | } |
| 1697 | } |
| 1698 | /* |
| 1699 | * 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] | 1700 | * be a 'connect' one when checking an IPv4/IPv6 server. |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1701 | */ |
Willy Tarreau | 5cf0b52 | 2014-05-09 23:59:19 +0200 | [diff] [blame] | 1702 | if (!newsrv->check.port && |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1703 | (is_inet_addr(&newsrv->check.addr) || |
| 1704 | (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1705 | struct tcpcheck_rule *n = NULL, *r = NULL; |
| 1706 | struct list *l; |
| 1707 | |
| 1708 | r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n; |
| 1709 | if (!r) { |
| 1710 | Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n", |
| 1711 | file, linenum, newsrv->id); |
| 1712 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1713 | goto out; |
| 1714 | } |
| 1715 | if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) { |
| 1716 | 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", |
| 1717 | file, linenum, newsrv->id); |
| 1718 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1719 | goto out; |
| 1720 | } |
| 1721 | else { |
| 1722 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 1723 | l = &newsrv->proxy->tcpcheck_rules; |
| 1724 | list_for_each_entry(n, l, list) { |
| 1725 | r = (struct tcpcheck_rule *)n->list.p; |
| 1726 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) { |
| 1727 | 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", |
| 1728 | file, linenum, newsrv->id); |
| 1729 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1730 | goto out; |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | /* note: check type will be set during the config review phase */ |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1737 | ret = init_check(&newsrv->check, 0); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1738 | if (ret) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1739 | Alert("parsing [%s:%d] : %s.\n", file, linenum, ret); |
| 1740 | err_code |= ERR_ALERT | ERR_ABORT; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1741 | goto out; |
| 1742 | } |
| 1743 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1744 | if (newsrv->resolution) |
| 1745 | newsrv->resolution->resolver_family_priority = newsrv->resolver_family_priority; |
| 1746 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1747 | newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 1748 | } |
| 1749 | |
| 1750 | if (do_agent) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1751 | const char *ret; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1752 | |
| 1753 | if (!newsrv->agent.port) { |
| 1754 | Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n", |
| 1755 | file, linenum, newsrv->id); |
| 1756 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1757 | goto out; |
| 1758 | } |
| 1759 | |
| 1760 | if (!newsrv->agent.inter) |
| 1761 | newsrv->agent.inter = newsrv->check.inter; |
| 1762 | |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1763 | ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1764 | if (ret) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1765 | Alert("parsing [%s:%d] : %s.\n", file, linenum, ret); |
| 1766 | err_code |= ERR_ALERT | ERR_ABORT; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1767 | goto out; |
| 1768 | } |
| 1769 | |
| 1770 | newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 1771 | } |
| 1772 | |
| 1773 | if (!defsrv) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 1774 | if (newsrv->flags & SRV_F_BACKUP) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1775 | curproxy->srv_bck++; |
| 1776 | else |
| 1777 | curproxy->srv_act++; |
| 1778 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 1779 | srv_lb_commit_status(newsrv); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | return 0; |
| 1783 | |
| 1784 | out: |
| 1785 | free(errmsg); |
| 1786 | return err_code; |
| 1787 | } |
| 1788 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 1789 | /* Returns a pointer to the first server matching either id <id>. |
| 1790 | * NULL is returned if no match is found. |
| 1791 | * the lookup is performed in the backend <bk> |
| 1792 | */ |
| 1793 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 1794 | { |
| 1795 | struct eb32_node *eb32; |
| 1796 | struct server *curserver; |
| 1797 | |
| 1798 | if (!bk || (id ==0)) |
| 1799 | return NULL; |
| 1800 | |
| 1801 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 1802 | if (!(bk->cap & PR_CAP_BE)) |
| 1803 | return NULL; |
| 1804 | |
| 1805 | curserver = NULL; |
| 1806 | |
| 1807 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 1808 | if (eb32) |
| 1809 | curserver = container_of(eb32, struct server, conf.id); |
| 1810 | |
| 1811 | return curserver; |
| 1812 | } |
| 1813 | |
| 1814 | /* Returns a pointer to the first server matching either name <name>, or id |
| 1815 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 1816 | * the lookup is performed in the backend <bk> |
| 1817 | */ |
| 1818 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 1819 | { |
| 1820 | struct server *curserver; |
| 1821 | |
| 1822 | if (!bk || !name) |
| 1823 | return NULL; |
| 1824 | |
| 1825 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 1826 | if (!(bk->cap & PR_CAP_BE)) |
| 1827 | return NULL; |
| 1828 | |
| 1829 | curserver = NULL; |
| 1830 | if (*name == '#') { |
| 1831 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 1832 | if (curserver) |
| 1833 | return curserver; |
| 1834 | } |
| 1835 | else { |
| 1836 | curserver = bk->srv; |
| 1837 | |
| 1838 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 1839 | curserver = curserver->next; |
| 1840 | |
| 1841 | if (curserver) |
| 1842 | return curserver; |
| 1843 | } |
| 1844 | |
| 1845 | return NULL; |
| 1846 | } |
| 1847 | |
| 1848 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 1849 | { |
| 1850 | struct server *byname; |
| 1851 | struct server *byid; |
| 1852 | |
| 1853 | if (!name && !id) |
| 1854 | return NULL; |
| 1855 | |
| 1856 | if (diff) |
| 1857 | *diff = 0; |
| 1858 | |
| 1859 | byname = byid = NULL; |
| 1860 | |
| 1861 | if (name) { |
| 1862 | byname = server_find_by_name(bk, name); |
| 1863 | if (byname && (!id || byname->puid == id)) |
| 1864 | return byname; |
| 1865 | } |
| 1866 | |
| 1867 | /* remaining possibilities : |
| 1868 | * - name not set |
| 1869 | * - name set but not found |
| 1870 | * - name found but ID doesn't match |
| 1871 | */ |
| 1872 | if (id) { |
| 1873 | byid = server_find_by_id(bk, id); |
| 1874 | if (byid) { |
| 1875 | if (byname) { |
| 1876 | /* use id only if forced by configuration */ |
| 1877 | if (byid->flags & SRV_F_FORCED_ID) { |
| 1878 | if (diff) |
| 1879 | *diff |= 2; |
| 1880 | return byid; |
| 1881 | } |
| 1882 | else { |
| 1883 | if (diff) |
| 1884 | *diff |= 1; |
| 1885 | return byname; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | /* remaining possibilities: |
| 1890 | * - name not set |
| 1891 | * - name set but not found |
| 1892 | */ |
| 1893 | if (name && diff) |
| 1894 | *diff |= 2; |
| 1895 | return byid; |
| 1896 | } |
| 1897 | |
| 1898 | /* id bot found */ |
| 1899 | if (byname) { |
| 1900 | if (diff) |
| 1901 | *diff |= 1; |
| 1902 | return byname; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | return NULL; |
| 1907 | } |
| 1908 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1909 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 1910 | * update a server's current IP address. |
| 1911 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 1912 | * ip is in network format. |
| 1913 | * updater is a string which contains an information about the requester of the update. |
| 1914 | * updater is used if not NULL. |
| 1915 | * |
| 1916 | * A log line and a stderr warning message is generated based on server's backend options. |
| 1917 | */ |
| 1918 | int update_server_addr(struct server *s, void *ip, int ip_sin_family, char *updater) |
| 1919 | { |
| 1920 | /* generates a log line and a warning on stderr */ |
| 1921 | if (1) { |
| 1922 | /* book enough space for both IPv4 and IPv6 */ |
| 1923 | char oldip[INET6_ADDRSTRLEN]; |
| 1924 | char newip[INET6_ADDRSTRLEN]; |
| 1925 | |
| 1926 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 1927 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 1928 | |
| 1929 | /* copy old IP address in a string */ |
| 1930 | switch (s->addr.ss_family) { |
| 1931 | case AF_INET: |
| 1932 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 1933 | break; |
| 1934 | case AF_INET6: |
| 1935 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 1936 | break; |
| 1937 | }; |
| 1938 | |
| 1939 | /* copy new IP address in a string */ |
| 1940 | switch (ip_sin_family) { |
| 1941 | case AF_INET: |
| 1942 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 1943 | break; |
| 1944 | case AF_INET6: |
| 1945 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 1946 | break; |
| 1947 | }; |
| 1948 | |
| 1949 | /* save log line into a buffer */ |
| 1950 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 1951 | s->proxy->id, s->id, oldip, newip, updater); |
| 1952 | |
| 1953 | /* write the buffer on stderr */ |
| 1954 | Warning("%s.\n", trash.str); |
| 1955 | |
| 1956 | /* send a log */ |
| 1957 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 1958 | } |
| 1959 | |
| 1960 | /* save the new IP family */ |
| 1961 | s->addr.ss_family = ip_sin_family; |
| 1962 | /* save the new IP address */ |
| 1963 | switch (ip_sin_family) { |
| 1964 | case AF_INET: |
| 1965 | ((struct sockaddr_in *)&s->addr)->sin_addr.s_addr = *(uint32_t *)ip; |
| 1966 | break; |
| 1967 | case AF_INET6: |
| 1968 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 1969 | break; |
| 1970 | }; |
| 1971 | |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1976 | * update server status based on result of name resolution |
| 1977 | * returns: |
| 1978 | * 0 if server status is updated |
| 1979 | * 1 if server status has not changed |
| 1980 | */ |
| 1981 | int snr_update_srv_status(struct server *s) |
| 1982 | { |
| 1983 | struct dns_resolution *resolution = s->resolution; |
| 1984 | |
| 1985 | switch (resolution->status) { |
| 1986 | case RSLV_STATUS_NONE: |
| 1987 | /* status when HAProxy has just (re)started */ |
| 1988 | trigger_resolution(s); |
| 1989 | break; |
| 1990 | |
| 1991 | default: |
| 1992 | break; |
| 1993 | } |
| 1994 | |
| 1995 | return 1; |
| 1996 | } |
| 1997 | |
| 1998 | /* |
| 1999 | * Server Name Resolution valid response callback |
| 2000 | * It expects: |
| 2001 | * - <nameserver>: the name server which answered the valid response |
| 2002 | * - <response>: buffer containing a valid DNS response |
| 2003 | * - <response_len>: size of <response> |
| 2004 | * It performs the following actions: |
| 2005 | * - ignore response if current ip found and server family not met |
| 2006 | * - update with first new ip found if family is met and current IP is not found |
| 2007 | * returns: |
| 2008 | * 0 on error |
| 2009 | * 1 when no error or safe ignore |
| 2010 | */ |
| 2011 | int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, unsigned char *response, int response_len) |
| 2012 | { |
| 2013 | struct server *s; |
| 2014 | void *serverip, *firstip; |
| 2015 | short server_sin_family, firstip_sin_family; |
| 2016 | unsigned char *response_end; |
| 2017 | int ret; |
| 2018 | struct chunk *chk = get_trash_chunk(); |
| 2019 | |
| 2020 | /* initializing variables */ |
| 2021 | response_end = response + response_len; /* pointer to mark the end of the response */ |
| 2022 | firstip = NULL; /* pointer to the first valid response found */ |
| 2023 | /* it will be used as the new IP if a change is required */ |
| 2024 | firstip_sin_family = AF_UNSPEC; |
| 2025 | serverip = NULL; /* current server IP address */ |
| 2026 | |
| 2027 | /* shortcut to the server whose name is being resolved */ |
| 2028 | s = (struct server *)resolution->requester; |
| 2029 | |
| 2030 | /* initializing server IP pointer */ |
| 2031 | server_sin_family = s->addr.ss_family; |
| 2032 | switch (server_sin_family) { |
| 2033 | case AF_INET: |
| 2034 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 2035 | break; |
| 2036 | |
| 2037 | case AF_INET6: |
| 2038 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 2039 | break; |
| 2040 | |
| 2041 | default: |
| 2042 | goto invalid; |
| 2043 | } |
| 2044 | |
| 2045 | ret = dns_get_ip_from_response(response, response_end, resolution->hostname_dn, resolution->hostname_dn_len, |
| 2046 | serverip, server_sin_family, resolution->resolver_family_priority, &firstip, |
| 2047 | &firstip_sin_family); |
| 2048 | |
| 2049 | switch (ret) { |
| 2050 | case DNS_UPD_NO: |
| 2051 | if (resolution->status != RSLV_STATUS_VALID) { |
| 2052 | resolution->status = RSLV_STATUS_VALID; |
| 2053 | resolution->last_status_change = now_ms; |
| 2054 | } |
| 2055 | goto stop_resolution; |
| 2056 | |
| 2057 | case DNS_UPD_SRVIP_NOT_FOUND: |
| 2058 | goto save_ip; |
| 2059 | |
| 2060 | case DNS_UPD_CNAME: |
| 2061 | if (resolution->status != RSLV_STATUS_VALID) { |
| 2062 | resolution->status = RSLV_STATUS_VALID; |
| 2063 | resolution->last_status_change = now_ms; |
| 2064 | } |
| 2065 | goto invalid; |
| 2066 | |
| 2067 | default: |
| 2068 | goto invalid; |
| 2069 | |
| 2070 | } |
| 2071 | |
| 2072 | save_ip: |
| 2073 | nameserver->counters.update += 1; |
| 2074 | if (resolution->status != RSLV_STATUS_VALID) { |
| 2075 | resolution->status = RSLV_STATUS_VALID; |
| 2076 | resolution->last_status_change = now_ms; |
| 2077 | } |
| 2078 | |
| 2079 | /* save the first ip we found */ |
| 2080 | chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id); |
| 2081 | update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str); |
| 2082 | |
| 2083 | stop_resolution: |
| 2084 | /* update last resolution date and time */ |
| 2085 | resolution->last_resolution = now_ms; |
| 2086 | /* reset current status flag */ |
| 2087 | resolution->step = RSLV_STEP_NONE; |
| 2088 | /* reset values */ |
| 2089 | dns_reset_resolution(resolution); |
| 2090 | |
| 2091 | LIST_DEL(&resolution->list); |
| 2092 | dns_update_resolvers_timeout(nameserver->resolvers); |
| 2093 | |
| 2094 | snr_update_srv_status(s); |
| 2095 | return 0; |
| 2096 | |
| 2097 | invalid: |
| 2098 | nameserver->counters.invalid += 1; |
| 2099 | if (resolution->nb_responses >= nameserver->resolvers->count_nameservers) |
| 2100 | goto stop_resolution; |
| 2101 | |
| 2102 | snr_update_srv_status(s); |
| 2103 | return 0; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Server Name Resolution error management callback |
| 2108 | * returns: |
| 2109 | * 0 on error |
| 2110 | * 1 when no error or safe ignore |
| 2111 | */ |
| 2112 | int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code) |
| 2113 | { |
| 2114 | struct server *s; |
| 2115 | struct dns_resolvers *resolvers; |
| 2116 | |
| 2117 | /* shortcut to the server whose name is being resolved */ |
| 2118 | s = (struct server *)resolution->requester; |
| 2119 | resolvers = resolution->resolvers; |
| 2120 | |
| 2121 | /* can be ignored if this is not the last response */ |
| 2122 | if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) { |
| 2123 | return 1; |
| 2124 | } |
| 2125 | |
| 2126 | switch (error_code) { |
| 2127 | case DNS_RESP_INVALID: |
| 2128 | case DNS_RESP_WRONG_NAME: |
| 2129 | if (resolution->status != RSLV_STATUS_INVALID) { |
| 2130 | resolution->status = RSLV_STATUS_INVALID; |
| 2131 | resolution->last_status_change = now_ms; |
| 2132 | } |
| 2133 | break; |
| 2134 | |
| 2135 | case DNS_RESP_ANCOUNT_ZERO: |
| 2136 | case DNS_RESP_ERROR: |
| 2137 | if (resolution->query_type == DNS_RTYPE_ANY) { |
| 2138 | /* let's change the query type */ |
| 2139 | if (resolution->resolver_family_priority == AF_INET6) |
| 2140 | resolution->query_type = DNS_RTYPE_AAAA; |
| 2141 | else |
| 2142 | resolution->query_type = DNS_RTYPE_A; |
| 2143 | |
| 2144 | dns_send_query(resolution); |
| 2145 | |
| 2146 | /* |
| 2147 | * move the resolution to the last element of the FIFO queue |
| 2148 | * and update timeout wakeup based on the new first entry |
| 2149 | */ |
| 2150 | if (dns_check_resolution_queue(resolvers) > 1) { |
| 2151 | /* second resolution becomes first one */ |
| 2152 | LIST_DEL(&resolvers->curr_resolution); |
| 2153 | /* ex first resolution goes to the end of the queue */ |
| 2154 | LIST_ADDQ(&resolvers->curr_resolution, &resolution->list); |
| 2155 | } |
| 2156 | dns_update_resolvers_timeout(resolvers); |
| 2157 | goto leave; |
| 2158 | } |
| 2159 | else { |
| 2160 | if (resolution->status != RSLV_STATUS_OTHER) { |
| 2161 | resolution->status = RSLV_STATUS_OTHER; |
| 2162 | resolution->last_status_change = now_ms; |
| 2163 | } |
| 2164 | } |
| 2165 | break; |
| 2166 | |
| 2167 | case DNS_RESP_NX_DOMAIN: |
| 2168 | if (resolution->status != RSLV_STATUS_NX) { |
| 2169 | resolution->status = RSLV_STATUS_NX; |
| 2170 | resolution->last_status_change = now_ms; |
| 2171 | } |
| 2172 | break; |
| 2173 | |
| 2174 | case DNS_RESP_REFUSED: |
| 2175 | if (resolution->status != RSLV_STATUS_REFUSED) { |
| 2176 | resolution->status = RSLV_STATUS_REFUSED; |
| 2177 | resolution->last_status_change = now_ms; |
| 2178 | } |
| 2179 | break; |
| 2180 | |
| 2181 | case DNS_RESP_CNAME_ERROR: |
| 2182 | break; |
| 2183 | |
| 2184 | case DNS_RESP_TIMEOUT: |
| 2185 | if (resolution->status != RSLV_STATUS_TIMEOUT) { |
| 2186 | resolution->status = RSLV_STATUS_TIMEOUT; |
| 2187 | resolution->last_status_change = now_ms; |
| 2188 | } |
| 2189 | break; |
| 2190 | } |
| 2191 | |
| 2192 | /* update last resolution date and time */ |
| 2193 | resolution->last_resolution = now_ms; |
| 2194 | /* reset current status flag */ |
| 2195 | resolution->step = RSLV_STEP_NONE; |
| 2196 | /* reset values */ |
| 2197 | dns_reset_resolution(resolution); |
| 2198 | |
| 2199 | LIST_DEL(&resolution->list); |
| 2200 | dns_update_resolvers_timeout(resolvers); |
| 2201 | |
| 2202 | leave: |
| 2203 | snr_update_srv_status(s); |
| 2204 | return 1; |
| 2205 | } |
| 2206 | |
| 2207 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2208 | * Local variables: |
| 2209 | * c-indent-level: 8 |
| 2210 | * c-basic-offset: 8 |
| 2211 | * End: |
| 2212 | */ |