blob: f0338d7cfc12e3925b1e5bc0883fda4fad72d686 [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>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090048#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010049#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/task.h>
51
Willy Tarreaubd741542010-03-16 18:46:54 +010052static int httpchk_expect(struct server *s, int done);
53
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020054const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
55 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
56 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
57 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020058
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010059 [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" },
60
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020061 [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020062
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020063 [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" },
64 [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" },
65 [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020066
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020067 [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" },
68 [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" },
69 [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020070
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020071 [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" },
72 [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020073
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020074 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020076 [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" },
77 [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
78 [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020079};
80
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010081const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
82 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
83
84 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
85 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
86
87 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
88 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
89 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
90 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
91
92 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
93 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
94 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
95};
96
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020097/*
98 * Convert check_status code to description
99 */
100const char *get_check_status_description(short check_status) {
101
102 const char *desc;
103
104 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200105 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200106 else
107 desc = NULL;
108
109 if (desc && *desc)
110 return desc;
111 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200112 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200113}
114
115/*
116 * Convert check_status code to short info
117 */
118const char *get_check_status_info(short check_status) {
119
120 const char *info;
121
122 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200123 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200124 else
125 info = NULL;
126
127 if (info && *info)
128 return info;
129 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200130 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200131}
132
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100133const char *get_analyze_status(short analyze_status) {
134
135 const char *desc;
136
137 if (analyze_status < HANA_STATUS_SIZE)
138 desc = analyze_statuses[analyze_status].desc;
139 else
140 desc = NULL;
141
142 if (desc && *desc)
143 return desc;
144 else
145 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
146}
147
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200148#define SSP_O_HCHK 0x0002
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200149
150static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
151
Krzysztof Piotr Oledzki5f5b7d22010-01-11 11:13:39 +0100152 if (s->tracked)
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200153 chunk_printf(msg, " via %s/%s",
154 s->tracked->proxy->id, s->tracked->id);
155
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200156 if (options & SSP_O_HCHK) {
157 chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status));
158
159 if (s->check_status >= HCHK_STATUS_L57DATA)
160 chunk_printf(msg, ", code: %d", s->check_code);
161
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200162 if (*s->check_desc) {
163 struct chunk src;
164
165 chunk_printf(msg, ", info: \"");
166
167 chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc));
168 chunk_asciiencode(msg, &src, '"');
169
170 chunk_printf(msg, "\"");
171 }
172
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100173 if (s->check_duration >= 0)
174 chunk_printf(msg, ", check duration: %ldms", s->check_duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200175 }
176
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200177 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100178 if (!(s->state & SRV_RUNNING))
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200179 chunk_printf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100180 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200181 s->proxy->srv_act, s->proxy->srv_bck,
182 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
183 s->cur_sess, xferred, s->nbpend);
184 else
185 chunk_printf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100186 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200187 s->proxy->srv_act, s->proxy->srv_bck,
188 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
189 xferred, s->nbpend);
190 }
191}
192
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200193/*
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200194 * Set s->check_status, update s->check_duration and fill s->result with
195 * an adequate SRV_CHK_* value.
196 *
197 * Show information in logs about failed health check if server is UP
198 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200199 */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200200static void set_server_check_status(struct server *s, short status, char *desc) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200201
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200202 struct chunk msg;
203
204 if (status == HCHK_STATUS_START) {
205 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200206 s->check_desc[0] = '\0';
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200207 s->check_start = now;
208 return;
209 }
210
211 if (!s->check_status)
212 return;
213
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200214 if (desc && *desc) {
215 strncpy(s->check_desc, desc, HCHK_DESC_LEN-1);
216 s->check_desc[HCHK_DESC_LEN-1] = '\0';
217 } else
218 s->check_desc[0] = '\0';
219
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200220 s->check_status = status;
221 if (check_statuses[status].result)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100222 s->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200223
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100224 if (status == HCHK_STATUS_HANA)
225 s->check_duration = -1;
226 else if (!tv_iszero(&s->check_start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200227 /* set_server_check_status() may be called more than once */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200228 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
229 tv_zero(&s->check_start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200230 }
231
232 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
233 (((s->health != 0) && (s->result & SRV_CHK_ERROR)) ||
234 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) ||
235 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
236 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
237
238 int health, rise, fall, state;
239
240 chunk_init(&msg, trash, sizeof(trash));
241
242 /* FIXME begin: calculate local version of the health/rise/fall/state */
243 health = s->health;
244 rise = s->rise;
245 fall = s->fall;
246 state = s->state;
247
248 if (s->result & SRV_CHK_ERROR) {
249 if (health > rise) {
250 health--; /* still good */
251 } else {
252 if (health == rise)
253 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
254
255 health = 0;
256 }
257 }
258
259 if (s->result & SRV_CHK_RUNNING) {
260 if (health < rise + fall - 1) {
261 health++; /* was bad, stays for a while */
262
263 if (health == rise)
264 state |= SRV_RUNNING;
265
266 if (health >= rise)
267 health = rise + fall - 1; /* OK now */
268 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100269
270 /* clear consecutive_errors if observing is enabled */
271 if (s->onerror)
272 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200273 }
274 /* FIXME end: calculate local version of the health/rise/fall/state */
275
276 chunk_printf(&msg,
277 "Health check for %sserver %s/%s %s%s",
278 s->state & SRV_BACKUP ? "backup " : "",
279 s->proxy->id, s->id,
280 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
281 (s->result & SRV_CHK_RUNNING)?"succeeded":"failed");
282
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200283 server_status_printf(&msg, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200284
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100285 chunk_printf(&msg, ", status: %d/%d %s",
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200286 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
287 (state & SRV_RUNNING) ? (fall) : (rise),
288 (state & SRV_RUNNING)?"UP":"DOWN");
289
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100290 Warning("%s.\n", trash);
291 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200292 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200293}
294
Willy Tarreau48494c02007-11-30 10:41:39 +0100295/* sends a log message when a backend goes down, and also sets last
296 * change date.
297 */
298static void set_backend_down(struct proxy *be)
299{
300 be->last_change = now.tv_sec;
301 be->down_trans++;
302
303 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
304 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
305}
306
307/* Redistribute pending connections when a server goes down. The number of
308 * connections redistributed is returned.
309 */
310static int redistribute_pending(struct server *s)
311{
312 struct pendconn *pc, *pc_bck, *pc_end;
313 int xferred = 0;
314
315 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
316 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100317 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
318 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100319 /* The REDISP option was specified. We will ignore
320 * cookie and force to balance or use the dispatcher.
321 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100322
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100323 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100324 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100325
Willy Tarreau48494c02007-11-30 10:41:39 +0100326 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200327 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100328 xferred++;
329 }
330 }
331 return xferred;
332}
333
334/* Check for pending connections at the backend, and assign some of them to
335 * the server coming up. The server's weight is checked before being assigned
336 * connections it may not be able to handle. The total number of transferred
337 * connections is returned.
338 */
339static int check_for_pending(struct server *s)
340{
341 int xferred;
342
343 if (!s->eweight)
344 return 0;
345
346 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
347 struct session *sess;
348 struct pendconn *p;
349
350 p = pendconn_from_px(s->proxy);
351 if (!p)
352 break;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100353 set_target_server(&p->sess->target, s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100354 sess = p->sess;
355 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200356 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100357 }
358 return xferred;
359}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900361/* Shutdown connections when their server goes down.
362 */
363static void shutdown_sessions(struct server *srv)
364{
365 struct session *session, *session_bck;
366
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200367 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
368 if (session->srv_conn == srv)
369 session_shutdown(session, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900370}
371
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372/* Sets server <s> down, notifies by all available means, recounts the
373 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200374 * possible to other servers. It automatically recomputes the number of
375 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100377void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100379 struct server *srv;
380 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381 int xferred;
382
Cyril Bontécd19e512010-01-31 22:34:03 +0100383 if (s->state & SRV_MAINTAIN) {
384 s->health = s->rise;
385 }
386
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100387 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100388 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200389 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200390
391 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100392 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100393 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900395 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
396 shutdown_sessions(s);
397
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398 /* we might have sessions queued on this server and waiting for
399 * a connection. Those which are redispatchable will be queued
400 * to another server or to the proxy itself.
401 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100402 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100403
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200404 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100405
Cyril Bontécd19e512010-01-31 22:34:03 +0100406 if (s->state & SRV_MAINTAIN) {
407 chunk_printf(&msg,
408 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
409 s->proxy->id, s->id);
410 } else {
411 chunk_printf(&msg,
412 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
413 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100414
Cyril Bontécd19e512010-01-31 22:34:03 +0100415 server_status_printf(&msg, s,
416 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
417 xferred);
418 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100419 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200420
Willy Tarreau48494c02007-11-30 10:41:39 +0100421 /* we don't send an alert if the server was previously paused */
422 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100423 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100424 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100425 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200426
Willy Tarreaud64d2252010-10-17 17:16:42 +0200427 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100428 set_backend_down(s->proxy);
429
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200430 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100431
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100432 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100433 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100434 if (! (srv->state & SRV_MAINTAIN))
435 /* Only notify tracking servers that are not already in maintenance. */
436 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100438
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 s->health = 0; /* failure */
440}
441
Cyril Bontécd19e512010-01-31 22:34:03 +0100442void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100443
444 struct server *srv;
445 struct chunk msg;
446 int xferred;
447
Cyril Bontécd19e512010-01-31 22:34:03 +0100448 if (s->state & SRV_MAINTAIN) {
449 s->health = s->rise;
450 }
451
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100452 if (s->health == s->rise || s->tracked) {
453 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
454 if (s->proxy->last_change < now.tv_sec) // ignore negative times
455 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
456 s->proxy->last_change = now.tv_sec;
457 }
458
459 if (s->last_change < now.tv_sec) // ignore negative times
460 s->down_time += now.tv_sec - s->last_change;
461
462 s->last_change = now.tv_sec;
463 s->state |= SRV_RUNNING;
464
465 if (s->slowstart > 0) {
466 s->state |= SRV_WARMINGUP;
467 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
468 /* For dynamic algorithms, start at the first step of the weight,
469 * without multiplying by BE_WEIGHT_SCALE.
470 */
471 s->eweight = s->uweight;
472 if (s->proxy->lbprm.update_server_eweight)
473 s->proxy->lbprm.update_server_eweight(s);
474 }
475 }
476 s->proxy->lbprm.set_server_status_up(s);
477
478 /* check if we can handle some connections queued at the proxy. We
479 * will take as many as we can handle.
480 */
481 xferred = check_for_pending(s);
482
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200483 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100484
Cyril Bontécd19e512010-01-31 22:34:03 +0100485 if (s->state & SRV_MAINTAIN) {
486 chunk_printf(&msg,
487 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
488 s->proxy->id, s->id);
489 } else {
490 chunk_printf(&msg,
491 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
492 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100493
Cyril Bontécd19e512010-01-31 22:34:03 +0100494 server_status_printf(&msg, s,
495 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
496 xferred);
497 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100498
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100499 Warning("%s.\n", trash);
500 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100501
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100502 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100503 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100504 if (! (srv->state & SRV_MAINTAIN))
505 /* Only notify tracking servers if they're not in maintenance. */
506 set_server_up(srv);
507
508 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100509 }
510
511 if (s->health >= s->rise)
512 s->health = s->rise + s->fall - 1; /* OK now */
513
514}
515
516static void set_server_disabled(struct server *s) {
517
518 struct server *srv;
519 struct chunk msg;
520 int xferred;
521
522 s->state |= SRV_GOINGDOWN;
523 s->proxy->lbprm.set_server_status_down(s);
524
525 /* we might have sessions queued on this server and waiting for
526 * a connection. Those which are redispatchable will be queued
527 * to another server or to the proxy itself.
528 */
529 xferred = redistribute_pending(s);
530
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200531 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100532
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200533 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100534 "Load-balancing on %sServer %s/%s is disabled",
535 s->state & SRV_BACKUP ? "Backup " : "",
536 s->proxy->id, s->id);
537
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200538 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200539 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100540 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100541
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100542 Warning("%s.\n", trash);
543 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100544
545 if (!s->proxy->srv_bck && !s->proxy->srv_act)
546 set_backend_down(s->proxy);
547
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100548 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100549 for(srv = s->tracknext; srv; srv = srv->tracknext)
550 set_server_disabled(srv);
551}
552
553static void set_server_enabled(struct server *s) {
554
555 struct server *srv;
556 struct chunk msg;
557 int xferred;
558
559 s->state &= ~SRV_GOINGDOWN;
560 s->proxy->lbprm.set_server_status_up(s);
561
562 /* check if we can handle some connections queued at the proxy. We
563 * will take as many as we can handle.
564 */
565 xferred = check_for_pending(s);
566
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200567 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100568
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200569 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100570 "Load-balancing on %sServer %s/%s is enabled again",
571 s->state & SRV_BACKUP ? "Backup " : "",
572 s->proxy->id, s->id);
573
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200574 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200575 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100576 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100577
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100578 Warning("%s.\n", trash);
579 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100580
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100581 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100582 for(srv = s->tracknext; srv; srv = srv->tracknext)
583 set_server_enabled(srv);
584}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100586void health_adjust(struct server *s, short status) {
587
588 int failed;
589 int expire;
590
591 /* return now if observing nor health check is not enabled */
592 if (!s->observe || !s->check)
593 return;
594
595 if (s->observe >= HANA_OBS_SIZE)
596 return;
597
598 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
599 return;
600
601 switch (analyze_statuses[status].lr[s->observe - 1]) {
602 case 1:
603 failed = 1;
604 break;
605
606 case 2:
607 failed = 0;
608 break;
609
610 default:
611 return;
612 }
613
614 if (!failed) {
615 /* good: clear consecutive_errors */
616 s->consecutive_errors = 0;
617 return;
618 }
619
620 s->consecutive_errors++;
621
622 if (s->consecutive_errors < s->consecutive_errors_limit)
623 return;
624
625 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
626 s->consecutive_errors, get_analyze_status(status));
627
628 switch (s->onerror) {
629 case HANA_ONERR_FASTINTER:
630 /* force fastinter - nothing to do here as all modes force it */
631 break;
632
633 case HANA_ONERR_SUDDTH:
634 /* simulate a pre-fatal failed health check */
635 if (s->health > s->rise)
636 s->health = s->rise + 1;
637
638 /* no break - fall through */
639
640 case HANA_ONERR_FAILCHK:
641 /* simulate a failed health check */
642 set_server_check_status(s, HCHK_STATUS_HANA, trash);
643
644 if (s->health > s->rise) {
645 s->health--; /* still good */
646 s->counters.failed_checks++;
647 }
648 else
649 set_server_down(s);
650
651 break;
652
653 case HANA_ONERR_MARKDWN:
654 /* mark server down */
655 s->health = s->rise;
656 set_server_check_status(s, HCHK_STATUS_HANA, trash);
657 set_server_down(s);
658
659 break;
660
661 default:
662 /* write a warning? */
663 break;
664 }
665
666 s->consecutive_errors = 0;
667 s->counters.failed_hana++;
668
669 if (s->fastinter) {
670 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
671 if (s->check->expire > expire)
672 s->check->expire = expire;
673 }
674}
675
Willy Tarreauef781042010-01-27 11:53:01 +0100676static int httpchk_build_status_header(struct server *s, char *buffer)
677{
678 int sv_state;
679 int ratio;
680 int hlen = 0;
681 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
682 "UP %d/%d", "UP",
683 "NOLB %d/%d", "NOLB",
684 "no check" };
685
686 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
687 hlen += 24;
688
689 if (!(s->state & SRV_CHECKED))
690 sv_state = 6; /* should obviously never happen */
691 else if (s->state & SRV_RUNNING) {
692 if (s->health == s->rise + s->fall - 1)
693 sv_state = 3; /* UP */
694 else
695 sv_state = 2; /* going down */
696
697 if (s->state & SRV_GOINGDOWN)
698 sv_state += 2;
699 } else {
700 if (s->health)
701 sv_state = 1; /* going up */
702 else
703 sv_state = 0; /* DOWN */
704 }
705
706 hlen += sprintf(buffer + hlen,
707 srv_hlt_st[sv_state],
708 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
709 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
710
711 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
712 s->proxy->id, s->id,
713 global.node,
714 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
715 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
716 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
717 s->nbpend);
718
719 if ((s->state & SRV_WARMINGUP) &&
720 now.tv_sec < s->last_change + s->slowstart &&
721 now.tv_sec >= s->last_change) {
722 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
723 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
724 }
725
726 buffer[hlen++] = '\r';
727 buffer[hlen++] = '\n';
728
729 return hlen;
730}
731
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732/*
733 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200734 * the connection acknowledgement. If the proxy requires L7 health-checks,
735 * it sends the request. In other cases, it calls set_server_check_status()
736 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200737 * The function itself returns 0 if it needs some polling before being called
738 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739 */
Willy Tarreau83749182007-04-15 20:56:27 +0200740static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741{
Willy Tarreau6996e152007-04-30 14:37:43 +0200742 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 struct task *t = fdtab[fd].owner;
744 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100746 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200747 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100748 int skerr, err = errno;
749 socklen_t lskerr = sizeof(skerr);
750
751 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
752 err = skerr;
753
754 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200755 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200756 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200757
758 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200759
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100760 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200762 if (s->proxy->options2 & PR_O2_CHK_ANY) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100764 const char *check_req = s->proxy->check_req;
765 int check_len = s->proxy->check_len;
766
Willy Tarreauf3c69202006-07-09 16:42:34 +0200767 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 * so we'll send the request, and won't wake the checker up now.
769 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200770
Willy Tarreau1620ec32011-08-06 17:05:02 +0200771 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200772 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200773 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200774 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
775 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200776 else if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK) {
Willy Tarreaue9d87882010-01-27 11:28:42 +0100777 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100778
779 if (s->proxy->options2 & PR_O2_CHK_SNDST)
780 check_len += httpchk_build_status_header(s, trash + check_len);
781
Willy Tarreaue9d87882010-01-27 11:28:42 +0100782 trash[check_len++] = '\r';
783 trash[check_len++] = '\n';
784 trash[check_len] = '\0';
785 check_req = trash;
786 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200787
Willy Tarreaue9d87882010-01-27 11:28:42 +0100788 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
789 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100790 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100791 if (s->proxy->timeout.check)
792 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200793 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200794 goto out_nowake;
795 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200796 else if (ret == 0 || errno == EAGAIN)
797 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200798 else {
799 switch (errno) {
800 case ECONNREFUSED:
801 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200802 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200803 break;
804
805 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200806 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200807 }
808
Willy Tarreau6996e152007-04-30 14:37:43 +0200809 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200810 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 }
812 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200813 /* We have no data to send to check the connection, and
814 * getsockopt() will not inform us whether the connection
815 * is still pending. So we'll reuse connect() to check the
816 * state of the socket. This has the advantage of givig us
817 * the following info :
818 * - error
819 * - connecting (EALREADY, EINPROGRESS)
820 * - connected (EISCONN, 0)
821 */
822
David du Colombier6f5ccb12011-03-10 22:26:24 +0100823 struct sockaddr_storage sa;
Willy Tarreau6996e152007-04-30 14:37:43 +0200824
David du Colombier6f5ccb12011-03-10 22:26:24 +0100825 if (is_addr(&s->check_addr))
826 sa = s->check_addr;
827 else
828 sa = s->addr;
829
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200830 set_host_port(&sa, s->check_port);
Willy Tarreau6996e152007-04-30 14:37:43 +0200831
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200832 if (connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) == 0)
Willy Tarreau6996e152007-04-30 14:37:43 +0200833 errno = 0;
834
835 if (errno == EALREADY || errno == EINPROGRESS)
836 goto out_poll;
837
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200838 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200839 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200840 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200841 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200842
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200844 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200845 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846 }
847 }
Willy Tarreau83749182007-04-15 20:56:27 +0200848 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200849 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200850 out_nowake:
851 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100852 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200853 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200854 out_poll:
855 /* The connection is still pending. We'll have to poll it
856 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100857 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200858 return 0;
859 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200860 fdtab[fd].state = FD_STERROR;
861 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862}
863
864
865/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200866 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200867 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
868 * set_server_check_status() to update s->check_status, s->check_duration
869 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200870
871 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
872 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
873 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
874 * response to an SSL HELLO (the principle is that this is enough to
875 * distinguish between an SSL server and a pure TCP relay). All other cases will
876 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
877 * etc.
878 *
879 * The function returns 0 if it needs to be called again after some polling,
880 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881 */
Willy Tarreau83749182007-04-15 20:56:27 +0200882static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883{
Willy Tarreau83749182007-04-15 20:56:27 +0200884 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100885 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200886 struct task *t = fdtab[fd].owner;
887 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200888 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100889 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200890 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200891
Willy Tarreau659d7bc2010-03-16 21:14:41 +0100892 if (unlikely((s->result & SRV_CHK_ERROR) || (fdtab[fd].state == FD_STERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200893 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200894 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200895 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200896
Willy Tarreau83749182007-04-15 20:56:27 +0200897 goto out_wakeup;
898 }
899
Willy Tarreau83749182007-04-15 20:56:27 +0200900 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
901 * but the connection was closed on the remote end. Fortunately, recv still
902 * works correctly and we don't need to do the getsockopt() on linux.
903 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000904
905 /* Set buffer to point to the end of the data already read, and check
906 * that there is free space remaining. If the buffer is full, proceed
907 * with running the checks without attempting another socket read.
908 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000909
Willy Tarreau03938182010-03-17 21:52:07 +0100910 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200911 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
912 len = recv(fd, s->check_data + s->check_data_len, global.tune.chksize - s->check_data_len, 0);
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100913 if (len <= 0)
914 break;
915 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000916
Willy Tarreau03938182010-03-17 21:52:07 +0100917 if (len == 0)
918 done = 1; /* connection hangup received */
919 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100920 /* Report network errors only if we got no other data. Otherwise
921 * we'll let the upper layers decide whether the response is OK
922 * or not. It is very common that an RST sent by the server is
923 * reported as an error just after the last data chunk.
924 */
Willy Tarreau03938182010-03-17 21:52:07 +0100925 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100926 if (!s->check_data_len) {
927 if (!(s->result & SRV_CHK_ERROR))
928 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
929 goto out_wakeup;
930 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 }
932
Willy Tarreau03938182010-03-17 21:52:07 +0100933 /* Intermediate or complete response received.
934 * Terminate string in check_data buffer.
935 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200936 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100937 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100938 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100939 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100940 done = 1; /* buffer full, don't wait for more data */
941 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200942
Nick Chalk57b1bf72010-03-16 15:50:46 +0000943 /* Run the checks... */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200944 switch (s->proxy->options2 & PR_O2_CHK_ANY) {
945 case PR_O2_HTTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100946 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
947 goto wait_more_data;
948
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100949 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000950 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
951 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
952 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
953 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
954 !isdigit((unsigned char) *(s->check_data + 11))) {
955 cut_crlf(s->check_data);
956 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200957
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100958 goto out_wakeup;
959 }
960
Nick Chalk57b1bf72010-03-16 15:50:46 +0000961 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000962 desc = ltrim(s->check_data + 12, ' ');
963
Willy Tarreaubd741542010-03-16 18:46:54 +0100964 if ((s->proxy->options & PR_O_DISABLE404) &&
965 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000966 /* 404 may be accepted as "stopping" only if the server was up */
967 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200968 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000969 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100970 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
971 /* Run content verification check... We know we have at least 13 chars */
972 if (!httpchk_expect(s, done))
973 goto wait_more_data;
974 }
975 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
976 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
977 cut_crlf(desc);
978 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
979 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000980 else {
981 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200982 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000983 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200984 break;
985
986 case PR_O2_SSL3_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100987 if (!done && s->check_data_len < 5)
988 goto wait_more_data;
989
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100990 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000991 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200992 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200993 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200994 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200995 break;
996
997 case PR_O2_SMTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100998 if (!done && s->check_data_len < strlen("000\r"))
999 goto wait_more_data;
1000
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001001 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001002 if ((s->check_data_len < strlen("000\r")) ||
1003 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
1004 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
1005 !isdigit((unsigned char) *(s->check_data + 2))) {
1006 cut_crlf(s->check_data);
1007 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001008
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001009 goto out_wakeup;
1010 }
1011
Nick Chalk57b1bf72010-03-16 15:50:46 +00001012 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001013
Nick Chalk57b1bf72010-03-16 15:50:46 +00001014 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001015 cut_crlf(desc);
1016
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001017 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001018 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001019 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001020 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001021 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001022 break;
1023
1024 case PR_O2_PGSQL_CHK:
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001025 if (!done && s->check_data_len < 9)
1026 goto wait_more_data;
1027
1028 if (s->check_data[0] == 'R') {
1029 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1030 }
1031 else {
1032 if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0))
1033 desc = &s->check_data[6];
1034 else
1035 desc = "PostgreSQL unknown error";
1036
1037 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1038 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001039 break;
1040
1041 case PR_O2_REDIS_CHK:
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001042 if (!done && s->check_data_len < 7)
1043 goto wait_more_data;
1044
1045 if (strcmp(s->check_data, "+PONG\r\n") == 0) {
1046 set_server_check_status(s, HCHK_STATUS_L7OKD, "Redis server is ok");
1047 }
1048 else {
1049 set_server_check_status(s, HCHK_STATUS_L7STS, s->check_data);
1050 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001051 break;
1052
1053 case PR_O2_MYSQL_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001054 if (!done && s->check_data_len < 5)
1055 goto wait_more_data;
1056
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001057 if (s->proxy->check_len == 0) { // old mode
1058 if (*(s->check_data + 4) != '\xff') {
1059 /* We set the MySQL Version in description for information purpose
1060 * FIXME : it can be cool to use MySQL Version for other purpose,
1061 * like mark as down old MySQL server.
1062 */
1063 if (s->check_data_len > 51) {
1064 desc = ltrim(s->check_data + 5, ' ');
1065 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1066 }
1067 else {
1068 if (!done)
1069 goto wait_more_data;
1070 /* it seems we have a OK packet but without a valid length,
1071 * it must be a protocol error
1072 */
1073 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1074 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001075 }
1076 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001077 /* An error message is attached in the Error packet */
1078 desc = ltrim(s->check_data + 7, ' ');
1079 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1080 }
1081 } else {
1082 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1083 (((unsigned int) *(s->check_data + 1)) << 8) +
1084 (((unsigned int) *(s->check_data + 2)) << 16);
1085
1086 if (s->check_data_len == first_packet_len + 4) {
1087 /* MySQL Error packet always begin with field_count = 0xff */
1088 if (*(s->check_data + 4) != '\xff') {
1089 /* We have only one MySQL packet and it is a Handshake Initialization packet
1090 * but we need to have a second packet to know if it is alright
1091 */
1092 if (!done && s->check_data_len < first_packet_len + 5)
1093 goto wait_more_data;
1094 }
1095 else {
1096 /* We have only one packet and it is an Error packet,
1097 * an error message is attached, so we can display it
1098 */
1099 desc = &s->check_data[7];
1100 //Warning("onlyoneERR: %s\n", desc);
1101 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1102 }
1103 } else if (s->check_data_len > first_packet_len + 4) {
1104 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1105 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1106 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1107
1108 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1109 /* We have 2 packets and that's good */
1110 /* Check if the second packet is a MySQL Error packet or not */
1111 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1112 /* No error packet */
1113 /* We set the MySQL Version in description for information purpose */
1114 desc = &s->check_data[5];
1115 //Warning("2packetOK: %s\n", desc);
1116 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1117 }
1118 else {
1119 /* An error message is attached in the Error packet
1120 * so we can display it ! :)
1121 */
1122 desc = &s->check_data[first_packet_len+11];
1123 //Warning("2packetERR: %s\n", desc);
1124 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1125 }
1126 }
1127 }
1128 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001129 if (!done)
1130 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001131 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001132 * it must be a protocol error
1133 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001134 desc = &s->check_data[5];
1135 //Warning("protoerr: %s\n", desc);
1136 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001137 }
1138 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001139 break;
1140
1141 case PR_O2_LDAP_CHK:
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001142 if (!done && s->check_data_len < 14)
1143 goto wait_more_data;
1144
1145 /* Check if the server speaks LDAP (ASN.1/BER)
1146 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1147 * http://tools.ietf.org/html/rfc4511
1148 */
1149
1150 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1151 * LDAPMessage: 0x30: SEQUENCE
1152 */
1153 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1154 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1155 }
1156 else {
1157 /* size of LDAPMessage */
1158 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1159
1160 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1161 * messageID: 0x02 0x01 0x01: INTEGER 1
1162 * protocolOp: 0x61: bindResponse
1163 */
1164 if ((msglen > 2) ||
1165 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1166 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1167
1168 goto out_wakeup;
1169 }
1170
1171 /* size of bindResponse */
1172 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1173
1174 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1175 * ldapResult: 0x0a 0x01: ENUMERATION
1176 */
1177 if ((msglen > 4) ||
1178 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1179 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1180
1181 goto out_wakeup;
1182 }
1183
1184 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1185 * resultCode
1186 */
1187 s->check_code = *(s->check_data + msglen + 9);
1188 if (s->check_code) {
1189 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1190 } else {
1191 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1192 }
1193 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001194 break;
1195
1196 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001197 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001198 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001199 break;
1200 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001201
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001202 out_wakeup:
1203 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001204 fdtab[fd].state = FD_STERROR;
1205
Nick Chalk57b1bf72010-03-16 15:50:46 +00001206 /* Reset the check buffer... */
1207 *s->check_data = '\0';
1208 s->check_data_len = 0;
1209
Willy Tarreau03938182010-03-17 21:52:07 +01001210 /* Close the connection... */
1211 shutdown(fd, SHUT_RDWR);
Willy Tarreauf161a342007-04-08 16:59:42 +02001212 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001213 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +01001214 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +02001215 return 1;
Willy Tarreau03938182010-03-17 21:52:07 +01001216
1217 wait_more_data:
1218 fdtab[fd].ev &= ~FD_POLL_IN;
1219 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001220}
1221
1222/*
1223 * manages a server health-check. Returns
1224 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1225 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001226struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227{
Willy Tarreaue3838802009-03-21 18:58:32 +01001228 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 struct server *s = t->context;
David du Colombier6f5ccb12011-03-10 22:26:24 +01001230 struct sockaddr_storage sa;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001231 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001232 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233
1234 //fprintf(stderr, "process_chk: task=%p\n", t);
1235
1236 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001237 if (attempts++ > 0) {
1238 /* we always fail to create a server, let's stop insisting... */
1239 while (tick_is_expired(t->expire, now_ms))
1240 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1241 return t;
1242 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001243 fd = s->curfd;
1244 if (fd < 0) { /* no check currently running */
1245 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +01001246 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1247 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001248
1249 /* we don't send any health-checks when the proxy is stopped or when
1250 * the server should not be checked.
1251 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001252 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001253 while (tick_is_expired(t->expire, now_ms))
1254 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001255 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001256 }
1257
1258 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001259 set_server_check_status(s, HCHK_STATUS_START, NULL);
David du Colombier6f5ccb12011-03-10 22:26:24 +01001260 if ((fd = socket(s->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001261 if ((fd < global.maxsock) &&
1262 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1263 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1264 //fprintf(stderr, "process_chk: 3\n");
1265
Willy Tarreau9edd1612007-10-18 18:07:48 +02001266 if (s->proxy->options & PR_O_TCP_NOLING) {
1267 /* We don't want to useless data */
1268 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1269 }
David du Colombier6f5ccb12011-03-10 22:26:24 +01001270
1271 if (is_addr(&s->check_addr))
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001272 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001273 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001274 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001275 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001276 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001277
Willy Tarreau48da04a2011-08-27 11:51:36 +02001278 set_host_port(&sa, s->check_port);
1279
Willy Tarreaubaaee002006-06-26 02:48:02 +02001280 /* allow specific binding :
1281 * - server-specific at first
1282 * - proxy-specific next
1283 */
1284 if (s->state & SRV_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001285 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001286 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001287
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001288#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001289 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001290 remote = &s->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001291 flags = 3;
1292 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001293#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001294#ifdef SO_BINDTODEVICE
1295 /* Note: this might fail if not CAP_NET_RAW */
1296 if (s->iface_name)
1297 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001298 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001299#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001300 if (s->sport_range) {
1301 int bind_attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +01001302 struct sockaddr_storage src;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001303
1304 ret = 1;
1305 src = s->source_addr;
1306
1307 do {
1308 /* note: in case of retry, we may have to release a previously
1309 * allocated port, hence this loop's construct.
1310 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001311 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1312 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001313
1314 if (!bind_attempts)
1315 break;
1316 bind_attempts--;
1317
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001318 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1319 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001320 break;
1321
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001322 fdinfo[fd].port_range = s->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001323 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001324
David du Colombier6f5ccb12011-03-10 22:26:24 +01001325 ret = tcp_bind_socket(fd, flags, &src, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001326 } while (ret != 0); /* binding NOK */
1327 }
1328 else {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001329 ret = tcp_bind_socket(fd, flags, &s->source_addr, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001330 }
1331
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001332 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001333 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001334 switch (ret) {
1335 case 1:
1336 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1337 s->proxy->id, s->id);
1338 break;
1339 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001340 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1341 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001342 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001343 }
1344 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001345 }
1346 else if (s->proxy->options & PR_O_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001347 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001348 int ret, flags = 0;
1349
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001350#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001351 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001352 remote = &s->proxy->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001353 flags = 3;
1354 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001355#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001356#ifdef SO_BINDTODEVICE
1357 /* Note: this might fail if not CAP_NET_RAW */
1358 if (s->proxy->iface_name)
1359 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001360 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001361#endif
David du Colombier6f5ccb12011-03-10 22:26:24 +01001362 ret = tcp_bind_socket(fd, flags, &s->proxy->source_addr, remote);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001363 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001364 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001365 switch (ret) {
1366 case 1:
1367 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1368 proxy_type_str(s->proxy), s->proxy->id);
1369 break;
1370 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001371 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1372 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001373 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001374 }
1375 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001376 }
1377
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001378 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001379#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001380 /* disabling tcp quick ack now allows
1381 * the request to leave the machine with
1382 * the first ACK.
1383 */
1384 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001385 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001386#endif
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +02001387 if ((connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) != -1) || (errno == EINPROGRESS)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001388 /* OK, connection in progress or established */
1389
1390 //fprintf(stderr, "process_chk: 4\n");
1391
1392 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +02001393 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +02001395 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
1396 fdtab[fd].cb[DIR_RD].b = NULL;
1397 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
1398 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001399 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +02001400 fdinfo[fd].peerlen = get_addr_len(&sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001402 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001403 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001405 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001406#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001407 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1408 /* we allow up to min(inter, timeout.connect) for a connection
1409 * to establish but only when timeout.check is set
1410 * as it may be to short for a full check otherwise
1411 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001412 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001413
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001414 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1415 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1416 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001417 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001418 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419 }
1420 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001421 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001422
1423 switch (errno) {
1424 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1425 case ECONNREFUSED:
1426 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001427 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001428 break;
1429
1430 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001431 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001432 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001433 }
1434 }
1435 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001436 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1437 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001438 close(fd); /* socket creation error */
1439 }
Willy Tarreaud11ad782011-03-09 20:38:33 +01001440 else
1441 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001442
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001443 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001444 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001445 while (tick_is_expired(t->expire, now_ms))
1446 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001447 goto new_chk; /* may be we should initialize a new check */
1448 }
1449
1450 /* here, we have seen a failure */
1451 if (s->health > s->rise) {
1452 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001453 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001454 }
1455 else
1456 set_server_down(s);
1457
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001458 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1459 /* we allow up to min(inter, timeout.connect) for a connection
1460 * to establish but only when timeout.check is set
1461 * as it may be to short for a full check otherwise
1462 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001463 while (tick_is_expired(t->expire, now_ms)) {
1464 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001465
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001466 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1467 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001468
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001469 if (s->proxy->timeout.check)
1470 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001471 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001472 goto new_chk;
1473 }
1474 else {
1475 //fprintf(stderr, "process_chk: 8\n");
1476 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001477 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001478 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001479
Willy Tarreau9909fc12007-11-30 17:42:05 +01001480 if (s->state & SRV_WARMINGUP) {
1481 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1482 s->state &= ~SRV_WARMINGUP;
1483 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1484 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1485 if (s->proxy->lbprm.update_server_eweight)
1486 s->proxy->lbprm.update_server_eweight(s);
1487 }
1488 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1489 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +01001490 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1491 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +01001492 s->eweight *= s->uweight;
1493 if (s->proxy->lbprm.update_server_eweight)
1494 s->proxy->lbprm.update_server_eweight(s);
1495 }
1496 /* probably that we can refill this server with a bit more connections */
1497 check_for_pending(s);
1498 }
1499
Willy Tarreau48494c02007-11-30 10:41:39 +01001500 /* we may have to add/remove this server from the LB group */
1501 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1502 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001503 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1504 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001505 else if (!(s->state & SRV_GOINGDOWN) &&
1506 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001507 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1508 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001509 }
1510
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001511 if (s->health < s->rise + s->fall - 1) {
1512 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001513
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001514 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001515 }
1516 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001517 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001518
1519 rv = 0;
1520 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001521 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001522 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001523 //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 +02001524 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001525 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001526 goto new_chk;
1527 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001528 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001529 if (!(s->result & SRV_CHK_ERROR)) {
1530 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001531 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001532 } else {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001533 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001534 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001535 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001536 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001537 }
1538 }
1539
Willy Tarreaubaaee002006-06-26 02:48:02 +02001540 //fprintf(stderr, "process_chk: 10\n");
1541 /* failure or timeout detected */
1542 if (s->health > s->rise) {
1543 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001544 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001545 }
1546 else
1547 set_server_down(s);
1548 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001549 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001550
1551 rv = 0;
1552 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001553 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001554 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001555 //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 +02001556 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001557 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001558 goto new_chk;
1559 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001560 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001561 }
1562 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001563 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001564 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001565}
1566
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001567/*
1568 * Start health-check.
1569 * Returns 0 if OK, -1 if error, and prints the error in this case.
1570 */
1571int start_checks() {
1572
1573 struct proxy *px;
1574 struct server *s;
1575 struct task *t;
1576 int nbchk=0, mininter=0, srvpos=0;
1577
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001578 /* 1- count the checkers to run simultaneously.
1579 * We also determine the minimum interval among all of those which
1580 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1581 * will be used to spread their start-up date. Those which have
1582 * a shorter interval will start independantly and will not dictate
1583 * too short an interval for all others.
1584 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001585 for (px = proxy; px; px = px->next) {
1586 for (s = px->srv; s; s = s->next) {
1587 if (!(s->state & SRV_CHECKED))
1588 continue;
1589
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001590 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1591 (!mininter || mininter > srv_getinter(s)))
1592 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001593
1594 nbchk++;
1595 }
1596 }
1597
1598 if (!nbchk)
1599 return 0;
1600
1601 srand((unsigned)time(NULL));
1602
1603 /*
1604 * 2- start them as far as possible from each others. For this, we will
1605 * start them after their interval set to the min interval divided by
1606 * the number of servers, weighted by the server's position in the list.
1607 */
1608 for (px = proxy; px; px = px->next) {
1609 for (s = px->srv; s; s = s->next) {
1610 if (!(s->state & SRV_CHECKED))
1611 continue;
1612
Willy Tarreaua4613182009-03-21 18:13:21 +01001613 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001614 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1615 return -1;
1616 }
1617
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001618 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001619 t->process = process_chk;
1620 t->context = s;
1621
1622 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001623 t->expire = tick_add(now_ms,
1624 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1625 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001626 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001627 task_queue(t);
1628
1629 srvpos++;
1630 }
1631 }
1632 return 0;
1633}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001634
1635/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001636 * Perform content verification check on data in s->check_data buffer.
1637 * The buffer MUST be terminated by a null byte before calling this function.
1638 * Sets server status appropriately. The caller is responsible for ensuring
1639 * that the buffer contains at least 13 characters. If <done> is zero, we may
1640 * return 0 to indicate that data is required to decide of a match.
1641 */
1642static int httpchk_expect(struct server *s, int done)
1643{
1644 static char status_msg[] = "HTTP status check returned code <000>";
1645 char status_code[] = "000";
1646 char *contentptr;
1647 int crlf;
1648 int ret;
1649
1650 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1651 case PR_O2_EXP_STS:
1652 case PR_O2_EXP_RSTS:
1653 memcpy(status_code, s->check_data + 9, 3);
1654 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1655
1656 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1657 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1658 else
1659 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1660
1661 /* we necessarily have the response, so there are no partial failures */
1662 if (s->proxy->options2 & PR_O2_EXP_INV)
1663 ret = !ret;
1664
1665 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1666 break;
1667
1668 case PR_O2_EXP_STR:
1669 case PR_O2_EXP_RSTR:
1670 /* very simple response parser: ignore CR and only count consecutive LFs,
1671 * stop with contentptr pointing to first char after the double CRLF or
1672 * to '\0' if crlf < 2.
1673 */
1674 crlf = 0;
1675 for (contentptr = s->check_data; *contentptr; contentptr++) {
1676 if (crlf >= 2)
1677 break;
1678 if (*contentptr == '\r')
1679 continue;
1680 else if (*contentptr == '\n')
1681 crlf++;
1682 else
1683 crlf = 0;
1684 }
1685
1686 /* Check that response contains a body... */
1687 if (crlf < 2) {
1688 if (!done)
1689 return 0;
1690
1691 set_server_check_status(s, HCHK_STATUS_L7RSP,
1692 "HTTP content check could not find a response body");
1693 return 1;
1694 }
1695
1696 /* Check that response body is not empty... */
1697 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001698 if (!done)
1699 return 0;
1700
Willy Tarreaubd741542010-03-16 18:46:54 +01001701 set_server_check_status(s, HCHK_STATUS_L7RSP,
1702 "HTTP content check found empty response body");
1703 return 1;
1704 }
1705
1706 /* Check the response content against the supplied string
1707 * or regex... */
1708 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1709 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1710 else
1711 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1712
1713 /* if we don't match, we may need to wait more */
1714 if (!ret && !done)
1715 return 0;
1716
1717 if (ret) {
1718 /* content matched */
1719 if (s->proxy->options2 & PR_O2_EXP_INV)
1720 set_server_check_status(s, HCHK_STATUS_L7RSP,
1721 "HTTP check matched unwanted content");
1722 else
1723 set_server_check_status(s, HCHK_STATUS_L7OKD,
1724 "HTTP content check matched");
1725 }
1726 else {
1727 if (s->proxy->options2 & PR_O2_EXP_INV)
1728 set_server_check_status(s, HCHK_STATUS_L7OKD,
1729 "HTTP check did not match unwanted content");
1730 else
1731 set_server_check_status(s, HCHK_STATUS_L7RSP,
1732 "HTTP content check did not match");
1733 }
1734 break;
1735 }
1736 return 1;
1737}
1738
1739/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001740 * Local variables:
1741 * c-indent-level: 8
1742 * c-basic-offset: 8
1743 * End:
1744 */