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> |
| 23 | |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 24 | #include <proto/checks.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 25 | #include <proto/port_range.h> |
| 26 | #include <proto/protocol.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 27 | #include <proto/queue.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 28 | #include <proto/raw_sock.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 29 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 30 | #include <proto/stream.h> |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 31 | #include <proto/task.h> |
| 32 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 34 | /* List head of all known server keywords */ |
| 35 | static struct srv_kw_list srv_keywords = { |
| 36 | .list = LIST_HEAD_INIT(srv_keywords.list) |
| 37 | }; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 38 | |
Simon Horman | a360844 | 2013-11-01 16:46:15 +0900 | [diff] [blame] | 39 | int srv_downtime(const struct server *s) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 40 | { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 41 | 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] | 42 | return s->down_time; |
| 43 | |
| 44 | return now.tv_sec - s->last_change + s->down_time; |
| 45 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 47 | int srv_lastsession(const struct server *s) |
| 48 | { |
| 49 | if (s->counters.last_sess) |
| 50 | return now.tv_sec - s->counters.last_sess; |
| 51 | |
| 52 | return -1; |
| 53 | } |
| 54 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 55 | int srv_getinter(const struct check *check) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 56 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 57 | const struct server *s = check->server; |
| 58 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 59 | 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] | 60 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 61 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 62 | if ((s->state == SRV_ST_STOPPED) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 63 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 64 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 65 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 68 | /* |
| 69 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 70 | * parsing sessions. |
| 71 | */ |
| 72 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 73 | { |
| 74 | LIST_ADDQ(&srv_keywords.list, &kwl->list); |
| 75 | } |
| 76 | |
| 77 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 78 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 79 | * find one with a valid ->parse() function. This way it is possible to declare |
| 80 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 81 | * if some options are met. Note that if the requested keyword contains an |
| 82 | * opening parenthesis, everything from this point is ignored. |
| 83 | */ |
| 84 | struct srv_kw *srv_find_kw(const char *kw) |
| 85 | { |
| 86 | int index; |
| 87 | const char *kwend; |
| 88 | struct srv_kw_list *kwl; |
| 89 | struct srv_kw *ret = NULL; |
| 90 | |
| 91 | kwend = strchr(kw, '('); |
| 92 | if (!kwend) |
| 93 | kwend = kw + strlen(kw); |
| 94 | |
| 95 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 96 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 97 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 98 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 99 | if (kwl->kw[index].parse) |
| 100 | return &kwl->kw[index]; /* found it !*/ |
| 101 | else |
| 102 | ret = &kwl->kw[index]; /* may be OK */ |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 110 | * unsupported keywords are only dumped if their supported form was not |
| 111 | * found. |
| 112 | */ |
| 113 | void srv_dump_kws(char **out) |
| 114 | { |
| 115 | struct srv_kw_list *kwl; |
| 116 | int index; |
| 117 | |
| 118 | *out = NULL; |
| 119 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 120 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 121 | if (kwl->kw[index].parse || |
| 122 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 123 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 124 | kwl->scope, |
| 125 | kwl->kw[index].kw, |
| 126 | kwl->kw[index].skip ? " <arg>" : "", |
| 127 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 128 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 133 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 134 | /* parse the "id" server keyword */ |
| 135 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 136 | { |
| 137 | struct eb32_node *node; |
| 138 | |
| 139 | if (!*args[*cur_arg + 1]) { |
| 140 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 141 | return ERR_ALERT | ERR_FATAL; |
| 142 | } |
| 143 | |
| 144 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 145 | newsrv->conf.id.key = newsrv->puid; |
| 146 | |
| 147 | if (newsrv->puid <= 0) { |
| 148 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 149 | return ERR_ALERT | ERR_FATAL; |
| 150 | } |
| 151 | |
| 152 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 153 | if (node) { |
| 154 | struct server *target = container_of(node, struct server, conf.id); |
| 155 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 156 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 157 | target->id); |
| 158 | return ERR_ALERT | ERR_FATAL; |
| 159 | } |
| 160 | |
| 161 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 162 | return 0; |
| 163 | } |
| 164 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 165 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 166 | * 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] | 167 | * shutdown. |
| 168 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 169 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 170 | { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 171 | struct stream *stream, *stream_bck; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 172 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 173 | list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv) |
| 174 | if (stream->srv_conn == srv) |
| 175 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* 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] | 179 | * 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] | 180 | * the reason for the shutdown. |
| 181 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 182 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 183 | { |
| 184 | struct server *srv; |
| 185 | |
| 186 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 187 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 188 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 191 | /* Appends some information to a message string related to a server going UP or |
| 192 | * DOWN. If both <forced> and <reason> are null and the server tracks another |
| 193 | * one, a "via" information will be provided to know where the status came from. |
| 194 | * If <reason> is non-null, the entire string will be appended after a comma and |
| 195 | * 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] | 196 | * If <xferred> is non-negative, some information about requeued streams are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 197 | * provided. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 198 | */ |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 199 | 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] | 200 | { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 201 | if (reason) |
| 202 | chunk_appendf(msg, ", %s", reason); |
| 203 | else if (!forced && s->track) |
| 204 | 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] | 205 | |
| 206 | if (xferred >= 0) { |
| 207 | if (s->state == SRV_ST_STOPPED) |
| 208 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 209 | " %d sessions active, %d requeued, %d remaining in queue", |
| 210 | s->proxy->srv_act, s->proxy->srv_bck, |
| 211 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 212 | s->cur_sess, xferred, s->nbpend); |
| 213 | else |
| 214 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 215 | " %d sessions requeued, %d total in queue", |
| 216 | s->proxy->srv_act, s->proxy->srv_bck, |
| 217 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 218 | xferred, s->nbpend); |
| 219 | } |
| 220 | } |
| 221 | |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 222 | /* Marks server <s> down, regardless of its checks' statuses, notifies by all |
| 223 | * available means, recounts the remaining servers on the proxy and transfers |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 224 | * queued streams whenever possible to other servers. It automatically |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 225 | * recomputes the number of servers, but not the map. Maintenance servers are |
| 226 | * ignored. It reports <reason> if non-null as the reason for going down. Note |
| 227 | * that it makes use of the trash to build the log strings, so <reason> must |
| 228 | * not be placed there. |
| 229 | */ |
| 230 | void srv_set_stopped(struct server *s, const char *reason) |
| 231 | { |
| 232 | struct server *srv; |
| 233 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 234 | int srv_was_stopping = (s->state == SRV_ST_STOPPING); |
Simon Horman | 64e3416 | 2015-02-06 11:11:57 +0900 | [diff] [blame] | 235 | int log_level; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 236 | int xferred; |
| 237 | |
| 238 | if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED) |
| 239 | return; |
| 240 | |
| 241 | s->last_change = now.tv_sec; |
| 242 | s->state = SRV_ST_STOPPED; |
| 243 | if (s->proxy->lbprm.set_server_status_down) |
| 244 | s->proxy->lbprm.set_server_status_down(s); |
| 245 | |
| 246 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 247 | srv_shutdown_streams(s, SF_ERR_DOWN); |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 248 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 249 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 250 | * a connection. Those which are redispatchable will be queued |
| 251 | * to another server or to the proxy itself. |
| 252 | */ |
| 253 | xferred = pendconn_redistribute(s); |
| 254 | |
| 255 | chunk_printf(&trash, |
| 256 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 257 | s->proxy->id, s->id); |
| 258 | |
| 259 | srv_append_status(&trash, s, reason, xferred, 0); |
| 260 | Warning("%s.\n", trash.str); |
| 261 | |
| 262 | /* we don't send an alert if the server was previously paused */ |
Simon Horman | 64e3416 | 2015-02-06 11:11:57 +0900 | [diff] [blame] | 263 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
| 264 | send_log(s->proxy, log_level, "%s.\n", trash.str); |
| 265 | send_email_alert(s, log_level, "%s", trash.str); |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 266 | |
| 267 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 268 | set_backend_down(s->proxy); |
| 269 | |
| 270 | s->counters.down_trans++; |
| 271 | |
| 272 | for (srv = s->trackers; srv; srv = srv->tracknext) |
| 273 | srv_set_stopped(srv, NULL); |
| 274 | } |
| 275 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 276 | /* Marks server <s> up regardless of its checks' statuses and provided it isn't |
| 277 | * in maintenance. Notifies by all available means, recounts the remaining |
| 278 | * servers on the proxy and tries to grab requests from the proxy. It |
| 279 | * automatically recomputes the number of servers, but not the map. Maintenance |
| 280 | * servers are ignored. It reports <reason> if non-null as the reason for going |
| 281 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 282 | * must not be placed there. |
| 283 | */ |
| 284 | void srv_set_running(struct server *s, const char *reason) |
| 285 | { |
| 286 | struct server *srv; |
| 287 | int xferred; |
| 288 | |
| 289 | if (s->admin & SRV_ADMF_MAINT) |
| 290 | return; |
| 291 | |
| 292 | if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING) |
| 293 | return; |
| 294 | |
| 295 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 296 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 297 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 298 | s->proxy->last_change = now.tv_sec; |
| 299 | } |
| 300 | |
| 301 | if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times |
| 302 | s->down_time += now.tv_sec - s->last_change; |
| 303 | |
| 304 | s->last_change = now.tv_sec; |
| 305 | |
| 306 | s->state = SRV_ST_STARTING; |
| 307 | if (s->slowstart > 0) |
| 308 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 309 | else |
| 310 | s->state = SRV_ST_RUNNING; |
| 311 | |
| 312 | server_recalc_eweight(s); |
| 313 | |
| 314 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 315 | * 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] | 316 | * then it can accept new connections, so we shut down all streams |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 317 | * on all backup servers. |
| 318 | */ |
| 319 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 320 | !(s->flags & SRV_F_BACKUP) && s->eweight) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 321 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 322 | |
| 323 | /* check if we can handle some connections queued at the proxy. We |
| 324 | * will take as many as we can handle. |
| 325 | */ |
| 326 | xferred = pendconn_grab_from_px(s); |
| 327 | |
| 328 | chunk_printf(&trash, |
| 329 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 330 | s->proxy->id, s->id); |
| 331 | |
| 332 | srv_append_status(&trash, s, reason, xferred, 0); |
| 333 | Warning("%s.\n", trash.str); |
| 334 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Simon Horman | 4cd477f | 2015-04-30 13:10:34 +0900 | [diff] [blame] | 335 | send_email_alert(s, LOG_NOTICE, "%s", trash.str); |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 336 | |
| 337 | for (srv = s->trackers; srv; srv = srv->tracknext) |
| 338 | srv_set_running(srv, NULL); |
| 339 | } |
| 340 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 341 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
| 342 | * isn't in maintenance. Notifies by all available means, recounts the remaining |
| 343 | * servers on the proxy and tries to grab requests from the proxy. It |
| 344 | * automatically recomputes the number of servers, but not the map. Maintenance |
| 345 | * servers are ignored. It reports <reason> if non-null as the reason for going |
| 346 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 347 | * must not be placed there. |
| 348 | */ |
| 349 | void srv_set_stopping(struct server *s, const char *reason) |
| 350 | { |
| 351 | struct server *srv; |
| 352 | int xferred; |
| 353 | |
| 354 | if (s->admin & SRV_ADMF_MAINT) |
| 355 | return; |
| 356 | |
| 357 | if (s->state == SRV_ST_STOPPING) |
| 358 | return; |
| 359 | |
| 360 | s->last_change = now.tv_sec; |
| 361 | s->state = SRV_ST_STOPPING; |
| 362 | if (s->proxy->lbprm.set_server_status_down) |
| 363 | s->proxy->lbprm.set_server_status_down(s); |
| 364 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 365 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 366 | * a connection. Those which are redispatchable will be queued |
| 367 | * to another server or to the proxy itself. |
| 368 | */ |
| 369 | xferred = pendconn_redistribute(s); |
| 370 | |
| 371 | chunk_printf(&trash, |
| 372 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 373 | s->proxy->id, s->id); |
| 374 | |
| 375 | srv_append_status(&trash, s, reason, xferred, 0); |
| 376 | |
| 377 | Warning("%s.\n", trash.str); |
| 378 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 379 | |
| 380 | if (!s->proxy->srv_bck && !s->proxy->srv_act) |
| 381 | set_backend_down(s->proxy); |
| 382 | |
| 383 | for (srv = s->trackers; srv; srv = srv->tracknext) |
| 384 | srv_set_stopping(srv, NULL); |
| 385 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 386 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 387 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 388 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 389 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 390 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 391 | * checks). When either the flag is already set or no flag is passed, nothing |
| 392 | * is done. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 393 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 394 | void srv_set_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 395 | { |
| 396 | struct check *check = &s->check; |
| 397 | struct server *srv; |
| 398 | int xferred; |
| 399 | |
| 400 | if (!mode) |
| 401 | return; |
| 402 | |
| 403 | /* stop going down as soon as we meet a server already in the same state */ |
| 404 | if (s->admin & mode) |
| 405 | return; |
| 406 | |
| 407 | s->admin |= mode; |
| 408 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 409 | /* stop going down if the equivalent flag was already present (forced or inherited) */ |
| 410 | if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) || |
| 411 | ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN))) |
| 412 | return; |
| 413 | |
| 414 | /* Maintenance must also disable health checks */ |
| 415 | if (mode & SRV_ADMF_MAINT) { |
| 416 | if (s->check.state & CHK_ST_ENABLED) { |
| 417 | s->check.state |= CHK_ST_PAUSED; |
| 418 | check->health = 0; |
| 419 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 420 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 421 | if (s->state == SRV_ST_STOPPED) { /* server was already down */ |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 422 | chunk_printf(&trash, |
| 423 | "%sServer %s/%s was DOWN and now enters maintenance", |
| 424 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
| 425 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 426 | srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT)); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 427 | |
| 428 | Warning("%s.\n", trash.str); |
| 429 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 430 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 431 | else { /* server was still running */ |
| 432 | int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN); |
| 433 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 434 | |
| 435 | check->health = 0; /* failure */ |
| 436 | s->last_change = now.tv_sec; |
| 437 | s->state = SRV_ST_STOPPED; |
| 438 | if (s->proxy->lbprm.set_server_status_down) |
| 439 | s->proxy->lbprm.set_server_status_down(s); |
| 440 | |
| 441 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 442 | srv_shutdown_streams(s, SF_ERR_DOWN); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 443 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 444 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 445 | * a connection. Those which are redispatchable will be queued |
| 446 | * to another server or to the proxy itself. |
| 447 | */ |
| 448 | xferred = pendconn_redistribute(s); |
| 449 | |
| 450 | chunk_printf(&trash, |
| 451 | "%sServer %s/%s is going DOWN for maintenance", |
| 452 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 453 | s->proxy->id, s->id); |
| 454 | |
| 455 | srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT)); |
| 456 | |
| 457 | Warning("%s.\n", trash.str); |
| 458 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str); |
| 459 | |
| 460 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 461 | set_backend_down(s->proxy); |
| 462 | |
| 463 | s->counters.down_trans++; |
| 464 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 465 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 466 | |
| 467 | /* drain state is applied only if not yet in maint */ |
| 468 | if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 469 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 470 | |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 471 | s->last_change = now.tv_sec; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 472 | if (s->proxy->lbprm.set_server_status_down) |
| 473 | s->proxy->lbprm.set_server_status_down(s); |
| 474 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 475 | /* we might have streams queued on this server and waiting for |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 476 | * a connection. Those which are redispatchable will be queued |
| 477 | * to another server or to the proxy itself. |
| 478 | */ |
| 479 | xferred = pendconn_redistribute(s); |
| 480 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 481 | chunk_printf(&trash, "%sServer %s/%s enters drain state", |
| 482 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 483 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 484 | srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN)); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 485 | |
| 486 | Warning("%s.\n", trash.str); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 487 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Simon Horman | 4cd477f | 2015-04-30 13:10:34 +0900 | [diff] [blame] | 488 | send_email_alert(s, LOG_NOTICE, "%s", trash.str); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 489 | |
| 490 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 491 | set_backend_down(s->proxy); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 494 | /* compute the inherited flag to propagate */ |
| 495 | if (mode & SRV_ADMF_MAINT) |
| 496 | mode = SRV_ADMF_IMAINT; |
| 497 | else if (mode & SRV_ADMF_DRAIN) |
| 498 | mode = SRV_ADMF_IDRAIN; |
| 499 | |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 500 | for (srv = s->trackers; srv; srv = srv->tracknext) |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 501 | srv_set_admin_flag(srv, mode); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 504 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 505 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 506 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 507 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 508 | * 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] | 509 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 510 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 511 | { |
| 512 | struct check *check = &s->check; |
| 513 | struct server *srv; |
| 514 | int xferred = -1; |
| 515 | |
| 516 | if (!mode) |
| 517 | return; |
| 518 | |
| 519 | /* stop going down as soon as we see the flag is not there anymore */ |
| 520 | if (!(s->admin & mode)) |
| 521 | return; |
| 522 | |
| 523 | s->admin &= ~mode; |
| 524 | |
| 525 | if (s->admin & SRV_ADMF_MAINT) { |
| 526 | /* remaining in maintenance mode, let's inform precisely about the |
| 527 | * situation. |
| 528 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 529 | if (mode & SRV_ADMF_FMAINT) { |
| 530 | chunk_printf(&trash, |
| 531 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 532 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 533 | s->proxy->id, s->id); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 534 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 535 | if (s->track) /* normally it's mandatory here */ |
| 536 | chunk_appendf(&trash, " via %s/%s", |
| 537 | s->track->proxy->id, s->track->id); |
| 538 | Warning("%s.\n", trash.str); |
| 539 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 540 | } |
| 541 | else if (mode & SRV_ADMF_IMAINT) { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 542 | chunk_printf(&trash, |
| 543 | "%sServer %s/%s remains in forced maintenance", |
| 544 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 545 | s->proxy->id, s->id); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 546 | Warning("%s.\n", trash.str); |
| 547 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 548 | } |
| 549 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
| 550 | } |
| 551 | else if (mode & SRV_ADMF_MAINT) { |
| 552 | /* OK here we're leaving maintenance, we have many things to check, |
| 553 | * because the server might possibly be coming back up depending on |
| 554 | * its state. In practice, leaving maintenance means that we should |
| 555 | * immediately turn to UP (more or less the slowstart) under the |
| 556 | * following conditions : |
| 557 | * - server is neither checked nor tracked |
| 558 | * - server tracks another server which is not checked |
| 559 | * - server tracks another server which is already up |
| 560 | * Which sums up as something simpler : |
| 561 | * "either the tracking server is up or the server's checks are disabled |
| 562 | * or up". Otherwise we only re-enable health checks. There's a special |
| 563 | * case associated to the stopping state which can be inherited. Note |
| 564 | * that the server might still be in drain mode, which is naturally dealt |
| 565 | * with by the lower level functions. |
| 566 | */ |
| 567 | |
| 568 | if (s->check.state & CHK_ST_ENABLED) { |
| 569 | s->check.state &= ~CHK_ST_PAUSED; |
| 570 | check->health = check->rise; /* start OK but check immediately */ |
| 571 | } |
| 572 | |
| 573 | if ((!s->track || s->track->state != SRV_ST_STOPPED) && |
| 574 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 575 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 576 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 577 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 578 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 579 | s->proxy->last_change = now.tv_sec; |
| 580 | } |
| 581 | |
| 582 | if (s->last_change < now.tv_sec) // ignore negative times |
| 583 | s->down_time += now.tv_sec - s->last_change; |
| 584 | s->last_change = now.tv_sec; |
| 585 | |
| 586 | if (s->track && s->track->state == SRV_ST_STOPPING) |
| 587 | s->state = SRV_ST_STOPPING; |
| 588 | else { |
| 589 | s->state = SRV_ST_STARTING; |
| 590 | if (s->slowstart > 0) |
| 591 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 592 | else |
| 593 | s->state = SRV_ST_RUNNING; |
| 594 | } |
| 595 | |
| 596 | server_recalc_eweight(s); |
| 597 | |
| 598 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 599 | * 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] | 600 | * then it can accept new connections, so we shut down all streams |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 601 | * on all backup servers. |
| 602 | */ |
| 603 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 604 | !(s->flags & SRV_F_BACKUP) && s->eweight) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 605 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 606 | |
| 607 | /* check if we can handle some connections queued at the proxy. We |
| 608 | * will take as many as we can handle. |
| 609 | */ |
| 610 | xferred = pendconn_grab_from_px(s); |
| 611 | } |
| 612 | |
| 613 | if (mode & SRV_ADMF_FMAINT) { |
| 614 | chunk_printf(&trash, |
| 615 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 616 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 617 | s->proxy->id, s->id, |
| 618 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 619 | (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 620 | } |
| 621 | else { |
| 622 | chunk_printf(&trash, |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 623 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 624 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 625 | s->proxy->id, s->id, |
| 626 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 627 | (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 628 | srv_append_status(&trash, s, NULL, xferred, 0); |
| 629 | } |
| 630 | Warning("%s.\n", trash.str); |
| 631 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
| 632 | } |
| 633 | else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) { |
| 634 | /* remaining in drain mode after removing one of its flags */ |
| 635 | |
| 636 | if (mode & SRV_ADMF_FDRAIN) { |
| 637 | chunk_printf(&trash, |
| 638 | "%sServer %s/%s is leaving forced drain but remains in drain mode", |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 639 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 640 | s->proxy->id, s->id); |
| 641 | |
| 642 | if (s->track) /* normally it's mandatory here */ |
| 643 | chunk_appendf(&trash, " via %s/%s", |
| 644 | s->track->proxy->id, s->track->id); |
| 645 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 646 | else { |
| 647 | chunk_printf(&trash, |
| 648 | "%sServer %s/%s remains in forced drain mode", |
| 649 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 650 | s->proxy->id, s->id); |
| 651 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 652 | Warning("%s.\n", trash.str); |
| 653 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 654 | } |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 655 | else if (mode & SRV_ADMF_DRAIN) { |
| 656 | /* OK completely leaving drain mode */ |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 657 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 658 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 659 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 660 | s->proxy->last_change = now.tv_sec; |
| 661 | } |
| 662 | |
| 663 | if (s->last_change < now.tv_sec) // ignore negative times |
| 664 | s->down_time += now.tv_sec - s->last_change; |
| 665 | s->last_change = now.tv_sec; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 666 | server_recalc_eweight(s); |
| 667 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 668 | if (mode & SRV_ADMF_FDRAIN) { |
| 669 | chunk_printf(&trash, |
| 670 | "%sServer %s/%s is %s (leaving forced drain)", |
| 671 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 672 | s->proxy->id, s->id, |
| 673 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 674 | } |
| 675 | else { |
| 676 | chunk_printf(&trash, |
| 677 | "%sServer %s/%s is %s (leaving drain)", |
| 678 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 679 | s->proxy->id, s->id, |
| 680 | (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 681 | if (s->track) /* normally it's mandatory here */ |
| 682 | chunk_appendf(&trash, " via %s/%s", |
| 683 | s->track->proxy->id, s->track->id); |
| 684 | } |
| 685 | Warning("%s.\n", trash.str); |
| 686 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 689 | /* stop going down if the equivalent flag is still present (forced or inherited) */ |
| 690 | if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) || |
| 691 | ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN))) |
| 692 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 693 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 694 | if (mode & SRV_ADMF_MAINT) |
| 695 | mode = SRV_ADMF_IMAINT; |
| 696 | else if (mode & SRV_ADMF_DRAIN) |
| 697 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 698 | |
| 699 | for (srv = s->trackers; srv; srv = srv->tracknext) |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 700 | srv_clr_admin_flag(srv, mode); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 703 | /* Note: must not be declared <const> as its list will be overwritten. |
| 704 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 705 | * all code contributors. |
| 706 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 707 | * the config parser can report an appropriate error when a known keyword was |
| 708 | * not enabled. |
| 709 | */ |
| 710 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
| 711 | { "id", srv_parse_id, 1, 0 }, /* set id# of server */ |
| 712 | { NULL, NULL, 0 }, |
| 713 | }}; |
| 714 | |
| 715 | __attribute__((constructor)) |
| 716 | static void __listener_init(void) |
| 717 | { |
| 718 | srv_register_keywords(&srv_kws); |
| 719 | } |
| 720 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 721 | /* Recomputes the server's eweight based on its state, uweight, the current time, |
| 722 | * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup |
| 723 | * state is automatically disabled if the time is elapsed. |
| 724 | */ |
| 725 | void server_recalc_eweight(struct server *sv) |
| 726 | { |
| 727 | struct proxy *px = sv->proxy; |
| 728 | unsigned w; |
| 729 | |
| 730 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 731 | /* go to full throttle if the slowstart interval is reached */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 732 | if (sv->state == SRV_ST_STARTING) |
| 733 | sv->state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 737 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 738 | */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 739 | 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] | 740 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 741 | else |
| 742 | w = px->lbprm.wdiv; |
| 743 | |
| 744 | sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult; |
| 745 | |
| 746 | /* now propagate the status change to any LB algorithms */ |
| 747 | if (px->lbprm.update_server_eweight) |
| 748 | px->lbprm.update_server_eweight(sv); |
Willy Tarreau | 9943d31 | 2014-05-22 16:20:59 +0200 | [diff] [blame] | 749 | else if (srv_is_usable(sv)) { |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 750 | if (px->lbprm.set_server_status_up) |
| 751 | px->lbprm.set_server_status_up(sv); |
| 752 | } |
| 753 | else { |
| 754 | if (px->lbprm.set_server_status_down) |
| 755 | px->lbprm.set_server_status_down(sv); |
| 756 | } |
| 757 | } |
| 758 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 759 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 760 | * Parses weight_str and configures sv accordingly. |
| 761 | * Returns NULL on success, error message string otherwise. |
| 762 | */ |
| 763 | const char *server_parse_weight_change_request(struct server *sv, |
| 764 | const char *weight_str) |
| 765 | { |
| 766 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 767 | long int w; |
| 768 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 769 | |
| 770 | px = sv->proxy; |
| 771 | |
| 772 | /* if the weight is terminated with '%', it is set relative to |
| 773 | * the initial weight, otherwise it is absolute. |
| 774 | */ |
| 775 | if (!*weight_str) |
| 776 | return "Require <weight> or <weight%>.\n"; |
| 777 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 778 | w = strtol(weight_str, &end, 10); |
| 779 | if (end == weight_str) |
| 780 | return "Empty weight string empty or preceded by garbage"; |
| 781 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 782 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 783 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 784 | /* Avoid integer overflow */ |
| 785 | if (w > 25600) |
| 786 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 787 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 788 | if (w > 256) |
| 789 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 790 | } |
| 791 | else if (w < 0 || w > 256) |
| 792 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 793 | else if (end[0] != '\0') |
| 794 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 795 | |
| 796 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 797 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 798 | |
| 799 | sv->uweight = w; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 800 | server_recalc_eweight(sv); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 801 | |
| 802 | return NULL; |
| 803 | } |
| 804 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 805 | int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy) |
| 806 | { |
| 807 | struct server *newsrv = NULL; |
| 808 | const char *err; |
| 809 | char *errmsg = NULL; |
| 810 | int err_code = 0; |
| 811 | unsigned val; |
| 812 | |
| 813 | if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */ |
| 814 | int cur_arg; |
| 815 | short realport = 0; |
| 816 | int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd'); |
| 817 | |
| 818 | if (!defsrv && curproxy == defproxy) { |
| 819 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 820 | err_code |= ERR_ALERT | ERR_FATAL; |
| 821 | goto out; |
| 822 | } |
| 823 | else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) |
| 824 | err_code |= ERR_ALERT | ERR_FATAL; |
| 825 | |
| 826 | if (!*args[2]) { |
| 827 | Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 828 | file, linenum, args[0]); |
| 829 | err_code |= ERR_ALERT | ERR_FATAL; |
| 830 | goto out; |
| 831 | } |
| 832 | |
| 833 | err = invalid_char(args[1]); |
| 834 | if (err && !defsrv) { |
| 835 | Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n", |
| 836 | file, linenum, *err, args[1]); |
| 837 | err_code |= ERR_ALERT | ERR_FATAL; |
| 838 | goto out; |
| 839 | } |
| 840 | |
| 841 | if (!defsrv) { |
| 842 | struct sockaddr_storage *sk; |
| 843 | int port1, port2; |
| 844 | struct protocol *proto; |
| 845 | |
| 846 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 847 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 848 | err_code |= ERR_ALERT | ERR_ABORT; |
| 849 | goto out; |
| 850 | } |
| 851 | |
| 852 | /* the servers are linked backwards first */ |
| 853 | newsrv->next = curproxy->srv; |
| 854 | curproxy->srv = newsrv; |
| 855 | newsrv->proxy = curproxy; |
| 856 | newsrv->conf.file = strdup(file); |
| 857 | newsrv->conf.line = linenum; |
| 858 | |
| 859 | newsrv->obj_type = OBJ_TYPE_SERVER; |
| 860 | LIST_INIT(&newsrv->actconns); |
| 861 | LIST_INIT(&newsrv->pendconns); |
| 862 | do_check = 0; |
| 863 | do_agent = 0; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 864 | newsrv->flags = 0; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 865 | newsrv->admin = 0; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 866 | newsrv->state = SRV_ST_RUNNING; /* early server setup */ |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 867 | newsrv->last_change = now.tv_sec; |
| 868 | newsrv->id = strdup(args[1]); |
| 869 | |
| 870 | /* several ways to check the port component : |
| 871 | * - IP => port=+0, relative (IPv4 only) |
| 872 | * - IP: => port=+0, relative |
| 873 | * - IP:N => port=N, absolute |
| 874 | * - IP:+N => port=+N, relative |
| 875 | * - IP:-N => port=-N, relative |
| 876 | */ |
| 877 | sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL); |
| 878 | if (!sk) { |
| 879 | Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); |
| 880 | err_code |= ERR_ALERT | ERR_FATAL; |
| 881 | goto out; |
| 882 | } |
| 883 | |
| 884 | proto = protocol_by_family(sk->ss_family); |
| 885 | if (!proto || !proto->connect) { |
| 886 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 887 | file, linenum, args[0], args[1]); |
| 888 | err_code |= ERR_ALERT | ERR_FATAL; |
| 889 | goto out; |
| 890 | } |
| 891 | |
| 892 | if (!port1 || !port2) { |
| 893 | /* no port specified, +offset, -offset */ |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 894 | newsrv->flags |= SRV_F_MAPPORTS; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 895 | } |
| 896 | else if (port1 != port2) { |
| 897 | /* port range */ |
| 898 | Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n", |
| 899 | file, linenum, args[0], args[1], args[2]); |
| 900 | err_code |= ERR_ALERT | ERR_FATAL; |
| 901 | goto out; |
| 902 | } |
| 903 | else { |
| 904 | /* used by checks */ |
| 905 | realport = port1; |
| 906 | } |
| 907 | |
| 908 | newsrv->addr = *sk; |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 909 | newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = &raw_sock; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 910 | |
Thierry FOURNIER | bb2ae64 | 2015-01-14 11:31:49 +0100 | [diff] [blame] | 911 | if (!protocol_by_family(newsrv->addr.ss_family)) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 912 | Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n", |
| 913 | file, linenum, newsrv->addr.ss_family, args[2]); |
| 914 | err_code |= ERR_ALERT | ERR_FATAL; |
| 915 | goto out; |
| 916 | } |
| 917 | |
| 918 | newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl; |
| 919 | newsrv->check.port = curproxy->defsrv.check.port; |
| 920 | newsrv->check.inter = curproxy->defsrv.check.inter; |
| 921 | newsrv->check.fastinter = curproxy->defsrv.check.fastinter; |
| 922 | newsrv->check.downinter = curproxy->defsrv.check.downinter; |
| 923 | newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl; |
| 924 | newsrv->agent.port = curproxy->defsrv.agent.port; |
| 925 | newsrv->agent.inter = curproxy->defsrv.agent.inter; |
| 926 | newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter; |
| 927 | newsrv->agent.downinter = curproxy->defsrv.agent.downinter; |
| 928 | newsrv->maxqueue = curproxy->defsrv.maxqueue; |
| 929 | newsrv->minconn = curproxy->defsrv.minconn; |
| 930 | newsrv->maxconn = curproxy->defsrv.maxconn; |
| 931 | newsrv->slowstart = curproxy->defsrv.slowstart; |
| 932 | newsrv->onerror = curproxy->defsrv.onerror; |
| 933 | newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown; |
| 934 | newsrv->onmarkedup = curproxy->defsrv.onmarkedup; |
| 935 | newsrv->consecutive_errors_limit |
| 936 | = curproxy->defsrv.consecutive_errors_limit; |
| 937 | #ifdef OPENSSL |
| 938 | newsrv->use_ssl = curproxy->defsrv.use_ssl; |
| 939 | #endif |
| 940 | newsrv->uweight = newsrv->iweight |
| 941 | = curproxy->defsrv.iweight; |
| 942 | |
| 943 | newsrv->check.status = HCHK_STATUS_INI; |
| 944 | newsrv->check.rise = curproxy->defsrv.check.rise; |
| 945 | newsrv->check.fall = curproxy->defsrv.check.fall; |
| 946 | newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */ |
| 947 | newsrv->check.server = newsrv; |
Simon Horman | e16c1b3 | 2015-01-30 11:22:57 +0900 | [diff] [blame] | 948 | newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 949 | |
| 950 | newsrv->agent.status = HCHK_STATUS_INI; |
| 951 | newsrv->agent.rise = curproxy->defsrv.agent.rise; |
| 952 | newsrv->agent.fall = curproxy->defsrv.agent.fall; |
| 953 | newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */ |
| 954 | newsrv->agent.server = newsrv; |
| 955 | |
| 956 | cur_arg = 3; |
| 957 | } else { |
| 958 | newsrv = &curproxy->defsrv; |
| 959 | cur_arg = 1; |
| 960 | } |
| 961 | |
| 962 | while (*args[cur_arg]) { |
| 963 | if (!strcmp(args[cur_arg], "agent-check")) { |
| 964 | global.maxsock++; |
| 965 | do_agent = 1; |
| 966 | cur_arg += 1; |
| 967 | } else if (!strcmp(args[cur_arg], "agent-inter")) { |
| 968 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 969 | if (err) { |
| 970 | Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n", |
| 971 | file, linenum, *err, newsrv->id); |
| 972 | err_code |= ERR_ALERT | ERR_FATAL; |
| 973 | goto out; |
| 974 | } |
| 975 | if (val <= 0) { |
| 976 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 977 | file, linenum, val, args[cur_arg], newsrv->id); |
| 978 | err_code |= ERR_ALERT | ERR_FATAL; |
| 979 | goto out; |
| 980 | } |
| 981 | newsrv->agent.inter = val; |
| 982 | cur_arg += 2; |
| 983 | } |
| 984 | else if (!strcmp(args[cur_arg], "agent-port")) { |
| 985 | global.maxsock++; |
| 986 | newsrv->agent.port = atol(args[cur_arg + 1]); |
| 987 | cur_arg += 2; |
| 988 | } |
| 989 | else if (!defsrv && !strcmp(args[cur_arg], "cookie")) { |
| 990 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 991 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 992 | cur_arg += 2; |
| 993 | } |
| 994 | else if (!defsrv && !strcmp(args[cur_arg], "redir")) { |
| 995 | newsrv->rdr_pfx = strdup(args[cur_arg + 1]); |
| 996 | newsrv->rdr_len = strlen(args[cur_arg + 1]); |
| 997 | cur_arg += 2; |
| 998 | } |
| 999 | else if (!strcmp(args[cur_arg], "rise")) { |
| 1000 | if (!*args[cur_arg + 1]) { |
| 1001 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 1002 | file, linenum, args[cur_arg]); |
| 1003 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1004 | goto out; |
| 1005 | } |
| 1006 | |
| 1007 | newsrv->check.rise = atol(args[cur_arg + 1]); |
| 1008 | if (newsrv->check.rise <= 0) { |
| 1009 | Alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
| 1010 | file, linenum, args[cur_arg]); |
| 1011 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1012 | goto out; |
| 1013 | } |
| 1014 | |
| 1015 | if (newsrv->check.health) |
| 1016 | newsrv->check.health = newsrv->check.rise; |
| 1017 | cur_arg += 2; |
| 1018 | } |
| 1019 | else if (!strcmp(args[cur_arg], "fall")) { |
| 1020 | newsrv->check.fall = atol(args[cur_arg + 1]); |
| 1021 | |
| 1022 | if (!*args[cur_arg + 1]) { |
| 1023 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 1024 | file, linenum, args[cur_arg]); |
| 1025 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1026 | goto out; |
| 1027 | } |
| 1028 | |
| 1029 | if (newsrv->check.fall <= 0) { |
| 1030 | Alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
| 1031 | file, linenum, args[cur_arg]); |
| 1032 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1033 | goto out; |
| 1034 | } |
| 1035 | |
| 1036 | cur_arg += 2; |
| 1037 | } |
| 1038 | else if (!strcmp(args[cur_arg], "inter")) { |
| 1039 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1040 | if (err) { |
| 1041 | Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n", |
| 1042 | file, linenum, *err, newsrv->id); |
| 1043 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1044 | goto out; |
| 1045 | } |
| 1046 | if (val <= 0) { |
| 1047 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 1048 | file, linenum, val, args[cur_arg], newsrv->id); |
| 1049 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1050 | goto out; |
| 1051 | } |
| 1052 | newsrv->check.inter = val; |
| 1053 | cur_arg += 2; |
| 1054 | } |
| 1055 | else if (!strcmp(args[cur_arg], "fastinter")) { |
| 1056 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1057 | if (err) { |
| 1058 | Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n", |
| 1059 | file, linenum, *err, newsrv->id); |
| 1060 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1061 | goto out; |
| 1062 | } |
| 1063 | if (val <= 0) { |
| 1064 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 1065 | file, linenum, val, args[cur_arg], newsrv->id); |
| 1066 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1067 | goto out; |
| 1068 | } |
| 1069 | newsrv->check.fastinter = val; |
| 1070 | cur_arg += 2; |
| 1071 | } |
| 1072 | else if (!strcmp(args[cur_arg], "downinter")) { |
| 1073 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1074 | if (err) { |
| 1075 | Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n", |
| 1076 | file, linenum, *err, newsrv->id); |
| 1077 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1078 | goto out; |
| 1079 | } |
| 1080 | if (val <= 0) { |
| 1081 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 1082 | file, linenum, val, args[cur_arg], newsrv->id); |
| 1083 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1084 | goto out; |
| 1085 | } |
| 1086 | newsrv->check.downinter = val; |
| 1087 | cur_arg += 2; |
| 1088 | } |
| 1089 | else if (!defsrv && !strcmp(args[cur_arg], "addr")) { |
| 1090 | struct sockaddr_storage *sk; |
| 1091 | int port1, port2; |
| 1092 | struct protocol *proto; |
| 1093 | |
| 1094 | sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL); |
| 1095 | if (!sk) { |
| 1096 | Alert("parsing [%s:%d] : '%s' : %s\n", |
| 1097 | file, linenum, args[cur_arg], errmsg); |
| 1098 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1099 | goto out; |
| 1100 | } |
| 1101 | |
| 1102 | proto = protocol_by_family(sk->ss_family); |
| 1103 | if (!proto || !proto->connect) { |
| 1104 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 1105 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1106 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1107 | goto out; |
| 1108 | } |
| 1109 | |
| 1110 | if (port1 != port2) { |
| 1111 | Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n", |
| 1112 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1113 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1114 | goto out; |
| 1115 | } |
| 1116 | |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1117 | newsrv->check.addr = newsrv->agent.addr = *sk; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1118 | cur_arg += 2; |
| 1119 | } |
| 1120 | else if (!strcmp(args[cur_arg], "port")) { |
| 1121 | newsrv->check.port = atol(args[cur_arg + 1]); |
| 1122 | cur_arg += 2; |
| 1123 | } |
| 1124 | else if (!defsrv && !strcmp(args[cur_arg], "backup")) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 1125 | newsrv->flags |= SRV_F_BACKUP; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1126 | cur_arg ++; |
| 1127 | } |
| 1128 | else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 1129 | newsrv->flags |= SRV_F_NON_STICK; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1130 | cur_arg ++; |
| 1131 | } |
| 1132 | else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) { |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1133 | newsrv->pp_opts |= SRV_PP_V1; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1134 | cur_arg ++; |
| 1135 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1136 | else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) { |
| 1137 | newsrv->pp_opts |= SRV_PP_V2; |
| 1138 | cur_arg ++; |
| 1139 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1140 | else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) { |
| 1141 | newsrv->check.send_proxy = 1; |
| 1142 | cur_arg ++; |
| 1143 | } |
| 1144 | else if (!strcmp(args[cur_arg], "weight")) { |
| 1145 | int w; |
| 1146 | w = atol(args[cur_arg + 1]); |
| 1147 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 1148 | Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n", |
| 1149 | file, linenum, newsrv->id, SRV_UWGHT_MAX, w); |
| 1150 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1151 | goto out; |
| 1152 | } |
| 1153 | newsrv->uweight = newsrv->iweight = w; |
| 1154 | cur_arg += 2; |
| 1155 | } |
| 1156 | else if (!strcmp(args[cur_arg], "minconn")) { |
| 1157 | newsrv->minconn = atol(args[cur_arg + 1]); |
| 1158 | cur_arg += 2; |
| 1159 | } |
| 1160 | else if (!strcmp(args[cur_arg], "maxconn")) { |
| 1161 | newsrv->maxconn = atol(args[cur_arg + 1]); |
| 1162 | cur_arg += 2; |
| 1163 | } |
| 1164 | else if (!strcmp(args[cur_arg], "maxqueue")) { |
| 1165 | newsrv->maxqueue = atol(args[cur_arg + 1]); |
| 1166 | cur_arg += 2; |
| 1167 | } |
| 1168 | else if (!strcmp(args[cur_arg], "slowstart")) { |
| 1169 | /* slowstart is stored in seconds */ |
| 1170 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 1171 | if (err) { |
| 1172 | Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n", |
| 1173 | file, linenum, *err, newsrv->id); |
| 1174 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1175 | goto out; |
| 1176 | } |
| 1177 | newsrv->slowstart = (val + 999) / 1000; |
| 1178 | cur_arg += 2; |
| 1179 | } |
| 1180 | else if (!defsrv && !strcmp(args[cur_arg], "track")) { |
| 1181 | |
| 1182 | if (!*args[cur_arg + 1]) { |
| 1183 | Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n", |
| 1184 | file, linenum); |
| 1185 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1186 | goto out; |
| 1187 | } |
| 1188 | |
| 1189 | newsrv->trackit = strdup(args[cur_arg + 1]); |
| 1190 | |
| 1191 | cur_arg += 2; |
| 1192 | } |
| 1193 | else if (!defsrv && !strcmp(args[cur_arg], "check")) { |
| 1194 | global.maxsock++; |
| 1195 | do_check = 1; |
| 1196 | cur_arg += 1; |
| 1197 | } |
| 1198 | else if (!defsrv && !strcmp(args[cur_arg], "disabled")) { |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 1199 | newsrv->admin |= SRV_ADMF_FMAINT; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1200 | newsrv->state = SRV_ST_STOPPED; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1201 | newsrv->check.state |= CHK_ST_PAUSED; |
| 1202 | newsrv->check.health = 0; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1203 | cur_arg += 1; |
| 1204 | } |
| 1205 | else if (!defsrv && !strcmp(args[cur_arg], "observe")) { |
| 1206 | if (!strcmp(args[cur_arg + 1], "none")) |
| 1207 | newsrv->observe = HANA_OBS_NONE; |
| 1208 | else if (!strcmp(args[cur_arg + 1], "layer4")) |
| 1209 | newsrv->observe = HANA_OBS_LAYER4; |
| 1210 | else if (!strcmp(args[cur_arg + 1], "layer7")) { |
| 1211 | if (curproxy->mode != PR_MODE_HTTP) { |
| 1212 | Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n", |
| 1213 | file, linenum, args[cur_arg + 1]); |
| 1214 | err_code |= ERR_ALERT; |
| 1215 | } |
| 1216 | newsrv->observe = HANA_OBS_LAYER7; |
| 1217 | } |
| 1218 | else { |
| 1219 | Alert("parsing [%s:%d]: '%s' expects one of 'none', " |
| 1220 | "'layer4', 'layer7' but got '%s'\n", |
| 1221 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1222 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1223 | goto out; |
| 1224 | } |
| 1225 | |
| 1226 | cur_arg += 2; |
| 1227 | } |
| 1228 | else if (!strcmp(args[cur_arg], "on-error")) { |
| 1229 | if (!strcmp(args[cur_arg + 1], "fastinter")) |
| 1230 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 1231 | else if (!strcmp(args[cur_arg + 1], "fail-check")) |
| 1232 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 1233 | else if (!strcmp(args[cur_arg + 1], "sudden-death")) |
| 1234 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 1235 | else if (!strcmp(args[cur_arg + 1], "mark-down")) |
| 1236 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 1237 | else { |
| 1238 | Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', " |
| 1239 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n", |
| 1240 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1241 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1242 | goto out; |
| 1243 | } |
| 1244 | |
| 1245 | cur_arg += 2; |
| 1246 | } |
| 1247 | else if (!strcmp(args[cur_arg], "on-marked-down")) { |
| 1248 | if (!strcmp(args[cur_arg + 1], "shutdown-sessions")) |
| 1249 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 1250 | else { |
| 1251 | Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n", |
| 1252 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1253 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1254 | goto out; |
| 1255 | } |
| 1256 | |
| 1257 | cur_arg += 2; |
| 1258 | } |
| 1259 | else if (!strcmp(args[cur_arg], "on-marked-up")) { |
| 1260 | if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions")) |
| 1261 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 1262 | else { |
| 1263 | Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n", |
| 1264 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1265 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1266 | goto out; |
| 1267 | } |
| 1268 | |
| 1269 | cur_arg += 2; |
| 1270 | } |
| 1271 | else if (!strcmp(args[cur_arg], "error-limit")) { |
| 1272 | if (!*args[cur_arg + 1]) { |
| 1273 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 1274 | file, linenum, args[cur_arg]); |
| 1275 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1276 | goto out; |
| 1277 | } |
| 1278 | |
| 1279 | newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]); |
| 1280 | |
| 1281 | if (newsrv->consecutive_errors_limit <= 0) { |
| 1282 | Alert("parsing [%s:%d]: %s has to be > 0.\n", |
| 1283 | file, linenum, args[cur_arg]); |
| 1284 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1285 | goto out; |
| 1286 | } |
| 1287 | cur_arg += 2; |
| 1288 | } |
| 1289 | else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 1290 | int port_low, port_high; |
| 1291 | struct sockaddr_storage *sk; |
| 1292 | struct protocol *proto; |
| 1293 | |
| 1294 | if (!*args[cur_arg + 1]) { |
| 1295 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n", |
| 1296 | file, linenum, "source", "usesrc", "interface"); |
| 1297 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1298 | goto out; |
| 1299 | } |
| 1300 | |
| 1301 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 1302 | sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL); |
| 1303 | if (!sk) { |
| 1304 | Alert("parsing [%s:%d] : '%s %s' : %s\n", |
| 1305 | file, linenum, args[cur_arg], args[cur_arg+1], errmsg); |
| 1306 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1307 | goto out; |
| 1308 | } |
| 1309 | |
| 1310 | proto = protocol_by_family(sk->ss_family); |
| 1311 | if (!proto || !proto->connect) { |
| 1312 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 1313 | file, linenum, args[cur_arg], args[cur_arg+1]); |
| 1314 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1315 | goto out; |
| 1316 | } |
| 1317 | |
| 1318 | newsrv->conn_src.source_addr = *sk; |
| 1319 | |
| 1320 | if (port_low != port_high) { |
| 1321 | int i; |
| 1322 | |
| 1323 | if (!port_low || !port_high) { |
| 1324 | Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n", |
| 1325 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1326 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1327 | goto out; |
| 1328 | } |
| 1329 | |
| 1330 | if (port_low <= 0 || port_low > 65535 || |
| 1331 | port_high <= 0 || port_high > 65535 || |
| 1332 | port_low > port_high) { |
| 1333 | Alert("parsing [%s:%d] : invalid source port range %d-%d.\n", |
| 1334 | file, linenum, port_low, port_high); |
| 1335 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1336 | goto out; |
| 1337 | } |
| 1338 | newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1); |
| 1339 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 1340 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 1341 | } |
| 1342 | |
| 1343 | cur_arg += 2; |
| 1344 | while (*(args[cur_arg])) { |
| 1345 | if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */ |
| 1346 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) |
| 1347 | #if !defined(CONFIG_HAP_TRANSPARENT) |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 1348 | if (!is_inet_addr(&newsrv->conn_src.source_addr)) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1349 | Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n", |
| 1350 | file, linenum, "usesrc", "source"); |
| 1351 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1352 | goto out; |
| 1353 | } |
| 1354 | #endif |
| 1355 | if (!*args[cur_arg + 1]) { |
| 1356 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n", |
| 1357 | file, linenum, "usesrc"); |
| 1358 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1359 | goto out; |
| 1360 | } |
| 1361 | if (!strcmp(args[cur_arg + 1], "client")) { |
| 1362 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1363 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 1364 | } else if (!strcmp(args[cur_arg + 1], "clientip")) { |
| 1365 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1366 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 1367 | } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) { |
| 1368 | char *name, *end; |
| 1369 | |
| 1370 | name = args[cur_arg+1] + 7; |
| 1371 | while (isspace(*name)) |
| 1372 | name++; |
| 1373 | |
| 1374 | end = name; |
| 1375 | while (*end && !isspace(*end) && *end != ',' && *end != ')') |
| 1376 | end++; |
| 1377 | |
| 1378 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1379 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 1380 | newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1); |
| 1381 | newsrv->conn_src.bind_hdr_len = end - name; |
| 1382 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 1383 | newsrv->conn_src.bind_hdr_name[end-name] = '\0'; |
| 1384 | newsrv->conn_src.bind_hdr_occ = -1; |
| 1385 | |
| 1386 | /* now look for an occurrence number */ |
| 1387 | while (isspace(*end)) |
| 1388 | end++; |
| 1389 | if (*end == ',') { |
| 1390 | end++; |
| 1391 | name = end; |
| 1392 | if (*end == '-') |
| 1393 | end++; |
| 1394 | while (isdigit((int)*end)) |
| 1395 | end++; |
| 1396 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name); |
| 1397 | } |
| 1398 | |
| 1399 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
| 1400 | Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative" |
| 1401 | " occurrences values smaller than %d.\n", |
| 1402 | file, linenum, MAX_HDR_HISTORY); |
| 1403 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1404 | goto out; |
| 1405 | } |
| 1406 | } else { |
| 1407 | struct sockaddr_storage *sk; |
| 1408 | int port1, port2; |
| 1409 | |
| 1410 | sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL); |
| 1411 | if (!sk) { |
| 1412 | Alert("parsing [%s:%d] : '%s %s' : %s\n", |
| 1413 | file, linenum, args[cur_arg], args[cur_arg+1], errmsg); |
| 1414 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1415 | goto out; |
| 1416 | } |
| 1417 | |
| 1418 | proto = protocol_by_family(sk->ss_family); |
| 1419 | if (!proto || !proto->connect) { |
| 1420 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 1421 | file, linenum, args[cur_arg], args[cur_arg+1]); |
| 1422 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1423 | goto out; |
| 1424 | } |
| 1425 | |
| 1426 | if (port1 != port2) { |
| 1427 | Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n", |
| 1428 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 1429 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1430 | goto out; |
| 1431 | } |
| 1432 | newsrv->conn_src.tproxy_addr = *sk; |
| 1433 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 1434 | } |
| 1435 | global.last_checks |= LSTCHK_NETADM; |
| 1436 | #if !defined(CONFIG_HAP_TRANSPARENT) |
| 1437 | global.last_checks |= LSTCHK_CTTPROXY; |
| 1438 | #endif |
| 1439 | cur_arg += 2; |
| 1440 | continue; |
| 1441 | #else /* no TPROXY support */ |
| 1442 | Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n", |
| 1443 | file, linenum, "usesrc"); |
| 1444 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1445 | goto out; |
| 1446 | #endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */ |
| 1447 | } /* "usesrc" */ |
| 1448 | |
| 1449 | if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */ |
| 1450 | #ifdef SO_BINDTODEVICE |
| 1451 | if (!*args[cur_arg + 1]) { |
| 1452 | Alert("parsing [%s:%d] : '%s' : missing interface name.\n", |
| 1453 | file, linenum, args[0]); |
| 1454 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1455 | goto out; |
| 1456 | } |
| 1457 | free(newsrv->conn_src.iface_name); |
| 1458 | newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]); |
| 1459 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 1460 | global.last_checks |= LSTCHK_NETADM; |
| 1461 | #else |
| 1462 | Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n", |
| 1463 | file, linenum, args[0], args[cur_arg]); |
| 1464 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1465 | goto out; |
| 1466 | #endif |
| 1467 | cur_arg += 2; |
| 1468 | continue; |
| 1469 | } |
| 1470 | /* this keyword in not an option of "source" */ |
| 1471 | break; |
| 1472 | } /* while */ |
| 1473 | } |
| 1474 | else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */ |
| 1475 | Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n", |
| 1476 | file, linenum, "usesrc", "source"); |
| 1477 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1478 | goto out; |
| 1479 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1480 | else if (!defsrv && !strcmp(args[cur_arg], "namespace")) { |
| 1481 | #ifdef CONFIG_HAP_NS |
| 1482 | char *arg = args[cur_arg + 1]; |
| 1483 | if (!strcmp(arg, "*")) { |
| 1484 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 1485 | } else { |
| 1486 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 1487 | |
| 1488 | if (newsrv->netns == NULL) |
| 1489 | newsrv->netns = netns_store_insert(arg); |
| 1490 | |
| 1491 | if (newsrv->netns == NULL) { |
| 1492 | Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]); |
| 1493 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1494 | goto out; |
| 1495 | } |
| 1496 | } |
| 1497 | #else |
| 1498 | Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n", |
| 1499 | file, linenum, args[0], args[cur_arg]); |
| 1500 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1501 | goto out; |
| 1502 | #endif |
| 1503 | cur_arg += 2; |
| 1504 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1505 | else { |
| 1506 | static int srv_dumped; |
| 1507 | struct srv_kw *kw; |
| 1508 | char *err; |
| 1509 | |
| 1510 | kw = srv_find_kw(args[cur_arg]); |
| 1511 | if (kw) { |
| 1512 | char *err = NULL; |
| 1513 | int code; |
| 1514 | |
| 1515 | if (!kw->parse) { |
| 1516 | Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n", |
| 1517 | file, linenum, args[0], args[1], args[cur_arg]); |
| 1518 | cur_arg += 1 + kw->skip ; |
| 1519 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1520 | goto out; |
| 1521 | } |
| 1522 | |
| 1523 | if (defsrv && !kw->default_ok) { |
| 1524 | Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n", |
| 1525 | file, linenum, args[0], args[1], args[cur_arg]); |
| 1526 | cur_arg += 1 + kw->skip ; |
| 1527 | err_code |= ERR_ALERT; |
| 1528 | continue; |
| 1529 | } |
| 1530 | |
| 1531 | code = kw->parse(args, &cur_arg, curproxy, newsrv, &err); |
| 1532 | err_code |= code; |
| 1533 | |
| 1534 | if (code) { |
| 1535 | if (err && *err) { |
| 1536 | indent_msg(&err, 2); |
| 1537 | Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err); |
| 1538 | } |
| 1539 | else |
| 1540 | Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n", |
| 1541 | file, linenum, args[0], args[1], args[cur_arg]); |
| 1542 | if (code & ERR_FATAL) { |
| 1543 | free(err); |
| 1544 | cur_arg += 1 + kw->skip; |
| 1545 | goto out; |
| 1546 | } |
| 1547 | } |
| 1548 | free(err); |
| 1549 | cur_arg += 1 + kw->skip; |
| 1550 | continue; |
| 1551 | } |
| 1552 | |
| 1553 | err = NULL; |
| 1554 | if (!srv_dumped) { |
| 1555 | srv_dump_kws(&err); |
| 1556 | indent_msg(&err, 4); |
| 1557 | srv_dumped = 1; |
| 1558 | } |
| 1559 | |
| 1560 | Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n", |
| 1561 | file, linenum, args[0], args[1], args[cur_arg], |
| 1562 | err ? " Registered keywords :" : "", err ? err : ""); |
| 1563 | free(err); |
| 1564 | |
| 1565 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1566 | goto out; |
| 1567 | } |
| 1568 | } |
| 1569 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1570 | if (do_check) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1571 | const char *ret; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1572 | |
| 1573 | if (newsrv->trackit) { |
| 1574 | Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n", |
| 1575 | file, linenum); |
| 1576 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1577 | goto out; |
| 1578 | } |
| 1579 | |
| 1580 | /* If neither a port nor an addr was specified and no check transport |
| 1581 | * layer is forced, then the transport layer used by the checks is the |
| 1582 | * same as for the production traffic. Otherwise we use raw_sock by |
| 1583 | * default, unless one is specified. |
| 1584 | */ |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1585 | if (!newsrv->check.port && !is_addr(&newsrv->check.addr)) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1586 | #ifdef USE_OPENSSL |
| 1587 | newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL)); |
| 1588 | #endif |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1589 | newsrv->check.send_proxy |= (newsrv->pp_opts); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1590 | } |
| 1591 | /* try to get the port from check_core.addr if check.port not set */ |
| 1592 | if (!newsrv->check.port) |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1593 | newsrv->check.port = get_host_port(&newsrv->check.addr); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1594 | |
| 1595 | if (!newsrv->check.port) |
| 1596 | newsrv->check.port = realport; /* by default */ |
| 1597 | |
| 1598 | if (!newsrv->check.port) { |
| 1599 | /* not yet valid, because no port was set on |
| 1600 | * the server either. We'll check if we have |
| 1601 | * a known port on the first listener. |
| 1602 | */ |
| 1603 | struct listener *l; |
| 1604 | |
| 1605 | list_for_each_entry(l, &curproxy->conf.listeners, by_fe) { |
| 1606 | newsrv->check.port = get_host_port(&l->addr); |
| 1607 | if (newsrv->check.port) |
| 1608 | break; |
| 1609 | } |
| 1610 | } |
| 1611 | /* |
| 1612 | * 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] | 1613 | * be a 'connect' one when checking an IPv4/IPv6 server. |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1614 | */ |
Willy Tarreau | 5cf0b52 | 2014-05-09 23:59:19 +0200 | [diff] [blame] | 1615 | if (!newsrv->check.port && |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1616 | (is_inet_addr(&newsrv->check.addr) || |
| 1617 | (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1618 | struct tcpcheck_rule *n = NULL, *r = NULL; |
| 1619 | struct list *l; |
| 1620 | |
| 1621 | r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n; |
| 1622 | if (!r) { |
| 1623 | Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n", |
| 1624 | file, linenum, newsrv->id); |
| 1625 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1626 | goto out; |
| 1627 | } |
| 1628 | if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) { |
| 1629 | 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", |
| 1630 | file, linenum, newsrv->id); |
| 1631 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1632 | goto out; |
| 1633 | } |
| 1634 | else { |
| 1635 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 1636 | l = &newsrv->proxy->tcpcheck_rules; |
| 1637 | list_for_each_entry(n, l, list) { |
| 1638 | r = (struct tcpcheck_rule *)n->list.p; |
| 1639 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) { |
| 1640 | 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", |
| 1641 | file, linenum, newsrv->id); |
| 1642 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1643 | goto out; |
| 1644 | } |
| 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | /* note: check type will be set during the config review phase */ |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1650 | ret = init_check(&newsrv->check, 0); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1651 | if (ret) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1652 | Alert("parsing [%s:%d] : %s.\n", file, linenum, ret); |
| 1653 | err_code |= ERR_ALERT | ERR_ABORT; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1654 | goto out; |
| 1655 | } |
| 1656 | |
| 1657 | newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 1658 | } |
| 1659 | |
| 1660 | if (do_agent) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1661 | const char *ret; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1662 | |
| 1663 | if (!newsrv->agent.port) { |
| 1664 | Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n", |
| 1665 | file, linenum, newsrv->id); |
| 1666 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1667 | goto out; |
| 1668 | } |
| 1669 | |
| 1670 | if (!newsrv->agent.inter) |
| 1671 | newsrv->agent.inter = newsrv->check.inter; |
| 1672 | |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1673 | ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1674 | if (ret) { |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 1675 | Alert("parsing [%s:%d] : %s.\n", file, linenum, ret); |
| 1676 | err_code |= ERR_ALERT | ERR_ABORT; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1677 | goto out; |
| 1678 | } |
| 1679 | |
| 1680 | newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 1681 | } |
| 1682 | |
| 1683 | if (!defsrv) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 1684 | if (newsrv->flags & SRV_F_BACKUP) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1685 | curproxy->srv_bck++; |
| 1686 | else |
| 1687 | curproxy->srv_act++; |
| 1688 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 1689 | srv_lb_commit_status(newsrv); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | return 0; |
| 1693 | |
| 1694 | out: |
| 1695 | free(errmsg); |
| 1696 | return err_code; |
| 1697 | } |
| 1698 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1699 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1700 | * Local variables: |
| 1701 | * c-indent-level: 8 |
| 1702 | * c-basic-offset: 8 |
| 1703 | * End: |
| 1704 | */ |