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 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 37 | if ((s->state & SRV_CHECKED) && (s->health == s->rise + s->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 | |
| 40 | if (!(s->state & SRV_RUNNING) && s->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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 162 | * Parses weight_str and configures sv accordingly. |
| 163 | * Returns NULL on success, error message string otherwise. |
| 164 | */ |
| 165 | const char *server_parse_weight_change_request(struct server *sv, |
| 166 | const char *weight_str) |
| 167 | { |
| 168 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 169 | long int w; |
| 170 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 171 | |
| 172 | px = sv->proxy; |
| 173 | |
| 174 | /* if the weight is terminated with '%', it is set relative to |
| 175 | * the initial weight, otherwise it is absolute. |
| 176 | */ |
| 177 | if (!*weight_str) |
| 178 | return "Require <weight> or <weight%>.\n"; |
| 179 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 180 | w = strtol(weight_str, &end, 10); |
| 181 | if (end == weight_str) |
| 182 | return "Empty weight string empty or preceded by garbage"; |
| 183 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 184 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 185 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 186 | /* Avoid integer overflow */ |
| 187 | if (w > 25600) |
| 188 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 189 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 190 | if (w > 256) |
| 191 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 192 | } |
| 193 | else if (w < 0 || w > 256) |
| 194 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 195 | else if (end[0] != '\0') |
| 196 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 197 | |
| 198 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 199 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 200 | |
| 201 | sv->uweight = w; |
| 202 | |
| 203 | if (px->lbprm.algo & BE_LB_PROP_DYN) { |
| 204 | /* we must take care of not pushing the server to full throttle during slow starts */ |
| 205 | if ((sv->state & SRV_WARMINGUP)) |
| 206 | sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart; |
| 207 | else |
| 208 | sv->eweight = BE_WEIGHT_SCALE; |
| 209 | sv->eweight *= sv->uweight; |
| 210 | } else { |
| 211 | sv->eweight = sv->uweight; |
| 212 | } |
| 213 | |
| 214 | /* static LB algorithms are a bit harder to update */ |
| 215 | if (px->lbprm.update_server_eweight) |
| 216 | px->lbprm.update_server_eweight(sv); |
| 217 | else if (sv->eweight) { |
| 218 | if (px->lbprm.set_server_status_up) |
| 219 | px->lbprm.set_server_status_up(sv); |
| 220 | } |
| 221 | else { |
| 222 | if (px->lbprm.set_server_status_down) |
| 223 | px->lbprm.set_server_status_down(sv); |
| 224 | } |
| 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 230 | * Local variables: |
| 231 | * c-indent-level: 8 |
| 232 | * c-basic-offset: 8 |
| 233 | * End: |
| 234 | */ |