blob: ccae58be261cf7e4131987469ed3fbf38dc9001a [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
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050033int 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 Horman4a741432013-02-23 15:35:38 +090041int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020042{
Simon Horman4a741432013-02-23 15:35:38 +090043 const struct server *s = check->server;
44
Willy Tarreauff5ae352013-12-11 20:36:34 +010045 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090046 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010047
Simon Horman125d0992013-02-24 17:23:38 +090048 if (!(s->state & SRV_RUNNING) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090049 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010050
Simon Horman4a741432013-02-23 15:35:38 +090051 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010052}
53
Willy Tarreau21faa912012-10-10 08:27:36 +020054/*
55 * Registers the server keyword list <kwl> as a list of valid keywords for next
56 * parsing sessions.
57 */
58void 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 */
70struct 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 */
99void 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 Oledzki5259dfe2008-01-21 01:54:06 +0100119
Willy Tarreaudff55432012-10-10 17:51:05 +0200120/* parse the "id" server keyword */
121static 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 */
158static 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))
164static void __listener_init(void)
165{
166 srv_register_keywords(&srv_kws);
167}
168
Willy Tarreau004e0452013-11-21 11:22:01 +0100169/* 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 */
173void 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 Tarreaubaaee002006-06-26 02:48:02 +0200206/*
Simon Horman7d09b9a2013-02-12 10:45:51 +0900207 * Parses weight_str and configures sv accordingly.
208 * Returns NULL on success, error message string otherwise.
209 */
210const char *server_parse_weight_change_request(struct server *sv,
211 const char *weight_str)
212{
213 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +0900214 long int w;
215 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900216
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 Hormanb796afa2013-02-12 10:45:53 +0900225 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 Horman58b5d292013-02-12 10:45:52 +0900229 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +0900230 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +0900231 /* Avoid integer overflow */
232 if (w > 25600)
233 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900234 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +0900235 if (w > 256)
236 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +0900237 }
238 else if (w < 0 || w > 256)
239 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +0900240 else if (end[0] != '\0')
241 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +0900242
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 Tarreau004e0452013-11-21 11:22:01 +0100247 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +0900248
249 return NULL;
250}
251
252/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253 * Local variables:
254 * c-indent-level: 8
255 * c-basic-offset: 8
256 * End:
257 */