blob: 98a9fbe9c78bc7b37bf14375e2f6a9e26fe966a1 [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
Willy Tarreau21faa912012-10-10 08:27:36 +020025int srv_downtime(struct server *s)
26{
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
Willy Tarreau21faa912012-10-10 08:27:36 +020033int srv_getinter(struct server *s)
34{
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010035 if ((s->state & SRV_CHECKED) && (s->health == s->rise + s->fall - 1))
36 return s->inter;
37
38 if (!(s->state & SRV_RUNNING) && s->health==0)
39 return (s->downinter)?(s->downinter):(s->inter);
40
41 return (s->fastinter)?(s->fastinter):(s->inter);
42}
43
Willy Tarreau21faa912012-10-10 08:27:36 +020044/*
45 * Registers the server keyword list <kwl> as a list of valid keywords for next
46 * parsing sessions.
47 */
48void srv_register_keywords(struct srv_kw_list *kwl)
49{
50 LIST_ADDQ(&srv_keywords.list, &kwl->list);
51}
52
53/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
54 * keyword is found with a NULL ->parse() function, then an attempt is made to
55 * find one with a valid ->parse() function. This way it is possible to declare
56 * platform-dependant, known keywords as NULL, then only declare them as valid
57 * if some options are met. Note that if the requested keyword contains an
58 * opening parenthesis, everything from this point is ignored.
59 */
60struct srv_kw *srv_find_kw(const char *kw)
61{
62 int index;
63 const char *kwend;
64 struct srv_kw_list *kwl;
65 struct srv_kw *ret = NULL;
66
67 kwend = strchr(kw, '(');
68 if (!kwend)
69 kwend = kw + strlen(kw);
70
71 list_for_each_entry(kwl, &srv_keywords.list, list) {
72 for (index = 0; kwl->kw[index].kw != NULL; index++) {
73 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
74 kwl->kw[index].kw[kwend-kw] == 0) {
75 if (kwl->kw[index].parse)
76 return &kwl->kw[index]; /* found it !*/
77 else
78 ret = &kwl->kw[index]; /* may be OK */
79 }
80 }
81 }
82 return ret;
83}
84
85/* Dumps all registered "server" keywords to the <out> string pointer. The
86 * unsupported keywords are only dumped if their supported form was not
87 * found.
88 */
89void srv_dump_kws(char **out)
90{
91 struct srv_kw_list *kwl;
92 int index;
93
94 *out = NULL;
95 list_for_each_entry(kwl, &srv_keywords.list, list) {
96 for (index = 0; kwl->kw[index].kw != NULL; index++) {
97 if (kwl->kw[index].parse ||
98 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
99 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
100 kwl->scope,
101 kwl->kw[index].kw,
102 kwl->kw[index].skip ? " <arg>" : "",
103 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
104 kwl->kw[index].parse ? "" : " (not supported)");
105 }
106 }
107 }
108}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100109
Willy Tarreaudff55432012-10-10 17:51:05 +0200110/* parse the "id" server keyword */
111static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
112{
113 struct eb32_node *node;
114
115 if (!*args[*cur_arg + 1]) {
116 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
117 return ERR_ALERT | ERR_FATAL;
118 }
119
120 newsrv->puid = atol(args[*cur_arg + 1]);
121 newsrv->conf.id.key = newsrv->puid;
122
123 if (newsrv->puid <= 0) {
124 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
125 return ERR_ALERT | ERR_FATAL;
126 }
127
128 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
129 if (node) {
130 struct server *target = container_of(node, struct server, conf.id);
131 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
132 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
133 target->id);
134 return ERR_ALERT | ERR_FATAL;
135 }
136
137 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
138 return 0;
139}
140
141/* Note: must not be declared <const> as its list will be overwritten.
142 * Please take care of keeping this list alphabetically sorted, doing so helps
143 * all code contributors.
144 * Optional keywords are also declared with a NULL ->parse() function so that
145 * the config parser can report an appropriate error when a known keyword was
146 * not enabled.
147 */
148static struct srv_kw_list srv_kws = { "ALL", { }, {
149 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
150 { NULL, NULL, 0 },
151}};
152
153__attribute__((constructor))
154static void __listener_init(void)
155{
156 srv_register_keywords(&srv_kws);
157}
158
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900160 * Parses weight_str and configures sv accordingly.
161 * Returns NULL on success, error message string otherwise.
162 */
163const char *server_parse_weight_change_request(struct server *sv,
164 const char *weight_str)
165{
166 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900167 long int w;
168 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900169
170 px = sv->proxy;
171
172 /* if the weight is terminated with '%', it is set relative to
173 * the initial weight, otherwise it is absolute.
174 */
175 if (!*weight_str)
176 return "Require <weight> or <weight%>.\n";
177
Simon Hormanb796afa2013-02-12 10:45:53 +0900178 w = strtol(weight_str, &end, 10);
179 if (end == weight_str)
180 return "Empty weight string empty or preceded by garbage";
181 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +0900182 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900183 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900184 /* Avoid integer overflow */
185 if (w > 25600)
186 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900187 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900188 if (w > 256)
189 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900190 }
191 else if (w < 0 || w > 256)
192 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900193 else if (end[0] != '\0')
194 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900195
196 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
197 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
198
199 sv->uweight = w;
200
201 if (px->lbprm.algo & BE_LB_PROP_DYN) {
202 /* we must take care of not pushing the server to full throttle during slow starts */
203 if ((sv->state & SRV_WARMINGUP))
204 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
205 else
206 sv->eweight = BE_WEIGHT_SCALE;
207 sv->eweight *= sv->uweight;
208 } else {
209 sv->eweight = sv->uweight;
210 }
211
212 /* static LB algorithms are a bit harder to update */
213 if (px->lbprm.update_server_eweight)
214 px->lbprm.update_server_eweight(sv);
215 else if (sv->eweight) {
216 if (px->lbprm.set_server_status_up)
217 px->lbprm.set_server_status_up(sv);
218 }
219 else {
220 if (px->lbprm.set_server_status_down)
221 px->lbprm.set_server_status_down(sv);
222 }
223
224 return NULL;
225}
226
227/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228 * Local variables:
229 * c-indent-level: 8
230 * c-basic-offset: 8
231 * End:
232 */