blob: af11c1d4ad1783c6c1dcb6fad98b4a38f1cd6ee6 [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 Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020033#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020039#include <proto/checks.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 Tarreaufb56aab2012-09-28 14:40:02 +020047#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090049#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010050#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/task.h>
52
Willy Tarreaubd741542010-03-16 18:46:54 +010053static int httpchk_expect(struct server *s, int done);
54
Simon Horman63a4a822012-03-19 07:24:41 +090055static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020056 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
57 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
58 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020059
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010060 [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" },
61
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020062 [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020063
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020064 [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" },
65 [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" },
66 [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020067
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020068 [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" },
69 [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" },
70 [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020071
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020072 [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" },
73 [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020074
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020075 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020077 [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" },
78 [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
79 [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020080};
81
Simon Horman63a4a822012-03-19 07:24:41 +090082static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010083 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
84
85 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
86 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
87
88 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
89 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
90 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
91 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
92
93 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
94 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
95 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
96};
97
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020098/*
99 * Convert check_status code to description
100 */
101const char *get_check_status_description(short check_status) {
102
103 const char *desc;
104
105 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200106 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200107 else
108 desc = NULL;
109
110 if (desc && *desc)
111 return desc;
112 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200113 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200114}
115
116/*
117 * Convert check_status code to short info
118 */
119const char *get_check_status_info(short check_status) {
120
121 const char *info;
122
123 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200124 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200125 else
126 info = NULL;
127
128 if (info && *info)
129 return info;
130 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200131 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200132}
133
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100134const char *get_analyze_status(short analyze_status) {
135
136 const char *desc;
137
138 if (analyze_status < HANA_STATUS_SIZE)
139 desc = analyze_statuses[analyze_status].desc;
140 else
141 desc = NULL;
142
143 if (desc && *desc)
144 return desc;
145 else
146 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
147}
148
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200149#define SSP_O_HCHK 0x0002
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200150
151static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
152
Willy Tarreau44267702011-10-28 15:35:33 +0200153 if (s->track)
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200154 chunk_printf(msg, " via %s/%s",
Willy Tarreau44267702011-10-28 15:35:33 +0200155 s->track->proxy->id, s->track->id);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200156
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200157 if (options & SSP_O_HCHK) {
158 chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status));
159
160 if (s->check_status >= HCHK_STATUS_L57DATA)
161 chunk_printf(msg, ", code: %d", s->check_code);
162
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200163 if (*s->check_desc) {
164 struct chunk src;
165
166 chunk_printf(msg, ", info: \"");
167
168 chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc));
169 chunk_asciiencode(msg, &src, '"');
170
171 chunk_printf(msg, "\"");
172 }
173
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100174 if (s->check_duration >= 0)
175 chunk_printf(msg, ", check duration: %ldms", s->check_duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200176 }
177
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200178 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100179 if (!(s->state & SRV_RUNNING))
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200180 chunk_printf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100181 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200182 s->proxy->srv_act, s->proxy->srv_bck,
183 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
184 s->cur_sess, xferred, s->nbpend);
185 else
186 chunk_printf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100187 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200188 s->proxy->srv_act, s->proxy->srv_bck,
189 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
190 xferred, s->nbpend);
191 }
192}
193
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200194/*
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200195 * Set s->check_status, update s->check_duration and fill s->result with
196 * an adequate SRV_CHK_* value.
197 *
198 * Show information in logs about failed health check if server is UP
199 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200200 */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200201static void set_server_check_status(struct server *s, short status, char *desc) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200202
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200203 struct chunk msg;
204
205 if (status == HCHK_STATUS_START) {
206 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200207 s->check_desc[0] = '\0';
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200208 s->check_start = now;
209 return;
210 }
211
212 if (!s->check_status)
213 return;
214
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200215 if (desc && *desc) {
216 strncpy(s->check_desc, desc, HCHK_DESC_LEN-1);
217 s->check_desc[HCHK_DESC_LEN-1] = '\0';
218 } else
219 s->check_desc[0] = '\0';
220
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200221 s->check_status = status;
222 if (check_statuses[status].result)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100223 s->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200224
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100225 if (status == HCHK_STATUS_HANA)
226 s->check_duration = -1;
227 else if (!tv_iszero(&s->check_start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200228 /* set_server_check_status() may be called more than once */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200229 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
230 tv_zero(&s->check_start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200231 }
232
233 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
234 (((s->health != 0) && (s->result & SRV_CHK_ERROR)) ||
235 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) ||
236 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
237 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
238
239 int health, rise, fall, state;
240
David du Colombier7af46052012-05-16 14:16:48 +0200241 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200242
243 /* FIXME begin: calculate local version of the health/rise/fall/state */
244 health = s->health;
245 rise = s->rise;
246 fall = s->fall;
247 state = s->state;
248
249 if (s->result & SRV_CHK_ERROR) {
250 if (health > rise) {
251 health--; /* still good */
252 } else {
253 if (health == rise)
254 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
255
256 health = 0;
257 }
258 }
259
260 if (s->result & SRV_CHK_RUNNING) {
261 if (health < rise + fall - 1) {
262 health++; /* was bad, stays for a while */
263
264 if (health == rise)
265 state |= SRV_RUNNING;
266
267 if (health >= rise)
268 health = rise + fall - 1; /* OK now */
269 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100270
271 /* clear consecutive_errors if observing is enabled */
272 if (s->onerror)
273 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200274 }
275 /* FIXME end: calculate local version of the health/rise/fall/state */
276
277 chunk_printf(&msg,
278 "Health check for %sserver %s/%s %s%s",
279 s->state & SRV_BACKUP ? "backup " : "",
280 s->proxy->id, s->id,
281 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
282 (s->result & SRV_CHK_RUNNING)?"succeeded":"failed");
283
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200284 server_status_printf(&msg, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100286 chunk_printf(&msg, ", status: %d/%d %s",
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200287 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
288 (state & SRV_RUNNING) ? (fall) : (rise),
289 (state & SRV_RUNNING)?"UP":"DOWN");
290
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100291 Warning("%s.\n", trash);
292 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200293 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200294}
295
Willy Tarreau48494c02007-11-30 10:41:39 +0100296/* sends a log message when a backend goes down, and also sets last
297 * change date.
298 */
299static void set_backend_down(struct proxy *be)
300{
301 be->last_change = now.tv_sec;
302 be->down_trans++;
303
304 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
305 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
306}
307
308/* Redistribute pending connections when a server goes down. The number of
309 * connections redistributed is returned.
310 */
311static int redistribute_pending(struct server *s)
312{
313 struct pendconn *pc, *pc_bck, *pc_end;
314 int xferred = 0;
315
316 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
317 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100318 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
319 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100320 /* The REDISP option was specified. We will ignore
321 * cookie and force to balance or use the dispatcher.
322 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100323
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100324 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100325 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100326
Willy Tarreau48494c02007-11-30 10:41:39 +0100327 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200328 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100329 xferred++;
330 }
331 }
332 return xferred;
333}
334
335/* Check for pending connections at the backend, and assign some of them to
336 * the server coming up. The server's weight is checked before being assigned
337 * connections it may not be able to handle. The total number of transferred
338 * connections is returned.
339 */
340static int check_for_pending(struct server *s)
341{
342 int xferred;
343
344 if (!s->eweight)
345 return 0;
346
347 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
348 struct session *sess;
349 struct pendconn *p;
350
351 p = pendconn_from_px(s->proxy);
352 if (!p)
353 break;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100354 set_target_server(&p->sess->target, s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100355 sess = p->sess;
356 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200357 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100358 }
359 return xferred;
360}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700362/* Shutdown all connections of a server. The caller must pass a termination
363 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
364 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900365 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700366static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900367{
368 struct session *session, *session_bck;
369
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200370 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
371 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700372 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900373}
374
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700375/* Shutdown all connections of all backup servers of a proxy. The caller must
376 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
377 * the reason for the shutdown.
378 */
379static void shutdown_backup_sessions(struct proxy *px, int why)
380{
381 struct server *srv;
382
383 for (srv = px->srv; srv != NULL; srv = srv->next)
384 if (srv->state & SRV_BACKUP)
385 shutdown_sessions(srv, why);
386}
387
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388/* Sets server <s> down, notifies by all available means, recounts the
389 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200390 * possible to other servers. It automatically recomputes the number of
391 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100393void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100395 struct server *srv;
396 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397 int xferred;
398
Cyril Bontécd19e512010-01-31 22:34:03 +0100399 if (s->state & SRV_MAINTAIN) {
400 s->health = s->rise;
401 }
402
Willy Tarreau44267702011-10-28 15:35:33 +0200403 if (s->health == s->rise || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100404 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200405 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200406
407 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100408 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200409 if (s->proxy->lbprm.set_server_status_down)
410 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900412 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700413 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900414
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415 /* we might have sessions queued on this server and waiting for
416 * a connection. Those which are redispatchable will be queued
417 * to another server or to the proxy itself.
418 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100419 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100420
David du Colombier7af46052012-05-16 14:16:48 +0200421 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100422
Cyril Bontécd19e512010-01-31 22:34:03 +0100423 if (s->state & SRV_MAINTAIN) {
424 chunk_printf(&msg,
425 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
426 s->proxy->id, s->id);
427 } else {
428 chunk_printf(&msg,
429 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
430 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100431
Cyril Bontécd19e512010-01-31 22:34:03 +0100432 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200433 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Cyril Bontécd19e512010-01-31 22:34:03 +0100434 xferred);
435 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100436 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200437
Willy Tarreau48494c02007-11-30 10:41:39 +0100438 /* we don't send an alert if the server was previously paused */
439 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100440 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100441 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100442 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200443
Willy Tarreaud64d2252010-10-17 17:16:42 +0200444 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100445 set_backend_down(s->proxy);
446
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200447 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100448
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100449 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100450 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100451 if (! (srv->state & SRV_MAINTAIN))
452 /* Only notify tracking servers that are not already in maintenance. */
453 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100455
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456 s->health = 0; /* failure */
457}
458
Cyril Bontécd19e512010-01-31 22:34:03 +0100459void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100460
461 struct server *srv;
462 struct chunk msg;
463 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100464 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100465
Cyril Bontécd19e512010-01-31 22:34:03 +0100466 if (s->state & SRV_MAINTAIN) {
467 s->health = s->rise;
468 }
469
Willy Tarreau44267702011-10-28 15:35:33 +0200470 if (s->health == s->rise || s->track) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100471 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
472 if (s->proxy->last_change < now.tv_sec) // ignore negative times
473 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
474 s->proxy->last_change = now.tv_sec;
475 }
476
477 if (s->last_change < now.tv_sec) // ignore negative times
478 s->down_time += now.tv_sec - s->last_change;
479
480 s->last_change = now.tv_sec;
481 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100482 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100483
484 if (s->slowstart > 0) {
485 s->state |= SRV_WARMINGUP;
486 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
487 /* For dynamic algorithms, start at the first step of the weight,
488 * without multiplying by BE_WEIGHT_SCALE.
489 */
490 s->eweight = s->uweight;
491 if (s->proxy->lbprm.update_server_eweight)
492 s->proxy->lbprm.update_server_eweight(s);
493 }
Willy Tarreau2e993902011-10-31 11:53:20 +0100494 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100495 }
Willy Tarreau9580d162012-05-19 19:07:40 +0200496 if (s->proxy->lbprm.set_server_status_up)
497 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100498
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700499 /* If the server is set with "on-marked-up shutdown-backup-sessions",
500 * and it's not a backup server and its effective weight is > 0,
501 * then it can accept new connections, so we shut down all sessions
502 * on all backup servers.
503 */
504 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
505 !(s->state & SRV_BACKUP) && s->eweight)
506 shutdown_backup_sessions(s->proxy, SN_ERR_UP);
507
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100508 /* check if we can handle some connections queued at the proxy. We
509 * will take as many as we can handle.
510 */
511 xferred = check_for_pending(s);
512
David du Colombier7af46052012-05-16 14:16:48 +0200513 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100514
Willy Tarreau45446782012-03-09 17:16:09 +0100515 if (old_state & SRV_MAINTAIN) {
Cyril Bontécd19e512010-01-31 22:34:03 +0100516 chunk_printf(&msg,
517 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
518 s->proxy->id, s->id);
519 } else {
520 chunk_printf(&msg,
521 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
522 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100523
Cyril Bontécd19e512010-01-31 22:34:03 +0100524 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200525 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Cyril Bontécd19e512010-01-31 22:34:03 +0100526 xferred);
527 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100528
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100529 Warning("%s.\n", trash);
530 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100531
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100532 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100533 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100534 if (! (srv->state & SRV_MAINTAIN))
535 /* Only notify tracking servers if they're not in maintenance. */
536 set_server_up(srv);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100537 }
538
539 if (s->health >= s->rise)
540 s->health = s->rise + s->fall - 1; /* OK now */
541
542}
543
544static void set_server_disabled(struct server *s) {
545
546 struct server *srv;
547 struct chunk msg;
548 int xferred;
549
550 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200551 if (s->proxy->lbprm.set_server_status_down)
552 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100553
554 /* we might have sessions queued on this server and waiting for
555 * a connection. Those which are redispatchable will be queued
556 * to another server or to the proxy itself.
557 */
558 xferred = redistribute_pending(s);
559
David du Colombier7af46052012-05-16 14:16:48 +0200560 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100561
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200562 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100563 "Load-balancing on %sServer %s/%s is disabled",
564 s->state & SRV_BACKUP ? "Backup " : "",
565 s->proxy->id, s->id);
566
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200567 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200568 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100569 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100570
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100571 Warning("%s.\n", trash);
572 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100573
574 if (!s->proxy->srv_bck && !s->proxy->srv_act)
575 set_backend_down(s->proxy);
576
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100577 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100578 for(srv = s->tracknext; srv; srv = srv->tracknext)
579 set_server_disabled(srv);
580}
581
582static void set_server_enabled(struct server *s) {
583
584 struct server *srv;
585 struct chunk msg;
586 int xferred;
587
588 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200589 if (s->proxy->lbprm.set_server_status_up)
590 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100591
592 /* check if we can handle some connections queued at the proxy. We
593 * will take as many as we can handle.
594 */
595 xferred = check_for_pending(s);
596
David du Colombier7af46052012-05-16 14:16:48 +0200597 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100598
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200599 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100600 "Load-balancing on %sServer %s/%s is enabled again",
601 s->state & SRV_BACKUP ? "Backup " : "",
602 s->proxy->id, s->id);
603
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200604 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200605 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100606 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100607
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100608 Warning("%s.\n", trash);
609 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100610
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100611 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100612 for(srv = s->tracknext; srv; srv = srv->tracknext)
613 set_server_enabled(srv);
614}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200615
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100616void health_adjust(struct server *s, short status) {
617
618 int failed;
619 int expire;
620
621 /* return now if observing nor health check is not enabled */
622 if (!s->observe || !s->check)
623 return;
624
625 if (s->observe >= HANA_OBS_SIZE)
626 return;
627
628 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
629 return;
630
631 switch (analyze_statuses[status].lr[s->observe - 1]) {
632 case 1:
633 failed = 1;
634 break;
635
636 case 2:
637 failed = 0;
638 break;
639
640 default:
641 return;
642 }
643
644 if (!failed) {
645 /* good: clear consecutive_errors */
646 s->consecutive_errors = 0;
647 return;
648 }
649
650 s->consecutive_errors++;
651
652 if (s->consecutive_errors < s->consecutive_errors_limit)
653 return;
654
655 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
656 s->consecutive_errors, get_analyze_status(status));
657
658 switch (s->onerror) {
659 case HANA_ONERR_FASTINTER:
660 /* force fastinter - nothing to do here as all modes force it */
661 break;
662
663 case HANA_ONERR_SUDDTH:
664 /* simulate a pre-fatal failed health check */
665 if (s->health > s->rise)
666 s->health = s->rise + 1;
667
668 /* no break - fall through */
669
670 case HANA_ONERR_FAILCHK:
671 /* simulate a failed health check */
672 set_server_check_status(s, HCHK_STATUS_HANA, trash);
673
674 if (s->health > s->rise) {
675 s->health--; /* still good */
676 s->counters.failed_checks++;
677 }
678 else
679 set_server_down(s);
680
681 break;
682
683 case HANA_ONERR_MARKDWN:
684 /* mark server down */
685 s->health = s->rise;
686 set_server_check_status(s, HCHK_STATUS_HANA, trash);
687 set_server_down(s);
688
689 break;
690
691 default:
692 /* write a warning? */
693 break;
694 }
695
696 s->consecutive_errors = 0;
697 s->counters.failed_hana++;
698
699 if (s->fastinter) {
700 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
701 if (s->check->expire > expire)
702 s->check->expire = expire;
703 }
704}
705
Willy Tarreauef781042010-01-27 11:53:01 +0100706static int httpchk_build_status_header(struct server *s, char *buffer)
707{
708 int sv_state;
709 int ratio;
710 int hlen = 0;
711 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
712 "UP %d/%d", "UP",
713 "NOLB %d/%d", "NOLB",
714 "no check" };
715
716 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
717 hlen += 24;
718
719 if (!(s->state & SRV_CHECKED))
720 sv_state = 6; /* should obviously never happen */
721 else if (s->state & SRV_RUNNING) {
722 if (s->health == s->rise + s->fall - 1)
723 sv_state = 3; /* UP */
724 else
725 sv_state = 2; /* going down */
726
727 if (s->state & SRV_GOINGDOWN)
728 sv_state += 2;
729 } else {
730 if (s->health)
731 sv_state = 1; /* going up */
732 else
733 sv_state = 0; /* DOWN */
734 }
735
736 hlen += sprintf(buffer + hlen,
737 srv_hlt_st[sv_state],
738 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
739 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
740
741 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
742 s->proxy->id, s->id,
743 global.node,
744 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
745 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
746 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
747 s->nbpend);
748
749 if ((s->state & SRV_WARMINGUP) &&
750 now.tv_sec < s->last_change + s->slowstart &&
751 now.tv_sec >= s->last_change) {
752 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
753 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
754 }
755
756 buffer[hlen++] = '\r';
757 buffer[hlen++] = '\n';
758
759 return hlen;
760}
761
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762/*
763 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200764 * the connection acknowledgement. If the proxy requires L7 health-checks,
765 * it sends the request. In other cases, it calls set_server_check_status()
766 * to set s->check_status, s->check_duration and s->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200767 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200768static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769{
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200770 struct server *s = conn->owner;
771 int fd = conn->t.sock.fd;
772 struct task *t = s->check;
773
774 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH | CO_FL_WAIT_DATA | CO_FL_WAIT_WR)) {
775 conn->flags |= CO_FL_ERROR;
776 return;
777 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200779 if (unlikely(conn->flags & CO_FL_ERROR)) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100780 int skerr, err = errno;
781 socklen_t lskerr = sizeof(skerr);
782
783 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
784 err = skerr;
785
786 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200787 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200788 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200789
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200790 if (conn->flags & CO_FL_HANDSHAKE)
791 return;
Willy Tarreau83749182007-04-15 20:56:27 +0200792
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200793 /* here, we know that the connection is established */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100794 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200796 if (s->proxy->options2 & PR_O2_CHK_ANY) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100798 const char *check_req = s->proxy->check_req;
799 int check_len = s->proxy->check_len;
800
Willy Tarreauf3c69202006-07-09 16:42:34 +0200801 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802 * so we'll send the request, and won't wake the checker up now.
803 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200804
Willy Tarreau1620ec32011-08-06 17:05:02 +0200805 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200806 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200807 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200808 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
809 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200810 else if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK) {
Willy Tarreaue9d87882010-01-27 11:28:42 +0100811 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100812
813 if (s->proxy->options2 & PR_O2_CHK_SNDST)
814 check_len += httpchk_build_status_header(s, trash + check_len);
815
Willy Tarreaue9d87882010-01-27 11:28:42 +0100816 trash[check_len++] = '\r';
817 trash[check_len++] = '\n';
818 trash[check_len] = '\0';
819 check_req = trash;
820 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200821
Willy Tarreaue9d87882010-01-27 11:28:42 +0100822 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
823 if (ret == check_len) {
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200824 if (conn->flags & CO_FL_WAIT_L4_CONN)
825 conn->flags &= ~CO_FL_WAIT_L4_CONN;
826
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100827 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau1e44a492012-05-25 07:41:38 +0200828 if (s->proxy->timeout.check) {
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100829 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreau1e44a492012-05-25 07:41:38 +0200830 task_queue(t);
831 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200832 __conn_data_want_recv(conn); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200833 goto out_nowake;
834 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200835 else if (ret == 0 || errno == EAGAIN)
836 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200837 else {
838 switch (errno) {
839 case ECONNREFUSED:
840 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200841 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200842 break;
843
844 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200845 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200846 }
847
Willy Tarreau6996e152007-04-30 14:37:43 +0200848 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200849 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850 }
851 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200852 /* We have no data to send to check the connection, and
853 * getsockopt() will not inform us whether the connection
854 * is still pending. So we'll reuse connect() to check the
855 * state of the socket. This has the advantage of givig us
856 * the following info :
857 * - error
858 * - connecting (EALREADY, EINPROGRESS)
859 * - connected (EISCONN, 0)
860 */
861
David du Colombier6f5ccb12011-03-10 22:26:24 +0100862 struct sockaddr_storage sa;
Willy Tarreau6996e152007-04-30 14:37:43 +0200863
David du Colombier6f5ccb12011-03-10 22:26:24 +0100864 if (is_addr(&s->check_addr))
865 sa = s->check_addr;
866 else
867 sa = s->addr;
868
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200869 set_host_port(&sa, s->check_port);
Willy Tarreau6996e152007-04-30 14:37:43 +0200870
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200871 if (connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) == 0)
Willy Tarreau6996e152007-04-30 14:37:43 +0200872 errno = 0;
873
874 if (errno == EALREADY || errno == EINPROGRESS)
875 goto out_poll;
876
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200877 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200878 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200879 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200880 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200881
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200882 if (conn->flags & CO_FL_WAIT_L4_CONN)
883 conn->flags &= ~CO_FL_WAIT_L4_CONN;
884
Willy Tarreaubaaee002006-06-26 02:48:02 +0200885 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200886 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200887 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888 }
889 }
Willy Tarreau83749182007-04-15 20:56:27 +0200890 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200891 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200892 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200893 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreau3267d362012-08-17 23:53:56 +0200894 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200895 out_poll:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200896 __conn_data_poll_send(conn);
Willy Tarreau3267d362012-08-17 23:53:56 +0200897 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200898 out_error:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200899 conn->flags |= CO_FL_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200900 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200901}
902
903
904/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200905 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200906 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
907 * set_server_check_status() to update s->check_status, s->check_duration
908 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200909
910 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
911 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
912 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
913 * response to an SSL HELLO (the principle is that this is enough to
914 * distinguish between an SSL server and a pure TCP relay). All other cases will
915 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
916 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200918static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919{
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100920 int len;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200921 struct server *s = conn->owner;
922 int fd = conn->t.sock.fd;
923 struct task *t = s->check;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200924 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100925 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200926 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200927
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200928 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH | CO_FL_WAIT_DATA | CO_FL_WAIT_WR)) {
929 conn->flags |= CO_FL_ERROR;
930 return;
931 }
932
933 if (unlikely((s->result & SRV_CHK_ERROR) || (conn->flags & CO_FL_ERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200934 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200935 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200936 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200937
Willy Tarreau83749182007-04-15 20:56:27 +0200938 goto out_wakeup;
939 }
940
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200941 if (conn->flags & CO_FL_HANDSHAKE)
942 return;
943
Willy Tarreau83749182007-04-15 20:56:27 +0200944 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
945 * but the connection was closed on the remote end. Fortunately, recv still
946 * works correctly and we don't need to do the getsockopt() on linux.
947 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000948
949 /* Set buffer to point to the end of the data already read, and check
950 * that there is free space remaining. If the buffer is full, proceed
951 * with running the checks without attempting another socket read.
952 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000953
Willy Tarreau03938182010-03-17 21:52:07 +0100954 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200955 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
956 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 +0100957 if (len <= 0)
958 break;
959 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000960
Willy Tarreau03938182010-03-17 21:52:07 +0100961 if (len == 0)
962 done = 1; /* connection hangup received */
963 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100964 /* Report network errors only if we got no other data. Otherwise
965 * we'll let the upper layers decide whether the response is OK
966 * or not. It is very common that an RST sent by the server is
967 * reported as an error just after the last data chunk.
968 */
Willy Tarreau03938182010-03-17 21:52:07 +0100969 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100970 if (!s->check_data_len) {
971 if (!(s->result & SRV_CHK_ERROR))
972 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
973 goto out_wakeup;
974 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 }
976
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200977 if (len >= 0 && (conn->flags & CO_FL_WAIT_L4_CONN))
978 conn->flags &= ~CO_FL_WAIT_L4_CONN;
979
Willy Tarreau03938182010-03-17 21:52:07 +0100980 /* Intermediate or complete response received.
981 * Terminate string in check_data buffer.
982 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200983 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100984 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100985 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100986 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100987 done = 1; /* buffer full, don't wait for more data */
988 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200989
Nick Chalk57b1bf72010-03-16 15:50:46 +0000990 /* Run the checks... */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200991 switch (s->proxy->options2 & PR_O2_CHK_ANY) {
992 case PR_O2_HTTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100993 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
994 goto wait_more_data;
995
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100996 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000997 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
998 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
999 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
1000 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
1001 !isdigit((unsigned char) *(s->check_data + 11))) {
1002 cut_crlf(s->check_data);
1003 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001004
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001005 goto out_wakeup;
1006 }
1007
Nick Chalk57b1bf72010-03-16 15:50:46 +00001008 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001009 desc = ltrim(s->check_data + 12, ' ');
1010
Willy Tarreaubd741542010-03-16 18:46:54 +01001011 if ((s->proxy->options & PR_O_DISABLE404) &&
1012 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +00001013 /* 404 may be accepted as "stopping" only if the server was up */
1014 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001015 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001016 }
Willy Tarreaubd741542010-03-16 18:46:54 +01001017 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
1018 /* Run content verification check... We know we have at least 13 chars */
1019 if (!httpchk_expect(s, done))
1020 goto wait_more_data;
1021 }
1022 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
1023 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
1024 cut_crlf(desc);
1025 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1026 }
Nick Chalk57b1bf72010-03-16 15:50:46 +00001027 else {
1028 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001029 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001030 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001031 break;
1032
1033 case PR_O2_SSL3_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001034 if (!done && s->check_data_len < 5)
1035 goto wait_more_data;
1036
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001037 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001038 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001039 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001040 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001041 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001042 break;
1043
1044 case PR_O2_SMTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001045 if (!done && s->check_data_len < strlen("000\r"))
1046 goto wait_more_data;
1047
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001048 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001049 if ((s->check_data_len < strlen("000\r")) ||
1050 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
1051 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
1052 !isdigit((unsigned char) *(s->check_data + 2))) {
1053 cut_crlf(s->check_data);
1054 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001055
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001056 goto out_wakeup;
1057 }
1058
Nick Chalk57b1bf72010-03-16 15:50:46 +00001059 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001060
Nick Chalk57b1bf72010-03-16 15:50:46 +00001061 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001062 cut_crlf(desc);
1063
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001064 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001065 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001066 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001067 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001068 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001069 break;
1070
1071 case PR_O2_PGSQL_CHK:
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001072 if (!done && s->check_data_len < 9)
1073 goto wait_more_data;
1074
1075 if (s->check_data[0] == 'R') {
1076 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1077 }
1078 else {
1079 if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0))
1080 desc = &s->check_data[6];
1081 else
1082 desc = "PostgreSQL unknown error";
1083
1084 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1085 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001086 break;
1087
1088 case PR_O2_REDIS_CHK:
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001089 if (!done && s->check_data_len < 7)
1090 goto wait_more_data;
1091
1092 if (strcmp(s->check_data, "+PONG\r\n") == 0) {
1093 set_server_check_status(s, HCHK_STATUS_L7OKD, "Redis server is ok");
1094 }
1095 else {
1096 set_server_check_status(s, HCHK_STATUS_L7STS, s->check_data);
1097 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001098 break;
1099
1100 case PR_O2_MYSQL_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001101 if (!done && s->check_data_len < 5)
1102 goto wait_more_data;
1103
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001104 if (s->proxy->check_len == 0) { // old mode
1105 if (*(s->check_data + 4) != '\xff') {
1106 /* We set the MySQL Version in description for information purpose
1107 * FIXME : it can be cool to use MySQL Version for other purpose,
1108 * like mark as down old MySQL server.
1109 */
1110 if (s->check_data_len > 51) {
1111 desc = ltrim(s->check_data + 5, ' ');
1112 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1113 }
1114 else {
1115 if (!done)
1116 goto wait_more_data;
1117 /* it seems we have a OK packet but without a valid length,
1118 * it must be a protocol error
1119 */
1120 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1121 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001122 }
1123 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001124 /* An error message is attached in the Error packet */
1125 desc = ltrim(s->check_data + 7, ' ');
1126 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1127 }
1128 } else {
1129 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1130 (((unsigned int) *(s->check_data + 1)) << 8) +
1131 (((unsigned int) *(s->check_data + 2)) << 16);
1132
1133 if (s->check_data_len == first_packet_len + 4) {
1134 /* MySQL Error packet always begin with field_count = 0xff */
1135 if (*(s->check_data + 4) != '\xff') {
1136 /* We have only one MySQL packet and it is a Handshake Initialization packet
1137 * but we need to have a second packet to know if it is alright
1138 */
1139 if (!done && s->check_data_len < first_packet_len + 5)
1140 goto wait_more_data;
1141 }
1142 else {
1143 /* We have only one packet and it is an Error packet,
1144 * an error message is attached, so we can display it
1145 */
1146 desc = &s->check_data[7];
1147 //Warning("onlyoneERR: %s\n", desc);
1148 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1149 }
1150 } else if (s->check_data_len > first_packet_len + 4) {
1151 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1152 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1153 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1154
1155 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1156 /* We have 2 packets and that's good */
1157 /* Check if the second packet is a MySQL Error packet or not */
1158 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1159 /* No error packet */
1160 /* We set the MySQL Version in description for information purpose */
1161 desc = &s->check_data[5];
1162 //Warning("2packetOK: %s\n", desc);
1163 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1164 }
1165 else {
1166 /* An error message is attached in the Error packet
1167 * so we can display it ! :)
1168 */
1169 desc = &s->check_data[first_packet_len+11];
1170 //Warning("2packetERR: %s\n", desc);
1171 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1172 }
1173 }
1174 }
1175 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001176 if (!done)
1177 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001178 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001179 * it must be a protocol error
1180 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001181 desc = &s->check_data[5];
1182 //Warning("protoerr: %s\n", desc);
1183 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001184 }
1185 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001186 break;
1187
1188 case PR_O2_LDAP_CHK:
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001189 if (!done && s->check_data_len < 14)
1190 goto wait_more_data;
1191
1192 /* Check if the server speaks LDAP (ASN.1/BER)
1193 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1194 * http://tools.ietf.org/html/rfc4511
1195 */
1196
1197 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1198 * LDAPMessage: 0x30: SEQUENCE
1199 */
1200 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1201 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1202 }
1203 else {
1204 /* size of LDAPMessage */
1205 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1206
1207 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1208 * messageID: 0x02 0x01 0x01: INTEGER 1
1209 * protocolOp: 0x61: bindResponse
1210 */
1211 if ((msglen > 2) ||
1212 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1213 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1214
1215 goto out_wakeup;
1216 }
1217
1218 /* size of bindResponse */
1219 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1220
1221 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1222 * ldapResult: 0x0a 0x01: ENUMERATION
1223 */
1224 if ((msglen > 4) ||
1225 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1226 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1227
1228 goto out_wakeup;
1229 }
1230
1231 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1232 * resultCode
1233 */
1234 s->check_code = *(s->check_data + msglen + 9);
1235 if (s->check_code) {
1236 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1237 } else {
1238 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1239 }
1240 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001241 break;
1242
1243 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001244 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001245 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001246 break;
1247 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001248
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001249 out_wakeup:
1250 if (s->result & SRV_CHK_ERROR)
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001251 conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001252
Nick Chalk57b1bf72010-03-16 15:50:46 +00001253 /* Reset the check buffer... */
1254 *s->check_data = '\0';
1255 s->check_data_len = 0;
1256
Willy Tarreau03938182010-03-17 21:52:07 +01001257 /* Close the connection... */
1258 shutdown(fd, SHUT_RDWR);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001259 __conn_data_stop_recv(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001260 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001261 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001262
1263 wait_more_data:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001264 __conn_data_poll_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001265}
1266
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001267/*
1268 * This function is used only for server health-checks. It handles connection
1269 * status updates including errors. If necessary, it wakes the check task up.
1270 * It always returns 0.
1271 */
1272static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001273{
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001274 struct server *s = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001275
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001276 if (unlikely(conn->flags & CO_FL_ERROR))
1277 task_wakeup(s->check, TASK_WOKEN_IO);
Willy Tarreau20bea422012-07-06 12:00:49 +02001278
Willy Tarreau3267d362012-08-17 23:53:56 +02001279 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001280}
1281
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001282struct data_cb check_conn_cb = {
1283 .recv = event_srv_chk_r,
1284 .send = event_srv_chk_w,
1285 .wake = wake_srv_chk,
1286};
1287
Willy Tarreaubaaee002006-06-26 02:48:02 +02001288/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001289 * updates the server's weight during a warmup stage. Once the final weight is
1290 * reached, the task automatically stops. Note that any server status change
1291 * must have updated s->last_change accordingly.
1292 */
1293static struct task *server_warmup(struct task *t)
1294{
1295 struct server *s = t->context;
1296
1297 /* by default, plan on stopping the task */
1298 t->expire = TICK_ETERNITY;
1299 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1300 return t;
1301
1302 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1303 /* go to full throttle if the slowstart interval is reached */
1304 s->state &= ~SRV_WARMINGUP;
1305 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1306 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1307 if (s->proxy->lbprm.update_server_eweight)
1308 s->proxy->lbprm.update_server_eweight(s);
1309 }
1310 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1311 /* for dynamic algorithms, let's slowly update the weight */
1312 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1313 s->slowstart - 1) / s->slowstart;
1314 s->eweight *= s->uweight;
1315 if (s->proxy->lbprm.update_server_eweight)
1316 s->proxy->lbprm.update_server_eweight(s);
1317 }
1318 /* Note that static algorithms are already running at full throttle */
1319
1320 /* probably that we can refill this server with a bit more connections */
1321 check_for_pending(s);
1322
1323 /* get back there in 1 second or 1/20th of the slowstart interval,
1324 * whichever is greater, resulting in small 5% steps.
1325 */
1326 if (s->state & SRV_WARMINGUP)
1327 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1328 return t;
1329}
1330
1331/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001332 * manages a server health-check. Returns
1333 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1334 */
Simon Horman63a4a822012-03-19 07:24:41 +09001335static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001336{
Willy Tarreaue3838802009-03-21 18:58:32 +01001337 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001338 struct server *s = t->context;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001339 struct connection *conn = s->check_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001340 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001341 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001342 int ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001343
Willy Tarreaubaaee002006-06-26 02:48:02 +02001344 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001345 if (attempts++ > 0) {
1346 /* we always fail to create a server, let's stop insisting... */
1347 while (tick_is_expired(t->expire, now_ms))
1348 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1349 return t;
1350 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001351
1352 fd = conn->t.sock.fd;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353 if (fd < 0) { /* no check currently running */
Willy Tarreau26c25062009-03-08 09:38:41 +01001354 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1355 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001356
1357 /* we don't send any health-checks when the proxy is stopped or when
1358 * the server should not be checked.
1359 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001360 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001361 while (tick_is_expired(t->expire, now_ms))
1362 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001363 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001364 }
1365
1366 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001367 set_server_check_status(s, HCHK_STATUS_START, NULL);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001368 set_target_server(&conn->target, s);
1369 conn_prepare(conn, &check_conn_cb, s->proto, &raw_sock, s);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001370
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001371 if (is_addr(&s->check_addr))
1372 /* we'll connect to the check addr specified on the server */
1373 conn->addr.to = s->check_addr;
1374 else
1375 /* we'll connect to the addr on the server */
1376 conn->addr.to = s->addr;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001377
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001378 set_host_port(&conn->addr.to, s->check_port);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001379
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001380 /* It can return one of :
1381 * - SN_ERR_NONE if everything's OK
1382 * - SN_ERR_SRVTO if there are no more servers
1383 * - SN_ERR_SRVCL if the connection was refused by the server
1384 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1385 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1386 * - SN_ERR_INTERNAL for any other purely internal errors
1387 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1388 */
1389 ret = tcp_connect_server(conn, 1);
1390 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001391
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001392 switch (ret) {
1393 case SN_ERR_NONE:
1394 break;
1395 case SN_ERR_SRVTO: /* ETIMEDOUT */
1396 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1397 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
1398 break;
1399 case SN_ERR_PRXCOND:
1400 case SN_ERR_RESOURCE:
1401 case SN_ERR_INTERNAL:
1402 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
1403 break;
1404 }
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001405
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001406 if (s->result == SRV_CHK_UNKNOWN) {
1407 /* connection attempt was started */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001408
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001409 /* we allow up to min(inter, timeout.connect) for a connection
1410 * to establish but only when timeout.check is set
1411 * as it may be to short for a full check otherwise
1412 */
1413 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001414
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001415 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1416 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1417 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001418 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001419 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420 }
1421
1422 /* here, we have seen a failure */
1423 if (s->health > s->rise) {
1424 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001425 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426 }
1427 else
1428 set_server_down(s);
1429
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001430 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1431 /* we allow up to min(inter, timeout.connect) for a connection
1432 * to establish but only when timeout.check is set
1433 * as it may be to short for a full check otherwise
1434 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001435 while (tick_is_expired(t->expire, now_ms)) {
1436 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001437
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001438 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1439 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001440
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001441 if (s->proxy->timeout.check)
1442 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001443 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001444 goto new_chk;
1445 }
1446 else {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001447 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001448 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreau48494c02007-11-30 10:41:39 +01001449 /* we may have to add/remove this server from the LB group */
1450 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1451 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001452 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1453 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001454 else if (!(s->state & SRV_GOINGDOWN) &&
1455 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001456 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1457 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001458 }
1459
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001460 if (s->health < s->rise + s->fall - 1) {
1461 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001462
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001463 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001464 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001465 conn->t.sock.fd = -1; /* no check running anymore */
1466 conn_xprt_close(conn);
1467 if (conn->ctrl)
1468 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001469
1470 rv = 0;
1471 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001472 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001473 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001474 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001475 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001476 goto new_chk;
1477 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001478 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001479 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001480 if (conn->flags & CO_FL_WAIT_L4_CONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001481 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001482 } else {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001483 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001484 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001485 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001486 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001487 }
1488 }
1489
Willy Tarreaubaaee002006-06-26 02:48:02 +02001490 /* failure or timeout detected */
1491 if (s->health > s->rise) {
1492 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001493 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001494 }
1495 else
1496 set_server_down(s);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001497 conn->t.sock.fd = -1;
1498 conn_xprt_close(conn);
1499 if (conn->ctrl)
1500 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001501
1502 rv = 0;
1503 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001504 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001505 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001506 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001507 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001508 goto new_chk;
1509 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001510 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001511 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001512 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001513 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001514}
1515
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001516/*
1517 * Start health-check.
1518 * Returns 0 if OK, -1 if error, and prints the error in this case.
1519 */
1520int start_checks() {
1521
1522 struct proxy *px;
1523 struct server *s;
1524 struct task *t;
1525 int nbchk=0, mininter=0, srvpos=0;
1526
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001527 /* 1- count the checkers to run simultaneously.
1528 * We also determine the minimum interval among all of those which
1529 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1530 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001531 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001532 * too short an interval for all others.
1533 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001534 for (px = proxy; px; px = px->next) {
1535 for (s = px->srv; s; s = s->next) {
1536 if (!(s->state & SRV_CHECKED))
1537 continue;
1538
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001539 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1540 (!mininter || mininter > srv_getinter(s)))
1541 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001542
1543 nbchk++;
1544 }
1545 }
1546
1547 if (!nbchk)
1548 return 0;
1549
1550 srand((unsigned)time(NULL));
1551
1552 /*
1553 * 2- start them as far as possible from each others. For this, we will
1554 * start them after their interval set to the min interval divided by
1555 * the number of servers, weighted by the server's position in the list.
1556 */
1557 for (px = proxy; px; px = px->next) {
1558 for (s = px->srv; s; s = s->next) {
Willy Tarreau2e993902011-10-31 11:53:20 +01001559 if (s->slowstart) {
1560 if ((t = task_new()) == NULL) {
1561 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1562 return -1;
1563 }
1564 /* We need a warmup task that will be called when the server
1565 * state switches from down to up.
1566 */
1567 s->warmup = t;
1568 t->process = server_warmup;
1569 t->context = s;
1570 t->expire = TICK_ETERNITY;
1571 }
1572
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001573 if (!(s->state & SRV_CHECKED))
1574 continue;
1575
Willy Tarreau2e993902011-10-31 11:53:20 +01001576 /* one task for the checks */
Willy Tarreaua4613182009-03-21 18:13:21 +01001577 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001578 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1579 return -1;
1580 }
1581
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001582 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001583 t->process = process_chk;
1584 t->context = s;
1585
1586 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001587 t->expire = tick_add(now_ms,
1588 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1589 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001590 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001591 task_queue(t);
1592
1593 srvpos++;
1594 }
1595 }
1596 return 0;
1597}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001598
1599/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001600 * Perform content verification check on data in s->check_data buffer.
1601 * The buffer MUST be terminated by a null byte before calling this function.
1602 * Sets server status appropriately. The caller is responsible for ensuring
1603 * that the buffer contains at least 13 characters. If <done> is zero, we may
1604 * return 0 to indicate that data is required to decide of a match.
1605 */
1606static int httpchk_expect(struct server *s, int done)
1607{
1608 static char status_msg[] = "HTTP status check returned code <000>";
1609 char status_code[] = "000";
1610 char *contentptr;
1611 int crlf;
1612 int ret;
1613
1614 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1615 case PR_O2_EXP_STS:
1616 case PR_O2_EXP_RSTS:
1617 memcpy(status_code, s->check_data + 9, 3);
1618 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1619
1620 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1621 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1622 else
1623 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1624
1625 /* we necessarily have the response, so there are no partial failures */
1626 if (s->proxy->options2 & PR_O2_EXP_INV)
1627 ret = !ret;
1628
1629 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1630 break;
1631
1632 case PR_O2_EXP_STR:
1633 case PR_O2_EXP_RSTR:
1634 /* very simple response parser: ignore CR and only count consecutive LFs,
1635 * stop with contentptr pointing to first char after the double CRLF or
1636 * to '\0' if crlf < 2.
1637 */
1638 crlf = 0;
1639 for (contentptr = s->check_data; *contentptr; contentptr++) {
1640 if (crlf >= 2)
1641 break;
1642 if (*contentptr == '\r')
1643 continue;
1644 else if (*contentptr == '\n')
1645 crlf++;
1646 else
1647 crlf = 0;
1648 }
1649
1650 /* Check that response contains a body... */
1651 if (crlf < 2) {
1652 if (!done)
1653 return 0;
1654
1655 set_server_check_status(s, HCHK_STATUS_L7RSP,
1656 "HTTP content check could not find a response body");
1657 return 1;
1658 }
1659
1660 /* Check that response body is not empty... */
1661 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001662 if (!done)
1663 return 0;
1664
Willy Tarreaubd741542010-03-16 18:46:54 +01001665 set_server_check_status(s, HCHK_STATUS_L7RSP,
1666 "HTTP content check found empty response body");
1667 return 1;
1668 }
1669
1670 /* Check the response content against the supplied string
1671 * or regex... */
1672 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1673 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1674 else
1675 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1676
1677 /* if we don't match, we may need to wait more */
1678 if (!ret && !done)
1679 return 0;
1680
1681 if (ret) {
1682 /* content matched */
1683 if (s->proxy->options2 & PR_O2_EXP_INV)
1684 set_server_check_status(s, HCHK_STATUS_L7RSP,
1685 "HTTP check matched unwanted content");
1686 else
1687 set_server_check_status(s, HCHK_STATUS_L7OKD,
1688 "HTTP content check matched");
1689 }
1690 else {
1691 if (s->proxy->options2 & PR_O2_EXP_INV)
1692 set_server_check_status(s, HCHK_STATUS_L7OKD,
1693 "HTTP check did not match unwanted content");
1694 else
1695 set_server_check_status(s, HCHK_STATUS_L7RSP,
1696 "HTTP content check did not match");
1697 }
1698 break;
1699 }
1700 return 1;
1701}
1702
1703/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001704 * Local variables:
1705 * c-indent-level: 8
1706 * c-basic-offset: 8
1707 * End:
1708 */