blob: b48179afb95f262a776e93c2c21b54dc1cc183f5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34#include <proto/backend.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010035#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/fd.h>
37#include <proto/log.h>
38#include <proto/queue.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010039#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010040#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010041#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/server.h>
43#include <proto/task.h>
44
Willy Tarreau48494c02007-11-30 10:41:39 +010045/* sends a log message when a backend goes down, and also sets last
46 * change date.
47 */
48static void set_backend_down(struct proxy *be)
49{
50 be->last_change = now.tv_sec;
51 be->down_trans++;
52
53 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
54 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
55}
56
57/* Redistribute pending connections when a server goes down. The number of
58 * connections redistributed is returned.
59 */
60static int redistribute_pending(struct server *s)
61{
62 struct pendconn *pc, *pc_bck, *pc_end;
63 int xferred = 0;
64
65 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
66 struct session *sess = pc->sess;
67 if (sess->be->options & PR_O_REDISP) {
68 /* The REDISP option was specified. We will ignore
69 * cookie and force to balance or use the dispatcher.
70 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010071
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010072 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +010073 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010074
Willy Tarreau48494c02007-11-30 10:41:39 +010075 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +020076 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +010077 xferred++;
78 }
79 }
80 return xferred;
81}
82
83/* Check for pending connections at the backend, and assign some of them to
84 * the server coming up. The server's weight is checked before being assigned
85 * connections it may not be able to handle. The total number of transferred
86 * connections is returned.
87 */
88static int check_for_pending(struct server *s)
89{
90 int xferred;
91
92 if (!s->eweight)
93 return 0;
94
95 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
96 struct session *sess;
97 struct pendconn *p;
98
99 p = pendconn_from_px(s->proxy);
100 if (!p)
101 break;
102 p->sess->srv = s;
103 sess = p->sess;
104 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200105 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100106 }
107 return xferred;
108}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
110/* Sets server <s> down, notifies by all available means, recounts the
111 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200112 * possible to other servers. It automatically recomputes the number of
113 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 */
Willy Tarreau83749182007-04-15 20:56:27 +0200115static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100117 struct server *srv;
118 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 int xferred;
120
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100121 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100122 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200123
124 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100125 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100126 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127
128 /* we might have sessions queued on this server and waiting for
129 * a connection. Those which are redispatchable will be queued
130 * to another server or to the proxy itself.
131 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100132 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100133
134 msg.len = 0;
135 msg.str = trash;
136
137 chunk_printf(&msg, sizeof(trash),
138 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
139 s->proxy->id, s->id);
140
141 if (s->tracked)
142 chunk_printf(&msg, sizeof(trash), " via %s/%s",
143 s->tracked->proxy->id, s->tracked->id);
144
145 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers left.%s"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146 " %d sessions active, %d requeued, %d remaining in queue.\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100147 s->proxy->srv_act, s->proxy->srv_bck,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
149 s->cur_sess, xferred, s->nbpend);
150
151 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200152
Willy Tarreau48494c02007-11-30 10:41:39 +0100153 /* we don't send an alert if the server was previously paused */
154 if (srv_was_paused)
155 send_log(s->proxy, LOG_NOTICE, "%s", trash);
156 else
157 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200158
Willy Tarreau48494c02007-11-30 10:41:39 +0100159 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
160 set_backend_down(s->proxy);
161
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162 s->down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100163
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100164 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100165 for(srv = s->tracknext; srv; srv = srv->tracknext)
166 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100168
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169 s->health = 0; /* failure */
170}
171
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100172static void set_server_up(struct server *s) {
173
174 struct server *srv;
175 struct chunk msg;
176 int xferred;
177
178 if (s->health == s->rise || s->tracked) {
179 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
180 if (s->proxy->last_change < now.tv_sec) // ignore negative times
181 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
182 s->proxy->last_change = now.tv_sec;
183 }
184
185 if (s->last_change < now.tv_sec) // ignore negative times
186 s->down_time += now.tv_sec - s->last_change;
187
188 s->last_change = now.tv_sec;
189 s->state |= SRV_RUNNING;
190
191 if (s->slowstart > 0) {
192 s->state |= SRV_WARMINGUP;
193 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
194 /* For dynamic algorithms, start at the first step of the weight,
195 * without multiplying by BE_WEIGHT_SCALE.
196 */
197 s->eweight = s->uweight;
198 if (s->proxy->lbprm.update_server_eweight)
199 s->proxy->lbprm.update_server_eweight(s);
200 }
201 }
202 s->proxy->lbprm.set_server_status_up(s);
203
204 /* check if we can handle some connections queued at the proxy. We
205 * will take as many as we can handle.
206 */
207 xferred = check_for_pending(s);
208
209 msg.len = 0;
210 msg.str = trash;
211
212 chunk_printf(&msg, sizeof(trash),
213 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
214 s->proxy->id, s->id);
215
216 if (s->tracked)
217 chunk_printf(&msg, sizeof(trash), " via %s/%s",
218 s->tracked->proxy->id, s->tracked->id);
219
220 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
221 " %d sessions requeued, %d total in queue.\n",
222 s->proxy->srv_act, s->proxy->srv_bck,
223 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
224 s->cur_sess, xferred, s->nbpend);
225
226 Warning("%s", trash);
227 send_log(s->proxy, LOG_NOTICE, "%s", trash);
228
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100229 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100230 for(srv = s->tracknext; srv; srv = srv->tracknext)
231 set_server_up(srv);
232 }
233
234 if (s->health >= s->rise)
235 s->health = s->rise + s->fall - 1; /* OK now */
236
237}
238
239static void set_server_disabled(struct server *s) {
240
241 struct server *srv;
242 struct chunk msg;
243 int xferred;
244
245 s->state |= SRV_GOINGDOWN;
246 s->proxy->lbprm.set_server_status_down(s);
247
248 /* we might have sessions queued on this server and waiting for
249 * a connection. Those which are redispatchable will be queued
250 * to another server or to the proxy itself.
251 */
252 xferred = redistribute_pending(s);
253
254 msg.len = 0;
255 msg.str = trash;
256
257 chunk_printf(&msg, sizeof(trash),
258 "Load-balancing on %sServer %s/%s is disabled",
259 s->state & SRV_BACKUP ? "Backup " : "",
260 s->proxy->id, s->id);
261
262 if (s->tracked)
263 chunk_printf(&msg, sizeof(trash), " via %s/%s",
264 s->tracked->proxy->id, s->tracked->id);
265
266
267 chunk_printf(&msg, sizeof(trash),". %d active and %d backup servers online.%s"
268 " %d sessions requeued, %d total in queue.\n",
269 s->proxy->srv_act, s->proxy->srv_bck,
270 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
271 xferred, s->nbpend);
272
273 Warning("%s", trash);
274
275 send_log(s->proxy, LOG_NOTICE, "%s", trash);
276
277 if (!s->proxy->srv_bck && !s->proxy->srv_act)
278 set_backend_down(s->proxy);
279
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100280 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100281 for(srv = s->tracknext; srv; srv = srv->tracknext)
282 set_server_disabled(srv);
283}
284
285static void set_server_enabled(struct server *s) {
286
287 struct server *srv;
288 struct chunk msg;
289 int xferred;
290
291 s->state &= ~SRV_GOINGDOWN;
292 s->proxy->lbprm.set_server_status_up(s);
293
294 /* check if we can handle some connections queued at the proxy. We
295 * will take as many as we can handle.
296 */
297 xferred = check_for_pending(s);
298
299 msg.len = 0;
300 msg.str = trash;
301
302 chunk_printf(&msg, sizeof(trash),
303 "Load-balancing on %sServer %s/%s is enabled again",
304 s->state & SRV_BACKUP ? "Backup " : "",
305 s->proxy->id, s->id);
306
307 if (s->tracked)
308 chunk_printf(&msg, sizeof(trash), " via %s/%s",
309 s->tracked->proxy->id, s->tracked->id);
310
311 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
312 " %d sessions requeued, %d total in queue.\n",
313 s->proxy->srv_act, s->proxy->srv_bck,
314 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
315 xferred, s->nbpend);
316
317 Warning("%s", trash);
318 send_log(s->proxy, LOG_NOTICE, "%s", trash);
319
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100320 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100321 for(srv = s->tracknext; srv; srv = srv->tracknext)
322 set_server_enabled(srv);
323}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324
325/*
326 * This function is used only for server health-checks. It handles
327 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100328 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200329 * The function itself returns 0 if it needs some polling before being called
330 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200331 */
Willy Tarreau83749182007-04-15 20:56:27 +0200332static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333{
Willy Tarreau6996e152007-04-30 14:37:43 +0200334 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 struct task *t = fdtab[fd].owner;
336 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100338 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Willy Tarreau6996e152007-04-30 14:37:43 +0200339 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
340 goto out_error;
341
342 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200343
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100344 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200346 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200347 (s->proxy->options & PR_O_SSL3_CHK) ||
348 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200350 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351 * so we'll send the request, and won't wake the checker up now.
352 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200353
354 if (s->proxy->options & PR_O_SSL3_CHK) {
355 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200356 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200357 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
358 }
359
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360#ifndef MSG_NOSIGNAL
361 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
362#else
363 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
364#endif
365 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100366 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100367 if (s->proxy->timeout.check)
368 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200369 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200370 goto out_nowake;
371 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200372 else if (ret == 0 || errno == EAGAIN)
373 goto out_poll;
374 else
375 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376 }
377 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200378 /* We have no data to send to check the connection, and
379 * getsockopt() will not inform us whether the connection
380 * is still pending. So we'll reuse connect() to check the
381 * state of the socket. This has the advantage of givig us
382 * the following info :
383 * - error
384 * - connecting (EALREADY, EINPROGRESS)
385 * - connected (EISCONN, 0)
386 */
387
388 struct sockaddr_in sa;
389
390 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
391 sa.sin_port = htons(s->check_port);
392
393 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
394 errno = 0;
395
396 if (errno == EALREADY || errno == EINPROGRESS)
397 goto out_poll;
398
399 if (errno && errno != EISCONN)
400 goto out_error;
401
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100403 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200404 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405 }
406 }
Willy Tarreau83749182007-04-15 20:56:27 +0200407 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200408 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200409 out_nowake:
410 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100411 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200412 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200413 out_poll:
414 /* The connection is still pending. We'll have to poll it
415 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100416 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200417 return 0;
418 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100419 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200420 fdtab[fd].state = FD_STERROR;
421 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422}
423
424
425/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200426 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100427 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
428 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
429 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
430 * to an SSL HELLO (the principle is that this is enough to distinguish between
431 * an SSL server and a pure TCP relay). All other cases will set s->result to
432 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
433 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434 */
Willy Tarreau83749182007-04-15 20:56:27 +0200435static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436{
Willy Tarreau83749182007-04-15 20:56:27 +0200437 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100438 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 struct task *t = fdtab[fd].owner;
440 struct server *s = t->context;
441 int skerr;
442 socklen_t lskerr = sizeof(skerr);
443
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100444 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200445
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100446 if (unlikely((s->result & SRV_CHK_ERROR) ||
447 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200448 (fdtab[fd].ev & FD_POLL_ERR) ||
449 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
450 (skerr != 0))) {
451 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100452 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200453 goto out_wakeup;
454 }
455
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200457 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458#else
Willy Tarreau83749182007-04-15 20:56:27 +0200459 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
460 * but the connection was closed on the remote end. Fortunately, recv still
461 * works correctly and we don't need to do the getsockopt() on linux.
462 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200463 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200465 if (unlikely(len < 0 && errno == EAGAIN)) {
466 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100467 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200468 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 }
470
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100471 /* Note: the response will only be accepted if read at once */
472 if (s->proxy->options & PR_O_HTTP_CHK) {
473 /* Check if the server speaks HTTP 1.X */
474 if ((len < strlen("HTTP/1.0 000\r")) ||
475 (memcmp(trash, "HTTP/1.", 7) != 0)) {
476 s->result |= SRV_CHK_ERROR;
477 goto out_wakeup;
478 }
479
480 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
481 if (trash[9] == '2' || trash[9] == '3')
482 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100483 else if ((s->proxy->options & PR_O_DISABLE404) &&
484 (s->state & SRV_RUNNING) &&
485 (memcmp(&trash[9], "404", 3) == 0)) {
486 /* 404 may be accepted as "stopping" only if the server was up */
487 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
488 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100489 else
490 s->result |= SRV_CHK_ERROR;
491 }
492 else if (s->proxy->options & PR_O_SSL3_CHK) {
493 /* Check for SSLv3 alert or handshake */
494 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
495 s->result |= SRV_CHK_RUNNING;
496 else
497 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200498 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100499 else if (s->proxy->options & PR_O_SMTP_CHK) {
500 /* Check for SMTP code 2xx (should be 250) */
501 if ((len >= 3) && (trash[0] == '2'))
502 s->result |= SRV_CHK_RUNNING;
503 else
504 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200505 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100506 else {
507 /* other checks are valid if the connection succeeded anyway */
508 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200509 }
Willy Tarreau83749182007-04-15 20:56:27 +0200510
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100511 out_wakeup:
512 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513 fdtab[fd].state = FD_STERROR;
514
Willy Tarreauf161a342007-04-08 16:59:42 +0200515 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200516 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100517 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200518 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519}
520
521/*
522 * manages a server health-check. Returns
523 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
524 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100525struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526{
Willy Tarreaue3838802009-03-21 18:58:32 +0100527 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528 struct server *s = t->context;
529 struct sockaddr_in sa;
530 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200531 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532
533 //fprintf(stderr, "process_chk: task=%p\n", t);
534
535 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +0100536 if (attempts++ > 0) {
537 /* we always fail to create a server, let's stop insisting... */
538 while (tick_is_expired(t->expire, now_ms))
539 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
540 return t;
541 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 fd = s->curfd;
543 if (fd < 0) { /* no check currently running */
544 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +0100545 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
546 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547
548 /* we don't send any health-checks when the proxy is stopped or when
549 * the server should not be checked.
550 */
551 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200552 while (tick_is_expired(t->expire, now_ms))
553 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +0100554 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556
557 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100558 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
560 if ((fd < global.maxsock) &&
561 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
562 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
563 //fprintf(stderr, "process_chk: 3\n");
564
Willy Tarreau9edd1612007-10-18 18:07:48 +0200565 if (s->proxy->options & PR_O_TCP_NOLING) {
566 /* We don't want to useless data */
567 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
568 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200569
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200570 if (s->check_addr.sin_addr.s_addr)
571 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200572 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200573 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200574 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200575 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200576
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 sa.sin_port = htons(s->check_port);
579
580 /* allow specific binding :
581 * - server-specific at first
582 * - proxy-specific next
583 */
584 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100585 struct sockaddr_in *remote = NULL;
586 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100587
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100588#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100589 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
590 remote = (struct sockaddr_in *)&s->tproxy_addr;
591 flags = 3;
592 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100593#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +0100594#ifdef SO_BINDTODEVICE
595 /* Note: this might fail if not CAP_NET_RAW */
596 if (s->iface_name)
597 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100598 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +0100599#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100600 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
601 if (ret) {
602 s->result |= SRV_CHK_ERROR;
603 switch (ret) {
604 case 1:
605 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
606 s->proxy->id, s->id);
607 break;
608 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100609 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
610 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100611 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100612 }
613 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200614 }
615 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100616 struct sockaddr_in *remote = NULL;
617 int ret, flags = 0;
618
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100619#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100620 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100621 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
622 flags = 3;
623 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100624#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100625#ifdef SO_BINDTODEVICE
626 /* Note: this might fail if not CAP_NET_RAW */
627 if (s->proxy->iface_name)
628 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100629 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100630#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100631 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
632 if (ret) {
633 s->result |= SRV_CHK_ERROR;
634 switch (ret) {
635 case 1:
636 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
637 proxy_type_str(s->proxy), s->proxy->id);
638 break;
639 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100640 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
641 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100642 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100643 }
644 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 }
646
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100647 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
649 /* OK, connection in progress or established */
650
651 //fprintf(stderr, "process_chk: 4\n");
652
653 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200654 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200656 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
657 fdtab[fd].cb[DIR_RD].b = NULL;
658 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
659 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200660 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
661 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreauf161a342007-04-08 16:59:42 +0200663 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200665 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100667 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
668 /* we allow up to min(inter, timeout.connect) for a connection
669 * to establish but only when timeout.check is set
670 * as it may be to short for a full check otherwise
671 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200672 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100673
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200674 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
675 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
676 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +0100677 }
Willy Tarreau26c25062009-03-08 09:38:41 +0100678 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679 }
680 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100681 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 }
683 }
684 }
685 close(fd); /* socket creation error */
686 }
687
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100688 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200689 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200690 while (tick_is_expired(t->expire, now_ms))
691 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692 goto new_chk; /* may be we should initialize a new check */
693 }
694
695 /* here, we have seen a failure */
696 if (s->health > s->rise) {
697 s->health--; /* still good */
698 s->failed_checks++;
699 }
700 else
701 set_server_down(s);
702
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100703 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
704 /* we allow up to min(inter, timeout.connect) for a connection
705 * to establish but only when timeout.check is set
706 * as it may be to short for a full check otherwise
707 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200708 while (tick_is_expired(t->expire, now_ms)) {
709 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100710
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200711 t_con = tick_add(t->expire, s->proxy->timeout.connect);
712 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100713
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200714 if (s->proxy->timeout.check)
715 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100716 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 goto new_chk;
718 }
719 else {
720 //fprintf(stderr, "process_chk: 8\n");
721 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100722 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200724
Willy Tarreau9909fc12007-11-30 17:42:05 +0100725 if (s->state & SRV_WARMINGUP) {
726 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
727 s->state &= ~SRV_WARMINGUP;
728 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
729 s->eweight = s->uweight * BE_WEIGHT_SCALE;
730 if (s->proxy->lbprm.update_server_eweight)
731 s->proxy->lbprm.update_server_eweight(s);
732 }
733 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
734 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100735 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
736 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100737 s->eweight *= s->uweight;
738 if (s->proxy->lbprm.update_server_eweight)
739 s->proxy->lbprm.update_server_eweight(s);
740 }
741 /* probably that we can refill this server with a bit more connections */
742 check_for_pending(s);
743 }
744
Willy Tarreau48494c02007-11-30 10:41:39 +0100745 /* we may have to add/remove this server from the LB group */
746 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
747 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100748 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
749 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100750 else if (!(s->state & SRV_GOINGDOWN) &&
751 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100752 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
753 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100754 }
755
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200756 if (s->health < s->rise + s->fall - 1) {
757 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100759 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 }
761 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200763
764 rv = 0;
765 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100766 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200767 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100768 //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 +0200769 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200770 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771 goto new_chk;
772 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200773 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 //fprintf(stderr, "process_chk: 10\n");
775 /* failure or timeout detected */
776 if (s->health > s->rise) {
777 s->health--; /* still good */
778 s->failed_checks++;
779 }
780 else
781 set_server_down(s);
782 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200783 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200784
785 rv = 0;
786 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100787 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200788 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100789 //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 +0200790 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200791 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 goto new_chk;
793 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100794 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 }
796 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100797 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +0100798 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799}
800
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200801/*
802 * Start health-check.
803 * Returns 0 if OK, -1 if error, and prints the error in this case.
804 */
805int start_checks() {
806
807 struct proxy *px;
808 struct server *s;
809 struct task *t;
810 int nbchk=0, mininter=0, srvpos=0;
811
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200812 /* 1- count the checkers to run simultaneously.
813 * We also determine the minimum interval among all of those which
814 * have an interval larger than SRV_CHK_INTER_THRES. This interval
815 * will be used to spread their start-up date. Those which have
816 * a shorter interval will start independantly and will not dictate
817 * too short an interval for all others.
818 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200819 for (px = proxy; px; px = px->next) {
820 for (s = px->srv; s; s = s->next) {
821 if (!(s->state & SRV_CHECKED))
822 continue;
823
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100824 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
825 (!mininter || mininter > srv_getinter(s)))
826 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200827
828 nbchk++;
829 }
830 }
831
832 if (!nbchk)
833 return 0;
834
835 srand((unsigned)time(NULL));
836
837 /*
838 * 2- start them as far as possible from each others. For this, we will
839 * start them after their interval set to the min interval divided by
840 * the number of servers, weighted by the server's position in the list.
841 */
842 for (px = proxy; px; px = px->next) {
843 for (s = px->srv; s; s = s->next) {
844 if (!(s->state & SRV_CHECKED))
845 continue;
846
Willy Tarreaua4613182009-03-21 18:13:21 +0100847 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200848 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
849 return -1;
850 }
851
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200852 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200853 t->process = process_chk;
854 t->context = s;
855
856 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200857 t->expire = tick_add(now_ms,
858 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
859 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 */