blob: e7aefb49338c3cd3a3d73fe9542c55a7630a4787 [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 Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 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>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
18#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
23#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040024#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020026#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <arpa/inet.h>
28
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020032#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
35#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020038#include <proto/checks.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010039#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/fd.h>
41#include <proto/log.h>
42#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020043#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010044#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010045#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010046#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/server.h>
48#include <proto/task.h>
49
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020050const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
51 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
52 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
53 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020054
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020055 [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020056
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020057 [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" },
58 [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" },
59 [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020060
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020061 [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" },
62 [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" },
63 [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020064
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020065 [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" },
66 [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020067
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020068 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020069
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020070 [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" },
71 [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
72 [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020073};
74
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075/*
76 * Convert check_status code to description
77 */
78const char *get_check_status_description(short check_status) {
79
80 const char *desc;
81
82 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020083 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020084 else
85 desc = NULL;
86
87 if (desc && *desc)
88 return desc;
89 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020090 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020091}
92
93/*
94 * Convert check_status code to short info
95 */
96const char *get_check_status_info(short check_status) {
97
98 const char *info;
99
100 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200101 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200102 else
103 info = NULL;
104
105 if (info && *info)
106 return info;
107 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200108 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200109}
110
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200111#define SSP_O_VIA 0x0001
112#define SSP_O_HCHK 0x0002
113#define SSP_O_STATUS 0x0004
114
115static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
116
117 if (options & SSP_O_VIA)
118 chunk_printf(msg, " via %s/%s",
119 s->tracked->proxy->id, s->tracked->id);
120
121
122 if (options & SSP_O_HCHK) {
123 chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status));
124
125 if (s->check_status >= HCHK_STATUS_L57DATA)
126 chunk_printf(msg, ", code: %d", s->check_code);
127
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200128 if (*s->check_desc) {
129 struct chunk src;
130
131 chunk_printf(msg, ", info: \"");
132
133 chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc));
134 chunk_asciiencode(msg, &src, '"');
135
136 chunk_printf(msg, "\"");
137 }
138
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200139 chunk_printf(msg, ", check duration: %lums", s->check_duration);
140 }
141
142 if (options & SSP_O_STATUS) {
143 if (xferred)
144 chunk_printf(msg, ". %d active and %d backup servers left.%s"
145 " %d sessions active, %d requeued, %d remaining in queue.\n",
146 s->proxy->srv_act, s->proxy->srv_bck,
147 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
148 s->cur_sess, xferred, s->nbpend);
149 else
150 chunk_printf(msg, ". %d active and %d backup servers online.%s"
151 " %d sessions requeued, %d total in queue.\n",
152 s->proxy->srv_act, s->proxy->srv_bck,
153 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
154 xferred, s->nbpend);
155 }
156}
157
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200158/*
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200159 * Set s->check_status, update s->check_duration and fill s->result with
160 * an adequate SRV_CHK_* value.
161 *
162 * Show information in logs about failed health check if server is UP
163 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200164 */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200165static void set_server_check_status(struct server *s, short status, char *desc) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200166
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200167 struct chunk msg;
168
169 if (status == HCHK_STATUS_START) {
170 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200171 s->check_desc[0] = '\0';
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200172 s->check_start = now;
173 return;
174 }
175
176 if (!s->check_status)
177 return;
178
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200179 if (desc && *desc) {
180 strncpy(s->check_desc, desc, HCHK_DESC_LEN-1);
181 s->check_desc[HCHK_DESC_LEN-1] = '\0';
182 } else
183 s->check_desc[0] = '\0';
184
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200185 s->check_status = status;
186 if (check_statuses[status].result)
187 s->result |= check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200188
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200189 if (!tv_iszero(&s->check_start)) {
190 /* set_server_check_status() may be called more than once */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200191 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
192 tv_zero(&s->check_start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200193 }
194
195 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
196 (((s->health != 0) && (s->result & SRV_CHK_ERROR)) ||
197 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) ||
198 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
199 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
200
201 int health, rise, fall, state;
202
203 chunk_init(&msg, trash, sizeof(trash));
204
205 /* FIXME begin: calculate local version of the health/rise/fall/state */
206 health = s->health;
207 rise = s->rise;
208 fall = s->fall;
209 state = s->state;
210
211 if (s->result & SRV_CHK_ERROR) {
212 if (health > rise) {
213 health--; /* still good */
214 } else {
215 if (health == rise)
216 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
217
218 health = 0;
219 }
220 }
221
222 if (s->result & SRV_CHK_RUNNING) {
223 if (health < rise + fall - 1) {
224 health++; /* was bad, stays for a while */
225
226 if (health == rise)
227 state |= SRV_RUNNING;
228
229 if (health >= rise)
230 health = rise + fall - 1; /* OK now */
231 }
232 }
233 /* FIXME end: calculate local version of the health/rise/fall/state */
234
235 chunk_printf(&msg,
236 "Health check for %sserver %s/%s %s%s",
237 s->state & SRV_BACKUP ? "backup " : "",
238 s->proxy->id, s->id,
239 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
240 (s->result & SRV_CHK_RUNNING)?"succeeded":"failed");
241
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200242 server_status_printf(&msg, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200243
244 chunk_printf(&msg, ", status: %d/%d %s.\n",
245 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
246 (state & SRV_RUNNING) ? (fall) : (rise),
247 (state & SRV_RUNNING)?"UP":"DOWN");
248
249 Warning("%s", trash);
250 send_log(s->proxy, LOG_NOTICE, "%s", trash);
251 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200252}
253
Willy Tarreau48494c02007-11-30 10:41:39 +0100254/* sends a log message when a backend goes down, and also sets last
255 * change date.
256 */
257static void set_backend_down(struct proxy *be)
258{
259 be->last_change = now.tv_sec;
260 be->down_trans++;
261
262 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
263 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
264}
265
266/* Redistribute pending connections when a server goes down. The number of
267 * connections redistributed is returned.
268 */
269static int redistribute_pending(struct server *s)
270{
271 struct pendconn *pc, *pc_bck, *pc_end;
272 int xferred = 0;
273
274 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
275 struct session *sess = pc->sess;
276 if (sess->be->options & PR_O_REDISP) {
277 /* The REDISP option was specified. We will ignore
278 * cookie and force to balance or use the dispatcher.
279 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100280
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100281 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100282 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100283
Willy Tarreau48494c02007-11-30 10:41:39 +0100284 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200285 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100286 xferred++;
287 }
288 }
289 return xferred;
290}
291
292/* Check for pending connections at the backend, and assign some of them to
293 * the server coming up. The server's weight is checked before being assigned
294 * connections it may not be able to handle. The total number of transferred
295 * connections is returned.
296 */
297static int check_for_pending(struct server *s)
298{
299 int xferred;
300
301 if (!s->eweight)
302 return 0;
303
304 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
305 struct session *sess;
306 struct pendconn *p;
307
308 p = pendconn_from_px(s->proxy);
309 if (!p)
310 break;
311 p->sess->srv = s;
312 sess = p->sess;
313 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200314 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100315 }
316 return xferred;
317}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318
319/* Sets server <s> down, notifies by all available means, recounts the
320 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200321 * possible to other servers. It automatically recomputes the number of
322 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 */
Willy Tarreau83749182007-04-15 20:56:27 +0200324static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100326 struct server *srv;
327 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 int xferred;
329
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100330 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100331 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200332
333 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100334 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100335 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336
337 /* we might have sessions queued on this server and waiting for
338 * a connection. Those which are redispatchable will be queued
339 * to another server or to the proxy itself.
340 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100341 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100342
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200343 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100344
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200345 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100346 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
347 s->proxy->id, s->id);
348
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200349 server_status_printf(&msg, s,
350 (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS |
351 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
352 xferred);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353
354 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200355
Willy Tarreau48494c02007-11-30 10:41:39 +0100356 /* we don't send an alert if the server was previously paused */
357 if (srv_was_paused)
358 send_log(s->proxy, LOG_NOTICE, "%s", trash);
359 else
360 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200361
Willy Tarreau48494c02007-11-30 10:41:39 +0100362 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
363 set_backend_down(s->proxy);
364
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200365 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100366
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100367 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100368 for(srv = s->tracknext; srv; srv = srv->tracknext)
369 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100371
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 s->health = 0; /* failure */
373}
374
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100375static void set_server_up(struct server *s) {
376
377 struct server *srv;
378 struct chunk msg;
379 int xferred;
380
381 if (s->health == s->rise || s->tracked) {
382 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
383 if (s->proxy->last_change < now.tv_sec) // ignore negative times
384 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
385 s->proxy->last_change = now.tv_sec;
386 }
387
388 if (s->last_change < now.tv_sec) // ignore negative times
389 s->down_time += now.tv_sec - s->last_change;
390
391 s->last_change = now.tv_sec;
392 s->state |= SRV_RUNNING;
393
394 if (s->slowstart > 0) {
395 s->state |= SRV_WARMINGUP;
396 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
397 /* For dynamic algorithms, start at the first step of the weight,
398 * without multiplying by BE_WEIGHT_SCALE.
399 */
400 s->eweight = s->uweight;
401 if (s->proxy->lbprm.update_server_eweight)
402 s->proxy->lbprm.update_server_eweight(s);
403 }
404 }
405 s->proxy->lbprm.set_server_status_up(s);
406
407 /* check if we can handle some connections queued at the proxy. We
408 * will take as many as we can handle.
409 */
410 xferred = check_for_pending(s);
411
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200412 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100413
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200414 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100415 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
416 s->proxy->id, s->id);
417
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200418 server_status_printf(&msg, s,
419 (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS |
420 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
421 -1);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100422
423 Warning("%s", trash);
424 send_log(s->proxy, LOG_NOTICE, "%s", trash);
425
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100426 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100427 for(srv = s->tracknext; srv; srv = srv->tracknext)
428 set_server_up(srv);
429 }
430
431 if (s->health >= s->rise)
432 s->health = s->rise + s->fall - 1; /* OK now */
433
434}
435
436static void set_server_disabled(struct server *s) {
437
438 struct server *srv;
439 struct chunk msg;
440 int xferred;
441
442 s->state |= SRV_GOINGDOWN;
443 s->proxy->lbprm.set_server_status_down(s);
444
445 /* we might have sessions queued on this server and waiting for
446 * a connection. Those which are redispatchable will be queued
447 * to another server or to the proxy itself.
448 */
449 xferred = redistribute_pending(s);
450
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200451 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100452
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200453 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100454 "Load-balancing on %sServer %s/%s is disabled",
455 s->state & SRV_BACKUP ? "Backup " : "",
456 s->proxy->id, s->id);
457
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200458 server_status_printf(&msg, s,
459 (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS |
460 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
461 -1);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100462
463 Warning("%s", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100464 send_log(s->proxy, LOG_NOTICE, "%s", trash);
465
466 if (!s->proxy->srv_bck && !s->proxy->srv_act)
467 set_backend_down(s->proxy);
468
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100469 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100470 for(srv = s->tracknext; srv; srv = srv->tracknext)
471 set_server_disabled(srv);
472}
473
474static void set_server_enabled(struct server *s) {
475
476 struct server *srv;
477 struct chunk msg;
478 int xferred;
479
480 s->state &= ~SRV_GOINGDOWN;
481 s->proxy->lbprm.set_server_status_up(s);
482
483 /* check if we can handle some connections queued at the proxy. We
484 * will take as many as we can handle.
485 */
486 xferred = check_for_pending(s);
487
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200488 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100489
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200490 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100491 "Load-balancing on %sServer %s/%s is enabled again",
492 s->state & SRV_BACKUP ? "Backup " : "",
493 s->proxy->id, s->id);
494
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200495 server_status_printf(&msg, s,
496 (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS |
497 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
498 -1);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100499
500 Warning("%s", trash);
501 send_log(s->proxy, LOG_NOTICE, "%s", trash);
502
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100503 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100504 for(srv = s->tracknext; srv; srv = srv->tracknext)
505 set_server_enabled(srv);
506}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200507
508/*
509 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200510 * the connection acknowledgement. If the proxy requires L7 health-checks,
511 * it sends the request. In other cases, it calls set_server_check_status()
512 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200513 * The function itself returns 0 if it needs some polling before being called
514 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515 */
Willy Tarreau83749182007-04-15 20:56:27 +0200516static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517{
Willy Tarreau6996e152007-04-30 14:37:43 +0200518 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519 struct task *t = fdtab[fd].owner;
520 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100522 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200523 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200524 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200525 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200526 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200527
528 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200529
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100530 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200532 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200533 (s->proxy->options & PR_O_SSL3_CHK) ||
534 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200536 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537 * so we'll send the request, and won't wake the checker up now.
538 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200539
540 if (s->proxy->options & PR_O_SSL3_CHK) {
541 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200542 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200543 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
544 }
545
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100548 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100549 if (s->proxy->timeout.check)
550 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200551 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200552 goto out_nowake;
553 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200554 else if (ret == 0 || errno == EAGAIN)
555 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200556 else {
557 switch (errno) {
558 case ECONNREFUSED:
559 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200560 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200561 break;
562
563 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200564 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200565 }
566
Willy Tarreau6996e152007-04-30 14:37:43 +0200567 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200568 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 }
570 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200571 /* We have no data to send to check the connection, and
572 * getsockopt() will not inform us whether the connection
573 * is still pending. So we'll reuse connect() to check the
574 * state of the socket. This has the advantage of givig us
575 * the following info :
576 * - error
577 * - connecting (EALREADY, EINPROGRESS)
578 * - connected (EISCONN, 0)
579 */
580
581 struct sockaddr_in sa;
582
583 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
584 sa.sin_port = htons(s->check_port);
585
586 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
587 errno = 0;
588
589 if (errno == EALREADY || errno == EINPROGRESS)
590 goto out_poll;
591
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200592 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200593 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200594 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200595 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200596
Willy Tarreaubaaee002006-06-26 02:48:02 +0200597 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200598 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200599 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600 }
601 }
Willy Tarreau83749182007-04-15 20:56:27 +0200602 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200603 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200604 out_nowake:
605 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100606 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200607 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200608 out_poll:
609 /* The connection is still pending. We'll have to poll it
610 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100611 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200612 return 0;
613 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200614 fdtab[fd].state = FD_STERROR;
615 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200616}
617
618
619/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200620 * This function is used only for server health-checks. It handles the server's
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200621 * reply to an HTTP request or SSL HELLO. It calls set_server_check_status() to
622 * update s->check_status, s->check_duration and s->result.
623
624 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
625 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
626 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
627 * response to an SSL HELLO (the principle is that this is enough to
628 * distinguish between an SSL server and a pure TCP relay). All other cases will
629 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
630 * etc.
631 *
632 * The function returns 0 if it needs to be called again after some polling,
633 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 */
Willy Tarreau83749182007-04-15 20:56:27 +0200635static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636{
Willy Tarreau83749182007-04-15 20:56:27 +0200637 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100638 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 struct task *t = fdtab[fd].owner;
640 struct server *s = t->context;
641 int skerr;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200642 char *desc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200643 socklen_t lskerr = sizeof(skerr);
644
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100645 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200646
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100647 if (unlikely((s->result & SRV_CHK_ERROR) ||
648 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200649 (fdtab[fd].ev & FD_POLL_ERR) ||
650 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
651 (skerr != 0))) {
652 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200653
654 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200655 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200656
Willy Tarreau83749182007-04-15 20:56:27 +0200657 goto out_wakeup;
658 }
659
Willy Tarreau83749182007-04-15 20:56:27 +0200660 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
661 * but the connection was closed on the remote end. Fortunately, recv still
662 * works correctly and we don't need to do the getsockopt() on linux.
663 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200664 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreau83749182007-04-15 20:56:27 +0200665 if (unlikely(len < 0 && errno == EAGAIN)) {
666 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100667 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200668 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 }
670
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200671 if (len < sizeof(trash))
672 trash[len] = '\0';
673 else
674 trash[len-1] = '\0';
675
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100676 /* Note: the response will only be accepted if read at once */
677 if (s->proxy->options & PR_O_HTTP_CHK) {
678 /* Check if the server speaks HTTP 1.X */
679 if ((len < strlen("HTTP/1.0 000\r")) ||
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200680 (memcmp(trash, "HTTP/1.", 7) != 0 ||
681 (trash[12] != ' ' && trash[12] != '\r')) ||
682 !isdigit(trash[9]) || !isdigit(trash[10]) || !isdigit(trash[11])) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200683
684 cut_crlf(trash);
685 set_server_check_status(s, HCHK_STATUS_L7RSP, trash);
686
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100687 goto out_wakeup;
688 }
689
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200690 s->check_code = str2uic(&trash[9]);
691
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200692 desc = ltrim(&trash[12], ' ');
693 cut_crlf(desc);
694
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100695 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200696 if (trash[9] == '2' || trash[9] == '3')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200697 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200698 else if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau48494c02007-11-30 10:41:39 +0100699 (s->state & SRV_RUNNING) &&
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200700 (s->check_code == 404))
701 /* 404 may be accepted as "stopping" only if the server was up */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200702 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200703 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200704 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100705 }
706 else if (s->proxy->options & PR_O_SSL3_CHK) {
707 /* Check for SSLv3 alert or handshake */
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200708 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200709 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200710 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200711 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200712 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100713 else if (s->proxy->options & PR_O_SMTP_CHK) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200714 /* Check if the server speaks SMTP */
715 if ((len < strlen("000\r")) ||
716 (trash[3] != ' ' && trash[3] != '\r') ||
717 !isdigit(trash[0]) || !isdigit(trash[1]) || !isdigit(trash[2])) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200718
719 cut_crlf(trash);
720 set_server_check_status(s, HCHK_STATUS_L7RSP, trash);
721
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200722 goto out_wakeup;
723 }
724
725 s->check_code = str2uic(&trash[0]);
726
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200727 desc = ltrim(&trash[3], ' ');
728 cut_crlf(desc);
729
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100730 /* Check for SMTP code 2xx (should be 250) */
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200731 if (trash[0] == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200732 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200733 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200734 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau6996e152007-04-30 14:37:43 +0200735 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100736 else {
737 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200738 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau23677902007-05-08 23:50:35 +0200739 }
Willy Tarreau83749182007-04-15 20:56:27 +0200740
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100741 out_wakeup:
742 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 fdtab[fd].state = FD_STERROR;
744
Willy Tarreauf161a342007-04-08 16:59:42 +0200745 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200746 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100747 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200748 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749}
750
751/*
752 * manages a server health-check. Returns
753 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
754 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100755struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756{
Willy Tarreaue3838802009-03-21 18:58:32 +0100757 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758 struct server *s = t->context;
759 struct sockaddr_in sa;
760 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200761 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762
763 //fprintf(stderr, "process_chk: task=%p\n", t);
764
765 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +0100766 if (attempts++ > 0) {
767 /* we always fail to create a server, let's stop insisting... */
768 while (tick_is_expired(t->expire, now_ms))
769 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
770 return t;
771 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 fd = s->curfd;
773 if (fd < 0) { /* no check currently running */
774 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +0100775 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
776 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777
778 /* we don't send any health-checks when the proxy is stopped or when
779 * the server should not be checked.
780 */
781 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200782 while (tick_is_expired(t->expire, now_ms))
783 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +0100784 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 }
786
787 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200788 set_server_check_status(s, HCHK_STATUS_START, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
790 if ((fd < global.maxsock) &&
791 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
792 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
793 //fprintf(stderr, "process_chk: 3\n");
794
Willy Tarreau9edd1612007-10-18 18:07:48 +0200795 if (s->proxy->options & PR_O_TCP_NOLING) {
796 /* We don't want to useless data */
797 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
798 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200799
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200800 if (s->check_addr.sin_addr.s_addr)
801 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200802 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200803 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200804 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200805 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200806
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 sa.sin_port = htons(s->check_port);
809
810 /* allow specific binding :
811 * - server-specific at first
812 * - proxy-specific next
813 */
814 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100815 struct sockaddr_in *remote = NULL;
816 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100817
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100818#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100819 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
820 remote = (struct sockaddr_in *)&s->tproxy_addr;
821 flags = 3;
822 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100823#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +0100824#ifdef SO_BINDTODEVICE
825 /* Note: this might fail if not CAP_NET_RAW */
826 if (s->iface_name)
827 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100828 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +0100829#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200830 if (s->sport_range) {
831 int bind_attempts = 10; /* should be more than enough to find a spare port */
832 struct sockaddr_in src;
833
834 ret = 1;
835 src = s->source_addr;
836
837 do {
838 /* note: in case of retry, we may have to release a previously
839 * allocated port, hence this loop's construct.
840 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200841 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
842 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200843
844 if (!bind_attempts)
845 break;
846 bind_attempts--;
847
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200848 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
849 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200850 break;
851
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200852 fdinfo[fd].port_range = s->sport_range;
853 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200854
855 ret = tcpv4_bind_socket(fd, flags, &src, remote);
856 } while (ret != 0); /* binding NOK */
857 }
858 else {
859 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
860 }
861
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100862 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200863 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100864 switch (ret) {
865 case 1:
866 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
867 s->proxy->id, s->id);
868 break;
869 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100870 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
871 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100872 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100873 }
874 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200875 }
876 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100877 struct sockaddr_in *remote = NULL;
878 int ret, flags = 0;
879
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100880#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100881 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100882 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
883 flags = 3;
884 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100885#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100886#ifdef SO_BINDTODEVICE
887 /* Note: this might fail if not CAP_NET_RAW */
888 if (s->proxy->iface_name)
889 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100890 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100891#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100892 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
893 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200894 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100895 switch (ret) {
896 case 1:
897 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
898 proxy_type_str(s->proxy), s->proxy->id);
899 break;
900 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100901 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
902 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100903 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100904 }
905 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200906 }
907
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100908 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400909#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +0200910 /* disabling tcp quick ack now allows
911 * the request to leave the machine with
912 * the first ACK.
913 */
914 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400915 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +0200916#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
918 /* OK, connection in progress or established */
919
920 //fprintf(stderr, "process_chk: 4\n");
921
922 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200923 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200925 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
926 fdtab[fd].cb[DIR_RD].b = NULL;
927 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
928 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200929 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
930 fdinfo[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200932 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +0200933 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200935 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100937 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
938 /* we allow up to min(inter, timeout.connect) for a connection
939 * to establish but only when timeout.check is set
940 * as it may be to short for a full check otherwise
941 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200942 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100943
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200944 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
945 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
946 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +0100947 }
Willy Tarreau26c25062009-03-08 09:38:41 +0100948 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949 }
950 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200951 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200952
953 switch (errno) {
954 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
955 case ECONNREFUSED:
956 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200957 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200958 break;
959
960 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200961 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200962 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963 }
964 }
965 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200966 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
967 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200968 close(fd); /* socket creation error */
969 }
970
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100971 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200973 while (tick_is_expired(t->expire, now_ms))
974 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 goto new_chk; /* may be we should initialize a new check */
976 }
977
978 /* here, we have seen a failure */
979 if (s->health > s->rise) {
980 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200981 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200982 }
983 else
984 set_server_down(s);
985
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100986 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
987 /* we allow up to min(inter, timeout.connect) for a connection
988 * to establish but only when timeout.check is set
989 * as it may be to short for a full check otherwise
990 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200991 while (tick_is_expired(t->expire, now_ms)) {
992 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100993
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200994 t_con = tick_add(t->expire, s->proxy->timeout.connect);
995 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100996
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200997 if (s->proxy->timeout.check)
998 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100999 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001000 goto new_chk;
1001 }
1002 else {
1003 //fprintf(stderr, "process_chk: 8\n");
1004 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001005 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001007
Willy Tarreau9909fc12007-11-30 17:42:05 +01001008 if (s->state & SRV_WARMINGUP) {
1009 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1010 s->state &= ~SRV_WARMINGUP;
1011 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1012 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1013 if (s->proxy->lbprm.update_server_eweight)
1014 s->proxy->lbprm.update_server_eweight(s);
1015 }
1016 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1017 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +01001018 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1019 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +01001020 s->eweight *= s->uweight;
1021 if (s->proxy->lbprm.update_server_eweight)
1022 s->proxy->lbprm.update_server_eweight(s);
1023 }
1024 /* probably that we can refill this server with a bit more connections */
1025 check_for_pending(s);
1026 }
1027
Willy Tarreau48494c02007-11-30 10:41:39 +01001028 /* we may have to add/remove this server from the LB group */
1029 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1030 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001031 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1032 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001033 else if (!(s->state & SRV_GOINGDOWN) &&
1034 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001035 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1036 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001037 }
1038
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001039 if (s->health < s->rise + s->fall - 1) {
1040 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001041
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001042 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 }
1044 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001045 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001046
1047 rv = 0;
1048 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001049 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001050 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001051 //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 +02001052 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001053 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 goto new_chk;
1055 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001056 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001057 if (!(s->result & SRV_CHK_ERROR)) {
1058 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001059 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001060 } else {
1061 if (s->proxy->options & PR_O_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001062 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001063 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001064 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001065 }
1066 }
1067
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068 //fprintf(stderr, "process_chk: 10\n");
1069 /* failure or timeout detected */
1070 if (s->health > s->rise) {
1071 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001072 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001073 }
1074 else
1075 set_server_down(s);
1076 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001078
1079 rv = 0;
1080 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001081 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001082 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001083 //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 +02001084 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001085 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086 goto new_chk;
1087 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001088 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089 }
1090 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001091 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001092 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001093}
1094
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001095/*
1096 * Start health-check.
1097 * Returns 0 if OK, -1 if error, and prints the error in this case.
1098 */
1099int start_checks() {
1100
1101 struct proxy *px;
1102 struct server *s;
1103 struct task *t;
1104 int nbchk=0, mininter=0, srvpos=0;
1105
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001106 /* 1- count the checkers to run simultaneously.
1107 * We also determine the minimum interval among all of those which
1108 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1109 * will be used to spread their start-up date. Those which have
1110 * a shorter interval will start independantly and will not dictate
1111 * too short an interval for all others.
1112 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001113 for (px = proxy; px; px = px->next) {
1114 for (s = px->srv; s; s = s->next) {
1115 if (!(s->state & SRV_CHECKED))
1116 continue;
1117
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001118 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1119 (!mininter || mininter > srv_getinter(s)))
1120 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001121
1122 nbchk++;
1123 }
1124 }
1125
1126 if (!nbchk)
1127 return 0;
1128
1129 srand((unsigned)time(NULL));
1130
1131 /*
1132 * 2- start them as far as possible from each others. For this, we will
1133 * start them after their interval set to the min interval divided by
1134 * the number of servers, weighted by the server's position in the list.
1135 */
1136 for (px = proxy; px; px = px->next) {
1137 for (s = px->srv; s; s = s->next) {
1138 if (!(s->state & SRV_CHECKED))
1139 continue;
1140
Willy Tarreaua4613182009-03-21 18:13:21 +01001141 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001142 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1143 return -1;
1144 }
1145
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001146 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001147 t->process = process_chk;
1148 t->context = s;
1149
1150 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001151 t->expire = tick_add(now_ms,
1152 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1153 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001154 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001155 task_queue(t);
1156
1157 srvpos++;
1158 }
1159 }
1160 return 0;
1161}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001162
1163/*
1164 * Local variables:
1165 * c-indent-level: 8
1166 * c-basic-offset: 8
1167 * End:
1168 */