blob: f59fbbe89ea7df5868d9466fc144da9ee54273e8 [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 Oledzki97f07b82009-12-15 22:31:24 +010055 [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" },
56
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020057 [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020058
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020059 [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" },
60 [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" },
61 [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020062
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020063 [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" },
64 [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" },
65 [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020066
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020067 [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" },
68 [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020069
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020070 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020071
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020072 [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" },
73 [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
74 [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075};
76
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010077const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
78 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
79
80 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
81 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
82
83 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
84 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
85 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
86 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
87
88 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
89 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
90 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
91};
92
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020093/*
94 * Convert check_status code to description
95 */
96const char *get_check_status_description(short check_status) {
97
98 const char *desc;
99
100 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200101 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200102 else
103 desc = NULL;
104
105 if (desc && *desc)
106 return desc;
107 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200108 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200109}
110
111/*
112 * Convert check_status code to short info
113 */
114const char *get_check_status_info(short check_status) {
115
116 const char *info;
117
118 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200119 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200120 else
121 info = NULL;
122
123 if (info && *info)
124 return info;
125 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200126 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200127}
128
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100129const char *get_analyze_status(short analyze_status) {
130
131 const char *desc;
132
133 if (analyze_status < HANA_STATUS_SIZE)
134 desc = analyze_statuses[analyze_status].desc;
135 else
136 desc = NULL;
137
138 if (desc && *desc)
139 return desc;
140 else
141 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
142}
143
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200144#define SSP_O_HCHK 0x0002
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200145
146static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
147
Krzysztof Piotr Oledzki5f5b7d22010-01-11 11:13:39 +0100148 if (s->tracked)
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200149 chunk_printf(msg, " via %s/%s",
150 s->tracked->proxy->id, s->tracked->id);
151
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200152 if (options & SSP_O_HCHK) {
153 chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status));
154
155 if (s->check_status >= HCHK_STATUS_L57DATA)
156 chunk_printf(msg, ", code: %d", s->check_code);
157
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200158 if (*s->check_desc) {
159 struct chunk src;
160
161 chunk_printf(msg, ", info: \"");
162
163 chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc));
164 chunk_asciiencode(msg, &src, '"');
165
166 chunk_printf(msg, "\"");
167 }
168
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100169 if (s->check_duration >= 0)
170 chunk_printf(msg, ", check duration: %ldms", s->check_duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200171 }
172
Krzysztof Piotr Oledzki5f5b7d22010-01-11 11:13:39 +0100173 if (xferred > 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100174 if (!(s->state & SRV_RUNNING))
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200175 chunk_printf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100176 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200177 s->proxy->srv_act, s->proxy->srv_bck,
178 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
179 s->cur_sess, xferred, s->nbpend);
180 else
181 chunk_printf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100182 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200183 s->proxy->srv_act, s->proxy->srv_bck,
184 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
185 xferred, s->nbpend);
186 }
187}
188
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200189/*
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200190 * Set s->check_status, update s->check_duration and fill s->result with
191 * an adequate SRV_CHK_* value.
192 *
193 * Show information in logs about failed health check if server is UP
194 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200195 */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200196static void set_server_check_status(struct server *s, short status, char *desc) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200197
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200198 struct chunk msg;
199
200 if (status == HCHK_STATUS_START) {
201 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200202 s->check_desc[0] = '\0';
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200203 s->check_start = now;
204 return;
205 }
206
207 if (!s->check_status)
208 return;
209
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200210 if (desc && *desc) {
211 strncpy(s->check_desc, desc, HCHK_DESC_LEN-1);
212 s->check_desc[HCHK_DESC_LEN-1] = '\0';
213 } else
214 s->check_desc[0] = '\0';
215
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200216 s->check_status = status;
217 if (check_statuses[status].result)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100218 s->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200219
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100220 if (status == HCHK_STATUS_HANA)
221 s->check_duration = -1;
222 else if (!tv_iszero(&s->check_start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200223 /* set_server_check_status() may be called more than once */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200224 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
225 tv_zero(&s->check_start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200226 }
227
228 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
229 (((s->health != 0) && (s->result & SRV_CHK_ERROR)) ||
230 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) ||
231 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
232 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
233
234 int health, rise, fall, state;
235
236 chunk_init(&msg, trash, sizeof(trash));
237
238 /* FIXME begin: calculate local version of the health/rise/fall/state */
239 health = s->health;
240 rise = s->rise;
241 fall = s->fall;
242 state = s->state;
243
244 if (s->result & SRV_CHK_ERROR) {
245 if (health > rise) {
246 health--; /* still good */
247 } else {
248 if (health == rise)
249 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
250
251 health = 0;
252 }
253 }
254
255 if (s->result & SRV_CHK_RUNNING) {
256 if (health < rise + fall - 1) {
257 health++; /* was bad, stays for a while */
258
259 if (health == rise)
260 state |= SRV_RUNNING;
261
262 if (health >= rise)
263 health = rise + fall - 1; /* OK now */
264 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100265
266 /* clear consecutive_errors if observing is enabled */
267 if (s->onerror)
268 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200269 }
270 /* FIXME end: calculate local version of the health/rise/fall/state */
271
272 chunk_printf(&msg,
273 "Health check for %sserver %s/%s %s%s",
274 s->state & SRV_BACKUP ? "backup " : "",
275 s->proxy->id, s->id,
276 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
277 (s->result & SRV_CHK_RUNNING)?"succeeded":"failed");
278
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200279 server_status_printf(&msg, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200280
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100281 chunk_printf(&msg, ", status: %d/%d %s",
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200282 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
283 (state & SRV_RUNNING) ? (fall) : (rise),
284 (state & SRV_RUNNING)?"UP":"DOWN");
285
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100286 Warning("%s.\n", trash);
287 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200288 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200289}
290
Willy Tarreau48494c02007-11-30 10:41:39 +0100291/* sends a log message when a backend goes down, and also sets last
292 * change date.
293 */
294static void set_backend_down(struct proxy *be)
295{
296 be->last_change = now.tv_sec;
297 be->down_trans++;
298
299 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
300 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
301}
302
303/* Redistribute pending connections when a server goes down. The number of
304 * connections redistributed is returned.
305 */
306static int redistribute_pending(struct server *s)
307{
308 struct pendconn *pc, *pc_bck, *pc_end;
309 int xferred = 0;
310
311 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
312 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100313 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
314 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100315 /* The REDISP option was specified. We will ignore
316 * cookie and force to balance or use the dispatcher.
317 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100318
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100319 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100320 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100321
Willy Tarreau48494c02007-11-30 10:41:39 +0100322 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200323 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100324 xferred++;
325 }
326 }
327 return xferred;
328}
329
330/* Check for pending connections at the backend, and assign some of them to
331 * the server coming up. The server's weight is checked before being assigned
332 * connections it may not be able to handle. The total number of transferred
333 * connections is returned.
334 */
335static int check_for_pending(struct server *s)
336{
337 int xferred;
338
339 if (!s->eweight)
340 return 0;
341
342 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
343 struct session *sess;
344 struct pendconn *p;
345
346 p = pendconn_from_px(s->proxy);
347 if (!p)
348 break;
349 p->sess->srv = s;
350 sess = p->sess;
351 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200352 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100353 }
354 return xferred;
355}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356
357/* Sets server <s> down, notifies by all available means, recounts the
358 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200359 * possible to other servers. It automatically recomputes the number of
360 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 */
Willy Tarreau83749182007-04-15 20:56:27 +0200362static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100364 struct server *srv;
365 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366 int xferred;
367
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100368 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100369 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200370
371 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100372 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100373 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374
375 /* we might have sessions queued on this server and waiting for
376 * a connection. Those which are redispatchable will be queued
377 * to another server or to the proxy itself.
378 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100379 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100380
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200381 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100382
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200383 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100384 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
385 s->proxy->id, s->id);
386
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200387 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200388 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
389 xferred);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100391 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200392
Willy Tarreau48494c02007-11-30 10:41:39 +0100393 /* we don't send an alert if the server was previously paused */
394 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100395 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100396 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100397 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200398
Willy Tarreau48494c02007-11-30 10:41:39 +0100399 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
400 set_backend_down(s->proxy);
401
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200402 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100403
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100404 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100405 for(srv = s->tracknext; srv; srv = srv->tracknext)
406 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100408
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 s->health = 0; /* failure */
410}
411
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100412static void set_server_up(struct server *s) {
413
414 struct server *srv;
415 struct chunk msg;
416 int xferred;
417
418 if (s->health == s->rise || s->tracked) {
419 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
420 if (s->proxy->last_change < now.tv_sec) // ignore negative times
421 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
422 s->proxy->last_change = now.tv_sec;
423 }
424
425 if (s->last_change < now.tv_sec) // ignore negative times
426 s->down_time += now.tv_sec - s->last_change;
427
428 s->last_change = now.tv_sec;
429 s->state |= SRV_RUNNING;
430
431 if (s->slowstart > 0) {
432 s->state |= SRV_WARMINGUP;
433 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
434 /* For dynamic algorithms, start at the first step of the weight,
435 * without multiplying by BE_WEIGHT_SCALE.
436 */
437 s->eweight = s->uweight;
438 if (s->proxy->lbprm.update_server_eweight)
439 s->proxy->lbprm.update_server_eweight(s);
440 }
441 }
442 s->proxy->lbprm.set_server_status_up(s);
443
444 /* check if we can handle some connections queued at the proxy. We
445 * will take as many as we can handle.
446 */
447 xferred = check_for_pending(s);
448
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200449 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100450
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200451 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100452 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
453 s->proxy->id, s->id);
454
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200455 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200456 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100457 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100458
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100459 Warning("%s.\n", trash);
460 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100461
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100462 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100463 for(srv = s->tracknext; srv; srv = srv->tracknext)
464 set_server_up(srv);
465 }
466
467 if (s->health >= s->rise)
468 s->health = s->rise + s->fall - 1; /* OK now */
469
470}
471
472static void set_server_disabled(struct server *s) {
473
474 struct server *srv;
475 struct chunk msg;
476 int xferred;
477
478 s->state |= SRV_GOINGDOWN;
479 s->proxy->lbprm.set_server_status_down(s);
480
481 /* we might have sessions queued on this server and waiting for
482 * a connection. Those which are redispatchable will be queued
483 * to another server or to the proxy itself.
484 */
485 xferred = redistribute_pending(s);
486
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200487 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100488
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200489 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100490 "Load-balancing on %sServer %s/%s is disabled",
491 s->state & SRV_BACKUP ? "Backup " : "",
492 s->proxy->id, s->id);
493
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200494 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200495 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100496 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100497
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100498 Warning("%s.\n", trash);
499 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100500
501 if (!s->proxy->srv_bck && !s->proxy->srv_act)
502 set_backend_down(s->proxy);
503
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100504 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100505 for(srv = s->tracknext; srv; srv = srv->tracknext)
506 set_server_disabled(srv);
507}
508
509static void set_server_enabled(struct server *s) {
510
511 struct server *srv;
512 struct chunk msg;
513 int xferred;
514
515 s->state &= ~SRV_GOINGDOWN;
516 s->proxy->lbprm.set_server_status_up(s);
517
518 /* check if we can handle some connections queued at the proxy. We
519 * will take as many as we can handle.
520 */
521 xferred = check_for_pending(s);
522
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200523 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100524
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200525 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100526 "Load-balancing on %sServer %s/%s is enabled again",
527 s->state & SRV_BACKUP ? "Backup " : "",
528 s->proxy->id, s->id);
529
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200530 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200531 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100532 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100533
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100534 Warning("%s.\n", trash);
535 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100536
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100537 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100538 for(srv = s->tracknext; srv; srv = srv->tracknext)
539 set_server_enabled(srv);
540}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100542void health_adjust(struct server *s, short status) {
543
544 int failed;
545 int expire;
546
547 /* return now if observing nor health check is not enabled */
548 if (!s->observe || !s->check)
549 return;
550
551 if (s->observe >= HANA_OBS_SIZE)
552 return;
553
554 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
555 return;
556
557 switch (analyze_statuses[status].lr[s->observe - 1]) {
558 case 1:
559 failed = 1;
560 break;
561
562 case 2:
563 failed = 0;
564 break;
565
566 default:
567 return;
568 }
569
570 if (!failed) {
571 /* good: clear consecutive_errors */
572 s->consecutive_errors = 0;
573 return;
574 }
575
576 s->consecutive_errors++;
577
578 if (s->consecutive_errors < s->consecutive_errors_limit)
579 return;
580
581 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
582 s->consecutive_errors, get_analyze_status(status));
583
584 switch (s->onerror) {
585 case HANA_ONERR_FASTINTER:
586 /* force fastinter - nothing to do here as all modes force it */
587 break;
588
589 case HANA_ONERR_SUDDTH:
590 /* simulate a pre-fatal failed health check */
591 if (s->health > s->rise)
592 s->health = s->rise + 1;
593
594 /* no break - fall through */
595
596 case HANA_ONERR_FAILCHK:
597 /* simulate a failed health check */
598 set_server_check_status(s, HCHK_STATUS_HANA, trash);
599
600 if (s->health > s->rise) {
601 s->health--; /* still good */
602 s->counters.failed_checks++;
603 }
604 else
605 set_server_down(s);
606
607 break;
608
609 case HANA_ONERR_MARKDWN:
610 /* mark server down */
611 s->health = s->rise;
612 set_server_check_status(s, HCHK_STATUS_HANA, trash);
613 set_server_down(s);
614
615 break;
616
617 default:
618 /* write a warning? */
619 break;
620 }
621
622 s->consecutive_errors = 0;
623 s->counters.failed_hana++;
624
625 if (s->fastinter) {
626 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
627 if (s->check->expire > expire)
628 s->check->expire = expire;
629 }
630}
631
Willy Tarreauef781042010-01-27 11:53:01 +0100632static int httpchk_build_status_header(struct server *s, char *buffer)
633{
634 int sv_state;
635 int ratio;
636 int hlen = 0;
637 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
638 "UP %d/%d", "UP",
639 "NOLB %d/%d", "NOLB",
640 "no check" };
641
642 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
643 hlen += 24;
644
645 if (!(s->state & SRV_CHECKED))
646 sv_state = 6; /* should obviously never happen */
647 else if (s->state & SRV_RUNNING) {
648 if (s->health == s->rise + s->fall - 1)
649 sv_state = 3; /* UP */
650 else
651 sv_state = 2; /* going down */
652
653 if (s->state & SRV_GOINGDOWN)
654 sv_state += 2;
655 } else {
656 if (s->health)
657 sv_state = 1; /* going up */
658 else
659 sv_state = 0; /* DOWN */
660 }
661
662 hlen += sprintf(buffer + hlen,
663 srv_hlt_st[sv_state],
664 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
665 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
666
667 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
668 s->proxy->id, s->id,
669 global.node,
670 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
671 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
672 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
673 s->nbpend);
674
675 if ((s->state & SRV_WARMINGUP) &&
676 now.tv_sec < s->last_change + s->slowstart &&
677 now.tv_sec >= s->last_change) {
678 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
679 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
680 }
681
682 buffer[hlen++] = '\r';
683 buffer[hlen++] = '\n';
684
685 return hlen;
686}
687
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688/*
689 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200690 * the connection acknowledgement. If the proxy requires L7 health-checks,
691 * it sends the request. In other cases, it calls set_server_check_status()
692 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200693 * The function itself returns 0 if it needs some polling before being called
694 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 */
Willy Tarreau83749182007-04-15 20:56:27 +0200696static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697{
Willy Tarreau6996e152007-04-30 14:37:43 +0200698 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699 struct task *t = fdtab[fd].owner;
700 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100702 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200703 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100704 int skerr, err = errno;
705 socklen_t lskerr = sizeof(skerr);
706
707 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
708 err = skerr;
709
710 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200711 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200712 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200713
714 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200715
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100716 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200718 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200719 (s->proxy->options & PR_O_SSL3_CHK) ||
Hervé COMMOWICK698ae002010-01-12 09:25:13 +0100720 (s->proxy->options & PR_O_SMTP_CHK) ||
721 (s->proxy->options2 & PR_O2_MYSQL_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100723 const char *check_req = s->proxy->check_req;
724 int check_len = s->proxy->check_len;
725
Willy Tarreauf3c69202006-07-09 16:42:34 +0200726 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 * so we'll send the request, and won't wake the checker up now.
728 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200729
730 if (s->proxy->options & PR_O_SSL3_CHK) {
731 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200732 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200733 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
734 }
Willy Tarreaue9d87882010-01-27 11:28:42 +0100735 else if (s->proxy->options & PR_O_HTTP_CHK) {
736 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100737
738 if (s->proxy->options2 & PR_O2_CHK_SNDST)
739 check_len += httpchk_build_status_header(s, trash + check_len);
740
Willy Tarreaue9d87882010-01-27 11:28:42 +0100741 trash[check_len++] = '\r';
742 trash[check_len++] = '\n';
743 trash[check_len] = '\0';
744 check_req = trash;
745 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200746
Willy Tarreaue9d87882010-01-27 11:28:42 +0100747 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
748 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100749 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100750 if (s->proxy->timeout.check)
751 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200752 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200753 goto out_nowake;
754 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200755 else if (ret == 0 || errno == EAGAIN)
756 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200757 else {
758 switch (errno) {
759 case ECONNREFUSED:
760 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200761 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200762 break;
763
764 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200765 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200766 }
767
Willy Tarreau6996e152007-04-30 14:37:43 +0200768 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200769 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 }
771 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200772 /* We have no data to send to check the connection, and
773 * getsockopt() will not inform us whether the connection
774 * is still pending. So we'll reuse connect() to check the
775 * state of the socket. This has the advantage of givig us
776 * the following info :
777 * - error
778 * - connecting (EALREADY, EINPROGRESS)
779 * - connected (EISCONN, 0)
780 */
781
782 struct sockaddr_in sa;
783
784 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
785 sa.sin_port = htons(s->check_port);
786
787 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
788 errno = 0;
789
790 if (errno == EALREADY || errno == EINPROGRESS)
791 goto out_poll;
792
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200793 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200794 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200795 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200796 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200797
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200799 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200800 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 }
802 }
Willy Tarreau83749182007-04-15 20:56:27 +0200803 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200804 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200805 out_nowake:
806 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100807 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200808 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200809 out_poll:
810 /* The connection is still pending. We'll have to poll it
811 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100812 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200813 return 0;
814 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200815 fdtab[fd].state = FD_STERROR;
816 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817}
818
819
820/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200821 * This function is used only for server health-checks. It handles the server's
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200822 * reply to an HTTP request or SSL HELLO. It calls set_server_check_status() to
823 * update s->check_status, s->check_duration and s->result.
824
825 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
826 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
827 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
828 * response to an SSL HELLO (the principle is that this is enough to
829 * distinguish between an SSL server and a pure TCP relay). All other cases will
830 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
831 * etc.
832 *
833 * The function returns 0 if it needs to be called again after some polling,
834 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835 */
Willy Tarreau83749182007-04-15 20:56:27 +0200836static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200837{
Willy Tarreau83749182007-04-15 20:56:27 +0200838 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100839 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840 struct task *t = fdtab[fd].owner;
841 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200842 char *desc;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100844 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200845
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100846 if (unlikely((s->result & SRV_CHK_ERROR) ||
847 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreaua5aa1c82010-01-15 10:35:58 +0100848 (fdtab[fd].ev & FD_POLL_ERR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200849 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200850 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200851 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200852
Willy Tarreau83749182007-04-15 20:56:27 +0200853 goto out_wakeup;
854 }
855
Willy Tarreau83749182007-04-15 20:56:27 +0200856 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
857 * but the connection was closed on the remote end. Fortunately, recv still
858 * works correctly and we don't need to do the getsockopt() on linux.
859 */
Willy Tarreaufc1daaf2010-01-15 10:26:13 +0100860 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaua5aa1c82010-01-15 10:35:58 +0100861 if (unlikely(len < 0)) {
862 if (errno == EAGAIN) {
863 /* not ready, we want to poll first */
864 fdtab[fd].ev &= ~FD_POLL_IN;
865 return 0;
866 }
867 /* network error, report it */
868 if (!(s->result & SRV_CHK_ERROR))
869 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
870 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871 }
872
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200873 if (len < sizeof(trash))
874 trash[len] = '\0';
875 else
876 trash[len-1] = '\0';
877
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100878 /* Note: the response will only be accepted if read at once */
879 if (s->proxy->options & PR_O_HTTP_CHK) {
880 /* Check if the server speaks HTTP 1.X */
881 if ((len < strlen("HTTP/1.0 000\r")) ||
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200882 (memcmp(trash, "HTTP/1.", 7) != 0 ||
883 (trash[12] != ' ' && trash[12] != '\r')) ||
884 !isdigit(trash[9]) || !isdigit(trash[10]) || !isdigit(trash[11])) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200885
886 cut_crlf(trash);
887 set_server_check_status(s, HCHK_STATUS_L7RSP, trash);
888
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100889 goto out_wakeup;
890 }
891
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200892 s->check_code = str2uic(&trash[9]);
893
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200894 desc = ltrim(&trash[12], ' ');
895 cut_crlf(desc);
896
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100897 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200898 if (trash[9] == '2' || trash[9] == '3')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200899 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200900 else if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau48494c02007-11-30 10:41:39 +0100901 (s->state & SRV_RUNNING) &&
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200902 (s->check_code == 404))
903 /* 404 may be accepted as "stopping" only if the server was up */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200904 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200905 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200906 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100907 }
908 else if (s->proxy->options & PR_O_SSL3_CHK) {
909 /* Check for SSLv3 alert or handshake */
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200910 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200911 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200912 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200913 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200914 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100915 else if (s->proxy->options & PR_O_SMTP_CHK) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200916 /* Check if the server speaks SMTP */
917 if ((len < strlen("000\r")) ||
918 (trash[3] != ' ' && trash[3] != '\r') ||
919 !isdigit(trash[0]) || !isdigit(trash[1]) || !isdigit(trash[2])) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200920
921 cut_crlf(trash);
922 set_server_check_status(s, HCHK_STATUS_L7RSP, trash);
923
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200924 goto out_wakeup;
925 }
926
927 s->check_code = str2uic(&trash[0]);
928
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200929 desc = ltrim(&trash[3], ' ');
930 cut_crlf(desc);
931
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100932 /* Check for SMTP code 2xx (should be 250) */
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200933 if (trash[0] == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200934 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200935 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200936 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau6996e152007-04-30 14:37:43 +0200937 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +0100938 else if (s->proxy->options2 & PR_O2_MYSQL_CHK) {
939 /* MySQL Error packet always begin with field_count = 0xff
940 * contrary to OK Packet who always begin whith 0x00 */
Willy Tarreau2a3a5172010-01-14 11:41:31 +0100941 if (trash[4] != '\xff') {
Hervé COMMOWICK698ae002010-01-12 09:25:13 +0100942 /* We set the MySQL Version in description for information purpose
943 * FIXME : it can be cool to use MySQL Version for other purpose,
944 * like mark as down old MySQL server.
945 */
946 if (len > 51) {
947 desc = ltrim(&trash[5], ' ');
948 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
949 }
950 else {
951 /* it seems we have a OK packet but without a valid length,
952 * it must be a protocol error
953 */
954 set_server_check_status(s, HCHK_STATUS_L7RSP, trash);
955 }
956 }
957 else {
958 /* An error message is attached in the Error packet,
959 * so we can display it ! :)
960 */
961 desc = ltrim(&trash[7], ' ');
962 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
963 }
964 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100965 else {
966 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200967 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau23677902007-05-08 23:50:35 +0200968 }
Willy Tarreau83749182007-04-15 20:56:27 +0200969
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100970 out_wakeup:
971 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 fdtab[fd].state = FD_STERROR;
973
Willy Tarreauf161a342007-04-08 16:59:42 +0200974 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200975 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100976 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200977 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200978}
979
980/*
981 * manages a server health-check. Returns
982 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
983 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100984struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985{
Willy Tarreaue3838802009-03-21 18:58:32 +0100986 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200987 struct server *s = t->context;
988 struct sockaddr_in sa;
989 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200990 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200991
992 //fprintf(stderr, "process_chk: task=%p\n", t);
993
994 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +0100995 if (attempts++ > 0) {
996 /* we always fail to create a server, let's stop insisting... */
997 while (tick_is_expired(t->expire, now_ms))
998 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
999 return t;
1000 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001 fd = s->curfd;
1002 if (fd < 0) { /* no check currently running */
1003 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +01001004 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1005 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006
1007 /* we don't send any health-checks when the proxy is stopped or when
1008 * the server should not be checked.
1009 */
1010 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001011 while (tick_is_expired(t->expire, now_ms))
1012 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001013 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 }
1015
1016 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001017 set_server_check_status(s, HCHK_STATUS_START, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
1019 if ((fd < global.maxsock) &&
1020 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1021 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1022 //fprintf(stderr, "process_chk: 3\n");
1023
Willy Tarreau9edd1612007-10-18 18:07:48 +02001024 if (s->proxy->options & PR_O_TCP_NOLING) {
1025 /* We don't want to useless data */
1026 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1027 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001028
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001029 if (s->check_addr.sin_addr.s_addr)
1030 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001031 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001032 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001033 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001034 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001035
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001037 sa.sin_port = htons(s->check_port);
1038
1039 /* allow specific binding :
1040 * - server-specific at first
1041 * - proxy-specific next
1042 */
1043 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001044 struct sockaddr_in *remote = NULL;
1045 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001046
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001047#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001048 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
1049 remote = (struct sockaddr_in *)&s->tproxy_addr;
1050 flags = 3;
1051 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001052#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001053#ifdef SO_BINDTODEVICE
1054 /* Note: this might fail if not CAP_NET_RAW */
1055 if (s->iface_name)
1056 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001057 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001058#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001059 if (s->sport_range) {
1060 int bind_attempts = 10; /* should be more than enough to find a spare port */
1061 struct sockaddr_in src;
1062
1063 ret = 1;
1064 src = s->source_addr;
1065
1066 do {
1067 /* note: in case of retry, we may have to release a previously
1068 * allocated port, hence this loop's construct.
1069 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001070 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1071 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001072
1073 if (!bind_attempts)
1074 break;
1075 bind_attempts--;
1076
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001077 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1078 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001079 break;
1080
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001081 fdinfo[fd].port_range = s->sport_range;
1082 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001083
1084 ret = tcpv4_bind_socket(fd, flags, &src, remote);
1085 } while (ret != 0); /* binding NOK */
1086 }
1087 else {
1088 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
1089 }
1090
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001091 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001092 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001093 switch (ret) {
1094 case 1:
1095 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1096 s->proxy->id, s->id);
1097 break;
1098 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001099 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1100 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001101 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001102 }
1103 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104 }
1105 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001106 struct sockaddr_in *remote = NULL;
1107 int ret, flags = 0;
1108
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001109#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001110 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001111 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
1112 flags = 3;
1113 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001114#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001115#ifdef SO_BINDTODEVICE
1116 /* Note: this might fail if not CAP_NET_RAW */
1117 if (s->proxy->iface_name)
1118 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001119 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001120#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001121 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
1122 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001123 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001124 switch (ret) {
1125 case 1:
1126 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1127 proxy_type_str(s->proxy), s->proxy->id);
1128 break;
1129 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001130 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1131 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001132 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001133 }
1134 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001135 }
1136
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001137 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001138#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001139 /* disabling tcp quick ack now allows
1140 * the request to leave the machine with
1141 * the first ACK.
1142 */
1143 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001144 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001145#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
1147 /* OK, connection in progress or established */
1148
1149 //fprintf(stderr, "process_chk: 4\n");
1150
1151 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +02001152 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +02001154 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
1155 fdtab[fd].cb[DIR_RD].b = NULL;
1156 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
1157 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001158 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
1159 fdinfo[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001161 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001162 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001163#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001164 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001166 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1167 /* we allow up to min(inter, timeout.connect) for a connection
1168 * to establish but only when timeout.check is set
1169 * as it may be to short for a full check otherwise
1170 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001171 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001172
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001173 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1174 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1175 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001176 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001177 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001178 }
1179 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001180 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001181
1182 switch (errno) {
1183 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1184 case ECONNREFUSED:
1185 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001186 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001187 break;
1188
1189 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001190 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001191 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001192 }
1193 }
1194 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001195 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1196 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197 close(fd); /* socket creation error */
1198 }
1199
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001200 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001201 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001202 while (tick_is_expired(t->expire, now_ms))
1203 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001204 goto new_chk; /* may be we should initialize a new check */
1205 }
1206
1207 /* here, we have seen a failure */
1208 if (s->health > s->rise) {
1209 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001210 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001211 }
1212 else
1213 set_server_down(s);
1214
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001215 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1216 /* we allow up to min(inter, timeout.connect) for a connection
1217 * to establish but only when timeout.check is set
1218 * as it may be to short for a full check otherwise
1219 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001220 while (tick_is_expired(t->expire, now_ms)) {
1221 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001222
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001223 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1224 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001225
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001226 if (s->proxy->timeout.check)
1227 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001228 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 goto new_chk;
1230 }
1231 else {
1232 //fprintf(stderr, "process_chk: 8\n");
1233 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001234 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001235 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001236
Willy Tarreau9909fc12007-11-30 17:42:05 +01001237 if (s->state & SRV_WARMINGUP) {
1238 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1239 s->state &= ~SRV_WARMINGUP;
1240 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1241 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1242 if (s->proxy->lbprm.update_server_eweight)
1243 s->proxy->lbprm.update_server_eweight(s);
1244 }
1245 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1246 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +01001247 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1248 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +01001249 s->eweight *= s->uweight;
1250 if (s->proxy->lbprm.update_server_eweight)
1251 s->proxy->lbprm.update_server_eweight(s);
1252 }
1253 /* probably that we can refill this server with a bit more connections */
1254 check_for_pending(s);
1255 }
1256
Willy Tarreau48494c02007-11-30 10:41:39 +01001257 /* we may have to add/remove this server from the LB group */
1258 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1259 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001260 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1261 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001262 else if (!(s->state & SRV_GOINGDOWN) &&
1263 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001264 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1265 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001266 }
1267
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001268 if (s->health < s->rise + s->fall - 1) {
1269 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001271 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001272 }
1273 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001274 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001275
1276 rv = 0;
1277 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001278 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001279 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001280 //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 +02001281 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001282 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001283 goto new_chk;
1284 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001285 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001286 if (!(s->result & SRV_CHK_ERROR)) {
1287 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001288 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001289 } else {
1290 if (s->proxy->options & PR_O_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001291 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001292 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001293 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001294 }
1295 }
1296
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297 //fprintf(stderr, "process_chk: 10\n");
1298 /* failure or timeout detected */
1299 if (s->health > s->rise) {
1300 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001301 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001302 }
1303 else
1304 set_server_down(s);
1305 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001307
1308 rv = 0;
1309 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001310 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001311 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001312 //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 +02001313 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001314 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 goto new_chk;
1316 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001317 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001318 }
1319 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001320 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001321 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001322}
1323
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001324/*
1325 * Start health-check.
1326 * Returns 0 if OK, -1 if error, and prints the error in this case.
1327 */
1328int start_checks() {
1329
1330 struct proxy *px;
1331 struct server *s;
1332 struct task *t;
1333 int nbchk=0, mininter=0, srvpos=0;
1334
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001335 /* 1- count the checkers to run simultaneously.
1336 * We also determine the minimum interval among all of those which
1337 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1338 * will be used to spread their start-up date. Those which have
1339 * a shorter interval will start independantly and will not dictate
1340 * too short an interval for all others.
1341 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001342 for (px = proxy; px; px = px->next) {
1343 for (s = px->srv; s; s = s->next) {
1344 if (!(s->state & SRV_CHECKED))
1345 continue;
1346
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001347 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1348 (!mininter || mininter > srv_getinter(s)))
1349 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001350
1351 nbchk++;
1352 }
1353 }
1354
1355 if (!nbchk)
1356 return 0;
1357
1358 srand((unsigned)time(NULL));
1359
1360 /*
1361 * 2- start them as far as possible from each others. For this, we will
1362 * start them after their interval set to the min interval divided by
1363 * the number of servers, weighted by the server's position in the list.
1364 */
1365 for (px = proxy; px; px = px->next) {
1366 for (s = px->srv; s; s = s->next) {
1367 if (!(s->state & SRV_CHECKED))
1368 continue;
1369
Willy Tarreaua4613182009-03-21 18:13:21 +01001370 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001371 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1372 return -1;
1373 }
1374
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001375 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001376 t->process = process_chk;
1377 t->context = s;
1378
1379 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001380 t->expire = tick_add(now_ms,
1381 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1382 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001383 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001384 task_queue(t);
1385
1386 srvpos++;
1387 }
1388 }
1389 return 0;
1390}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001391
1392/*
1393 * Local variables:
1394 * c-indent-level: 8
1395 * c-basic-offset: 8
1396 * End:
1397 */