blob: d91c726a4590a7e9e51c7baf813297e90db563b3 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
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 Tarreaue3ba5f02006-06-29 18:54:54 +020014#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020015#include <common/errors.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020016#include <common/time.h>
17
Willy Tarreauec6c5df2008-07-15 00:22:45 +020018#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019
Willy Tarreau21faa912012-10-10 08:27:36 +020020/* List head of all known server keywords */
21static struct srv_kw_list srv_keywords = {
22 .list = LIST_HEAD_INIT(srv_keywords.list)
23};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024
Simon Hormana3608442013-11-01 16:46:15 +090025int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020026{
Krzysztof Oledzki85130942007-10-22 16:21:10 +020027 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 Tarreaubaaee002006-06-26 02:48:02 +020032
Simon Horman4a741432013-02-23 15:35:38 +090033int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020034{
Simon Horman4a741432013-02-23 15:35:38 +090035 const struct server *s = check->server;
36
Willy Tarreauff5ae352013-12-11 20:36:34 +010037 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090038 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010039
Simon Horman125d0992013-02-24 17:23:38 +090040 if (!(s->state & SRV_RUNNING) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090041 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010042
Simon Horman4a741432013-02-23 15:35:38 +090043 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010044}
45
Willy Tarreau21faa912012-10-10 08:27:36 +020046/*
47 * Registers the server keyword list <kwl> as a list of valid keywords for next
48 * parsing sessions.
49 */
50void 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 */
62struct 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 */
91void 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 Oledzki5259dfe2008-01-21 01:54:06 +0100111
Willy Tarreaudff55432012-10-10 17:51:05 +0200112/* parse the "id" server keyword */
113static 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 */
150static 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))
156static void __listener_init(void)
157{
158 srv_register_keywords(&srv_kws);
159}
160
Willy Tarreau004e0452013-11-21 11:22:01 +0100161/* 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 */
165void 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 Tarreaubaaee002006-06-26 02:48:02 +0200198/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900199 * Parses weight_str and configures sv accordingly.
200 * Returns NULL on success, error message string otherwise.
201 */
202const char *server_parse_weight_change_request(struct server *sv,
203 const char *weight_str)
204{
205 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900206 long int w;
207 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900208
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 Hormanb796afa2013-02-12 10:45:53 +0900217 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 Horman58b5d292013-02-12 10:45:52 +0900221 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900222 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900223 /* Avoid integer overflow */
224 if (w > 25600)
225 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900226 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900227 if (w > 256)
228 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900229 }
230 else if (w < 0 || w > 256)
231 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900232 else if (end[0] != '\0')
233 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900234
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 Tarreau004e0452013-11-21 11:22:01 +0100239 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900240
241 return NULL;
242}
243
244/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 * Local variables:
246 * c-indent-level: 8
247 * c-basic-offset: 8
248 * End:
249 */