blob: 99c21224f3bca2362489612b67b18d41d2984525 [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>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040023#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020025#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <arpa/inet.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020031#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020032#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <proto/backend.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010037#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/fd.h>
39#include <proto/log.h>
40#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020041#include <proto/port_range.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);
Willy Tarreaufdccded2008-08-29 18:19:04 +020079 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +010080 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);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200108 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100109 }
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
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100167 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100168 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." : "",
Willy Tarreau1772ece2009-04-03 14:49:12 +0200227 xferred, s->nbpend);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100228
229 Warning("%s", trash);
230 send_log(s->proxy, LOG_NOTICE, "%s", trash);
231
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100232 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100233 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
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100283 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100284 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
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100323 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100324 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 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100365 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100366 if (s->proxy->timeout.check)
367 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200368 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200369 goto out_nowake;
370 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200371 else if (ret == 0 || errno == EAGAIN)
372 goto out_poll;
373 else
374 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375 }
376 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200377 /* We have no data to send to check the connection, and
378 * getsockopt() will not inform us whether the connection
379 * is still pending. So we'll reuse connect() to check the
380 * state of the socket. This has the advantage of givig us
381 * the following info :
382 * - error
383 * - connecting (EALREADY, EINPROGRESS)
384 * - connected (EISCONN, 0)
385 */
386
387 struct sockaddr_in sa;
388
389 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
390 sa.sin_port = htons(s->check_port);
391
392 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
393 errno = 0;
394
395 if (errno == EALREADY || errno == EINPROGRESS)
396 goto out_poll;
397
398 if (errno && errno != EISCONN)
399 goto out_error;
400
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100402 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200403 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404 }
405 }
Willy Tarreau83749182007-04-15 20:56:27 +0200406 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200407 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200408 out_nowake:
409 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100410 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200411 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200412 out_poll:
413 /* The connection is still pending. We'll have to poll it
414 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100415 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200416 return 0;
417 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100418 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200419 fdtab[fd].state = FD_STERROR;
420 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200421}
422
423
424/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200425 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100426 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
427 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
428 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
429 * to an SSL HELLO (the principle is that this is enough to distinguish between
430 * an SSL server and a pure TCP relay). All other cases will set s->result to
431 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
432 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433 */
Willy Tarreau83749182007-04-15 20:56:27 +0200434static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435{
Willy Tarreau83749182007-04-15 20:56:27 +0200436 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100437 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 struct task *t = fdtab[fd].owner;
439 struct server *s = t->context;
440 int skerr;
441 socklen_t lskerr = sizeof(skerr);
442
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100443 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200444
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100445 if (unlikely((s->result & SRV_CHK_ERROR) ||
446 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200447 (fdtab[fd].ev & FD_POLL_ERR) ||
448 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
449 (skerr != 0))) {
450 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100451 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200452 goto out_wakeup;
453 }
454
Willy Tarreau83749182007-04-15 20:56:27 +0200455 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
456 * but the connection was closed on the remote end. Fortunately, recv still
457 * works correctly and we don't need to do the getsockopt() on linux.
458 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200459 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreau83749182007-04-15 20:56:27 +0200460 if (unlikely(len < 0 && errno == EAGAIN)) {
461 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100462 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200463 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464 }
465
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100466 /* Note: the response will only be accepted if read at once */
467 if (s->proxy->options & PR_O_HTTP_CHK) {
468 /* Check if the server speaks HTTP 1.X */
469 if ((len < strlen("HTTP/1.0 000\r")) ||
470 (memcmp(trash, "HTTP/1.", 7) != 0)) {
471 s->result |= SRV_CHK_ERROR;
472 goto out_wakeup;
473 }
474
475 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
476 if (trash[9] == '2' || trash[9] == '3')
477 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100478 else if ((s->proxy->options & PR_O_DISABLE404) &&
479 (s->state & SRV_RUNNING) &&
480 (memcmp(&trash[9], "404", 3) == 0)) {
481 /* 404 may be accepted as "stopping" only if the server was up */
482 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
483 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100484 else
485 s->result |= SRV_CHK_ERROR;
486 }
487 else if (s->proxy->options & PR_O_SSL3_CHK) {
488 /* Check for SSLv3 alert or handshake */
489 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
490 s->result |= SRV_CHK_RUNNING;
491 else
492 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200493 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100494 else if (s->proxy->options & PR_O_SMTP_CHK) {
495 /* Check for SMTP code 2xx (should be 250) */
496 if ((len >= 3) && (trash[0] == '2'))
497 s->result |= SRV_CHK_RUNNING;
498 else
499 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200500 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100501 else {
502 /* other checks are valid if the connection succeeded anyway */
503 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200504 }
Willy Tarreau83749182007-04-15 20:56:27 +0200505
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100506 out_wakeup:
507 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508 fdtab[fd].state = FD_STERROR;
509
Willy Tarreauf161a342007-04-08 16:59:42 +0200510 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200511 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100512 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200513 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200514}
515
516/*
517 * manages a server health-check. Returns
518 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
519 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100520struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521{
Willy Tarreaue3838802009-03-21 18:58:32 +0100522 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523 struct server *s = t->context;
524 struct sockaddr_in sa;
525 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200526 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527
528 //fprintf(stderr, "process_chk: task=%p\n", t);
529
530 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +0100531 if (attempts++ > 0) {
532 /* we always fail to create a server, let's stop insisting... */
533 while (tick_is_expired(t->expire, now_ms))
534 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
535 return t;
536 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537 fd = s->curfd;
538 if (fd < 0) { /* no check currently running */
539 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +0100540 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
541 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542
543 /* we don't send any health-checks when the proxy is stopped or when
544 * the server should not be checked.
545 */
546 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200547 while (tick_is_expired(t->expire, now_ms))
548 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +0100549 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550 }
551
552 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100553 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
555 if ((fd < global.maxsock) &&
556 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
557 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
558 //fprintf(stderr, "process_chk: 3\n");
559
Willy Tarreau9edd1612007-10-18 18:07:48 +0200560 if (s->proxy->options & PR_O_TCP_NOLING) {
561 /* We don't want to useless data */
562 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
563 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200564
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200565 if (s->check_addr.sin_addr.s_addr)
566 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200567 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200568 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200569 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200570 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200571
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 sa.sin_port = htons(s->check_port);
574
575 /* allow specific binding :
576 * - server-specific at first
577 * - proxy-specific next
578 */
579 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100580 struct sockaddr_in *remote = NULL;
581 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100582
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100583#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100584 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
585 remote = (struct sockaddr_in *)&s->tproxy_addr;
586 flags = 3;
587 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100588#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +0100589#ifdef SO_BINDTODEVICE
590 /* Note: this might fail if not CAP_NET_RAW */
591 if (s->iface_name)
592 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100593 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +0100594#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200595 if (s->sport_range) {
596 int bind_attempts = 10; /* should be more than enough to find a spare port */
597 struct sockaddr_in src;
598
599 ret = 1;
600 src = s->source_addr;
601
602 do {
603 /* note: in case of retry, we may have to release a previously
604 * allocated port, hence this loop's construct.
605 */
606 port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port);
607 fdtab[fd].port_range = NULL;
608
609 if (!bind_attempts)
610 break;
611 bind_attempts--;
612
613 fdtab[fd].local_port = port_range_alloc_port(s->sport_range);
614 if (!fdtab[fd].local_port)
615 break;
616
617 fdtab[fd].port_range = s->sport_range;
618 src.sin_port = htons(fdtab[fd].local_port);
619
620 ret = tcpv4_bind_socket(fd, flags, &src, remote);
621 } while (ret != 0); /* binding NOK */
622 }
623 else {
624 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
625 }
626
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100627 if (ret) {
628 s->result |= SRV_CHK_ERROR;
629 switch (ret) {
630 case 1:
631 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
632 s->proxy->id, s->id);
633 break;
634 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100635 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
636 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100637 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100638 }
639 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200640 }
641 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100642 struct sockaddr_in *remote = NULL;
643 int ret, flags = 0;
644
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100645#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100646 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100647 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
648 flags = 3;
649 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100650#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100651#ifdef SO_BINDTODEVICE
652 /* Note: this might fail if not CAP_NET_RAW */
653 if (s->proxy->iface_name)
654 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100655 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100656#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100657 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
658 if (ret) {
659 s->result |= SRV_CHK_ERROR;
660 switch (ret) {
661 case 1:
662 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
663 proxy_type_str(s->proxy), s->proxy->id);
664 break;
665 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100666 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
667 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100668 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100669 }
670 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671 }
672
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100673 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400674#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +0200675 /* disabling tcp quick ack now allows
676 * the request to leave the machine with
677 * the first ACK.
678 */
679 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400680 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +0200681#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
683 /* OK, connection in progress or established */
684
685 //fprintf(stderr, "process_chk: 4\n");
686
687 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200688 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200689 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200690 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
691 fdtab[fd].cb[DIR_RD].b = NULL;
692 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
693 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200694 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
695 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200697 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +0200698 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200700 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100702 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
703 /* we allow up to min(inter, timeout.connect) for a connection
704 * to establish but only when timeout.check is set
705 * as it may be to short for a full check otherwise
706 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200707 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100708
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200709 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
710 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
711 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +0100712 }
Willy Tarreau26c25062009-03-08 09:38:41 +0100713 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 }
715 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100716 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 }
718 }
719 }
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200720 port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port);
721 fdtab[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 close(fd); /* socket creation error */
723 }
724
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100725 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200727 while (tick_is_expired(t->expire, now_ms))
728 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 goto new_chk; /* may be we should initialize a new check */
730 }
731
732 /* here, we have seen a failure */
733 if (s->health > s->rise) {
734 s->health--; /* still good */
735 s->failed_checks++;
736 }
737 else
738 set_server_down(s);
739
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100740 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
741 /* we allow up to min(inter, timeout.connect) for a connection
742 * to establish but only when timeout.check is set
743 * as it may be to short for a full check otherwise
744 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200745 while (tick_is_expired(t->expire, now_ms)) {
746 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100747
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200748 t_con = tick_add(t->expire, s->proxy->timeout.connect);
749 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100750
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200751 if (s->proxy->timeout.check)
752 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100753 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 goto new_chk;
755 }
756 else {
757 //fprintf(stderr, "process_chk: 8\n");
758 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100759 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200761
Willy Tarreau9909fc12007-11-30 17:42:05 +0100762 if (s->state & SRV_WARMINGUP) {
763 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
764 s->state &= ~SRV_WARMINGUP;
765 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
766 s->eweight = s->uweight * BE_WEIGHT_SCALE;
767 if (s->proxy->lbprm.update_server_eweight)
768 s->proxy->lbprm.update_server_eweight(s);
769 }
770 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
771 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100772 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
773 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100774 s->eweight *= s->uweight;
775 if (s->proxy->lbprm.update_server_eweight)
776 s->proxy->lbprm.update_server_eweight(s);
777 }
778 /* probably that we can refill this server with a bit more connections */
779 check_for_pending(s);
780 }
781
Willy Tarreau48494c02007-11-30 10:41:39 +0100782 /* we may have to add/remove this server from the LB group */
783 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
784 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100785 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
786 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100787 else if (!(s->state & SRV_GOINGDOWN) &&
788 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100789 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
790 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100791 }
792
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200793 if (s->health < s->rise + s->fall - 1) {
794 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100796 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 }
798 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200800
801 rv = 0;
802 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100803 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200804 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100805 //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 +0200806 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200807 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 goto new_chk;
809 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200810 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 //fprintf(stderr, "process_chk: 10\n");
812 /* failure or timeout detected */
813 if (s->health > s->rise) {
814 s->health--; /* still good */
815 s->failed_checks++;
816 }
817 else
818 set_server_down(s);
819 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200821
822 rv = 0;
823 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100824 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200825 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100826 //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 +0200827 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200828 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 goto new_chk;
830 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100831 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832 }
833 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100834 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +0100835 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836}
837
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200838/*
839 * Start health-check.
840 * Returns 0 if OK, -1 if error, and prints the error in this case.
841 */
842int start_checks() {
843
844 struct proxy *px;
845 struct server *s;
846 struct task *t;
847 int nbchk=0, mininter=0, srvpos=0;
848
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200849 /* 1- count the checkers to run simultaneously.
850 * We also determine the minimum interval among all of those which
851 * have an interval larger than SRV_CHK_INTER_THRES. This interval
852 * will be used to spread their start-up date. Those which have
853 * a shorter interval will start independantly and will not dictate
854 * too short an interval for all others.
855 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200856 for (px = proxy; px; px = px->next) {
857 for (s = px->srv; s; s = s->next) {
858 if (!(s->state & SRV_CHECKED))
859 continue;
860
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100861 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
862 (!mininter || mininter > srv_getinter(s)))
863 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200864
865 nbchk++;
866 }
867 }
868
869 if (!nbchk)
870 return 0;
871
872 srand((unsigned)time(NULL));
873
874 /*
875 * 2- start them as far as possible from each others. For this, we will
876 * start them after their interval set to the min interval divided by
877 * the number of servers, weighted by the server's position in the list.
878 */
879 for (px = proxy; px; px = px->next) {
880 for (s = px->srv; s; s = s->next) {
881 if (!(s->state & SRV_CHECKED))
882 continue;
883
Willy Tarreaua4613182009-03-21 18:13:21 +0100884 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200885 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
886 return -1;
887 }
888
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200889 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200890 t->process = process_chk;
891 t->context = s;
892
893 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200894 t->expire = tick_add(now_ms,
895 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
896 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200897 task_queue(t);
898
899 srvpos++;
900 }
901 }
902 return 0;
903}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200904
905/*
906 * Local variables:
907 * c-indent-level: 8
908 * c-basic-offset: 8
909 * End:
910 */