blob: 6a102dfd4cdb39803748eeca83634fe62f8807a2 [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
367 list_for_each_entry_safe(session, session_bck,
368 &srv->actconns, by_srv) {
369 if (session->srv_conn == srv &&
370 !(session->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
371 buffer_shutw_now(session->req);
372 buffer_shutr_now(session->rep);
373 session->task->nice = 1024;
Simon Horman752dc4a2011-06-21 14:34:59 +0900374 if (!(session->flags & SN_ERR_MASK))
375 session->flags |= SN_ERR_DOWN;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900376 task_wakeup(session->task, TASK_WOKEN_OTHER);
377 }
378 }
379}
380
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381/* Sets server <s> down, notifies by all available means, recounts the
382 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200383 * possible to other servers. It automatically recomputes the number of
384 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200385 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100386void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100388 struct server *srv;
389 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390 int xferred;
391
Cyril Bontécd19e512010-01-31 22:34:03 +0100392 if (s->state & SRV_MAINTAIN) {
393 s->health = s->rise;
394 }
395
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100396 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100397 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200398 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200399
400 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100401 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100402 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900404 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
405 shutdown_sessions(s);
406
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407 /* we might have sessions queued on this server and waiting for
408 * a connection. Those which are redispatchable will be queued
409 * to another server or to the proxy itself.
410 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100411 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100412
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200413 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100414
Cyril Bontécd19e512010-01-31 22:34:03 +0100415 if (s->state & SRV_MAINTAIN) {
416 chunk_printf(&msg,
417 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
418 s->proxy->id, s->id);
419 } else {
420 chunk_printf(&msg,
421 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
422 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100423
Cyril Bontécd19e512010-01-31 22:34:03 +0100424 server_status_printf(&msg, s,
425 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
426 xferred);
427 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100428 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200429
Willy Tarreau48494c02007-11-30 10:41:39 +0100430 /* we don't send an alert if the server was previously paused */
431 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100432 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100433 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100434 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200435
Willy Tarreaud64d2252010-10-17 17:16:42 +0200436 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100437 set_backend_down(s->proxy);
438
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200439 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100440
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100441 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100442 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100443 if (! (srv->state & SRV_MAINTAIN))
444 /* Only notify tracking servers that are not already in maintenance. */
445 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100447
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448 s->health = 0; /* failure */
449}
450
Cyril Bontécd19e512010-01-31 22:34:03 +0100451void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100452
453 struct server *srv;
454 struct chunk msg;
455 int xferred;
456
Cyril Bontécd19e512010-01-31 22:34:03 +0100457 if (s->state & SRV_MAINTAIN) {
458 s->health = s->rise;
459 }
460
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100461 if (s->health == s->rise || s->tracked) {
462 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
463 if (s->proxy->last_change < now.tv_sec) // ignore negative times
464 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
465 s->proxy->last_change = now.tv_sec;
466 }
467
468 if (s->last_change < now.tv_sec) // ignore negative times
469 s->down_time += now.tv_sec - s->last_change;
470
471 s->last_change = now.tv_sec;
472 s->state |= SRV_RUNNING;
473
474 if (s->slowstart > 0) {
475 s->state |= SRV_WARMINGUP;
476 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
477 /* For dynamic algorithms, start at the first step of the weight,
478 * without multiplying by BE_WEIGHT_SCALE.
479 */
480 s->eweight = s->uweight;
481 if (s->proxy->lbprm.update_server_eweight)
482 s->proxy->lbprm.update_server_eweight(s);
483 }
484 }
485 s->proxy->lbprm.set_server_status_up(s);
486
487 /* check if we can handle some connections queued at the proxy. We
488 * will take as many as we can handle.
489 */
490 xferred = check_for_pending(s);
491
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200492 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100493
Cyril Bontécd19e512010-01-31 22:34:03 +0100494 if (s->state & SRV_MAINTAIN) {
495 chunk_printf(&msg,
496 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
497 s->proxy->id, s->id);
498 } else {
499 chunk_printf(&msg,
500 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
501 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100502
Cyril Bontécd19e512010-01-31 22:34:03 +0100503 server_status_printf(&msg, s,
504 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
505 xferred);
506 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100507
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100508 Warning("%s.\n", trash);
509 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100510
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100511 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100512 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100513 if (! (srv->state & SRV_MAINTAIN))
514 /* Only notify tracking servers if they're not in maintenance. */
515 set_server_up(srv);
516
517 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100518 }
519
520 if (s->health >= s->rise)
521 s->health = s->rise + s->fall - 1; /* OK now */
522
523}
524
525static void set_server_disabled(struct server *s) {
526
527 struct server *srv;
528 struct chunk msg;
529 int xferred;
530
531 s->state |= SRV_GOINGDOWN;
532 s->proxy->lbprm.set_server_status_down(s);
533
534 /* we might have sessions queued on this server and waiting for
535 * a connection. Those which are redispatchable will be queued
536 * to another server or to the proxy itself.
537 */
538 xferred = redistribute_pending(s);
539
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200540 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100541
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200542 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100543 "Load-balancing on %sServer %s/%s is disabled",
544 s->state & SRV_BACKUP ? "Backup " : "",
545 s->proxy->id, s->id);
546
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200547 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200548 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100549 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100550
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100551 Warning("%s.\n", trash);
552 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100553
554 if (!s->proxy->srv_bck && !s->proxy->srv_act)
555 set_backend_down(s->proxy);
556
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100557 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100558 for(srv = s->tracknext; srv; srv = srv->tracknext)
559 set_server_disabled(srv);
560}
561
562static void set_server_enabled(struct server *s) {
563
564 struct server *srv;
565 struct chunk msg;
566 int xferred;
567
568 s->state &= ~SRV_GOINGDOWN;
569 s->proxy->lbprm.set_server_status_up(s);
570
571 /* check if we can handle some connections queued at the proxy. We
572 * will take as many as we can handle.
573 */
574 xferred = check_for_pending(s);
575
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200576 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100577
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200578 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100579 "Load-balancing on %sServer %s/%s is enabled again",
580 s->state & SRV_BACKUP ? "Backup " : "",
581 s->proxy->id, s->id);
582
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200583 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200584 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100585 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100586
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100587 Warning("%s.\n", trash);
588 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100589
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100590 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100591 for(srv = s->tracknext; srv; srv = srv->tracknext)
592 set_server_enabled(srv);
593}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100595void health_adjust(struct server *s, short status) {
596
597 int failed;
598 int expire;
599
600 /* return now if observing nor health check is not enabled */
601 if (!s->observe || !s->check)
602 return;
603
604 if (s->observe >= HANA_OBS_SIZE)
605 return;
606
607 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
608 return;
609
610 switch (analyze_statuses[status].lr[s->observe - 1]) {
611 case 1:
612 failed = 1;
613 break;
614
615 case 2:
616 failed = 0;
617 break;
618
619 default:
620 return;
621 }
622
623 if (!failed) {
624 /* good: clear consecutive_errors */
625 s->consecutive_errors = 0;
626 return;
627 }
628
629 s->consecutive_errors++;
630
631 if (s->consecutive_errors < s->consecutive_errors_limit)
632 return;
633
634 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
635 s->consecutive_errors, get_analyze_status(status));
636
637 switch (s->onerror) {
638 case HANA_ONERR_FASTINTER:
639 /* force fastinter - nothing to do here as all modes force it */
640 break;
641
642 case HANA_ONERR_SUDDTH:
643 /* simulate a pre-fatal failed health check */
644 if (s->health > s->rise)
645 s->health = s->rise + 1;
646
647 /* no break - fall through */
648
649 case HANA_ONERR_FAILCHK:
650 /* simulate a failed health check */
651 set_server_check_status(s, HCHK_STATUS_HANA, trash);
652
653 if (s->health > s->rise) {
654 s->health--; /* still good */
655 s->counters.failed_checks++;
656 }
657 else
658 set_server_down(s);
659
660 break;
661
662 case HANA_ONERR_MARKDWN:
663 /* mark server down */
664 s->health = s->rise;
665 set_server_check_status(s, HCHK_STATUS_HANA, trash);
666 set_server_down(s);
667
668 break;
669
670 default:
671 /* write a warning? */
672 break;
673 }
674
675 s->consecutive_errors = 0;
676 s->counters.failed_hana++;
677
678 if (s->fastinter) {
679 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
680 if (s->check->expire > expire)
681 s->check->expire = expire;
682 }
683}
684
Willy Tarreauef781042010-01-27 11:53:01 +0100685static int httpchk_build_status_header(struct server *s, char *buffer)
686{
687 int sv_state;
688 int ratio;
689 int hlen = 0;
690 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
691 "UP %d/%d", "UP",
692 "NOLB %d/%d", "NOLB",
693 "no check" };
694
695 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
696 hlen += 24;
697
698 if (!(s->state & SRV_CHECKED))
699 sv_state = 6; /* should obviously never happen */
700 else if (s->state & SRV_RUNNING) {
701 if (s->health == s->rise + s->fall - 1)
702 sv_state = 3; /* UP */
703 else
704 sv_state = 2; /* going down */
705
706 if (s->state & SRV_GOINGDOWN)
707 sv_state += 2;
708 } else {
709 if (s->health)
710 sv_state = 1; /* going up */
711 else
712 sv_state = 0; /* DOWN */
713 }
714
715 hlen += sprintf(buffer + hlen,
716 srv_hlt_st[sv_state],
717 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
718 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
719
720 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
721 s->proxy->id, s->id,
722 global.node,
723 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
724 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
725 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
726 s->nbpend);
727
728 if ((s->state & SRV_WARMINGUP) &&
729 now.tv_sec < s->last_change + s->slowstart &&
730 now.tv_sec >= s->last_change) {
731 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
732 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
733 }
734
735 buffer[hlen++] = '\r';
736 buffer[hlen++] = '\n';
737
738 return hlen;
739}
740
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741/*
742 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200743 * the connection acknowledgement. If the proxy requires L7 health-checks,
744 * it sends the request. In other cases, it calls set_server_check_status()
745 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200746 * The function itself returns 0 if it needs some polling before being called
747 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 */
Willy Tarreau83749182007-04-15 20:56:27 +0200749static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750{
Willy Tarreau6996e152007-04-30 14:37:43 +0200751 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752 struct task *t = fdtab[fd].owner;
753 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100755 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200756 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100757 int skerr, err = errno;
758 socklen_t lskerr = sizeof(skerr);
759
760 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
761 err = skerr;
762
763 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200764 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200765 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200766
767 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200768
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100769 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200771 if (s->proxy->options2 & PR_O2_CHK_ANY) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100773 const char *check_req = s->proxy->check_req;
774 int check_len = s->proxy->check_len;
775
Willy Tarreauf3c69202006-07-09 16:42:34 +0200776 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777 * so we'll send the request, and won't wake the checker up now.
778 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200779
Willy Tarreau1620ec32011-08-06 17:05:02 +0200780 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200781 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200782 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200783 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
784 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200785 else if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK) {
Willy Tarreaue9d87882010-01-27 11:28:42 +0100786 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100787
788 if (s->proxy->options2 & PR_O2_CHK_SNDST)
789 check_len += httpchk_build_status_header(s, trash + check_len);
790
Willy Tarreaue9d87882010-01-27 11:28:42 +0100791 trash[check_len++] = '\r';
792 trash[check_len++] = '\n';
793 trash[check_len] = '\0';
794 check_req = trash;
795 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200796
Willy Tarreaue9d87882010-01-27 11:28:42 +0100797 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
798 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100799 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100800 if (s->proxy->timeout.check)
801 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200802 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200803 goto out_nowake;
804 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200805 else if (ret == 0 || errno == EAGAIN)
806 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200807 else {
808 switch (errno) {
809 case ECONNREFUSED:
810 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200811 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200812 break;
813
814 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200815 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200816 }
817
Willy Tarreau6996e152007-04-30 14:37:43 +0200818 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200819 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820 }
821 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200822 /* We have no data to send to check the connection, and
823 * getsockopt() will not inform us whether the connection
824 * is still pending. So we'll reuse connect() to check the
825 * state of the socket. This has the advantage of givig us
826 * the following info :
827 * - error
828 * - connecting (EALREADY, EINPROGRESS)
829 * - connected (EISCONN, 0)
830 */
831
David du Colombier6f5ccb12011-03-10 22:26:24 +0100832 struct sockaddr_storage sa;
Willy Tarreau6996e152007-04-30 14:37:43 +0200833
David du Colombier6f5ccb12011-03-10 22:26:24 +0100834 if (is_addr(&s->check_addr))
835 sa = s->check_addr;
836 else
837 sa = s->addr;
838
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200839 set_host_port(&sa, s->check_port);
Willy Tarreau6996e152007-04-30 14:37:43 +0200840
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200841 if (connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) == 0)
Willy Tarreau6996e152007-04-30 14:37:43 +0200842 errno = 0;
843
844 if (errno == EALREADY || errno == EINPROGRESS)
845 goto out_poll;
846
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200847 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200848 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200849 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200850 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200851
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200853 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200854 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200855 }
856 }
Willy Tarreau83749182007-04-15 20:56:27 +0200857 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200858 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200859 out_nowake:
860 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100861 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200862 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200863 out_poll:
864 /* The connection is still pending. We'll have to poll it
865 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100866 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200867 return 0;
868 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200869 fdtab[fd].state = FD_STERROR;
870 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871}
872
873
874/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200875 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200876 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
877 * set_server_check_status() to update s->check_status, s->check_duration
878 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200879
880 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
881 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
882 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
883 * response to an SSL HELLO (the principle is that this is enough to
884 * distinguish between an SSL server and a pure TCP relay). All other cases will
885 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
886 * etc.
887 *
888 * The function returns 0 if it needs to be called again after some polling,
889 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200890 */
Willy Tarreau83749182007-04-15 20:56:27 +0200891static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200892{
Willy Tarreau83749182007-04-15 20:56:27 +0200893 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100894 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 struct task *t = fdtab[fd].owner;
896 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200897 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100898 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200899 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200900
Willy Tarreau659d7bc2010-03-16 21:14:41 +0100901 if (unlikely((s->result & SRV_CHK_ERROR) || (fdtab[fd].state == FD_STERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200902 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200903 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200904 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200905
Willy Tarreau83749182007-04-15 20:56:27 +0200906 goto out_wakeup;
907 }
908
Willy Tarreau83749182007-04-15 20:56:27 +0200909 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
910 * but the connection was closed on the remote end. Fortunately, recv still
911 * works correctly and we don't need to do the getsockopt() on linux.
912 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000913
914 /* Set buffer to point to the end of the data already read, and check
915 * that there is free space remaining. If the buffer is full, proceed
916 * with running the checks without attempting another socket read.
917 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000918
Willy Tarreau03938182010-03-17 21:52:07 +0100919 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200920 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
921 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 +0100922 if (len <= 0)
923 break;
924 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000925
Willy Tarreau03938182010-03-17 21:52:07 +0100926 if (len == 0)
927 done = 1; /* connection hangup received */
928 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100929 /* Report network errors only if we got no other data. Otherwise
930 * we'll let the upper layers decide whether the response is OK
931 * or not. It is very common that an RST sent by the server is
932 * reported as an error just after the last data chunk.
933 */
Willy Tarreau03938182010-03-17 21:52:07 +0100934 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100935 if (!s->check_data_len) {
936 if (!(s->result & SRV_CHK_ERROR))
937 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
938 goto out_wakeup;
939 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 }
941
Willy Tarreau03938182010-03-17 21:52:07 +0100942 /* Intermediate or complete response received.
943 * Terminate string in check_data buffer.
944 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200945 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100946 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100947 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100948 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100949 done = 1; /* buffer full, don't wait for more data */
950 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200951
Nick Chalk57b1bf72010-03-16 15:50:46 +0000952 /* Run the checks... */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200953 switch (s->proxy->options2 & PR_O2_CHK_ANY) {
954 case PR_O2_HTTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100955 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
956 goto wait_more_data;
957
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100958 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000959 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
960 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
961 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
962 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
963 !isdigit((unsigned char) *(s->check_data + 11))) {
964 cut_crlf(s->check_data);
965 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200966
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100967 goto out_wakeup;
968 }
969
Nick Chalk57b1bf72010-03-16 15:50:46 +0000970 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000971 desc = ltrim(s->check_data + 12, ' ');
972
Willy Tarreaubd741542010-03-16 18:46:54 +0100973 if ((s->proxy->options & PR_O_DISABLE404) &&
974 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000975 /* 404 may be accepted as "stopping" only if the server was up */
976 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200977 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000978 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100979 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
980 /* Run content verification check... We know we have at least 13 chars */
981 if (!httpchk_expect(s, done))
982 goto wait_more_data;
983 }
984 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
985 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
986 cut_crlf(desc);
987 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
988 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000989 else {
990 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200991 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000992 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200993 break;
994
995 case PR_O2_SSL3_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100996 if (!done && s->check_data_len < 5)
997 goto wait_more_data;
998
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100999 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001000 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001001 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001002 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001003 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001004 break;
1005
1006 case PR_O2_SMTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001007 if (!done && s->check_data_len < strlen("000\r"))
1008 goto wait_more_data;
1009
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001010 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001011 if ((s->check_data_len < strlen("000\r")) ||
1012 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
1013 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
1014 !isdigit((unsigned char) *(s->check_data + 2))) {
1015 cut_crlf(s->check_data);
1016 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001017
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001018 goto out_wakeup;
1019 }
1020
Nick Chalk57b1bf72010-03-16 15:50:46 +00001021 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001022
Nick Chalk57b1bf72010-03-16 15:50:46 +00001023 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001024 cut_crlf(desc);
1025
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001026 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001027 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001028 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001029 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001030 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001031 break;
1032
1033 case PR_O2_PGSQL_CHK:
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001034 if (!done && s->check_data_len < 9)
1035 goto wait_more_data;
1036
1037 if (s->check_data[0] == 'R') {
1038 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1039 }
1040 else {
1041 if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0))
1042 desc = &s->check_data[6];
1043 else
1044 desc = "PostgreSQL unknown error";
1045
1046 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1047 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001048 break;
1049
1050 case PR_O2_REDIS_CHK:
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001051 if (!done && s->check_data_len < 7)
1052 goto wait_more_data;
1053
1054 if (strcmp(s->check_data, "+PONG\r\n") == 0) {
1055 set_server_check_status(s, HCHK_STATUS_L7OKD, "Redis server is ok");
1056 }
1057 else {
1058 set_server_check_status(s, HCHK_STATUS_L7STS, s->check_data);
1059 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001060 break;
1061
1062 case PR_O2_MYSQL_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001063 if (!done && s->check_data_len < 5)
1064 goto wait_more_data;
1065
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001066 if (s->proxy->check_len == 0) { // old mode
1067 if (*(s->check_data + 4) != '\xff') {
1068 /* We set the MySQL Version in description for information purpose
1069 * FIXME : it can be cool to use MySQL Version for other purpose,
1070 * like mark as down old MySQL server.
1071 */
1072 if (s->check_data_len > 51) {
1073 desc = ltrim(s->check_data + 5, ' ');
1074 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1075 }
1076 else {
1077 if (!done)
1078 goto wait_more_data;
1079 /* it seems we have a OK packet but without a valid length,
1080 * it must be a protocol error
1081 */
1082 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1083 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001084 }
1085 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001086 /* An error message is attached in the Error packet */
1087 desc = ltrim(s->check_data + 7, ' ');
1088 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1089 }
1090 } else {
1091 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1092 (((unsigned int) *(s->check_data + 1)) << 8) +
1093 (((unsigned int) *(s->check_data + 2)) << 16);
1094
1095 if (s->check_data_len == first_packet_len + 4) {
1096 /* MySQL Error packet always begin with field_count = 0xff */
1097 if (*(s->check_data + 4) != '\xff') {
1098 /* We have only one MySQL packet and it is a Handshake Initialization packet
1099 * but we need to have a second packet to know if it is alright
1100 */
1101 if (!done && s->check_data_len < first_packet_len + 5)
1102 goto wait_more_data;
1103 }
1104 else {
1105 /* We have only one packet and it is an Error packet,
1106 * an error message is attached, so we can display it
1107 */
1108 desc = &s->check_data[7];
1109 //Warning("onlyoneERR: %s\n", desc);
1110 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1111 }
1112 } else if (s->check_data_len > first_packet_len + 4) {
1113 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1114 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1115 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1116
1117 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1118 /* We have 2 packets and that's good */
1119 /* Check if the second packet is a MySQL Error packet or not */
1120 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1121 /* No error packet */
1122 /* We set the MySQL Version in description for information purpose */
1123 desc = &s->check_data[5];
1124 //Warning("2packetOK: %s\n", desc);
1125 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1126 }
1127 else {
1128 /* An error message is attached in the Error packet
1129 * so we can display it ! :)
1130 */
1131 desc = &s->check_data[first_packet_len+11];
1132 //Warning("2packetERR: %s\n", desc);
1133 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1134 }
1135 }
1136 }
1137 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001138 if (!done)
1139 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001140 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001141 * it must be a protocol error
1142 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001143 desc = &s->check_data[5];
1144 //Warning("protoerr: %s\n", desc);
1145 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001146 }
1147 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001148 break;
1149
1150 case PR_O2_LDAP_CHK:
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001151 if (!done && s->check_data_len < 14)
1152 goto wait_more_data;
1153
1154 /* Check if the server speaks LDAP (ASN.1/BER)
1155 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1156 * http://tools.ietf.org/html/rfc4511
1157 */
1158
1159 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1160 * LDAPMessage: 0x30: SEQUENCE
1161 */
1162 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1163 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1164 }
1165 else {
1166 /* size of LDAPMessage */
1167 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1168
1169 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1170 * messageID: 0x02 0x01 0x01: INTEGER 1
1171 * protocolOp: 0x61: bindResponse
1172 */
1173 if ((msglen > 2) ||
1174 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1175 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1176
1177 goto out_wakeup;
1178 }
1179
1180 /* size of bindResponse */
1181 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1182
1183 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1184 * ldapResult: 0x0a 0x01: ENUMERATION
1185 */
1186 if ((msglen > 4) ||
1187 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1188 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1189
1190 goto out_wakeup;
1191 }
1192
1193 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1194 * resultCode
1195 */
1196 s->check_code = *(s->check_data + msglen + 9);
1197 if (s->check_code) {
1198 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1199 } else {
1200 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1201 }
1202 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001203 break;
1204
1205 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001206 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001207 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001208 break;
1209 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001210
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001211 out_wakeup:
1212 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001213 fdtab[fd].state = FD_STERROR;
1214
Nick Chalk57b1bf72010-03-16 15:50:46 +00001215 /* Reset the check buffer... */
1216 *s->check_data = '\0';
1217 s->check_data_len = 0;
1218
Willy Tarreau03938182010-03-17 21:52:07 +01001219 /* Close the connection... */
1220 shutdown(fd, SHUT_RDWR);
Willy Tarreauf161a342007-04-08 16:59:42 +02001221 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001222 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +01001223 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +02001224 return 1;
Willy Tarreau03938182010-03-17 21:52:07 +01001225
1226 wait_more_data:
1227 fdtab[fd].ev &= ~FD_POLL_IN;
1228 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229}
1230
1231/*
1232 * manages a server health-check. Returns
1233 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1234 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001235struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001236{
Willy Tarreaue3838802009-03-21 18:58:32 +01001237 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238 struct server *s = t->context;
David du Colombier6f5ccb12011-03-10 22:26:24 +01001239 struct sockaddr_storage sa;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001241 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001242
1243 //fprintf(stderr, "process_chk: task=%p\n", t);
1244
1245 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001246 if (attempts++ > 0) {
1247 /* we always fail to create a server, let's stop insisting... */
1248 while (tick_is_expired(t->expire, now_ms))
1249 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1250 return t;
1251 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001252 fd = s->curfd;
1253 if (fd < 0) { /* no check currently running */
1254 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +01001255 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1256 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257
1258 /* we don't send any health-checks when the proxy is stopped or when
1259 * the server should not be checked.
1260 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001261 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001262 while (tick_is_expired(t->expire, now_ms))
1263 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001264 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001265 }
1266
1267 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001268 set_server_check_status(s, HCHK_STATUS_START, NULL);
David du Colombier6f5ccb12011-03-10 22:26:24 +01001269 if ((fd = socket(s->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270 if ((fd < global.maxsock) &&
1271 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1272 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1273 //fprintf(stderr, "process_chk: 3\n");
1274
Willy Tarreau9edd1612007-10-18 18:07:48 +02001275 if (s->proxy->options & PR_O_TCP_NOLING) {
1276 /* We don't want to useless data */
1277 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1278 }
David du Colombier6f5ccb12011-03-10 22:26:24 +01001279
1280 if (is_addr(&s->check_addr))
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001281 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001282 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001283 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001284 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001285 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001286
Willy Tarreau48da04a2011-08-27 11:51:36 +02001287 set_host_port(&sa, s->check_port);
1288
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289 /* allow specific binding :
1290 * - server-specific at first
1291 * - proxy-specific next
1292 */
1293 if (s->state & SRV_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001294 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001295 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001296
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001297#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001298 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001299 remote = &s->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001300 flags = 3;
1301 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001302#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001303#ifdef SO_BINDTODEVICE
1304 /* Note: this might fail if not CAP_NET_RAW */
1305 if (s->iface_name)
1306 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001307 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001308#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001309 if (s->sport_range) {
1310 int bind_attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +01001311 struct sockaddr_storage src;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001312
1313 ret = 1;
1314 src = s->source_addr;
1315
1316 do {
1317 /* note: in case of retry, we may have to release a previously
1318 * allocated port, hence this loop's construct.
1319 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001320 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1321 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001322
1323 if (!bind_attempts)
1324 break;
1325 bind_attempts--;
1326
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001327 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1328 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001329 break;
1330
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001331 fdinfo[fd].port_range = s->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001332 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001333
David du Colombier6f5ccb12011-03-10 22:26:24 +01001334 ret = tcp_bind_socket(fd, flags, &src, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001335 } while (ret != 0); /* binding NOK */
1336 }
1337 else {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001338 ret = tcp_bind_socket(fd, flags, &s->source_addr, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001339 }
1340
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001341 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001342 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001343 switch (ret) {
1344 case 1:
1345 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1346 s->proxy->id, s->id);
1347 break;
1348 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001349 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1350 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001351 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001352 }
1353 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001354 }
1355 else if (s->proxy->options & PR_O_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001356 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001357 int ret, flags = 0;
1358
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001359#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001360 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001361 remote = &s->proxy->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001362 flags = 3;
1363 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001364#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001365#ifdef SO_BINDTODEVICE
1366 /* Note: this might fail if not CAP_NET_RAW */
1367 if (s->proxy->iface_name)
1368 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001369 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001370#endif
David du Colombier6f5ccb12011-03-10 22:26:24 +01001371 ret = tcp_bind_socket(fd, flags, &s->proxy->source_addr, remote);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001372 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001373 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001374 switch (ret) {
1375 case 1:
1376 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1377 proxy_type_str(s->proxy), s->proxy->id);
1378 break;
1379 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001380 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1381 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001382 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001383 }
1384 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001385 }
1386
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001387 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001388#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001389 /* disabling tcp quick ack now allows
1390 * the request to leave the machine with
1391 * the first ACK.
1392 */
1393 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001394 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001395#endif
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +02001396 if ((connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) != -1) || (errno == EINPROGRESS)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001397 /* OK, connection in progress or established */
1398
1399 //fprintf(stderr, "process_chk: 4\n");
1400
1401 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +02001402 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +02001404 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
1405 fdtab[fd].cb[DIR_RD].b = NULL;
1406 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
1407 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001408 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +02001409 fdinfo[fd].peerlen = get_addr_len(&sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001410 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001411 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001412 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001413#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001414 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001415#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001416 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1417 /* we allow up to min(inter, timeout.connect) for a connection
1418 * to establish but only when timeout.check is set
1419 * as it may be to short for a full check otherwise
1420 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001421 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001422
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001423 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1424 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1425 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001426 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001427 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001428 }
1429 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001430 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001431
1432 switch (errno) {
1433 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1434 case ECONNREFUSED:
1435 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001436 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001437 break;
1438
1439 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001440 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001441 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001442 }
1443 }
1444 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001445 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1446 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001447 close(fd); /* socket creation error */
1448 }
Willy Tarreaud11ad782011-03-09 20:38:33 +01001449 else
1450 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001451
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001452 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001453 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001454 while (tick_is_expired(t->expire, now_ms))
1455 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001456 goto new_chk; /* may be we should initialize a new check */
1457 }
1458
1459 /* here, we have seen a failure */
1460 if (s->health > s->rise) {
1461 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001462 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001463 }
1464 else
1465 set_server_down(s);
1466
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001467 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1468 /* we allow up to min(inter, timeout.connect) for a connection
1469 * to establish but only when timeout.check is set
1470 * as it may be to short for a full check otherwise
1471 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001472 while (tick_is_expired(t->expire, now_ms)) {
1473 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001474
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001475 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1476 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001477
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001478 if (s->proxy->timeout.check)
1479 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001480 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001481 goto new_chk;
1482 }
1483 else {
1484 //fprintf(stderr, "process_chk: 8\n");
1485 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001486 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001487 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001488
Willy Tarreau9909fc12007-11-30 17:42:05 +01001489 if (s->state & SRV_WARMINGUP) {
1490 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1491 s->state &= ~SRV_WARMINGUP;
1492 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1493 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1494 if (s->proxy->lbprm.update_server_eweight)
1495 s->proxy->lbprm.update_server_eweight(s);
1496 }
1497 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1498 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +01001499 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1500 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +01001501 s->eweight *= s->uweight;
1502 if (s->proxy->lbprm.update_server_eweight)
1503 s->proxy->lbprm.update_server_eweight(s);
1504 }
1505 /* probably that we can refill this server with a bit more connections */
1506 check_for_pending(s);
1507 }
1508
Willy Tarreau48494c02007-11-30 10:41:39 +01001509 /* we may have to add/remove this server from the LB group */
1510 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1511 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001512 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1513 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001514 else if (!(s->state & SRV_GOINGDOWN) &&
1515 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001516 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1517 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001518 }
1519
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001520 if (s->health < s->rise + s->fall - 1) {
1521 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001522
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001523 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001524 }
1525 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001526 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001527
1528 rv = 0;
1529 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001530 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001531 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001532 //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 +02001533 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001534 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001535 goto new_chk;
1536 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001537 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001538 if (!(s->result & SRV_CHK_ERROR)) {
1539 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001540 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001541 } else {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001542 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001543 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001544 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001545 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001546 }
1547 }
1548
Willy Tarreaubaaee002006-06-26 02:48:02 +02001549 //fprintf(stderr, "process_chk: 10\n");
1550 /* failure or timeout detected */
1551 if (s->health > s->rise) {
1552 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001553 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001554 }
1555 else
1556 set_server_down(s);
1557 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001558 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001559
1560 rv = 0;
1561 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001562 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001563 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001564 //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 +02001565 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001566 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001567 goto new_chk;
1568 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001569 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001570 }
1571 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001572 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001573 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001574}
1575
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001576/*
1577 * Start health-check.
1578 * Returns 0 if OK, -1 if error, and prints the error in this case.
1579 */
1580int start_checks() {
1581
1582 struct proxy *px;
1583 struct server *s;
1584 struct task *t;
1585 int nbchk=0, mininter=0, srvpos=0;
1586
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001587 /* 1- count the checkers to run simultaneously.
1588 * We also determine the minimum interval among all of those which
1589 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1590 * will be used to spread their start-up date. Those which have
1591 * a shorter interval will start independantly and will not dictate
1592 * too short an interval for all others.
1593 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001594 for (px = proxy; px; px = px->next) {
1595 for (s = px->srv; s; s = s->next) {
1596 if (!(s->state & SRV_CHECKED))
1597 continue;
1598
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001599 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1600 (!mininter || mininter > srv_getinter(s)))
1601 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001602
1603 nbchk++;
1604 }
1605 }
1606
1607 if (!nbchk)
1608 return 0;
1609
1610 srand((unsigned)time(NULL));
1611
1612 /*
1613 * 2- start them as far as possible from each others. For this, we will
1614 * start them after their interval set to the min interval divided by
1615 * the number of servers, weighted by the server's position in the list.
1616 */
1617 for (px = proxy; px; px = px->next) {
1618 for (s = px->srv; s; s = s->next) {
1619 if (!(s->state & SRV_CHECKED))
1620 continue;
1621
Willy Tarreaua4613182009-03-21 18:13:21 +01001622 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001623 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1624 return -1;
1625 }
1626
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001627 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001628 t->process = process_chk;
1629 t->context = s;
1630
1631 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001632 t->expire = tick_add(now_ms,
1633 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1634 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001635 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001636 task_queue(t);
1637
1638 srvpos++;
1639 }
1640 }
1641 return 0;
1642}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001643
1644/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001645 * Perform content verification check on data in s->check_data buffer.
1646 * The buffer MUST be terminated by a null byte before calling this function.
1647 * Sets server status appropriately. The caller is responsible for ensuring
1648 * that the buffer contains at least 13 characters. If <done> is zero, we may
1649 * return 0 to indicate that data is required to decide of a match.
1650 */
1651static int httpchk_expect(struct server *s, int done)
1652{
1653 static char status_msg[] = "HTTP status check returned code <000>";
1654 char status_code[] = "000";
1655 char *contentptr;
1656 int crlf;
1657 int ret;
1658
1659 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1660 case PR_O2_EXP_STS:
1661 case PR_O2_EXP_RSTS:
1662 memcpy(status_code, s->check_data + 9, 3);
1663 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1664
1665 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1666 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1667 else
1668 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1669
1670 /* we necessarily have the response, so there are no partial failures */
1671 if (s->proxy->options2 & PR_O2_EXP_INV)
1672 ret = !ret;
1673
1674 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1675 break;
1676
1677 case PR_O2_EXP_STR:
1678 case PR_O2_EXP_RSTR:
1679 /* very simple response parser: ignore CR and only count consecutive LFs,
1680 * stop with contentptr pointing to first char after the double CRLF or
1681 * to '\0' if crlf < 2.
1682 */
1683 crlf = 0;
1684 for (contentptr = s->check_data; *contentptr; contentptr++) {
1685 if (crlf >= 2)
1686 break;
1687 if (*contentptr == '\r')
1688 continue;
1689 else if (*contentptr == '\n')
1690 crlf++;
1691 else
1692 crlf = 0;
1693 }
1694
1695 /* Check that response contains a body... */
1696 if (crlf < 2) {
1697 if (!done)
1698 return 0;
1699
1700 set_server_check_status(s, HCHK_STATUS_L7RSP,
1701 "HTTP content check could not find a response body");
1702 return 1;
1703 }
1704
1705 /* Check that response body is not empty... */
1706 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001707 if (!done)
1708 return 0;
1709
Willy Tarreaubd741542010-03-16 18:46:54 +01001710 set_server_check_status(s, HCHK_STATUS_L7RSP,
1711 "HTTP content check found empty response body");
1712 return 1;
1713 }
1714
1715 /* Check the response content against the supplied string
1716 * or regex... */
1717 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1718 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1719 else
1720 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1721
1722 /* if we don't match, we may need to wait more */
1723 if (!ret && !done)
1724 return 0;
1725
1726 if (ret) {
1727 /* content matched */
1728 if (s->proxy->options2 & PR_O2_EXP_INV)
1729 set_server_check_status(s, HCHK_STATUS_L7RSP,
1730 "HTTP check matched unwanted content");
1731 else
1732 set_server_check_status(s, HCHK_STATUS_L7OKD,
1733 "HTTP content check matched");
1734 }
1735 else {
1736 if (s->proxy->options2 & PR_O2_EXP_INV)
1737 set_server_check_status(s, HCHK_STATUS_L7OKD,
1738 "HTTP check did not match unwanted content");
1739 else
1740 set_server_check_status(s, HCHK_STATUS_L7RSP,
1741 "HTTP content check did not match");
1742 }
1743 break;
1744 }
1745 return 1;
1746}
1747
1748/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001749 * Local variables:
1750 * c-indent-level: 8
1751 * c-basic-offset: 8
1752 * End:
1753 */