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