blob: 91db0268c82104f352ce57bb2ac8b8bc5d1e61a5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreaue8c66af2008-01-13 18:40:14 +01004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +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 Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020018#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <unistd.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/compat.h>
27#include <common/config.h>
28#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020029#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
32#include <types/global.h>
33#include <types/polling.h>
34#include <types/proxy.h>
35#include <types/session.h>
36
37#include <proto/backend.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010038#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/fd.h>
40#include <proto/log.h>
41#include <proto/queue.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010042#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010043#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010044#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/server.h>
46#include <proto/task.h>
47
Willy Tarreau48494c02007-11-30 10:41:39 +010048/* sends a log message when a backend goes down, and also sets last
49 * change date.
50 */
51static void set_backend_down(struct proxy *be)
52{
53 be->last_change = now.tv_sec;
54 be->down_trans++;
55
56 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
57 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
58}
59
60/* Redistribute pending connections when a server goes down. The number of
61 * connections redistributed is returned.
62 */
63static int redistribute_pending(struct server *s)
64{
65 struct pendconn *pc, *pc_bck, *pc_end;
66 int xferred = 0;
67
68 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
69 struct session *sess = pc->sess;
70 if (sess->be->options & PR_O_REDISP) {
71 /* The REDISP option was specified. We will ignore
72 * cookie and force to balance or use the dispatcher.
73 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010074
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010075 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +010076 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010077
Willy Tarreau48494c02007-11-30 10:41:39 +010078 pendconn_free(pc);
79 task_wakeup(sess->task);
80 xferred++;
81 }
82 }
83 return xferred;
84}
85
86/* Check for pending connections at the backend, and assign some of them to
87 * the server coming up. The server's weight is checked before being assigned
88 * connections it may not be able to handle. The total number of transferred
89 * connections is returned.
90 */
91static int check_for_pending(struct server *s)
92{
93 int xferred;
94
95 if (!s->eweight)
96 return 0;
97
98 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
99 struct session *sess;
100 struct pendconn *p;
101
102 p = pendconn_from_px(s->proxy);
103 if (!p)
104 break;
105 p->sess->srv = s;
106 sess = p->sess;
107 pendconn_free(p);
108 task_wakeup(sess->task);
109 }
110 return xferred;
111}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112
113/* Sets server <s> down, notifies by all available means, recounts the
114 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200115 * possible to other servers. It automatically recomputes the number of
116 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 */
Willy Tarreau83749182007-04-15 20:56:27 +0200118static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100120 struct server *srv;
121 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 int xferred;
123
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100124 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100125 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200126
127 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100128 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100129 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130
131 /* we might have sessions queued on this server and waiting for
132 * a connection. Those which are redispatchable will be queued
133 * to another server or to the proxy itself.
134 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100135 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100136
137 msg.len = 0;
138 msg.str = trash;
139
140 chunk_printf(&msg, sizeof(trash),
141 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
142 s->proxy->id, s->id);
143
144 if (s->tracked)
145 chunk_printf(&msg, sizeof(trash), " via %s/%s",
146 s->tracked->proxy->id, s->tracked->id);
147
148 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers left.%s"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 " %d sessions active, %d requeued, %d remaining in queue.\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100150 s->proxy->srv_act, s->proxy->srv_bck,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
152 s->cur_sess, xferred, s->nbpend);
153
154 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200155
Willy Tarreau48494c02007-11-30 10:41:39 +0100156 /* we don't send an alert if the server was previously paused */
157 if (srv_was_paused)
158 send_log(s->proxy, LOG_NOTICE, "%s", trash);
159 else
160 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200161
Willy Tarreau48494c02007-11-30 10:41:39 +0100162 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
163 set_backend_down(s->proxy);
164
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 s->down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100166
167 if (s->state && SRV_CHECKED)
168 for(srv = s->tracknext; srv; srv = srv->tracknext)
169 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100171
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172 s->health = 0; /* failure */
173}
174
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100175static void set_server_up(struct server *s) {
176
177 struct server *srv;
178 struct chunk msg;
179 int xferred;
180
181 if (s->health == s->rise || s->tracked) {
182 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
183 if (s->proxy->last_change < now.tv_sec) // ignore negative times
184 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
185 s->proxy->last_change = now.tv_sec;
186 }
187
188 if (s->last_change < now.tv_sec) // ignore negative times
189 s->down_time += now.tv_sec - s->last_change;
190
191 s->last_change = now.tv_sec;
192 s->state |= SRV_RUNNING;
193
194 if (s->slowstart > 0) {
195 s->state |= SRV_WARMINGUP;
196 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
197 /* For dynamic algorithms, start at the first step of the weight,
198 * without multiplying by BE_WEIGHT_SCALE.
199 */
200 s->eweight = s->uweight;
201 if (s->proxy->lbprm.update_server_eweight)
202 s->proxy->lbprm.update_server_eweight(s);
203 }
204 }
205 s->proxy->lbprm.set_server_status_up(s);
206
207 /* check if we can handle some connections queued at the proxy. We
208 * will take as many as we can handle.
209 */
210 xferred = check_for_pending(s);
211
212 msg.len = 0;
213 msg.str = trash;
214
215 chunk_printf(&msg, sizeof(trash),
216 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
217 s->proxy->id, s->id);
218
219 if (s->tracked)
220 chunk_printf(&msg, sizeof(trash), " via %s/%s",
221 s->tracked->proxy->id, s->tracked->id);
222
223 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
224 " %d sessions requeued, %d total in queue.\n",
225 s->proxy->srv_act, s->proxy->srv_bck,
226 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
227 s->cur_sess, xferred, s->nbpend);
228
229 Warning("%s", trash);
230 send_log(s->proxy, LOG_NOTICE, "%s", trash);
231
232 if (s->state && SRV_CHECKED)
233 for(srv = s->tracknext; srv; srv = srv->tracknext)
234 set_server_up(srv);
235 }
236
237 if (s->health >= s->rise)
238 s->health = s->rise + s->fall - 1; /* OK now */
239
240}
241
242static void set_server_disabled(struct server *s) {
243
244 struct server *srv;
245 struct chunk msg;
246 int xferred;
247
248 s->state |= SRV_GOINGDOWN;
249 s->proxy->lbprm.set_server_status_down(s);
250
251 /* we might have sessions queued on this server and waiting for
252 * a connection. Those which are redispatchable will be queued
253 * to another server or to the proxy itself.
254 */
255 xferred = redistribute_pending(s);
256
257 msg.len = 0;
258 msg.str = trash;
259
260 chunk_printf(&msg, sizeof(trash),
261 "Load-balancing on %sServer %s/%s is disabled",
262 s->state & SRV_BACKUP ? "Backup " : "",
263 s->proxy->id, s->id);
264
265 if (s->tracked)
266 chunk_printf(&msg, sizeof(trash), " via %s/%s",
267 s->tracked->proxy->id, s->tracked->id);
268
269
270 chunk_printf(&msg, sizeof(trash),". %d active and %d backup servers online.%s"
271 " %d sessions requeued, %d total in queue.\n",
272 s->proxy->srv_act, s->proxy->srv_bck,
273 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
274 xferred, s->nbpend);
275
276 Warning("%s", trash);
277
278 send_log(s->proxy, LOG_NOTICE, "%s", trash);
279
280 if (!s->proxy->srv_bck && !s->proxy->srv_act)
281 set_backend_down(s->proxy);
282
283 if (s->state && SRV_CHECKED)
284 for(srv = s->tracknext; srv; srv = srv->tracknext)
285 set_server_disabled(srv);
286}
287
288static void set_server_enabled(struct server *s) {
289
290 struct server *srv;
291 struct chunk msg;
292 int xferred;
293
294 s->state &= ~SRV_GOINGDOWN;
295 s->proxy->lbprm.set_server_status_up(s);
296
297 /* check if we can handle some connections queued at the proxy. We
298 * will take as many as we can handle.
299 */
300 xferred = check_for_pending(s);
301
302 msg.len = 0;
303 msg.str = trash;
304
305 chunk_printf(&msg, sizeof(trash),
306 "Load-balancing on %sServer %s/%s is enabled again",
307 s->state & SRV_BACKUP ? "Backup " : "",
308 s->proxy->id, s->id);
309
310 if (s->tracked)
311 chunk_printf(&msg, sizeof(trash), " via %s/%s",
312 s->tracked->proxy->id, s->tracked->id);
313
314 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
315 " %d sessions requeued, %d total in queue.\n",
316 s->proxy->srv_act, s->proxy->srv_bck,
317 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
318 xferred, s->nbpend);
319
320 Warning("%s", trash);
321 send_log(s->proxy, LOG_NOTICE, "%s", trash);
322
323 if (s->state && SRV_CHECKED)
324 for(srv = s->tracknext; srv; srv = srv->tracknext)
325 set_server_enabled(srv);
326}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327
328/*
329 * This function is used only for server health-checks. It handles
330 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100331 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200332 * The function itself returns 0 if it needs some polling before being called
333 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 */
Willy Tarreau83749182007-04-15 20:56:27 +0200335static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336{
Willy Tarreau6996e152007-04-30 14:37:43 +0200337 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338 struct task *t = fdtab[fd].owner;
339 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100341 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Willy Tarreau6996e152007-04-30 14:37:43 +0200342 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
343 goto out_error;
344
345 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200346
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100347 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200349 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200350 (s->proxy->options & PR_O_SSL3_CHK) ||
351 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200353 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354 * so we'll send the request, and won't wake the checker up now.
355 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200356
357 if (s->proxy->options & PR_O_SSL3_CHK) {
358 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200359 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200360 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
361 }
362
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363#ifndef MSG_NOSIGNAL
364 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
365#else
366 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
367#endif
368 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100369 /* we allow up to <timeout.check> if nonzero for a responce */
370 //fprintf(stderr, "event_srv_chk_w, ms=%lu\n", __tv_to_ms(&s->proxy->timeout.check));
371 tv_add_ifset(&t->expire, &now, &s->proxy->timeout.check);
372
Willy Tarreauf161a342007-04-08 16:59:42 +0200373 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200374 goto out_nowake;
375 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200376 else if (ret == 0 || errno == EAGAIN)
377 goto out_poll;
378 else
379 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200380 }
381 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200382 /* We have no data to send to check the connection, and
383 * getsockopt() will not inform us whether the connection
384 * is still pending. So we'll reuse connect() to check the
385 * state of the socket. This has the advantage of givig us
386 * the following info :
387 * - error
388 * - connecting (EALREADY, EINPROGRESS)
389 * - connected (EISCONN, 0)
390 */
391
392 struct sockaddr_in sa;
393
394 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
395 sa.sin_port = htons(s->check_port);
396
397 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
398 errno = 0;
399
400 if (errno == EALREADY || errno == EINPROGRESS)
401 goto out_poll;
402
403 if (errno && errno != EISCONN)
404 goto out_error;
405
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100407 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200408 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 }
410 }
Willy Tarreau83749182007-04-15 20:56:27 +0200411 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200412 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200413 out_nowake:
414 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100415 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200416 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200417 out_poll:
418 /* The connection is still pending. We'll have to poll it
419 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100420 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200421 return 0;
422 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100423 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200424 fdtab[fd].state = FD_STERROR;
425 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426}
427
428
429/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200430 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100431 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
432 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
433 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
434 * to an SSL HELLO (the principle is that this is enough to distinguish between
435 * an SSL server and a pure TCP relay). All other cases will set s->result to
436 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
437 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 */
Willy Tarreau83749182007-04-15 20:56:27 +0200439static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440{
Willy Tarreau83749182007-04-15 20:56:27 +0200441 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100442 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443 struct task *t = fdtab[fd].owner;
444 struct server *s = t->context;
445 int skerr;
446 socklen_t lskerr = sizeof(skerr);
447
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100448 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200449
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100450 if (unlikely((s->result & SRV_CHK_ERROR) ||
451 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200452 (fdtab[fd].ev & FD_POLL_ERR) ||
453 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
454 (skerr != 0))) {
455 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100456 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200457 goto out_wakeup;
458 }
459
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200461 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462#else
Willy Tarreau83749182007-04-15 20:56:27 +0200463 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
464 * but the connection was closed on the remote end. Fortunately, recv still
465 * works correctly and we don't need to do the getsockopt() on linux.
466 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200467 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200469 if (unlikely(len < 0 && errno == EAGAIN)) {
470 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100471 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200472 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 }
474
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100475 /* Note: the response will only be accepted if read at once */
476 if (s->proxy->options & PR_O_HTTP_CHK) {
477 /* Check if the server speaks HTTP 1.X */
478 if ((len < strlen("HTTP/1.0 000\r")) ||
479 (memcmp(trash, "HTTP/1.", 7) != 0)) {
480 s->result |= SRV_CHK_ERROR;
481 goto out_wakeup;
482 }
483
484 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
485 if (trash[9] == '2' || trash[9] == '3')
486 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100487 else if ((s->proxy->options & PR_O_DISABLE404) &&
488 (s->state & SRV_RUNNING) &&
489 (memcmp(&trash[9], "404", 3) == 0)) {
490 /* 404 may be accepted as "stopping" only if the server was up */
491 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
492 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100493 else
494 s->result |= SRV_CHK_ERROR;
495 }
496 else if (s->proxy->options & PR_O_SSL3_CHK) {
497 /* Check for SSLv3 alert or handshake */
498 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
499 s->result |= SRV_CHK_RUNNING;
500 else
501 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200502 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100503 else if (s->proxy->options & PR_O_SMTP_CHK) {
504 /* Check for SMTP code 2xx (should be 250) */
505 if ((len >= 3) && (trash[0] == '2'))
506 s->result |= SRV_CHK_RUNNING;
507 else
508 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200509 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100510 else {
511 /* other checks are valid if the connection succeeded anyway */
512 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200513 }
Willy Tarreau83749182007-04-15 20:56:27 +0200514
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100515 out_wakeup:
516 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517 fdtab[fd].state = FD_STERROR;
518
Willy Tarreauf161a342007-04-08 16:59:42 +0200519 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200520 task_wakeup(t);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100521 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200522 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523}
524
525/*
526 * manages a server health-check. Returns
527 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
528 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200529void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200531 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532 struct server *s = t->context;
533 struct sockaddr_in sa;
534 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200535 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200536
537 //fprintf(stderr, "process_chk: task=%p\n", t);
538
539 new_chk:
540 fd = s->curfd;
541 if (fd < 0) { /* no check currently running */
542 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200543 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200545 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200546 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547 }
548
549 /* we don't send any health-checks when the proxy is stopped or when
550 * the server should not be checked.
551 */
552 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200553 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200554 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200556 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200557 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558 }
559
560 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100561 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
563 if ((fd < global.maxsock) &&
564 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
565 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
566 //fprintf(stderr, "process_chk: 3\n");
567
Willy Tarreau9edd1612007-10-18 18:07:48 +0200568 if (s->proxy->options & PR_O_TCP_NOLING) {
569 /* We don't want to useless data */
570 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
571 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200572
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200573 if (s->check_addr.sin_addr.s_addr)
574 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200575 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200576 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200577 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200578 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200579
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581 sa.sin_port = htons(s->check_port);
582
583 /* allow specific binding :
584 * - server-specific at first
585 * - proxy-specific next
586 */
587 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100588 struct sockaddr_in *remote = NULL;
589 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100590
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100591#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100592 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
593 remote = (struct sockaddr_in *)&s->tproxy_addr;
594 flags = 3;
595 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100596#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100597 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
598 if (ret) {
599 s->result |= SRV_CHK_ERROR;
600 switch (ret) {
601 case 1:
602 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
603 s->proxy->id, s->id);
604 break;
605 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100606 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
607 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100608 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100609 }
610 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611 }
612 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100613 struct sockaddr_in *remote = NULL;
614 int ret, flags = 0;
615
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100616#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100617 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100618 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
619 flags = 3;
620 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100621#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100622 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
623 if (ret) {
624 s->result |= SRV_CHK_ERROR;
625 switch (ret) {
626 case 1:
627 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
628 proxy_type_str(s->proxy), s->proxy->id);
629 break;
630 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100631 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
632 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100633 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100634 }
635 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 }
637
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100638 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100640 struct timeval tv_con;
641
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 /* OK, connection in progress or established */
643
644 //fprintf(stderr, "process_chk: 4\n");
645
646 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200647 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200649 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
650 fdtab[fd].cb[DIR_RD].b = NULL;
651 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
652 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200653 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
654 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreauf161a342007-04-08 16:59:42 +0200656 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200658 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100660 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
661 /* we allow up to min(inter, timeout.connect) for a connection
662 * to establish but only when timeout.check is set
663 * as it may be to short for a full check otherwise
664 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200665 tv_ms_add(&t->expire, &now, s->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100666
Willy Tarreau60548192008-02-17 11:34:10 +0100667 if (tv_isset(&s->proxy->timeout.check) && tv_isset(&s->proxy->timeout.connect)) {
668 tv_add(&tv_con, &now, &s->proxy->timeout.connect);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100669 tv_bound(&t->expire, &tv_con);
Willy Tarreau60548192008-02-17 11:34:10 +0100670 }
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100671
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200673 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200674 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
676 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100677 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678 }
679 }
680 }
681 close(fd); /* socket creation error */
682 }
683
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100684 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200686 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200687 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 goto new_chk; /* may be we should initialize a new check */
689 }
690
691 /* here, we have seen a failure */
692 if (s->health > s->rise) {
693 s->health--; /* still good */
694 s->failed_checks++;
695 }
696 else
697 set_server_down(s);
698
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100699 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
700 /* we allow up to min(inter, timeout.connect) for a connection
701 * to establish but only when timeout.check is set
702 * as it may be to short for a full check otherwise
703 */
704 while (tv_isle(&t->expire, &now)) {
705 struct timeval tv_con;
706
707 tv_add(&tv_con, &t->expire, &s->proxy->timeout.connect);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200708 tv_ms_add(&t->expire, &t->expire, s->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100709
710 if (tv_isset(&s->proxy->timeout.check))
711 tv_bound(&t->expire, &tv_con);
712 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713 goto new_chk;
714 }
715 else {
716 //fprintf(stderr, "process_chk: 8\n");
717 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100718 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200720
Willy Tarreau9909fc12007-11-30 17:42:05 +0100721 if (s->state & SRV_WARMINGUP) {
722 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
723 s->state &= ~SRV_WARMINGUP;
724 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
725 s->eweight = s->uweight * BE_WEIGHT_SCALE;
726 if (s->proxy->lbprm.update_server_eweight)
727 s->proxy->lbprm.update_server_eweight(s);
728 }
729 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
730 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100731 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
732 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100733 s->eweight *= s->uweight;
734 if (s->proxy->lbprm.update_server_eweight)
735 s->proxy->lbprm.update_server_eweight(s);
736 }
737 /* probably that we can refill this server with a bit more connections */
738 check_for_pending(s);
739 }
740
Willy Tarreau48494c02007-11-30 10:41:39 +0100741 /* we may have to add/remove this server from the LB group */
742 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
743 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100744 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
745 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100746 else if (!(s->state & SRV_GOINGDOWN) &&
747 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100748 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
749 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100750 }
751
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200752 if (s->health < s->rise + s->fall - 1) {
753 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100755 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756 }
757 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200759
760 rv = 0;
761 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100762 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200763 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100764 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200765 }
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100766 tv_ms_add(&t->expire, &now, srv_getinter(s) + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200767 goto new_chk;
768 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100769 else if ((s->result & SRV_CHK_ERROR) || tv_isle(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 //fprintf(stderr, "process_chk: 10\n");
771 /* failure or timeout detected */
772 if (s->health > s->rise) {
773 s->health--; /* still good */
774 s->failed_checks++;
775 }
776 else
777 set_server_down(s);
778 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200780
781 rv = 0;
782 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100783 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200784 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100785 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200786 }
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100787 tv_ms_add(&t->expire, &now, srv_getinter(s) + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788 goto new_chk;
789 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100790 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 }
792 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100793 s->result = SRV_CHK_UNKNOWN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200795 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200796 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200797 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798}
799
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200800/*
801 * Start health-check.
802 * Returns 0 if OK, -1 if error, and prints the error in this case.
803 */
804int start_checks() {
805
806 struct proxy *px;
807 struct server *s;
808 struct task *t;
809 int nbchk=0, mininter=0, srvpos=0;
810
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200811 /* 1- count the checkers to run simultaneously.
812 * We also determine the minimum interval among all of those which
813 * have an interval larger than SRV_CHK_INTER_THRES. This interval
814 * will be used to spread their start-up date. Those which have
815 * a shorter interval will start independantly and will not dictate
816 * too short an interval for all others.
817 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200818 for (px = proxy; px; px = px->next) {
819 for (s = px->srv; s; s = s->next) {
820 if (!(s->state & SRV_CHECKED))
821 continue;
822
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100823 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
824 (!mininter || mininter > srv_getinter(s)))
825 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200826
827 nbchk++;
828 }
829 }
830
831 if (!nbchk)
832 return 0;
833
834 srand((unsigned)time(NULL));
835
836 /*
837 * 2- start them as far as possible from each others. For this, we will
838 * start them after their interval set to the min interval divided by
839 * the number of servers, weighted by the server's position in the list.
840 */
841 for (px = proxy; px; px = px->next) {
842 for (s = px->srv; s; s = s->next) {
843 if (!(s->state & SRV_CHECKED))
844 continue;
845
846 if ((t = pool_alloc2(pool2_task)) == NULL) {
847 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
848 return -1;
849 }
850
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200851 s->check = t;
852
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200853 task_init(t);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200854 t->process = process_chk;
855 t->context = s;
856
857 /* check this every ms */
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200858 tv_ms_add(&t->expire, &now,
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100859 ((mininter && mininter >= srv_getinter(s)) ? mininter : srv_getinter(s)) * srvpos / nbchk);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200860 task_queue(t);
861
862 srvpos++;
863 }
864 }
865 return 0;
866}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867
868/*
869 * Local variables:
870 * c-indent-level: 8
871 * c-basic-offset: 8
872 * End:
873 */