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 | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | #include <common/config.h> |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 15 | #include <common/errors.h> |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 16 | #include <common/time.h> |
| 17 | |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 18 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 20 | /* List head of all known server keywords */ |
| 21 | static struct srv_kw_list srv_keywords = { |
| 22 | .list = LIST_HEAD_INIT(srv_keywords.list) |
| 23 | }; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 24 | |
Simon Horman | a360844 | 2013-11-01 16:46:15 +0900 | [diff] [blame] | 25 | int srv_downtime(const struct server *s) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 26 | { |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 27 | if ((s->state & SRV_RUNNING) && s->last_change < now.tv_sec) // ignore negative time |
| 28 | return s->down_time; |
| 29 | |
| 30 | return now.tv_sec - s->last_change + s->down_time; |
| 31 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 33 | int srv_lastsession(const struct server *s) |
| 34 | { |
| 35 | if (s->counters.last_sess) |
| 36 | return now.tv_sec - s->counters.last_sess; |
| 37 | |
| 38 | return -1; |
| 39 | } |
| 40 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 41 | int srv_getinter(const struct check *check) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 42 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 43 | const struct server *s = check->server; |
| 44 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 45 | 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] | 46 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 47 | |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 48 | if (!(s->state & SRV_RUNNING) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 49 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 50 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 51 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 54 | /* |
| 55 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 56 | * parsing sessions. |
| 57 | */ |
| 58 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 59 | { |
| 60 | LIST_ADDQ(&srv_keywords.list, &kwl->list); |
| 61 | } |
| 62 | |
| 63 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 64 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 65 | * find one with a valid ->parse() function. This way it is possible to declare |
| 66 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 67 | * if some options are met. Note that if the requested keyword contains an |
| 68 | * opening parenthesis, everything from this point is ignored. |
| 69 | */ |
| 70 | struct srv_kw *srv_find_kw(const char *kw) |
| 71 | { |
| 72 | int index; |
| 73 | const char *kwend; |
| 74 | struct srv_kw_list *kwl; |
| 75 | struct srv_kw *ret = NULL; |
| 76 | |
| 77 | kwend = strchr(kw, '('); |
| 78 | if (!kwend) |
| 79 | kwend = kw + strlen(kw); |
| 80 | |
| 81 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 82 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 83 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 84 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 85 | if (kwl->kw[index].parse) |
| 86 | return &kwl->kw[index]; /* found it !*/ |
| 87 | else |
| 88 | ret = &kwl->kw[index]; /* may be OK */ |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 96 | * unsupported keywords are only dumped if their supported form was not |
| 97 | * found. |
| 98 | */ |
| 99 | void srv_dump_kws(char **out) |
| 100 | { |
| 101 | struct srv_kw_list *kwl; |
| 102 | int index; |
| 103 | |
| 104 | *out = NULL; |
| 105 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 106 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 107 | if (kwl->kw[index].parse || |
| 108 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 109 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 110 | kwl->scope, |
| 111 | kwl->kw[index].kw, |
| 112 | kwl->kw[index].skip ? " <arg>" : "", |
| 113 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 114 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 119 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 120 | /* parse the "id" server keyword */ |
| 121 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 122 | { |
| 123 | struct eb32_node *node; |
| 124 | |
| 125 | if (!*args[*cur_arg + 1]) { |
| 126 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 127 | return ERR_ALERT | ERR_FATAL; |
| 128 | } |
| 129 | |
| 130 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 131 | newsrv->conf.id.key = newsrv->puid; |
| 132 | |
| 133 | if (newsrv->puid <= 0) { |
| 134 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 135 | return ERR_ALERT | ERR_FATAL; |
| 136 | } |
| 137 | |
| 138 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 139 | if (node) { |
| 140 | struct server *target = container_of(node, struct server, conf.id); |
| 141 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 142 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 143 | target->id); |
| 144 | return ERR_ALERT | ERR_FATAL; |
| 145 | } |
| 146 | |
| 147 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* Note: must not be declared <const> as its list will be overwritten. |
| 152 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 153 | * all code contributors. |
| 154 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 155 | * the config parser can report an appropriate error when a known keyword was |
| 156 | * not enabled. |
| 157 | */ |
| 158 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
| 159 | { "id", srv_parse_id, 1, 0 }, /* set id# of server */ |
| 160 | { NULL, NULL, 0 }, |
| 161 | }}; |
| 162 | |
| 163 | __attribute__((constructor)) |
| 164 | static void __listener_init(void) |
| 165 | { |
| 166 | srv_register_keywords(&srv_kws); |
| 167 | } |
| 168 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 169 | /* Recomputes the server's eweight based on its state, uweight, the current time, |
| 170 | * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup |
| 171 | * state is automatically disabled if the time is elapsed. |
| 172 | */ |
| 173 | void server_recalc_eweight(struct server *sv) |
| 174 | { |
| 175 | struct proxy *px = sv->proxy; |
| 176 | unsigned w; |
| 177 | |
| 178 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 179 | /* go to full throttle if the slowstart interval is reached */ |
| 180 | sv->state &= ~SRV_WARMINGUP; |
| 181 | } |
| 182 | |
| 183 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 184 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 185 | */ |
| 186 | if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
| 187 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 188 | else |
| 189 | w = px->lbprm.wdiv; |
| 190 | |
| 191 | sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult; |
| 192 | |
| 193 | /* now propagate the status change to any LB algorithms */ |
| 194 | if (px->lbprm.update_server_eweight) |
| 195 | px->lbprm.update_server_eweight(sv); |
| 196 | else if (sv->eweight) { |
| 197 | if (px->lbprm.set_server_status_up) |
| 198 | px->lbprm.set_server_status_up(sv); |
| 199 | } |
| 200 | else { |
| 201 | if (px->lbprm.set_server_status_down) |
| 202 | px->lbprm.set_server_status_down(sv); |
| 203 | } |
| 204 | } |
| 205 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 206 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 207 | * Parses weight_str and configures sv accordingly. |
| 208 | * Returns NULL on success, error message string otherwise. |
| 209 | */ |
| 210 | const char *server_parse_weight_change_request(struct server *sv, |
| 211 | const char *weight_str) |
| 212 | { |
| 213 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 214 | long int w; |
| 215 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 216 | |
| 217 | px = sv->proxy; |
| 218 | |
| 219 | /* if the weight is terminated with '%', it is set relative to |
| 220 | * the initial weight, otherwise it is absolute. |
| 221 | */ |
| 222 | if (!*weight_str) |
| 223 | return "Require <weight> or <weight%>.\n"; |
| 224 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 225 | w = strtol(weight_str, &end, 10); |
| 226 | if (end == weight_str) |
| 227 | return "Empty weight string empty or preceded by garbage"; |
| 228 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 229 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 230 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 231 | /* Avoid integer overflow */ |
| 232 | if (w > 25600) |
| 233 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 234 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 235 | if (w > 256) |
| 236 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 237 | } |
| 238 | else if (w < 0 || w > 256) |
| 239 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 240 | else if (end[0] != '\0') |
| 241 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 242 | |
| 243 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 244 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 245 | |
| 246 | sv->uweight = w; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 247 | server_recalc_eweight(sv); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 248 | |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | * Local variables: |
| 254 | * c-indent-level: 8 |
| 255 | * c-basic-offset: 8 |
| 256 | * End: |
| 257 | */ |