blob: b4e985702e242269a13cc41101486040f725b9c2 [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 Tarreauc6f4ce82009-06-10 11:09:37 +020039#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010040#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010041#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010042#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/server.h>
44#include <proto/task.h>
45
Willy Tarreau48494c02007-11-30 10:41:39 +010046/* sends a log message when a backend goes down, and also sets last
47 * change date.
48 */
49static void set_backend_down(struct proxy *be)
50{
51 be->last_change = now.tv_sec;
52 be->down_trans++;
53
54 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
55 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
56}
57
58/* Redistribute pending connections when a server goes down. The number of
59 * connections redistributed is returned.
60 */
61static int redistribute_pending(struct server *s)
62{
63 struct pendconn *pc, *pc_bck, *pc_end;
64 int xferred = 0;
65
66 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
67 struct session *sess = pc->sess;
68 if (sess->be->options & PR_O_REDISP) {
69 /* The REDISP option was specified. We will ignore
70 * cookie and force to balance or use the dispatcher.
71 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010072
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010073 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +010074 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010075
Willy Tarreau48494c02007-11-30 10:41:39 +010076 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +020077 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +010078 xferred++;
79 }
80 }
81 return xferred;
82}
83
84/* Check for pending connections at the backend, and assign some of them to
85 * the server coming up. The server's weight is checked before being assigned
86 * connections it may not be able to handle. The total number of transferred
87 * connections is returned.
88 */
89static int check_for_pending(struct server *s)
90{
91 int xferred;
92
93 if (!s->eweight)
94 return 0;
95
96 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
97 struct session *sess;
98 struct pendconn *p;
99
100 p = pendconn_from_px(s->proxy);
101 if (!p)
102 break;
103 p->sess->srv = s;
104 sess = p->sess;
105 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200106 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100107 }
108 return xferred;
109}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110
111/* Sets server <s> down, notifies by all available means, recounts the
112 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200113 * possible to other servers. It automatically recomputes the number of
114 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 */
Willy Tarreau83749182007-04-15 20:56:27 +0200116static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100118 struct server *srv;
119 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 int xferred;
121
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100122 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100123 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200124
125 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100126 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100127 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128
129 /* we might have sessions queued on this server and waiting for
130 * a connection. Those which are redispatchable will be queued
131 * to another server or to the proxy itself.
132 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100133 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100134
135 msg.len = 0;
136 msg.str = trash;
137
138 chunk_printf(&msg, sizeof(trash),
139 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
140 s->proxy->id, s->id);
141
142 if (s->tracked)
143 chunk_printf(&msg, sizeof(trash), " via %s/%s",
144 s->tracked->proxy->id, s->tracked->id);
145
146 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers left.%s"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147 " %d sessions active, %d requeued, %d remaining in queue.\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100148 s->proxy->srv_act, s->proxy->srv_bck,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
150 s->cur_sess, xferred, s->nbpend);
151
152 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200153
Willy Tarreau48494c02007-11-30 10:41:39 +0100154 /* we don't send an alert if the server was previously paused */
155 if (srv_was_paused)
156 send_log(s->proxy, LOG_NOTICE, "%s", trash);
157 else
158 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200159
Willy Tarreau48494c02007-11-30 10:41:39 +0100160 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
161 set_backend_down(s->proxy);
162
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 s->down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100164
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100165 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100166 for(srv = s->tracknext; srv; srv = srv->tracknext)
167 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100169
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 s->health = 0; /* failure */
171}
172
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100173static void set_server_up(struct server *s) {
174
175 struct server *srv;
176 struct chunk msg;
177 int xferred;
178
179 if (s->health == s->rise || s->tracked) {
180 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
181 if (s->proxy->last_change < now.tv_sec) // ignore negative times
182 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
183 s->proxy->last_change = now.tv_sec;
184 }
185
186 if (s->last_change < now.tv_sec) // ignore negative times
187 s->down_time += now.tv_sec - s->last_change;
188
189 s->last_change = now.tv_sec;
190 s->state |= SRV_RUNNING;
191
192 if (s->slowstart > 0) {
193 s->state |= SRV_WARMINGUP;
194 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
195 /* For dynamic algorithms, start at the first step of the weight,
196 * without multiplying by BE_WEIGHT_SCALE.
197 */
198 s->eweight = s->uweight;
199 if (s->proxy->lbprm.update_server_eweight)
200 s->proxy->lbprm.update_server_eweight(s);
201 }
202 }
203 s->proxy->lbprm.set_server_status_up(s);
204
205 /* check if we can handle some connections queued at the proxy. We
206 * will take as many as we can handle.
207 */
208 xferred = check_for_pending(s);
209
210 msg.len = 0;
211 msg.str = trash;
212
213 chunk_printf(&msg, sizeof(trash),
214 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
215 s->proxy->id, s->id);
216
217 if (s->tracked)
218 chunk_printf(&msg, sizeof(trash), " via %s/%s",
219 s->tracked->proxy->id, s->tracked->id);
220
221 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
222 " %d sessions requeued, %d total in queue.\n",
223 s->proxy->srv_act, s->proxy->srv_bck,
224 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreau1772ece2009-04-03 14:49:12 +0200225 xferred, s->nbpend);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100226
227 Warning("%s", trash);
228 send_log(s->proxy, LOG_NOTICE, "%s", trash);
229
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100230 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100231 for(srv = s->tracknext; srv; srv = srv->tracknext)
232 set_server_up(srv);
233 }
234
235 if (s->health >= s->rise)
236 s->health = s->rise + s->fall - 1; /* OK now */
237
238}
239
240static void set_server_disabled(struct server *s) {
241
242 struct server *srv;
243 struct chunk msg;
244 int xferred;
245
246 s->state |= SRV_GOINGDOWN;
247 s->proxy->lbprm.set_server_status_down(s);
248
249 /* we might have sessions queued on this server and waiting for
250 * a connection. Those which are redispatchable will be queued
251 * to another server or to the proxy itself.
252 */
253 xferred = redistribute_pending(s);
254
255 msg.len = 0;
256 msg.str = trash;
257
258 chunk_printf(&msg, sizeof(trash),
259 "Load-balancing on %sServer %s/%s is disabled",
260 s->state & SRV_BACKUP ? "Backup " : "",
261 s->proxy->id, s->id);
262
263 if (s->tracked)
264 chunk_printf(&msg, sizeof(trash), " via %s/%s",
265 s->tracked->proxy->id, s->tracked->id);
266
267
268 chunk_printf(&msg, sizeof(trash),". %d active and %d backup servers online.%s"
269 " %d sessions requeued, %d total in queue.\n",
270 s->proxy->srv_act, s->proxy->srv_bck,
271 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
272 xferred, s->nbpend);
273
274 Warning("%s", trash);
275
276 send_log(s->proxy, LOG_NOTICE, "%s", trash);
277
278 if (!s->proxy->srv_bck && !s->proxy->srv_act)
279 set_backend_down(s->proxy);
280
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100281 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100282 for(srv = s->tracknext; srv; srv = srv->tracknext)
283 set_server_disabled(srv);
284}
285
286static void set_server_enabled(struct server *s) {
287
288 struct server *srv;
289 struct chunk msg;
290 int xferred;
291
292 s->state &= ~SRV_GOINGDOWN;
293 s->proxy->lbprm.set_server_status_up(s);
294
295 /* check if we can handle some connections queued at the proxy. We
296 * will take as many as we can handle.
297 */
298 xferred = check_for_pending(s);
299
300 msg.len = 0;
301 msg.str = trash;
302
303 chunk_printf(&msg, sizeof(trash),
304 "Load-balancing on %sServer %s/%s is enabled again",
305 s->state & SRV_BACKUP ? "Backup " : "",
306 s->proxy->id, s->id);
307
308 if (s->tracked)
309 chunk_printf(&msg, sizeof(trash), " via %s/%s",
310 s->tracked->proxy->id, s->tracked->id);
311
312 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
313 " %d sessions requeued, %d total in queue.\n",
314 s->proxy->srv_act, s->proxy->srv_bck,
315 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
316 xferred, s->nbpend);
317
318 Warning("%s", trash);
319 send_log(s->proxy, LOG_NOTICE, "%s", trash);
320
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100321 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100322 for(srv = s->tracknext; srv; srv = srv->tracknext)
323 set_server_enabled(srv);
324}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325
326/*
327 * This function is used only for server health-checks. It handles
328 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100329 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200330 * The function itself returns 0 if it needs some polling before being called
331 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332 */
Willy Tarreau83749182007-04-15 20:56:27 +0200333static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334{
Willy Tarreau6996e152007-04-30 14:37:43 +0200335 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336 struct task *t = fdtab[fd].owner;
337 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100339 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Willy Tarreau6996e152007-04-30 14:37:43 +0200340 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
341 goto out_error;
342
343 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200344
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100345 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200347 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200348 (s->proxy->options & PR_O_SSL3_CHK) ||
349 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200351 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352 * so we'll send the request, and won't wake the checker up now.
353 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200354
355 if (s->proxy->options & PR_O_SSL3_CHK) {
356 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200357 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200358 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
359 }
360
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361#ifndef MSG_NOSIGNAL
362 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
363#else
364 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
365#endif
366 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100367 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100368 if (s->proxy->timeout.check)
369 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200370 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200371 goto out_nowake;
372 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200373 else if (ret == 0 || errno == EAGAIN)
374 goto out_poll;
375 else
376 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377 }
378 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200379 /* We have no data to send to check the connection, and
380 * getsockopt() will not inform us whether the connection
381 * is still pending. So we'll reuse connect() to check the
382 * state of the socket. This has the advantage of givig us
383 * the following info :
384 * - error
385 * - connecting (EALREADY, EINPROGRESS)
386 * - connected (EISCONN, 0)
387 */
388
389 struct sockaddr_in sa;
390
391 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
392 sa.sin_port = htons(s->check_port);
393
394 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
395 errno = 0;
396
397 if (errno == EALREADY || errno == EINPROGRESS)
398 goto out_poll;
399
400 if (errno && errno != EISCONN)
401 goto out_error;
402
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100404 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200405 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 }
407 }
Willy Tarreau83749182007-04-15 20:56:27 +0200408 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200409 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200410 out_nowake:
411 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100412 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200413 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200414 out_poll:
415 /* The connection is still pending. We'll have to poll it
416 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100417 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200418 return 0;
419 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100420 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200421 fdtab[fd].state = FD_STERROR;
422 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423}
424
425
426/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200427 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100428 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
429 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
430 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
431 * to an SSL HELLO (the principle is that this is enough to distinguish between
432 * an SSL server and a pure TCP relay). All other cases will set s->result to
433 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
434 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435 */
Willy Tarreau83749182007-04-15 20:56:27 +0200436static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437{
Willy Tarreau83749182007-04-15 20:56:27 +0200438 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100439 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 struct task *t = fdtab[fd].owner;
441 struct server *s = t->context;
442 int skerr;
443 socklen_t lskerr = sizeof(skerr);
444
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100445 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200446
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100447 if (unlikely((s->result & SRV_CHK_ERROR) ||
448 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200449 (fdtab[fd].ev & FD_POLL_ERR) ||
450 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
451 (skerr != 0))) {
452 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100453 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200454 goto out_wakeup;
455 }
456
Willy Tarreaubaaee002006-06-26 02:48:02 +0200457#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200458 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459#else
Willy Tarreau83749182007-04-15 20:56:27 +0200460 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
461 * but the connection was closed on the remote end. Fortunately, recv still
462 * works correctly and we don't need to do the getsockopt() on linux.
463 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200464 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200466 if (unlikely(len < 0 && errno == EAGAIN)) {
467 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100468 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200469 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470 }
471
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100472 /* Note: the response will only be accepted if read at once */
473 if (s->proxy->options & PR_O_HTTP_CHK) {
474 /* Check if the server speaks HTTP 1.X */
475 if ((len < strlen("HTTP/1.0 000\r")) ||
476 (memcmp(trash, "HTTP/1.", 7) != 0)) {
477 s->result |= SRV_CHK_ERROR;
478 goto out_wakeup;
479 }
480
481 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
482 if (trash[9] == '2' || trash[9] == '3')
483 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100484 else if ((s->proxy->options & PR_O_DISABLE404) &&
485 (s->state & SRV_RUNNING) &&
486 (memcmp(&trash[9], "404", 3) == 0)) {
487 /* 404 may be accepted as "stopping" only if the server was up */
488 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
489 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100490 else
491 s->result |= SRV_CHK_ERROR;
492 }
493 else if (s->proxy->options & PR_O_SSL3_CHK) {
494 /* Check for SSLv3 alert or handshake */
495 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
496 s->result |= SRV_CHK_RUNNING;
497 else
498 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200499 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100500 else if (s->proxy->options & PR_O_SMTP_CHK) {
501 /* Check for SMTP code 2xx (should be 250) */
502 if ((len >= 3) && (trash[0] == '2'))
503 s->result |= SRV_CHK_RUNNING;
504 else
505 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200506 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100507 else {
508 /* other checks are valid if the connection succeeded anyway */
509 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200510 }
Willy Tarreau83749182007-04-15 20:56:27 +0200511
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100512 out_wakeup:
513 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200514 fdtab[fd].state = FD_STERROR;
515
Willy Tarreauf161a342007-04-08 16:59:42 +0200516 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200517 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100518 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200519 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520}
521
522/*
523 * manages a server health-check. Returns
524 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
525 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100526struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527{
Willy Tarreaue3838802009-03-21 18:58:32 +0100528 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529 struct server *s = t->context;
530 struct sockaddr_in sa;
531 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200532 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533
534 //fprintf(stderr, "process_chk: task=%p\n", t);
535
536 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +0100537 if (attempts++ > 0) {
538 /* we always fail to create a server, let's stop insisting... */
539 while (tick_is_expired(t->expire, now_ms))
540 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
541 return t;
542 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 fd = s->curfd;
544 if (fd < 0) { /* no check currently running */
545 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +0100546 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
547 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548
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 Tarreau0c303ee2008-07-07 00:09:58 +0200553 while (tick_is_expired(t->expire, now_ms))
554 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +0100555 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556 }
557
558 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100559 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200560 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
561 if ((fd < global.maxsock) &&
562 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
563 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
564 //fprintf(stderr, "process_chk: 3\n");
565
Willy Tarreau9edd1612007-10-18 18:07:48 +0200566 if (s->proxy->options & PR_O_TCP_NOLING) {
567 /* We don't want to useless data */
568 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
569 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200570
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200571 if (s->check_addr.sin_addr.s_addr)
572 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200573 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200574 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200575 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200576 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200577
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 sa.sin_port = htons(s->check_port);
580
581 /* allow specific binding :
582 * - server-specific at first
583 * - proxy-specific next
584 */
585 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100586 struct sockaddr_in *remote = NULL;
587 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100588
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100589#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100590 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
591 remote = (struct sockaddr_in *)&s->tproxy_addr;
592 flags = 3;
593 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100594#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +0100595#ifdef SO_BINDTODEVICE
596 /* Note: this might fail if not CAP_NET_RAW */
597 if (s->iface_name)
598 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100599 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +0100600#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200601 if (s->sport_range) {
602 int bind_attempts = 10; /* should be more than enough to find a spare port */
603 struct sockaddr_in src;
604
605 ret = 1;
606 src = s->source_addr;
607
608 do {
609 /* note: in case of retry, we may have to release a previously
610 * allocated port, hence this loop's construct.
611 */
612 port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port);
613 fdtab[fd].port_range = NULL;
614
615 if (!bind_attempts)
616 break;
617 bind_attempts--;
618
619 fdtab[fd].local_port = port_range_alloc_port(s->sport_range);
620 if (!fdtab[fd].local_port)
621 break;
622
623 fdtab[fd].port_range = s->sport_range;
624 src.sin_port = htons(fdtab[fd].local_port);
625
626 ret = tcpv4_bind_socket(fd, flags, &src, remote);
627 } while (ret != 0); /* binding NOK */
628 }
629 else {
630 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
631 }
632
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100633 if (ret) {
634 s->result |= SRV_CHK_ERROR;
635 switch (ret) {
636 case 1:
637 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
638 s->proxy->id, s->id);
639 break;
640 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100641 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
642 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100643 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100644 }
645 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 }
647 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100648 struct sockaddr_in *remote = NULL;
649 int ret, flags = 0;
650
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100651#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100652 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100653 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
654 flags = 3;
655 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100656#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100657#ifdef SO_BINDTODEVICE
658 /* Note: this might fail if not CAP_NET_RAW */
659 if (s->proxy->iface_name)
660 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100661 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100662#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100663 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
664 if (ret) {
665 s->result |= SRV_CHK_ERROR;
666 switch (ret) {
667 case 1:
668 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
669 proxy_type_str(s->proxy), s->proxy->id);
670 break;
671 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100672 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
673 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100674 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100675 }
676 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 }
678
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100679 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
681 /* OK, connection in progress or established */
682
683 //fprintf(stderr, "process_chk: 4\n");
684
685 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200686 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200688 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
689 fdtab[fd].cb[DIR_RD].b = NULL;
690 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
691 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200692 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
693 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreauf161a342007-04-08 16:59:42 +0200695 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200697 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100699 //fprintf(stderr, "process_chk: 4+, %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 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200704 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100705
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200706 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
707 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
708 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +0100709 }
Willy Tarreau26c25062009-03-08 09:38:41 +0100710 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711 }
712 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100713 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 }
715 }
716 }
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200717 port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port);
718 fdtab[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 close(fd); /* socket creation error */
720 }
721
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100722 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200724 while (tick_is_expired(t->expire, now_ms))
725 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 goto new_chk; /* may be we should initialize a new check */
727 }
728
729 /* here, we have seen a failure */
730 if (s->health > s->rise) {
731 s->health--; /* still good */
732 s->failed_checks++;
733 }
734 else
735 set_server_down(s);
736
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100737 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
738 /* we allow up to min(inter, timeout.connect) for a connection
739 * to establish but only when timeout.check is set
740 * as it may be to short for a full check otherwise
741 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200742 while (tick_is_expired(t->expire, now_ms)) {
743 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100744
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200745 t_con = tick_add(t->expire, s->proxy->timeout.connect);
746 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100747
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200748 if (s->proxy->timeout.check)
749 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100750 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751 goto new_chk;
752 }
753 else {
754 //fprintf(stderr, "process_chk: 8\n");
755 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100756 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200758
Willy Tarreau9909fc12007-11-30 17:42:05 +0100759 if (s->state & SRV_WARMINGUP) {
760 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
761 s->state &= ~SRV_WARMINGUP;
762 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
763 s->eweight = s->uweight * BE_WEIGHT_SCALE;
764 if (s->proxy->lbprm.update_server_eweight)
765 s->proxy->lbprm.update_server_eweight(s);
766 }
767 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
768 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100769 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
770 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100771 s->eweight *= s->uweight;
772 if (s->proxy->lbprm.update_server_eweight)
773 s->proxy->lbprm.update_server_eweight(s);
774 }
775 /* probably that we can refill this server with a bit more connections */
776 check_for_pending(s);
777 }
778
Willy Tarreau48494c02007-11-30 10:41:39 +0100779 /* we may have to add/remove this server from the LB group */
780 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
781 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100782 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
783 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100784 else if (!(s->state & SRV_GOINGDOWN) &&
785 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100786 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
787 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100788 }
789
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200790 if (s->health < s->rise + s->fall - 1) {
791 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100793 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794 }
795 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200797
798 rv = 0;
799 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100800 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200801 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100802 //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 +0200803 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200804 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 goto new_chk;
806 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200807 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 //fprintf(stderr, "process_chk: 10\n");
809 /* failure or timeout detected */
810 if (s->health > s->rise) {
811 s->health--; /* still good */
812 s->failed_checks++;
813 }
814 else
815 set_server_down(s);
816 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200818
819 rv = 0;
820 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100821 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200822 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100823 //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 +0200824 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200825 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 goto new_chk;
827 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100828 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 }
830 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100831 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +0100832 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833}
834
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200835/*
836 * Start health-check.
837 * Returns 0 if OK, -1 if error, and prints the error in this case.
838 */
839int start_checks() {
840
841 struct proxy *px;
842 struct server *s;
843 struct task *t;
844 int nbchk=0, mininter=0, srvpos=0;
845
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200846 /* 1- count the checkers to run simultaneously.
847 * We also determine the minimum interval among all of those which
848 * have an interval larger than SRV_CHK_INTER_THRES. This interval
849 * will be used to spread their start-up date. Those which have
850 * a shorter interval will start independantly and will not dictate
851 * too short an interval for all others.
852 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200853 for (px = proxy; px; px = px->next) {
854 for (s = px->srv; s; s = s->next) {
855 if (!(s->state & SRV_CHECKED))
856 continue;
857
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100858 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
859 (!mininter || mininter > srv_getinter(s)))
860 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200861
862 nbchk++;
863 }
864 }
865
866 if (!nbchk)
867 return 0;
868
869 srand((unsigned)time(NULL));
870
871 /*
872 * 2- start them as far as possible from each others. For this, we will
873 * start them after their interval set to the min interval divided by
874 * the number of servers, weighted by the server's position in the list.
875 */
876 for (px = proxy; px; px = px->next) {
877 for (s = px->srv; s; s = s->next) {
878 if (!(s->state & SRV_CHECKED))
879 continue;
880
Willy Tarreaua4613182009-03-21 18:13:21 +0100881 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200882 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
883 return -1;
884 }
885
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200886 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200887 t->process = process_chk;
888 t->context = s;
889
890 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200891 t->expire = tick_add(now_ms,
892 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
893 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200894 task_queue(t);
895
896 srvpos++;
897 }
898 }
899 return 0;
900}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200901
902/*
903 * Local variables:
904 * c-indent-level: 8
905 * c-basic-offset: 8
906 * End:
907 */